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=========================================================
22 package org.onap.vid.mso;
24 import com.fasterxml.jackson.core.type.TypeReference;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import org.apache.commons.io.IOUtils;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.ArgumentMatcher;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.onap.portalsdk.core.util.SystemProperties;
34 import org.onap.vid.exceptions.GenericUncheckedException;
35 import org.onap.vid.mso.rest.Request;
36 import org.onap.vid.mso.rest.RequestDetails;
37 import org.onap.vid.mso.rest.RequestDetailsWrapper;
38 import org.onap.vid.properties.Features;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.test.context.ContextConfiguration;
41 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
42 import org.togglz.core.manager.FeatureManager;
44 import java.io.IOException;
46 import java.nio.file.Path;
47 import java.nio.file.Paths;
48 import java.util.List;
49 import java.util.stream.Collectors;
51 import static org.assertj.core.api.Assertions.assertThat;
52 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
53 import static org.junit.Assert.assertEquals;
54 import static org.mockito.ArgumentMatchers.*;
55 import static org.mockito.BDDMockito.given;
56 import static org.mockito.Mockito.mock;
57 import static org.onap.vid.mso.MsoBusinessLogicImpl.validateEndpointPath;
59 @ContextConfiguration(classes = {SystemProperties.class})
60 @RunWith(SpringJUnit4ClassRunner.class)
61 public class MsoBusinessLogicImplTest {
63 private static final ObjectMapper objectMapper = new ObjectMapper();
66 private FeatureManager featureManager;
69 private MsoInterface msoInterface;
71 private MsoBusinessLogicImpl msoBusinessLogic;
75 MockitoAnnotations.initMocks(this);
76 msoBusinessLogic = new MsoBusinessLogicImpl(msoInterface, featureManager);
80 public void createConfigurationInstance_shouldCallMsoInterface_withCorrectServiceInstanceId() throws Exception {
82 String serviceInstanceId = "3f93c7cb-2fd0-4557-9514-e189b7b04f9d";
83 String endpointTemplate = String.format("/serviceInstances/v6/%s/configurations", serviceInstanceId);
84 RequestDetailsWrapper requestDetailsWrapper = createRequestDetails("mso_request_create_configuration.json");
85 MsoResponseWrapper expectedResponse = createOkResponse();
86 given(msoInterface.createConfigurationInstance(requestDetailsWrapper, endpointTemplate))
87 .willReturn(expectedResponse);
90 MsoResponseWrapper msoResponseWrapper = msoBusinessLogic
91 .createConfigurationInstance(requestDetailsWrapper, serviceInstanceId);
94 assertThat(msoResponseWrapper).isEqualToComparingFieldByField(expectedResponse);
97 private RequestDetailsWrapper createRequestDetails(String bodyFileName) throws Exception {
98 final URL resource = this.getClass().getResource("/payload_jsons/" + bodyFileName);
99 RequestDetails requestDetails = objectMapper.readValue(resource, RequestDetails.class);
100 return new RequestDetailsWrapper(requestDetails);
104 public void validateEndpointPath_endPointIsNotEmptyAndVaild_returnProperty() {
105 System.setProperty("TestEnv", "123");
106 String foundEndPoint = validateEndpointPath("TestEnv");
107 assertEquals("123", foundEndPoint);
111 public void validateEndpointPath_endPointIsNull_throwRuntimeException() {
112 assertThatExceptionOfType(RuntimeException.class)
113 .isThrownBy(() -> validateEndpointPath("NotExists"));
117 public void validateEndpointPath_endPointIsNotEmptyButDoesntExists_throwRuntimeException() {
118 String endPoint = "EmptyEndPoint";
119 System.setProperty(endPoint, "");
120 assertThatExceptionOfType(GenericUncheckedException.class)
121 .isThrownBy(() -> validateEndpointPath(endPoint))
122 .withMessage(endPoint + " env variable is not defined");
126 public void deleteSvcInstance_verifyEndPointPathConstructing_unAssignFeatureOffOrUnAssignFlagIsFalse() {
128 String endpointTemplate = "/serviceInstances/v5/%s";
129 String serviceInstanceId = "3f93c7cb-2fd0-4557-9514-e189b7b04f9d";
130 String svcEndpoint = String.format(endpointTemplate, serviceInstanceId);
131 RequestDetails requestDetails = new RequestDetails();
132 MsoResponseWrapper expectedResponse = createOkResponse();
133 given(msoInterface.deleteSvcInstance(requestDetails, svcEndpoint)).willReturn(expectedResponse);
134 given(featureManager.isActive(Features.FLAG_UNASSIGN_SERVICE)).willReturn(false);
137 MsoResponseWrapper msoResponseWrapper = msoBusinessLogic
138 .deleteSvcInstance(requestDetails, serviceInstanceId, "unAssignOrDeleteParams");
141 assertThat(msoResponseWrapper).isEqualToComparingFieldByField(expectedResponse);
145 public void deleteSvcInstance_verifyEndPointPathConstructing_unAssignFeatureOnAndUnAssignFlagIsTrue() {
147 String endpointTemplate = "/serviceInstantiation/v5/serviceInstances/%s/unassign";
148 String serviceInstanceId = "3f93c7cb-2fd0-4557-9514-e189b7b04f9d";
149 String svcEndpoint = String.format(endpointTemplate, serviceInstanceId);
150 RequestDetails requestDetails = new RequestDetails();
151 MsoResponseWrapper expectedResponse = createOkResponse();
152 given(msoInterface.unassignSvcInstance(requestDetails, svcEndpoint)).willReturn(expectedResponse);
153 given(featureManager.isActive(Features.FLAG_UNASSIGN_SERVICE)).willReturn(true);
156 MsoResponseWrapper msoResponseWrapper = msoBusinessLogic
157 .deleteSvcInstance(requestDetails, serviceInstanceId, "assigned");
160 assertThat(msoResponseWrapper).isEqualToComparingFieldByField(expectedResponse);
164 public void deleteVnf_verifyEndPointPathConstructing() {
166 String endpointTemplate = "/serviceInstances/v5/%s/vnfs/%s";
167 String serviceInstanceId = "3f93c7cb-2fd0-4557-9514-e189b7b04f9d";
168 String vnfInstanceId = "testVnfInstanceTempId";
169 String vnfEndpoint = String.format(endpointTemplate, serviceInstanceId, vnfInstanceId);
170 RequestDetails requestDetails = new RequestDetails();
171 MsoResponseWrapper expectedResponse = createOkResponse();
172 given(msoInterface.deleteVnf(requestDetails, vnfEndpoint)).willReturn(expectedResponse);
175 MsoResponseWrapper msoResponseWrapper = msoBusinessLogic
176 .deleteVnf(requestDetails, serviceInstanceId, vnfInstanceId);
179 assertThat(msoResponseWrapper).isEqualToComparingFieldByField(expectedResponse);
183 public void deleteVfModule_verifyEndPointPathConstructing() {
185 String endpointTemplate = "/serviceInstances/v7/%s/vnfs/%s/vfModules/%s";
186 String serviceInstanceId = "3f93c7cb-2fd0-4557-9514-e189b7b04f9d";
187 String vnfInstanceId = "testVnfInstanceTempId";
188 String vfModuleId = "testVfModuleId";
189 String vnfEndpoint = String.format(endpointTemplate, serviceInstanceId, vnfInstanceId, vfModuleId);
190 RequestDetails requestDetails = new RequestDetails();
191 MsoResponseWrapper expectedResponse = createOkResponse();
192 given(msoInterface.deleteVfModule(requestDetails, vnfEndpoint)).willReturn(expectedResponse);
195 MsoResponseWrapper msoResponseWrapper = msoBusinessLogic
196 .deleteVfModule(requestDetails, serviceInstanceId, vnfInstanceId, vfModuleId);
198 assertThat(msoResponseWrapper).isEqualToComparingFieldByField(expectedResponse);
202 public void shouldSendProperScaleOutRequest() throws IOException {
204 String serviceInstanceId = "3f93c7cb-2fd0-4557-9514-e189b7b04f9d";
205 String vnfInstanceId = "testVnfInstanceTempId";
206 String endpointTemplate = "/serviceInstantiation/v7/serviceInstances/%s/vnfs/%s/vfModules/scaleOut";
207 String vnfEndpoint = String.format(endpointTemplate, serviceInstanceId, vnfInstanceId);
208 org.onap.vid.changeManagement.RequestDetails requestDetails = readRequest(
209 "scaleOutVfModulePayload.json");
210 org.onap.vid.changeManagement.RequestDetailsWrapper<org.onap.vid.changeManagement.RequestDetails> expectedRequest = readExpectedRequest(
211 "scaleOutVfModulePayloadToMso.json");
212 MsoResponseWrapper expectedMsoResponseWrapper = createOkResponse();
215 .scaleOutVFModuleInstance(argThat(new MsoRequestWrapperMatcher(expectedRequest)),
217 .willReturn(expectedMsoResponseWrapper);
220 MsoResponseWrapper msoResponseWrapper = msoBusinessLogic
221 .scaleOutVfModuleInstance(requestDetails, serviceInstanceId, vnfInstanceId);
224 assertThat(msoResponseWrapper).isEqualToComparingFieldByField(expectedMsoResponseWrapper);
227 private org.onap.vid.changeManagement.RequestDetails readRequest(String requestJsonFilename) throws IOException {
228 Path path = Paths.get("payload_jsons", requestJsonFilename);
229 URL url = this.getClass().getClassLoader().getResource(path.toString());
230 return objectMapper.readValue(url, org.onap.vid.changeManagement.RequestDetails.class);
233 private org.onap.vid.changeManagement.RequestDetailsWrapper<org.onap.vid.changeManagement.RequestDetails> readExpectedRequest(String requestJsonFilename)
235 Path path = Paths.get("payload_jsons", requestJsonFilename);
236 URL url = this.getClass().getClassLoader().getResource(path.toString());
237 return objectMapper.readValue(url,
238 new TypeReference<org.onap.vid.changeManagement.RequestDetailsWrapper<org.onap.vid.changeManagement.RequestDetails>>() {
242 private MsoResponseWrapper createOkResponse() {
243 HttpStatus expectedStatus = HttpStatus.ACCEPTED;
244 String expectedBody = " \"body\": {\n" +
245 " \"requestReferences\": {\n" +
246 " \"instanceId\": \" 123456 \",\n" +
247 " \"requestId\": \"b6dc9806-b094-42f7-9386-a48de8218ce8\"\n" +
249 MsoResponseWrapper responseWrapper = new MsoResponseWrapper();
250 responseWrapper.setEntity(expectedBody);
251 responseWrapper.setStatus(expectedStatus.value());
252 return responseWrapper;
256 public void shouldFilterOutOrchestrationRequestsNotAllowedInDashboard() throws Exception {
258 String vnfModelTypeOrchestrationRequests = getFileContentAsString("mso_model_info_sample_response.json");
259 String scaleOutActionOrchestrationRequests = getFileContentAsString("mso_action_scaleout_sample_response.json");
261 MsoResponseWrapper msoResponseWrapperMock = mock(MsoResponseWrapper.class);
263 .getOrchestrationRequest(any(String.class), any(String.class), any(String.class),
264 any(RestObject.class), anyBoolean()))
265 .willReturn(msoResponseWrapperMock);
266 given(msoResponseWrapperMock.getEntity())
267 .willReturn(vnfModelTypeOrchestrationRequests, scaleOutActionOrchestrationRequests);
270 List<Request> filteredOrchestrationReqs = msoBusinessLogic.getOrchestrationRequestsForDashboard();
273 assertThat(filteredOrchestrationReqs).hasSize(3);
274 assertThat(MsoBusinessLogicImpl.DASHBOARD_ALLOWED_TYPES)
275 .containsAll(filteredOrchestrationReqs
277 .map(el -> el.getRequestType().toUpperCase())
278 .collect(Collectors.toList()));
279 assertThat(filteredOrchestrationReqs)
280 .extracting(Request::getRequestScope)
281 .containsOnly("vnf", "vfModule");
284 private String getFileContentAsString(String resourceName) throws Exception {
285 Path path = Paths.get("payload_jsons", resourceName);
286 URL url = this.getClass().getClassLoader().getResource(path.toString());
287 return IOUtils.toString(url.toURI(), "UTF-8");
290 private static class MsoRequestWrapperMatcher implements
291 ArgumentMatcher<org.onap.vid.changeManagement.RequestDetailsWrapper> {
293 private final org.onap.vid.changeManagement.RequestDetailsWrapper expectedRequest;
295 public MsoRequestWrapperMatcher(org.onap.vid.changeManagement.RequestDetailsWrapper expectedRequest) {
296 this.expectedRequest = expectedRequest;
300 public boolean matches(org.onap.vid.changeManagement.RequestDetailsWrapper argument) {
301 return expectedRequest.requestDetails.equals(argument.requestDetails);