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