2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2018 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.vid.changeManagement.ChangeManagementRequest;
28 import org.onap.vid.changeManagement.RequestDetailsWrapper;
29 import org.onap.vid.client.SyncRestClient;
30 import org.onap.vid.controllers.MsoConfig;
31 import org.onap.vid.controllers.WebConfig;
32 import org.onap.vid.model.RequestReferencesContainer;
33 import org.onap.vid.mso.MsoBusinessLogic;
34 import org.onap.vid.mso.MsoInterface;
35 import org.onap.vid.mso.rest.MsoRestClientNew;
36 import org.onap.vid.mso.rest.RequestDetails;
37 import org.onap.vid.properties.AsdcClientConfiguration;
38 import org.onap.vid.scheduler.SchedulerRestInterfaceIfc;
39 import org.onap.vid.testUtils.RegExMatcher;
40 import org.onap.portalsdk.core.service.DataAccessService;
41 import org.onap.portalsdk.core.util.SystemProperties;
42 import org.skyscreamer.jsonassert.JSONAssert;
43 import org.skyscreamer.jsonassert.JSONCompareMode;
44 import org.springframework.context.annotation.Bean;
45 import org.springframework.context.annotation.Configuration;
46 import org.springframework.test.context.ContextConfiguration;
47 import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
48 import org.springframework.test.context.web.WebAppConfiguration;
49 import org.testng.annotations.Test;
51 import javax.inject.Inject;
54 import static org.hamcrest.MatcherAssert.assertThat;
55 import static org.hamcrest.core.IsEqual.equalTo;
56 import static org.hamcrest.core.IsInstanceOf.instanceOf;
57 import static org.mockito.Matchers.anyObject;
58 import static org.mockito.Matchers.anyString;
59 import static org.mockito.Mockito.*;
63 @ContextConfiguration(classes = {WebConfig.class, AsdcClientConfiguration.class, SystemProperties.class, ChangeManagementServiceUnitTest.TestMsoConfig.class})
65 public class ChangeManagementServiceUnitTest extends AbstractTestNGSpringContextTests {
67 private ObjectMapper objectMapper = new ObjectMapper();
69 private ChangeManagementService changeManagementService;
71 private MsoInterface restClientUnderTest;
74 void testInPlaceSoftwareUpdateRequest() throws Exception {
77 doReturn(new HttpResponse<>(anyObject(), RequestReferencesContainer.class, anyObject())).when(restClientUnderTest).post(anyString(), anyObject(), anyObject());
79 URL requestJsonUrl = this.getClass().getResource("/services/change_management_software_update_request.json");
80 ChangeManagementRequest changeManagementRequest = objectMapper.readValue(requestJsonUrl, ChangeManagementRequest.class);
81 changeManagementService.doChangeManagement(changeManagementRequest, "vidVnf");
83 ArgumentCaptor<String> endpointCaptor = ArgumentCaptor.forClass(String.class);
84 ArgumentCaptor<RequestDetailsWrapper> requestCaptor = ArgumentCaptor.forClass(RequestDetailsWrapper.class);
85 ArgumentCaptor<Class> responseTypeCaptor = ArgumentCaptor.forClass(Class.class);
86 verify(restClientUnderTest).post(endpointCaptor.capture(), requestCaptor.capture(), responseTypeCaptor.capture());
88 org.onap.vid.changeManagement.RequestDetails expectedRequest = changeManagementRequest.getRequestDetails().get(0);
90 String serviceInstanceId = expectedRequest.getRelatedInstList().get(0).getRelatedInstance().instanceId;
92 String vnfInstanceId = expectedRequest.getVnfInstanceId();
93 String regEx = String.format("/serviceInstances/v[0-9]+/%s/vnfs/%s/inPlaceSoftwareUpdate", serviceInstanceId, vnfInstanceId);
94 assertThat(endpointCaptor.getValue(), RegExMatcher.matchesRegEx(regEx));
95 assertThat(requestCaptor.getValue(), instanceOf(RequestDetails.class));
96 RequestDetails actualRequest = ((RequestDetails) requestCaptor.getValue().requestDetails);
98 assertThat(actualRequest.getCloudConfiguration().getTenantId(), equalTo(expectedRequest.getCloudConfiguration().getTenantId()));
99 assertThat(actualRequest.getCloudConfiguration().getLcpCloudRegionId(), equalTo(expectedRequest.getCloudConfiguration().getLcpCloudRegionId()));
100 assertThat(actualRequest.getRequestInfo(), equalTo(expectedRequest.getRequestInfo()));
101 assertThat(actualRequest.getRequestParameters(), equalTo(expectedRequest.getRequestParameters()));
103 URL expectedMsoRequestUrl = this.getClass().getResource("/services/change_management_software_update_expected_mso_request.json");
104 String expectedMsoRequestString = IOUtils.toString(expectedMsoRequestUrl, "UTF-8");
105 String actualRequestString = objectMapper.writeValueAsString(actualRequest);
107 JSONAssert.assertEquals("built mso request is not ok", expectedMsoRequestString, actualRequestString, JSONCompareMode.NON_EXTENSIBLE);
108 } catch (AssertionError | Exception e) {
109 System.out.println("requestDetailsAsString: \n" + actualRequestString);
110 System.out.println("expected: \n" + expectedMsoRequestString);
117 public static class TestMsoConfig extends MsoConfig {
120 public MsoRestClientNew getMsoClient() {
121 MsoRestClientNew spyClient = spy(new MsoRestClientNew(new SyncRestClient(), ""));
126 public ChangeManagementService getChangeManagementService(DataAccessService dataAccessService, MsoBusinessLogic msoInterface, SchedulerRestInterfaceIfc schedulerRestInterface) {
127 return new ChangeManagementServiceImpl(dataAccessService, msoInterface, schedulerRestInterface);