3b6fe4d78a0c613cd9ca7ddfebbf57175a4bbfb3
[dcaegen2/services/prh.git] /
1 /*-
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
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.services.prh.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.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;
44
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("PRH");
70         when(aaiConfigurationMock.aaiUserPassword()).thenReturn("PRH");
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(aaiProducerReactiveHttpClient.getAaiProducerResponse(Mono.just(dmaapModel))).expectSubscription()
117                 .expectError(Exception.class).verify();
118     }
119
120
121     private void mockWebClientDependantObject() {
122         WebClient.RequestHeadersSpec requestHeadersSpec = mock(WebClient.RequestHeadersSpec.class);
123         when(webClient.patch()).thenReturn(requestBodyUriSpec);
124         when(requestBodyUriSpec.uri((URI) any())).thenReturn(requestBodyUriSpec);
125         when(requestBodyUriSpec.body(any())).thenReturn(requestHeadersSpec);
126         doReturn(responseSpec).when(requestHeadersSpec).retrieve();
127         doReturn(responseSpec).when(responseSpec).onStatus(any(), any());
128     }
129
130     private static void setupHeaders() {
131         aaiHeaders = new HashMap<>();
132         aaiHeaders.put("X-FromAppId", "PRH");
133         aaiHeaders.put("X-TransactionId", "vv-temp");
134         aaiHeaders.put("Accept", "application/json");
135         aaiHeaders.put("Real-Time", "true");
136         aaiHeaders.put("Content-Type", "application/merge-patch+json");
137     }
138
139 }
140