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.doThrow;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.spy;
28 import static org.mockito.Mockito.when;
29 import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
32 import java.net.URISyntaxException;
33 import java.util.HashMap;
36 import org.junit.jupiter.api.Assertions;
37 import org.junit.jupiter.api.BeforeEach;
38 import org.junit.jupiter.api.Test;
39 import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration;
40 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
41 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModelForUnitTest;
42 import org.springframework.web.reactive.function.client.ClientResponse;
43 import org.springframework.web.reactive.function.client.WebClient;
44 import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
45 import reactor.core.publisher.Mono;
46 import reactor.test.StepVerifier;
48 class AaiProducerReactiveHttpClientTest {
50 private static final Integer SUCCESS_RESPONSE = 200;
51 private static AaiProducerReactiveHttpClient aaiProducerReactiveHttpClient;
52 private static AaiClientConfiguration aaiConfigurationMock = mock(AaiClientConfiguration.class);
53 private static WebClient webClient = mock(WebClient.class);
56 private ConsumerDmaapModel dmaapModel = spy(new ConsumerDmaapModelForUnitTest());
57 private WebClient.RequestBodyUriSpec requestBodyUriSpec;
58 private ResponseSpec responseSpec;
60 private Map<String, String> aaiHeaders;
61 private ClientResponse clientResponse;
62 private Mono<ClientResponse> clientResponseMono;
67 clientResponse = mock(ClientResponse.class);
68 clientResponseMono = Mono.just(clientResponse);
69 when(dmaapModel.getSourceName()).thenReturn("NOKnhfsadhff");
70 when(aaiConfigurationMock.aaiHost()).thenReturn("54.45.33.2");
71 when(aaiConfigurationMock.aaiProtocol()).thenReturn("https");
72 when(aaiConfigurationMock.aaiPort()).thenReturn(1234);
73 when(aaiConfigurationMock.aaiUserName()).thenReturn("PRH");
74 when(aaiConfigurationMock.aaiUserPassword()).thenReturn("PRH");
75 when(aaiConfigurationMock.aaiBasePath()).thenReturn("/aai/v11");
76 when(aaiConfigurationMock.aaiPnfPath()).thenReturn("/network/pnfs/pnf");
77 when(aaiConfigurationMock.aaiHeaders()).thenReturn(aaiHeaders);
79 aaiProducerReactiveHttpClient = new AaiProducerReactiveHttpClient(aaiConfigurationMock);
81 webClient = spy(WebClient.builder()
82 .defaultHeaders(httpHeaders -> httpHeaders.setAll(aaiHeaders))
83 .filter(basicAuthentication(aaiConfigurationMock.aaiUserName(), aaiConfigurationMock.aaiUserPassword()))
86 requestBodyUriSpec = mock(WebClient.RequestBodyUriSpec.class);
87 responseSpec = mock(ResponseSpec.class);
91 void getAaiProducerResponse_shouldReturn200() {
93 Mono<Integer> expectedResult = Mono.just(SUCCESS_RESPONSE);
96 mockWebClientDependantObject();
97 doReturn(expectedResult).when(responseSpec).bodyToMono(Integer.class);
98 aaiProducerReactiveHttpClient.createAaiWebClient(webClient);
101 StepVerifier.create(aaiProducerReactiveHttpClient.getAaiProducerResponse(dmaapModel)).expectSubscription()
102 .expectNextMatches(results -> {
103 Assertions.assertEquals(results, clientResponse);
110 void getAppropriateUri_whenPassingCorrectedPathForPnf() {
111 Assertions.assertEquals(aaiProducerReactiveHttpClient.getUri("NOKnhfsadhff"),
112 URI.create("https://54.45.33.2:1234/aai/v11/network/pnfs/pnf/NOKnhfsadhff"));
116 private void setupHeaders() {
117 aaiHeaders = new HashMap<>();
118 aaiHeaders.put("X-FromAppId", "PRH");
119 aaiHeaders.put("X-TransactionId", "vv-temp");
120 aaiHeaders.put("Accept", "application/json");
121 aaiHeaders.put("Real-Time", "true");
122 aaiHeaders.put("Content-Type", "application/merge-patch+json");
125 private void mockWebClientDependantObject() {
126 WebClient.RequestHeadersSpec requestHeadersSpec = mock(WebClient.RequestHeadersSpec.class);
127 when(webClient.patch()).thenReturn(requestBodyUriSpec);
128 when(requestBodyUriSpec.uri((URI) any())).thenReturn(requestBodyUriSpec);
129 when(requestBodyUriSpec.header(any(), any())).thenReturn(requestBodyUriSpec);
130 when(requestBodyUriSpec.body(any(), (Class<Object>) any())).thenReturn(requestHeadersSpec);
131 when(requestHeadersSpec.exchange()).thenReturn(clientResponseMono);