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