fixing bugs connected with undefined parameters
[vid.git] / vid-app-common / src / test / java / org / onap / vid / mso / rest / MsoRestClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2018 - 2019 Nokia. 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 package org.onap.vid.mso.rest;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.hamcrest.Matchers.allOf;
25 import static org.hamcrest.Matchers.hasEntry;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyMap;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.when;
30 import static org.mockito.MockitoAnnotations.initMocks;
31
32 import io.joshworks.restclient.http.HttpResponse;
33 import io.joshworks.restclient.http.JsonMapper;
34 import org.apache.http.ProtocolVersion;
35 import org.apache.http.StatusLine;
36 import org.apache.http.message.BasicHttpResponse;
37 import org.apache.http.message.BasicStatusLine;
38 import org.jetbrains.annotations.NotNull;
39 import org.mockito.Mock;
40 import org.onap.portalsdk.core.util.SystemProperties;
41 import org.onap.vid.changeManagement.RequestDetailsWrapper;
42 import org.onap.vid.changeManagement.RequestParameters;
43 import org.onap.vid.changeManagement.WorkflowRequestDetail;
44 import org.onap.vid.client.SyncRestClient;
45 import org.onap.vid.controller.LocalWebConfig;
46 import org.onap.vid.model.RequestReferencesContainer;
47 import org.onap.vid.mso.MsoProperties;
48 import org.onap.vid.mso.MsoResponseWrapper;
49 import org.onap.vid.mso.MsoResponseWrapperInterface;
50 import org.onap.vid.mso.MsoUtil;
51 import org.onap.vid.mso.RestObject;
52 import org.onap.vid.mso.model.CloudConfiguration;
53 import org.onap.vid.mso.model.RequestReferences;
54 import org.onap.vid.utils.SystemPropertiesWrapper;
55 import org.springframework.test.context.ContextConfiguration;
56 import org.springframework.test.context.web.WebAppConfiguration;
57 import org.testng.annotations.BeforeClass;
58 import org.testng.annotations.Test;
59
60 import java.util.ArrayList;
61 import java.util.HashMap;
62 import java.util.List;
63 import java.util.Map;
64 import java.util.UUID;
65
66 import static org.mockito.ArgumentMatchers.refEq;
67 import static org.mockito.hamcrest.MockitoHamcrest.argThat;
68
69
70 @ContextConfiguration(classes = {LocalWebConfig.class, SystemProperties.class})
71 @WebAppConfiguration
72 public class MsoRestClientTest {
73
74
75     private final String baseUrl = "http://testURL/";
76
77     @Mock
78     private SyncRestClient client;
79
80     @Mock
81     private SystemPropertiesWrapper systemProperties;
82
83     private MsoRestClientNew restClient;
84
85
86     @BeforeClass
87     private void setUp(){
88         initMocks(this);
89         when(systemProperties.getProperty(MsoProperties.MSO_PASSWORD)).thenReturn("OBF:1ghz1kfx1j1w1m7w1i271e8q1eas1hzj1m4i1iyy1kch1gdz");
90         when(systemProperties.getProperty("app_display_name")).thenReturn("vid");
91         restClient = new MsoRestClientNew(client,baseUrl,null,systemProperties);
92     }
93
94     @Test
95     public void shouldProperlyCreateServiceInstance() {
96         //  given
97         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
98
99         String endpoint = "testEndpoint";
100         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
101         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
102
103         when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
104
105         //  when
106         MsoResponseWrapper response = restClient.createSvcInstance(requestDetails,endpoint);
107
108         //  then
109         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
110     }
111
112     @Test( expectedExceptions = MsoTestException.class)
113     public void shouldThrowExceptionWhenCreateSvcInstanceGetsWrongParameters() {
114         //  given
115         String endpoint = "";
116
117         when( client.post( eq(baseUrl+endpoint),anyMap(),eq(null),eq(String.class) )  ).
118                 thenThrow(new MsoTestException("test-post-exception"));
119
120         //  when
121         restClient.createSvcInstance(null,endpoint);
122     }
123
124     @Test
125     public void shouldProperlyCreateE2eSvcInstance() {
126         //  given
127         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
128
129         String endpoint = "testEndpoint";
130         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
131         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
132
133         when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
134
135         //  when
136         MsoResponseWrapper response = restClient.createE2eSvcInstance(requestDetails,endpoint);
137
138         //  then
139         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
140     }
141
142     @Test
143     public void shouldProperlyCreateVnf() {
144         //  given
145         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
146
147         String endpoint = "testEndpoint";
148         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
149         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
150
151         when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
152
153         //  when
154         MsoResponseWrapper response = restClient.createVnf(requestDetails,endpoint);
155
156         //  then
157         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
158     }
159
160     @Test
161     public void shouldProperlyCreateNwInstance() {
162         //  given
163         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
164
165         String endpoint = "testEndpoint";
166         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
167         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
168
169         when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
170
171         //  when
172         MsoResponseWrapper response = restClient.createNwInstance(requestDetails,endpoint);
173
174         //  then
175         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
176     }
177
178     @Test
179     public void shouldProperlyCreateVolumeGroupInstance() {
180         //  given
181         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
182
183         String endpoint = "testEndpoint";
184         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
185         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
186
187         when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
188
189         //  when
190         MsoResponseWrapper response = restClient.createVolumeGroupInstance(requestDetails,endpoint);
191
192         //  then
193         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
194     }
195
196     @Test
197     public void shouldProperlyCreateVfModuleInstance() {
198         //  given
199         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
200
201         String endpoint = "testEndpoint";
202         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
203         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
204
205         when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
206
207         //  when
208         MsoResponseWrapper response = restClient.createVfModuleInstance(requestDetails,endpoint);
209
210         //  then
211         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
212     }
213
214     @Test
215     public void shouldProperlyScaleOutVFModuleInstance() {
216         //  given
217         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
218
219         RequestDetailsWrapper<RequestDetails> wrappedRequestDetails = new RequestDetailsWrapper<>(requestDetails);
220         String endpoint = "testEndpoint";
221         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
222         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
223
224         when( client.post( eq(baseUrl+endpoint),anyMap(),eq(wrappedRequestDetails),eq(String.class) )  ).thenReturn(httpResponse);
225
226         //  when
227         MsoResponseWrapper response = restClient.scaleOutVFModuleInstance(wrappedRequestDetails,endpoint);
228
229         //  then
230         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
231     }
232
233     @Test
234     public void shouldProperlyCreateConfigurationInstance() {
235         //  given
236         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
237
238         org.onap.vid.mso.rest.RequestDetailsWrapper wrappedRequestDetails = new  org.onap.vid.mso.rest.RequestDetailsWrapper(requestDetails);
239         String endpoint = "testEndpoint";
240         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
241         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
242
243         when( client.post( eq(baseUrl+endpoint),anyMap(),eq(wrappedRequestDetails),eq(String.class) )  ).thenReturn(httpResponse);
244
245         //  when
246         MsoResponseWrapper response = restClient.createConfigurationInstance(wrappedRequestDetails,endpoint);
247
248         //  then
249         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
250     }
251
252     @Test
253     public void shouldProperlyDeleteE2eSvcInstance() {
254         //  given
255         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
256
257         org.onap.vid.mso.rest.RequestDetailsWrapper wrappedRequestDetails = new  org.onap.vid.mso.rest.RequestDetailsWrapper(requestDetails);
258         String endpoint = "testEndpoint";
259         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
260         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
261
262         when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(wrappedRequestDetails),eq(String.class) )  ).thenReturn(httpResponse);
263
264         //  when
265         MsoResponseWrapper response = restClient.deleteE2eSvcInstance(wrappedRequestDetails,endpoint);
266
267         //  then
268         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
269     }
270
271     @Test
272     public void shouldProperlyDeleteSvcInstance() {
273         //  given
274         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
275
276         String endpoint = "testEndpoint";
277         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
278         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
279
280         when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
281
282         //  when
283         MsoResponseWrapper response = restClient.deleteSvcInstance(requestDetails,endpoint);
284
285         //  then
286         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
287     }
288
289     @Test
290     public void shouldProperlyUnassignSvcInstance() {
291         //  given
292         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
293
294         String endpoint = "testEndpoint";
295         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
296         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
297
298         when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
299
300         //  when
301         MsoResponseWrapper response = restClient.unassignSvcInstance(requestDetails,endpoint);
302
303         //  then
304         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
305     }
306
307     @Test
308     public void shouldProperlyDeleteVnf() {
309         //  given
310         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
311
312         String endpoint = "testEndpoint";
313         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
314         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
315
316         when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
317
318         //  when
319         MsoResponseWrapper response = restClient.deleteVnf(requestDetails,endpoint);
320
321         //  then
322         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
323     }
324
325     @Test
326     public void shouldProperlyDeleteVfModule() {
327         //  given
328         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
329
330         String endpoint = "testEndpoint";
331         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
332         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
333
334         when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
335
336         //  when
337         MsoResponseWrapper response = restClient.deleteVfModule(requestDetails,endpoint);
338
339         //  then
340         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
341     }
342
343     @Test
344     public void shouldProperlyDeleteVolumeGroupInstance() {
345         //  given
346         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
347
348         String endpoint = "testEndpoint";
349         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
350         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
351
352         when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
353
354         //  when
355         MsoResponseWrapper response = restClient.deleteVolumeGroupInstance(requestDetails,endpoint);
356
357         //  then
358         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
359     }
360
361     @Test
362     public void shouldProperlyDeleteNwInstance() {
363         //  given
364         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
365
366         String endpoint = "testEndpoint";
367         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
368         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
369
370         when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
371
372         //  when
373         MsoResponseWrapper response = restClient.deleteNwInstance(requestDetails,endpoint);
374
375         //  then
376         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
377     }
378
379     @Test
380     public void shouldProperlyGetOrchestrationRequest() {
381         //  given
382         RestObject restObject = generateMockMsoRestObject();
383
384         String endpoint = "testEndpoint";
385         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
386         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
387
388         when( client.get( eq(baseUrl+endpoint),anyMap(),anyMap(),eq(String.class) )  ).thenReturn(httpResponse);
389
390         //  when
391         MsoResponseWrapper response = restClient.getOrchestrationRequest(null,null,endpoint,restObject,true);
392
393         //  then
394         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
395     }
396
397     @Test
398     public void shouldProperlyGetOrchestrationRequestWithOnlyEndpoint() {
399         //  given
400         String endpoint = "testEndpoint";
401         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
402         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
403
404         when( client.get( eq(baseUrl+endpoint),anyMap(),anyMap(),eq(String.class) )  ).thenReturn(httpResponse);
405
406         //  when
407         MsoResponseWrapper response = restClient.getOrchestrationRequest(endpoint);
408
409         //  then
410         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
411     }
412
413     @Test
414     public void shouldProperlyGetManualTasksByRequestId() {
415         //  given
416         RestObject restObject = generateMockMsoRestObject();
417
418         String endpoint = "testEndpoint";
419         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
420         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
421
422         when( client.get( eq(baseUrl+endpoint),anyMap(),anyMap(),eq(String.class) )  ).thenReturn(httpResponse);
423
424         //  when
425         MsoResponseWrapper response = restClient.getManualTasksByRequestId(null,null,endpoint,restObject);
426
427         //  then
428         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
429     }
430
431     @Test(expectedExceptions = MsoTestException.class)
432     public void shouldThrowExceptionWhenGetManualTasksByRequestIdGetsWrongParameter() {
433         //  given
434         when( client.get( eq(baseUrl),anyMap(),anyMap(),eq(String.class) )  ).thenThrow(new MsoTestException("testsException"));
435
436         //  when
437         restClient.getManualTasksByRequestId(null,null,"",null);
438     }
439
440     @Test
441     public void shouldProperlyCompleteManualTask() {
442         //  given
443         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
444         RestObject restObject = generateMockMsoRestObject();
445
446         String endpoint = "testEndpoint";
447         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
448         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
449
450         when( client.post( eq(baseUrl+endpoint), anyMap(), eq(requestDetails), eq(String.class) )  ).thenReturn(httpResponse);
451
452         //  when
453         MsoResponseWrapper response = restClient.completeManualTask(requestDetails,null,null,endpoint,restObject);
454
455         //  then
456         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
457     }
458
459     @Test(expectedExceptions = MsoTestException.class)
460     public void shouldThrowExceptionWhenCompleteManualTaskWrongParameter() {
461         //  given
462         when( client.post( eq(baseUrl),anyMap(),eq(null), eq(String.class) )  ).thenThrow(new MsoTestException("testsException"));
463
464         //  when
465         restClient.completeManualTask(null, null,null,"",null);
466     }
467
468     @Test
469     public void shouldProperlyReplaceVnf() {
470         //  given
471         org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest();
472
473         String endpoint = "testEndpoint";
474         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
475         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
476
477         when( client.post( eq(baseUrl+endpoint), anyMap(), any(RequestDetailsWrapper.class), eq(String.class) )  ).thenReturn(httpResponse);
478
479         //  when
480         MsoResponseWrapper response = restClient.replaceVnf(requestDetails,endpoint);
481
482         //  then
483         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
484     }
485
486     @Test
487     public void shouldProperlyReplaceVnfWithStatus202() {
488         //  given
489         org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest();
490
491         String endpoint = "testEndpoint";
492         HttpResponse<String> httpResponse = createOkResponse();
493         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
494
495         when( client.post( eq(baseUrl+endpoint), anyMap(), any(RequestDetailsWrapper.class), eq(String.class) )  ).thenReturn(httpResponse);
496
497         //  when
498         MsoResponseWrapper response = restClient.replaceVnf(requestDetails,endpoint);
499
500         //  then
501         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
502     }
503
504     @Test( expectedExceptions = MsoTestException.class)
505     public void shouldThrowExceptionWhenReplaceVnfGetsWrongParameters() {
506         //  given
507         org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest();
508
509         when( client.post( eq(baseUrl), anyMap(), any(RequestDetailsWrapper.class), eq(String.class) )  ).thenThrow(new MsoTestException("test-post-exception"));
510
511         //  when
512         restClient.replaceVnf(requestDetails, "");
513     }
514
515     @Test
516     public void shouldProperlyDeleteConfiguration() {
517         //  given
518         org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest();
519         org.onap.vid.mso.rest.RequestDetailsWrapper requestDetailsWrapper = new org.onap.vid.mso.rest.RequestDetailsWrapper(requestDetails);
520
521         String endpoint = "testEndpoint";
522         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
523         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
524
525         when( client.delete( eq(baseUrl+endpoint), anyMap(), eq(requestDetailsWrapper), eq(String.class) )  ).thenReturn(httpResponse);
526
527         //  when
528         MsoResponseWrapper response = restClient.deleteConfiguration(requestDetailsWrapper,endpoint);
529
530         //  then
531         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
532     }
533
534     @Test( expectedExceptions = MsoTestException.class )
535     public void shouldThrowExceptionWhenProperlyDeleteConfigurationGetsWrongParameters() {
536         //  given
537         when( client.delete( eq(baseUrl), anyMap(), eq(null), eq(String.class) )  ).thenThrow(new MsoTestException("test-delete-exception"));
538
539         //  when
540         restClient.deleteConfiguration(null,"");
541     }
542
543     @Test
544     public void shouldProperlySetConfigurationActiveStatus() {
545         //  given
546         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
547
548         String endpoint = "testEndpoint";
549         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
550         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
551
552         when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse);
553
554         //  when
555         MsoResponseWrapper response = restClient.setConfigurationActiveStatus(requestDetails, endpoint);
556
557         //  then
558         assertThat(expectedResponse).isEqualToComparingFieldByField(response);
559     }
560
561     @Test(expectedExceptions = MsoTestException.class)
562     public void shouldThrowExceptionWhenSetConfigurationActiveStatusGetsWrongParameters() {
563         //  given
564
565         when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception"));
566
567         //  when
568         restClient.setConfigurationActiveStatus(null, "");
569     }
570
571     @Test
572     public void shouldProperlySetPortOnConfigurationStatus() {
573         //  given
574         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
575
576         String endpoint = "testEndpoint";
577         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
578         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
579
580         when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse);
581
582         //  when
583         MsoResponseWrapper response = restClient.setPortOnConfigurationStatus(requestDetails, endpoint);
584
585         //  then
586         assertThat(expectedResponse).isEqualToComparingFieldByField(response);
587     }
588
589     @Test(expectedExceptions = MsoTestException.class)
590     public void shouldThrowExceptionWhenSetPortOnConfigurationStatusGetsWrongParameters() {
591         //  given
592         String endpoint = "";
593
594         when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception"));
595
596         //  when
597         restClient.setPortOnConfigurationStatus(null, endpoint);
598     }
599
600     @Test
601     public void shouldProperlyChangeManagementUpdate() {
602         //  given
603         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
604         RequestDetailsWrapper<RequestDetails> requestDetailsWrapper = new RequestDetailsWrapper<>(requestDetails);
605
606         String endpoint = "testEndpoint";
607         HttpResponse<RequestReferencesContainer> httpResponse = HttpResponse.fallback(
608                 new RequestReferencesContainer(
609                         new RequestReferences()));
610
611         MsoResponseWrapperInterface expectedResponse = MsoUtil.wrapResponse(httpResponse);
612
613         when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetailsWrapper), eq(RequestReferencesContainer.class))).thenReturn(httpResponse);
614
615         //  when
616         MsoResponseWrapperInterface response = restClient.changeManagementUpdate(requestDetailsWrapper, endpoint);
617
618         //  then
619         assertThat(expectedResponse).isEqualToComparingFieldByField(response);
620     }
621
622     @Test
623     public void shouldProperlyUpdateVnfAndUpdateInstance() {
624         //  given
625         org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest();
626
627         String endpoint = "testEndpoint";
628         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
629         MsoResponseWrapperInterface expectedResponse = MsoUtil.wrapResponse(httpResponse);
630
631
632         when(client.put(eq(baseUrl + endpoint), anyMap(), any(RequestDetailsWrapper.class), eq(String.class))).thenReturn(httpResponse);
633
634         //  when
635         MsoResponseWrapperInterface response = restClient.updateVnf(requestDetails, endpoint);
636
637         //  then
638         assertThat(expectedResponse).isEqualToComparingFieldByField(response);
639     }
640
641     @Test( expectedExceptions = MsoTestException.class )
642     public void shouldThrowExceptionWhenUpdateVnfAndUpdateInstanceGetsWrongParameter() {
643         //  given
644         org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest();
645         String endpoint = "";
646
647         when(client.put(eq(baseUrl), anyMap(), any(RequestDetailsWrapper.class), eq(String.class))).thenThrow(new MsoTestException("test-post-exception"));
648
649         //  when
650         restClient.updateVnf(requestDetails, endpoint);
651
652         //  then
653     }
654
655     @Test
656     public void shouldProperlySetServiceInstanceStatus() {
657         //  given
658         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
659         RestObject<String> restObject = generateMockMsoRestObject();
660
661         String endpoint = "testEndpoint";
662         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
663
664         when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse);
665
666         //  when
667         restClient.setServiceInstanceStatus(requestDetails,"", "", endpoint, restObject);
668     }
669
670     @Test( expectedExceptions = MsoTestException.class)
671     public void shouldThrowExceptionWhenSetServiceInstanceStatusGetsWrongParameter() {
672         //  given
673         String endpoint = "";
674
675         when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception"));
676
677         //  when
678         restClient.setServiceInstanceStatus(null,"", "", endpoint, null);
679     }
680
681     @Test
682     public void shouldProperlyRemoveRelationshipFromServiceInstance() {
683         //  given
684         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
685
686         String endpoint = "testEndpoint";
687         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
688         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
689
690         when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse);
691
692         //  when
693         MsoResponseWrapper response = restClient.removeRelationshipFromServiceInstance(requestDetails, endpoint);
694
695         //  then
696         assertThat(expectedResponse).isEqualToComparingFieldByField(response);
697     }
698
699     @Test( expectedExceptions = MsoTestException.class)
700     public void shouldThrowExceptionWhenRemoveRelationshipFromServiceInstanceGetsWrongParameter() {
701         //  given
702         String endpoint = "";
703
704         when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception"));
705
706         //  when
707         restClient.removeRelationshipFromServiceInstance(null,endpoint);
708     }
709
710     @Test
711     public void shouldProperlyAddRelationshipToServiceInstance() {
712         //  given
713         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
714
715         String endpoint = "testEndpoint";
716         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
717         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
718
719         when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse);
720
721         //  when
722         MsoResponseWrapper response = restClient.addRelationshipToServiceInstance(requestDetails, endpoint);
723
724         //  then
725         assertThat(expectedResponse).isEqualToComparingFieldByField(response);
726     }
727
728     @Test( expectedExceptions = MsoTestException.class)
729     public void shouldThrowExceptionWhenAddRelationshipToServiceInstanceGetsWrongParameter() {
730         //  given
731         String endpoint = "";
732
733         when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception"));
734
735         //  when
736         restClient.addRelationshipToServiceInstance(null,endpoint);
737     }
738
739     @Test
740     public void shouldProperlyPerformGetRequest() {
741         //  given
742         String endpoint = "testEndpoint";
743         HttpResponse<String> expectedResponse = HttpResponse.fallback("testOkResponse");
744
745         when(client.get(eq(baseUrl + endpoint), anyMap(), anyMap(), eq(String.class))).thenReturn(expectedResponse);
746
747         //  when
748         HttpResponse<String>  response = restClient.get(endpoint, String.class);
749
750         //  then
751         assertThat(expectedResponse).isEqualToComparingFieldByField(response);
752     }
753
754     @Test
755     public void shouldProperlyPerformPostRequest() {
756         //  given
757
758         RequestDetailsWrapper<RequestDetails> requestDetailsWrapper = new RequestDetailsWrapper<>(MsoRestClientTestUtil.generateMockMsoRequest());
759
760         String endpoint = "testEndpoint";
761         HttpResponse<String> expectedResponse = HttpResponse.fallback("testOkResponse");
762
763         when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetailsWrapper), eq(String.class))).thenReturn(expectedResponse);
764
765         //  when
766         HttpResponse<String>  response = restClient.post(endpoint,requestDetailsWrapper, String.class);
767
768         //  then
769         assertThat(expectedResponse).isEqualToComparingFieldByField(response);
770     }
771
772     @Test
773     public void shouldProperlyInvokeWorkflows() {
774         //  given
775         String endpoint = "testPath";
776         HttpResponse expectedResponse = createOkResponse();
777
778         WorkflowRequestDetail workflowRequestDetail = MsoRestClientTestUtil.createWorkflowRequestDetail();
779
780         RequestDetailsWrapper<WorkflowRequestDetail> requestDetailsWrapper = new RequestDetailsWrapper<>(workflowRequestDetail);
781
782         UUID requestId = UUID.randomUUID();
783
784         when(client.post(eq(baseUrl + endpoint), argThat(allOf(hasEntry("X-ONAP-RequestID", requestId.toString()),hasEntry("Content-Type", "application/json"))), refEq(requestDetailsWrapper))).
785                 thenReturn(expectedResponse);
786
787         Map<String,String> extraHeaders = new HashMap<>();
788         extraHeaders.put("X-ONAP-RequestID",requestId.toString());
789         extraHeaders.put("X-ONAP-PartnerName","VID");
790         extraHeaders.put("X-RequestorID","testRequester");
791
792         //  when
793         MsoResponseWrapper response = restClient.invokeWorkflow(workflowRequestDetail, endpoint, extraHeaders);
794
795         //  then
796         assertThat(response).isEqualToComparingFieldByField(MsoUtil.wrapResponse(expectedResponse));
797
798     }
799
800     private class MsoTestException extends RuntimeException{
801         MsoTestException(String testException) {
802             super(testException);
803         }
804     }
805
806     private HttpResponse<String> createOkResponse() {
807         StatusLine statusline = new BasicStatusLine(
808                 new ProtocolVersion("http",1,1), 202, "acceptResponse");
809
810         org.apache.http.HttpResponse responseBase = new BasicHttpResponse(statusline);
811
812         return new HttpResponse<>(responseBase ,String.class, new JsonMapper());
813     }
814
815     private RestObject<String> generateMockMsoRestObject() {
816         RestObject<String> restObject = new RestObject<>();
817
818         restObject.set("test-rest-object-body");
819         restObject.setRaw("test-rest-object-raw-string");
820         restObject.setStatusCode(202);
821         return restObject;
822     }
823 }