2 * ============LICENSE_START=======================================================
3 * PNF-REGISTRATION-HANDLER
4 * ================================================================================
5 * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.dcaegen2.services.prh.service.producer;
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.when;
28 import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
31 import java.net.URISyntaxException;
32 import java.util.HashMap;
34 import org.junit.jupiter.api.Assertions;
35 import org.junit.jupiter.api.BeforeAll;
36 import org.junit.jupiter.api.Test;
37 import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration;
38 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
39 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModelForUnitTest;
40 import org.springframework.web.reactive.function.client.WebClient;
41 import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
42 import reactor.core.publisher.Mono;
43 import reactor.test.StepVerifier;
46 class AaiProducerReactiveHttpClientTest {
48 private static final Integer SUCCESS_RESPONSE = 200;
49 private static AaiProducerReactiveHttpClient aaiProducerReactiveHttpClient;
50 private static AaiClientConfiguration aaiConfigurationMock = mock(AaiClientConfiguration.class);
51 private static WebClient webClient = mock(WebClient.class);
53 private static ConsumerDmaapModel dmaapModel = new ConsumerDmaapModelForUnitTest();
54 private static WebClient.RequestBodyUriSpec requestBodyUriSpec;
55 private static ResponseSpec responseSpec;
57 private static Map<String, String> aaiHeaders;
63 when(aaiConfigurationMock.aaiHost()).thenReturn("54.45.33.2");
64 when(aaiConfigurationMock.aaiProtocol()).thenReturn("https");
65 when(aaiConfigurationMock.aaiPort()).thenReturn(1234);
66 when(aaiConfigurationMock.aaiUserName()).thenReturn("PRH");
67 when(aaiConfigurationMock.aaiUserPassword()).thenReturn("PRH");
68 when(aaiConfigurationMock.aaiBasePath()).thenReturn("/aai/v11");
69 when(aaiConfigurationMock.aaiPnfPath()).thenReturn("/network/pnfs/pnf");
70 when(aaiConfigurationMock.aaiHeaders()).thenReturn(aaiHeaders);
72 aaiProducerReactiveHttpClient = new AaiProducerReactiveHttpClient(aaiConfigurationMock);
74 webClient = spy(WebClient.builder()
75 .defaultHeaders(httpHeaders -> httpHeaders.setAll(aaiHeaders))
76 .filter(basicAuthentication(aaiConfigurationMock.aaiUserName(), aaiConfigurationMock.aaiUserPassword()))
79 requestBodyUriSpec = mock(WebClient.RequestBodyUriSpec.class);
80 responseSpec = mock(ResponseSpec.class);
83 private static void setupHeaders() {
84 aaiHeaders = new HashMap<>();
85 aaiHeaders.put("X-FromAppId", "PRH");
86 aaiHeaders.put("X-TransactionId", "vv-temp");
87 aaiHeaders.put("Accept", "application/json");
88 aaiHeaders.put("Real-Time", "true");
89 aaiHeaders.put("Content-Type", "application/merge-patch+json");
93 void getAaiProducerResponse_shouldReturn200() {
95 Mono<Integer> expectedResult = Mono.just(SUCCESS_RESPONSE);
98 mockWebClientDependantObject();
99 doReturn(expectedResult).when(responseSpec).bodyToMono(Integer.class);
100 aaiProducerReactiveHttpClient.createAaiWebClient(webClient);
101 Mono<Integer> response = aaiProducerReactiveHttpClient.getAaiProducerResponse(Mono.just(dmaapModel));
104 StepVerifier.create(response).expectSubscription()
105 .expectNextMatches(results -> {
106 Assertions.assertEquals(results, expectedResult.block());
112 void getHttpResponse_whenUriSyntaxExceptionHasBeenThrown() throws URISyntaxException {
114 aaiProducerReactiveHttpClient = spy(aaiProducerReactiveHttpClient);
116 when(webClient.patch()).thenReturn(requestBodyUriSpec);
117 aaiProducerReactiveHttpClient.createAaiWebClient(webClient);
118 when(aaiProducerReactiveHttpClient.getUri("pnfName")).thenThrow(URISyntaxException.class);
122 aaiProducerReactiveHttpClient.getAaiProducerResponse(
123 Mono.just(dmaapModel)
124 )).expectSubscription().expectError(Exception.class).verify();
127 private void mockWebClientDependantObject() {
128 WebClient.RequestHeadersSpec requestHeadersSpec = mock(WebClient.RequestHeadersSpec.class);
129 when(webClient.patch()).thenReturn(requestBodyUriSpec);
130 when(requestBodyUriSpec.uri((URI) any())).thenReturn(requestBodyUriSpec);
131 when(requestBodyUriSpec.header(any(), any())).thenReturn(requestBodyUriSpec);
132 when(requestBodyUriSpec.body(any())).thenReturn(requestHeadersSpec);
133 doReturn(responseSpec).when(requestHeadersSpec).retrieve();
134 doReturn(responseSpec).when(responseSpec).onStatus(any(), any());