Add http put AAI client for SDK
[dcaegen2/services/sdk.git] / rest-services / aai-client / src / test / java / org / onap / dcaegen2 / services / sdk / rest / services / aai / client / service / http / put / AaiReactiveHttpPutClientTest.java
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.put;
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
28 import org.onap.dcaegen2.services.sdk.rest.services.model.AaiModel;
29 import org.onap.dcaegen2.services.sdk.rest.services.model.JsonBodyBuilder;
30 import org.springframework.web.reactive.function.client.ClientResponse;
31 import org.springframework.web.reactive.function.client.WebClient;
32 import reactor.core.publisher.Mono;
33 import reactor.test.StepVerifier;
34
35 import java.net.URI;
36 import java.util.HashMap;
37 import java.util.Map;
38
39 import static org.mockito.ArgumentMatchers.any;
40 import static org.mockito.Mockito.doReturn;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.spy;
43 import static org.mockito.Mockito.when;
44 import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
45
46
47 class AaiReactiveHttpPutClientTest {
48     private static final Integer SUCCESS_RESPONSE = 200;
49     private static AaiClientConfiguration aaiConfigurationMock = mock(AaiClientConfiguration.class);
50
51
52     private AaiReactiveHttpPutClient httpClient;
53     private WebClient webClient;
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     private AaiModel aaiModel = mock(AaiModel.class);
62     private JsonBodyBuilder<AaiModel> jsonBodyBuilder = mock(JsonBodyBuilder.class);
63
64     @BeforeEach
65     void setUp() {
66         setupHeaders();
67         clientResponse = mock(ClientResponse.class);
68         clientResponseMono = Mono.just(clientResponse);
69
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         when(aaiModel.getCorrelationId()).thenReturn("NOKnhfsadhff");
80
81         when(jsonBodyBuilder.createJsonBody(aaiModel)).thenReturn(
82                 "{\"correlationId\":\"NOKnhfsadhff\"," +
83                         "\"ipaddress-v4\":\"256.22.33.155\", " +
84                         "\"ipaddress-v6\":\"200J:0db8:85a3:0000:0000:8a2e:0370:7334\"}");
85
86         httpClient = new AaiReactiveHttpPutClient(aaiConfigurationMock, jsonBodyBuilder);
87
88         webClient = spy(WebClient.builder()
89                 .defaultHeaders(httpHeaders -> httpHeaders.setAll(aaiHeaders))
90                 .filter(basicAuthentication(aaiConfigurationMock.aaiUserName(), aaiConfigurationMock.aaiUserPassword()))
91                 .build());
92
93         requestBodyUriSpec = mock(WebClient.RequestBodyUriSpec.class);
94         responseSpec = mock(WebClient.ResponseSpec.class);
95     }
96
97     @Test
98     void getAaiProducerResponse_shouldReturn200() {
99         //given
100         Mono<Integer> expectedResult = Mono.just(SUCCESS_RESPONSE);
101
102         //when
103         mockWebClientDependantObject();
104         doReturn(expectedResult).when(responseSpec).bodyToMono(Integer.class);
105         httpClient.createAaiWebClient(webClient);
106
107         //then
108         StepVerifier.create(httpClient.getAaiProducerResponse(aaiModel)).expectSubscription()
109                 .expectNextMatches(results -> {
110                     Assertions.assertEquals(results, clientResponse);
111                     return true;
112                 }).verifyComplete();
113     }
114
115
116     @Test
117     void getAppropriateUri_whenPassingCorrectedPathForPnf() {
118         Assertions.assertEquals(httpClient.getUri("NOKnhfsadhff"),
119                 URI.create("https://54.45.33.2:1234/aai/v11/network/pnfs/pnf/NOKnhfsadhff"));
120     }
121
122
123     private void setupHeaders() {
124         aaiHeaders = new HashMap<>();
125         aaiHeaders.put("X-FromAppId", "PRH");
126         aaiHeaders.put("X-TransactionId", "vv-temp");
127         aaiHeaders.put("Accept", "application/json");
128         aaiHeaders.put("Real-Time", "true");
129         aaiHeaders.put("Content-Type", "application/json");
130     }
131
132     private void mockWebClientDependantObject() {
133         WebClient.RequestHeadersSpec requestHeadersSpec = mock(WebClient.RequestHeadersSpec.class);
134         when(webClient.put()).thenReturn(requestBodyUriSpec);
135         when(requestBodyUriSpec.uri((URI) any())).thenReturn(requestBodyUriSpec);
136         when(requestBodyUriSpec.header(any(), any())).thenReturn(requestBodyUriSpec);
137         when(requestBodyUriSpec.body(any(), (Class<Object>) any())).thenReturn(requestHeadersSpec);
138         when(requestHeadersSpec.exchange()).thenReturn(clientResponseMono);
139     }
140 }