Here are some sample JUnit tests for the EPSServiceGrpc class:
java Copy code
import static org.mockito.Mockito.*;
import io.grpc.stub.StreamObserver;
import org.junit.Before;
import org.junit.Test;
public class EPSServiceGrpcTest {
private EPSServiceAPIMetrics mockMetrics;
private EPSService mockServiceImpl;
private EPSServiceGrpc grpcService;
@Before
public void setUp() {
mockMetrics = mock(EPSServiceAPIMetrics.class);
mockServiceImpl = mock(EPSService.class);
grpcService = new EPSServiceGrpc(mockServiceImpl, mockMetrics);
}
@Test
public void testConnectSuccess() {
DataEpsServiceProto.ConnectRequest request = DataEpsServiceProto.ConnectRequest.newBuilder()
.setConnectorName("connector")
.putRequestInfoMap("key1", "value1")
.putRequestInfoMap("key2", "value2")
.build();
StreamObserver<DataEpsServiceProto.ConnectResponse> mockResponseObserver = mock(StreamObserver.class);
ExtractorInfo mockExtractorInfo = mock(ExtractorInfo.class);
when(mockServiceImpl.connect("connector", request.getRequestInfoMap())).thenReturn(mockExtractorInfo);
grpcService.connect(request, mockResponseObserver);
verify(mockMetrics.getHitCount()).incrementAndGet();
verify(mockResponseObserver).onNext(DataEpsServiceProto.ConnectResponse.newBuilder().setIsSuccess(true).build());
verify(mockResponseObserver).onCompleted();
verifyNoMoreInteractions(mockMetrics, mockResponseObserver);
}
@Test
public void testConnectException() {
DataEpsServiceProto.ConnectRequest request = DataEpsServiceProto.ConnectRequest.newBuilder()
.setConnectorName("connector")
.putRequestInfoMap("key1", "value1")
.putRequestInfoMap("key2", "value2")
.build();
StreamObserver<DataEpsServiceProto.ConnectResponse> mockResponseObserver = mock(StreamObserver.class);
Exception mockException = mock(Exception.class);
when(mockServiceImpl.connect("connector", request.getRequestInfoMap())).thenThrow(mockException);
grpcService.connect(request, mockResponseObserver);
verify(mockMetrics.invalidCount()).incrementAndGet();
verify(mockResponseObserver).onError(any());
verifyNoMoreInteractions(mockMetrics, mockResponseObserver);
}
}
In the first test case (testConnectSuccess()), it verifies that the connect() method increments the hit count metric, calls the connect() method of the service implementation, and sends a success response to the response observer.
In the second test case (testConnectException()), it verifies that when an exception occurs during the connect() method of the service implementation, the invalid count metric is incremented and an error is sent to the response observer.