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