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