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.services;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import io.joshworks.restclient.http.HttpResponse;
25 import org.apache.commons.io.IOUtils;
26 import org.mockito.ArgumentCaptor;
27 import org.onap.portalsdk.core.service.DataAccessService;
28 import org.onap.portalsdk.core.util.SystemProperties;
29 import org.onap.vid.aai.util.HttpsAuthClient;
30 import org.onap.vid.changeManagement.ChangeManagementRequest;
31 import org.onap.vid.changeManagement.RequestDetailsWrapper;
32 import org.onap.vid.client.SyncRestClient;
33 import org.onap.vid.controller.MsoConfig;
34 import org.onap.vid.controller.WebConfig;
35 import org.onap.vid.model.RequestReferencesContainer;
36 import org.onap.vid.mso.MsoBusinessLogic;
37 import org.onap.vid.mso.MsoInterface;
38 import org.onap.vid.mso.rest.MsoRestClientNew;
39 import org.onap.vid.mso.rest.RequestDetails;
40 import org.onap.vid.properties.AsdcClientConfiguration;
41 import org.onap.vid.scheduler.SchedulerRestInterfaceIfc;
42 import org.onap.vid.testUtils.RegExMatcher;
43 import org.onap.vid.utils.SystemPropertiesWrapper;
44 import org.skyscreamer.jsonassert.JSONAssert;
45 import org.skyscreamer.jsonassert.JSONCompareMode;
46 import org.springframework.context.annotation.Bean;
47 import org.springframework.context.annotation.Configuration;
48 import org.springframework.test.context.ContextConfiguration;
49 import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
50 import org.springframework.test.context.web.WebAppConfiguration;
51 import org.testng.annotations.Test;
53 import javax.inject.Inject;
56 import static org.hamcrest.MatcherAssert.assertThat;
57 import static org.hamcrest.core.IsEqual.equalTo;
58 import static org.hamcrest.core.IsInstanceOf.instanceOf;
59 import static org.mockito.ArgumentMatchers.any;
60 import static org.mockito.ArgumentMatchers.anyString;
61 import static org.mockito.Mockito.*;
65 @ContextConfiguration(classes = {WebConfig.class, AsdcClientConfiguration.class, SystemProperties.class, ChangeManagementServiceUnitTest.TestMsoConfig.class})
67 public class ChangeManagementServiceUnitTest extends AbstractTestNGSpringContextTests {
69 private ObjectMapper objectMapper = new ObjectMapper();
71 private ChangeManagementService changeManagementService;
73 private MsoInterface restClientUnderTest;
76 void testInPlaceSoftwareUpdateRequest() throws Exception {
79 doReturn(new HttpResponse<>(any(), RequestReferencesContainer.class, any())).when(restClientUnderTest).post(anyString(), any(RequestDetailsWrapper.class), any());
81 URL requestJsonUrl = this.getClass().getResource("/services/change_management_software_update_request.json");
82 ChangeManagementRequest changeManagementRequest = objectMapper.readValue(requestJsonUrl, ChangeManagementRequest.class);
83 changeManagementService.doChangeManagement(changeManagementRequest, "vidVnf");
85 ArgumentCaptor<String> endpointCaptor = ArgumentCaptor.forClass(String.class);
86 ArgumentCaptor<RequestDetailsWrapper> requestCaptor = ArgumentCaptor.forClass(RequestDetailsWrapper.class);
87 ArgumentCaptor<Class> responseTypeCaptor = ArgumentCaptor.forClass(Class.class);
88 verify(restClientUnderTest).post(endpointCaptor.capture(), requestCaptor.capture(), responseTypeCaptor.capture());
90 org.onap.vid.changeManagement.RequestDetails expectedRequest = changeManagementRequest.getRequestDetails().get(0);
92 String serviceInstanceId = expectedRequest.getRelatedInstList().get(0).getRelatedInstance().instanceId;
94 String vnfInstanceId = expectedRequest.getVnfInstanceId();
95 String regEx = String.format("/serviceInstances/v[0-9]+/%s/vnfs/%s/inPlaceSoftwareUpdate", serviceInstanceId, vnfInstanceId);
96 assertThat(endpointCaptor.getValue(), RegExMatcher.matchesRegEx(regEx));
97 assertThat(requestCaptor.getValue(), instanceOf(RequestDetails.class));
98 RequestDetails actualRequest = ((RequestDetails) requestCaptor.getValue().requestDetails);
100 assertThat(actualRequest.getCloudConfiguration().getTenantId(), equalTo(expectedRequest.getCloudConfiguration().getTenantId()));
101 assertThat(actualRequest.getCloudConfiguration().getLcpCloudRegionId(), equalTo(expectedRequest.getCloudConfiguration().getLcpCloudRegionId()));
102 assertThat(actualRequest.getRequestInfo(), equalTo(expectedRequest.getRequestInfo()));
103 assertThat(actualRequest.getRequestParameters(), equalTo(expectedRequest.getRequestParameters()));
105 URL expectedMsoRequestUrl = this.getClass().getResource("/services/change_management_software_update_expected_mso_request.json");
106 String expectedMsoRequestString = IOUtils.toString(expectedMsoRequestUrl, "UTF-8");
107 String actualRequestString = objectMapper.writeValueAsString(actualRequest);
109 JSONAssert.assertEquals("built mso request is not ok", expectedMsoRequestString, actualRequestString, JSONCompareMode.NON_EXTENSIBLE);
110 } catch (AssertionError | Exception e) {
111 System.out.println("requestDetailsAsString: \n" + actualRequestString);
112 System.out.println("expected: \n" + expectedMsoRequestString);
119 public static class TestMsoConfig extends MsoConfig {
122 public ChangeManagementService getChangeManagementService(DataAccessService dataAccessService, MsoBusinessLogic msoInterface, SchedulerRestInterfaceIfc schedulerRestInterface, CloudOwnerService cloudOwnerService, SystemPropertiesWrapper systemPropertiesWrapper) {
123 return new ChangeManagementServiceImpl(dataAccessService, msoInterface, schedulerRestInterface, cloudOwnerService, systemPropertiesWrapper);