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.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;
77 @ContextConfiguration(classes = {LocalWebConfig.class, SystemProperties.class})
79 public class MsoRestClientTest {
82 private final String baseUrl = "http://testURL/";
85 private SyncRestClient client;
88 private SystemPropertiesWrapper systemProperties;
90 private MsoRestClientNew restClient;
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);
102 public void shouldProperlyCreateServiceInstance() {
104 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
106 String endpoint = "testEndpoint";
107 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
108 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
110 when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
113 MsoResponseWrapper response = restClient.createSvcInstance(requestDetails,endpoint);
116 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
119 @Test( expectedExceptions = MsoTestException.class)
120 public void shouldThrowExceptionWhenCreateSvcInstanceGetsWrongParameters() {
122 String endpoint = "";
124 when( client.post( eq(baseUrl+endpoint),anyMap(),eq(null),eq(String.class) ) ).
125 thenThrow(new MsoTestException("test-post-exception"));
128 restClient.createSvcInstance(null,endpoint);
132 public void shouldProperlyCreateE2eSvcInstance() {
134 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
136 String endpoint = "testEndpoint";
137 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
138 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
140 when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
143 MsoResponseWrapper response = restClient.createE2eSvcInstance(requestDetails,endpoint);
146 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
150 public void whenCreateInstanceTwice_thenRequestIdHeaderIsDifferentEachTime() {
152 RequestAttributes prevRequestAttributes = RequestContextHolder.getRequestAttributes();
156 Mockito.reset(client);
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);
164 //create different ECOMP_REQUEST_ID header in Spring HttpServlet each time
165 OutgoingRequestHeadersTest.putRequestInSpringContext();
166 restClient.createInstance(requestDetails, "someEndPoint");
168 OutgoingRequestHeadersTest.putRequestInSpringContext();
169 restClient.createInstance(requestDetails, "someEndPoint");
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");
183 //make sure other test keep go smooth
184 RequestContextHolder.setRequestAttributes(prevRequestAttributes);
189 public void shouldProperlyCreateVnf() {
191 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
193 String endpoint = "testEndpoint";
194 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
195 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
197 when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
200 MsoResponseWrapper response = restClient.createVnf(requestDetails,endpoint);
203 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
207 public void shouldProperlyCreateNwInstance() {
209 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
211 String endpoint = "testEndpoint";
212 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
213 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
215 when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
218 MsoResponseWrapper response = restClient.createNwInstance(requestDetails,endpoint);
221 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
225 public void shouldProperlyCreateVolumeGroupInstance() {
227 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
229 String endpoint = "testEndpoint";
230 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
231 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
233 when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
236 MsoResponseWrapper response = restClient.createVolumeGroupInstance(requestDetails,endpoint);
239 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
243 public void shouldProperlyCreateVfModuleInstance() {
245 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
247 String endpoint = "testEndpoint";
248 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
249 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
251 when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
254 MsoResponseWrapper response = restClient.createVfModuleInstance(requestDetails,endpoint);
257 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
261 public void shouldProperlyScaleOutVFModuleInstance() {
263 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
265 RequestDetailsWrapper<RequestDetails> wrappedRequestDetails = new RequestDetailsWrapper<>(requestDetails);
266 String endpoint = "testEndpoint";
267 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
268 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
270 when( client.post( eq(baseUrl+endpoint),anyMap(),eq(wrappedRequestDetails),eq(String.class) ) ).thenReturn(httpResponse);
273 MsoResponseWrapper response = restClient.scaleOutVFModuleInstance(wrappedRequestDetails,endpoint);
276 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
280 public void shouldProperlyCreateConfigurationInstance() {
282 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
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);
289 when( client.post( eq(baseUrl+endpoint),anyMap(),eq(wrappedRequestDetails),eq(String.class) ) ).thenReturn(httpResponse);
292 MsoResponseWrapper response = restClient.createConfigurationInstance(wrappedRequestDetails,endpoint);
295 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
299 public void shouldProperlyDeleteE2eSvcInstance() {
301 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
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);
308 when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(wrappedRequestDetails),eq(String.class) ) ).thenReturn(httpResponse);
311 MsoResponseWrapper response = restClient.deleteE2eSvcInstance(wrappedRequestDetails,endpoint);
314 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
318 public void shouldProperlyDeleteSvcInstance() {
320 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
322 String endpoint = "testEndpoint";
323 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
324 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
326 when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
329 MsoResponseWrapper response = restClient.deleteSvcInstance(requestDetails,endpoint);
332 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
336 public void shouldProperlyUnassignSvcInstance() {
338 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
340 String endpoint = "testEndpoint";
341 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
342 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
344 when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
347 MsoResponseWrapper response = restClient.unassignSvcInstance(requestDetails,endpoint);
350 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
354 public void shouldProperlyDeleteVnf() {
356 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
358 String endpoint = "testEndpoint";
359 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
360 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
362 when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
365 MsoResponseWrapper response = restClient.deleteVnf(requestDetails,endpoint);
368 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
372 public void shouldProperlyDeleteVfModule() {
374 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
376 String endpoint = "testEndpoint";
377 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
378 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
380 when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
383 MsoResponseWrapper response = restClient.deleteVfModule(requestDetails,endpoint);
386 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
390 public void shouldProperlyDeleteVolumeGroupInstance() {
392 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
394 String endpoint = "testEndpoint";
395 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
396 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
398 when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
401 MsoResponseWrapper response = restClient.deleteVolumeGroupInstance(requestDetails,endpoint);
404 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
408 public void shouldProperlyDeleteNwInstance() {
410 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
412 String endpoint = "testEndpoint";
413 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
414 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
416 when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse);
419 MsoResponseWrapper response = restClient.deleteNwInstance(requestDetails,endpoint);
422 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
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);
432 when( client.get( eq(expectedPath), anyMap(), anyMap(), eq(String.class) )).thenReturn(httpResponse);
435 HttpResponseWithRequestInfo<String> response = restClient.getOrchestrationRequest(endpoint, true);
438 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
442 public void shouldProperlyGetOrchestrationRequestWithOnlyEndpoint() {
444 String endpoint = "testEndpoint";
445 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
446 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
448 when( client.get( eq(baseUrl+endpoint),anyMap(),anyMap(),eq(String.class) ) ).thenReturn(httpResponse);
451 MsoResponseWrapper response = restClient.getOrchestrationRequest(endpoint);
454 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
458 public void shouldProperlyGetManualTasksByRequestId() {
460 RestObject restObject = generateMockMsoRestObject();
462 String endpoint = "testEndpoint";
463 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
464 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
466 when( client.get( eq(baseUrl+endpoint),anyMap(),anyMap(),eq(String.class) ) ).thenReturn(httpResponse);
469 MsoResponseWrapper response = restClient.getManualTasksByRequestId(null,null,endpoint,restObject);
472 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
475 @Test(expectedExceptions = MsoTestException.class)
476 public void shouldThrowExceptionWhenGetManualTasksByRequestIdGetsWrongParameter() {
478 when( client.get( eq(baseUrl),anyMap(),anyMap(),eq(String.class) ) ).thenThrow(new MsoTestException("testsException"));
481 restClient.getManualTasksByRequestId(null,null,"",null);
485 public void shouldProperlyCompleteManualTask() {
487 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
488 RestObject restObject = generateMockMsoRestObject();
490 String endpoint = "testEndpoint";
491 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
492 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
494 when( client.post( eq(baseUrl+endpoint), anyMap(), eq(requestDetails), eq(String.class) ) ).thenReturn(httpResponse);
497 MsoResponseWrapper response = restClient.completeManualTask(requestDetails,null,null,endpoint,restObject);
500 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
503 @Test(expectedExceptions = MsoTestException.class)
504 public void shouldThrowExceptionWhenCompleteManualTaskWrongParameter() {
506 when( client.post( eq(baseUrl),anyMap(),eq(null), eq(String.class) ) ).thenThrow(new MsoTestException("testsException"));
509 restClient.completeManualTask(null, null,null,"",null);
513 public void shouldProperlyReplaceVnf() {
515 org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest();
517 String endpoint = "testEndpoint";
518 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
519 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
521 when( client.post( eq(baseUrl+endpoint), anyMap(), any(RequestDetailsWrapper.class), eq(String.class) ) ).thenReturn(httpResponse);
524 MsoResponseWrapper response = restClient.replaceVnf(requestDetails,endpoint);
527 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
531 public void shouldProperlyReplaceVnfWithStatus202() {
533 org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest();
535 String endpoint = "testEndpoint";
536 HttpResponse<String> httpResponse = createOkResponse();
537 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
539 when( client.post( eq(baseUrl+endpoint), anyMap(), any(RequestDetailsWrapper.class), eq(String.class) ) ).thenReturn(httpResponse);
542 MsoResponseWrapper response = restClient.replaceVnf(requestDetails,endpoint);
545 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
548 @Test( expectedExceptions = MsoTestException.class)
549 public void shouldThrowExceptionWhenReplaceVnfGetsWrongParameters() {
551 org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest();
553 when( client.post( eq(baseUrl), anyMap(), any(RequestDetailsWrapper.class), eq(String.class) ) ).thenThrow(new MsoTestException("test-post-exception"));
556 restClient.replaceVnf(requestDetails, "");
560 public void shouldProperlyDeleteConfiguration() {
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);
565 String endpoint = "testEndpoint";
566 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
567 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
569 when( client.delete( eq(baseUrl+endpoint), anyMap(), eq(requestDetailsWrapper), eq(String.class) ) ).thenReturn(httpResponse);
572 MsoResponseWrapper response = restClient.deleteConfiguration(requestDetailsWrapper,endpoint);
575 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
578 @Test( expectedExceptions = MsoTestException.class )
579 public void shouldThrowExceptionWhenProperlyDeleteConfigurationGetsWrongParameters() {
581 when( client.delete( eq(baseUrl), anyMap(), eq(null), eq(String.class) ) ).thenThrow(new MsoTestException("test-delete-exception"));
584 restClient.deleteConfiguration(null,"");
588 public void shouldProperlySetConfigurationActiveStatus() {
590 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
592 String endpoint = "testEndpoint";
593 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
594 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
596 when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse);
599 MsoResponseWrapper response = restClient.setConfigurationActiveStatus(requestDetails, endpoint);
602 assertThat(expectedResponse).isEqualToComparingFieldByField(response);
605 @Test(expectedExceptions = MsoTestException.class)
606 public void shouldThrowExceptionWhenSetConfigurationActiveStatusGetsWrongParameters() {
609 when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception"));
612 restClient.setConfigurationActiveStatus(null, "");
616 public void shouldProperlySetPortOnConfigurationStatus() {
618 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
620 String endpoint = "testEndpoint";
621 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
622 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
624 when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse);
627 MsoResponseWrapper response = restClient.setPortOnConfigurationStatus(requestDetails, endpoint);
630 assertThat(expectedResponse).isEqualToComparingFieldByField(response);
633 @Test(expectedExceptions = MsoTestException.class)
634 public void shouldThrowExceptionWhenSetPortOnConfigurationStatusGetsWrongParameters() {
636 String endpoint = "";
638 when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception"));
641 restClient.setPortOnConfigurationStatus(null, endpoint);
645 public void shouldProperlyChangeManagementUpdate() throws JsonProcessingException {
647 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
648 RequestDetailsWrapper<RequestDetails> requestDetailsWrapper = new RequestDetailsWrapper<>(requestDetails);
650 String endpoint = "testEndpoint";
651 RequestReferencesContainer entity = new RequestReferencesContainer(new RequestReferences());
652 HttpResponse<String> httpResponse = HttpResponse.fallback(JACKSON_OBJECT_MAPPER.writeValueAsString(entity));
654 when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetailsWrapper), eq(String.class))).thenReturn(httpResponse);
657 MsoResponseWrapperInterface response = restClient.changeManagementUpdate(requestDetailsWrapper, endpoint);
660 assertThat(response.getEntity()).isEqualToComparingFieldByField(entity);
661 assertThat(response.getStatus()).isEqualTo(0);
665 public void shouldProperlyUpdateVnfAndUpdateInstance() {
667 org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest();
669 String endpoint = "testEndpoint";
670 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
671 MsoResponseWrapperInterface expectedResponse = MsoUtil.wrapResponse(httpResponse);
674 when(client.put(eq(baseUrl + endpoint), anyMap(), any(RequestDetailsWrapper.class), eq(String.class))).thenReturn(httpResponse);
677 MsoResponseWrapperInterface response = restClient.updateVnf(requestDetails, endpoint);
680 assertThat(expectedResponse).isEqualToComparingFieldByField(response);
683 @Test( expectedExceptions = MsoTestException.class )
684 public void shouldThrowExceptionWhenUpdateVnfAndUpdateInstanceGetsWrongParameter() {
686 org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest();
687 String endpoint = "";
689 when(client.put(eq(baseUrl), anyMap(), any(RequestDetailsWrapper.class), eq(String.class))).thenThrow(new MsoTestException("test-post-exception"));
692 restClient.updateVnf(requestDetails, endpoint);
698 public void shouldProperlySetServiceInstanceStatus() {
700 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
701 RestObject<String> restObject = generateMockMsoRestObject();
703 String endpoint = "testEndpoint";
704 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
706 when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse);
709 restClient.setServiceInstanceStatus(requestDetails, endpoint);
712 @Test( expectedExceptions = MsoTestException.class)
713 public void shouldThrowExceptionWhenSetServiceInstanceStatusGetsWrongParameter() {
715 String endpoint = "";
717 when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception"));
720 restClient.setServiceInstanceStatus(null, endpoint);
724 public void shouldProperlyRemoveRelationshipFromServiceInstance() {
726 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
728 String endpoint = "testEndpoint";
729 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
730 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
732 when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse);
735 MsoResponseWrapper response = restClient.removeRelationshipFromServiceInstance(requestDetails, endpoint);
738 assertThat(expectedResponse).isEqualToComparingFieldByField(response);
741 @Test( expectedExceptions = MsoTestException.class)
742 public void shouldThrowExceptionWhenRemoveRelationshipFromServiceInstanceGetsWrongParameter() {
744 String endpoint = "";
746 when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception"));
749 restClient.removeRelationshipFromServiceInstance(null,endpoint);
753 public void shouldProperlyAddRelationshipToServiceInstance() {
755 RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
757 String endpoint = "testEndpoint";
758 HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
759 MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
761 when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse);
764 MsoResponseWrapper response = restClient.addRelationshipToServiceInstance(requestDetails, endpoint);
767 assertThat(expectedResponse).isEqualToComparingFieldByField(response);
770 @Test( expectedExceptions = MsoTestException.class)
771 public void shouldThrowExceptionWhenAddRelationshipToServiceInstanceGetsWrongParameter() {
773 String endpoint = "";
775 when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception"));
778 restClient.addRelationshipToServiceInstance(null,endpoint);
782 public void shouldProperlyPerformGetRequest() {
784 String endpoint = "testEndpoint";
785 HttpResponse<String> expectedResponse = HttpResponse.fallback("testOkResponse");
787 when(client.get(eq(baseUrl + endpoint), anyMap(), anyMap(), eq(String.class))).thenReturn(expectedResponse);
790 HttpResponse<String> response = restClient.get(endpoint, String.class);
793 assertThat(expectedResponse).isEqualToComparingFieldByField(response);
797 public void shouldProperlyPerformPostRequest() {
800 RequestDetailsWrapper<RequestDetails> requestDetailsWrapper = new RequestDetailsWrapper<>(MsoRestClientTestUtil.generateMockMsoRequest());
802 String endpoint = "testEndpoint";
803 HttpResponse<String> expectedResponse = HttpResponse.fallback("testOkResponse");
805 when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetailsWrapper), eq(String.class))).thenReturn(expectedResponse);
808 HttpResponse<String> response = restClient.post(endpoint,requestDetailsWrapper, String.class);
811 assertThat(expectedResponse).isEqualToComparingFieldByField(response);
815 public void shouldProperlyInvokeWorkflows() {
817 String endpoint = "testPath";
818 HttpResponse expectedResponse = createOkResponse();
820 WorkflowRequestDetail workflowRequestDetail = MsoRestClientTestUtil.createWorkflowRequestDetail();
822 RequestDetailsWrapper<WorkflowRequestDetail> requestDetailsWrapper = new RequestDetailsWrapper<>(workflowRequestDetail);
824 UUID requestId = UUID.randomUUID();
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);
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");
835 MsoResponseWrapper response = restClient.invokeWorkflow(workflowRequestDetail, endpoint, extraHeaders);
838 assertThat(response).isEqualToComparingFieldByField(MsoUtil.wrapResponse(expectedResponse));
842 private class MsoTestException extends RuntimeException{
843 MsoTestException(String testException) {
844 super(testException);
848 private HttpResponse<String> createOkResponse() {
849 StatusLine statusline = new BasicStatusLine(
850 new ProtocolVersion("http",1,1), 202, "acceptResponse");
852 org.apache.http.HttpResponse responseBase = new BasicHttpResponse(statusline);
854 return new HttpResponse<>(responseBase ,String.class, new JsonMapper());
857 private RestObject<String> generateMockMsoRestObject() {
858 RestObject<String> restObject = new RestObject<>();
860 restObject.set("test-rest-object-body");
861 restObject.setRaw("test-rest-object-raw-string");
862 restObject.setStatusCode(202);