2 * ============LICENSE_START=======================================================
3 * Datafile Collector Service
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.collectors.datafile.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.collectors.datafile.config.AaiClientConfiguration;
38 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
39 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModelForUnitTest;
40 import org.onap.dcaegen2.collectors.datafile.service.producer.AaiProducerReactiveHttpClient;
41 import org.springframework.web.reactive.function.client.WebClient;
42 import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
43 import reactor.core.publisher.Mono;
44 import reactor.test.StepVerifier;
47 class AaiProducerReactiveHttpClientTest {
49 private static AaiProducerReactiveHttpClient aaiProducerReactiveHttpClient;
51 private static final Integer SUCCESS_RESPONSE = 200;
53 private static AaiClientConfiguration aaiConfigurationMock = mock(AaiClientConfiguration.class);
54 private static WebClient webClient = mock(WebClient.class);
56 private static ConsumerDmaapModel dmaapModel = new ConsumerDmaapModelForUnitTest();
57 private static WebClient.RequestBodyUriSpec requestBodyUriSpec;
58 private static ResponseSpec responseSpec;
60 private static Map<String, String> aaiHeaders;
66 when(aaiConfigurationMock.aaiHost()).thenReturn("54.45.33.2");
67 when(aaiConfigurationMock.aaiProtocol()).thenReturn("https");
68 when(aaiConfigurationMock.aaiPort()).thenReturn(1234);
69 when(aaiConfigurationMock.aaiUserName()).thenReturn("Datafile");
70 when(aaiConfigurationMock.aaiUserPassword()).thenReturn("Datafile");
71 when(aaiConfigurationMock.aaiBasePath()).thenReturn("/aai/v11");
72 when(aaiConfigurationMock.aaiPnfPath()).thenReturn("/network/pnfs/pnf");
73 when(aaiConfigurationMock.aaiHeaders()).thenReturn(aaiHeaders);
75 aaiProducerReactiveHttpClient = new AaiProducerReactiveHttpClient(aaiConfigurationMock);
77 webClient = spy(WebClient.builder()
78 .defaultHeaders(httpHeaders -> httpHeaders.setAll(aaiHeaders))
79 .filter(basicAuthentication(aaiConfigurationMock.aaiUserName(), aaiConfigurationMock.aaiUserPassword()))
82 requestBodyUriSpec = mock(WebClient.RequestBodyUriSpec.class);
83 responseSpec = mock(ResponseSpec.class);
88 void getAaiProducerResponse_shouldReturn200() {
90 Mono<Integer> expectedResult = Mono.just(SUCCESS_RESPONSE);
93 mockWebClientDependantObject();
94 doReturn(expectedResult).when(responseSpec).bodyToMono(Integer.class);
95 aaiProducerReactiveHttpClient.createAaiWebClient(webClient);
96 Mono<Integer> response = aaiProducerReactiveHttpClient.getAaiProducerResponse(Mono.just(dmaapModel));
99 StepVerifier.create(response).expectSubscription()
100 .expectNextMatches(results -> {
101 Assertions.assertEquals(results, expectedResult.block());
107 void getHttpResponse_whenUriSyntaxExceptionHasBeenThrown() throws URISyntaxException {
109 aaiProducerReactiveHttpClient = spy(aaiProducerReactiveHttpClient);
111 when(webClient.patch()).thenReturn(requestBodyUriSpec);
112 aaiProducerReactiveHttpClient.createAaiWebClient(webClient);
113 when(aaiProducerReactiveHttpClient.getUri("pnfName")).thenThrow(URISyntaxException.class);
117 aaiProducerReactiveHttpClient.getAaiProducerResponse(
118 Mono.just(dmaapModel)
119 )).expectSubscription().expectError(Exception.class).verify();
123 private void mockWebClientDependantObject() {
124 WebClient.RequestHeadersSpec requestHeadersSpec = mock(WebClient.RequestHeadersSpec.class);
125 when(webClient.patch()).thenReturn(requestBodyUriSpec);
126 when(requestBodyUriSpec.uri((URI) any())).thenReturn(requestBodyUriSpec);
127 when(requestBodyUriSpec.body(any())).thenReturn(requestHeadersSpec);
128 doReturn(responseSpec).when(requestHeadersSpec).retrieve();
129 doReturn(responseSpec).when(responseSpec).onStatus(any(), any());
132 private static void setupHeaders() {
133 aaiHeaders = new HashMap<>();
134 aaiHeaders.put("X-FromAppId", "Datafile");
135 aaiHeaders.put("X-TransactionId", "vv-temp");
136 aaiHeaders.put("Accept", "application/json");
137 aaiHeaders.put("Real-Time", "true");
138 aaiHeaders.put("Content-Type", "application/merge-patch+json");