de02d62ef6ec8e026557a8c0a2e2a4e5a3edd9dd
[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
32 import java.util.HashMap;
33 import java.util.Map;
34
35 import org.junit.jupiter.api.Assertions;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration;
39 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
40 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModelForUnitTest;
41 import org.springframework.web.reactive.function.client.ClientResponse;
42 import org.springframework.web.reactive.function.client.WebClient;
43 import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
44 import reactor.core.publisher.Mono;
45 import reactor.test.StepVerifier;
46
47 class AaiProducerReactiveHttpClientTest {
48
49     private static final Integer SUCCESS_RESPONSE = 200;
50     private static AaiClientConfiguration aaiConfigurationMock = mock(AaiClientConfiguration.class);
51
52
53     private AaiProducerReactiveHttpClient aaiProducerReactiveHttpClient;
54     private WebClient webClient = mock(WebClient.class);
55     private ConsumerDmaapModel dmaapModel = spy(new ConsumerDmaapModelForUnitTest());
56     private WebClient.RequestBodyUriSpec requestBodyUriSpec;
57     private ResponseSpec responseSpec;
58
59     private Map<String, String> aaiHeaders;
60     private ClientResponse clientResponse;
61     private Mono<ClientResponse> clientResponseMono;
62
63     @BeforeEach
64     void setUp() {
65         setupHeaders();
66         clientResponse = mock(ClientResponse.class);
67         clientResponseMono = Mono.just(clientResponse);
68         when(dmaapModel.getCorrelationId()).thenReturn("NOKnhfsadhff");
69         when(aaiConfigurationMock.aaiHost()).thenReturn("54.45.33.2");
70         when(aaiConfigurationMock.aaiProtocol()).thenReturn("https");
71         when(aaiConfigurationMock.aaiPort()).thenReturn(1234);
72         when(aaiConfigurationMock.aaiUserName()).thenReturn("PRH");
73         when(aaiConfigurationMock.aaiUserPassword()).thenReturn("PRH");
74         when(aaiConfigurationMock.aaiBasePath()).thenReturn("/aai/v11");
75         when(aaiConfigurationMock.aaiPnfPath()).thenReturn("/network/pnfs/pnf");
76         when(aaiConfigurationMock.aaiHeaders()).thenReturn(aaiHeaders);
77
78         aaiProducerReactiveHttpClient = new AaiProducerReactiveHttpClient(aaiConfigurationMock);
79
80         webClient = spy(WebClient.builder()
81             .defaultHeaders(httpHeaders -> httpHeaders.setAll(aaiHeaders))
82             .filter(basicAuthentication(aaiConfigurationMock.aaiUserName(), aaiConfigurationMock.aaiUserPassword()))
83             .build());
84
85         requestBodyUriSpec = mock(WebClient.RequestBodyUriSpec.class);
86         responseSpec = mock(ResponseSpec.class);
87     }
88
89     @Test
90     void getAaiProducerResponse_shouldReturn200() {
91         //given
92         Mono<Integer> expectedResult = Mono.just(SUCCESS_RESPONSE);
93
94         //when
95         mockWebClientDependantObject();
96         doReturn(expectedResult).when(responseSpec).bodyToMono(Integer.class);
97         aaiProducerReactiveHttpClient.createAaiWebClient(webClient);
98
99         //then
100         StepVerifier.create(aaiProducerReactiveHttpClient.getAaiProducerResponse(dmaapModel)).expectSubscription()
101             .expectNextMatches(results -> {
102                 Assertions.assertEquals(results, clientResponse);
103                 return true;
104             }).verifyComplete();
105     }
106
107
108     @Test
109     void getAppropriateUri_whenPassingCorrectedPathForPnf() {
110         Assertions.assertEquals(aaiProducerReactiveHttpClient.getUri("NOKnhfsadhff"),
111             URI.create("https://54.45.33.2:1234/aai/v11/network/pnfs/pnf/NOKnhfsadhff"));
112     }
113
114
115     private void setupHeaders() {
116         aaiHeaders = new HashMap<>();
117         aaiHeaders.put("X-FromAppId", "PRH");
118         aaiHeaders.put("X-TransactionId", "vv-temp");
119         aaiHeaders.put("Accept", "application/json");
120         aaiHeaders.put("Real-Time", "true");
121         aaiHeaders.put("Content-Type", "application/merge-patch+json");
122     }
123
124     private void mockWebClientDependantObject() {
125         WebClient.RequestHeadersSpec requestHeadersSpec = mock(WebClient.RequestHeadersSpec.class);
126         when(webClient.patch()).thenReturn(requestBodyUriSpec);
127         when(requestBodyUriSpec.uri((URI) any())).thenReturn(requestBodyUriSpec);
128         when(requestBodyUriSpec.header(any(), any())).thenReturn(requestBodyUriSpec);
129         when(requestBodyUriSpec.body(any(), (Class<Object>) any())).thenReturn(requestHeadersSpec);
130         when(requestHeadersSpec.exchange()).thenReturn(clientResponseMono);
131     }
132 }
133