probe mso by retrieving empty list of request status
[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 com.fasterxml.jackson.core.JsonProcessingException;
33 import io.joshworks.restclient.http.HttpResponse;
34 import io.joshworks.restclient.http.JsonMapper;
35 import org.apache.http.ProtocolVersion;
36 import org.apache.http.StatusLine;
37 import org.apache.http.message.BasicHttpResponse;
38 import org.apache.http.message.BasicStatusLine;
39 import org.mockito.Mock;
40 import org.onap.portalsdk.core.util.SystemProperties;
41 import org.onap.vid.aai.HttpResponseWithRequestInfo;
42 import org.onap.vid.changeManagement.RequestDetailsWrapper;
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.RequestReferences;
53 import org.onap.vid.utils.SystemPropertiesWrapper;
54 import org.springframework.http.HttpMethod;
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.HashMap;
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 import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER;
67
68
69 @ContextConfiguration(classes = {LocalWebConfig.class, SystemProperties.class})
70 @WebAppConfiguration
71 public class MsoRestClientTest {
72
73
74     private final String baseUrl = "http://testURL/";
75
76     @Mock
77     private SyncRestClient client;
78
79     @Mock
80     private SystemPropertiesWrapper systemProperties;
81
82     private MsoRestClientNew restClient;
83
84
85     @BeforeClass
86     private void setUp(){
87         initMocks(this);
88         when(systemProperties.getProperty(MsoProperties.MSO_PASSWORD)).thenReturn("OBF:1ghz1kfx1j1w1m7w1i271e8q1eas1hzj1m4i1iyy1kch1gdz");
89         when(systemProperties.getProperty("app_display_name")).thenReturn("vid");
90         restClient = new MsoRestClientNew(client,baseUrl,null,systemProperties);
91     }
92
93     @Test
94     public void shouldProperlyCreateServiceInstance() {
95         //  given
96         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
97
98         String endpoint = "testEndpoint";
99         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
100         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
101
102         when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
103
104         //  when
105         MsoResponseWrapper response = restClient.createSvcInstance(requestDetails,endpoint);
106
107         //  then
108         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
109     }
110
111     @Test( expectedExceptions = MsoTestException.class)
112     public void shouldThrowExceptionWhenCreateSvcInstanceGetsWrongParameters() {
113         //  given
114         String endpoint = "";
115
116         when( client.post( eq(baseUrl+endpoint),anyMap(),eq(null),eq(String.class) )  ).
117                 thenThrow(new MsoTestException("test-post-exception"));
118
119         //  when
120         restClient.createSvcInstance(null,endpoint);
121     }
122
123     @Test
124     public void shouldProperlyCreateE2eSvcInstance() {
125         //  given
126         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
127
128         String endpoint = "testEndpoint";
129         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
130         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
131
132         when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
133
134         //  when
135         MsoResponseWrapper response = restClient.createE2eSvcInstance(requestDetails,endpoint);
136
137         //  then
138         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
139     }
140
141     @Test
142     public void shouldProperlyCreateVnf() {
143         //  given
144         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
145
146         String endpoint = "testEndpoint";
147         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
148         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
149
150         when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
151
152         //  when
153         MsoResponseWrapper response = restClient.createVnf(requestDetails,endpoint);
154
155         //  then
156         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
157     }
158
159     @Test
160     public void shouldProperlyCreateNwInstance() {
161         //  given
162         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
163
164         String endpoint = "testEndpoint";
165         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
166         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
167
168         when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
169
170         //  when
171         MsoResponseWrapper response = restClient.createNwInstance(requestDetails,endpoint);
172
173         //  then
174         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
175     }
176
177     @Test
178     public void shouldProperlyCreateVolumeGroupInstance() {
179         //  given
180         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
181
182         String endpoint = "testEndpoint";
183         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
184         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
185
186         when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
187
188         //  when
189         MsoResponseWrapper response = restClient.createVolumeGroupInstance(requestDetails,endpoint);
190
191         //  then
192         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
193     }
194
195     @Test
196     public void shouldProperlyCreateVfModuleInstance() {
197         //  given
198         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
199
200         String endpoint = "testEndpoint";
201         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
202         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
203
204         when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
205
206         //  when
207         MsoResponseWrapper response = restClient.createVfModuleInstance(requestDetails,endpoint);
208
209         //  then
210         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
211     }
212
213     @Test
214     public void shouldProperlyScaleOutVFModuleInstance() {
215         //  given
216         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
217
218         RequestDetailsWrapper<RequestDetails> wrappedRequestDetails = new RequestDetailsWrapper<>(requestDetails);
219         String endpoint = "testEndpoint";
220         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
221         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
222
223         when( client.post( eq(baseUrl+endpoint),anyMap(),eq(wrappedRequestDetails),eq(String.class) )  ).thenReturn(httpResponse);
224
225         //  when
226         MsoResponseWrapper response = restClient.scaleOutVFModuleInstance(wrappedRequestDetails,endpoint);
227
228         //  then
229         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
230     }
231
232     @Test
233     public void shouldProperlyCreateConfigurationInstance() {
234         //  given
235         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
236
237         org.onap.vid.mso.rest.RequestDetailsWrapper wrappedRequestDetails = new  org.onap.vid.mso.rest.RequestDetailsWrapper(requestDetails);
238         String endpoint = "testEndpoint";
239         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
240         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
241
242         when( client.post( eq(baseUrl+endpoint),anyMap(),eq(wrappedRequestDetails),eq(String.class) )  ).thenReturn(httpResponse);
243
244         //  when
245         MsoResponseWrapper response = restClient.createConfigurationInstance(wrappedRequestDetails,endpoint);
246
247         //  then
248         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
249     }
250
251     @Test
252     public void shouldProperlyDeleteE2eSvcInstance() {
253         //  given
254         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
255
256         org.onap.vid.mso.rest.RequestDetailsWrapper wrappedRequestDetails = new  org.onap.vid.mso.rest.RequestDetailsWrapper(requestDetails);
257         String endpoint = "testEndpoint";
258         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
259         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
260
261         when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(wrappedRequestDetails),eq(String.class) )  ).thenReturn(httpResponse);
262
263         //  when
264         MsoResponseWrapper response = restClient.deleteE2eSvcInstance(wrappedRequestDetails,endpoint);
265
266         //  then
267         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
268     }
269
270     @Test
271     public void shouldProperlyDeleteSvcInstance() {
272         //  given
273         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
274
275         String endpoint = "testEndpoint";
276         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
277         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
278
279         when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
280
281         //  when
282         MsoResponseWrapper response = restClient.deleteSvcInstance(requestDetails,endpoint);
283
284         //  then
285         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
286     }
287
288     @Test
289     public void shouldProperlyUnassignSvcInstance() {
290         //  given
291         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
292
293         String endpoint = "testEndpoint";
294         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
295         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
296
297         when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
298
299         //  when
300         MsoResponseWrapper response = restClient.unassignSvcInstance(requestDetails,endpoint);
301
302         //  then
303         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
304     }
305
306     @Test
307     public void shouldProperlyDeleteVnf() {
308         //  given
309         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
310
311         String endpoint = "testEndpoint";
312         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
313         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
314
315         when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
316
317         //  when
318         MsoResponseWrapper response = restClient.deleteVnf(requestDetails,endpoint);
319
320         //  then
321         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
322     }
323
324     @Test
325     public void shouldProperlyDeleteVfModule() {
326         //  given
327         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
328
329         String endpoint = "testEndpoint";
330         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
331         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
332
333         when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
334
335         //  when
336         MsoResponseWrapper response = restClient.deleteVfModule(requestDetails,endpoint);
337
338         //  then
339         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
340     }
341
342     @Test
343     public void shouldProperlyDeleteVolumeGroupInstance() {
344         //  given
345         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
346
347         String endpoint = "testEndpoint";
348         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
349         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
350
351         when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
352
353         //  when
354         MsoResponseWrapper response = restClient.deleteVolumeGroupInstance(requestDetails,endpoint);
355
356         //  then
357         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
358     }
359
360     @Test
361     public void shouldProperlyDeleteNwInstance() {
362         //  given
363         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
364
365         String endpoint = "testEndpoint";
366         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
367         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
368
369         when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) )  ).thenReturn(httpResponse);
370
371         //  when
372         MsoResponseWrapper response = restClient.deleteNwInstance(requestDetails,endpoint);
373
374         //  then
375         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
376     }
377
378     @Test
379     public void shouldProperlyGetOrchestrationRequest() {
380         String endpoint = "testEndpoint";
381         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
382         String expectedPath = baseUrl+endpoint;
383         HttpResponseWithRequestInfo<String> expectedResponse = new HttpResponseWithRequestInfo<>(httpResponse, expectedPath, HttpMethod.GET);
384
385         when( client.get( eq(expectedPath), anyMap(), anyMap(), eq(String.class) )).thenReturn(httpResponse);
386
387         //  when
388         HttpResponseWithRequestInfo<String> response = restClient.getOrchestrationRequest(endpoint, true);
389
390         //  then
391         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
392     }
393
394     @Test
395     public void shouldProperlyGetOrchestrationRequestWithOnlyEndpoint() {
396         //  given
397         String endpoint = "testEndpoint";
398         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
399         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
400
401         when( client.get( eq(baseUrl+endpoint),anyMap(),anyMap(),eq(String.class) )  ).thenReturn(httpResponse);
402
403         //  when
404         MsoResponseWrapper response = restClient.getOrchestrationRequest(endpoint);
405
406         //  then
407         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
408     }
409
410     @Test
411     public void shouldProperlyGetManualTasksByRequestId() {
412         //  given
413         RestObject restObject = generateMockMsoRestObject();
414
415         String endpoint = "testEndpoint";
416         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
417         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
418
419         when( client.get( eq(baseUrl+endpoint),anyMap(),anyMap(),eq(String.class) )  ).thenReturn(httpResponse);
420
421         //  when
422         MsoResponseWrapper response = restClient.getManualTasksByRequestId(null,null,endpoint,restObject);
423
424         //  then
425         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
426     }
427
428     @Test(expectedExceptions = MsoTestException.class)
429     public void shouldThrowExceptionWhenGetManualTasksByRequestIdGetsWrongParameter() {
430         //  given
431         when( client.get( eq(baseUrl),anyMap(),anyMap(),eq(String.class) )  ).thenThrow(new MsoTestException("testsException"));
432
433         //  when
434         restClient.getManualTasksByRequestId(null,null,"",null);
435     }
436
437     @Test
438     public void shouldProperlyCompleteManualTask() {
439         //  given
440         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
441         RestObject restObject = generateMockMsoRestObject();
442
443         String endpoint = "testEndpoint";
444         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
445         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
446
447         when( client.post( eq(baseUrl+endpoint), anyMap(), eq(requestDetails), eq(String.class) )  ).thenReturn(httpResponse);
448
449         //  when
450         MsoResponseWrapper response = restClient.completeManualTask(requestDetails,null,null,endpoint,restObject);
451
452         //  then
453         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
454     }
455
456     @Test(expectedExceptions = MsoTestException.class)
457     public void shouldThrowExceptionWhenCompleteManualTaskWrongParameter() {
458         //  given
459         when( client.post( eq(baseUrl),anyMap(),eq(null), eq(String.class) )  ).thenThrow(new MsoTestException("testsException"));
460
461         //  when
462         restClient.completeManualTask(null, null,null,"",null);
463     }
464
465     @Test
466     public void shouldProperlyReplaceVnf() {
467         //  given
468         org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest();
469
470         String endpoint = "testEndpoint";
471         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
472         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
473
474         when( client.post( eq(baseUrl+endpoint), anyMap(), any(RequestDetailsWrapper.class), eq(String.class) )  ).thenReturn(httpResponse);
475
476         //  when
477         MsoResponseWrapper response = restClient.replaceVnf(requestDetails,endpoint);
478
479         //  then
480         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
481     }
482
483     @Test
484     public void shouldProperlyReplaceVnfWithStatus202() {
485         //  given
486         org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest();
487
488         String endpoint = "testEndpoint";
489         HttpResponse<String> httpResponse = createOkResponse();
490         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
491
492         when( client.post( eq(baseUrl+endpoint), anyMap(), any(RequestDetailsWrapper.class), eq(String.class) )  ).thenReturn(httpResponse);
493
494         //  when
495         MsoResponseWrapper response = restClient.replaceVnf(requestDetails,endpoint);
496
497         //  then
498         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
499     }
500
501     @Test( expectedExceptions = MsoTestException.class)
502     public void shouldThrowExceptionWhenReplaceVnfGetsWrongParameters() {
503         //  given
504         org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest();
505
506         when( client.post( eq(baseUrl), anyMap(), any(RequestDetailsWrapper.class), eq(String.class) )  ).thenThrow(new MsoTestException("test-post-exception"));
507
508         //  when
509         restClient.replaceVnf(requestDetails, "");
510     }
511
512     @Test
513     public void shouldProperlyDeleteConfiguration() {
514         //  given
515         org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest();
516         org.onap.vid.mso.rest.RequestDetailsWrapper requestDetailsWrapper = new org.onap.vid.mso.rest.RequestDetailsWrapper(requestDetails);
517
518         String endpoint = "testEndpoint";
519         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
520         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
521
522         when( client.delete( eq(baseUrl+endpoint), anyMap(), eq(requestDetailsWrapper), eq(String.class) )  ).thenReturn(httpResponse);
523
524         //  when
525         MsoResponseWrapper response = restClient.deleteConfiguration(requestDetailsWrapper,endpoint);
526
527         //  then
528         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
529     }
530
531     @Test( expectedExceptions = MsoTestException.class )
532     public void shouldThrowExceptionWhenProperlyDeleteConfigurationGetsWrongParameters() {
533         //  given
534         when( client.delete( eq(baseUrl), anyMap(), eq(null), eq(String.class) )  ).thenThrow(new MsoTestException("test-delete-exception"));
535
536         //  when
537         restClient.deleteConfiguration(null,"");
538     }
539
540     @Test
541     public void shouldProperlySetConfigurationActiveStatus() {
542         //  given
543         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
544
545         String endpoint = "testEndpoint";
546         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
547         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
548
549         when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse);
550
551         //  when
552         MsoResponseWrapper response = restClient.setConfigurationActiveStatus(requestDetails, endpoint);
553
554         //  then
555         assertThat(expectedResponse).isEqualToComparingFieldByField(response);
556     }
557
558     @Test(expectedExceptions = MsoTestException.class)
559     public void shouldThrowExceptionWhenSetConfigurationActiveStatusGetsWrongParameters() {
560         //  given
561
562         when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception"));
563
564         //  when
565         restClient.setConfigurationActiveStatus(null, "");
566     }
567
568     @Test
569     public void shouldProperlySetPortOnConfigurationStatus() {
570         //  given
571         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
572
573         String endpoint = "testEndpoint";
574         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
575         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
576
577         when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse);
578
579         //  when
580         MsoResponseWrapper response = restClient.setPortOnConfigurationStatus(requestDetails, endpoint);
581
582         //  then
583         assertThat(expectedResponse).isEqualToComparingFieldByField(response);
584     }
585
586     @Test(expectedExceptions = MsoTestException.class)
587     public void shouldThrowExceptionWhenSetPortOnConfigurationStatusGetsWrongParameters() {
588         //  given
589         String endpoint = "";
590
591         when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception"));
592
593         //  when
594         restClient.setPortOnConfigurationStatus(null, endpoint);
595     }
596
597     @Test
598     public void shouldProperlyChangeManagementUpdate() throws JsonProcessingException {
599         //  given
600         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
601         RequestDetailsWrapper<RequestDetails> requestDetailsWrapper = new RequestDetailsWrapper<>(requestDetails);
602
603         String endpoint = "testEndpoint";
604         RequestReferencesContainer entity = new RequestReferencesContainer(new RequestReferences());
605         HttpResponse<String> httpResponse = HttpResponse.fallback(JACKSON_OBJECT_MAPPER.writeValueAsString(entity));
606
607         when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetailsWrapper), eq(String.class))).thenReturn(httpResponse);
608
609         //  when
610         MsoResponseWrapperInterface response = restClient.changeManagementUpdate(requestDetailsWrapper, endpoint);
611
612         //  then
613         assertThat(response.getEntity()).isEqualToComparingFieldByField(entity);
614         assertThat(response.getStatus()).isEqualTo(0);
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);
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);
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 }