d8002fb2e635cafca5a7058338343a633abeeb58
[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 AaiClientConfiguration aaiConfigurationMock = mock(AaiClientConfiguration.class);
52
53
54     private AaiProducerReactiveHttpClient aaiProducerReactiveHttpClient;
55     private WebClient webClient = mock(WebClient.class);
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.getCorrelationId()).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
109     @Test
110     void getAppropriateUri_whenPassingCorrectedPathForPnf() {
111         Assertions.assertEquals(aaiProducerReactiveHttpClient.getUri("NOKnhfsadhff"),
112             URI.create("https://54.45.33.2:1234/aai/v11/network/pnfs/pnf/NOKnhfsadhff"));
113     }
114
115
116     private void setupHeaders() {
117         aaiHeaders = new HashMap<>();
118         aaiHeaders.put("X-FromAppId", "PRH");
119         aaiHeaders.put("X-TransactionId", "vv-temp");
120         aaiHeaders.put("Accept", "application/json");
121         aaiHeaders.put("Real-Time", "true");
122         aaiHeaders.put("Content-Type", "application/merge-patch+json");
123     }
124
125     private void mockWebClientDependantObject() {
126         WebClient.RequestHeadersSpec requestHeadersSpec = mock(WebClient.RequestHeadersSpec.class);
127         when(webClient.patch()).thenReturn(requestBodyUriSpec);
128         when(requestBodyUriSpec.uri((URI) any())).thenReturn(requestBodyUriSpec);
129         when(requestBodyUriSpec.header(any(), any())).thenReturn(requestBodyUriSpec);
130         when(requestBodyUriSpec.body(any(), (Class<Object>) any())).thenReturn(requestHeadersSpec);
131         when(requestHeadersSpec.exchange()).thenReturn(clientResponseMono);
132     }
133 }
134