3 * * ============LICENSE_START=======================================================
4 * * Copyright (C) 2019 Nordix Foundation.
5 * * ================================================================================
6 * * Licensed under the Apache License, Version 2.0 (the "License");
7 * * you may not use this file except in compliance with the License.
8 * * You may obtain a copy of the License at
10 * * http://www.apache.org/licenses/LICENSE-2.0
12 * * Unless required by applicable law or agreed to in writing, software
13 * * distributed under the License is distributed on an "AS IS" BASIS,
14 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * * See the License for the specific language governing permissions and
16 * * limitations under the License.
18 * * SPDX-License-Identifier: Apache-2.0
19 * * ============LICENSE_END=========================================================
23 package org.openecomp.sdc.be.components.upgrade;
25 import fj.data.Either;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.InjectMocks;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
33 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
34 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
35 import org.openecomp.sdc.be.components.validation.UserValidations;
36 import org.openecomp.sdc.be.dao.api.ActionStatus;
37 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
38 import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition;
39 import org.openecomp.sdc.be.datatypes.components.ServiceMetadataDataDefinition;
40 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
41 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
42 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
43 import org.openecomp.sdc.be.impl.ComponentsUtils;
44 import org.openecomp.sdc.be.model.Component;
45 import org.openecomp.sdc.be.model.ComponentDependency;
46 import org.openecomp.sdc.be.model.ComponentInstance;
47 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
48 import org.openecomp.sdc.be.model.ComponentParametersView;
49 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
50 import org.openecomp.sdc.be.model.LifecycleStateEnum;
51 import org.openecomp.sdc.be.model.Resource;
52 import org.openecomp.sdc.be.model.ResourceMetadataDefinition;
53 import org.openecomp.sdc.be.model.Service;
54 import org.openecomp.sdc.be.model.ServiceMetadataDefinition;
55 import org.openecomp.sdc.be.model.User;
56 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
57 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.UpgradeOperation;
58 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
59 import org.openecomp.sdc.be.user.Role;
60 import org.openecomp.sdc.exception.ResponseFormat;
62 import java.util.ArrayList;
63 import java.util.HashMap;
64 import java.util.List;
67 import static org.junit.Assert.assertEquals;
68 import static org.mockito.ArgumentMatchers.any;
69 import static org.mockito.ArgumentMatchers.anyBoolean;
70 import static org.mockito.ArgumentMatchers.anyString;
71 import static org.mockito.ArgumentMatchers.eq;
72 import static org.mockito.Mockito.doReturn;
73 import static org.mockito.Mockito.when;
76 public class UpgradeBusinessLogicTest {
78 private static final String COMPONENT_ID = "componentId";
79 private static final String USER_ID = "admin123";
80 private static final String SERVICE_ID = "service01";
81 private static final String RESOURCE_ID = "resource01";
84 private Resource resource;
85 private Service service;
86 private ResourceMetadataDefinition resourceMetadataDefinition;
87 private ResourceMetadataDataDefinition resourceMetadataDataDefinition;
88 private ServiceMetadataDefinition serviceMetadataDefinition;
89 private ServiceMetadataDataDefinition serviceMetadataDataDefinition;
92 private LifecycleBusinessLogic lifecycleBusinessLogic;
95 private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
98 private UserValidations userValidations;
101 private ToscaOperationFacade toscaOperationFacade;
104 private ComponentsUtils componentsUtils;
107 private UpgradeOperation upgradeOperation;
110 private JanusGraphDao janusGraphDao;
113 private UpgradeBusinessLogic upgradeBusinessLogic;
116 public void setUp() {
117 MockitoAnnotations.initMocks(this);
119 upgradeBusinessLogic = new UpgradeBusinessLogic(lifecycleBusinessLogic, componentInstanceBusinessLogic,
120 userValidations, toscaOperationFacade, componentsUtils, upgradeOperation,
124 user.setRole(Role.ADMIN.name());
125 user.setUserId(USER_ID);
127 resourceMetadataDataDefinition = new ResourceMetadataDataDefinition();
128 resourceMetadataDefinition = new ResourceMetadataDefinition(resourceMetadataDataDefinition);
129 serviceMetadataDataDefinition = new ServiceMetadataDataDefinition();
130 serviceMetadataDefinition = new ServiceMetadataDefinition(serviceMetadataDataDefinition);
132 resource = new Resource(resourceMetadataDefinition);
133 service = new Service(serviceMetadataDefinition);
135 when(userValidations.validateUserExists(eq(user.getUserId())))
140 public void testAutomatedUpgrade_givenValidResource_returnsSuccessful() {
142 resourceMetadataDataDefinition.setHighestVersion(true);
143 resourceMetadataDataDefinition.setLifecycleState(LifecycleStateEnum.CERTIFIED.name());
144 resourceMetadataDataDefinition.setComponentType(ComponentTypeEnum.RESOURCE);
145 resourceMetadataDataDefinition.setResourceType(ResourceTypeEnum.VF);
147 when(toscaOperationFacade.getToscaFullElement(anyString()))
148 .thenReturn(Either.left(resource));
150 UpgradeStatus automatedUpgradeStatus = upgradeBusinessLogic.automatedUpgrade(COMPONENT_ID, getRequests(), user.getUserId());
151 assertEquals(ActionStatus.OK, automatedUpgradeStatus.getStatus());
155 public void testAutomatedUpgrade_givenResourceIsNotVFType_returnsUnsuccessful() {
156 resourceMetadataDataDefinition.setResourceType(ResourceTypeEnum.PNF);
157 resourceMetadataDataDefinition.setHighestVersion(true);
158 resourceMetadataDataDefinition.setLifecycleState(LifecycleStateEnum.CERTIFIED.name());
159 when(toscaOperationFacade.getToscaFullElement(COMPONENT_ID))
160 .thenReturn(Either.left(resource));
162 UpgradeStatus automatedUpgradeStatus = upgradeBusinessLogic.automatedUpgrade(
163 COMPONENT_ID, getRequests(), user.getUserId());
165 assertEquals("Status should be GENERAL Error ", ActionStatus.GENERAL_ERROR,
166 automatedUpgradeStatus.getStatus());
170 public void testAutomatedUpgrade_givenResourceVersionIsNotHighest_thenReturnsError() {
171 ResponseFormat responseFormat = new ResponseFormat();
172 responseFormat.setStatus(001);
174 resourceMetadataDataDefinition.setHighestVersion(false);
176 when(toscaOperationFacade.getToscaFullElement(COMPONENT_ID))
177 .thenReturn(Either.left(resource));
178 when(componentsUtils.getResponseFormat(eq(ActionStatus.COMPONENT_IS_NOT_HIHGEST_CERTIFIED), any()))
179 .thenReturn(responseFormat);
181 UpgradeStatus status = upgradeBusinessLogic.automatedUpgrade(COMPONENT_ID, null, user.getUserId());
183 assertEquals(responseFormat.getStatus(), status.getError().getStatus());
187 public void testAutomatedUpgrade_givenInvalidResourceId_thenReturnsError() {
189 String invalidResourceId = "invalidResourceId";
190 ResponseFormat stubResponseFormat = new ResponseFormat();
191 stubResponseFormat.setStatus(001);
193 when(toscaOperationFacade.getToscaFullElement(invalidResourceId))
194 .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
195 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR))
196 .thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
198 when(componentsUtils.getResponseFormatByResource(eq(ActionStatus.RESOURCE_NOT_FOUND), anyString()))
199 .thenReturn(stubResponseFormat);
202 UpgradeStatus status = upgradeBusinessLogic.automatedUpgrade(invalidResourceId,
203 null, user.getUserId());
204 assertEquals(stubResponseFormat.getStatus(), status.getError().getStatus());
208 public void testAutomatedUpgrade_givenResourceIsArchived_thenReturnsError() {
209 ResponseFormat responseFormat = new ResponseFormat();
210 responseFormat.setStatus(001);
212 resourceMetadataDataDefinition.setHighestVersion(true);
213 resourceMetadataDataDefinition.setLifecycleState(LifecycleStateEnum.CERTIFIED.name());
214 resourceMetadataDataDefinition.setArchived(true);
216 when(toscaOperationFacade.getToscaFullElement(COMPONENT_ID))
217 .thenReturn(Either.left(resource));
218 when(componentsUtils.getResponseFormat(eq(ActionStatus.COMPONENT_IS_ARCHIVED), any()))
219 .thenReturn(responseFormat);
221 UpgradeStatus status = upgradeBusinessLogic.automatedUpgrade(COMPONENT_ID, null, user.getUserId());
223 assertEquals(responseFormat.getStatus(), status.getError().getStatus());
227 public void testAutomatedUpgrade_givenService_thenReturnsSuccessful() {
228 user.setRole(Role.TESTER.name());
230 List<String> stubInstanceIdsList = new ArrayList<>();
232 List<ComponentInstance> stubComponentInstanceList = new ArrayList<>();
233 ComponentInstance componentInstance = new ComponentInstance();
234 componentInstance.setToscaPresentationValue(JsonPresentationFields.CI_IS_PROXY, Boolean.TRUE);
235 componentInstance.setSourceModelUid("sm1");
236 stubComponentInstanceList.add(componentInstance);
238 List<ComponentInstanceProperty> stubComponentPropertyList = new ArrayList<>();
239 ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
240 stubComponentPropertyList.add(componentInstanceProperty);
242 Map<String, List<ComponentInstanceProperty>> stubComponentInstanceProperties = new HashMap<>();
243 stubComponentInstanceProperties.put("prop1", stubComponentPropertyList);
244 service.setComponentInstancesProperties(stubComponentInstanceProperties);
245 service.setComponentInstances(stubComponentInstanceList);
247 serviceMetadataDataDefinition.setHighestVersion(true);
248 serviceMetadataDataDefinition.setLifecycleState(LifecycleStateEnum.CERTIFIED.name());
249 serviceMetadataDataDefinition.setComponentType(ComponentTypeEnum.SERVICE);
250 serviceMetadataDataDefinition.setUniqueId("sUniqueId1");
251 serviceMetadataDataDefinition.setInvariantUUID("iid");
252 serviceMetadataDataDefinition.setVersion("1.0");
255 ServiceMetadataDataDefinition serviceMetadataDataDefinition2 = new ServiceMetadataDataDefinition();
256 serviceMetadataDataDefinition2.setLifecycleState(LifecycleStateEnum.CERTIFIED.name());
257 serviceMetadataDataDefinition2.setComponentType(ComponentTypeEnum.SERVICE);
258 serviceMetadataDataDefinition2.setUniqueId("sUniqueId2");
259 serviceMetadataDataDefinition2.setInvariantUUID("iid");
260 serviceMetadataDataDefinition2.setVersion("2.0");
262 Service service2 = new Service(new ServiceMetadataDefinition(serviceMetadataDataDefinition2));
264 when(toscaOperationFacade.getToscaFullElement(anyString()))
265 .thenReturn(Either.left(service));
267 when(toscaOperationFacade.getToscaElement(RESOURCE_ID))
268 .thenReturn(Either.left(service));
269 when(toscaOperationFacade.getToscaElement(eq(componentInstance.getSourceModelUid()), any(ComponentParametersView.class)))
270 .thenReturn(Either.left(service2));
272 doReturn(Either.left(service)).when(lifecycleBusinessLogic).changeComponentState(
273 any(ComponentTypeEnum.class), any(), any(User.class),
274 any(LifeCycleTransitionEnum.class), any(LifecycleChangeInfoWithAction.class), anyBoolean(), anyBoolean());
276 when(upgradeOperation.getInstanceIdFromAllottedEdge(any(), any()))
277 .thenReturn(stubInstanceIdsList);
278 when(toscaOperationFacade.updateComponentInstancePropsToComponent(any(Map.class), any()))
279 .thenReturn(Either.left(stubComponentInstanceProperties));
280 when(componentInstanceBusinessLogic.changeInstanceVersion(any(Component.class), any(ComponentInstance.class), any(ComponentInstance.class), any(User.class), any(ComponentTypeEnum.class)))
281 .thenReturn(componentInstance);
283 UpgradeStatus status = upgradeBusinessLogic.automatedUpgrade(COMPONENT_ID, getRequests(), user.getUserId());
284 Assert.assertEquals(ActionStatus.OK, status.getStatus());
288 public void testGetComponentDependencies_givenValidComponentId_thenReturnsSuccessful() {
289 String componentId = "componentId";
290 List<ComponentDependency> stubComponentDependencies
292 stubComponentDependencies.add(new ComponentDependency());
294 when(upgradeOperation.getComponentDependencies(componentId))
295 .thenReturn(Either.left(stubComponentDependencies));
297 assertEquals("should contain one component dependency", 1,
298 upgradeBusinessLogic.getComponentDependencies(componentId, user.getUserId()).left().value().size());
301 private List<UpgradeRequest> getRequests() {
302 List<UpgradeRequest> stubRequests = new ArrayList<>();
303 UpgradeRequest request1 = new UpgradeRequest();
304 request1.setServiceId(SERVICE_ID);
305 request1.setResourceId(RESOURCE_ID);
306 stubRequests.add(request1);