2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.mso.rest;
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;
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;
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;
78 @ContextConfiguration(classes = {LocalWebConfig.class, SystemProperties.class})
80 public class MsoRestClientTest {
83 private final String baseUrl = "http://testURL/";
86 private SyncRestClient client;
89 private SystemPropertiesWrapper systemProperties;
92 private Logging loggingService;
94 private MsoRestClientNew restClient;
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,null,systemProperties,loggingService);
106 public void shouldProperlyCreateServiceInstance() {
108 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
110 String endpoint = "testEndpoint";
111 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
112 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
114 when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
117 MsoResponseWrapper response = restClient.createSvcInstance(requestDetails,endpoint);
120 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
123 @Test( expectedExceptions = MsoTestException.class)
124 public void shouldThrowExceptionWhenCreateSvcInstanceGetsWrongParameters() {
126 String endpoint = "";
128 when( client.post( eq(baseUrl+endpoint),anyMap(),eq(null),eq(String.class) ) ).
129 thenThrow(new MsoTestException("test-post-exception"));
132 restClient.createSvcInstance(null,endpoint);
136 public void shouldProperlyCreateE2eSvcInstance() {
138 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
140 String endpoint = "testEndpoint";
141 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
142 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
144 when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
147 MsoResponseWrapper response = restClient.createE2eSvcInstance(requestDetails,endpoint);
150 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
154 public void whenCreateInstanceTwice_thenRequestIdHeaderIsDifferentEachTime() {
156 RequestAttributes prevRequestAttributes = RequestContextHolder.getRequestAttributes();
160 Mockito.reset(client);
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);
168 //create different ECOMP_REQUEST_ID header in Spring HttpServlet each time
169 OutgoingRequestHeadersTest.putRequestInSpringContext();
170 restClient.createInstance(requestDetails, "someEndPoint");
172 OutgoingRequestHeadersTest.putRequestInSpringContext();
173 restClient.createInstance(requestDetails, "someEndPoint");
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");
187 //make sure other test keep go smooth
188 RequestContextHolder.setRequestAttributes(prevRequestAttributes);
193 public void shouldProperlyCreateVnf() {
195 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
197 String endpoint = "testEndpoint";
198 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
199 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
201 when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
204 MsoResponseWrapper response = restClient.createVnf(requestDetails,endpoint);
207 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
211 public void shouldProperlyCreateNwInstance() {
213 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
215 String endpoint = "testEndpoint";
216 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
217 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
219 when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
222 MsoResponseWrapper response = restClient.createNwInstance(requestDetails,endpoint);
225 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
229 public void shouldProperlyCreateVolumeGroupInstance() {
231 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
233 String endpoint = "testEndpoint";
234 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
235 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
237 when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
240 MsoResponseWrapper response = restClient.createVolumeGroupInstance(requestDetails,endpoint);
243 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
247 public void shouldProperlyCreateVfModuleInstance() {
249 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
251 String endpoint = "testEndpoint";
252 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
253 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
255 when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
258 MsoResponseWrapper response = restClient.createVfModuleInstance(requestDetails,endpoint);
261 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
265 public void shouldProperlyScaleOutVFModuleInstance() {
267 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
269 RequestDetailsWrapper<RequestDetails> wrappedRequestDetails = new RequestDetailsWrapper<>(requestDetails);
270 String endpoint = "testEndpoint";
271 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
272 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
274 when( client.post( eq(baseUrl+endpoint),anyMap(),eq(wrappedRequestDetails),eq(String.class) ) ).thenReturn(httpResponse);
277 MsoResponseWrapper response = restClient.scaleOutVFModuleInstance(wrappedRequestDetails,endpoint);
280 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
284 public void shouldProperlyCreateConfigurationInstance() {
286 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
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);
293 when( client.post( eq(baseUrl+endpoint),anyMap(),eq(wrappedRequestDetails),eq(String.class) ) ).thenReturn(httpResponse);
296 MsoResponseWrapper response = restClient.createConfigurationInstance(wrappedRequestDetails,endpoint);
299 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
303 public void shouldProperlyDeleteE2eSvcInstance() {
305 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
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);
312 when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(wrappedRequestDetails),eq(String.class) ) ).thenReturn(httpResponse);
315 MsoResponseWrapper response = restClient.deleteE2eSvcInstance(wrappedRequestDetails,endpoint);
318 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
322 public void shouldProperlyDeleteSvcInstance() {
324 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
326 String endpoint = "testEndpoint";
327 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
328 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
330 when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
333 MsoResponseWrapper response = restClient.deleteSvcInstance(requestDetails,endpoint);
336 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
340 public void shouldProperlyUnassignSvcInstance() {
342 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
344 String endpoint = "testEndpoint";
345 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
346 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
348 when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
351 MsoResponseWrapper response = restClient.unassignSvcInstance(requestDetails,endpoint);
354 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
358 public void shouldProperlyDeleteVnf() {
360 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
362 String endpoint = "testEndpoint";
363 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
364 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
366 when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
369 MsoResponseWrapper response = restClient.deleteVnf(requestDetails,endpoint);
372 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
376 public void shouldProperlyDeleteVfModule() {
378 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
380 String endpoint = "testEndpoint";
381 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
382 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
384 when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
387 MsoResponseWrapper response = restClient.deleteVfModule(requestDetails,endpoint);
390 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
394 public void shouldProperlyDeleteVolumeGroupInstance() {
396 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
398 String endpoint = "testEndpoint";
399 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
400 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
402 when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
405 MsoResponseWrapper response = restClient.deleteVolumeGroupInstance(requestDetails,endpoint);
408 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
412 public void shouldProperlyDeleteNwInstance() {
414 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
416 String endpoint = "testEndpoint";
417 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
418 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
420 when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
423 MsoResponseWrapper response = restClient.deleteNwInstance(requestDetails,endpoint);
426 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
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);
436 when( client.get( eq(expectedPath), anyMap(), anyMap(), eq(String.class) )).thenReturn(httpResponse);
439 HttpResponseWithRequestInfo<String> response = restClient.getOrchestrationRequest(endpoint, true);
442 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
446 public void shouldProperlyGetOrchestrationRequestWithOnlyEndpoint() {
448 String endpoint = "testEndpoint";
449 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
450 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
452 when( client.get( eq(baseUrl+endpoint),anyMap(),anyMap(),eq(String.class) ) ).thenReturn(httpResponse);
455 MsoResponseWrapper response = restClient.getOrchestrationRequest(endpoint);
458 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
462 public void shouldProperlyGetManualTasksByRequestId() {
464 RestObject restObject = generateMockMsoRestObject();
466 String endpoint = "testEndpoint";
467 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
468 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
470 when( client.get( eq(baseUrl+endpoint),anyMap(),anyMap(),eq(String.class) ) ).thenReturn(httpResponse);
473 MsoResponseWrapper response = restClient.getManualTasksByRequestId(null,null,endpoint,restObject);
476 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
479 @Test(expectedExceptions = MsoTestException.class)
480 public void shouldThrowExceptionWhenGetManualTasksByRequestIdGetsWrongParameter() {
482 when( client.get( eq(baseUrl),anyMap(),anyMap(),eq(String.class) ) ).thenThrow(new MsoTestException("testsException"));
485 restClient.getManualTasksByRequestId(null,null,"",null);
489 public void shouldProperlyCompleteManualTask() {
491 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
492 RestObject restObject = generateMockMsoRestObject();
494 String endpoint = "testEndpoint";
495 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
496 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
498 when( client.post( eq(baseUrl+endpoint), anyMap(), eq(requestDetails), eq(String.class) ) ).thenReturn(httpResponse);
501 MsoResponseWrapper response = restClient.completeManualTask(requestDetails,null,null,endpoint,restObject);
504 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
507 @Test(expectedExceptions = MsoTestException.class)
508 public void shouldThrowExceptionWhenCompleteManualTaskWrongParameter() {
510 when( client.post( eq(baseUrl),anyMap(),eq(null), eq(String.class) ) ).thenThrow(new MsoTestException("testsException"));
513 restClient.completeManualTask(null, null,null,"",null);
517 public void shouldProperlyReplaceVnf() {
519 org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest();
521 String endpoint = "testEndpoint";
522 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
523 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
525 when( client.post( eq(baseUrl+endpoint), anyMap(), any(RequestDetailsWrapper.class), eq(String.class) ) ).thenReturn(httpResponse);
528 MsoResponseWrapper response = restClient.replaceVnf(requestDetails,endpoint);
531 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
535 public void shouldProperlyReplaceVnfWithStatus202() {
537 org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest();
539 String endpoint = "testEndpoint";
540 HttpResponse<String> httpResponse = createOkResponse();
541 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
543 when( client.post( eq(baseUrl+endpoint), anyMap(), any(RequestDetailsWrapper.class), eq(String.class) ) ).thenReturn(httpResponse);
546 MsoResponseWrapper response = restClient.replaceVnf(requestDetails,endpoint);
549 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
552 @Test( expectedExceptions = MsoTestException.class)
553 public void shouldThrowExceptionWhenReplaceVnfGetsWrongParameters() {
555 org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest();
557 when( client.post( eq(baseUrl), anyMap(), any(RequestDetailsWrapper.class), eq(String.class) ) ).thenThrow(new MsoTestException("test-post-exception"));
560 restClient.replaceVnf(requestDetails, "");
564 public void shouldProperlyDeleteConfiguration() {
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);
569 String endpoint = "testEndpoint";
570 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
571 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
573 when( client.delete( eq(baseUrl+endpoint), anyMap(), eq(requestDetailsWrapper), eq(String.class) ) ).thenReturn(httpResponse);
576 MsoResponseWrapper response = restClient.deleteConfiguration(requestDetailsWrapper,endpoint);
579 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
582 @Test( expectedExceptions = MsoTestException.class )
583 public void shouldThrowExceptionWhenProperlyDeleteConfigurationGetsWrongParameters() {
585 when( client.delete( eq(baseUrl), anyMap(), eq(null), eq(String.class) ) ).thenThrow(new MsoTestException("test-delete-exception"));
588 restClient.deleteConfiguration(null,"");
592 public void shouldProperlySetConfigurationActiveStatus() {
594 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
596 String endpoint = "testEndpoint";
597 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
598 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
600 when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse);
603 MsoResponseWrapper response = restClient.setConfigurationActiveStatus(requestDetails, endpoint);
606 assertThat(expectedResponse).isEqualToComparingFieldByField(response);
609 @Test(expectedExceptions = MsoTestException.class)
610 public void shouldThrowExceptionWhenSetConfigurationActiveStatusGetsWrongParameters() {
613 when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception"));
616 restClient.setConfigurationActiveStatus(null, "");
620 public void shouldProperlySetPortOnConfigurationStatus() {
622 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
624 String endpoint = "testEndpoint";
625 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
626 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
628 when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse);
631 MsoResponseWrapper response = restClient.setPortOnConfigurationStatus(requestDetails, endpoint);
634 assertThat(expectedResponse).isEqualToComparingFieldByField(response);
637 @Test(expectedExceptions = MsoTestException.class)
638 public void shouldThrowExceptionWhenSetPortOnConfigurationStatusGetsWrongParameters() {
640 String endpoint = "";
642 when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception"));
645 restClient.setPortOnConfigurationStatus(null, endpoint);
649 public void shouldProperlyChangeManagementUpdate() throws JsonProcessingException {
651 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
652 RequestDetailsWrapper<RequestDetails> requestDetailsWrapper = new RequestDetailsWrapper<>(requestDetails);
654 String endpoint = "testEndpoint";
655 RequestReferencesContainer entity = new RequestReferencesContainer(new RequestReferences());
656 HttpResponse<String> httpResponse = HttpResponse.fallback(JACKSON_OBJECT_MAPPER.writeValueAsString(entity));
658 when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetailsWrapper), eq(String.class))).thenReturn(httpResponse);
661 MsoResponseWrapperInterface response = restClient.changeManagementUpdate(requestDetailsWrapper, endpoint);
664 assertThat(response.getEntity()).isEqualToComparingFieldByField(entity);
665 assertThat(response.getStatus()).isEqualTo(0);
669 public void shouldProperlyUpdateVnfAndUpdateInstance() {
671 org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest();
673 String endpoint = "testEndpoint";
674 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
675 MsoResponseWrapperInterface expectedResponse = MsoUtil.wrapResponse(httpResponse);
678 when(client.put(eq(baseUrl + endpoint), anyMap(), any(RequestDetailsWrapper.class), eq(String.class))).thenReturn(httpResponse);
681 MsoResponseWrapperInterface response = restClient.updateVnf(requestDetails, endpoint);
684 assertThat(expectedResponse).isEqualToComparingFieldByField(response);
687 @Test( expectedExceptions = MsoTestException.class )
688 public void shouldThrowExceptionWhenUpdateVnfAndUpdateInstanceGetsWrongParameter() {
690 org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest();
691 String endpoint = "";
693 when(client.put(eq(baseUrl), anyMap(), any(RequestDetailsWrapper.class), eq(String.class))).thenThrow(new MsoTestException("test-post-exception"));
696 restClient.updateVnf(requestDetails, endpoint);
702 public void shouldProperlySetServiceInstanceStatus() {
704 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
705 RestObject<String> restObject = generateMockMsoRestObject();
707 String endpoint = "testEndpoint";
708 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
710 when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse);
713 restClient.setServiceInstanceStatus(requestDetails, endpoint);
716 @Test( expectedExceptions = MsoTestException.class)
717 public void shouldThrowExceptionWhenSetServiceInstanceStatusGetsWrongParameter() {
719 String endpoint = "";
721 when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception"));
724 restClient.setServiceInstanceStatus(null, endpoint);
728 public void shouldProperlyRemoveRelationshipFromServiceInstance() {
730 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
732 String endpoint = "testEndpoint";
733 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
734 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
736 when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse);
739 MsoResponseWrapper response = restClient.removeRelationshipFromServiceInstance(requestDetails, endpoint);
742 assertThat(expectedResponse).isEqualToComparingFieldByField(response);
745 @Test( expectedExceptions = MsoTestException.class)
746 public void shouldThrowExceptionWhenRemoveRelationshipFromServiceInstanceGetsWrongParameter() {
748 String endpoint = "";
750 when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception"));
753 restClient.removeRelationshipFromServiceInstance(null,endpoint);
757 public void shouldProperlyAddRelationshipToServiceInstance() {
759 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
761 String endpoint = "testEndpoint";
762 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
763 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
765 when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse);
768 MsoResponseWrapper response = restClient.addRelationshipToServiceInstance(requestDetails, endpoint);
771 assertThat(expectedResponse).isEqualToComparingFieldByField(response);
774 @Test( expectedExceptions = MsoTestException.class)
775 public void shouldThrowExceptionWhenAddRelationshipToServiceInstanceGetsWrongParameter() {
777 String endpoint = "";
779 when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception"));
782 restClient.addRelationshipToServiceInstance(null,endpoint);
786 public void shouldProperlyPerformGetRequest() {
788 String endpoint = "testEndpoint";
789 HttpResponse<String> expectedResponse = HttpResponse.fallback("testOkResponse");
791 when(client.get(eq(baseUrl + endpoint), anyMap(), anyMap(), eq(String.class))).thenReturn(expectedResponse);
794 HttpResponse<String> response = restClient.get(endpoint, String.class);
797 assertThat(expectedResponse).isEqualToComparingFieldByField(response);
801 public void shouldProperlyPerformPostRequest() {
804 RequestDetailsWrapper<RequestDetails> requestDetailsWrapper = new RequestDetailsWrapper<>(MsoRestClientTestUtil.generateMockMsoRequest());
806 String endpoint = "testEndpoint";
807 HttpResponse<String> expectedResponse = HttpResponse.fallback("testOkResponse");
809 when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetailsWrapper), eq(String.class))).thenReturn(expectedResponse);
812 HttpResponse<String> response = restClient.post(endpoint,requestDetailsWrapper, String.class);
815 assertThat(expectedResponse).isEqualToComparingFieldByField(response);
819 public void shouldProperlyInvokeWorkflows() {
821 String endpoint = "testPath";
822 HttpResponse expectedResponse = createOkResponse();
824 WorkflowRequestDetail workflowRequestDetail = MsoRestClientTestUtil.createWorkflowRequestDetail();
826 RequestDetailsWrapper<WorkflowRequestDetail> requestDetailsWrapper = new RequestDetailsWrapper<>(workflowRequestDetail);
828 UUID requestId = UUID.randomUUID();
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);
833 Map<String,String> extraHeaders = new HashMap<>();
834 extraHeaders.put("X-ONAP-RequestID",requestId.toString());
835 extraHeaders.put("X-ONAP-PartnerName","VID");
836 extraHeaders.put("X-RequestorID","testRequester");
839 MsoResponseWrapper response = restClient.invokeWorkflow(workflowRequestDetail, endpoint, extraHeaders);
842 assertThat(response).isEqualToComparingFieldByField(MsoUtil.wrapResponse(expectedResponse));
846 private class MsoTestException extends RuntimeException{
847 MsoTestException(String testException) {
848 super(testException);
852 private HttpResponse<String> createOkResponse() {
853 StatusLine statusline = new BasicStatusLine(
854 new ProtocolVersion("http",1,1), 202, "acceptResponse");
856 org.apache.http.HttpResponse responseBase = new BasicHttpResponse(statusline);
858 return new HttpResponse<>(responseBase ,String.class, new JsonMapper());
861 private RestObject<String> generateMockMsoRestObject() {
862 RestObject<String> restObject = new RestObject<>();
864 restObject.set("test-rest-object-body");
865 restObject.setRaw("test-rest-object-raw-string");
866 restObject.setStatusCode(202);