2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.components.impl;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertSame;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.ArgumentMatchers.anySet;
31 import static org.mockito.ArgumentMatchers.eq;
32 import static org.mockito.Mockito.when;
34 import fj.data.Either;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.HashSet;
40 import java.util.List;
42 import java.util.Optional;
44 import mockit.Deencapsulation;
45 import org.apache.commons.lang3.tuple.ImmutablePair;
46 import org.assertj.core.util.Lists;
47 import org.junit.Assert;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.mockito.InjectMocks;
52 import org.mockito.Mock;
53 import org.mockito.Mockito;
54 import org.mockito.MockitoAnnotations;
55 import org.mockito.junit.MockitoJUnitRunner;
56 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
57 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
58 import org.openecomp.sdc.be.components.validation.UserValidations;
59 import org.openecomp.sdc.be.config.ConfigurationManager;
60 import org.openecomp.sdc.be.dao.api.ActionStatus;
61 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
62 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
63 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
64 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
65 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
66 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathElementDataDefinition;
67 import org.openecomp.sdc.be.datatypes.elements.GetPolicyValueDataDefinition;
68 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
69 import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition;
70 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
71 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
72 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
73 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
74 import org.openecomp.sdc.be.impl.ComponentsUtils;
75 import org.openecomp.sdc.be.impl.ServletUtils;
76 import org.openecomp.sdc.be.model.ArtifactDefinition;
77 import org.openecomp.sdc.be.model.CapabilityDefinition;
78 import org.openecomp.sdc.be.model.CapabilityRequirementRelationship;
79 import org.openecomp.sdc.be.model.Component;
80 import org.openecomp.sdc.be.model.ComponentInstance;
81 import org.openecomp.sdc.be.model.ComponentInstanceInput;
82 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
83 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
84 import org.openecomp.sdc.be.model.ComponentParametersView;
85 import org.openecomp.sdc.be.model.DataTypeDefinition;
86 import org.openecomp.sdc.be.model.InputDefinition;
87 import org.openecomp.sdc.be.model.LifecycleStateEnum;
88 import org.openecomp.sdc.be.model.PolicyDefinition;
89 import org.openecomp.sdc.be.model.RelationshipImpl;
90 import org.openecomp.sdc.be.model.RelationshipInfo;
91 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
92 import org.openecomp.sdc.be.model.RequirementDefinition;
93 import org.openecomp.sdc.be.model.Resource;
94 import org.openecomp.sdc.be.model.Service;
95 import org.openecomp.sdc.be.model.User;
96 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
97 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ForwardingPathOperation;
98 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
99 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
100 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
101 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
102 import org.openecomp.sdc.be.user.UserBusinessLogic;
103 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
104 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
105 import org.openecomp.sdc.common.api.ConfigurationSource;
106 import org.openecomp.sdc.common.impl.ExternalConfiguration;
107 import org.openecomp.sdc.common.impl.FSConfigurationSource;
108 import org.openecomp.sdc.exception.ResponseFormat;
111 * The test suite designed for test functionality of ComponentInstanceBusinessLogic class
114 @RunWith(MockitoJUnitRunner.class)
115 public class ComponentInstanceBusinessLogicTest {
117 private final static String USER_ID = "jh0003";
118 private final static String COMPONENT_ID = "componentId";
119 private final static String ORIGIN_COMPONENT_ID = "originComponentId";
120 private final static String COMPONENT_INST_ID = "componentInstId";
121 private final static String TO_INSTANCE_ID = "toInstanceId";
122 private final static String TO_INSTANCE_NAME = "toInstanceName";
123 private final static String COMPONENT_INSTANCE_ID = "componentInstanceId";
124 private final static String FROM_INSTANCE_ID = "fromInstanceId";
125 private final static String RELATION_ID = "relationId";
126 private final static String CAPABILITY_OWNER_ID = "capabilityOwnerId";
127 private final static String CAPABILITY_UID = "capabilityUid";
128 private final static String CAPABILITY_NAME = "capabilityName";
129 private final static String REQUIREMENT_OWNER_ID = "requirementOwnerId";
130 private final static String REQUIREMENT_UID = "requirementUid";
131 private final static String REQUIREMENT_NAME = "requirementName";
132 private final static String RELATIONSHIP_TYPE = "relationshipType";
133 private final static String ARTIFACT_1 = "cloudtech_k8s_charts.zip";
134 private final static String ARTIFACT_2 = "cloudtech_azure_day0.zip";
135 private final static String ARTIFACT_3 = "cloudtech_aws_configtemplate.zip";
136 private final static String ARTIFACT_4 = "k8s_charts.zip";
137 private final static String ARTIFACT_5 = "cloudtech_openstack_configtemplate.zip";
138 private final static String PROP_NAME = "propName";
139 private final static String NON_EXIST_NAME = "nonExistName";
141 static ConfigurationSource configurationSource = new FSConfigurationSource(
142 ExternalConfiguration.getChangeListener(),
143 "src/test/resources/config/catalog-be");
144 static ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
147 private static ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
149 private ComponentInstancePropInput componentInstancePropInput;
151 ArtifactsBusinessLogic artifactsBusinessLogic;
153 private ComponentsUtils componentsUtils;
155 private ServletUtils servletUtils;
157 private ResponseFormat responseFormat;
159 private ToscaOperationFacade toscaOperationFacade;
161 private UserBusinessLogic userAdmin;
163 private ForwardingPathOperation forwardingPathOperation;
167 private UserValidations userValidations;
169 GraphLockOperation graphLockOperation;
171 private JanusGraphDao janusGraphDao;
173 ApplicationDataTypeCache dataTypeCache;
175 PropertyOperation propertyOperation;
177 ApplicationDataTypeCache applicationDataTypeCache;
179 private Component service;
180 private Component resource;
181 private ComponentInstance toInstance;
182 private ComponentInstance fromInstance;
183 private CapabilityDataDefinition capability;
184 private RequirementDataDefinition requirement;
185 private RequirementCapabilityRelDef relation;
189 MockitoAnnotations.initMocks(componentInstanceBusinessLogic);
195 public void testGetRelationByIdSuccess() {
196 getServiceRelationByIdSuccess(service);
197 getServiceRelationByIdSuccess(resource);
201 public void testGetRelationByIdUserValidationFailure() {
202 getServiceRelationByIdUserValidationFailure(service);
203 getServiceRelationByIdUserValidationFailure(resource);
207 public void testGetRelationByIdComponentNotFoundFailure() {
208 getRelationByIdComponentNotFoundFailure(service);
209 getRelationByIdComponentNotFoundFailure(resource);
213 public void testForwardingPathOnVersionChange() {
214 getforwardingPathOnVersionChange();
218 public void testIsCloudSpecificArtifact() {
219 assertTrue(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_1));
220 assertTrue(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_2));
221 assertTrue(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_3));
222 assertFalse(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_4));
223 assertFalse(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_5));
226 private void getforwardingPathOnVersionChange() {
227 String containerComponentParam = "services";
228 String containerComponentID = "121-cont";
229 String componentInstanceID = "121-cont-1-comp";
230 Service component = new Service();
231 Map<String, ForwardingPathDataDefinition> forwardingPaths = generateForwardingPath(componentInstanceID);
233 //Add existing componentInstance to component
234 List<ComponentInstance> componentInstanceList = new ArrayList<>();
235 ComponentInstance oldComponentInstance = new ComponentInstance();
236 oldComponentInstance.setName("OLD_COMP_INSTANCE");
237 oldComponentInstance.setUniqueId(componentInstanceID);
238 oldComponentInstance.setName(componentInstanceID);
239 oldComponentInstance.setToscaPresentationValue(JsonPresentationFields.CI_COMPONENT_UID, "1-comp");
240 componentInstanceList.add(oldComponentInstance);
241 component.setComponentInstances(componentInstanceList);
242 component.setForwardingPaths(forwardingPaths);
244 List<ComponentInstance> componentInstanceListNew = new ArrayList<>();
245 ComponentInstance newComponentInstance = new ComponentInstance();
246 String new_Comp_UID = "2-comp";
247 newComponentInstance.setToscaPresentationValue(JsonPresentationFields.CI_COMPONENT_UID, new_Comp_UID);
248 newComponentInstance.setUniqueId(new_Comp_UID);
249 componentInstanceListNew.add(newComponentInstance);
250 Component component2 = new Service();
251 component2.setComponentInstances(componentInstanceListNew);
253 //Mock for getting component
254 when(toscaOperationFacade.getToscaElement(eq(containerComponentID), any(ComponentParametersView.class)))
255 .thenReturn(Either.left(component));
256 when(toscaOperationFacade.validateComponentExists(any(String.class))).thenReturn(Either.left(Boolean.TRUE));
257 when(toscaOperationFacade.getToscaFullElement(eq(new_Comp_UID))).thenReturn(Either.left(component2));
259 Either<Set<String>, ResponseFormat> resultOp = componentInstanceBusinessLogic
260 .forwardingPathOnVersionChange(containerComponentParam,
261 containerComponentID, componentInstanceID,
262 newComponentInstance);
263 assertEquals(1, resultOp.left().value().size());
264 assertEquals("FP-ID-1", resultOp.left().value().iterator().next());
269 public void testCreateOrUpdatePropertiesValues2() {
270 String containerComponentID = "containerId";
271 String resourceInstanceId = "resourceId";
272 String componentInstanceID = "componentInstance";
273 List<ComponentInstanceProperty> properties = new ArrayList<>();
274 ComponentInstanceProperty property = new ComponentInstanceProperty();
275 property.setName("property");
276 property.setValue("newVal");
277 property.setType("string");
278 properties.add(property);
280 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
281 ComponentInstanceProperty origProperty = new ComponentInstanceProperty();
282 origProperty.setName("property");
283 origProperty.setValue("value");
284 origProperty.setType("string");
285 origProperties.add(origProperty);
287 Map<String, DataTypeDefinition> types = new HashMap<>();
288 DataTypeDefinition dataTypeDef = new DataTypeDefinition();
289 types.put("string", dataTypeDef);
291 Component component = new Service();
292 component.setLastUpdaterUserId("userId");
293 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
294 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
295 componentInstanceProps.put("resourceId", origProperties);
296 component.setComponentInstancesProperties(componentInstanceProps);
297 ComponentInstance ci = createComponentInstance("ci1");
298 ci.setUniqueId("resourceId");
299 component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
300 createComponentInstance(componentInstanceID)));
301 HashMap<String, DataTypeDefinition> dataTypeDefinitionHashMap = new HashMap<>();
302 DataTypeDefinition dataTypeDefinition = new DataTypeDefinition();
303 dataTypeDefinition.setName("string");
304 dataTypeDefinitionHashMap.put("string", dataTypeDefinition);
306 //when(userValidations.validateUserExists(user.getUserId(), false)).thenReturn(user);
307 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
308 .thenReturn(Either.left(component));
309 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
310 .thenReturn(StorageOperationStatus.OK);
311 when(dataTypeCache.getAll()).thenReturn(Either.left(types));
312 when(propertyOperation.validateAndUpdatePropertyValue(property.getType(), "newVal", true, null, types))
313 .thenReturn(Either.left("newVal"));
314 when(propertyOperation.validateAndUpdateRules("string", property.getRules(),
315 null, types, true)).thenReturn(ImmutablePair.of("string", null));
316 when(toscaOperationFacade.updateComponentInstanceProperty(component, ci.getUniqueId(),
317 origProperty)).thenReturn(StorageOperationStatus.OK);
318 origProperties.get(0).setValue("newVal");
319 when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(component))
320 .thenReturn(Either.left(component));
321 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
322 when(graphLockOperation.unlockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
323 .thenReturn(StorageOperationStatus.OK);
325 Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = componentInstanceBusinessLogic
326 .createOrUpdatePropertiesValues(
327 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
328 assertThat(responseFormatEither.left().value()).isEqualTo(properties);
332 public void testCreateOrUpdatePropertiesValuesPropertyNotExists() {
333 String containerComponentID = "containerId";
334 String resourceInstanceId = "resourceId";
335 String componentInstanceID = "componentInstance";
336 List<ComponentInstanceProperty> properties = new ArrayList<>();
337 ComponentInstanceProperty property = new ComponentInstanceProperty();
338 property.setName("property");
339 property.setValue("newVal");
340 property.setType("string");
341 properties.add(property);
343 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
345 Map<String, DataTypeDefinition> types = new HashMap<>();
346 DataTypeDefinition dataTypeDef = new DataTypeDefinition();
347 types.put("string", dataTypeDef);
349 Component component = new Service();
350 component.setLastUpdaterUserId("userId");
351 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
352 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
353 componentInstanceProps.put("resourceId", origProperties);
354 component.setComponentInstancesProperties(componentInstanceProps);
355 ComponentInstance ci = createComponentInstance("ci1");
356 ci.setUniqueId("resourceId");
357 component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
358 createComponentInstance(componentInstanceID)));
359 HashMap<String, DataTypeDefinition> dataTypeDefinitionHashMap = new HashMap<>();
360 DataTypeDefinition dataTypeDefinition = new DataTypeDefinition();
361 dataTypeDefinition.setName("string");
362 dataTypeDefinitionHashMap.put("string", dataTypeDefinition);
364 //when(userValidations.validateUserExists(user.getUserId(), false)).thenReturn(user);
365 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
366 .thenReturn(Either.left(component));
367 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
368 .thenReturn(StorageOperationStatus.OK);
369 //when(dataTypeCache.getAll()).thenReturn(Either.left(types));
370 //when (janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
371 when(graphLockOperation.unlockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
372 .thenReturn(StorageOperationStatus.OK);
375 componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
376 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
377 } catch (ComponentException e) {
378 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.PROPERTY_NOT_FOUND);
384 public void testCreateOrUpdatePropertiesValuesValidationFailure() {
385 String containerComponentID = "containerId";
386 String resourceInstanceId = "resourceId";
387 String componentInstanceID = "componentInstance";
388 List<ComponentInstanceProperty> properties = new ArrayList<>();
389 ComponentInstanceProperty property = new ComponentInstanceProperty();
390 property.setName("property");
391 property.setValue("newVal");
392 property.setType("string");
393 properties.add(property);
395 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
396 ComponentInstanceProperty origProperty = new ComponentInstanceProperty();
397 origProperty.setName("property");
398 origProperty.setValue("value");
399 origProperty.setType("string");
400 origProperties.add(origProperty);
402 Map<String, DataTypeDefinition> types = new HashMap<>();
403 DataTypeDefinition dataTypeDef = new DataTypeDefinition();
404 types.put("string", dataTypeDef);
406 Component component = new Service();
407 component.setLastUpdaterUserId("userId");
408 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
409 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
410 componentInstanceProps.put("resourceId", origProperties);
411 component.setComponentInstancesProperties(componentInstanceProps);
412 ComponentInstance ci = createComponentInstance("ci1");
413 ci.setUniqueId("resourceId");
414 component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
415 createComponentInstance(componentInstanceID)));
416 HashMap<String, DataTypeDefinition> dataTypeDefinitionHashMap = new HashMap<>();
417 DataTypeDefinition dataTypeDefinition = new DataTypeDefinition();
418 dataTypeDefinition.setName("string");
419 dataTypeDefinitionHashMap.put("string", dataTypeDefinition);
421 //when(userValidations.validateUserExists(user.getUserId(), false)).thenReturn(user);
422 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
423 .thenReturn(Either.left(component));
424 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
425 .thenReturn(StorageOperationStatus.OK);
426 when(dataTypeCache.getAll()).thenReturn(Either.left(types));
427 when(propertyOperation.validateAndUpdatePropertyValue(property.getType(), "newVal", true, null, types))
428 .thenReturn(Either.right(false));
429 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.BAD_REQUEST))
430 .thenReturn(ActionStatus.INVALID_CONTENT);
433 componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
434 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
435 } catch (ComponentException e) {
436 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.INVALID_CONTENT);
443 public void testCreateOrUpdatePropertiesValuesMissingFieldFailure() {
444 String containerComponentID = "containerId";
445 String resourceInstanceId = "resourceId";
446 String componentInstanceID = "componentInstance";
447 List<ComponentInstanceProperty> properties = new ArrayList<>();
448 ComponentInstanceProperty property = new ComponentInstanceProperty();
449 property.setValue("newVal");
450 property.setType("string");
451 properties.add(property);
453 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
454 ComponentInstanceProperty origProperty = new ComponentInstanceProperty();
455 origProperty.setName("property");
456 origProperty.setValue("value");
457 origProperty.setType("string");
458 origProperties.add(origProperty);
460 Component component = new Service();
461 component.setLastUpdaterUserId("userId");
462 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
463 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
464 componentInstanceProps.put("resourceId", origProperties);
465 component.setComponentInstancesProperties(componentInstanceProps);
466 ComponentInstance ci = createComponentInstance("ci1");
467 ci.setUniqueId("resourceId");
468 component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
469 createComponentInstance(componentInstanceID)));
471 HashMap<String, DataTypeDefinition> dataTypeDefinitionHashMap = new HashMap<>();
472 DataTypeDefinition dataTypeDefinition = new DataTypeDefinition();
473 dataTypeDefinition.setName("string");
474 dataTypeDefinitionHashMap.put("string", dataTypeDefinition);
476 //when(userValidations.validateUserExists(user.getUserId(), false)).thenReturn(user);
477 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
478 .thenReturn(Either.left(component));
479 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
480 .thenReturn(StorageOperationStatus.OK);
483 componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
484 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
485 } catch (ComponentException e) {
486 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.MISSING_PROPERTY_NAME);
491 public void testDeleteForwardingPathsWhenComponentinstanceDeleted() {
493 ComponentTypeEnum containerComponentType = ComponentTypeEnum.findByParamName("services");
494 String containerComponentID = "Service-comp";
495 String componentInstanceID = "NodeA1";
496 Service component = new Service();
498 .setComponentInstances(Arrays.asList(createComponentInstance("NodeA2"), createComponentInstance("NodeB2"),
499 createComponentInstance(componentInstanceID)));
501 component.addForwardingPath(createPath("path1", componentInstanceID, "NodeB1", "1"));
502 component.addForwardingPath(createPath("Path2", "NodeA2", "NodeB2", "2"));
503 when(toscaOperationFacade.getToscaElement(eq(containerComponentID), any(ComponentParametersView.class)))
504 .thenReturn(Either.left(component));
505 when(toscaOperationFacade.getToscaElement(eq(containerComponentID))).thenReturn(Either.left(component));
506 when(forwardingPathOperation.deleteForwardingPath(any(Service.class), anySet()))
507 .thenReturn(Either.left(new HashSet<>()));
508 final ComponentInstance ci = new ComponentInstance();
509 ci.setName(componentInstanceID);
510 ComponentInstance responseFormatEither = componentInstanceBusinessLogic
511 .deleteForwardingPathsRelatedTobeDeletedComponentInstance(
512 containerComponentID, containerComponentType, ci);
513 assertThat(!responseFormatEither.isEmpty()).isEqualTo(true);
518 public void testAddComponentInstanceDeploymentArtifacts() {
520 Component containerComponent = new Service();
521 ComponentInstance componentInstance = new ComponentInstance();
522 componentInstance.setUniqueId(COMPONENT_INSTANCE_ID);
523 Component originComponent = fillOriginComponent(new Resource());
525 Map<String, ArtifactDefinition> artifacts = new HashMap<>();
526 ArtifactDefinition deploymentArtifact1 = getArtifact("deploymentArtifact1", ArtifactTypeEnum.HEAT.getType());
527 artifacts.put(deploymentArtifact1.getArtifactLabel(), deploymentArtifact1);
528 ArtifactDefinition deploymentArtifact2 = getArtifact("deploymentArtifact2",
529 ArtifactTypeEnum.HEAT_ENV.getType());
530 artifacts.put(deploymentArtifact2.getArtifactLabel(), deploymentArtifact2);
531 ArtifactDefinition deploymentArtifact3 = getArtifact("deploymentArtifact3",
532 ArtifactTypeEnum.HEAT_VOL.getType());
533 artifacts.put(deploymentArtifact3.getArtifactLabel(), deploymentArtifact3);
534 ArtifactDefinition heatEnvPlaceHolder = getArtifact("deploymentArtifact4", ArtifactTypeEnum.HEAT_ENV.getType());
535 ArtifactDefinition heatEnvPlaceHolder2 = getArtifact("deploymentArtifact5",
536 ArtifactTypeEnum.HEAT_ENV.getType());
538 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getResourceDeploymentArtifacts = Either
541 Map<String, ArtifactDefinition> finalDeploymentArtifacts = new HashMap<>();
542 finalDeploymentArtifacts.put(deploymentArtifact1.getArtifactLabel(), deploymentArtifact1);
543 finalDeploymentArtifacts.put(deploymentArtifact3.getArtifactLabel(), deploymentArtifact3);
544 finalDeploymentArtifacts.put(heatEnvPlaceHolder.getArtifactLabel(), heatEnvPlaceHolder);
545 finalDeploymentArtifacts.put(heatEnvPlaceHolder2.getArtifactLabel(), heatEnvPlaceHolder2);
547 when(artifactsBusinessLogic.getArtifacts(componentInstance.getComponentUid(), NodeTypeEnum.Resource,
548 ArtifactGroupTypeEnum.DEPLOYMENT, null)).thenReturn(getResourceDeploymentArtifacts);
549 when(artifactsBusinessLogic.createHeatEnvPlaceHolder(new ArrayList<>(),
550 deploymentArtifact1, ArtifactsBusinessLogic.HEAT_ENV_NAME, componentInstance.getUniqueId(),
551 NodeTypeEnum.ResourceInstance, componentInstance.getName(), user, containerComponent,
552 null)).thenReturn(heatEnvPlaceHolder);
553 when(artifactsBusinessLogic.createHeatEnvPlaceHolder(new ArrayList<>(),
554 deploymentArtifact3, ArtifactsBusinessLogic.HEAT_ENV_NAME, componentInstance.getUniqueId(),
555 NodeTypeEnum.ResourceInstance, componentInstance.getName(), user, containerComponent,
556 null)).thenReturn(heatEnvPlaceHolder2);
558 componentInstanceBusinessLogic.setToscaOperationFacade(toscaOperationFacade);
559 when(toscaOperationFacade.addDeploymentArtifactsToInstance(containerComponent.getUniqueId(), componentInstance,
560 finalDeploymentArtifacts)).thenReturn(StorageOperationStatus.OK);
561 when(toscaOperationFacade
562 .addGroupInstancesToComponentInstance(containerComponent, componentInstance, new ArrayList<>(), new HashMap<>()))
563 .thenReturn(StorageOperationStatus.OK);
564 when(toscaOperationFacade
565 .addInformationalArtifactsToInstance(containerComponent.getUniqueId(), componentInstance, null))
566 .thenReturn(StorageOperationStatus.OK);
568 ActionStatus status = componentInstanceBusinessLogic.addComponentInstanceArtifacts(containerComponent,
569 componentInstance, originComponent, user, null);
571 assertThat(status).isEqualTo(ActionStatus.OK);
575 private Component fillOriginComponent(Resource originComponent) {
576 originComponent.setUniqueId("resourceId");
577 originComponent.setUniqueId(ORIGIN_COMPONENT_ID);
578 originComponent.setComponentInstances(Lists.newArrayList(toInstance, fromInstance));
579 originComponent.setComponentType(ComponentTypeEnum.RESOURCE);
580 originComponent.setState(LifecycleStateEnum.CERTIFIED);
581 return originComponent;
584 private ArtifactDefinition getArtifact(String artifactLabel, String artifactType) {
585 ArtifactDefinition artifactDefinition = new ArtifactDefinition();
586 artifactDefinition.setArtifactLabel(artifactLabel);
587 artifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
588 artifactDefinition.setEsId("esId" + artifactLabel);
589 artifactDefinition.setArtifactType(artifactType);
590 artifactDefinition.setArtifactName("artifactName");
591 return artifactDefinition;
594 private ComponentInstance createComponentInstance(String path1) {
595 ComponentInstance componentInstance = new ComponentInstance();
596 componentInstance.setName(path1);
597 return componentInstance;
600 private ForwardingPathDataDefinition createPath(String pathName, String fromNode, String toNode, String uniqueId) {
601 ForwardingPathDataDefinition forwardingPath = new ForwardingPathDataDefinition(pathName);
602 forwardingPath.setProtocol("protocol");
603 forwardingPath.setDestinationPortNumber("port");
604 forwardingPath.setUniqueId(uniqueId);
605 ListDataDefinition<ForwardingPathElementDataDefinition> forwardingPathElementListDataDefinition =
606 new ListDataDefinition<>();
607 forwardingPathElementListDataDefinition
608 .add(new ForwardingPathElementDataDefinition(fromNode, toNode, "nodeAcpType", "nodeBcpType",
609 "nodeDcpName", "nodeBcpName"));
610 forwardingPath.setPathElements(forwardingPathElementListDataDefinition);
612 return forwardingPath;
615 private Map<String, ForwardingPathDataDefinition> generateForwardingPath(String componentInstanceID) {
616 ForwardingPathDataDefinition forwardingPath = new ForwardingPathDataDefinition("fpName");
617 String protocol = "protocol";
618 forwardingPath.setProtocol(protocol);
619 forwardingPath.setDestinationPortNumber("DestinationPortNumber");
620 forwardingPath.setUniqueId("FP-ID-1");
621 ListDataDefinition<ForwardingPathElementDataDefinition> forwardingPathElementListDataDefinition =
622 new ListDataDefinition<>();
623 forwardingPathElementListDataDefinition
624 .add(new ForwardingPathElementDataDefinition(componentInstanceID, "nodeB", "nodeA_FORWARDER_CAPABILITY",
625 "nodeBcpType", "nodeDcpName", "nodeBcpName"));
626 forwardingPath.setPathElements(forwardingPathElementListDataDefinition);
627 Map<String, ForwardingPathDataDefinition> forwardingPaths = new HashMap<>();
628 forwardingPaths.put("1122", forwardingPath);
629 return forwardingPaths;
632 @SuppressWarnings("unchecked")
633 private void getServiceRelationByIdSuccess(Component component) {
634 Either<Component, StorageOperationStatus> getComponentRes = Either.left(component);
635 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
636 .thenReturn(getComponentRes);
637 Either<RequirementCapabilityRelDef, ResponseFormat> response = componentInstanceBusinessLogic
638 .getRelationById(COMPONENT_ID,
639 RELATION_ID, USER_ID,
640 component.getComponentType());
641 assertTrue(response.isLeft());
644 private void getServiceRelationByIdUserValidationFailure(Component component) {
645 when(userValidations.validateUserExists(eq(USER_ID)))
646 .thenThrow(new ByActionStatusComponentException(ActionStatus.USER_NOT_FOUND));
648 componentInstanceBusinessLogic
649 .getRelationById(COMPONENT_ID, RELATION_ID, USER_ID, component.getComponentType());
650 } catch (ByActionStatusComponentException e) {
651 assertSame(e.getActionStatus(), ActionStatus.USER_NOT_FOUND);
655 private void getRelationByIdComponentNotFoundFailure(Component component) {
656 Either<User, ActionStatus> eitherCreator = Either.left(user);
657 Either<Component, StorageOperationStatus> getComponentRes = Either.right(StorageOperationStatus.NOT_FOUND);
658 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
659 .thenReturn(getComponentRes);
661 Either<RequirementCapabilityRelDef, ResponseFormat> response = componentInstanceBusinessLogic
662 .getRelationById(COMPONENT_ID,
663 RELATION_ID, USER_ID,
664 component.getComponentType());
665 assertTrue(response.isRight());
668 private void stubMethods() {
669 when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(user);
671 .convertFromStorageResponse(eq(StorageOperationStatus.GENERAL_ERROR), any(ComponentTypeEnum.class)))
672 .thenReturn(ActionStatus.GENERAL_ERROR);
675 private void createComponents() {
682 private void createResource() {
683 resource = new Resource();
684 resource.setUniqueId(COMPONENT_ID);
685 resource.setComponentInstancesRelations(Lists.newArrayList(relation));
686 resource.setComponentInstances(Lists.newArrayList(toInstance, fromInstance));
687 resource.setCapabilities(toInstance.getCapabilities());
688 resource.setRequirements(fromInstance.getRequirements());
689 resource.setComponentType(ComponentTypeEnum.RESOURCE);
690 resource.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
693 private void createService() {
694 service = new Service();
695 service.setUniqueId(COMPONENT_ID);
696 service.setComponentInstancesRelations(Lists.newArrayList(relation));
697 service.setComponentInstances(Lists.newArrayList(toInstance, fromInstance));
698 service.setCapabilities(toInstance.getCapabilities());
699 service.setRequirements(fromInstance.getRequirements());
700 service.setComponentType(ComponentTypeEnum.SERVICE);
701 service.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
704 private void createInstances() {
705 toInstance = new ComponentInstance();
706 toInstance.setUniqueId(TO_INSTANCE_ID);
707 toInstance.setName(TO_INSTANCE_NAME);
709 fromInstance = new ComponentInstance();
710 fromInstance.setUniqueId(FROM_INSTANCE_ID);
712 capability = new CapabilityDataDefinition();
713 capability.setOwnerId(CAPABILITY_OWNER_ID);
714 capability.setUniqueId(CAPABILITY_UID);
715 capability.setName(CAPABILITY_NAME);
717 Map<String, List<CapabilityDefinition>> capabilities = new HashMap<>();
718 final CapabilityDefinition capabilityDefinition = new CapabilityDefinition(capability);
719 final ArrayList<ComponentInstanceProperty> properties = new ArrayList<>();
720 properties.add(componentInstancePropInput);
721 capabilityDefinition.setProperties(properties);
722 capabilities.put(capability.getName(), Lists.newArrayList(capabilityDefinition));
724 requirement = new RequirementDataDefinition();
725 requirement.setOwnerId(REQUIREMENT_OWNER_ID);
726 requirement.setUniqueId(REQUIREMENT_UID);
727 requirement.setName(REQUIREMENT_NAME);
728 requirement.setRelationship(RELATIONSHIP_TYPE);
730 Map<String, List<RequirementDefinition>> requirements = new HashMap<>();
731 requirements.put(requirement.getCapability(), Lists.newArrayList(new RequirementDefinition(requirement)));
733 toInstance.setCapabilities(capabilities);
734 fromInstance.setRequirements(requirements);
737 private void createRelation() {
739 relation = new RequirementCapabilityRelDef();
740 CapabilityRequirementRelationship relationship = new CapabilityRequirementRelationship();
741 RelationshipInfo relationInfo = new RelationshipInfo();
742 relationInfo.setId(RELATION_ID);
743 relationship.setRelation(relationInfo);
745 relation.setRelationships(Lists.newArrayList(relationship));
746 relation.setToNode(TO_INSTANCE_ID);
747 relation.setFromNode(FROM_INSTANCE_ID);
749 relationInfo.setCapabilityOwnerId(CAPABILITY_OWNER_ID);
750 relationInfo.setCapabilityUid(CAPABILITY_UID);
751 relationInfo.setCapability(CAPABILITY_NAME);
752 relationInfo.setRequirementOwnerId(REQUIREMENT_OWNER_ID);
753 relationInfo.setRequirementUid(REQUIREMENT_UID);
754 relationInfo.setRequirement(REQUIREMENT_NAME);
755 RelationshipImpl relationshipImpl = new RelationshipImpl();
756 relationshipImpl.setType(RELATIONSHIP_TYPE);
757 relationInfo.setRelationships(relationshipImpl);
760 private ComponentInstanceBusinessLogic createTestSubject() {
761 return componentInstanceBusinessLogic;
765 public void testChangeServiceProxyVersion() {
766 ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
768 Either<ComponentInstance, ResponseFormat> result;
771 componentInstanceBusinessLogic = createTestSubject();
772 result = componentInstanceBusinessLogic.changeServiceProxyVersion();
773 Assert.assertNotNull(result);
777 public void testCreateServiceProxy() {
778 ComponentInstanceBusinessLogic testSubject;
779 Either<ComponentInstance, ResponseFormat> result;
782 testSubject = createTestSubject();
783 result = testSubject.createServiceProxy();
784 Assert.assertNotNull(result);
788 public void testDeleteServiceProxy() {
789 ComponentInstanceBusinessLogic testSubject;
791 Either<ComponentInstance, ResponseFormat> result;
794 testSubject = createTestSubject();
795 result = testSubject.deleteServiceProxy();
796 Assert.assertNotNull(result);
800 public void testGetComponentInstanceInputsByInputId() {
801 ComponentInstanceBusinessLogic testSubject;
802 Component component = new Service();
804 List<ComponentInstanceInput> result;
807 testSubject = createTestSubject();
808 result = testSubject.getComponentInstanceInputsByInputId(component, inputId);
809 Assert.assertNotNull(result);
813 public void testGetComponentInstancePropertiesByInputId() {
814 ComponentInstanceBusinessLogic testSubject;
815 Component component = new Service();
817 List<ComponentInstanceProperty> result;
820 testSubject = createTestSubject();
821 result = testSubject.getComponentInstancePropertiesByInputId(component, inputId);
822 Assert.assertNotNull(result);
826 public void testGetRelationById() {
827 ComponentInstanceBusinessLogic testSubject;
828 String componentId = "";
829 String relationId = "";
830 String userId = user.getUserId();
831 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE_INSTANCE;
832 Either<RequirementCapabilityRelDef, ResponseFormat> result;
835 testSubject = createTestSubject();
836 result = testSubject.getRelationById(componentId, relationId, userId, componentTypeEnum);
837 Assert.assertNotNull(result);
841 public void testValidateParent() {
842 ComponentInstanceBusinessLogic testSubject;
844 String nodeTemplateId = "";
848 testSubject = createTestSubject();
849 result = Deencapsulation.invoke(testSubject, "validateParent", new Object[]{resource, nodeTemplateId});
850 Assert.assertNotNull(result);
854 public void testGetComponentType() {
855 ComponentInstanceBusinessLogic testSubject;
856 ComponentTypeEnum result;
859 testSubject = createTestSubject();
860 result = Deencapsulation.invoke(testSubject, "getComponentType", new Object[]{ComponentTypeEnum.class});
861 Assert.assertNotNull(result);
865 public void testGetNewGroupName() {
866 ComponentInstanceBusinessLogic testSubject;
867 String oldPrefix = "";
868 String newNormailzedPrefix = "";
869 String qualifiedGroupInstanceName = "";
873 testSubject = createTestSubject();
874 result = Deencapsulation.invoke(testSubject, "getNewGroupName",
875 new Object[]{oldPrefix, newNormailzedPrefix, qualifiedGroupInstanceName});
876 Assert.assertNotNull(result);
880 public void testUpdateComponentInstanceMetadata_3() {
881 ComponentInstanceBusinessLogic testSubject;
883 ComponentInstance newComponentInstance = null;
884 ComponentInstance result;
887 testSubject = createTestSubject();
888 result = Deencapsulation
889 .invoke(testSubject, "updateComponentInstanceMetadata", new Object[]{toInstance, toInstance});
890 Assert.assertNotNull(result);
894 public void testFindRelation() throws Exception {
895 ComponentInstanceBusinessLogic testSubject;
896 String relationId = "";
897 List<RequirementCapabilityRelDef> requirementCapabilityRelations = new ArrayList<>();
898 RequirementCapabilityRelDef result;
901 testSubject = createTestSubject();
902 result = Deencapsulation.invoke(testSubject, "findRelation",
903 new Object[]{relationId, requirementCapabilityRelations});
907 public void testCreateOrUpdatePropertiesValues() throws Exception {
908 ComponentInstanceBusinessLogic testSubject;
909 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
911 String componentId = resource.getUniqueId();
912 String resourceInstanceId = "";
913 List<ComponentInstanceProperty> properties = new ArrayList<>();
914 String userId = user.getUserId();
915 Either<List<ComponentInstanceProperty>, ResponseFormat> result;
917 when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll))
918 .thenReturn(Either.left(resource));
921 testSubject = createTestSubject();
923 .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
925 Assert.assertNotNull(result);
927 componentTypeEnum = null;
929 .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
931 Assert.assertNotNull(result);
934 .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
936 Assert.assertNotNull(result);
940 public void testUpdateCapabilityPropertyOnContainerComponent() throws Exception {
941 ComponentInstanceBusinessLogic testSubject;
942 ComponentInstanceProperty property = new ComponentInstanceProperty();
943 String newValue = "";
946 String capabilityType = "";
947 String capabilityName = "";
948 ResponseFormat result;
951 testSubject = createTestSubject();
952 result = Deencapsulation.invoke(testSubject, "updateCapabilityPropertyOnContainerComponent",
953 new Object[]{property, newValue, resource, toInstance, capabilityType, capabilityName});
957 public void testCreateOrUpdateInstanceInputValues() throws Exception {
958 ComponentInstanceBusinessLogic testSubject;
959 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
961 String componentId = resource.getUniqueId();
962 String resourceInstanceId = "";
963 List<ComponentInstanceInput> inputs = new ArrayList<>();
964 String userId = user.getUserId();
965 Either<List<ComponentInstanceInput>, ResponseFormat> result;
967 when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll))
968 .thenReturn(Either.left(resource));
971 testSubject = createTestSubject();
973 .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
975 Assert.assertNotNull(result);
976 componentTypeEnum = null;
978 .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
980 Assert.assertNotNull(result);
983 .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
985 Assert.assertNotNull(result);
989 public void testCreateOrUpdateGroupInstancePropertyValue() throws Exception {
990 ComponentInstanceBusinessLogic testSubject;
991 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
993 String componentId = resource.getUniqueId();
994 String resourceInstanceId = "";
995 String groupInstanceId = "";
996 ComponentInstanceProperty property = new ComponentInstanceProperty();
997 String userId = user.getUserId();
998 Either<ComponentInstanceProperty, ResponseFormat> result;
1000 when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseMetadata))
1001 .thenReturn(Either.left(resource));
1004 testSubject = createTestSubject();
1005 result = testSubject
1006 .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1007 groupInstanceId, property, userId);
1008 Assert.assertNotNull(result);
1009 componentTypeEnum = null;
1010 result = testSubject
1011 .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1012 groupInstanceId, property, userId);
1013 Assert.assertNotNull(result);
1015 result = testSubject
1016 .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1017 groupInstanceId, property, userId);
1018 Assert.assertNotNull(result);
1022 public void testDeletePropertyValue() throws Exception {
1023 ComponentInstanceBusinessLogic testSubject;
1024 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1026 String serviceId = service.getUniqueId();
1027 String resourceInstanceId = "";
1028 String propertyValueId = "";
1029 String userId = user.getUserId();
1030 Either<ComponentInstanceProperty, ResponseFormat> result;
1032 when(toscaOperationFacade.getToscaElement(serviceId, JsonParseFlagEnum.ParseMetadata))
1033 .thenReturn(Either.left(service));
1036 testSubject = createTestSubject();
1037 result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1039 Assert.assertNotNull(result);
1040 componentTypeEnum = null;
1041 result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1043 Assert.assertNotNull(result);
1045 result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1047 Assert.assertNotNull(result);
1051 public void testGetComponentParametersViewForForwardingPath() throws Exception {
1052 ComponentInstanceBusinessLogic testSubject;
1053 ComponentParametersView result;
1056 testSubject = createTestSubject();
1057 result = Deencapsulation.invoke(testSubject, "getComponentParametersViewForForwardingPath");
1058 Assert.assertNotNull(result);
1062 public void testGetResourceInstanceById() throws Exception {
1063 ComponentInstanceBusinessLogic testSubject;
1065 String instanceId = "";
1066 Either<ComponentInstance, StorageOperationStatus> result;
1069 testSubject = createTestSubject();
1070 result = Deencapsulation.invoke(testSubject, "getResourceInstanceById", new Object[]{resource, instanceId});
1071 Assert.assertNotNull(result);
1075 public void testUpdateInstanceCapabilityProperties_1() throws Exception {
1076 ComponentInstanceBusinessLogic testSubject;
1077 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1079 String containerComponentId = resource.getUniqueId();
1080 String componentInstanceUniqueId = "";
1081 String capabilityType = "";
1082 String capabilityName = "";
1083 List<ComponentInstanceProperty> properties = new ArrayList<>();
1084 String userId = user.getUserId();
1085 Either<List<ComponentInstanceProperty>, ResponseFormat> result;
1087 when(toscaOperationFacade.getToscaFullElement(containerComponentId))
1088 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
1090 testSubject = createTestSubject();
1091 result = testSubject.updateInstanceCapabilityProperties(componentTypeEnum, containerComponentId,
1092 componentInstanceUniqueId, capabilityType, capabilityName, properties, userId);
1093 Assert.assertNotNull(result);
1094 when(toscaOperationFacade.getToscaFullElement(containerComponentId)).thenReturn(Either.left(resource));
1095 result = testSubject.updateInstanceCapabilityProperties(componentTypeEnum, containerComponentId,
1096 componentInstanceUniqueId, capabilityType, capabilityName, properties, userId);
1097 Assert.assertNotNull(result);
1101 public void testCopyComponentInstanceWrongUserId() {
1103 Either<Map<String, ComponentInstance>, ResponseFormat> result;
1104 ComponentInstance inputComponentInstance = createComponetInstanceFromComponent(resource);
1105 String containerComponentId = service.getUniqueId();
1106 String componentInstanceId = resource.getUniqueId();
1107 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1108 service.setLastUpdaterUserId("wrong user id");
1110 Either<Component, StorageOperationStatus> leftServiceOp = Either.left(service);
1111 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(leftServiceOp);
1112 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1113 .thenReturn(leftServiceOp);
1114 when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
1115 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1116 .thenReturn(StorageOperationStatus.OK);
1117 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1118 .thenReturn(StorageOperationStatus.OK);
1120 result = componentInstanceBusinessLogic
1121 .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId,
1123 Assert.assertNotNull(result);
1125 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1126 assertThat(result.isRight());
1130 public void testCopyComponentInstanceComponentWrongState() {
1131 Either<Map<String, ComponentInstance>, ResponseFormat> result;
1132 ComponentInstance inputComponentInstance = createComponetInstanceFromComponent(resource);
1133 String containerComponentId = service.getUniqueId();
1134 String componentInstanceId = resource.getUniqueId();
1135 String oldServiceLastUpdatedUserId = service.getLastUpdaterUserId();
1136 service.setLastUpdaterUserId(USER_ID);
1138 Either<Component, StorageOperationStatus> leftServiceOp = Either.left(service);
1139 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(leftServiceOp);
1140 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1141 .thenReturn(leftServiceOp);
1142 when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
1143 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1144 .thenReturn(StorageOperationStatus.OK);
1145 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1146 .thenReturn(StorageOperationStatus.OK);
1147 Either<Component, StorageOperationStatus> getComponentRes = Either.left(resource);
1148 result = componentInstanceBusinessLogic
1149 .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId, USER_ID);
1150 Assert.assertNotNull(result);
1151 service.setLastUpdaterUserId(oldServiceLastUpdatedUserId);
1152 assertThat(result.isRight());
1156 public void testCopyComponentInstance() {
1157 Either<Map<String, ComponentInstance>, ResponseFormat> result;
1158 ComponentInstance inputComponentInstance = createComponetInstanceFromComponent(resource);
1159 String containerComponentId = service.getUniqueId();
1160 String componentInstanceId = resource.getUniqueId();
1161 String oldServiceLastUpdatedUserId = service.getLastUpdaterUserId();
1162 service.setLastUpdaterUserId(USER_ID);
1163 LifecycleStateEnum oldResourceLifeCycle = resource.getLifecycleState();
1164 resource.setLifecycleState(LifecycleStateEnum.CERTIFIED);
1166 Either<Component, StorageOperationStatus> leftServiceOp = Either.left(service);
1167 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(leftServiceOp);
1168 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1169 .thenReturn(leftServiceOp);
1170 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1171 .thenReturn(StorageOperationStatus.OK);
1172 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1173 .thenReturn(StorageOperationStatus.OK);
1174 Either<Component, StorageOperationStatus> getComponentRes = Either.left(resource);
1175 ImmutablePair<Component, String> pair = new ImmutablePair<>(resource, TO_INSTANCE_ID);
1176 Either<ImmutablePair<Component, String>, StorageOperationStatus> result2 = Either.left(pair);
1177 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getResourceDeploymentArtifacts = Either
1178 .left(new HashMap<String, ArtifactDefinition>());
1179 StorageOperationStatus artStatus = StorageOperationStatus.OK;
1181 result = componentInstanceBusinessLogic
1182 .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId,
1184 Assert.assertNotNull(result);
1186 service.setLastUpdaterUserId(oldServiceLastUpdatedUserId);
1187 resource.setLifecycleState(oldResourceLifeCycle);
1189 assertThat(result.isLeft());
1193 public void testCreateOrUpdateAttributeValueForCopyPaste() {
1194 ComponentInstance serviceComponentInstance = createComponetInstanceFromComponent(service);
1195 ComponentInstanceProperty attribute = new ComponentInstanceProperty();
1196 attribute.setType("string");
1197 attribute.setUniqueId("testCreateOrUpdateAttributeValueForCopyPaste");
1198 SchemaDefinition def = Mockito.mock(SchemaDefinition.class);
1199 attribute.setSchema(def);
1200 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1201 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1202 service.setLastUpdaterUserId(USER_ID);
1203 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1205 Map<String, List<ComponentInstanceProperty>> instAttrsMap =
1206 new HashMap<String, List<ComponentInstanceProperty>>();
1207 List<ComponentInstanceProperty> instAttrsList = new ArrayList<ComponentInstanceProperty>();
1208 ComponentInstanceProperty prop = new ComponentInstanceProperty();
1209 prop.setUniqueId(attribute.getUniqueId());
1210 instAttrsList.add(prop);
1211 instAttrsMap.put(toInstance.getUniqueId(), instAttrsList);
1212 service.setComponentInstancesAttributes(instAttrsMap);
1214 Either<Component, StorageOperationStatus> serviceEitherLeft = Either.left(service);
1215 when(toscaOperationFacade.getToscaElement(serviceComponentInstance.getUniqueId(), JsonParseFlagEnum.ParseAll))
1216 .thenReturn(serviceEitherLeft);
1217 when(toscaOperationFacade.updateComponentInstanceAttribute(service, toInstance.getUniqueId(), attribute))
1218 .thenReturn(StorageOperationStatus.OK);
1219 when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(service))
1220 .thenReturn(serviceEitherLeft);
1222 Either<ComponentInstanceProperty, ResponseFormat> result = Deencapsulation
1223 .invoke(componentInstanceBusinessLogic,
1224 "createOrUpdateAttributeValueForCopyPaste",
1225 ComponentTypeEnum.SERVICE,
1226 serviceComponentInstance
1228 toInstance.getUniqueId(), attribute,
1230 Assert.assertNotNull(result);
1232 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1233 service.setLifecycleState(oldLifeCycleState);
1235 assertTrue(result.isLeft());
1236 ComponentInstanceProperty resultProp = result.left().value();
1237 assertEquals(resultProp.getPath().size(), 1);
1238 assertEquals(resultProp.getPath().get(0), toInstance.getUniqueId());
1242 public void testUpdateComponentInstanceProperty() {
1244 String containerComponentId = service.getUniqueId();
1245 String componentInstanceId = "dummy_id";
1246 ComponentInstanceProperty property = Mockito.mock(ComponentInstanceProperty.class);
1248 Either<Component, StorageOperationStatus> getComponent = Either.left(service);
1249 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(getComponent);
1250 StorageOperationStatus status = StorageOperationStatus.OK;
1251 when(toscaOperationFacade.updateComponentInstanceProperty(service, componentInstanceId, property))
1252 .thenReturn(status);
1253 Either<Component, StorageOperationStatus> updateContainerRes = Either.left(service);
1254 when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(service))
1255 .thenReturn(updateContainerRes);
1257 Either<String, ResponseFormat> result = Deencapsulation.invoke(componentInstanceBusinessLogic,
1258 "updateComponentInstanceProperty", containerComponentId, componentInstanceId, property);
1259 Assert.assertNotNull(result);
1260 assertTrue(result.isLeft());
1264 public void testGetInputListDefaultValue() {
1265 Component component = service;
1266 String inputId = "dummy_id";
1267 String defaultValue = "dummy_default_value";
1268 List<InputDefinition> newInputs = new ArrayList<InputDefinition>();
1269 InputDefinition in = new InputDefinition();
1270 in.setUniqueId(inputId);
1271 in.setDefaultValue(defaultValue);
1273 List<InputDefinition> oldInputs = service.getInputs();
1274 service.setInputs(newInputs);
1276 Either<String, ResponseFormat> result =
1277 Deencapsulation.invoke(componentInstanceBusinessLogic, "getInputListDefaultValue", component, inputId);
1279 service.setInputs(oldInputs);
1281 assertEquals(result.left().value(), defaultValue);
1285 public void testBatchDeleteComponentInstanceFailureWrongType() {
1286 Map<String, List<String>> result;
1287 List<String> componentInstanceIdList = new ArrayList<>();
1288 String containerComponentParam = "WRONG_TYPE";
1289 String containerComponentId = "containerComponentId";
1290 String componentInstanceId = "componentInstanceId";
1291 componentInstanceIdList.add(componentInstanceId);
1292 String userId = USER_ID;
1293 Map<String, List<String>> deleteErrorMap = new HashMap<>();
1294 List<String> deleteErrorIds = new ArrayList<>();
1295 deleteErrorIds.add(componentInstanceId);
1296 deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
1297 Either<Component, StorageOperationStatus> cont = Either.left(service);
1298 when(componentsUtils.convertFromStorageResponse(eq(StorageOperationStatus.NOT_FOUND), eq(null)))
1299 .thenReturn(ActionStatus.GENERAL_ERROR);
1300 when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class)))
1304 result = componentInstanceBusinessLogic
1305 .batchDeleteComponentInstance(containerComponentParam, containerComponentId, componentInstanceIdList,
1307 Assert.assertNotNull(result);
1308 assertEquals(deleteErrorMap, result);
1309 } catch (ComponentException e) {
1310 assertEquals(e.getActionStatus().toString(), StorageOperationStatus.GENERAL_ERROR.toString());
1315 public void testBatchDeleteComponentInstanceFailureCompIds() {
1316 Map<String, List<String>> result = new HashMap<>();
1317 String containerComponentParam = ComponentTypeEnum.SERVICE_PARAM_NAME;
1318 String containerComponentId = "containerComponentId";
1319 String componentInstanceId = "componentInstanceId";
1320 List<String> componentInstanceIdList = new ArrayList<>();
1321 componentInstanceIdList.add(componentInstanceId);
1322 String userId = USER_ID;
1323 Map<String, List<String>> deleteErrorMap = new HashMap<>();
1324 List<String> deleteErrorIds = new ArrayList<>();
1325 deleteErrorIds.add(componentInstanceId);
1326 deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
1328 Either<Component, StorageOperationStatus> err = Either.right(StorageOperationStatus.GENERAL_ERROR);
1329 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1333 result = componentInstanceBusinessLogic
1334 .batchDeleteComponentInstance(containerComponentParam, containerComponentId, componentInstanceIdList,
1336 Assert.assertNotNull(result);
1337 assertEquals(deleteErrorMap, result);
1338 } catch (ComponentException e) {
1339 assertEquals(e.getActionStatus().toString(), StorageOperationStatus.GENERAL_ERROR.toString());
1344 public void testBatchDeleteComponentInstanceSuccess() {
1345 Map<String, List<String>> result;
1346 String containerComponentParam = ComponentTypeEnum.SERVICE_PARAM_NAME;
1347 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1348 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1349 service.setLastUpdaterUserId(USER_ID);
1350 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1351 String containerComponentId = service.getUniqueId();
1352 String componentInstanceId = TO_INSTANCE_ID;
1353 String userId = USER_ID;
1354 List<String> componentInstanceIdList = new ArrayList<>();
1355 componentInstanceIdList.add(componentInstanceId);
1356 Map<String, List<String>> deleteErrorMap = new HashMap<>();
1357 List<String> deleteErrorIds = new ArrayList<>();
1358 deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
1360 Either<Component, StorageOperationStatus> cont = Either.left(service);
1361 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1362 .thenReturn(StorageOperationStatus.OK);
1363 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1364 .thenReturn(StorageOperationStatus.OK);
1365 ImmutablePair<Component, String> pair = new ImmutablePair<>(resource, TO_INSTANCE_ID);
1366 Either<ImmutablePair<Component, String>, StorageOperationStatus> result2 = Either.left(pair);
1367 when(toscaOperationFacade.deleteComponentInstanceFromTopologyTemplate(service, componentInstanceId))
1368 .thenReturn(result2);
1369 when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
1371 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
1373 result = componentInstanceBusinessLogic
1374 .batchDeleteComponentInstance(containerComponentParam, containerComponentId,
1375 componentInstanceIdList, userId);
1376 Assert.assertNotNull(result);
1378 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1379 service.setLifecycleState(oldLifeCycleState);
1380 assertEquals(deleteErrorMap, result);
1384 public void testDissociateRIFromRIFailDissociate() {
1386 List<RequirementCapabilityRelDef> result;
1387 RequirementCapabilityRelDef ref = new RequirementCapabilityRelDef();
1388 ref.setFromNode(FROM_INSTANCE_ID);
1389 ref.setToNode(TO_INSTANCE_ID);
1390 List<CapabilityRequirementRelationship> relationships = new ArrayList<>();
1391 CapabilityRequirementRelationship relationship = new CapabilityRequirementRelationship();
1392 RelationshipInfo ri = new RelationshipInfo();
1393 ri.setRequirement(REQUIREMENT_NAME);
1394 relationship.setRelation(ri);
1395 relationships.add(relationship);
1396 ref.setRelationships(relationships);
1397 List<RequirementCapabilityRelDef> requirementDefList = new ArrayList<>();
1398 requirementDefList.add(ref);
1399 ComponentTypeEnum componentTypeEnum = service.getComponentType();
1400 String componentId = service.getUniqueId();
1401 String userId = USER_ID;
1402 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1403 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1404 service.setLastUpdaterUserId(USER_ID);
1405 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1407 Either<Component, StorageOperationStatus> cont = Either.left(service);
1408 when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
1410 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1411 .thenReturn(StorageOperationStatus.OK);
1412 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1413 .thenReturn(StorageOperationStatus.OK);
1414 Either<RequirementCapabilityRelDef, StorageOperationStatus> resultEither;
1415 resultEither = Either.right(StorageOperationStatus.OK);
1416 when(componentsUtils.convertFromStorageResponseForResourceInstance(eq(StorageOperationStatus.OK), eq(true)))
1417 .thenReturn(ActionStatus.GENERAL_ERROR);
1418 when(toscaOperationFacade.dissociateResourceInstances(componentId, ref)).thenReturn(resultEither);
1421 result = componentInstanceBusinessLogic
1422 .batchDissociateRIFromRI(componentId, userId, requirementDefList, componentTypeEnum);
1423 Assert.assertNotNull(result);
1424 assertEquals(new ArrayList<>(), result);
1425 } catch (ComponentException e) {
1426 assertEquals(e.getActionStatus().toString(), StorageOperationStatus.GENERAL_ERROR.toString());
1429 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1430 service.setLifecycleState(oldLifeCycleState);
1435 public void testDissociateRIFromRISuccess() {
1437 List<RequirementCapabilityRelDef> result;
1438 RequirementCapabilityRelDef ref = new RequirementCapabilityRelDef();
1439 List<RequirementCapabilityRelDef> requirementDefList = new ArrayList<>();
1440 requirementDefList.add(ref);
1441 ComponentTypeEnum componentTypeEnum = service.getComponentType();
1442 String componentId = service.getUniqueId();
1443 String userId = USER_ID;
1444 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1445 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1446 service.setLastUpdaterUserId(USER_ID);
1447 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1449 Either<Component, StorageOperationStatus> cont = Either.left(service);
1450 when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
1452 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1453 .thenReturn(StorageOperationStatus.OK);
1454 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1455 .thenReturn(StorageOperationStatus.OK);
1456 Either<RequirementCapabilityRelDef, StorageOperationStatus> resultEither;
1457 resultEither = Either.left(ref);
1458 when(toscaOperationFacade.dissociateResourceInstances(componentId, ref)).thenReturn(resultEither);
1460 result = componentInstanceBusinessLogic
1461 .batchDissociateRIFromRI(componentId, userId, requirementDefList, componentTypeEnum);
1462 Assert.assertNotNull(result);
1464 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1465 service.setLifecycleState(oldLifeCycleState);
1467 assertEquals(requirementDefList, result);
1471 public void testGetComponentInstancePropertyByPolicyId_success() {
1472 Optional<ComponentInstanceProperty> propertyCandidate =
1473 getComponentInstanceProperty(PROP_NAME);
1475 Assert.assertTrue(propertyCandidate.isPresent());
1476 Assert.assertEquals(propertyCandidate.get().getName(), PROP_NAME);
1480 public void testGetComponentInstancePropertyByPolicyId_failure() {
1481 Optional<ComponentInstanceProperty> propertyCandidate =
1482 getComponentInstanceProperty(NON_EXIST_NAME);
1484 Assert.assertEquals(propertyCandidate, Optional.empty());
1487 private Optional<ComponentInstanceProperty> getComponentInstanceProperty(String propertyName) {
1488 ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
1489 componentInstanceProperty.setName(propertyName);
1491 PolicyDefinition policyDefinition = getPolicyDefinition();
1492 componentInstanceProperty.setGetPolicyValues(policyDefinition.getGetPolicyValues());
1494 service.setComponentInstancesProperties(
1495 Collections.singletonMap(COMPONENT_INST_ID, Collections.singletonList(componentInstanceProperty)));
1497 return componentInstanceBusinessLogic.getComponentInstancePropertyByPolicyId(service, policyDefinition);
1500 private PolicyDefinition getPolicyDefinition() {
1501 PolicyDefinition policyDefinition = new PolicyDefinition();
1502 policyDefinition.setInstanceUniqueId(COMPONENT_INST_ID);
1503 policyDefinition.setName(PROP_NAME);
1505 GetPolicyValueDataDefinition getPolicy = new GetPolicyValueDataDefinition();
1506 getPolicy.setPropertyName(PROP_NAME);
1508 List<GetPolicyValueDataDefinition> getPolicies = new ArrayList<>();
1509 getPolicies.add(getPolicy);
1510 policyDefinition.setGetPolicyValues(getPolicies);
1512 return policyDefinition;
1515 private ComponentInstance createComponetInstanceFromComponent(Component component) {
1516 ComponentInstance componentInst = new ComponentInstance();
1517 componentInst.setUniqueId(component.getUniqueId());
1518 componentInst.setComponentUid(component.getUniqueId() + "_test");
1519 componentInst.setPosX("10");
1520 componentInst.setPosY("10");
1521 componentInst.setCapabilities(component.getCapabilities());
1522 componentInst.setRequirements(component.getRequirements());
1523 componentInst.setArtifacts(component.getArtifacts());
1524 componentInst.setDeploymentArtifacts(component.getDeploymentArtifacts());
1525 return componentInst;