9b0f4fe8d0937ee67af81416865373325489a2df
[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 class AaiProducerReactiveHttpClientTest {
47
48     private static final Integer SUCCESS_RESPONSE = 200;
49     private static AaiProducerReactiveHttpClient aaiProducerReactiveHttpClient;
50     private static AaiClientConfiguration aaiConfigurationMock = mock(AaiClientConfiguration.class);
51     private static WebClient webClient = mock(WebClient.class);
52
53     private static ConsumerDmaapModel dmaapModel = new ConsumerDmaapModelForUnitTest();
54     private static WebClient.RequestBodyUriSpec requestBodyUriSpec;
55     private static ResponseSpec responseSpec;
56
57     private static Map<String, String> aaiHeaders;
58
59     @BeforeAll
60     static void setUp() {
61         setupHeaders();
62
63         when(aaiConfigurationMock.aaiHost()).thenReturn("54.45.33.2");
64         when(aaiConfigurationMock.aaiProtocol()).thenReturn("https");
65         when(aaiConfigurationMock.aaiPort()).thenReturn(1234);
66         when(aaiConfigurationMock.aaiUserName()).thenReturn("PRH");
67         when(aaiConfigurationMock.aaiUserPassword()).thenReturn("PRH");
68         when(aaiConfigurationMock.aaiBasePath()).thenReturn("/aai/v11");
69         when(aaiConfigurationMock.aaiPnfPath()).thenReturn("/network/pnfs/pnf");
70         when(aaiConfigurationMock.aaiHeaders()).thenReturn(aaiHeaders);
71
72         aaiProducerReactiveHttpClient = new AaiProducerReactiveHttpClient(aaiConfigurationMock);
73
74         webClient = spy(WebClient.builder()
75             .defaultHeaders(httpHeaders -> httpHeaders.setAll(aaiHeaders))
76             .filter(basicAuthentication(aaiConfigurationMock.aaiUserName(), aaiConfigurationMock.aaiUserPassword()))
77             .build());
78
79         requestBodyUriSpec = mock(WebClient.RequestBodyUriSpec.class);
80         responseSpec = mock(ResponseSpec.class);
81     }
82
83     private static void setupHeaders() {
84         aaiHeaders = new HashMap<>();
85         aaiHeaders.put("X-FromAppId", "PRH");
86         aaiHeaders.put("X-TransactionId", "vv-temp");
87         aaiHeaders.put("Accept", "application/json");
88         aaiHeaders.put("Real-Time", "true");
89         aaiHeaders.put("Content-Type", "application/merge-patch+json");
90     }
91
92     @Test
93     void getAaiProducerResponse_shouldReturn200() {
94         //given
95         Mono<Integer> expectedResult = Mono.just(SUCCESS_RESPONSE);
96
97         //when
98         mockWebClientDependantObject();
99         doReturn(expectedResult).when(responseSpec).bodyToMono(Integer.class);
100         aaiProducerReactiveHttpClient.createAaiWebClient(webClient);
101         Mono<Integer> response = aaiProducerReactiveHttpClient.getAaiProducerResponse(Mono.just(dmaapModel));
102
103         //then
104         StepVerifier.create(response).expectSubscription()
105             .expectNextMatches(results -> {
106                 Assertions.assertEquals(results, expectedResult.block());
107                 return true;
108             }).verifyComplete();
109     }
110
111     @Test
112     void getHttpResponse_whenUriSyntaxExceptionHasBeenThrown() throws URISyntaxException {
113         ///given
114         aaiProducerReactiveHttpClient = spy(aaiProducerReactiveHttpClient);
115         //when
116         when(webClient.patch()).thenReturn(requestBodyUriSpec);
117         aaiProducerReactiveHttpClient.createAaiWebClient(webClient);
118         when(aaiProducerReactiveHttpClient.getUri("pnfName")).thenThrow(URISyntaxException.class);
119
120         //then
121         StepVerifier.create(
122             aaiProducerReactiveHttpClient.getAaiProducerResponse(
123                 Mono.just(dmaapModel)
124             )).expectSubscription().expectError(Exception.class).verify();
125     }
126
127     private void mockWebClientDependantObject() {
128         WebClient.RequestHeadersSpec requestHeadersSpec = mock(WebClient.RequestHeadersSpec.class);
129         when(webClient.patch()).thenReturn(requestBodyUriSpec);
130         when(requestBodyUriSpec.uri((URI) any())).thenReturn(requestBodyUriSpec);
131         when(requestBodyUriSpec.header(any(), any())).thenReturn(requestBodyUriSpec);
132         when(requestBodyUriSpec.body(any())).thenReturn(requestHeadersSpec);
133         doReturn(responseSpec).when(requestHeadersSpec).retrieve();
134         doReturn(responseSpec).when(responseSpec).onStatus(any(), any());
135     }
136
137 }
138