d849869b08baab7cb32661ba85eee22cbbee404d
[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 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
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.changeManagement.ChangeManagementRequest;
30 import org.onap.vid.changeManagement.RequestDetailsWrapper;
31 import org.onap.vid.client.SyncRestClient;
32 import org.onap.vid.controllers.MsoConfig;
33 import org.onap.vid.controllers.WebConfig;
34 import org.onap.vid.model.RequestReferencesContainer;
35 import org.onap.vid.mso.MsoBusinessLogic;
36 import org.onap.vid.mso.MsoInterface;
37 import org.onap.vid.mso.rest.MsoRestClientNew;
38 import org.onap.vid.mso.rest.RequestDetails;
39 import org.onap.vid.properties.AsdcClientConfiguration;
40 import org.onap.vid.scheduler.SchedulerRestInterfaceIfc;
41 import org.onap.vid.testUtils.RegExMatcher;
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;
50
51 import javax.inject.Inject;
52 import java.net.URL;
53
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.ArgumentMatchers.any;
58 import static org.mockito.ArgumentMatchers.anyString;
59 import static org.mockito.Mockito.*;
60
61
62 @Test
63 @ContextConfiguration(classes = {WebConfig.class, AsdcClientConfiguration.class, SystemProperties.class, ChangeManagementServiceUnitTest.TestMsoConfig.class})
64 @WebAppConfiguration
65 public class ChangeManagementServiceUnitTest extends AbstractTestNGSpringContextTests {
66
67     private ObjectMapper objectMapper = new ObjectMapper();
68     @Inject
69     private ChangeManagementService changeManagementService;
70     @Inject
71     private MsoInterface restClientUnderTest;
72
73    // @Test
74     void testInPlaceSoftwareUpdateRequest() throws Exception {
75
76
77         doReturn(new HttpResponse<>(any(), RequestReferencesContainer.class, any())).when(restClientUnderTest).post(anyString(), any(), any());
78
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");
82
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());
87
88         org.onap.vid.changeManagement.RequestDetails expectedRequest = changeManagementRequest.getRequestDetails().get(0);
89
90         String serviceInstanceId = expectedRequest.getRelatedInstList().get(0).getRelatedInstance().instanceId;
91         ;
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);
97
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()));
102
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);
106         try {
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);
111             throw e;
112         }
113
114     }
115
116     @Configuration
117     public static class TestMsoConfig extends MsoConfig {
118
119         @Override
120         public MsoRestClientNew getMsoClient() {
121             MsoRestClientNew spyClient = spy(new MsoRestClientNew(new SyncRestClient(), ""));
122             return spyClient;
123         }
124
125         @Bean
126         public ChangeManagementService getChangeManagementService(DataAccessService dataAccessService, MsoBusinessLogic msoInterface, SchedulerRestInterfaceIfc schedulerRestInterface) {
127             return new ChangeManagementServiceImpl(dataAccessService, msoInterface, schedulerRestInterface);
128         }
129     }
130 }