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