102c89ac1eace454e10314f154bb4cc79dc4f786
[vid.git] / vid-app-common / src / test / java / org / onap / vid / mso / RestMsoImplementationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2019 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.vid.mso;
22
23 import io.joshworks.restclient.request.HttpRequest;
24 import org.glassfish.jersey.client.JerseyInvocation;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.onap.vid.aai.util.HttpsAuthClient;
28 import org.onap.vid.changeManagement.RequestDetailsWrapper;
29 import org.onap.vid.exceptions.GenericUncheckedException;
30 import org.onap.vid.mso.rest.RequestDetails;
31 import org.onap.vid.utils.SystemPropertiesWrapper;
32 import org.springframework.http.HttpMethod;
33 import org.springframework.http.HttpStatus;
34 import org.testng.annotations.BeforeClass;
35 import org.testng.annotations.Test;
36
37 import javax.ws.rs.client.Client;
38 import javax.ws.rs.client.Entity;
39 import javax.ws.rs.client.WebTarget;
40 import javax.ws.rs.core.MultivaluedHashMap;
41 import javax.ws.rs.core.MultivaluedMap;
42 import javax.ws.rs.core.Response;
43
44 import java.util.Optional;
45
46 import static org.assertj.core.api.Java6Assertions.assertThat;
47 import static org.mockito.ArgumentMatchers.any;
48 import static org.mockito.ArgumentMatchers.eq;
49 import static org.mockito.Mockito.when;
50 import static org.mockito.MockitoAnnotations.initMocks;
51
52 public class RestMsoImplementationTest  {
53
54     @Mock
55     private HttpRequest httpRequest;
56
57     @Mock
58     private Client mockClient;
59
60     @Mock
61     private HttpsAuthClient mockHttpsAuthClient;
62
63     @Mock
64     private WebTarget webTarget;
65
66     @Mock
67     private javax.ws.rs.client.Invocation.Builder builder;
68
69     @Mock
70     private Response response;
71
72     @Mock
73     private JerseyInvocation jerseyInvocation;
74
75     @Mock
76     private SystemPropertiesWrapper systemProperties;
77
78     @InjectMocks
79     private RestMsoImplementation restMsoImplementation = new RestMsoImplementation(mockHttpsAuthClient, systemProperties);
80
81     private String path = "/test_path/";
82     private String rawData = "test-row-data";
83
84     @BeforeClass
85     public void setUp(){
86         initMocks(this);
87         when(systemProperties.getProperty(MsoProperties.MSO_PASSWORD)).thenReturn("OBF:1ghz1kfx1j1w1m7w1i271e8q1eas1hzj1m4i1iyy1kch1gdz");
88     }
89
90     @Test
91     public void shouldProperlyInitMsoClient() {
92         //  when
93         MultivaluedHashMap<String, Object> result = restMsoImplementation.initMsoClient();
94
95         //  then
96         assertThat(result).containsKeys("Authorization","X-ONAP-PartnerName");
97         assertThat(result).doesNotContainKey("notExistingKey");
98     }
99
100     @Test
101     public void shouldProperlyGetRestObjectWithRequestInfo() {
102         //  given
103         RestObject<HttpRequest> restObject = new RestObject<>();
104
105         prepareMocks(rawData, HttpStatus.ACCEPTED.value(),"");
106
107         //  when
108         RestObjectWithRequestInfo<HttpRequest> response = restMsoImplementation.Get(httpRequest, path, restObject,false);
109
110         //  then
111         assertThat(response.getRequestedUrl()).contains(path);
112         assertThat(response.getRawData()).isEqualTo(rawData);
113         assertThat(response.getHttpCode()).isEqualTo(HttpStatus.ACCEPTED.value());
114         assertThat(response.getHttpMethod()).isEqualTo(HttpMethod.GET);
115     }
116
117     @Test( expectedExceptions = GenericUncheckedException.class)
118     public void shouldThrowExceptionWhenGetRestObjectWithRequestInfoGetsWrongStatus() {
119         //  given
120         RestObject<HttpRequest> restObject = new RestObject<>();
121
122         prepareMocks("",HttpStatus.BAD_REQUEST.value(),"");
123
124         //  when
125         restMsoImplementation.Get(httpRequest, "", restObject,false);
126     }
127
128     @Test( expectedExceptions = MsoTestException.class)
129     public void shouldThrowExceptionWhenGetRestObjectWithRequestInfoGetsWrongParameters() {
130         //  given
131         RestObject<HttpRequest> restObject = new RestObject<>();
132
133         prepareMocks("",HttpStatus.ACCEPTED.value(),"");
134         when(systemProperties.getProperty(MsoProperties.MSO_SERVER_URL)).thenReturn("SAMPLE_URL");
135         when(mockClient.target("SAMPLE_URL")).thenThrow(new MsoTestException("test-target-exception"));
136
137         //  when
138         restMsoImplementation.Get(httpRequest, "", restObject,false);
139     }
140
141     @Test()
142     public void shouldProperlyGetRestObjectForObjectWithRequestInfoAndAcceptCode() {
143         //  given
144         prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"");
145
146         //  when
147         RestObject response = restMsoImplementation.GetForObject(path, HttpRequest.class);
148
149         //  then
150         assertThat(response.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED.value());
151         assertThat(response.getRaw()).isEqualTo(rawData);
152     }
153
154     @Test()
155     public void shouldProperlyGetRestObjectForObjectWithRequestInfoAndBadRequestCode() {
156         //  given
157         prepareMocks(rawData,HttpStatus.BAD_REQUEST.value(),"");
158
159         //  when
160         RestObject response = restMsoImplementation.GetForObject(path, HttpRequest.class);
161
162         //  then
163         assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST.value());
164         assertThat(response.getRaw()).isEqualTo(rawData);
165     }
166
167     @Test()
168     public void shouldProperlyDeleteRestObjectWithStatusHttpAccepted() {
169         //  given
170         RestObject<HttpRequest> restObject = new RestObject<>();
171
172         prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"DELETE");
173
174         //  when
175         restMsoImplementation.Delete(httpRequest, "testObject", path, restObject);
176
177         //  then
178         assertThat(restObject.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED.value());
179     }
180
181     @Test()
182     public void shouldProperlyDeleteRestObjectWithStatusOK() {
183         //  given
184         RestObject<HttpRequest> restObject = new RestObject<>();
185
186         prepareMocks(rawData,HttpStatus.OK.value(),"DELETE");
187
188         //  when
189         restMsoImplementation.Delete(httpRequest, "testObject", path, restObject);
190
191         //  then
192         assertThat(restObject.getStatusCode()).isEqualTo(HttpStatus.OK.value());
193     }
194
195     @Test()
196     public void shouldProperlyReturnFromDeleteWithStatusBadRequest() {
197         //  given
198         RestObject<HttpRequest> restObject = new RestObject<>();
199
200         prepareMocks(rawData,HttpStatus.BAD_REQUEST.value(),"DELETE");
201
202         //  when
203         restMsoImplementation.Delete(httpRequest, "testObject", path, restObject);
204
205         //  then
206         assertThat(restObject.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST.value());
207     }
208
209     @Test()
210     public void shouldProperlyReturnFromDeleteWithStatusOtherThenAbove() {
211         //  given
212         RestObject<HttpRequest> restObject = new RestObject<>();
213         prepareMocks(rawData,HttpStatus.NOT_EXTENDED.value(),"DELETE");
214
215         //  when
216         restMsoImplementation.Delete(httpRequest, "testObject", path, restObject);
217
218         //  then
219         assertThat(restObject.getStatusCode()).isEqualTo(HttpStatus.NOT_EXTENDED.value());
220     }
221
222     @Test( expectedExceptions = MsoTestException.class)
223     public void shouldThrowExceptionWhenCallsDeleteWithWrongParameters() {
224         //  given
225         when(mockClient.target(any(String.class))).thenThrow(new MsoTestException("testDeleteException"));
226
227         //  when
228         restMsoImplementation.Delete(httpRequest, "testObject", "", null);
229     }
230
231     @Test( expectedExceptions = NullPointerException.class)
232     public void shouldThrowExceptionWhenCallsDeleteWithWrongObjectType() {
233         //  given
234         RestObject<HttpRequest> restObject = new RestObject<>();
235         prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"DELETE");
236
237         //  when
238         restMsoImplementation.Delete(null, "testObject", path, restObject);
239     }
240
241     @Test
242     public void shouldProperlyPostForObject() {
243         //  given
244         RequestDetails requestDetails = new RequestDetails();
245
246         RestObject<HttpRequest> expectedResponse = new RestObject<>();
247         expectedResponse.setStatusCode(HttpStatus.ACCEPTED.value());
248         expectedResponse.setRaw(rawData);
249
250         prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"POST");
251
252         //  when
253         RestObject<HttpRequest> response = restMsoImplementation.PostForObject(requestDetails, path, HttpRequest.class);
254
255         //  then
256         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
257     }
258
259     @Test
260     public void shouldProperlyDeleteForObject() {
261         //  given
262         RequestDetails requestDetails = new RequestDetails();
263
264         RestObject<HttpRequest> expectedResponse = new RestObject<>();
265         expectedResponse.setStatusCode(HttpStatus.ACCEPTED.value());
266         expectedResponse.setRaw(rawData);
267
268         prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"DELETE");
269
270         //  when
271         RestObject<HttpRequest> response = restMsoImplementation.DeleteForObject(requestDetails, path, HttpRequest.class);
272
273         //  then
274         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
275     }
276
277     @Test
278     public void shouldProperlyPost() {
279         //  given
280         RequestDetails requestDetails = new RequestDetails();
281         RestObject<String> response = new RestObject<>();
282
283         RestObject<String> expectedResponse = new RestObject<>();
284         expectedResponse.setStatusCode(HttpStatus.ACCEPTED.value());
285         expectedResponse.setRaw(rawData);
286
287         prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"POST");
288
289         //  when
290         restMsoImplementation.Post(rawData,requestDetails, path, response);
291
292         //  then
293         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
294     }
295
296     @Test
297     public void shouldProperlyPrepareClient() {
298         //  given
299         String method = "POST";
300         prepareMocks(rawData,HttpStatus.ACCEPTED.value(),method);
301
302         //  when
303         javax.ws.rs.client.Invocation.Builder response = restMsoImplementation.prepareClient(path, method);
304
305         //  then
306         assertThat(response).isEqualTo(builder);
307     }
308
309     @Test
310     public void shouldCreatRestObjectOnlyWithHttpMethod() {
311         //  given
312         String method = "GET";
313         prepareMocks(rawData,HttpStatus.ACCEPTED.value(),method);
314
315         RestObject<String> expectedResponse = new RestObject<>();
316         expectedResponse.setStatusCode(HttpStatus.ACCEPTED.value());
317         expectedResponse.setRaw(rawData);
318
319         //  when
320         RestObject<String> response = restMsoImplementation.restCall(HttpMethod.GET, String.class, null, path, Optional.empty());
321
322         //  then
323         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
324     }
325
326     @Test( expectedExceptions = MsoTestException.class)
327     public void shouldThrowExceptionWhenCreateRestObjectIsCalledWithoutDefinedClient() {
328         //  given
329         when(mockClient.target(any(String.class))).thenThrow(new MsoTestException("testNoClientException"));
330
331         //  when
332         restMsoImplementation.restCall(HttpMethod.GET, String.class, null, "", Optional.empty());
333     }
334
335     @Test
336     public void shouldProperlyPutRestObjectWithProperParametersAndStatusAccepted() {
337         //  given
338         String method = "PUT";
339         prepareMocks(rawData,HttpStatus.ACCEPTED.value(),method);
340
341         org.onap.vid.changeManagement.RequestDetailsWrapper requestDetailsWrapper = new RequestDetailsWrapper();
342         RestObject<String> response = new RestObject<>();
343
344         RestObject<String> expectedResponse = new RestObject<>();
345         expectedResponse.setStatusCode(HttpStatus.ACCEPTED.value());
346         expectedResponse.set(rawData);
347
348         //  when
349         restMsoImplementation.Put("testPutBody", requestDetailsWrapper , path, response);
350
351         //  then
352         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
353     }
354
355     @Test
356     public void shouldProperlyPutRestObjectWithProperParametersAndStatusMultipleChoices() {
357         //  given
358         String method = "PUT";
359         prepareMocks(rawData,HttpStatus.MULTIPLE_CHOICES.value(),method);
360
361         org.onap.vid.changeManagement.RequestDetailsWrapper requestDetailsWrapper = new RequestDetailsWrapper();
362         RestObject<String> response = new RestObject<>();
363
364         RestObject<String> expectedResponse = new RestObject<>();
365         expectedResponse.setStatusCode(HttpStatus.MULTIPLE_CHOICES.value());
366         expectedResponse.set(rawData);
367
368         //  when
369         restMsoImplementation.Put("testPutBody", requestDetailsWrapper , path, response);
370
371         //  then
372         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
373     }
374
375     @Test( expectedExceptions = MsoTestException.class)
376     public void shouldThrowExceptionWhenCallsPutWithWrongParameters() {
377         //  given
378         when(mockClient.target(any(String.class))).thenThrow(new MsoTestException("testDeleteException"));
379         org.onap.vid.changeManagement.RequestDetailsWrapper requestDetailsWrapper = new RequestDetailsWrapper();
380
381         //  when
382         restMsoImplementation.Put(null, requestDetailsWrapper, "", null);
383     }
384
385     @Test( expectedExceptions = NullPointerException.class)
386     public void shouldThrowExceptionWhenCallsPutWithWrongObjectType() {
387         //  given
388         RestObject<HttpRequest> restObject = new RestObject<>();
389         org.onap.vid.changeManagement.RequestDetailsWrapper requestDetailsWrapper = new RequestDetailsWrapper();
390
391         prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"DELETE");
392
393         //  when
394         restMsoImplementation.Put(null, requestDetailsWrapper, path, restObject);
395     }
396
397
398
399     private void prepareMocks(String rawData,int status,String httpMethod) {
400
401         when(mockClient.target(any(String.class))).thenReturn(webTarget);
402         when(webTarget.request()).thenReturn(builder);
403
404
405         when(builder.accept(any(String.class))).thenReturn(builder);
406         when(builder.headers(any(MultivaluedMap.class))).thenReturn(builder);
407         when(builder.get()).thenReturn(response);
408
409         when(builder.build( eq(httpMethod), any(Entity.class))).thenReturn(jerseyInvocation);
410         when(builder.build( eq(httpMethod))).thenReturn(jerseyInvocation);
411
412         when(builder.put( any(Entity.class))).thenReturn(response);
413         when(jerseyInvocation.invoke()).thenReturn(response);
414
415
416         when(response.getStatus()).thenReturn(status);
417         when(response.readEntity(String.class)).thenReturn(rawData);
418     }
419
420     private class MsoTestException extends RuntimeException{
421         MsoTestException(String testException) {
422             super(testException);
423         }
424     }
425
426 }