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;
61 import java.util.ArrayList;
62 import java.util.HashMap;
63 import java.util.List;
66 import static org.junit.Assert.assertEquals;
67 import static org.mockito.ArgumentMatchers.any;
68 import static org.mockito.ArgumentMatchers.anyBoolean;
69 import static org.mockito.ArgumentMatchers.anyString;
70 import static org.mockito.ArgumentMatchers.eq;
71 import static org.mockito.Mockito.doReturn;
72 import static org.mockito.Mockito.when;
75 public class UpgradeBusinessLogicTest {
77 private static final String COMPONENT_ID = "componentId";
78 private static final String USER_ID = "admin123";
79 private static final String SERVICE_ID = "service01";
80 private static final String RESOURCE_ID = "resource01";
83 private Resource resource;
84 private Service service;
85 private ResourceMetadataDefinition resourceMetadataDefinition;
86 private ResourceMetadataDataDefinition resourceMetadataDataDefinition;
87 private ServiceMetadataDefinition serviceMetadataDefinition;
88 private ServiceMetadataDataDefinition serviceMetadataDataDefinition;
91 private LifecycleBusinessLogic lifecycleBusinessLogic;
94 private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
97 private UserValidations userValidations;
100 private ToscaOperationFacade toscaOperationFacade;
103 private ComponentsUtils componentsUtils;
106 private UpgradeOperation upgradeOperation;
109 private JanusGraphDao janusGraphDao;
112 private UpgradeBusinessLogic upgradeBusinessLogic;
115 public void setUp() {
116 MockitoAnnotations.initMocks(this);
118 upgradeBusinessLogic = new UpgradeBusinessLogic(lifecycleBusinessLogic, componentInstanceBusinessLogic,
119 userValidations, toscaOperationFacade, componentsUtils, upgradeOperation,
123 user.setRole(Role.ADMIN.name());
124 user.setUserId(USER_ID);
126 resourceMetadataDataDefinition = new ResourceMetadataDataDefinition();
127 resourceMetadataDefinition = new ResourceMetadataDefinition(resourceMetadataDataDefinition);
128 serviceMetadataDataDefinition = new ServiceMetadataDataDefinition();
129 serviceMetadataDefinition = new ServiceMetadataDefinition(serviceMetadataDataDefinition);
131 resource = new Resource(resourceMetadataDefinition);
132 service = new Service(serviceMetadataDefinition);
134 when(userValidations.validateUserExists(eq(user.getUserId()), anyString(), anyBoolean()))
139 public void testAutomatedUpgrade_givenValidResource_returnsSuccessful() {
141 resourceMetadataDataDefinition.setHighestVersion(true);
142 resourceMetadataDataDefinition.setLifecycleState(LifecycleStateEnum.CERTIFIED.name());
143 resourceMetadataDataDefinition.setComponentType(ComponentTypeEnum.RESOURCE);
144 resourceMetadataDataDefinition.setResourceType(ResourceTypeEnum.VF);
146 when(toscaOperationFacade.getToscaFullElement(anyString()))
147 .thenReturn(Either.left(resource));
149 UpgradeStatus automatedUpgradeStatus = upgradeBusinessLogic.automatedUpgrade(COMPONENT_ID, getRequests(), user.getUserId());
150 assertEquals(ActionStatus.OK, automatedUpgradeStatus.getStatus());
154 public void testAutomatedUpgrade_givenResourceIsNotVFType_returnsUnsuccessful() {
155 resourceMetadataDataDefinition.setResourceType(ResourceTypeEnum.PNF);
156 resourceMetadataDataDefinition.setHighestVersion(true);
157 resourceMetadataDataDefinition.setLifecycleState(LifecycleStateEnum.CERTIFIED.name());
158 when(toscaOperationFacade.getToscaFullElement(COMPONENT_ID))
159 .thenReturn(Either.left(resource));
161 UpgradeStatus automatedUpgradeStatus = upgradeBusinessLogic.automatedUpgrade(
162 COMPONENT_ID, getRequests(), user.getUserId());
164 assertEquals("Status should be GENERAL Error ", ActionStatus.GENERAL_ERROR,
165 automatedUpgradeStatus.getStatus());
169 public void testAutomatedUpgrade_givenResourceVersionIsNotHighest_thenReturnsError() {
170 ResponseFormat responseFormat = new ResponseFormat();
171 responseFormat.setStatus(001);
173 resourceMetadataDataDefinition.setHighestVersion(false);
175 when(toscaOperationFacade.getToscaFullElement(COMPONENT_ID))
176 .thenReturn(Either.left(resource));
177 when(componentsUtils.getResponseFormat(eq(ActionStatus.COMPONENT_IS_NOT_HIHGEST_CERTIFIED), any()))
178 .thenReturn(responseFormat);
180 UpgradeStatus status = upgradeBusinessLogic.automatedUpgrade(COMPONENT_ID, null, user.getUserId());
182 assertEquals(responseFormat.getStatus(), status.getError().getStatus());
186 public void testAutomatedUpgrade_givenInvalidResourceId_thenReturnsError() {
188 String invalidResourceId = "invalidResourceId";
189 ResponseFormat stubResponseFormat = new ResponseFormat();
190 stubResponseFormat.setStatus(001);
192 when(toscaOperationFacade.getToscaFullElement(invalidResourceId))
193 .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
194 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR))
195 .thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
197 when(componentsUtils.getResponseFormatByResource(eq(ActionStatus.RESOURCE_NOT_FOUND), anyString()))
198 .thenReturn(stubResponseFormat);
201 UpgradeStatus status = upgradeBusinessLogic.automatedUpgrade(invalidResourceId,
202 null, user.getUserId());
203 assertEquals(stubResponseFormat.getStatus(), status.getError().getStatus());
207 public void testAutomatedUpgrade_givenResourceIsArchived_thenReturnsError() {
208 ResponseFormat responseFormat = new ResponseFormat();
209 responseFormat.setStatus(001);
211 resourceMetadataDataDefinition.setHighestVersion(true);
212 resourceMetadataDataDefinition.setLifecycleState(LifecycleStateEnum.CERTIFIED.name());
213 resourceMetadataDataDefinition.setArchived(true);
215 when(toscaOperationFacade.getToscaFullElement(COMPONENT_ID))
216 .thenReturn(Either.left(resource));
217 when(componentsUtils.getResponseFormat(eq(ActionStatus.COMPONENT_IS_ARCHIVED), any()))
218 .thenReturn(responseFormat);
220 UpgradeStatus status = upgradeBusinessLogic.automatedUpgrade(COMPONENT_ID, null, user.getUserId());
222 assertEquals(responseFormat.getStatus(), status.getError().getStatus());
226 public void testAutomatedUpgrade_givenService_thenReturnsSuccessful() {
227 user.setRole(Role.TESTER.name());
229 List<String> stubInstanceIdsList = new ArrayList<>();
231 List<ComponentInstance> stubComponentInstanceList = new ArrayList<>();
232 ComponentInstance componentInstance = new ComponentInstance();
233 componentInstance.setToscaPresentationValue(JsonPresentationFields.CI_IS_PROXY, Boolean.TRUE);
234 componentInstance.setSourceModelUid("sm1");
235 stubComponentInstanceList.add(componentInstance);
237 List<ComponentInstanceProperty> stubComponentPropertyList = new ArrayList<>();
238 ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
239 stubComponentPropertyList.add(componentInstanceProperty);
241 Map<String, List<ComponentInstanceProperty>> stubComponentInstanceProperties = new HashMap<>();
242 stubComponentInstanceProperties.put("prop1", stubComponentPropertyList);
243 service.setComponentInstancesProperties(stubComponentInstanceProperties);
244 service.setComponentInstances(stubComponentInstanceList);
246 serviceMetadataDataDefinition.setHighestVersion(true);
247 serviceMetadataDataDefinition.setLifecycleState(LifecycleStateEnum.CERTIFIED.name());
248 serviceMetadataDataDefinition.setComponentType(ComponentTypeEnum.SERVICE);
249 serviceMetadataDataDefinition.setUniqueId("sUniqueId1");
250 serviceMetadataDataDefinition.setInvariantUUID("iid");
251 serviceMetadataDataDefinition.setVersion("1.0");
254 ServiceMetadataDataDefinition serviceMetadataDataDefinition2 = new ServiceMetadataDataDefinition();
255 serviceMetadataDataDefinition2.setLifecycleState(LifecycleStateEnum.CERTIFIED.name());
256 serviceMetadataDataDefinition2.setComponentType(ComponentTypeEnum.SERVICE);
257 serviceMetadataDataDefinition2.setUniqueId("sUniqueId2");
258 serviceMetadataDataDefinition2.setInvariantUUID("iid");
259 serviceMetadataDataDefinition2.setVersion("2.0");
261 Service service2 = new Service(new ServiceMetadataDefinition(serviceMetadataDataDefinition2));
263 when(toscaOperationFacade.getToscaFullElement(anyString()))
264 .thenReturn(Either.left(service));
266 when(toscaOperationFacade.getToscaElement(RESOURCE_ID))
267 .thenReturn(Either.left(service));
268 when(toscaOperationFacade.getToscaElement(eq(componentInstance.getSourceModelUid()), any(ComponentParametersView.class)))
269 .thenReturn(Either.left(service2));
271 doReturn(Either.left(service)).when(lifecycleBusinessLogic).changeComponentState(
272 any(ComponentTypeEnum.class), any(), any(User.class),
273 any(LifeCycleTransitionEnum.class), any(LifecycleChangeInfoWithAction.class), anyBoolean(), anyBoolean());
275 when(upgradeOperation.getInstanceIdFromAllottedEdge(any(), any()))
276 .thenReturn(stubInstanceIdsList);
277 when(toscaOperationFacade.updateComponentInstancePropsToComponent(any(Map.class), any()))
278 .thenReturn(Either.left(stubComponentInstanceProperties));
279 when(componentInstanceBusinessLogic.changeInstanceVersion(any(Component.class), any(ComponentInstance.class), any(ComponentInstance.class), any(User.class), any(ComponentTypeEnum.class)))
280 .thenReturn(Either.left(componentInstance));
282 UpgradeStatus status = upgradeBusinessLogic.automatedUpgrade(COMPONENT_ID, getRequests(), user.getUserId());
283 Assert.assertEquals(ActionStatus.OK, status.getStatus());
287 public void testGetComponentDependencies_givenValidComponentId_thenReturnsSuccessful() {
288 String componentId = "componentId";
289 List<ComponentDependency> stubComponentDependencies
291 stubComponentDependencies.add(new ComponentDependency());
293 when(upgradeOperation.getComponentDependencies(componentId))
294 .thenReturn(Either.left(stubComponentDependencies));
296 assertEquals("should contain one component dependency", 1,
297 upgradeBusinessLogic.getComponentDependencies(componentId, user.getUserId()).left().value().size());
300 private List<UpgradeRequest> getRequests() {
301 List<UpgradeRequest> stubRequests = new ArrayList<>();
302 UpgradeRequest request1 = new UpgradeRequest();
303 request1.setServiceId(SERVICE_ID);
304 request1.setResourceId(RESOURCE_ID);
305 stubRequests.add(request1);