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