Increasing test coverage for vid.mso.rest
[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.skyscreamer.jsonassert.JSONAssert;
44 import org.skyscreamer.jsonassert.JSONCompareMode;
45 import org.springframework.context.annotation.Bean;
46 import org.springframework.context.annotation.Configuration;
47 import org.springframework.test.context.ContextConfiguration;
48 import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
49 import org.springframework.test.context.web.WebAppConfiguration;
50 import org.testng.annotations.Test;
51
52 import javax.inject.Inject;
53 import java.net.URL;
54
55 import static org.hamcrest.MatcherAssert.assertThat;
56 import static org.hamcrest.core.IsEqual.equalTo;
57 import static org.hamcrest.core.IsInstanceOf.instanceOf;
58 import static org.mockito.ArgumentMatchers.any;
59 import static org.mockito.ArgumentMatchers.anyString;
60 import static org.mockito.Mockito.*;
61
62
63 @Test
64 @ContextConfiguration(classes = {WebConfig.class, AsdcClientConfiguration.class, SystemProperties.class, ChangeManagementServiceUnitTest.TestMsoConfig.class})
65 @WebAppConfiguration
66 public class ChangeManagementServiceUnitTest extends AbstractTestNGSpringContextTests {
67
68     private ObjectMapper objectMapper = new ObjectMapper();
69     @Inject
70     private ChangeManagementService changeManagementService;
71     @Inject
72     private MsoInterface restClientUnderTest;
73
74    // @Test
75     void testInPlaceSoftwareUpdateRequest() throws Exception {
76
77
78         doReturn(new HttpResponse<>(any(), RequestReferencesContainer.class, any())).when(restClientUnderTest).post(anyString(), any(), any());
79
80         URL requestJsonUrl = this.getClass().getResource("/services/change_management_software_update_request.json");
81         ChangeManagementRequest changeManagementRequest = objectMapper.readValue(requestJsonUrl, ChangeManagementRequest.class);
82         changeManagementService.doChangeManagement(changeManagementRequest, "vidVnf");
83
84         ArgumentCaptor<String> endpointCaptor = ArgumentCaptor.forClass(String.class);
85         ArgumentCaptor<RequestDetailsWrapper> requestCaptor = ArgumentCaptor.forClass(RequestDetailsWrapper.class);
86         ArgumentCaptor<Class> responseTypeCaptor = ArgumentCaptor.forClass(Class.class);
87         verify(restClientUnderTest).post(endpointCaptor.capture(), requestCaptor.capture(), responseTypeCaptor.capture());
88
89         org.onap.vid.changeManagement.RequestDetails expectedRequest = changeManagementRequest.getRequestDetails().get(0);
90
91         String serviceInstanceId = expectedRequest.getRelatedInstList().get(0).getRelatedInstance().instanceId;
92         ;
93         String vnfInstanceId = expectedRequest.getVnfInstanceId();
94         String regEx = String.format("/serviceInstances/v[0-9]+/%s/vnfs/%s/inPlaceSoftwareUpdate", serviceInstanceId, vnfInstanceId);
95         assertThat(endpointCaptor.getValue(), RegExMatcher.matchesRegEx(regEx));
96         assertThat(requestCaptor.getValue(), instanceOf(RequestDetails.class));
97         RequestDetails actualRequest = ((RequestDetails) requestCaptor.getValue().requestDetails);
98
99         assertThat(actualRequest.getCloudConfiguration().getTenantId(), equalTo(expectedRequest.getCloudConfiguration().getTenantId()));
100         assertThat(actualRequest.getCloudConfiguration().getLcpCloudRegionId(), equalTo(expectedRequest.getCloudConfiguration().getLcpCloudRegionId()));
101         assertThat(actualRequest.getRequestInfo(), equalTo(expectedRequest.getRequestInfo()));
102         assertThat(actualRequest.getRequestParameters(), equalTo(expectedRequest.getRequestParameters()));
103
104         URL expectedMsoRequestUrl = this.getClass().getResource("/services/change_management_software_update_expected_mso_request.json");
105         String expectedMsoRequestString = IOUtils.toString(expectedMsoRequestUrl, "UTF-8");
106         String actualRequestString = objectMapper.writeValueAsString(actualRequest);
107         try {
108             JSONAssert.assertEquals("built mso request is not ok", expectedMsoRequestString, actualRequestString, JSONCompareMode.NON_EXTENSIBLE);
109         } catch (AssertionError | Exception e) {
110             System.out.println("requestDetailsAsString: \n" + actualRequestString);
111             System.out.println("expected: \n" + expectedMsoRequestString);
112             throw e;
113         }
114
115     }
116
117     @Configuration
118     public static class TestMsoConfig extends MsoConfig {
119
120         @Bean
121         public ChangeManagementService getChangeManagementService(DataAccessService dataAccessService, MsoBusinessLogic msoInterface, SchedulerRestInterfaceIfc schedulerRestInterface, CloudOwnerService cloudOwnerService) {
122             return new ChangeManagementServiceImpl(dataAccessService, msoInterface, schedulerRestInterface, cloudOwnerService);
123         }
124     }
125 }