diff options
author | pwielebs <piotr.wielebski@nokia.com> | 2018-07-12 10:16:00 +0200 |
---|---|---|
committer | wasala <przemyslaw.wasala@nokia.com> | 2018-08-07 10:27:50 +0200 |
commit | 2cd0124def5b2267028e71220201efebf46fdcd4 (patch) | |
tree | fc5242000c1775d5656781f0c02dbf071ccf0b08 /prh-aai-client/src/test/java/org | |
parent | feeef5bf392e1639a13866a727933f960c70905c (diff) |
Adding AAI reactive webClient
Change-Id: Iedf88dfe12f41f0d994bcfc7c94e21ddf945a001
Issue-ID: DCAEGEN2-609
Signed-off-by: pwielebs <piotr.wielebski@nokia.com>
Diffstat (limited to 'prh-aai-client/src/test/java/org')
4 files changed, 143 insertions, 3 deletions
diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiConsumerClientTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiConsumerClientTest.java index 8b4a2110..a7b82b5f 100644 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiConsumerClientTest.java +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiConsumerClientTest.java @@ -62,7 +62,7 @@ class AaiConsumerClientTest { when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("54.45.33.2"); when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https"); - when(aaiHttpClientConfigurationMock.aaiHostPortNumber()).thenReturn(1234); + when(aaiHttpClientConfigurationMock.aaiPort()).thenReturn(1234); when(aaiHttpClientConfigurationMock.aaiUserName()).thenReturn("PRH"); when(aaiHttpClientConfigurationMock.aaiUserPassword()).thenReturn("PRH"); when(aaiHttpClientConfigurationMock.aaiBasePath()).thenReturn("/aai/v11"); diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiHttpClientImplTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiHttpClientImplTest.java index 419fe32b..ebedc4a0 100644 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiHttpClientImplTest.java +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiHttpClientImplTest.java @@ -38,7 +38,7 @@ class AaiHttpClientImplTest { AaiClientConfiguration aaiHttpClientConfigurationMock = mock(AaiClientConfiguration.class); when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("54.45.33.2"); when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https"); - when(aaiHttpClientConfigurationMock.aaiHostPortNumber()).thenReturn(1234); + when(aaiHttpClientConfigurationMock.aaiPort()).thenReturn(1234); when(aaiHttpClientConfigurationMock.aaiUserName()).thenReturn("PNF"); when(aaiHttpClientConfigurationMock.aaiUserPassword()).thenReturn("PNF"); when(aaiHttpClientConfigurationMock.aaiIgnoreSslCertificateErrors()).thenReturn(true); diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiProducerClientTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiProducerClientTest.java index 27cb374d..13d4dcec 100644 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiProducerClientTest.java +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiProducerClientTest.java @@ -64,7 +64,7 @@ class AaiProducerClientTest { static void setup() throws NoSuchFieldException, IllegalAccessException { when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("eucalyptus.es-si-eu-dhn-20.eecloud.nsn-net.net"); when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https"); - when(aaiHttpClientConfigurationMock.aaiHostPortNumber()).thenReturn(1234); + when(aaiHttpClientConfigurationMock.aaiPort()).thenReturn(1234); when(aaiHttpClientConfigurationMock.aaiUserName()).thenReturn("PRH"); when(aaiHttpClientConfigurationMock.aaiUserPassword()).thenReturn("PRH"); when(aaiHttpClientConfigurationMock.aaiBasePath()).thenReturn("/aai/v11"); diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClientTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClientTest.java new file mode 100644 index 00000000..3b6fe4d7 --- /dev/null +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClientTest.java @@ -0,0 +1,140 @@ +/*- + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.services.prh.service.producer; + +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.when; +import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration; +import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; +import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModelForUnitTest; +import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.reactive.function.client.WebClient.ResponseSpec; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + + + +class AaiProducerReactiveHttpClientTest { + + private static AaiProducerReactiveHttpClient aaiProducerReactiveHttpClient; + + private static final Integer SUCCESS_RESPONSE = 200; + + private static AaiClientConfiguration aaiConfigurationMock = mock(AaiClientConfiguration.class); + private static WebClient webClient = mock(WebClient.class); + + private static ConsumerDmaapModel dmaapModel = new ConsumerDmaapModelForUnitTest(); + private static WebClient.RequestBodyUriSpec requestBodyUriSpec; + private static ResponseSpec responseSpec; + + private static Map<String, String> aaiHeaders; + + @BeforeAll + static void setUp() { + setupHeaders(); + + when(aaiConfigurationMock.aaiHost()).thenReturn("54.45.33.2"); + when(aaiConfigurationMock.aaiProtocol()).thenReturn("https"); + when(aaiConfigurationMock.aaiPort()).thenReturn(1234); + when(aaiConfigurationMock.aaiUserName()).thenReturn("PRH"); + when(aaiConfigurationMock.aaiUserPassword()).thenReturn("PRH"); + when(aaiConfigurationMock.aaiBasePath()).thenReturn("/aai/v11"); + when(aaiConfigurationMock.aaiPnfPath()).thenReturn("/network/pnfs/pnf"); + when(aaiConfigurationMock.aaiHeaders()).thenReturn(aaiHeaders); + + aaiProducerReactiveHttpClient = new AaiProducerReactiveHttpClient(aaiConfigurationMock); + + webClient = spy(WebClient.builder() + .defaultHeaders(httpHeaders -> httpHeaders.setAll(aaiHeaders)) + .filter(basicAuthentication(aaiConfigurationMock.aaiUserName(), aaiConfigurationMock.aaiUserPassword())) + .build()); + + requestBodyUriSpec = mock(WebClient.RequestBodyUriSpec.class); + responseSpec = mock(ResponseSpec.class); + } + + + @Test + void getAaiProducerResponse_shouldReturn200() { + //given + Mono<Integer> expectedResult = Mono.just(SUCCESS_RESPONSE); + + //when + mockWebClientDependantObject(); + doReturn(expectedResult).when(responseSpec).bodyToMono(Integer.class); + aaiProducerReactiveHttpClient.createAaiWebClient(webClient); + Mono<Integer> response = aaiProducerReactiveHttpClient.getAaiProducerResponse(Mono.just(dmaapModel)); + + //then + StepVerifier.create(response).expectSubscription() + .expectNextMatches(results -> { + Assertions.assertEquals(results, expectedResult.block()); + return true; + }).verifyComplete(); + } + + @Test + void getHttpResponse_whenUriSyntaxExceptionHasBeenThrown() throws URISyntaxException { + ///given + aaiProducerReactiveHttpClient = spy(aaiProducerReactiveHttpClient); + //when + when(webClient.patch()).thenReturn(requestBodyUriSpec); + aaiProducerReactiveHttpClient.createAaiWebClient(webClient); + when(aaiProducerReactiveHttpClient.getUri("pnfName")).thenThrow(URISyntaxException.class); + + //then + StepVerifier.create(aaiProducerReactiveHttpClient.getAaiProducerResponse(Mono.just(dmaapModel))).expectSubscription() + .expectError(Exception.class).verify(); + } + + + private void mockWebClientDependantObject() { + WebClient.RequestHeadersSpec requestHeadersSpec = mock(WebClient.RequestHeadersSpec.class); + when(webClient.patch()).thenReturn(requestBodyUriSpec); + when(requestBodyUriSpec.uri((URI) any())).thenReturn(requestBodyUriSpec); + when(requestBodyUriSpec.body(any())).thenReturn(requestHeadersSpec); + doReturn(responseSpec).when(requestHeadersSpec).retrieve(); + doReturn(responseSpec).when(responseSpec).onStatus(any(), any()); + } + + private static void setupHeaders() { + aaiHeaders = new HashMap<>(); + aaiHeaders.put("X-FromAppId", "PRH"); + aaiHeaders.put("X-TransactionId", "vv-temp"); + aaiHeaders.put("Accept", "application/json"); + aaiHeaders.put("Real-Time", "true"); + aaiHeaders.put("Content-Type", "application/merge-patch+json"); + } + +} + |