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