4160f3562eebe155956a5f9033edd43942da96d1
[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.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;
30
31 import java.net.URI;
32 import java.net.URISyntaxException;
33 import java.util.HashMap;
34 import java.util.Map;
35
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;
47
48 class AaiProducerReactiveHttpClientTest {
49
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);
54
55
56     private ConsumerDmaapModel dmaapModel = spy(new ConsumerDmaapModelForUnitTest());
57     private WebClient.RequestBodyUriSpec requestBodyUriSpec;
58     private ResponseSpec responseSpec;
59
60     private Map<String, String> aaiHeaders;
61     private ClientResponse clientResponse;
62     private Mono<ClientResponse> clientResponseMono;
63
64     @BeforeEach
65     void setUp() {
66         setupHeaders();
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);
78
79         aaiProducerReactiveHttpClient = new AaiProducerReactiveHttpClient(aaiConfigurationMock);
80
81         webClient = spy(WebClient.builder()
82             .defaultHeaders(httpHeaders -> httpHeaders.setAll(aaiHeaders))
83             .filter(basicAuthentication(aaiConfigurationMock.aaiUserName(), aaiConfigurationMock.aaiUserPassword()))
84             .build());
85
86         requestBodyUriSpec = mock(WebClient.RequestBodyUriSpec.class);
87         responseSpec = mock(ResponseSpec.class);
88     }
89
90     @Test
91     void getAaiProducerResponse_shouldReturn200() {
92         //given
93         Mono<Integer> expectedResult = Mono.just(SUCCESS_RESPONSE);
94
95         //when
96         mockWebClientDependantObject();
97         doReturn(expectedResult).when(responseSpec).bodyToMono(Integer.class);
98         aaiProducerReactiveHttpClient.createAaiWebClient(webClient);
99
100         //then
101         StepVerifier.create(aaiProducerReactiveHttpClient.getAaiProducerResponse(dmaapModel)).expectSubscription()
102             .expectNextMatches(results -> {
103                 Assertions.assertEquals(results, clientResponse);
104                 return true;
105             }).verifyComplete();
106     }
107
108     @Test
109     void getHttpResponse_whenUriSyntaxExceptionHasBeenThrown() throws URISyntaxException {
110         ///given
111         aaiProducerReactiveHttpClient = spy(aaiProducerReactiveHttpClient);
112         //when
113         when(webClient.patch()).thenReturn(requestBodyUriSpec);
114         aaiProducerReactiveHttpClient.createAaiWebClient(webClient);
115         doThrow(URISyntaxException.class).when(aaiProducerReactiveHttpClient).getUri(any());
116         //then
117         StepVerifier.create(
118             aaiProducerReactiveHttpClient.getAaiProducerResponse(
119                 dmaapModel
120             )).expectSubscription().expectError(Exception.class).verify();
121     }
122
123     @Test
124     void getAppropriateUri_whenPassingCorrectedPathForPnf() throws URISyntaxException {
125         Assertions.assertEquals(aaiProducerReactiveHttpClient.getUri("NOKnhfsadhff"),
126             URI.create("https://54.45.33.2:1234/aai/v11/network/pnfs/pnf/NOKnhfsadhff"));
127     }
128
129
130     private 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     private void mockWebClientDependantObject() {
140         WebClient.RequestHeadersSpec requestHeadersSpec = mock(WebClient.RequestHeadersSpec.class);
141         when(webClient.patch()).thenReturn(requestBodyUriSpec);
142         when(requestBodyUriSpec.uri((URI) any())).thenReturn(requestBodyUriSpec);
143         when(requestBodyUriSpec.header(any(), any())).thenReturn(requestBodyUriSpec);
144         when(requestBodyUriSpec.body(any(), (Class<Object>) any())).thenReturn(requestHeadersSpec);
145         when(requestHeadersSpec.exchange()).thenReturn(clientResponseMono);
146     }
147 }
148