65f13553b1ea5342aa789c60765b5affc6ee89fc
[dcaegen2/collectors/datafile.git] /
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.dcaegen2.collectors.datafile.service.producer;
22
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;
29
30 import java.net.URI;
31 import java.net.URISyntaxException;
32 import java.util.HashMap;
33 import java.util.Map;
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;
45
46
47 class AaiProducerReactiveHttpClientTest {
48
49     private static AaiProducerReactiveHttpClient aaiProducerReactiveHttpClient;
50
51     private static final Integer SUCCESS_RESPONSE = 200;
52
53     private static AaiClientConfiguration aaiConfigurationMock = mock(AaiClientConfiguration.class);
54     private static WebClient webClient = mock(WebClient.class);
55
56     private static ConsumerDmaapModel dmaapModel = new ConsumerDmaapModelForUnitTest();
57     private static WebClient.RequestBodyUriSpec requestBodyUriSpec;
58     private static ResponseSpec responseSpec;
59
60     private static Map<String, String> aaiHeaders;
61
62     @BeforeAll
63     static void setUp() {
64         setupHeaders();
65
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);
74
75         aaiProducerReactiveHttpClient = new AaiProducerReactiveHttpClient(aaiConfigurationMock);
76
77         webClient = spy(WebClient.builder()
78             .defaultHeaders(httpHeaders -> httpHeaders.setAll(aaiHeaders))
79             .filter(basicAuthentication(aaiConfigurationMock.aaiUserName(), aaiConfigurationMock.aaiUserPassword()))
80             .build());
81
82         requestBodyUriSpec = mock(WebClient.RequestBodyUriSpec.class);
83         responseSpec = mock(ResponseSpec.class);
84     }
85
86
87     @Test
88     void getAaiProducerResponse_shouldReturn200() {
89         //given
90         Mono<Integer> expectedResult = Mono.just(SUCCESS_RESPONSE);
91
92         //when
93         mockWebClientDependantObject();
94         doReturn(expectedResult).when(responseSpec).bodyToMono(Integer.class);
95         aaiProducerReactiveHttpClient.createAaiWebClient(webClient);
96         Mono<Integer> response = aaiProducerReactiveHttpClient.getAaiProducerResponse(Mono.just(dmaapModel));
97
98         //then
99         StepVerifier.create(response).expectSubscription()
100             .expectNextMatches(results -> {
101                 Assertions.assertEquals(results, expectedResult.block());
102                 return true;
103             }).verifyComplete();
104     }
105
106     @Test
107     void getHttpResponse_whenUriSyntaxExceptionHasBeenThrown() throws URISyntaxException {
108         ///given
109         aaiProducerReactiveHttpClient = spy(aaiProducerReactiveHttpClient);
110         //when
111         when(webClient.patch()).thenReturn(requestBodyUriSpec);
112         aaiProducerReactiveHttpClient.createAaiWebClient(webClient);
113         when(aaiProducerReactiveHttpClient.getUri("pnfName")).thenThrow(URISyntaxException.class);
114
115         //then
116         StepVerifier.create(
117             aaiProducerReactiveHttpClient.getAaiProducerResponse(
118                 Mono.just(dmaapModel)
119             )).expectSubscription().expectError(Exception.class).verify();
120     }
121
122
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());
130     }
131
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");
139     }
140
141 }
142