Implement Attributes/Outputs BE (part 2)
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / ComponentInstanceBusinessLogicTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.impl;
22
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;
38
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;
46 import java.util.Map;
47 import java.util.Optional;
48 import java.util.Set;
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;
123
124 /**
125  * The test suite designed for test functionality of ComponentInstanceBusinessLogic class
126  */
127
128 @ExtendWith(MockitoExtension.class)
129 @MockitoSettings(strictness = Strictness.LENIENT)
130 class ComponentInstanceBusinessLogicTest {
131
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";
158
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);
163
164     @InjectMocks
165     private static ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
166     @Mock
167     private ComponentInstancePropInput componentInstancePropInput;
168     @Mock
169     private ArtifactsBusinessLogic artifactsBusinessLogic;
170     @Mock
171     private ComponentsUtils componentsUtils;
172     @Mock
173     private ToscaOperationFacade toscaOperationFacade;
174     @Mock
175     private ForwardingPathOperation forwardingPathOperation;
176     @Mock
177     private User user;
178     @Mock
179     private UserValidations userValidations;
180     @Mock
181     private GraphLockOperation graphLockOperation;
182     @Mock
183     private JanusGraphDao janusGraphDao;
184     @Mock
185     private ApplicationDataTypeCache dataTypeCache;
186     @Mock
187     private PropertyOperation propertyOperation;
188     @Mock
189     private ContainerInstanceTypesData containerInstanceTypeData;
190     @Mock
191     private CompositionBusinessLogic compositionBusinessLogic;
192
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;
200
201     @BeforeEach
202     void init() {
203         MockitoAnnotations.initMocks(this);
204         stubMethods();
205         createComponents();
206     }
207
208     @Test
209     void testGetRelationByIdSuccess() {
210         getServiceRelationByIdSuccess(service);
211         getServiceRelationByIdSuccess(resource);
212     }
213
214     @Test
215     void testGetRelationByIdUserValidationFailure() {
216         getServiceRelationByIdUserValidationFailure(service);
217         getServiceRelationByIdUserValidationFailure(resource);
218     }
219
220     @Test
221     void testGetRelationByIdComponentNotFoundFailure() {
222         getRelationByIdComponentNotFoundFailure(service);
223         getRelationByIdComponentNotFoundFailure(resource);
224     }
225
226     @Test
227     void testForwardingPathOnVersionChange() {
228         getforwardingPathOnVersionChange();
229     }
230
231     @Test
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();
238     }
239
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);
246
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);
257
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);
266
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));
272
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());
279
280     }
281
282     @Test
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);
293
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);
300
301         Map<String, DataTypeDefinition> types = new HashMap<>();
302         DataTypeDefinition dataTypeDef = new DataTypeDefinition();
303         types.put("string", dataTypeDef);
304
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)));
315
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);
333
334         Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = componentInstanceBusinessLogic
335             .createOrUpdatePropertiesValues(
336                 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
337         assertThat(responseFormatEither.left().value()).isEqualTo(properties);
338     }
339
340     @Test
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);
351
352         List<ComponentInstanceProperty> origProperties = new ArrayList<>();
353
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)));
364
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);
371
372         try {
373             componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
374                 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
375         } catch (ComponentException e) {
376             assertThat(e.getActionStatus()).isEqualTo(ActionStatus.PROPERTY_NOT_FOUND);
377         }
378
379     }
380
381     @Test
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);
392
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);
399
400         Map<String, DataTypeDefinition> types = new HashMap<>();
401         DataTypeDefinition dataTypeDef = new DataTypeDefinition();
402         types.put("string", dataTypeDef);
403
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)));
414
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);
424
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);
429     }
430
431     @Test
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);
441
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);
448
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)));
459
460         when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
461             .thenReturn(Either.left(component));
462         when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
463             .thenReturn(StorageOperationStatus.OK);
464
465         try {
466             componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
467                 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
468         } catch (ComponentException e) {
469             assertThat(e.getActionStatus()).isEqualTo(ActionStatus.MISSING_PROPERTY_NAME);
470         }
471     }
472
473     @Test
474     void testDeleteForwardingPathsWhenComponentinstanceDeleted() {
475
476         ComponentTypeEnum containerComponentType = ComponentTypeEnum.findByParamName("services");
477         String containerComponentID = "Service-comp";
478         String componentInstanceID = "NodeA1";
479         Service component = new Service();
480         component
481             .setComponentInstances(Arrays.asList(createComponentInstance("NodeA2"), createComponentInstance("NodeB2"),
482                 createComponentInstance(componentInstanceID)));
483
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());
497     }
498
499     @Test
500     void testAddComponentInstanceDeploymentArtifacts() {
501
502         Component containerComponent = new Service();
503         ComponentInstance componentInstance = new ComponentInstance();
504         componentInstance.setUniqueId(COMPONENT_INSTANCE_ID);
505         Component originComponent = fillOriginComponent(new Resource());
506
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());
519
520         Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getResourceDeploymentArtifacts = Either
521             .left(artifacts);
522
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);
528
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);
539
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<>(),
545                 new HashMap<>()))
546             .thenReturn(StorageOperationStatus.OK);
547         when(toscaOperationFacade
548             .addInformationalArtifactsToInstance(containerComponent.getUniqueId(), componentInstance, null))
549             .thenReturn(StorageOperationStatus.OK);
550
551         ActionStatus status = componentInstanceBusinessLogic.addComponentInstanceArtifacts(containerComponent,
552             componentInstance, originComponent, user, null);
553
554         assertThat(status).isEqualTo(ActionStatus.OK);
555
556     }
557
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;
565     }
566
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;
575     }
576
577     private ComponentInstance createComponentInstance(String path1) {
578         ComponentInstance componentInstance = new ComponentInstance();
579         componentInstance.setName(path1);
580         return componentInstance;
581     }
582
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);
594
595         return forwardingPath;
596     }
597
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;
613     }
614
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();
624     }
625
626     private void getServiceRelationByIdUserValidationFailure(Component component) {
627         when(userValidations.validateUserExists(eq(USER_ID)))
628             .thenThrow(new ByActionStatusComponentException(ActionStatus.USER_NOT_FOUND));
629         try {
630             componentInstanceBusinessLogic
631                 .getRelationById(COMPONENT_ID, RELATION_ID, USER_ID, component.getComponentType());
632         } catch (ByActionStatusComponentException e) {
633             assertSame(ActionStatus.USER_NOT_FOUND, e.getActionStatus());
634         }
635     }
636
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);
641
642         Either<RequirementCapabilityRelDef, ResponseFormat> response = componentInstanceBusinessLogic
643             .getRelationById(COMPONENT_ID,
644                 RELATION_ID, USER_ID,
645                 component.getComponentType());
646         assertThat(response.isRight()).isTrue();
647     }
648
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);
654     }
655
656     private void createComponents() {
657         createRelation();
658         createInstances();
659         createProperties();
660         createInputs();
661         createService();
662         resource = createResource();
663     }
664
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);
674         return resource;
675     }
676
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);
687
688         Map<String, List<ComponentInstanceProperty>> ciPropertyMap = new HashMap<>();
689         ciPropertyMap.put(TO_INSTANCE_ID, ciPropertyList);
690         service.setComponentInstancesProperties(ciPropertyMap);
691
692         Map<String, List<ComponentInstanceInput>> ciInputMap = new HashMap<>();
693         ciInputMap.put(TO_INSTANCE_ID, ciInputList);
694         service.setComponentInstancesInputs(ciInputMap);
695     }
696
697     private void createInstances() {
698         toInstance = new ComponentInstance();
699         toInstance.setUniqueId(TO_INSTANCE_ID);
700         toInstance.setName(TO_INSTANCE_NAME);
701
702         fromInstance = new ComponentInstance();
703         fromInstance.setUniqueId(FROM_INSTANCE_ID);
704
705         CapabilityDataDefinition capability = new CapabilityDataDefinition();
706         capability.setOwnerId(CAPABILITY_OWNER_ID);
707         capability.setUniqueId(CAPABILITY_UID);
708         capability.setName(CAPABILITY_NAME);
709
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));
716
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);
722
723         Map<String, List<RequirementDefinition>> requirements = new HashMap<>();
724         requirements.put(requirement.getCapability(), Lists.newArrayList(new RequirementDefinition(requirement)));
725
726         toInstance.setCapabilities(capabilities);
727         fromInstance.setRequirements(requirements);
728
729     }
730
731     private void createRelation() {
732
733         relation = new RequirementCapabilityRelDef();
734         CapabilityRequirementRelationship relationship = new CapabilityRequirementRelationship();
735         RelationshipInfo relationInfo = new RelationshipInfo();
736         relationInfo.setId(RELATION_ID);
737         relationship.setRelation(relationInfo);
738
739         relation.setRelationships(Lists.newArrayList(relationship));
740         relation.setToNode(TO_INSTANCE_ID);
741         relation.setFromNode(FROM_INSTANCE_ID);
742
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);
752     }
753
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);
767     }
768
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);
783     }
784
785     private ComponentInstanceBusinessLogic createTestSubject() {
786         return componentInstanceBusinessLogic;
787     }
788
789     @Test
790     void testChangeServiceProxyVersion() {
791         ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
792
793         Either<ComponentInstance, ResponseFormat> result;
794
795         // default test
796         componentInstanceBusinessLogic = createTestSubject();
797         result = componentInstanceBusinessLogic.changeServiceProxyVersion();
798         assertNotNull(result);
799     }
800
801     @Test
802     void testCreateServiceProxy() {
803         ComponentInstanceBusinessLogic testSubject;
804         Either<ComponentInstance, ResponseFormat> result;
805
806         // default test
807         testSubject = createTestSubject();
808         result = testSubject.createServiceProxy();
809         assertNotNull(result);
810     }
811
812     @Test
813     void testDeleteServiceProxy() {
814         ComponentInstanceBusinessLogic testSubject;
815
816         Either<ComponentInstance, ResponseFormat> result;
817
818         // default test
819         testSubject = createTestSubject();
820         result = testSubject.deleteServiceProxy();
821         assertNotNull(result);
822     }
823
824     @Test
825     void testGetComponentInstanceInputsByInputIdEmpty() {
826         Component component = new Service();
827         String inputId = "";
828         List<ComponentInstanceInput> result;
829
830         result = componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(component, inputId);
831         assertNotNull(result);
832         assertThat(result.isEmpty()).isTrue();
833     }
834
835     @Test
836     void testGetComponentInstanceInputsByInputIdPresent() {
837         List<ComponentInstanceInput> result;
838
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);
846     }
847
848     @Test
849     void testGetComponentInstancePropertiesByInputIdEmpty() {
850         Component component = new Service();
851         String inputId = "";
852         List<ComponentInstanceProperty> result;
853
854         result = componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(component, inputId);
855         assertNotNull(result);
856         assertThat(result.isEmpty()).isTrue();
857     }
858
859     @Test
860     void testGetComponentInstancePropertiesByInputIdPresent() {
861         List<ComponentInstanceProperty> result;
862
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);
869     }
870
871     @Test
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;
879
880         // default test
881         testSubject = createTestSubject();
882         result = testSubject.getRelationById(componentId, relationId, userId, componentTypeEnum);
883         assertNotNull(result);
884     }
885
886     @Test
887     void testValidateParent() {
888         ComponentInstanceBusinessLogic testSubject;
889         resource = createResource();
890         String nodeTemplateId = "";
891         boolean result;
892
893         // default test
894         testSubject = createTestSubject();
895         result = Deencapsulation.invoke(testSubject, "validateParent", new Object[]{resource, nodeTemplateId});
896         assertFalse(result);
897     }
898
899     @Test
900     void testGetComponentType() {
901         ComponentInstanceBusinessLogic testSubject;
902         ComponentTypeEnum result;
903
904         // default test
905         testSubject = createTestSubject();
906         result = Deencapsulation.invoke(testSubject, "getComponentType", new Object[]{ComponentTypeEnum.class});
907         assertNotNull(result);
908     }
909
910     @Test
911     void testGetNewGroupName() {
912         ComponentInstanceBusinessLogic testSubject;
913         String oldPrefix = "";
914         String newNormailzedPrefix = "";
915         String qualifiedGroupInstanceName = "";
916         String result;
917
918         // test 1
919         testSubject = createTestSubject();
920         result = Deencapsulation.invoke(testSubject, "getNewGroupName",
921             new Object[]{oldPrefix, newNormailzedPrefix, qualifiedGroupInstanceName});
922         assertNotNull(result);
923     }
924
925     @Test
926     void testUpdateComponentInstanceMetadata_3() {
927         ComponentInstanceBusinessLogic testSubject;
928         createInstances();
929         ComponentInstance result;
930
931         // default test
932         testSubject = createTestSubject();
933         result = Deencapsulation
934             .invoke(testSubject, "updateComponentInstanceMetadata", new Object[]{toInstance, toInstance});
935         assertNotNull(result);
936     }
937
938     @Test
939     void testFindRelation() {
940         ComponentInstanceBusinessLogic testSubject;
941         String relationId = "";
942         List<RequirementCapabilityRelDef> requirementCapabilityRelations = new ArrayList<>();
943         RequirementCapabilityRelDef result;
944
945         // default test
946         testSubject = createTestSubject();
947         result = Deencapsulation.invoke(testSubject, "findRelation",
948             new Object[]{relationId, requirementCapabilityRelations});
949         assertNull(result);
950     }
951
952     @Test
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;
962
963         when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll))
964             .thenReturn(Either.left(resource));
965
966         // test 1
967         testSubject = createTestSubject();
968         result = testSubject
969             .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
970                 userId);
971         assertNotNull(result);
972
973         componentTypeEnum = null;
974         result = testSubject
975             .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
976                 userId);
977         assertNotNull(result);
978
979         result = testSubject
980             .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
981                 userId);
982         assertNotNull(result);
983     }
984
985     @Test
986     void testUpdateCapabilityPropertyOnContainerComponent() {
987         ComponentInstanceBusinessLogic testSubject;
988         ComponentInstanceProperty property = new ComponentInstanceProperty();
989         String newValue = "";
990         resource = createResource();
991         createInstances();
992         String capabilityType = "";
993         String capabilityName = "";
994         ResponseFormat result;
995
996         // default test
997         testSubject = createTestSubject();
998         result = Deencapsulation.invoke(testSubject, "updateCapabilityPropertyOnContainerComponent",
999             new Object[]{property, newValue, resource, toInstance, capabilityType, capabilityName});
1000         assertNull(result);
1001     }
1002
1003     @Test
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;
1013
1014         when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll))
1015             .thenReturn(Either.left(resource));
1016
1017         // test 1
1018         testSubject = createTestSubject();
1019         result = testSubject
1020             .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
1021                 userId);
1022         assertNotNull(result);
1023         componentTypeEnum = null;
1024         result = testSubject
1025             .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
1026                 userId);
1027         assertNotNull(result);
1028
1029         result = testSubject
1030             .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
1031                 userId);
1032         assertNotNull(result);
1033     }
1034
1035     @Test
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;
1046
1047         when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseMetadata))
1048             .thenReturn(Either.left(resource));
1049
1050         // test 1
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);
1061
1062         result = testSubject
1063             .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1064                 groupInstanceId, property, userId);
1065         assertNotNull(result);
1066     }
1067
1068     @Test
1069     void testDeletePropertyValue() {
1070         ComponentInstanceBusinessLogic testSubject;
1071         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1072         createService();
1073         String serviceId = service.getUniqueId();
1074         String resourceInstanceId = "";
1075         String propertyValueId = "";
1076         String userId = user.getUserId();
1077         Either<ComponentInstanceProperty, ResponseFormat> result;
1078
1079         when(toscaOperationFacade.getToscaElement(serviceId, JsonParseFlagEnum.ParseMetadata))
1080             .thenReturn(Either.left(service));
1081
1082         // test 1
1083         testSubject = createTestSubject();
1084         result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1085             userId);
1086         assertNotNull(result);
1087         componentTypeEnum = null;
1088         result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1089             userId);
1090         assertNotNull(result);
1091
1092         result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1093             userId);
1094         assertNotNull(result);
1095     }
1096
1097     @Test
1098     void testGetComponentParametersViewForForwardingPath() {
1099         ComponentInstanceBusinessLogic testSubject;
1100         ComponentParametersView result;
1101
1102         // default test
1103         testSubject = createTestSubject();
1104         result = Deencapsulation.invoke(testSubject, "getComponentParametersViewForForwardingPath");
1105         assertNotNull(result);
1106     }
1107
1108     @Test
1109     void testGetResourceInstanceById() {
1110         ComponentInstanceBusinessLogic testSubject;
1111         resource = createResource();
1112         String instanceId = "";
1113         Either<ComponentInstance, StorageOperationStatus> result;
1114
1115         // default test
1116         testSubject = createTestSubject();
1117         result = Deencapsulation.invoke(testSubject, "getResourceInstanceById", new Object[]{resource, instanceId});
1118         assertNotNull(result);
1119     }
1120
1121     @Test
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;
1133
1134         when(toscaOperationFacade.getToscaFullElement(containerComponentId))
1135             .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
1136         // test 1
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);
1145     }
1146     
1147     @Test
1148     void testUpdateInstanceRequirement() {
1149         ComponentInstanceBusinessLogic testSubject;
1150         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1151         createComponents();
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();
1159         
1160         Either<RequirementDefinition, ResponseFormat> result;
1161
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);
1170         
1171         result = testSubject.updateInstanceRequirement(componentTypeEnum, containerComponentId,
1172             componentInstanceUniqueId, requirementDef, userId);
1173         assertEquals(requirementDef, result.left().value());
1174
1175     }
1176
1177     @Test
1178     void testCopyComponentInstanceWrongUserId() {
1179
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");
1186
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);
1196
1197         result = componentInstanceBusinessLogic
1198             .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId,
1199                 USER_ID);
1200         assertNotNull(result);
1201
1202         service.setLastUpdaterUserId(oldLastUpdatedUserId);
1203         assertThat(result.isRight()).isTrue();
1204     }
1205
1206     @Test
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);
1214
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();
1229     }
1230
1231     @Test
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);
1241
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);
1250
1251         result = componentInstanceBusinessLogic
1252             .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId,
1253                 USER_ID);
1254         assertNotNull(result);
1255
1256         service.setLastUpdaterUserId(oldServiceLastUpdatedUserId);
1257         resource.setLifecycleState(oldResourceLifeCycle);
1258
1259         assertThat(result.isLeft()).isFalse();
1260     }
1261
1262     @Test
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);
1274
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);
1282
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);
1290
1291         Either<ComponentInstanceAttribute, ResponseFormat> result = Deencapsulation
1292             .invoke(componentInstanceBusinessLogic,
1293                 "createOrUpdateAttributeValueForCopyPaste",
1294                 ComponentTypeEnum.SERVICE,
1295                 serviceComponentInstance
1296                     .getUniqueId(),
1297                 toInstance.getUniqueId(), attribute,
1298                 USER_ID);
1299         assertNotNull(result);
1300
1301         service.setLastUpdaterUserId(oldLastUpdatedUserId);
1302         service.setLifecycleState(oldLifeCycleState);
1303
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());
1308     }
1309
1310     @Test
1311     void testUpdateComponentInstanceProperty() {
1312
1313         String containerComponentId = service.getUniqueId();
1314         String componentInstanceId = "dummy_id";
1315         ComponentInstanceProperty property = Mockito.mock(ComponentInstanceProperty.class);
1316
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);
1325
1326         Either<String, ResponseFormat> result = Deencapsulation.invoke(componentInstanceBusinessLogic,
1327             "updateComponentInstanceProperty", containerComponentId, componentInstanceId, property);
1328         assertNotNull(result);
1329         assertThat(result.isLeft()).isTrue();
1330     }
1331
1332     @Test
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);
1341         newInputs.add(in);
1342         List<InputDefinition> oldInputs = service.getInputs();
1343         service.setInputs(newInputs);
1344
1345         Either<String, ResponseFormat> result =
1346             Deencapsulation.invoke(componentInstanceBusinessLogic, "getInputListDefaultValue", component, inputId);
1347
1348         service.setInputs(oldInputs);
1349
1350         assertEquals(result.left().value(), defaultValue);
1351     }
1352
1353     @Test
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)))
1369             .thenReturn(cont);
1370
1371         try {
1372             result = componentInstanceBusinessLogic
1373                 .batchDeleteComponentInstance(containerComponentParam, containerComponentId, componentInstanceIdList,
1374                     USER_ID);
1375             assertNotNull(result);
1376             assertEquals(deleteErrorMap, result);
1377         } catch (ComponentException e) {
1378             assertEquals(e.getActionStatus().toString(), StorageOperationStatus.GENERAL_ERROR.toString());
1379         }
1380     }
1381
1382     @Test
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);
1393
1394         Either<Component, StorageOperationStatus> err = Either.right(StorageOperationStatus.GENERAL_ERROR);
1395         when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1396             .thenReturn(err);
1397
1398         try {
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());
1405         }
1406     }
1407
1408     @Test
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);
1423
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)))
1434             .thenReturn(cont);
1435         when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
1436
1437         result = componentInstanceBusinessLogic
1438             .batchDeleteComponentInstance(containerComponentParam, containerComponentId,
1439                 componentInstanceIdList, USER_ID);
1440         assertNotNull(result);
1441
1442         service.setLastUpdaterUserId(oldLastUpdatedUserId);
1443         service.setLifecycleState(oldLifeCycleState);
1444         assertEquals(deleteErrorMap, result);
1445     }
1446
1447     @Test
1448     void testDissociateRIFromRIFailDissociate() {
1449
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);
1469
1470         Either<Component, StorageOperationStatus> cont = Either.left(service);
1471         when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
1472             .thenReturn(cont);
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);
1482
1483         try {
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());
1490         }
1491
1492         service.setLastUpdaterUserId(oldLastUpdatedUserId);
1493         service.setLifecycleState(oldLifeCycleState);
1494
1495     }
1496
1497     @Test
1498     void testDissociateRIFromRISuccess() {
1499
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);
1510
1511         Either<Component, StorageOperationStatus> cont = Either.left(service);
1512         when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
1513             .thenReturn(cont);
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);
1521
1522         result = componentInstanceBusinessLogic
1523             .batchDissociateRIFromRI(componentId, USER_ID, requirementDefList, componentTypeEnum);
1524         assertNotNull(result);
1525
1526         service.setLastUpdaterUserId(oldLastUpdatedUserId);
1527         service.setLifecycleState(oldLifeCycleState);
1528
1529         assertEquals(requirementDefList, result);
1530     }
1531
1532     @Test
1533     void testGetComponentInstancePropertyByPolicyId_success() {
1534         Optional<ComponentInstanceProperty> propertyCandidate =
1535             getComponentInstanceProperty(PROP_NAME);
1536
1537         assertThat(propertyCandidate).isPresent();
1538         assertEquals(PROP_NAME, propertyCandidate.get().getName());
1539     }
1540
1541     @Test
1542     void testGetComponentInstancePropertyByPolicyId_failure() {
1543         Optional<ComponentInstanceProperty> propertyCandidate =
1544             getComponentInstanceProperty(NON_EXIST_NAME);
1545
1546         assertEquals(propertyCandidate, Optional.empty());
1547     }
1548
1549     private Optional<ComponentInstanceProperty> getComponentInstanceProperty(String propertyName) {
1550         ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
1551         componentInstanceProperty.setName(propertyName);
1552
1553         PolicyDefinition policyDefinition = getPolicyDefinition();
1554         componentInstanceProperty.setGetPolicyValues(policyDefinition.getGetPolicyValues());
1555
1556         service.setComponentInstancesProperties(
1557             Collections.singletonMap(COMPONENT_INSTANCE_ID, Collections.singletonList(componentInstanceProperty)));
1558
1559         return componentInstanceBusinessLogic.getComponentInstancePropertyByPolicyId(service, policyDefinition);
1560     }
1561
1562     private PolicyDefinition getPolicyDefinition() {
1563         PolicyDefinition policyDefinition = new PolicyDefinition();
1564         policyDefinition.setInstanceUniqueId(COMPONENT_INSTANCE_ID);
1565         policyDefinition.setName(PROP_NAME);
1566
1567         GetPolicyValueDataDefinition getPolicy = new GetPolicyValueDataDefinition();
1568         getPolicy.setPropertyName(PROP_NAME);
1569
1570         List<GetPolicyValueDataDefinition> getPolicies = new ArrayList<>();
1571         getPolicies.add(getPolicy);
1572         policyDefinition.setGetPolicyValues(getPolicies);
1573
1574         return policyDefinition;
1575     }
1576
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;
1588     }
1589     
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);
1597
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);
1603
1604         return Pair.of(instanceToBeCreated, originComponent);
1605     }
1606
1607     // Common part for testing component instance name validation
1608     private void testCreateComponentInstanceNameValidationFailure(String ciName) {
1609         ComponentInstance ci = new ComponentInstance();
1610         ci.setName(ciName);
1611
1612         // Stub for getting component
1613         when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
1614             .thenReturn(Either.left(service));
1615
1616         // Expecting ByActionStatusComponentException
1617         ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
1618             componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1619         });
1620         assertEquals(ActionStatus.INVALID_COMPONENT_NAME, e.getActionStatus());
1621     }
1622
1623     @TestFactory
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))
1634         );
1635     }
1636
1637     @Test
1638     void testCreateComponentInstanceFailToGetComponent() {
1639         ComponentInstance ci = prepareResourcesForCreateComponentInstanceTest().getLeft();
1640
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);
1648
1649         ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
1650             componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1651         });
1652         assertThat(e.getActionStatus()).isEqualTo(ActionStatus.RESOURCE_NOT_FOUND);
1653     }
1654
1655     @Test
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);
1661
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));
1667
1668         ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
1669             componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1670         });
1671         assertThat(e.getActionStatus()).isEqualTo(ActionStatus.CONTAINER_CANNOT_CONTAIN_COMPONENT_IN_STATE);
1672     }
1673
1674     @Test
1675     void testCreateComponentInstanceFailureArchived() {
1676         Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
1677         ComponentInstance ci = p.getLeft();
1678         Resource originComponent = p.getRight();
1679         originComponent.setArchived(Boolean.TRUE);
1680
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));
1686
1687         ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
1688             componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1689         });
1690         assertThat(e.getActionStatus()).isEqualTo(ActionStatus.COMPONENT_IS_ARCHIVED);
1691     }
1692
1693     @Test
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
1699
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));
1705
1706         final ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
1707             componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1708         });
1709         assertThat(e.getActionStatus()).isEqualTo(ActionStatus.INVALID_CONTENT);
1710     }
1711
1712     @Test
1713     void testCreateComponentInstanceFailureCannotContainInstance() {
1714         final Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
1715         final ComponentInstance ci = p.getLeft();
1716         final Resource originComponent = p.getRight();
1717
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)))
1725             .thenReturn(false);
1726
1727         ByActionStatusComponentException actualException = assertThrows(ByActionStatusComponentException.class, () -> {
1728             componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1729         });
1730         assertThat(actualException.getActionStatus()).isEqualTo(ActionStatus.CONTAINER_CANNOT_CONTAIN_INSTANCE);
1731         verify(containerInstanceTypeData, times(1)).isAllowedForServiceComponent(eq(ResourceTypeEnum.VF));
1732
1733         //given
1734         final Resource resource = createResource();
1735         resource.setResourceType(ResourceTypeEnum.VF);
1736         resource.setLastUpdaterUserId(USER_ID);
1737         //when
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)))
1743             .thenReturn(false);
1744         actualException = assertThrows(ByActionStatusComponentException.class, () -> {
1745             componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.RESOURCE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1746         });
1747         //then
1748         assertThat(actualException.getActionStatus()).isEqualTo(ActionStatus.CONTAINER_CANNOT_CONTAIN_INSTANCE);
1749     }
1750
1751     @Test
1752     void testCreateComponentInstanceFailureAddToGraph() {
1753         final Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
1754         final ComponentInstance ci = p.getLeft();
1755         final Resource originComponent = p.getRight();
1756
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)))
1764             .thenReturn(true);
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);
1777
1778         assertThrows(ByResponseFormatComponentException.class, () -> {
1779             componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1780         });
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);
1787     }
1788
1789     @Test
1790     void testCreateComponentInstanceSuccess() {
1791         final Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
1792         final ComponentInstance instanceToBeCreated = p.getLeft();
1793         final Resource originComponent = p.getRight();
1794
1795         final Service updatedService = new Service();
1796         updatedService.setComponentInstances(Collections.singletonList(instanceToBeCreated));
1797         updatedService.setUniqueId(service.getUniqueId());
1798
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)))
1806             .thenReturn(true);
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);
1821
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();
1834     }
1835     
1836     @Test
1837     void testCreateComponentInstanceServiceSubstitutionSuccess() {
1838         ComponentInstance instanceToBeCreated = createServiceSubstitutionComponentInstance();
1839         Service originService = createServiceSubstitutionOriginService();
1840         Component serviceBaseComponent = createServiceSubstitutionServiceDerivedFromComponent();
1841
1842         Service updatedService = new Service();
1843         updatedService.setComponentInstances(Collections.singletonList(instanceToBeCreated));
1844         updatedService.setUniqueId(service.getUniqueId());
1845         
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);
1868
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();
1879     }
1880     
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);
1887         
1888         return instanceToBeCreated;
1889     }
1890     
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;
1899     }
1900     
1901     
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");
1909         return component;
1910     }
1911 }