diff options
Diffstat (limited to 'prh-app-server/src/test')
4 files changed, 128 insertions, 190 deletions
diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskImplTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskImplTest.java index 112b6960..570cb2a0 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskImplTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskImplTest.java @@ -33,6 +33,7 @@ import java.util.Optional; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import org.onap.dcaegen2.services.prh.config.AAIClientConfiguration; import org.onap.dcaegen2.services.prh.config.ImmutableAAIClientConfiguration; import org.onap.dcaegen2.services.prh.configuration.AppConfig; @@ -81,91 +82,66 @@ class AAIConsumerTaskImplTest { } @Test - public void whenPassedObjectDoesntFit_ThrowsPrhTaskException() throws IOException { - //given - Object response = null; - - //when + public void whenPassedObjectDoesntFit_ThrowsPrhTaskException() { + //given/when when(appConfig.getAAIClientConfiguration()).thenReturn(aaiClientConfiguration); - try { - aaiConsumerTask = new AAIConsumerTaskImpl(appConfig); - response = aaiConsumerTask.execute("Some string"); - } catch (PrhTaskException e) { - e.printStackTrace(); - } + aaiConsumerTask = new AAIConsumerTaskImpl(appConfig); + Executable executableCode = () -> aaiConsumerTask.execute(null); //then - Assertions.assertNull(response); + Assertions + .assertThrows(PrhTaskException.class, executableCode, "Passing wrong object type to execute function"); + } @Test public void whenPassedObjectFits_ReturnsCorrectStatus() throws PrhTaskException, IOException { - //given - Object response; - aaiConsumerClient = mock(AAIConsumerClient.class); - - //when - when(aaiConsumerClient.getHttpResponse(consumerDmaapModel)).thenReturn(Optional.of("200")); - when(appConfig.getAAIClientConfiguration()).thenReturn(aaiClientConfiguration); - aaiConsumerTask = spy(new AAIConsumerTaskImpl(appConfig)); - when(aaiConsumerTask.resolveConfiguration()).thenReturn(aaiClientConfiguration); - doReturn(aaiConsumerClient).when(aaiConsumerTask).resolveClient(); - aaiConsumerTask.setAAIClientConfig(); - response = aaiConsumerTask.execute(consumerDmaapModel); + //given/when + getAAIConsumerTask_WhenMockingHttpResponseCode("200", false); + String response = aaiConsumerTask.execute(consumerDmaapModel); //then verify(aaiConsumerClient, times(1)).getHttpResponse(any(ConsumerDmaapModel.class)); verifyNoMoreInteractions(aaiConsumerClient); - Assertions.assertNotNull(response); - Assertions.assertEquals(Optional.of("200"), response); - + Assertions.assertEquals("200", response); } @Test - public void whenPassedObjectFits_butIncorrectResponseReturns() throws IOException { - //given - Object response = null; - aaiConsumerClient = mock(AAIConsumerClient.class); - //when - when(aaiConsumerClient.getHttpResponse(consumerDmaapModel)).thenReturn(Optional.of("400")); - when(appConfig.getAAIClientConfiguration()).thenReturn(aaiClientConfiguration); - aaiConsumerTask = spy(new AAIConsumerTaskImpl(appConfig)); - when(aaiConsumerTask.resolveConfiguration()).thenReturn(aaiClientConfiguration); - doReturn(aaiConsumerClient).when(aaiConsumerTask).resolveClient(); - aaiConsumerTask.setAAIClientConfig(); - try { - response = aaiConsumerTask.execute(consumerDmaapModel); - } catch (PrhTaskException e) { - e.printStackTrace(); - } + public void whenPassedObjectFits_butIncorrectResponseReturns() throws IOException, AAINotFoundException { + //given/when + getAAIConsumerTask_WhenMockingHttpResponseCode("400", false); + String response = aaiConsumerTask.execute(consumerDmaapModel); //then verify(aaiConsumerClient, times(1)).getHttpResponse(any(ConsumerDmaapModel.class)); verifyNoMoreInteractions(aaiConsumerClient); - Assertions.assertEquals(Optional.of("400"), response); + Assertions.assertEquals("400", response); } @Test - public void whenPassedObjectFits_ThrowsIOException() throws IOException { - //given - Object response = null; + public void whenPassedObjectFits_ThrowsIOExceptionAndHandleIt() throws IOException { + //given/when + getAAIConsumerTask_WhenMockingHttpResponseCode(null, true); + Executable executableCode = () -> aaiConsumerTask.execute(any(ConsumerDmaapModel.class)); + Assertions + .assertThrows(PrhTaskException.class, executableCode, "HttpClient throws IOException"); + + //then + verifyNoMoreInteractions(aaiConsumerClient); + } + + + private static void getAAIConsumerTask_WhenMockingHttpResponseCode(String httpResponseCode, boolean throwsException) + throws IOException { aaiConsumerClient = mock(AAIConsumerClient.class); - //when + if (throwsException) { + when(aaiConsumerClient.getHttpResponse(consumerDmaapModel)).thenThrow(IOException.class); + } else { + when(aaiConsumerClient.getHttpResponse(consumerDmaapModel)).thenReturn(Optional.of(httpResponseCode)); + } when(appConfig.getAAIClientConfiguration()).thenReturn(aaiClientConfiguration); - when(aaiConsumerClient.getHttpResponse(consumerDmaapModel)).thenThrow(IOException.class); aaiConsumerTask = spy(new AAIConsumerTaskImpl(appConfig)); when(aaiConsumerTask.resolveConfiguration()).thenReturn(aaiClientConfiguration); doReturn(aaiConsumerClient).when(aaiConsumerTask).resolveClient(); - aaiConsumerTask.setAAIClientConfig(); - try { - response = aaiConsumerTask.execute(consumerDmaapModel); - } catch (AAINotFoundException e) { - e.printStackTrace(); - } - - //then - verify(aaiConsumerClient, times(1)).getHttpResponse(any(ConsumerDmaapModel.class)); - verifyNoMoreInteractions(aaiConsumerClient); - Assertions.assertNull(response); } }
\ No newline at end of file diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImplTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImplTest.java index 40fd20af..c82a98e4 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImplTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImplTest.java @@ -34,13 +34,14 @@ import java.util.Optional; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import org.onap.dcaegen2.services.prh.config.AAIClientConfiguration; import org.onap.dcaegen2.services.prh.config.ImmutableAAIClientConfiguration; +import org.onap.dcaegen2.services.prh.exceptions.PrhTaskException; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.prh.model.ImmutableConsumerDmaapModel; import org.onap.dcaegen2.services.prh.configuration.AppConfig; import org.onap.dcaegen2.services.prh.exceptions.AAINotFoundException; -import org.onap.dcaegen2.services.prh.exceptions.PrhTaskException; import org.onap.dcaegen2.services.prh.service.AAIProducerClient; /** @@ -48,8 +49,6 @@ import org.onap.dcaegen2.services.prh.service.AAIProducerClient; */ class AAIProducerTaskImplTest { - private static ConsumerDmaapModel consumerDmaapModel; - private static AAIProducerTaskImpl aaiProducerTask; private static final String AAI_HOST = "/aai/v11/network/pnfs/pnf/NOKQTFCOC540002E"; private static final Integer PORT = 1234; @@ -58,6 +57,8 @@ class AAIProducerTaskImplTest { private static final String BASE_PATH = "/aai/v11"; private static final String PNF_PATH = "/network/pnfs/pnf"; + private static ConsumerDmaapModel consumerDmaapModel; + private static AAIProducerTaskImpl aaiProducerTask; private static AAIClientConfiguration aaiClientConfiguration; private static AAIProducerClient aaiProducerClient; private static AppConfig appConfig; @@ -82,91 +83,68 @@ class AAIProducerTaskImplTest { } @Test - public void whenPassedObjectDoesntFit_ThrowsPrhTaskException() throws IOException { - //given - Object response = null; - - //when + public void whenPassedObjectDoesntFit_ThrowsPrhTaskException() { + //given/when/ when(appConfig.getAAIClientConfiguration()).thenReturn(aaiClientConfiguration); - try { - aaiProducerTask = new AAIProducerTaskImpl(appConfig); - response = aaiProducerTask.execute("Some string"); - } catch (PrhTaskException e) { - e.printStackTrace(); - } + aaiProducerTask = new AAIProducerTaskImpl(appConfig); + Executable executableCode = () -> aaiProducerTask.execute(null); //then - Assertions.assertNull(response); + Assertions + .assertThrows(PrhTaskException.class, executableCode, "Passing wrong object type to execute function"); } @Test public void whenPassedObjectFits_ReturnsCorrectStatus() throws AAINotFoundException, IOException { - //given - Object response; - aaiProducerClient = mock(AAIProducerClient.class); - - //when - when(aaiProducerClient.getHttpResponse(consumerDmaapModel)).thenReturn(Optional.of(200)); - when(appConfig.getAAIClientConfiguration()).thenReturn(aaiClientConfiguration); - aaiProducerTask = spy(new AAIProducerTaskImpl(appConfig)); - when(aaiProducerTask.resolveConfiguration()).thenReturn(aaiClientConfiguration); - doReturn(aaiProducerClient).when(aaiProducerTask).resolveClient(); - aaiProducerTask.setAAIClientConfig(); - response = aaiProducerTask.execute(consumerDmaapModel); + //given/when + getAAIProducerTask_whenMockingResponseObject(200, false); + ConsumerDmaapModel response = aaiProducerTask.execute(consumerDmaapModel); //then verify(aaiProducerClient, times(1)).getHttpResponse(any(ConsumerDmaapModel.class)); verifyNoMoreInteractions(aaiProducerClient); - Assertions.assertNotNull(response); Assertions.assertEquals(consumerDmaapModel, response); } + @Test public void whenPassedObjectFits_butIncorrectResponseReturns() throws IOException { - //given - Object response = null; - aaiProducerClient = mock(AAIProducerClient.class); - //when - when(aaiProducerClient.getHttpResponse(consumerDmaapModel)).thenReturn(Optional.of(400)); - when(appConfig.getAAIClientConfiguration()).thenReturn(aaiClientConfiguration); - aaiProducerTask = spy(new AAIProducerTaskImpl(appConfig)); - when(aaiProducerTask.resolveConfiguration()).thenReturn(aaiClientConfiguration); - doReturn(aaiProducerClient).when(aaiProducerTask).resolveClient(); - aaiProducerTask.setAAIClientConfig(); - try { - response = aaiProducerTask.execute(consumerDmaapModel); - } catch (AAINotFoundException e) { - e.printStackTrace(); - } - + //given/when + getAAIProducerTask_whenMockingResponseObject(400, false); + Executable executableCode = () -> aaiProducerTask.execute(consumerDmaapModel); + Assertions + .assertThrows(PrhTaskException.class, executableCode, "Incorrect status code in response message"); //then verify(aaiProducerClient, times(1)).getHttpResponse(any(ConsumerDmaapModel.class)); verifyNoMoreInteractions(aaiProducerClient); - Assertions.assertNull(response); } @Test - public void whenPassedObjectFits_ThrowsIOException() throws IOException { - //given - Object response = null; + public void whenPassedObjectFits_butHTTPClientThrowsIOExceptionHandleIt() throws IOException { + //given/when + getAAIProducerTask_whenMockingResponseObject(0, true); + + Executable executableCode = () -> aaiProducerTask.execute(consumerDmaapModel); + Assertions + .assertThrows(PrhTaskException.class, executableCode, ""); + //then + verify(aaiProducerClient, times(1)).getHttpResponse(any(ConsumerDmaapModel.class)); + verifyNoMoreInteractions(aaiProducerClient); + } + + + private static void getAAIProducerTask_whenMockingResponseObject(int statusCode, boolean throwsException) + throws IOException { aaiProducerClient = mock(AAIProducerClient.class); - //when + if (throwsException) { + when(aaiProducerClient.getHttpResponse(consumerDmaapModel)).thenThrow(IOException.class); + } else { + when(aaiProducerClient.getHttpResponse(consumerDmaapModel)).thenReturn(Optional.of(statusCode)); + } when(appConfig.getAAIClientConfiguration()).thenReturn(aaiClientConfiguration); - when(aaiProducerClient.getHttpResponse(consumerDmaapModel)).thenThrow(IOException.class); aaiProducerTask = spy(new AAIProducerTaskImpl(appConfig)); when(aaiProducerTask.resolveConfiguration()).thenReturn(aaiClientConfiguration); doReturn(aaiProducerClient).when(aaiProducerTask).resolveClient(); - aaiProducerTask.setAAIClientConfig(); - try { - response = aaiProducerTask.execute(consumerDmaapModel); - } catch (AAINotFoundException e) { - e.printStackTrace(); - } - - //then - verify(aaiProducerClient, times(1)).getHttpResponse(any(ConsumerDmaapModel.class)); - verifyNoMoreInteractions(aaiProducerClient); - Assertions.assertNull(response); } }
\ No newline at end of file diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImplTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImplTest.java index 786c04f9..373fc28a 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImplTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImplTest.java @@ -31,6 +31,7 @@ import java.util.Optional; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration; import org.onap.dcaegen2.services.prh.config.ImmutableDmaapConsumerConfiguration; import org.onap.dcaegen2.services.prh.configuration.AppConfig; @@ -49,7 +50,7 @@ class DmaapConsumerTaskImplTest { private static ExtendedDmaapConsumerHttpClientImpl extendedDmaapConsumerHttpClient; private static AppConfig appConfig; private static DmaapConsumerConfiguration dmaapConsumerConfiguration; - + private static String message; @BeforeAll public static void setUp() { @@ -62,56 +63,40 @@ class DmaapConsumerTaskImplTest { .ipv6("0:0:0:0:0:FFFF:0A10:7BEA") .pnfName("NOKQTFCOC540002E").build(); appConfig = mock(AppConfig.class); + message = + "{\"event\":{\"commonEventHeader\":{\"domain\":\"other\",\"eventId\":\"<<SerialNumber>>-reg\",\"eventName\"" + + ":\"pnfRegistration_5GDU\",\"eventType\":\"pnfRegistration\",\"internalHeaderFields\":{}," + + "\"lastEpochMicrosec\":1519837825682,\"nfNamingCode\":\"5GRAN\",\"nfcNamingCode\":\"5DU\"," + + "\"priority\":\"Normal\",\"reportingEntityName\":\"5GRAN_DU\",\"sequence\":0,\"sourceId\":" + + "\"<<SerialNumber>>\",\"sourceName\":\"5GRAN_DU\",\"startEpochMicrosec\":1519837825682,\"version\":3}," + + "\"otherFields\":{\"otherFieldsVersion\":1,\"pnfFamily\":\"BBU\",\"pnfLastServiceDate\":1517206400," + + "\"pnfManufactureDate\":1516406400,\"pnfModelNumber\":\"AJ02\",\"pnfOamIpv4Address\":\"10.16.123.234\"," + + "\"pnfOamIpv6Address\":\"0:0:0:0:0:FFFF:0A10:7BEA\",\"pnfSerialNumber\":\"QTFCOC540002E\"," + + "\"pnfSoftwareVersion\":\"v4.5.0.1\",\"pnfType\":\"AirScale\",\"pnfVendorName\":\"Nokia\"}}}"; } @Test - public void whenPassedObjectDoesntFit_DoNotThrowsPrhTaskException() { + public void whenPassedObjectDoesntFit_DoesNotThrowPrhTaskException() { //given - Object response = null; - extendedDmaapConsumerHttpClient = mock(ExtendedDmaapConsumerHttpClientImpl.class); + prepareMocksForDmaapConsumer(Optional.empty()); //when - when(extendedDmaapConsumerHttpClient.getHttpConsumerResponse()).thenReturn(Optional.empty()); - when(appConfig.getDmaapConsumerConfiguration()).thenReturn(dmaapConsumerConfiguration); - dmaapConsumerTask = spy(new DmaapConsumerTaskImpl(appConfig)); - when(dmaapConsumerTask.resolveConfiguration()).thenReturn(dmaapConsumerConfiguration); - doReturn(extendedDmaapConsumerHttpClient).when(dmaapConsumerTask).resolveClient(); - dmaapConsumerTask.setDmaapClientConfig(); - try { - response = dmaapConsumerTask.execute(consumerDmaapModel); - } catch (PrhTaskException e) { - e.printStackTrace(); - } + Executable executableFunction = () -> dmaapConsumerTask.execute("Sample input"); //then + Assertions + .assertThrows(PrhTaskException.class, executableFunction, + "Throwing exception when http response code won't fit to assignment range"); verify(extendedDmaapConsumerHttpClient, times(1)).getHttpConsumerResponse(); verifyNoMoreInteractions(extendedDmaapConsumerHttpClient); - Assertions.assertNull(response); } @Test public void whenPassedObjectFits_ReturnsCorrectResponse() throws PrhTaskException { //given - Object response; - extendedDmaapConsumerHttpClient = mock(ExtendedDmaapConsumerHttpClientImpl.class); - String message = - "{\"event\":{\"commonEventHeader\":{\"domain\":\"other\",\"eventId\":\"<<SerialNumber>>-reg\",\"eventName\"" - + ":\"pnfRegistration_5GDU\",\"eventType\":\"pnfRegistration\",\"internalHeaderFields\":{}," - + "\"lastEpochMicrosec\":1519837825682,\"nfNamingCode\":\"5GRAN\",\"nfcNamingCode\":\"5DU\"," - + "\"priority\":\"Normal\",\"reportingEntityName\":\"5GRAN_DU\",\"sequence\":0,\"sourceId\":" - + "\"<<SerialNumber>>\",\"sourceName\":\"5GRAN_DU\",\"startEpochMicrosec\":1519837825682,\"version\":3}," - + "\"otherFields\":{\"otherFieldsVersion\":1,\"pnfFamily\":\"BBU\",\"pnfLastServiceDate\":1517206400," - + "\"pnfManufactureDate\":1516406400,\"pnfModelNumber\":\"AJ02\",\"pnfOamIpv4Address\":\"10.16.123.234\"," - + "\"pnfOamIpv6Address\":\"0:0:0:0:0:FFFF:0A10:7BEA\",\"pnfSerialNumber\":\"QTFCOC540002E\"," - + "\"pnfSoftwareVersion\":\"v4.5.0.1\",\"pnfType\":\"AirScale\",\"pnfVendorName\":\"Nokia\"}}}"; + prepareMocksForDmaapConsumer(Optional.of(message)); //when - when(extendedDmaapConsumerHttpClient.getHttpConsumerResponse()).thenReturn(Optional.of(message)); - when(appConfig.getDmaapConsumerConfiguration()).thenReturn(dmaapConsumerConfiguration); - dmaapConsumerTask = spy(new DmaapConsumerTaskImpl(appConfig)); - when(dmaapConsumerTask.resolveConfiguration()).thenReturn(dmaapConsumerConfiguration); - doReturn(extendedDmaapConsumerHttpClient).when(dmaapConsumerTask).resolveClient(); - dmaapConsumerTask.setDmaapClientConfig(); - response = dmaapConsumerTask.execute(consumerDmaapModel); + ConsumerDmaapModel response = dmaapConsumerTask.execute("Sample input"); //then verify(extendedDmaapConsumerHttpClient, times(1)).getHttpConsumerResponse(); @@ -120,4 +105,13 @@ class DmaapConsumerTaskImplTest { Assertions.assertEquals(consumerDmaapModel, response); } + + private void prepareMocksForDmaapConsumer(Optional<String> message) { + extendedDmaapConsumerHttpClient = mock(ExtendedDmaapConsumerHttpClientImpl.class); + when(extendedDmaapConsumerHttpClient.getHttpConsumerResponse()).thenReturn(message); + when(appConfig.getDmaapConsumerConfiguration()).thenReturn(dmaapConsumerConfiguration); + dmaapConsumerTask = spy(new DmaapConsumerTaskImpl(appConfig)); + when(dmaapConsumerTask.resolveConfiguration()).thenReturn(dmaapConsumerConfiguration); + doReturn(extendedDmaapConsumerHttpClient).when(dmaapConsumerTask).resolveClient(); + } }
\ No newline at end of file diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImplTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImplTest.java index e4609193..41d46f66 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImplTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImplTest.java @@ -28,11 +28,11 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; -import java.io.IOException; import java.util.Optional; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration; import org.onap.dcaegen2.services.prh.config.ImmutableDmaapPublisherConfiguration; import org.onap.dcaegen2.services.prh.configuration.AppConfig; @@ -66,67 +66,57 @@ class DmaapPublisherTaskImplTest { } @Test - public void whenPassedObjectDoesntFit_ThrowsPrhTaskException() throws IOException { + public void whenPassedObjectDoesntFit_ThrowsPrhTaskException() { //given - Object response = null; + when(appConfig.getDmaapPublisherConfiguration()).thenReturn(dmaapPublisherConfiguration); + dmaapPublisherTask = new DmaapPublisherTaskImpl(appConfig); //when - when(appConfig.getDmaapPublisherConfiguration()).thenReturn(dmaapPublisherConfiguration); - try { - dmaapPublisherTask = new DmaapPublisherTaskImpl(appConfig); - response = dmaapPublisherTask.execute(""); - } catch (PrhTaskException e) { - e.printStackTrace(); - } + Executable executableFunction = () -> dmaapPublisherTask.execute(null); //then - Assertions.assertNull(response); + Assertions + .assertThrows(PrhTaskException.class, executableFunction, "The specified parameter is incorrect"); } @Test public void whenPassedObjectFits_ReturnsCorrectStatus() throws PrhTaskException { //given - Object response; - extendedDmaapProducerHttpClient = mock(ExtendedDmaapProducerHttpClientImpl.class); + prepareMocksForTests(HttpStatus.OK.toString()); //when - when(extendedDmaapProducerHttpClient.getHttpProducerResponse(consumerDmaapModel)) - .thenReturn(Optional.of(HttpStatus.OK.toString())); - when(appConfig.getDmaapPublisherConfiguration()).thenReturn(dmaapPublisherConfiguration); - dmaapPublisherTask = spy(new DmaapPublisherTaskImpl(appConfig)); - when(dmaapPublisherTask.resolveConfiguration()).thenReturn(dmaapPublisherConfiguration); - doReturn(extendedDmaapProducerHttpClient).when(dmaapPublisherTask).resolveClient(); - response = dmaapPublisherTask.execute(consumerDmaapModel); + String response = dmaapPublisherTask.execute(consumerDmaapModel); //then verify(extendedDmaapProducerHttpClient, times(1)) .getHttpProducerResponse(any(ConsumerDmaapModel.class)); verifyNoMoreInteractions(extendedDmaapProducerHttpClient); - Assertions.assertNotNull(response); Assertions.assertEquals(HttpStatus.OK.toString(), response); } @Test public void whenPassedObjectFits_butIncorrectResponseReturns() { //given - Object response = null; - extendedDmaapProducerHttpClient = mock(ExtendedDmaapProducerHttpClientImpl.class); + prepareMocksForTests("400"); + //when + Executable executableFunction = () -> dmaapPublisherTask.execute(consumerDmaapModel); + + //then + Assertions + .assertThrows(PrhTaskException.class, executableFunction, "Incorrect response from DMAAP"); + verify(extendedDmaapProducerHttpClient, times(1)).getHttpProducerResponse(any(ConsumerDmaapModel.class)); + verifyNoMoreInteractions(extendedDmaapProducerHttpClient); + } + + + private void prepareMocksForTests(String httpResponseCode) { + extendedDmaapProducerHttpClient = mock(ExtendedDmaapProducerHttpClientImpl.class); when(extendedDmaapProducerHttpClient.getHttpProducerResponse(consumerDmaapModel)) - .thenReturn(Optional.of("400")); + .thenReturn(Optional.of(httpResponseCode)); when(appConfig.getDmaapPublisherConfiguration()).thenReturn(dmaapPublisherConfiguration); dmaapPublisherTask = spy(new DmaapPublisherTaskImpl(appConfig)); when(dmaapPublisherTask.resolveConfiguration()).thenReturn(dmaapPublisherConfiguration); doReturn(extendedDmaapProducerHttpClient).when(dmaapPublisherTask).resolveClient(); - try { - response = dmaapPublisherTask.execute(consumerDmaapModel); - } catch (PrhTaskException e) { - e.printStackTrace(); - } - - //then - verify(extendedDmaapProducerHttpClient, times(1)).getHttpProducerResponse(any(ConsumerDmaapModel.class)); - verifyNoMoreInteractions(extendedDmaapProducerHttpClient); - Assertions.assertNull(response); } }
\ No newline at end of file |