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 AaiProducerReactiveHttpClient aaiProducerReactiveHttpClient;
50 private static final Integer SUCCESS_RESPONSE = 200;
52 private static AaiClientConfiguration aaiConfigurationMock = mock(AaiClientConfiguration.class);
53 private static WebClient webClient = mock(WebClient.class);
55 private static ConsumerDmaapModel dmaapModel = new ConsumerDmaapModelForUnitTest();
56 private static WebClient.RequestBodyUriSpec requestBodyUriSpec;
57 private static ResponseSpec responseSpec;
59 private static Map<String, String> aaiHeaders;
65 when(aaiConfigurationMock.aaiHost()).thenReturn("54.45.33.2");
66 when(aaiConfigurationMock.aaiProtocol()).thenReturn("https");
67 when(aaiConfigurationMock.aaiPort()).thenReturn(1234);
68 when(aaiConfigurationMock.aaiUserName()).thenReturn("PRH");
69 when(aaiConfigurationMock.aaiUserPassword()).thenReturn("PRH");
70 when(aaiConfigurationMock.aaiBasePath()).thenReturn("/aai/v11");
71 when(aaiConfigurationMock.aaiPnfPath()).thenReturn("/network/pnfs/pnf");
72 when(aaiConfigurationMock.aaiHeaders()).thenReturn(aaiHeaders);
74 aaiProducerReactiveHttpClient = new AaiProducerReactiveHttpClient(aaiConfigurationMock);
76 webClient = spy(WebClient.builder()
77 .defaultHeaders(httpHeaders -> httpHeaders.setAll(aaiHeaders))
78 .filter(basicAuthentication(aaiConfigurationMock.aaiUserName(), aaiConfigurationMock.aaiUserPassword()))
81 requestBodyUriSpec = mock(WebClient.RequestBodyUriSpec.class);
82 responseSpec = mock(ResponseSpec.class);
87 void getAaiProducerResponse_shouldReturn200() {
89 Mono<Integer> expectedResult = Mono.just(SUCCESS_RESPONSE);
92 mockWebClientDependantObject();
93 doReturn(expectedResult).when(responseSpec).bodyToMono(Integer.class);
94 aaiProducerReactiveHttpClient.createAaiWebClient(webClient);
95 Mono<Integer> response = aaiProducerReactiveHttpClient.getAaiProducerResponse(Mono.just(dmaapModel));
98 StepVerifier.create(response).expectSubscription()
99 .expectNextMatches(results -> {
100 Assertions.assertEquals(results, expectedResult.block());
106 void getHttpResponse_whenUriSyntaxExceptionHasBeenThrown() throws URISyntaxException {
108 aaiProducerReactiveHttpClient = spy(aaiProducerReactiveHttpClient);
110 when(webClient.patch()).thenReturn(requestBodyUriSpec);
111 aaiProducerReactiveHttpClient.createAaiWebClient(webClient);
112 when(aaiProducerReactiveHttpClient.getUri("pnfName")).thenThrow(URISyntaxException.class);
116 aaiProducerReactiveHttpClient.getAaiProducerResponse(
117 Mono.just(dmaapModel)
118 )).expectSubscription().expectError(Exception.class).verify();
122 private void mockWebClientDependantObject() {
123 WebClient.RequestHeadersSpec requestHeadersSpec = mock(WebClient.RequestHeadersSpec.class);
124 when(webClient.patch()).thenReturn(requestBodyUriSpec);
125 when(requestBodyUriSpec.uri((URI) any())).thenReturn(requestBodyUriSpec);
126 when(requestBodyUriSpec.body(any())).thenReturn(requestHeadersSpec);
127 doReturn(responseSpec).when(requestHeadersSpec).retrieve();
128 doReturn(responseSpec).when(responseSpec).onStatus(any(), any());
131 private static void setupHeaders() {
132 aaiHeaders = new HashMap<>();
133 aaiHeaders.put("X-FromAppId", "PRH");
134 aaiHeaders.put("X-TransactionId", "vv-temp");
135 aaiHeaders.put("Accept", "application/json");
136 aaiHeaders.put("Real-Time", "true");
137 aaiHeaders.put("Content-Type", "application/merge-patch+json");