From 83df6e1df5ec20627c85af9ba2f49036dd58f328 Mon Sep 17 00:00:00 2001 From: pwielebs Date: Tue, 4 Sep 2018 09:29:49 +0200 Subject: Refatoring due to prh workflow 1. Added specified HttpClient for DmaaPPublisher: *DmaaP Handle transfer-encoding: chunk header and reject the request if it will be set by the client. In conclusion no other reactive http client can be used for pushing something to dmaap. 2. Added sll support to A&AI rective webclient. *Behaviour of reactive A&AI HttpClient is different as in native spring have without it. 3. Added 10s fixed time in PRH for requesting DmaaP. 4. Added debug log in reactive/native http clients. 5. Fixed reactive workflow of prh. 6. Updated the version of: * spring-boot-dependencies:2.0.1.RELEASE->2.0.4.RELEASE * spring-boot-starter-reactor-netty:2.0.2.RELEASE->2.0.4.RELEASE * spring-webflux:5.0.5.RELEASE->5.0.8.RELEASE * reactor-bom:Bismuth-RELEASE->Bismuth-SR10 Change-Id: I815ffb5bdcf48d94f3b7c64040a73e98e404a5e8 Issue-ID: DCAEGEN2-743 Signed-off-by: pwielebs --- .../prh/tasks/AaiProducerTaskImplTest.java | 53 ++++++++++++---------- .../services/prh/tasks/AaiPublisherTaskSpy.java | 10 ++-- .../prh/tasks/DmaapPublisherTaskImplTest.java | 42 +++++++++-------- 3 files changed, 57 insertions(+), 48 deletions(-) (limited to 'prh-app-server/src/test') 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 54259397..f5cc6b24 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 @@ -29,8 +29,9 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; +import javax.net.ssl.SSLException; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.function.Executable; import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration; @@ -40,6 +41,8 @@ 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.service.producer.AaiProducerReactiveHttpClient; +import org.springframework.http.HttpStatus; +import org.springframework.web.reactive.function.client.ClientResponse; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @@ -56,14 +59,16 @@ 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 AaiProducerReactiveHttpClient aaiProducerReactiveHttpClient; - private static AppConfig appConfig; + private ConsumerDmaapModel consumerDmaapModel; + private AaiProducerTaskImpl aaiProducerTask; + private AaiClientConfiguration aaiClientConfiguration; + private AaiProducerReactiveHttpClient aaiProducerReactiveHttpClient; + private AppConfig appConfig; + private ClientResponse clientResponse; - @BeforeAll - static void setUp() { + @BeforeEach + void setUp() { + clientResponse = mock(ClientResponse.class); aaiClientConfiguration = new ImmutableAaiClientConfiguration.Builder() .aaiHost(AAI_HOST) .aaiPort(PORT) @@ -81,17 +86,6 @@ class AaiProducerTaskImplTest { } - private static void getAaiProducerTask_whenMockingResponseObject(Integer statusCode) { - //given - aaiProducerReactiveHttpClient = mock(AaiProducerReactiveHttpClient.class); - when(aaiProducerReactiveHttpClient.getAaiProducerResponse(any())) - .thenReturn(Mono.just(statusCode)); - when(appConfig.getAaiClientConfiguration()).thenReturn(aaiClientConfiguration); - aaiProducerTask = spy(new AaiProducerTaskImpl(appConfig)); - when(aaiProducerTask.resolveConfiguration()).thenReturn(aaiClientConfiguration); - doReturn(aaiProducerReactiveHttpClient).when(aaiProducerTask).resolveClient(); - } - @Test void whenPassedObjectDoesntFit_ThrowsPrhTaskException() { //given/when/ @@ -105,10 +99,10 @@ class AaiProducerTaskImplTest { } @Test - void whenPassedObjectFits_ReturnsCorrectStatus() throws PrhTaskException { + void whenPassedObjectFits_ReturnsCorrectStatus() throws PrhTaskException, SSLException { //given/when getAaiProducerTask_whenMockingResponseObject(200); - Mono response = aaiProducerTask.execute(Mono.just(consumerDmaapModel)); + Mono response = aaiProducerTask.execute(consumerDmaapModel); //then verify(aaiProducerReactiveHttpClient, times(1)).getAaiProducerResponse(any()); @@ -118,13 +112,26 @@ class AaiProducerTaskImplTest { } @Test - void whenPassedObjectFits_butIncorrectResponseReturns() throws PrhTaskException { + void whenPassedObjectFits_butIncorrectResponseReturns() throws PrhTaskException, SSLException { //given/when getAaiProducerTask_whenMockingResponseObject(400); - StepVerifier.create(aaiProducerTask.execute(Mono.just(consumerDmaapModel))).expectSubscription() + StepVerifier.create(aaiProducerTask.execute(consumerDmaapModel)).expectSubscription() .expectError(PrhTaskException.class).verify(); //then verify(aaiProducerReactiveHttpClient, times(1)).getAaiProducerResponse(any()); verifyNoMoreInteractions(aaiProducerReactiveHttpClient); } + + private void getAaiProducerTask_whenMockingResponseObject(int statusCode) throws SSLException { + //given + doReturn(HttpStatus.valueOf(statusCode)).when(clientResponse).statusCode(); + Mono clientResponseMono = Mono.just(clientResponse); + aaiProducerReactiveHttpClient = mock(AaiProducerReactiveHttpClient.class); + when(aaiProducerReactiveHttpClient.getAaiProducerResponse(any())) + .thenReturn(clientResponseMono); + when(appConfig.getAaiClientConfiguration()).thenReturn(aaiClientConfiguration); + aaiProducerTask = spy(new AaiProducerTaskImpl(appConfig)); + when(aaiProducerTask.resolveConfiguration()).thenReturn(aaiClientConfiguration); + doReturn(aaiProducerReactiveHttpClient).when(aaiProducerTask).resolveClient(); + } } \ No newline at end of file diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AaiPublisherTaskSpy.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AaiPublisherTaskSpy.java index 82dcdae9..231bf144 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AaiPublisherTaskSpy.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AaiPublisherTaskSpy.java @@ -20,10 +20,6 @@ package org.onap.dcaegen2.services.prh.tasks; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; - import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration; import org.onap.dcaegen2.services.prh.configuration.AppConfig; import org.onap.dcaegen2.services.prh.service.producer.AaiProducerReactiveHttpClient; @@ -31,6 +27,10 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; +import javax.net.ssl.SSLException; + +import static org.mockito.Mockito.*; + /** * @author Przemysław Wąsala on 4/13/18 */ @@ -44,7 +44,7 @@ public class AaiPublisherTaskSpy { */ @Bean @Primary - public AaiProducerTask registerSimpleAaiPublisherTask() { + public AaiProducerTask registerSimpleAaiPublisherTask() throws SSLException { AppConfig appConfig = spy(AppConfig.class); doReturn(mock(AaiClientConfiguration.class)).when(appConfig).getAaiClientConfiguration(); AaiProducerTaskImpl aaiProducerTask = spy(new AaiProducerTaskImpl(appConfig)); 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 453679df..ae7b8e77 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 @@ -20,16 +20,6 @@ package org.onap.dcaegen2.services.prh.tasks; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; - import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.function.Executable; @@ -42,9 +32,14 @@ import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.prh.model.ImmutableConsumerDmaapModel; import org.onap.dcaegen2.services.prh.service.producer.DMaaPProducerReactiveHttpClient; import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + /** * @author Przemysław Wąsala on 5/17/18 */ @@ -84,15 +79,16 @@ class DmaapPublisherTaskImplTest { @Test void whenPassedObjectFits_ReturnsCorrectStatus() throws PrhTaskException { //given - prepareMocksForTests(HttpStatus.OK.value()); + ResponseEntity responseEntity = prepareMocksForTests(HttpStatus.OK.value()); //when - StepVerifier.create(dmaapPublisherTask.execute(Mono.just(consumerDmaapModel))).expectSubscription() - .expectNext(HttpStatus.OK.toString()).verifyComplete(); + when(responseEntity.getStatusCode()).thenReturn(HttpStatus.OK); + StepVerifier.create(dmaapPublisherTask.execute(consumerDmaapModel)).expectSubscription() + .expectNext(responseEntity).verifyComplete(); //then verify(dMaaPProducerReactiveHttpClient, times(1)) - .getDMaaPProducerResponse(any()); + .getDMaaPProducerResponse(consumerDmaapModel); verifyNoMoreInteractions(dMaaPProducerReactiveHttpClient); } @@ -100,24 +96,30 @@ class DmaapPublisherTaskImplTest { @Test void whenPassedObjectFits_butIncorrectResponseReturns() throws DmaapNotFoundException { //given - prepareMocksForTests(HttpStatus.UNAUTHORIZED.value()); + ResponseEntity responseEntity = prepareMocksForTests(HttpStatus.UNAUTHORIZED.value()); //when - StepVerifier.create(dmaapPublisherTask.execute(Mono.just(consumerDmaapModel))).expectSubscription() - .expectNext(String.valueOf(HttpStatus.UNAUTHORIZED.value())).verifyComplete(); + when(responseEntity.getStatusCode()).thenReturn(HttpStatus.UNAUTHORIZED); + StepVerifier.create(dmaapPublisherTask.execute(consumerDmaapModel)).expectSubscription() + .expectNext(responseEntity).verifyComplete(); //then - verify(dMaaPProducerReactiveHttpClient, times(1)).getDMaaPProducerResponse(any()); + verify(dMaaPProducerReactiveHttpClient, times(1)) + .getDMaaPProducerResponse(consumerDmaapModel); verifyNoMoreInteractions(dMaaPProducerReactiveHttpClient); } - private void prepareMocksForTests(Integer httpResponseCode) { + private ResponseEntity prepareMocksForTests(Integer httpResponseCode) { + ResponseEntity responseEntity = mock(ResponseEntity.class); + //when + when(responseEntity.getStatusCode()).thenReturn(HttpStatus.valueOf(httpResponseCode)); dMaaPProducerReactiveHttpClient = mock(DMaaPProducerReactiveHttpClient.class); when(dMaaPProducerReactiveHttpClient.getDMaaPProducerResponse(any())) - .thenReturn(Mono.just(httpResponseCode.toString())); + .thenReturn(Mono.just(responseEntity)); dmaapPublisherTask = spy(new DmaapPublisherTaskImpl(appConfig)); when(dmaapPublisherTask.resolveConfiguration()).thenReturn(dmaapPublisherConfiguration); doReturn(dMaaPProducerReactiveHttpClient).when(dmaapPublisherTask).resolveClient(); + return responseEntity; } } \ No newline at end of file -- cgit 1.2.3-korg