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