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.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.junit.jupiter.api.Assertions.assertNull;
28 import static org.junit.jupiter.api.Assertions.assertSame;
29 import static org.junit.jupiter.api.Assertions.assertThrows;
30 import static org.junit.jupiter.api.Assertions.assertTrue;
31 import static org.junit.jupiter.api.DynamicTest.dynamicTest;
32 import static org.mockito.ArgumentMatchers.any;
33 import static org.mockito.ArgumentMatchers.anySet;
34 import static org.mockito.ArgumentMatchers.eq;
35 import static org.mockito.Mockito.times;
36 import static org.mockito.Mockito.verify;
37 import static org.mockito.Mockito.when;
39 import fj.data.Either;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.Collections;
43 import java.util.HashMap;
44 import java.util.HashSet;
45 import java.util.List;
47 import java.util.Optional;
49 import mockit.Deencapsulation;
50 import org.apache.commons.lang3.tuple.ImmutablePair;
51 import org.apache.commons.lang3.tuple.Pair;
52 import org.assertj.core.util.Lists;
53 import org.junit.jupiter.api.BeforeEach;
54 import org.junit.jupiter.api.DynamicTest;
55 import org.junit.jupiter.api.Test;
56 import org.junit.jupiter.api.TestFactory;
57 import org.junit.jupiter.api.extension.ExtendWith;
58 import org.mockito.InjectMocks;
59 import org.mockito.Mock;
60 import org.mockito.Mockito;
61 import org.mockito.MockitoAnnotations;
62 import org.mockito.junit.jupiter.MockitoExtension;
63 import org.mockito.junit.jupiter.MockitoSettings;
64 import org.mockito.quality.Strictness;
65 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
66 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
67 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
68 import org.openecomp.sdc.be.components.validation.UserValidations;
69 import org.openecomp.sdc.be.config.ConfigurationManager;
70 import org.openecomp.sdc.be.dao.api.ActionStatus;
71 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
72 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
73 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
74 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
75 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
76 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathElementDataDefinition;
77 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
78 import org.openecomp.sdc.be.datatypes.elements.GetPolicyValueDataDefinition;
79 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
80 import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition;
81 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
82 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
83 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
84 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
85 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
86 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
87 import org.openecomp.sdc.be.impl.ComponentsUtils;
88 import org.openecomp.sdc.be.model.ArtifactDefinition;
89 import org.openecomp.sdc.be.model.CapabilityDefinition;
90 import org.openecomp.sdc.be.model.CapabilityRequirementRelationship;
91 import org.openecomp.sdc.be.model.Component;
92 import org.openecomp.sdc.be.model.ComponentInstance;
93 import org.openecomp.sdc.be.model.ComponentInstanceInput;
94 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
95 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
96 import org.openecomp.sdc.be.model.ComponentInstanceAttribute;
97 import org.openecomp.sdc.be.model.ComponentParametersView;
98 import org.openecomp.sdc.be.model.DataTypeDefinition;
99 import org.openecomp.sdc.be.model.InputDefinition;
100 import org.openecomp.sdc.be.model.LifecycleStateEnum;
101 import org.openecomp.sdc.be.model.PolicyDefinition;
102 import org.openecomp.sdc.be.model.RelationshipImpl;
103 import org.openecomp.sdc.be.model.RelationshipInfo;
104 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
105 import org.openecomp.sdc.be.model.RequirementDefinition;
106 import org.openecomp.sdc.be.model.Resource;
107 import org.openecomp.sdc.be.model.Service;
108 import org.openecomp.sdc.be.model.User;
109 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
110 import org.openecomp.sdc.be.model.jsonjanusgraph.config.ContainerInstanceTypesData;
111 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ForwardingPathOperation;
112 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
113 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
114 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
115 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
116 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
117 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
118 import org.openecomp.sdc.common.api.ConfigurationSource;
119 import org.openecomp.sdc.common.impl.ExternalConfiguration;
120 import org.openecomp.sdc.common.impl.FSConfigurationSource;
121 import org.openecomp.sdc.common.util.ValidationUtils;
122 import org.openecomp.sdc.exception.ResponseFormat;
125 * The test suite designed for test functionality of ComponentInstanceBusinessLogic class
128 @ExtendWith(MockitoExtension.class)
129 @MockitoSettings(strictness = Strictness.LENIENT)
130 class ComponentInstanceBusinessLogicTest {
132 private final static String USER_ID = "jh0003";
133 private final static String COMPONENT_ID = "componentId";
134 private final static String ORIGIN_COMPONENT_ID = "originComponentId";
135 private final static String ORIGIN_COMPONENT_VERSION = "1.0";
136 private final static String TO_INSTANCE_ID = "toInstanceId";
137 private final static String TO_INSTANCE_NAME = "toInstanceName";
138 private final static String COMPONENT_INSTANCE_ID = "componentInstanceId";
139 private final static String COMPONENT_INSTANCE_NAME = "componentInstanceName";
140 private final static String FROM_INSTANCE_ID = "fromInstanceId";
141 private final static String RELATION_ID = "relationId";
142 private final static String CAPABILITY_OWNER_ID = "capabilityOwnerId";
143 private final static String CAPABILITY_UID = "capabilityUid";
144 private final static String CAPABILITY_NAME = "capabilityName";
145 private final static String REQUIREMENT_OWNER_ID = "requirementOwnerId";
146 private final static String REQUIREMENT_UID = "requirementUid";
147 private final static String REQUIREMENT_NAME = "requirementName";
148 private final static String RELATIONSHIP_TYPE = "relationshipType";
149 private final static String ARTIFACT_1 = "cloudtech_k8s_charts.zip";
150 private final static String ARTIFACT_2 = "cloudtech_azure_day0.zip";
151 private final static String ARTIFACT_3 = "cloudtech_aws_configtemplate.zip";
152 private final static String ARTIFACT_4 = "k8s_charts.zip";
153 private final static String ARTIFACT_5 = "cloudtech_openstack_configtemplate.zip";
154 private final static String PROP_NAME = "propName";
155 private final static String NON_EXIST_NAME = "nonExistName";
156 private final static String INPUT_ID = "inputId";
157 private final static String ICON_NAME = "icon";
159 private static ConfigurationSource configurationSource = new FSConfigurationSource(
160 ExternalConfiguration.getChangeListener(),
161 "src/test/resources/config/catalog-be");
162 private static ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
165 private static ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
167 private ComponentInstancePropInput componentInstancePropInput;
169 private ArtifactsBusinessLogic artifactsBusinessLogic;
171 private ComponentsUtils componentsUtils;
173 private ToscaOperationFacade toscaOperationFacade;
175 private ForwardingPathOperation forwardingPathOperation;
179 private UserValidations userValidations;
181 private GraphLockOperation graphLockOperation;
183 private JanusGraphDao janusGraphDao;
185 private ApplicationDataTypeCache dataTypeCache;
187 private PropertyOperation propertyOperation;
189 private ContainerInstanceTypesData containerInstanceTypeData;
191 private CompositionBusinessLogic compositionBusinessLogic;
193 private Component service;
194 private Component resource;
195 private ComponentInstance toInstance;
196 private ComponentInstance fromInstance;
197 private RequirementCapabilityRelDef relation;
198 private List<ComponentInstanceProperty> ciPropertyList;
199 private List<ComponentInstanceInput> ciInputList;
203 MockitoAnnotations.initMocks(this);
209 void testGetRelationByIdSuccess() {
210 getServiceRelationByIdSuccess(service);
211 getServiceRelationByIdSuccess(resource);
215 void testGetRelationByIdUserValidationFailure() {
216 getServiceRelationByIdUserValidationFailure(service);
217 getServiceRelationByIdUserValidationFailure(resource);
221 void testGetRelationByIdComponentNotFoundFailure() {
222 getRelationByIdComponentNotFoundFailure(service);
223 getRelationByIdComponentNotFoundFailure(resource);
227 void testForwardingPathOnVersionChange() {
228 getforwardingPathOnVersionChange();
232 void testIsCloudSpecificArtifact() {
233 assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_1)).isTrue();
234 assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_2)).isTrue();
235 assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_3)).isTrue();
236 assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_4)).isFalse();
237 assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_5)).isFalse();
240 private void getforwardingPathOnVersionChange() {
241 String containerComponentParam = "services";
242 String containerComponentID = "121-cont";
243 String componentInstanceID = "121-cont-1-comp";
244 Service component = new Service();
245 Map<String, ForwardingPathDataDefinition> forwardingPaths = generateForwardingPath(componentInstanceID);
247 //Add existing componentInstance to component
248 List<ComponentInstance> componentInstanceList = new ArrayList<>();
249 ComponentInstance oldComponentInstance = new ComponentInstance();
250 oldComponentInstance.setName("OLD_COMP_INSTANCE");
251 oldComponentInstance.setUniqueId(componentInstanceID);
252 oldComponentInstance.setName(componentInstanceID);
253 oldComponentInstance.setToscaPresentationValue(JsonPresentationFields.CI_COMPONENT_UID, "1-comp");
254 componentInstanceList.add(oldComponentInstance);
255 component.setComponentInstances(componentInstanceList);
256 component.setForwardingPaths(forwardingPaths);
258 List<ComponentInstance> componentInstanceListNew = new ArrayList<>();
259 ComponentInstance newComponentInstance = new ComponentInstance();
260 String new_Comp_UID = "2-comp";
261 newComponentInstance.setToscaPresentationValue(JsonPresentationFields.CI_COMPONENT_UID, new_Comp_UID);
262 newComponentInstance.setUniqueId(new_Comp_UID);
263 componentInstanceListNew.add(newComponentInstance);
264 Component component2 = new Service();
265 component2.setComponentInstances(componentInstanceListNew);
267 //Mock for getting component
268 when(toscaOperationFacade.getToscaElement(eq(containerComponentID), any(ComponentParametersView.class)))
269 .thenReturn(Either.left(component));
270 when(toscaOperationFacade.validateComponentExists(any(String.class))).thenReturn(Either.left(Boolean.TRUE));
271 when(toscaOperationFacade.getToscaFullElement(eq(new_Comp_UID))).thenReturn(Either.left(component2));
273 Either<Set<String>, ResponseFormat> resultOp = componentInstanceBusinessLogic
274 .forwardingPathOnVersionChange(containerComponentParam,
275 containerComponentID, componentInstanceID,
276 newComponentInstance);
277 assertEquals(1, resultOp.left().value().size());
278 assertEquals("FP-ID-1", resultOp.left().value().iterator().next());
283 void testCreateOrUpdatePropertiesValues2() {
284 String containerComponentID = "containerId";
285 String resourceInstanceId = "resourceId";
286 String componentInstanceID = "componentInstance";
287 List<ComponentInstanceProperty> properties = new ArrayList<>();
288 ComponentInstanceProperty property = new ComponentInstanceProperty();
289 property.setName("property");
290 property.setValue("newVal");
291 property.setType("string");
292 properties.add(property);
294 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
295 ComponentInstanceProperty origProperty = new ComponentInstanceProperty();
296 origProperty.setName("property");
297 origProperty.setValue("value");
298 origProperty.setType("string");
299 origProperties.add(origProperty);
301 Map<String, DataTypeDefinition> types = new HashMap<>();
302 DataTypeDefinition dataTypeDef = new DataTypeDefinition();
303 types.put("string", dataTypeDef);
305 Component component = new Service();
306 component.setLastUpdaterUserId("userId");
307 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
308 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
309 componentInstanceProps.put("resourceId", origProperties);
310 component.setComponentInstancesProperties(componentInstanceProps);
311 ComponentInstance ci = createComponentInstance("ci1");
312 ci.setUniqueId("resourceId");
313 component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
314 createComponentInstance(componentInstanceID)));
316 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
317 .thenReturn(Either.left(component));
318 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
319 .thenReturn(StorageOperationStatus.OK);
320 when(dataTypeCache.getAll()).thenReturn(Either.left(types));
321 when(propertyOperation.validateAndUpdatePropertyValue(property.getType(), "newVal", true, null, types))
322 .thenReturn(Either.left("newVal"));
323 when(propertyOperation.validateAndUpdateRules("string", property.getRules(),
324 null, types, true)).thenReturn(ImmutablePair.of("string", null));
325 when(toscaOperationFacade.updateComponentInstanceProperty(component, ci.getUniqueId(),
326 origProperty)).thenReturn(StorageOperationStatus.OK);
327 origProperties.get(0).setValue("newVal");
328 when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(component))
329 .thenReturn(Either.left(component));
330 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
331 when(graphLockOperation.unlockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
332 .thenReturn(StorageOperationStatus.OK);
334 Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = componentInstanceBusinessLogic
335 .createOrUpdatePropertiesValues(
336 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
337 assertThat(responseFormatEither.left().value()).isEqualTo(properties);
341 void testCreateOrUpdatePropertiesValuesPropertyNotExists() {
342 String containerComponentID = "containerId";
343 String resourceInstanceId = "resourceId";
344 String componentInstanceID = "componentInstance";
345 List<ComponentInstanceProperty> properties = new ArrayList<>();
346 ComponentInstanceProperty property = new ComponentInstanceProperty();
347 property.setName("property");
348 property.setValue("newVal");
349 property.setType("string");
350 properties.add(property);
352 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
354 Component component = new Service();
355 component.setLastUpdaterUserId("userId");
356 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
357 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
358 componentInstanceProps.put("resourceId", origProperties);
359 component.setComponentInstancesProperties(componentInstanceProps);
360 ComponentInstance ci = createComponentInstance("ci1");
361 ci.setUniqueId("resourceId");
362 component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
363 createComponentInstance(componentInstanceID)));
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(graphLockOperation.unlockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
370 .thenReturn(StorageOperationStatus.OK);
373 componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
374 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
375 } catch (ComponentException e) {
376 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.PROPERTY_NOT_FOUND);
382 void testCreateOrUpdatePropertiesValuesValidationFailure() {
383 String containerComponentID = "containerId";
384 String resourceInstanceId = "resourceId";
385 String componentInstanceID = "componentInstance";
386 List<ComponentInstanceProperty> properties = new ArrayList<>();
387 ComponentInstanceProperty property = new ComponentInstanceProperty();
388 property.setName("property");
389 property.setValue("newVal");
390 property.setType("string");
391 properties.add(property);
393 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
394 ComponentInstanceProperty origProperty = new ComponentInstanceProperty();
395 origProperty.setName("property");
396 origProperty.setValue("value");
397 origProperty.setType("string");
398 origProperties.add(origProperty);
400 Map<String, DataTypeDefinition> types = new HashMap<>();
401 DataTypeDefinition dataTypeDef = new DataTypeDefinition();
402 types.put("string", dataTypeDef);
404 Component component = new Service();
405 component.setLastUpdaterUserId("userId");
406 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
407 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
408 componentInstanceProps.put("resourceId", origProperties);
409 component.setComponentInstancesProperties(componentInstanceProps);
410 ComponentInstance ci = createComponentInstance("ci1");
411 ci.setUniqueId("resourceId");
412 component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
413 createComponentInstance(componentInstanceID)));
415 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
416 .thenReturn(Either.left(component));
417 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
418 .thenReturn(StorageOperationStatus.OK);
419 when(dataTypeCache.getAll()).thenReturn(Either.left(types));
420 when(propertyOperation.validateAndUpdatePropertyValue(property.getType(), "newVal", true, null, types))
421 .thenReturn(Either.right(false));
422 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.BAD_REQUEST))
423 .thenReturn(ActionStatus.INVALID_CONTENT);
425 ComponentException e = assertThrows(ComponentException.class,
426 () -> componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
427 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId"));
428 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.INVALID_CONTENT);
432 void testCreateOrUpdatePropertiesValuesMissingFieldFailure() {
433 String containerComponentID = "containerId";
434 String resourceInstanceId = "resourceId";
435 String componentInstanceID = "componentInstance";
436 List<ComponentInstanceProperty> properties = new ArrayList<>();
437 ComponentInstanceProperty property = new ComponentInstanceProperty();
438 property.setValue("newVal");
439 property.setType("string");
440 properties.add(property);
442 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
443 ComponentInstanceProperty origProperty = new ComponentInstanceProperty();
444 origProperty.setName("property");
445 origProperty.setValue("value");
446 origProperty.setType("string");
447 origProperties.add(origProperty);
449 Component component = new Service();
450 component.setLastUpdaterUserId("userId");
451 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
452 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
453 componentInstanceProps.put("resourceId", origProperties);
454 component.setComponentInstancesProperties(componentInstanceProps);
455 ComponentInstance ci = createComponentInstance("ci1");
456 ci.setUniqueId("resourceId");
457 component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
458 createComponentInstance(componentInstanceID)));
460 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
461 .thenReturn(Either.left(component));
462 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
463 .thenReturn(StorageOperationStatus.OK);
466 componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
467 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
468 } catch (ComponentException e) {
469 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.MISSING_PROPERTY_NAME);
474 void testDeleteForwardingPathsWhenComponentinstanceDeleted() {
476 ComponentTypeEnum containerComponentType = ComponentTypeEnum.findByParamName("services");
477 String containerComponentID = "Service-comp";
478 String componentInstanceID = "NodeA1";
479 Service component = new Service();
481 .setComponentInstances(Arrays.asList(createComponentInstance("NodeA2"), createComponentInstance("NodeB2"),
482 createComponentInstance(componentInstanceID)));
484 component.addForwardingPath(createPath("path1", componentInstanceID, "NodeB1", "1"));
485 component.addForwardingPath(createPath("Path2", "NodeA2", "NodeB2", "2"));
486 when(toscaOperationFacade.getToscaElement(eq(containerComponentID), any(ComponentParametersView.class)))
487 .thenReturn(Either.left(component));
488 when(toscaOperationFacade.getToscaElement(eq(containerComponentID))).thenReturn(Either.left(component));
489 when(forwardingPathOperation.deleteForwardingPath(any(Service.class), anySet()))
490 .thenReturn(Either.left(new HashSet<>()));
491 final ComponentInstance ci = new ComponentInstance();
492 ci.setName(componentInstanceID);
493 ComponentInstance responseFormatEither = componentInstanceBusinessLogic
494 .deleteForwardingPathsRelatedTobeDeletedComponentInstance(
495 containerComponentID, containerComponentType, ci);
496 assertFalse(responseFormatEither.isEmpty());
500 void testAddComponentInstanceDeploymentArtifacts() {
502 Component containerComponent = new Service();
503 ComponentInstance componentInstance = new ComponentInstance();
504 componentInstance.setUniqueId(COMPONENT_INSTANCE_ID);
505 Component originComponent = fillOriginComponent(new Resource());
507 Map<String, ArtifactDefinition> artifacts = new HashMap<>();
508 ArtifactDefinition deploymentArtifact1 = getArtifact("deploymentArtifact1", ArtifactTypeEnum.HEAT.getType());
509 artifacts.put(deploymentArtifact1.getArtifactLabel(), deploymentArtifact1);
510 ArtifactDefinition deploymentArtifact2 = getArtifact("deploymentArtifact2",
511 ArtifactTypeEnum.HEAT_ENV.getType());
512 artifacts.put(deploymentArtifact2.getArtifactLabel(), deploymentArtifact2);
513 ArtifactDefinition deploymentArtifact3 = getArtifact("deploymentArtifact3",
514 ArtifactTypeEnum.HEAT_VOL.getType());
515 artifacts.put(deploymentArtifact3.getArtifactLabel(), deploymentArtifact3);
516 ArtifactDefinition heatEnvPlaceHolder = getArtifact("deploymentArtifact4", ArtifactTypeEnum.HEAT_ENV.getType());
517 ArtifactDefinition heatEnvPlaceHolder2 = getArtifact("deploymentArtifact5",
518 ArtifactTypeEnum.HEAT_ENV.getType());
520 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getResourceDeploymentArtifacts = Either
523 Map<String, ArtifactDefinition> finalDeploymentArtifacts = new HashMap<>();
524 finalDeploymentArtifacts.put(deploymentArtifact1.getArtifactLabel(), deploymentArtifact1);
525 finalDeploymentArtifacts.put(deploymentArtifact3.getArtifactLabel(), deploymentArtifact3);
526 finalDeploymentArtifacts.put(heatEnvPlaceHolder.getArtifactLabel(), heatEnvPlaceHolder);
527 finalDeploymentArtifacts.put(heatEnvPlaceHolder2.getArtifactLabel(), heatEnvPlaceHolder2);
529 when(artifactsBusinessLogic.getArtifacts(componentInstance.getComponentUid(), NodeTypeEnum.Resource,
530 ArtifactGroupTypeEnum.DEPLOYMENT, null)).thenReturn(getResourceDeploymentArtifacts);
531 when(artifactsBusinessLogic.createHeatEnvPlaceHolder(new ArrayList<>(),
532 deploymentArtifact1, ArtifactsBusinessLogic.HEAT_ENV_NAME, componentInstance.getUniqueId(),
533 NodeTypeEnum.ResourceInstance, componentInstance.getName(), user, containerComponent,
534 null)).thenReturn(heatEnvPlaceHolder);
535 when(artifactsBusinessLogic.createHeatEnvPlaceHolder(new ArrayList<>(),
536 deploymentArtifact3, ArtifactsBusinessLogic.HEAT_ENV_NAME, componentInstance.getUniqueId(),
537 NodeTypeEnum.ResourceInstance, componentInstance.getName(), user, containerComponent,
538 null)).thenReturn(heatEnvPlaceHolder2);
540 componentInstanceBusinessLogic.setToscaOperationFacade(toscaOperationFacade);
541 when(toscaOperationFacade.addDeploymentArtifactsToInstance(containerComponent.getUniqueId(), componentInstance,
542 finalDeploymentArtifacts)).thenReturn(StorageOperationStatus.OK);
543 when(toscaOperationFacade
544 .addGroupInstancesToComponentInstance(containerComponent, componentInstance, new ArrayList<>(),
546 .thenReturn(StorageOperationStatus.OK);
547 when(toscaOperationFacade
548 .addInformationalArtifactsToInstance(containerComponent.getUniqueId(), componentInstance, null))
549 .thenReturn(StorageOperationStatus.OK);
551 ActionStatus status = componentInstanceBusinessLogic.addComponentInstanceArtifacts(containerComponent,
552 componentInstance, originComponent, user, null);
554 assertThat(status).isEqualTo(ActionStatus.OK);
558 private Component fillOriginComponent(Resource originComponent) {
559 originComponent.setUniqueId("resourceId");
560 originComponent.setUniqueId(ORIGIN_COMPONENT_ID);
561 originComponent.setComponentInstances(Lists.newArrayList(toInstance, fromInstance));
562 originComponent.setComponentType(ComponentTypeEnum.RESOURCE);
563 originComponent.setState(LifecycleStateEnum.CERTIFIED);
564 return originComponent;
567 private ArtifactDefinition getArtifact(String artifactLabel, String artifactType) {
568 ArtifactDefinition artifactDefinition = new ArtifactDefinition();
569 artifactDefinition.setArtifactLabel(artifactLabel);
570 artifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
571 artifactDefinition.setEsId("esId" + artifactLabel);
572 artifactDefinition.setArtifactType(artifactType);
573 artifactDefinition.setArtifactName("artifactName");
574 return artifactDefinition;
577 private ComponentInstance createComponentInstance(String path1) {
578 ComponentInstance componentInstance = new ComponentInstance();
579 componentInstance.setName(path1);
580 return componentInstance;
583 private ForwardingPathDataDefinition createPath(String pathName, String fromNode, String toNode, String uniqueId) {
584 ForwardingPathDataDefinition forwardingPath = new ForwardingPathDataDefinition(pathName);
585 forwardingPath.setProtocol("protocol");
586 forwardingPath.setDestinationPortNumber("port");
587 forwardingPath.setUniqueId(uniqueId);
588 ListDataDefinition<ForwardingPathElementDataDefinition> forwardingPathElementListDataDefinition =
589 new ListDataDefinition<>();
590 forwardingPathElementListDataDefinition
591 .add(new ForwardingPathElementDataDefinition(fromNode, toNode, "nodeAcpType", "nodeBcpType",
592 "nodeDcpName", "nodeBcpName"));
593 forwardingPath.setPathElements(forwardingPathElementListDataDefinition);
595 return forwardingPath;
598 private Map<String, ForwardingPathDataDefinition> generateForwardingPath(String componentInstanceID) {
599 ForwardingPathDataDefinition forwardingPath = new ForwardingPathDataDefinition("fpName");
600 String protocol = "protocol";
601 forwardingPath.setProtocol(protocol);
602 forwardingPath.setDestinationPortNumber("DestinationPortNumber");
603 forwardingPath.setUniqueId("FP-ID-1");
604 ListDataDefinition<ForwardingPathElementDataDefinition> forwardingPathElementListDataDefinition =
605 new ListDataDefinition<>();
606 forwardingPathElementListDataDefinition
607 .add(new ForwardingPathElementDataDefinition(componentInstanceID, "nodeB", "nodeA_FORWARDER_CAPABILITY",
608 "nodeBcpType", "nodeDcpName", "nodeBcpName"));
609 forwardingPath.setPathElements(forwardingPathElementListDataDefinition);
610 Map<String, ForwardingPathDataDefinition> forwardingPaths = new HashMap<>();
611 forwardingPaths.put("1122", forwardingPath);
612 return forwardingPaths;
615 private void getServiceRelationByIdSuccess(Component component) {
616 Either<Component, StorageOperationStatus> getComponentRes = Either.left(component);
617 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
618 .thenReturn(getComponentRes);
619 Either<RequirementCapabilityRelDef, ResponseFormat> response = componentInstanceBusinessLogic
620 .getRelationById(COMPONENT_ID,
621 RELATION_ID, USER_ID,
622 component.getComponentType());
623 assertThat(response.isLeft()).isTrue();
626 private void getServiceRelationByIdUserValidationFailure(Component component) {
627 when(userValidations.validateUserExists(eq(USER_ID)))
628 .thenThrow(new ByActionStatusComponentException(ActionStatus.USER_NOT_FOUND));
630 componentInstanceBusinessLogic
631 .getRelationById(COMPONENT_ID, RELATION_ID, USER_ID, component.getComponentType());
632 } catch (ByActionStatusComponentException e) {
633 assertSame(ActionStatus.USER_NOT_FOUND, e.getActionStatus());
637 private void getRelationByIdComponentNotFoundFailure(Component component) {
638 Either<Component, StorageOperationStatus> getComponentRes = Either.right(StorageOperationStatus.NOT_FOUND);
639 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
640 .thenReturn(getComponentRes);
642 Either<RequirementCapabilityRelDef, ResponseFormat> response = componentInstanceBusinessLogic
643 .getRelationById(COMPONENT_ID,
644 RELATION_ID, USER_ID,
645 component.getComponentType());
646 assertThat(response.isRight()).isTrue();
649 private void stubMethods() {
650 Mockito.lenient().when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(user);
651 Mockito.lenient().when(componentsUtils
652 .convertFromStorageResponse(eq(StorageOperationStatus.GENERAL_ERROR), any(ComponentTypeEnum.class)))
653 .thenReturn(ActionStatus.GENERAL_ERROR);
656 private void createComponents() {
662 resource = createResource();
665 private Resource createResource() {
666 final Resource resource = new Resource();
667 resource.setUniqueId(COMPONENT_ID);
668 resource.setComponentInstancesRelations(Lists.newArrayList(relation));
669 resource.setComponentInstances(Lists.newArrayList(toInstance, fromInstance));
670 resource.setCapabilities(toInstance.getCapabilities());
671 resource.setRequirements(fromInstance.getRequirements());
672 resource.setComponentType(ComponentTypeEnum.RESOURCE);
673 resource.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
677 private void createService() {
678 service = new Service();
679 service.setUniqueId(COMPONENT_ID);
680 service.setComponentInstancesRelations(Lists.newArrayList(relation));
681 service.setComponentInstances(Lists.newArrayList(toInstance, fromInstance));
682 service.setCapabilities(toInstance.getCapabilities());
683 service.setRequirements(fromInstance.getRequirements());
684 service.setComponentType(ComponentTypeEnum.SERVICE);
685 service.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
686 service.setLastUpdaterUserId(USER_ID);
688 Map<String, List<ComponentInstanceProperty>> ciPropertyMap = new HashMap<>();
689 ciPropertyMap.put(TO_INSTANCE_ID, ciPropertyList);
690 service.setComponentInstancesProperties(ciPropertyMap);
692 Map<String, List<ComponentInstanceInput>> ciInputMap = new HashMap<>();
693 ciInputMap.put(TO_INSTANCE_ID, ciInputList);
694 service.setComponentInstancesInputs(ciInputMap);
697 private void createInstances() {
698 toInstance = new ComponentInstance();
699 toInstance.setUniqueId(TO_INSTANCE_ID);
700 toInstance.setName(TO_INSTANCE_NAME);
702 fromInstance = new ComponentInstance();
703 fromInstance.setUniqueId(FROM_INSTANCE_ID);
705 CapabilityDataDefinition capability = new CapabilityDataDefinition();
706 capability.setOwnerId(CAPABILITY_OWNER_ID);
707 capability.setUniqueId(CAPABILITY_UID);
708 capability.setName(CAPABILITY_NAME);
710 Map<String, List<CapabilityDefinition>> capabilities = new HashMap<>();
711 final CapabilityDefinition capabilityDefinition = new CapabilityDefinition(capability);
712 final ArrayList<ComponentInstanceProperty> properties = new ArrayList<>();
713 properties.add(componentInstancePropInput);
714 capabilityDefinition.setProperties(properties);
715 capabilities.put(capability.getName(), Lists.newArrayList(capabilityDefinition));
717 RequirementDataDefinition requirement = new RequirementDataDefinition();
718 requirement.setOwnerId(REQUIREMENT_OWNER_ID);
719 requirement.setUniqueId(REQUIREMENT_UID);
720 requirement.setName(REQUIREMENT_NAME);
721 requirement.setRelationship(RELATIONSHIP_TYPE);
723 Map<String, List<RequirementDefinition>> requirements = new HashMap<>();
724 requirements.put(requirement.getCapability(), Lists.newArrayList(new RequirementDefinition(requirement)));
726 toInstance.setCapabilities(capabilities);
727 fromInstance.setRequirements(requirements);
731 private void createRelation() {
733 relation = new RequirementCapabilityRelDef();
734 CapabilityRequirementRelationship relationship = new CapabilityRequirementRelationship();
735 RelationshipInfo relationInfo = new RelationshipInfo();
736 relationInfo.setId(RELATION_ID);
737 relationship.setRelation(relationInfo);
739 relation.setRelationships(Lists.newArrayList(relationship));
740 relation.setToNode(TO_INSTANCE_ID);
741 relation.setFromNode(FROM_INSTANCE_ID);
743 relationInfo.setCapabilityOwnerId(CAPABILITY_OWNER_ID);
744 relationInfo.setCapabilityUid(CAPABILITY_UID);
745 relationInfo.setCapability(CAPABILITY_NAME);
746 relationInfo.setRequirementOwnerId(REQUIREMENT_OWNER_ID);
747 relationInfo.setRequirementUid(REQUIREMENT_UID);
748 relationInfo.setRequirement(REQUIREMENT_NAME);
749 RelationshipImpl relationshipImpl = new RelationshipImpl();
750 relationshipImpl.setType(RELATIONSHIP_TYPE);
751 relationInfo.setRelationships(relationshipImpl);
754 private void createProperties() {
755 // Create GetInputValueData
756 GetInputValueDataDefinition inputValueDef = new GetInputValueDataDefinition();
757 inputValueDef.setInputId(INPUT_ID);
758 List<GetInputValueDataDefinition> inputValueDefList = new ArrayList<>();
759 inputValueDefList.add(inputValueDef);
760 // Create ComponentInstanceProperty
761 ComponentInstanceProperty ciProperty = new ComponentInstanceProperty();
762 ciProperty.setGetInputValues(inputValueDefList);
763 ciProperty.setName(PROP_NAME);
764 // Create ComponentInstanceProperty list
765 ciPropertyList = new ArrayList<>();
766 ciPropertyList.add(ciProperty);
769 private void createInputs() {
770 // Create GetInputValueData
771 GetInputValueDataDefinition inputValueDef = new GetInputValueDataDefinition();
772 inputValueDef.setInputId(INPUT_ID);
773 List<GetInputValueDataDefinition> inputValueDefList = new ArrayList<>();
774 inputValueDefList.add(inputValueDef);
775 // Create ComponentInstanceInput
776 ComponentInstanceInput ciInput = new ComponentInstanceInput();
777 ciInput.setUniqueId(INPUT_ID);
778 ciInput.setName(PROP_NAME);
779 ciInput.setGetInputValues(inputValueDefList);
780 // Create ComponentInstanceInput list
781 ciInputList = new ArrayList<>();
782 ciInputList.add(ciInput);
785 private ComponentInstanceBusinessLogic createTestSubject() {
786 return componentInstanceBusinessLogic;
790 void testChangeServiceProxyVersion() {
791 ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
793 Either<ComponentInstance, ResponseFormat> result;
796 componentInstanceBusinessLogic = createTestSubject();
797 result = componentInstanceBusinessLogic.changeServiceProxyVersion();
798 assertNotNull(result);
802 void testCreateServiceProxy() {
803 ComponentInstanceBusinessLogic testSubject;
804 Either<ComponentInstance, ResponseFormat> result;
807 testSubject = createTestSubject();
808 result = testSubject.createServiceProxy();
809 assertNotNull(result);
813 void testDeleteServiceProxy() {
814 ComponentInstanceBusinessLogic testSubject;
816 Either<ComponentInstance, ResponseFormat> result;
819 testSubject = createTestSubject();
820 result = testSubject.deleteServiceProxy();
821 assertNotNull(result);
825 void testGetComponentInstanceInputsByInputIdEmpty() {
826 Component component = new Service();
828 List<ComponentInstanceInput> result;
830 result = componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(component, inputId);
831 assertNotNull(result);
832 assertThat(result.isEmpty()).isTrue();
836 void testGetComponentInstanceInputsByInputIdPresent() {
837 List<ComponentInstanceInput> result;
839 result = componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(service, INPUT_ID);
840 assertNotNull(result);
841 assertThat(result.isEmpty()).isFalse();
842 assertThat(result.size()).isOne();
843 ComponentInstanceInput resultInput = result.get(0);
844 assertThat(resultInput.getComponentInstanceId()).isEqualTo(TO_INSTANCE_ID);
845 assertThat(resultInput.getComponentInstanceName()).isEqualTo(TO_INSTANCE_NAME);
849 void testGetComponentInstancePropertiesByInputIdEmpty() {
850 Component component = new Service();
852 List<ComponentInstanceProperty> result;
854 result = componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(component, inputId);
855 assertNotNull(result);
856 assertThat(result.isEmpty()).isTrue();
860 void testGetComponentInstancePropertiesByInputIdPresent() {
861 List<ComponentInstanceProperty> result;
863 result = componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(service, INPUT_ID);
864 assertNotNull(result);
865 assertThat(result.size()).isOne();
866 ComponentInstanceProperty resultProperty = result.get(0);
867 assertThat(resultProperty.getComponentInstanceId()).isEqualTo(TO_INSTANCE_ID);
868 assertThat(resultProperty.getComponentInstanceName()).isEqualTo(TO_INSTANCE_NAME);
872 void testGetRelationById() {
873 ComponentInstanceBusinessLogic testSubject;
874 String componentId = "";
875 String relationId = "";
876 String userId = user.getUserId();
877 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE_INSTANCE;
878 Either<RequirementCapabilityRelDef, ResponseFormat> result;
881 testSubject = createTestSubject();
882 result = testSubject.getRelationById(componentId, relationId, userId, componentTypeEnum);
883 assertNotNull(result);
887 void testValidateParent() {
888 ComponentInstanceBusinessLogic testSubject;
889 resource = createResource();
890 String nodeTemplateId = "";
894 testSubject = createTestSubject();
895 result = Deencapsulation.invoke(testSubject, "validateParent", new Object[]{resource, nodeTemplateId});
900 void testGetComponentType() {
901 ComponentInstanceBusinessLogic testSubject;
902 ComponentTypeEnum result;
905 testSubject = createTestSubject();
906 result = Deencapsulation.invoke(testSubject, "getComponentType", new Object[]{ComponentTypeEnum.class});
907 assertNotNull(result);
911 void testGetNewGroupName() {
912 ComponentInstanceBusinessLogic testSubject;
913 String oldPrefix = "";
914 String newNormailzedPrefix = "";
915 String qualifiedGroupInstanceName = "";
919 testSubject = createTestSubject();
920 result = Deencapsulation.invoke(testSubject, "getNewGroupName",
921 new Object[]{oldPrefix, newNormailzedPrefix, qualifiedGroupInstanceName});
922 assertNotNull(result);
926 void testUpdateComponentInstanceMetadata_3() {
927 ComponentInstanceBusinessLogic testSubject;
929 ComponentInstance result;
932 testSubject = createTestSubject();
933 result = Deencapsulation
934 .invoke(testSubject, "updateComponentInstanceMetadata", new Object[]{toInstance, toInstance});
935 assertNotNull(result);
939 void testFindRelation() {
940 ComponentInstanceBusinessLogic testSubject;
941 String relationId = "";
942 List<RequirementCapabilityRelDef> requirementCapabilityRelations = new ArrayList<>();
943 RequirementCapabilityRelDef result;
946 testSubject = createTestSubject();
947 result = Deencapsulation.invoke(testSubject, "findRelation",
948 new Object[]{relationId, requirementCapabilityRelations});
953 void testCreateOrUpdatePropertiesValues() {
954 ComponentInstanceBusinessLogic testSubject;
955 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
956 resource = createResource();
957 String componentId = resource.getUniqueId();
958 String resourceInstanceId = "";
959 List<ComponentInstanceProperty> properties = new ArrayList<>();
960 String userId = user.getUserId();
961 Either<List<ComponentInstanceProperty>, ResponseFormat> result;
963 when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll))
964 .thenReturn(Either.left(resource));
967 testSubject = createTestSubject();
969 .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
971 assertNotNull(result);
973 componentTypeEnum = null;
975 .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
977 assertNotNull(result);
980 .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
982 assertNotNull(result);
986 void testUpdateCapabilityPropertyOnContainerComponent() {
987 ComponentInstanceBusinessLogic testSubject;
988 ComponentInstanceProperty property = new ComponentInstanceProperty();
989 String newValue = "";
990 resource = createResource();
992 String capabilityType = "";
993 String capabilityName = "";
994 ResponseFormat result;
997 testSubject = createTestSubject();
998 result = Deencapsulation.invoke(testSubject, "updateCapabilityPropertyOnContainerComponent",
999 new Object[]{property, newValue, resource, toInstance, capabilityType, capabilityName});
1004 void testCreateOrUpdateInstanceInputValues() {
1005 ComponentInstanceBusinessLogic testSubject;
1006 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1007 resource = createResource();
1008 String componentId = resource.getUniqueId();
1009 String resourceInstanceId = "";
1010 List<ComponentInstanceInput> inputs = new ArrayList<>();
1011 String userId = user.getUserId();
1012 Either<List<ComponentInstanceInput>, ResponseFormat> result;
1014 when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll))
1015 .thenReturn(Either.left(resource));
1018 testSubject = createTestSubject();
1019 result = testSubject
1020 .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
1022 assertNotNull(result);
1023 componentTypeEnum = null;
1024 result = testSubject
1025 .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
1027 assertNotNull(result);
1029 result = testSubject
1030 .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
1032 assertNotNull(result);
1036 void testCreateOrUpdateGroupInstancePropertyValue() {
1037 ComponentInstanceBusinessLogic testSubject;
1038 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1039 resource = createResource();
1040 String componentId = resource.getUniqueId();
1041 String resourceInstanceId = "";
1042 String groupInstanceId = "";
1043 ComponentInstanceProperty property = new ComponentInstanceProperty();
1044 String userId = user.getUserId();
1045 Either<ComponentInstanceProperty, ResponseFormat> result;
1047 when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseMetadata))
1048 .thenReturn(Either.left(resource));
1051 testSubject = createTestSubject();
1052 result = testSubject
1053 .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1054 groupInstanceId, property, userId);
1055 assertNotNull(result);
1056 componentTypeEnum = null;
1057 result = testSubject
1058 .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1059 groupInstanceId, property, userId);
1060 assertNotNull(result);
1062 result = testSubject
1063 .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1064 groupInstanceId, property, userId);
1065 assertNotNull(result);
1069 void testDeletePropertyValue() {
1070 ComponentInstanceBusinessLogic testSubject;
1071 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1073 String serviceId = service.getUniqueId();
1074 String resourceInstanceId = "";
1075 String propertyValueId = "";
1076 String userId = user.getUserId();
1077 Either<ComponentInstanceProperty, ResponseFormat> result;
1079 when(toscaOperationFacade.getToscaElement(serviceId, JsonParseFlagEnum.ParseMetadata))
1080 .thenReturn(Either.left(service));
1083 testSubject = createTestSubject();
1084 result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1086 assertNotNull(result);
1087 componentTypeEnum = null;
1088 result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1090 assertNotNull(result);
1092 result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1094 assertNotNull(result);
1098 void testGetComponentParametersViewForForwardingPath() {
1099 ComponentInstanceBusinessLogic testSubject;
1100 ComponentParametersView result;
1103 testSubject = createTestSubject();
1104 result = Deencapsulation.invoke(testSubject, "getComponentParametersViewForForwardingPath");
1105 assertNotNull(result);
1109 void testGetResourceInstanceById() {
1110 ComponentInstanceBusinessLogic testSubject;
1111 resource = createResource();
1112 String instanceId = "";
1113 Either<ComponentInstance, StorageOperationStatus> result;
1116 testSubject = createTestSubject();
1117 result = Deencapsulation.invoke(testSubject, "getResourceInstanceById", new Object[]{resource, instanceId});
1118 assertNotNull(result);
1122 void testUpdateInstanceCapabilityProperties_1() {
1123 ComponentInstanceBusinessLogic testSubject;
1124 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1125 resource = createResource();
1126 String containerComponentId = resource.getUniqueId();
1127 String componentInstanceUniqueId = "";
1128 String capabilityType = "";
1129 String capabilityName = "";
1130 List<ComponentInstanceProperty> properties = new ArrayList<>();
1131 String userId = user.getUserId();
1132 Either<List<ComponentInstanceProperty>, ResponseFormat> result;
1134 when(toscaOperationFacade.getToscaFullElement(containerComponentId))
1135 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
1137 testSubject = createTestSubject();
1138 result = testSubject.updateInstanceCapabilityProperties(componentTypeEnum, containerComponentId,
1139 componentInstanceUniqueId, capabilityType, capabilityName, properties, userId);
1140 assertNotNull(result);
1141 when(toscaOperationFacade.getToscaFullElement(containerComponentId)).thenReturn(Either.left(resource));
1142 result = testSubject.updateInstanceCapabilityProperties(componentTypeEnum, containerComponentId,
1143 componentInstanceUniqueId, capabilityType, capabilityName, properties, userId);
1144 assertNotNull(result);
1148 void testUpdateInstanceRequirement() {
1149 ComponentInstanceBusinessLogic testSubject;
1150 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1152 String userId = "userId";
1153 resource.setLastUpdaterUserId(userId);
1154 String containerComponentId = resource.getUniqueId();
1155 String componentInstanceUniqueId = TO_INSTANCE_ID;
1156 String capabilityType = "";
1157 String capabilityName = "";
1158 RequirementDefinition requirementDef = new RequirementDefinition();
1160 Either<RequirementDefinition, ResponseFormat> result;
1162 when(toscaOperationFacade.getToscaFullElement(containerComponentId)).thenReturn(Either.left(resource));
1163 testSubject = createTestSubject();
1164 when(toscaOperationFacade.updateComponentInstanceRequirement(containerComponentId, TO_INSTANCE_ID, requirementDef)).thenReturn(StorageOperationStatus.OK);
1165 when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(resource)).thenReturn(Either.left(resource));
1166 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource)))
1167 .thenReturn(StorageOperationStatus.OK);
1168 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource)))
1169 .thenReturn(StorageOperationStatus.OK);
1171 result = testSubject.updateInstanceRequirement(componentTypeEnum, containerComponentId,
1172 componentInstanceUniqueId, requirementDef, userId);
1173 assertEquals(requirementDef, result.left().value());
1178 void testCopyComponentInstanceWrongUserId() {
1180 Either<Map<String, ComponentInstance>, ResponseFormat> result;
1181 ComponentInstance inputComponentInstance = createComponetInstanceFromComponent(resource);
1182 String containerComponentId = service.getUniqueId();
1183 String componentInstanceId = resource.getUniqueId();
1184 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1185 service.setLastUpdaterUserId("wrong user id");
1187 Either<Component, StorageOperationStatus> leftServiceOp = Either.left(service);
1188 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(leftServiceOp);
1189 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1190 .thenReturn(leftServiceOp);
1191 when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
1192 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1193 .thenReturn(StorageOperationStatus.OK);
1194 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1195 .thenReturn(StorageOperationStatus.OK);
1197 result = componentInstanceBusinessLogic
1198 .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId,
1200 assertNotNull(result);
1202 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1203 assertThat(result.isRight()).isTrue();
1207 void testCopyComponentInstanceComponentWrongState() {
1208 Either<Map<String, ComponentInstance>, ResponseFormat> result;
1209 ComponentInstance inputComponentInstance = createComponetInstanceFromComponent(resource);
1210 String containerComponentId = service.getUniqueId();
1211 String componentInstanceId = resource.getUniqueId();
1212 String oldServiceLastUpdatedUserId = service.getLastUpdaterUserId();
1213 service.setLastUpdaterUserId(USER_ID);
1215 Either<Component, StorageOperationStatus> leftServiceOp = Either.left(service);
1216 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(leftServiceOp);
1217 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1218 .thenReturn(leftServiceOp);
1219 when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
1220 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1221 .thenReturn(StorageOperationStatus.OK);
1222 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1223 .thenReturn(StorageOperationStatus.OK);
1224 result = componentInstanceBusinessLogic
1225 .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId, USER_ID);
1226 assertNotNull(result);
1227 service.setLastUpdaterUserId(oldServiceLastUpdatedUserId);
1228 assertThat(result.isRight()).isTrue();
1232 void testCopyComponentInstance() {
1233 Either<Map<String, ComponentInstance>, ResponseFormat> result;
1234 ComponentInstance inputComponentInstance = createComponetInstanceFromComponent(resource);
1235 String containerComponentId = service.getUniqueId();
1236 String componentInstanceId = resource.getUniqueId();
1237 String oldServiceLastUpdatedUserId = service.getLastUpdaterUserId();
1238 service.setLastUpdaterUserId(USER_ID);
1239 LifecycleStateEnum oldResourceLifeCycle = resource.getLifecycleState();
1240 resource.setLifecycleState(LifecycleStateEnum.CERTIFIED);
1242 Either<Component, StorageOperationStatus> leftServiceOp = Either.left(service);
1243 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(leftServiceOp);
1244 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1245 .thenReturn(leftServiceOp);
1246 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1247 .thenReturn(StorageOperationStatus.OK);
1248 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1249 .thenReturn(StorageOperationStatus.OK);
1251 result = componentInstanceBusinessLogic
1252 .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId,
1254 assertNotNull(result);
1256 service.setLastUpdaterUserId(oldServiceLastUpdatedUserId);
1257 resource.setLifecycleState(oldResourceLifeCycle);
1259 assertThat(result.isLeft()).isFalse();
1263 void testCreateOrUpdateAttributeValueForCopyPaste() {
1264 ComponentInstance serviceComponentInstance = createComponetInstanceFromComponent(service);
1265 ComponentInstanceAttribute attribute = new ComponentInstanceAttribute();
1266 attribute.setType("string");
1267 attribute.setUniqueId("testCreateOrUpdateAttributeValueForCopyPaste");
1268 SchemaDefinition def = Mockito.mock(SchemaDefinition.class);
1269 attribute.setSchema(def);
1270 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1271 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1272 service.setLastUpdaterUserId(USER_ID);
1273 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1275 Map<String, List<ComponentInstanceAttribute>> instAttrsMap = new HashMap<>();
1276 List<ComponentInstanceAttribute> instAttrsList = new ArrayList<>();
1277 ComponentInstanceAttribute prop = new ComponentInstanceAttribute();
1278 prop.setUniqueId(attribute.getUniqueId());
1279 instAttrsList.add(prop);
1280 instAttrsMap.put(toInstance.getUniqueId(), instAttrsList);
1281 service.setComponentInstancesAttributes(instAttrsMap);
1283 Either<Component, StorageOperationStatus> serviceEitherLeft = Either.left(service);
1284 when(toscaOperationFacade.getToscaElement(serviceComponentInstance.getUniqueId(), JsonParseFlagEnum.ParseAll))
1285 .thenReturn(serviceEitherLeft);
1286 when(toscaOperationFacade.updateComponentInstanceAttribute(service, toInstance.getUniqueId(), attribute))
1287 .thenReturn(StorageOperationStatus.OK);
1288 when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(service))
1289 .thenReturn(serviceEitherLeft);
1291 Either<ComponentInstanceAttribute, ResponseFormat> result = Deencapsulation
1292 .invoke(componentInstanceBusinessLogic,
1293 "createOrUpdateAttributeValueForCopyPaste",
1294 ComponentTypeEnum.SERVICE,
1295 serviceComponentInstance
1297 toInstance.getUniqueId(), attribute,
1299 assertNotNull(result);
1301 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1302 service.setLifecycleState(oldLifeCycleState);
1304 assertThat(result.isLeft()).isTrue();
1305 ComponentInstanceAttribute resultProp = result.left().value();
1306 assertEquals(1, resultProp.getPath().size());
1307 assertEquals(resultProp.getPath().get(0), toInstance.getUniqueId());
1311 void testUpdateComponentInstanceProperty() {
1313 String containerComponentId = service.getUniqueId();
1314 String componentInstanceId = "dummy_id";
1315 ComponentInstanceProperty property = Mockito.mock(ComponentInstanceProperty.class);
1317 Either<Component, StorageOperationStatus> getComponent = Either.left(service);
1318 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(getComponent);
1319 StorageOperationStatus status = StorageOperationStatus.OK;
1320 when(toscaOperationFacade.updateComponentInstanceProperty(service, componentInstanceId, property))
1321 .thenReturn(status);
1322 Either<Component, StorageOperationStatus> updateContainerRes = Either.left(service);
1323 when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(service))
1324 .thenReturn(updateContainerRes);
1326 Either<String, ResponseFormat> result = Deencapsulation.invoke(componentInstanceBusinessLogic,
1327 "updateComponentInstanceProperty", containerComponentId, componentInstanceId, property);
1328 assertNotNull(result);
1329 assertThat(result.isLeft()).isTrue();
1333 void testGetInputListDefaultValue() {
1334 Component component = service;
1335 String inputId = "dummy_id";
1336 String defaultValue = "dummy_default_value";
1337 List<InputDefinition> newInputs = new ArrayList<>();
1338 InputDefinition in = new InputDefinition();
1339 in.setUniqueId(inputId);
1340 in.setDefaultValue(defaultValue);
1342 List<InputDefinition> oldInputs = service.getInputs();
1343 service.setInputs(newInputs);
1345 Either<String, ResponseFormat> result =
1346 Deencapsulation.invoke(componentInstanceBusinessLogic, "getInputListDefaultValue", component, inputId);
1348 service.setInputs(oldInputs);
1350 assertEquals(result.left().value(), defaultValue);
1354 void testBatchDeleteComponentInstanceFailureWrongType() {
1355 Map<String, List<String>> result;
1356 List<String> componentInstanceIdList = new ArrayList<>();
1357 String containerComponentParam = "WRONG_TYPE";
1358 String containerComponentId = "containerComponentId";
1359 String componentInstanceId = "componentInstanceId";
1360 componentInstanceIdList.add(componentInstanceId);
1361 Map<String, List<String>> deleteErrorMap = new HashMap<>();
1362 List<String> deleteErrorIds = new ArrayList<>();
1363 deleteErrorIds.add(componentInstanceId);
1364 deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
1365 Either<Component, StorageOperationStatus> cont = Either.left(service);
1366 when(componentsUtils.convertFromStorageResponse(eq(StorageOperationStatus.NOT_FOUND), eq(null)))
1367 .thenReturn(ActionStatus.GENERAL_ERROR);
1368 when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class)))
1372 result = componentInstanceBusinessLogic
1373 .batchDeleteComponentInstance(containerComponentParam, containerComponentId, componentInstanceIdList,
1375 assertNotNull(result);
1376 assertEquals(deleteErrorMap, result);
1377 } catch (ComponentException e) {
1378 assertEquals(e.getActionStatus().toString(), StorageOperationStatus.GENERAL_ERROR.toString());
1383 void testBatchDeleteComponentInstanceFailureCompIds() {
1384 String containerComponentParam = ComponentTypeEnum.SERVICE_PARAM_NAME;
1385 String containerComponentId = "containerComponentId";
1386 String componentInstanceId = "componentInstanceId";
1387 List<String> componentInstanceIdList = new ArrayList<>();
1388 componentInstanceIdList.add(componentInstanceId);
1389 Map<String, List<String>> deleteErrorMap = new HashMap<>();
1390 List<String> deleteErrorIds = new ArrayList<>();
1391 deleteErrorIds.add(componentInstanceId);
1392 deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
1394 Either<Component, StorageOperationStatus> err = Either.right(StorageOperationStatus.GENERAL_ERROR);
1395 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1399 Map<String, List<String>> result = componentInstanceBusinessLogic.batchDeleteComponentInstance(
1400 containerComponentParam, containerComponentId, componentInstanceIdList, USER_ID);
1401 assertNotNull(result);
1402 assertEquals(deleteErrorMap, result);
1403 } catch (ComponentException e) {
1404 assertEquals(e.getActionStatus().toString(), StorageOperationStatus.GENERAL_ERROR.toString());
1409 void testBatchDeleteComponentInstanceSuccess() {
1410 Map<String, List<String>> result;
1411 String containerComponentParam = ComponentTypeEnum.SERVICE_PARAM_NAME;
1412 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1413 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1414 service.setLastUpdaterUserId(USER_ID);
1415 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1416 String containerComponentId = service.getUniqueId();
1417 String componentInstanceId = TO_INSTANCE_ID;
1418 List<String> componentInstanceIdList = new ArrayList<>();
1419 componentInstanceIdList.add(componentInstanceId);
1420 Map<String, List<String>> deleteErrorMap = new HashMap<>();
1421 List<String> deleteErrorIds = new ArrayList<>();
1422 deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
1424 Either<Component, StorageOperationStatus> cont = Either.left(service);
1425 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1426 .thenReturn(StorageOperationStatus.OK);
1427 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1428 .thenReturn(StorageOperationStatus.OK);
1429 ImmutablePair<Component, String> pair = new ImmutablePair<>(resource, TO_INSTANCE_ID);
1430 Either<ImmutablePair<Component, String>, StorageOperationStatus> result2 = Either.left(pair);
1431 when(toscaOperationFacade.deleteComponentInstanceFromTopologyTemplate(service, componentInstanceId))
1432 .thenReturn(result2);
1433 when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
1435 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
1437 result = componentInstanceBusinessLogic
1438 .batchDeleteComponentInstance(containerComponentParam, containerComponentId,
1439 componentInstanceIdList, USER_ID);
1440 assertNotNull(result);
1442 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1443 service.setLifecycleState(oldLifeCycleState);
1444 assertEquals(deleteErrorMap, result);
1448 void testDissociateRIFromRIFailDissociate() {
1450 List<RequirementCapabilityRelDef> result;
1451 RequirementCapabilityRelDef ref = new RequirementCapabilityRelDef();
1452 ref.setFromNode(FROM_INSTANCE_ID);
1453 ref.setToNode(TO_INSTANCE_ID);
1454 List<CapabilityRequirementRelationship> relationships = new ArrayList<>();
1455 CapabilityRequirementRelationship relationship = new CapabilityRequirementRelationship();
1456 RelationshipInfo ri = new RelationshipInfo();
1457 ri.setRequirement(REQUIREMENT_NAME);
1458 relationship.setRelation(ri);
1459 relationships.add(relationship);
1460 ref.setRelationships(relationships);
1461 List<RequirementCapabilityRelDef> requirementDefList = new ArrayList<>();
1462 requirementDefList.add(ref);
1463 ComponentTypeEnum componentTypeEnum = service.getComponentType();
1464 String componentId = service.getUniqueId();
1465 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1466 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1467 service.setLastUpdaterUserId(USER_ID);
1468 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1470 Either<Component, StorageOperationStatus> cont = Either.left(service);
1471 when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
1473 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1474 .thenReturn(StorageOperationStatus.OK);
1475 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1476 .thenReturn(StorageOperationStatus.OK);
1477 Either<RequirementCapabilityRelDef, StorageOperationStatus> resultEither;
1478 resultEither = Either.right(StorageOperationStatus.OK);
1479 when(componentsUtils.convertFromStorageResponseForResourceInstance(eq(StorageOperationStatus.OK), eq(true)))
1480 .thenReturn(ActionStatus.GENERAL_ERROR);
1481 when(toscaOperationFacade.dissociateResourceInstances(componentId, ref)).thenReturn(resultEither);
1484 result = componentInstanceBusinessLogic
1485 .batchDissociateRIFromRI(componentId, USER_ID, requirementDefList, componentTypeEnum);
1486 assertNotNull(result);
1487 assertEquals(new ArrayList<>(), result);
1488 } catch (ComponentException e) {
1489 assertEquals(e.getActionStatus().toString(), StorageOperationStatus.GENERAL_ERROR.toString());
1492 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1493 service.setLifecycleState(oldLifeCycleState);
1498 void testDissociateRIFromRISuccess() {
1500 List<RequirementCapabilityRelDef> result;
1501 RequirementCapabilityRelDef ref = new RequirementCapabilityRelDef();
1502 List<RequirementCapabilityRelDef> requirementDefList = new ArrayList<>();
1503 requirementDefList.add(ref);
1504 ComponentTypeEnum componentTypeEnum = service.getComponentType();
1505 String componentId = service.getUniqueId();
1506 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1507 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1508 service.setLastUpdaterUserId(USER_ID);
1509 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1511 Either<Component, StorageOperationStatus> cont = Either.left(service);
1512 when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
1514 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1515 .thenReturn(StorageOperationStatus.OK);
1516 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1517 .thenReturn(StorageOperationStatus.OK);
1518 Either<RequirementCapabilityRelDef, StorageOperationStatus> resultEither;
1519 resultEither = Either.left(ref);
1520 when(toscaOperationFacade.dissociateResourceInstances(componentId, ref)).thenReturn(resultEither);
1522 result = componentInstanceBusinessLogic
1523 .batchDissociateRIFromRI(componentId, USER_ID, requirementDefList, componentTypeEnum);
1524 assertNotNull(result);
1526 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1527 service.setLifecycleState(oldLifeCycleState);
1529 assertEquals(requirementDefList, result);
1533 void testGetComponentInstancePropertyByPolicyId_success() {
1534 Optional<ComponentInstanceProperty> propertyCandidate =
1535 getComponentInstanceProperty(PROP_NAME);
1537 assertThat(propertyCandidate).isPresent();
1538 assertEquals(PROP_NAME, propertyCandidate.get().getName());
1542 void testGetComponentInstancePropertyByPolicyId_failure() {
1543 Optional<ComponentInstanceProperty> propertyCandidate =
1544 getComponentInstanceProperty(NON_EXIST_NAME);
1546 assertEquals(propertyCandidate, Optional.empty());
1549 private Optional<ComponentInstanceProperty> getComponentInstanceProperty(String propertyName) {
1550 ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
1551 componentInstanceProperty.setName(propertyName);
1553 PolicyDefinition policyDefinition = getPolicyDefinition();
1554 componentInstanceProperty.setGetPolicyValues(policyDefinition.getGetPolicyValues());
1556 service.setComponentInstancesProperties(
1557 Collections.singletonMap(COMPONENT_INSTANCE_ID, Collections.singletonList(componentInstanceProperty)));
1559 return componentInstanceBusinessLogic.getComponentInstancePropertyByPolicyId(service, policyDefinition);
1562 private PolicyDefinition getPolicyDefinition() {
1563 PolicyDefinition policyDefinition = new PolicyDefinition();
1564 policyDefinition.setInstanceUniqueId(COMPONENT_INSTANCE_ID);
1565 policyDefinition.setName(PROP_NAME);
1567 GetPolicyValueDataDefinition getPolicy = new GetPolicyValueDataDefinition();
1568 getPolicy.setPropertyName(PROP_NAME);
1570 List<GetPolicyValueDataDefinition> getPolicies = new ArrayList<>();
1571 getPolicies.add(getPolicy);
1572 policyDefinition.setGetPolicyValues(getPolicies);
1574 return policyDefinition;
1577 private ComponentInstance createComponetInstanceFromComponent(Component component) {
1578 ComponentInstance componentInst = new ComponentInstance();
1579 componentInst.setUniqueId(component.getUniqueId());
1580 componentInst.setComponentUid(component.getUniqueId() + "_test");
1581 componentInst.setPosX("10");
1582 componentInst.setPosY("10");
1583 componentInst.setCapabilities(component.getCapabilities());
1584 componentInst.setRequirements(component.getRequirements());
1585 componentInst.setArtifacts(component.getArtifacts());
1586 componentInst.setDeploymentArtifacts(component.getDeploymentArtifacts());
1587 return componentInst;
1590 // Prepare ComponentInstance & Resource objects used in createComponentInstance() tests
1591 private Pair<ComponentInstance, Resource> prepareResourcesForCreateComponentInstanceTest() {
1592 ComponentInstance instanceToBeCreated = new ComponentInstance();
1593 instanceToBeCreated.setName(COMPONENT_INSTANCE_NAME);
1594 instanceToBeCreated.setUniqueId(COMPONENT_INSTANCE_ID);
1595 instanceToBeCreated.setComponentUid(ORIGIN_COMPONENT_ID);
1596 instanceToBeCreated.setOriginType(OriginTypeEnum.VF);
1598 Resource originComponent = new Resource();
1599 originComponent.setLifecycleState(LifecycleStateEnum.CERTIFIED);
1600 originComponent.setResourceType(ResourceTypeEnum.VF);
1601 originComponent.setVersion(ORIGIN_COMPONENT_VERSION);
1602 originComponent.setIcon(ICON_NAME);
1604 return Pair.of(instanceToBeCreated, originComponent);
1607 // Common part for testing component instance name validation
1608 private void testCreateComponentInstanceNameValidationFailure(String ciName) {
1609 ComponentInstance ci = new ComponentInstance();
1612 // Stub for getting component
1613 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
1614 .thenReturn(Either.left(service));
1616 // Expecting ByActionStatusComponentException
1617 ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
1618 componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1620 assertEquals(ActionStatus.INVALID_COMPONENT_NAME, e.getActionStatus());
1624 Iterable<DynamicTest> testCreateComponentInstanceNameValidationFailureFactory() {
1625 String longName = String.join("", Collections.nCopies(ValidationUtils.COMPONENT_NAME_MAX_LENGTH + 1, "x"));
1626 String invalidName = "componentInstance#name";
1627 return Arrays.asList(
1628 dynamicTest("instance name is empty", () ->
1629 testCreateComponentInstanceNameValidationFailure("")),
1630 dynamicTest("instance name is too long", () ->
1631 testCreateComponentInstanceNameValidationFailure(longName)),
1632 dynamicTest("instance name includes invalid character", () ->
1633 testCreateComponentInstanceNameValidationFailure(invalidName))
1638 void testCreateComponentInstanceFailToGetComponent() {
1639 ComponentInstance ci = prepareResourcesForCreateComponentInstanceTest().getLeft();
1641 // Stub for getting component
1642 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
1643 .thenReturn(Either.left(service));
1644 when(toscaOperationFacade.getToscaFullElement(eq(ORIGIN_COMPONENT_ID)))
1645 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1646 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND, ComponentTypeEnum.RESOURCE))
1647 .thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
1649 ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
1650 componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1652 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.RESOURCE_NOT_FOUND);
1656 void testCreateComponentInstanceFailureInvalidState() {
1657 Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
1658 ComponentInstance ci = p.getLeft();
1659 Resource originComponent = p.getRight();
1660 originComponent.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1662 // Stub for getting component
1663 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
1664 .thenReturn(Either.left(service));
1665 when(toscaOperationFacade.getToscaFullElement(eq(ORIGIN_COMPONENT_ID)))
1666 .thenReturn(Either.left(originComponent));
1668 ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
1669 componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1671 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.CONTAINER_CANNOT_CONTAIN_COMPONENT_IN_STATE);
1675 void testCreateComponentInstanceFailureArchived() {
1676 Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
1677 ComponentInstance ci = p.getLeft();
1678 Resource originComponent = p.getRight();
1679 originComponent.setArchived(Boolean.TRUE);
1681 // Stub for getting component
1682 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
1683 .thenReturn(Either.left(service));
1684 when(toscaOperationFacade.getToscaFullElement(eq(ORIGIN_COMPONENT_ID)))
1685 .thenReturn(Either.left(originComponent));
1687 ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
1688 componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1690 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.COMPONENT_IS_ARCHIVED);
1694 void testCreateComponentInstanceFailureInvalidOriginType() {
1695 Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
1696 ComponentInstance ci = p.getLeft();
1697 Resource originComponent = p.getRight();
1698 ci.setOriginType(OriginTypeEnum.VFC); // Set different type from origin
1700 // Stub for getting component
1701 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
1702 .thenReturn(Either.left(service));
1703 when(toscaOperationFacade.getToscaFullElement(eq(ORIGIN_COMPONENT_ID)))
1704 .thenReturn(Either.left(originComponent));
1706 final ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
1707 componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1709 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.INVALID_CONTENT);
1713 void testCreateComponentInstanceFailureCannotContainInstance() {
1714 final Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
1715 final ComponentInstance ci = p.getLeft();
1716 final Resource originComponent = p.getRight();
1718 // Stub for getting component
1719 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
1720 .thenReturn(Either.left(service));
1721 when(toscaOperationFacade.getToscaFullElement(eq(ORIGIN_COMPONENT_ID)))
1722 .thenReturn(Either.left(originComponent));
1723 // Assume services cannot contain VF resource
1724 when(containerInstanceTypeData.isAllowedForServiceComponent(eq(ResourceTypeEnum.VF)))
1727 ByActionStatusComponentException actualException = assertThrows(ByActionStatusComponentException.class, () -> {
1728 componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1730 assertThat(actualException.getActionStatus()).isEqualTo(ActionStatus.CONTAINER_CANNOT_CONTAIN_INSTANCE);
1731 verify(containerInstanceTypeData, times(1)).isAllowedForServiceComponent(eq(ResourceTypeEnum.VF));
1734 final Resource resource = createResource();
1735 resource.setResourceType(ResourceTypeEnum.VF);
1736 resource.setLastUpdaterUserId(USER_ID);
1738 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
1739 .thenReturn(Either.left(resource));
1740 when(toscaOperationFacade.getToscaFullElement(eq(ORIGIN_COMPONENT_ID)))
1741 .thenReturn(Either.left(originComponent));
1742 when(containerInstanceTypeData.isAllowedForResourceComponent(eq(ResourceTypeEnum.VF), eq(ResourceTypeEnum.VF)))
1744 actualException = assertThrows(ByActionStatusComponentException.class, () -> {
1745 componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.RESOURCE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1748 assertThat(actualException.getActionStatus()).isEqualTo(ActionStatus.CONTAINER_CANNOT_CONTAIN_INSTANCE);
1752 void testCreateComponentInstanceFailureAddToGraph() {
1753 final Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
1754 final ComponentInstance ci = p.getLeft();
1755 final Resource originComponent = p.getRight();
1757 // TODO Refactor createComponentInstance() method and reduce these mocks
1758 // not to target the internal details too much
1759 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
1760 .thenReturn(Either.left(service));
1761 when(toscaOperationFacade.getToscaFullElement(eq(ORIGIN_COMPONENT_ID)))
1762 .thenReturn(Either.left(originComponent));
1763 when(containerInstanceTypeData.isAllowedForServiceComponent(eq(ResourceTypeEnum.VF)))
1765 Mockito.doNothing().when(compositionBusinessLogic).validateAndSetDefaultCoordinates(ci);
1766 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service))
1767 .thenReturn(StorageOperationStatus.OK);
1768 when(toscaOperationFacade.addComponentInstanceToTopologyTemplate(service, originComponent, ci, false, user))
1769 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
1770 when(componentsUtils.convertFromStorageResponseForResourceInstance(StorageOperationStatus.BAD_REQUEST, true))
1771 .thenReturn(ActionStatus.INVALID_CONTENT);
1772 when(componentsUtils.getResponseFormatForResourceInstance(ActionStatus.INVALID_CONTENT, "", null))
1773 .thenReturn(new ResponseFormat());
1774 when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
1775 when(graphLockOperation.unlockComponent(COMPONENT_ID, NodeTypeEnum.Service))
1776 .thenReturn(StorageOperationStatus.OK);
1778 assertThrows(ByResponseFormatComponentException.class, () -> {
1779 componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1781 verify(containerInstanceTypeData, times(1))
1782 .isAllowedForServiceComponent(eq(ResourceTypeEnum.VF));
1783 verify(compositionBusinessLogic, times(1)).validateAndSetDefaultCoordinates(ci);
1784 verify(toscaOperationFacade, times(1))
1785 .addComponentInstanceToTopologyTemplate(service, originComponent, ci, false, user);
1786 verify(graphLockOperation, times(1)).unlockComponent(COMPONENT_ID, NodeTypeEnum.Service);
1790 void testCreateComponentInstanceSuccess() {
1791 final Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
1792 final ComponentInstance instanceToBeCreated = p.getLeft();
1793 final Resource originComponent = p.getRight();
1795 final Service updatedService = new Service();
1796 updatedService.setComponentInstances(Collections.singletonList(instanceToBeCreated));
1797 updatedService.setUniqueId(service.getUniqueId());
1799 // TODO Refactor createComponentInstance() method and reduce these mocks
1800 // not to target the internal details too much
1801 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
1802 .thenReturn(Either.left(service));
1803 when(toscaOperationFacade.getToscaFullElement(eq(ORIGIN_COMPONENT_ID)))
1804 .thenReturn(Either.left(originComponent));
1805 when(containerInstanceTypeData.isAllowedForServiceComponent(eq(ResourceTypeEnum.VF)))
1807 Mockito.doNothing().when(compositionBusinessLogic).validateAndSetDefaultCoordinates(instanceToBeCreated);
1808 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service))
1809 .thenReturn(StorageOperationStatus.OK);
1810 when(toscaOperationFacade.addComponentInstanceToTopologyTemplate(service, originComponent, instanceToBeCreated, false, user))
1811 .thenReturn(Either.left(new ImmutablePair<>(updatedService, COMPONENT_INSTANCE_ID)));
1812 when(artifactsBusinessLogic.getArtifacts(
1813 ORIGIN_COMPONENT_ID, NodeTypeEnum.Resource, ArtifactGroupTypeEnum.DEPLOYMENT, null))
1814 .thenReturn(Either.left(new HashMap<>()));
1815 when(toscaOperationFacade
1816 .addInformationalArtifactsToInstance(service.getUniqueId(), instanceToBeCreated, originComponent.getArtifacts()))
1817 .thenReturn(StorageOperationStatus.OK);
1818 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
1819 when(graphLockOperation.unlockComponent(COMPONENT_ID, NodeTypeEnum.Service))
1820 .thenReturn(StorageOperationStatus.OK);
1822 final ComponentInstance result = componentInstanceBusinessLogic.createComponentInstance(
1823 ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, instanceToBeCreated);
1824 assertThat(result).isEqualTo(instanceToBeCreated);
1825 assertThat(instanceToBeCreated.getComponentVersion()).isEqualTo(originComponent.getVersion());
1826 assertThat(instanceToBeCreated.getIcon()).isEqualTo(originComponent.getIcon());
1827 verify(containerInstanceTypeData, times(1))
1828 .isAllowedForServiceComponent(eq(ResourceTypeEnum.VF));
1829 verify(compositionBusinessLogic, times(1)).validateAndSetDefaultCoordinates(instanceToBeCreated);
1830 verify(toscaOperationFacade, times(1))
1831 .addComponentInstanceToTopologyTemplate(service, originComponent, instanceToBeCreated, false, user);
1832 // Check graph db change was committed
1833 verify(janusGraphDao, times(1)).commit();
1837 void testCreateComponentInstanceServiceSubstitutionSuccess() {
1838 ComponentInstance instanceToBeCreated = createServiceSubstitutionComponentInstance();
1839 Service originService = createServiceSubstitutionOriginService();
1840 Component serviceBaseComponent = createServiceSubstitutionServiceDerivedFromComponent();
1842 Service updatedService = new Service();
1843 updatedService.setComponentInstances(Collections.singletonList(instanceToBeCreated));
1844 updatedService.setUniqueId(service.getUniqueId());
1846 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
1847 .thenReturn(Either.left(service));
1848 when(toscaOperationFacade.getToscaFullElement(eq(ORIGIN_COMPONENT_ID)))
1849 .thenReturn(Either.left(originService));
1850 when(toscaOperationFacade.getLatestByToscaResourceName(eq(originService.getDerivedFromGenericType())))
1851 .thenReturn(Either.left(serviceBaseComponent));
1852 when(toscaOperationFacade.getToscaElement(eq(ORIGIN_COMPONENT_ID), any(ComponentParametersView.class)))
1853 .thenReturn(Either.left(originService));
1854 Mockito.doNothing().when(compositionBusinessLogic).validateAndSetDefaultCoordinates(instanceToBeCreated);
1855 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service))
1856 .thenReturn(StorageOperationStatus.OK);
1857 when(toscaOperationFacade.addComponentInstanceToTopologyTemplate(service, serviceBaseComponent, instanceToBeCreated, false, user))
1858 .thenReturn(Either.left(new ImmutablePair<>(updatedService, COMPONENT_INSTANCE_ID)));
1859 when(artifactsBusinessLogic.getArtifacts(
1860 "baseComponentId", NodeTypeEnum.Resource, ArtifactGroupTypeEnum.DEPLOYMENT, null))
1861 .thenReturn(Either.left(new HashMap<>()));
1862 when(toscaOperationFacade
1863 .addInformationalArtifactsToInstance(service.getUniqueId(), instanceToBeCreated, originService.getArtifacts()))
1864 .thenReturn(StorageOperationStatus.OK);
1865 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
1866 when(graphLockOperation.unlockComponent(COMPONENT_ID, NodeTypeEnum.Service))
1867 .thenReturn(StorageOperationStatus.OK);
1869 ComponentInstance result = componentInstanceBusinessLogic.createComponentInstance(
1870 ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, instanceToBeCreated);
1871 assertThat(result).isEqualTo(instanceToBeCreated);
1872 assertThat(instanceToBeCreated.getComponentVersion()).isEqualTo(originService.getVersion());
1873 assertThat(instanceToBeCreated.getIcon()).isEqualTo(originService.getIcon());
1874 verify(compositionBusinessLogic, times(1)).validateAndSetDefaultCoordinates(instanceToBeCreated);
1875 verify(toscaOperationFacade, times(1))
1876 .addComponentInstanceToTopologyTemplate(service, serviceBaseComponent, instanceToBeCreated, false, user);
1877 // Check graph db change was committed
1878 verify(janusGraphDao, times(1)).commit();
1881 private ComponentInstance createServiceSubstitutionComponentInstance() {
1882 final ComponentInstance instanceToBeCreated = new ComponentInstance();
1883 instanceToBeCreated.setName(COMPONENT_INSTANCE_NAME);
1884 instanceToBeCreated.setUniqueId(COMPONENT_INSTANCE_ID);
1885 instanceToBeCreated.setComponentUid(ORIGIN_COMPONENT_ID);
1886 instanceToBeCreated.setOriginType(OriginTypeEnum.ServiceSubstitution);
1888 return instanceToBeCreated;
1891 private Service createServiceSubstitutionOriginService() {
1892 final Service originComponent = new Service();
1893 originComponent.setLifecycleState(LifecycleStateEnum.CERTIFIED);
1894 originComponent.setVersion(ORIGIN_COMPONENT_VERSION);
1895 originComponent.setIcon(ICON_NAME);
1896 originComponent.setDerivedFromGenericType("org.openecomp.resource.abstract.nodes.service");
1897 originComponent.setName("myService");
1898 return originComponent;
1902 private Component createServiceSubstitutionServiceDerivedFromComponent() {
1903 final Resource component = new Resource();
1904 component.setLifecycleState(LifecycleStateEnum.CERTIFIED);
1905 component.setVersion(ORIGIN_COMPONENT_VERSION);
1906 component.setIcon(ICON_NAME);
1907 component.setToscaResourceName("org.openecomp.resource.abstract.nodes.service");
1908 component.setUniqueId("baseComponentId");