Sending workflow data from UI to SO
[vid.git] / vid-app-common / src / test / java / org / onap / vid / services / ChangeManagementServiceUnitTest.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.services;
22
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;
52
53 import javax.inject.Inject;
54 import java.net.URL;
55
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.*;
62
63
64 @Test
65 @ContextConfiguration(classes = {WebConfig.class, AsdcClientConfiguration.class, SystemProperties.class, ChangeManagementServiceUnitTest.TestMsoConfig.class})
66 @WebAppConfiguration
67 public class ChangeManagementServiceUnitTest extends AbstractTestNGSpringContextTests {
68
69     private ObjectMapper objectMapper = new ObjectMapper();
70     @Inject
71     private ChangeManagementService changeManagementService;
72     @Inject
73     private MsoInterface restClientUnderTest;
74
75    // @Test
76     void testInPlaceSoftwareUpdateRequest() throws Exception {
77
78
79         doReturn(new HttpResponse<>(any(), RequestReferencesContainer.class, any())).when(restClientUnderTest).post(anyString(), any(), any());
80
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");
84
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());
89
90         org.onap.vid.changeManagement.RequestDetails expectedRequest = changeManagementRequest.getRequestDetails().get(0);
91
92         String serviceInstanceId = expectedRequest.getRelatedInstList().get(0).getRelatedInstance().instanceId;
93         ;
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);
99
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()));
104
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);
108         try {
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);
113             throw e;
114         }
115
116     }
117
118     @Configuration
119     public static class TestMsoConfig extends MsoConfig {
120
121         @Bean
122         public ChangeManagementService getChangeManagementService(DataAccessService dataAccessService, MsoBusinessLogic msoInterface, SchedulerRestInterfaceIfc schedulerRestInterface, CloudOwnerService cloudOwnerService, SystemPropertiesWrapper systemPropertiesWrapper) {
123             return new ChangeManagementServiceImpl(dataAccessService, msoInterface, schedulerRestInterface, cloudOwnerService, systemPropertiesWrapper);
124         }
125     }
126 }