bf3ec3cb7e20565aba4efc46698fcb2db9adfbd3
[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.assertNotEquals;
27 import static org.junit.jupiter.api.Assertions.assertNotNull;
28 import static org.junit.jupiter.api.Assertions.assertNull;
29 import static org.junit.jupiter.api.Assertions.assertSame;
30 import static org.junit.jupiter.api.Assertions.assertThrows;
31 import static org.junit.jupiter.api.Assertions.assertTrue;
32 import static org.junit.jupiter.api.DynamicTest.dynamicTest;
33 import static org.mockito.ArgumentMatchers.any;
34 import static org.mockito.ArgumentMatchers.anySet;
35 import static org.mockito.ArgumentMatchers.eq;
36 import static org.mockito.ArgumentMatchers.isNull;
37 import static org.mockito.Mockito.doReturn;
38 import static org.mockito.Mockito.doThrow;
39 import static org.mockito.Mockito.times;
40 import static org.mockito.Mockito.verify;
41 import static org.mockito.Mockito.when;
42 import static org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum.RESOURCE_PARAM_NAME;
43
44 import fj.data.Either;
45 import java.util.ArrayList;
46 import java.util.Arrays;
47 import java.util.Collections;
48 import java.util.HashMap;
49 import java.util.HashSet;
50 import java.util.List;
51 import java.util.Map;
52 import java.util.Map.Entry;
53 import java.util.Optional;
54 import java.util.Set;
55 import java.util.stream.Stream;
56 import mockit.Deencapsulation;
57 import org.apache.commons.collections4.CollectionUtils;
58 import org.apache.commons.collections4.MapUtils;
59 import org.apache.commons.lang3.tuple.ImmutablePair;
60 import org.apache.commons.lang3.tuple.Pair;
61 import org.assertj.core.util.Lists;
62 import org.junit.jupiter.api.BeforeAll;
63 import org.junit.jupiter.api.BeforeEach;
64 import org.junit.jupiter.api.DynamicTest;
65 import org.junit.jupiter.api.Test;
66 import org.junit.jupiter.api.TestFactory;
67 import org.junit.jupiter.params.ParameterizedTest;
68 import org.junit.jupiter.params.provider.Arguments;
69 import org.junit.jupiter.params.provider.MethodSource;
70 import org.mockito.Mock;
71 import org.mockito.Mockito;
72 import org.mockito.MockitoAnnotations;
73 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
74 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
75 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
76 import org.openecomp.sdc.be.components.impl.exceptions.ToscaGetFunctionExceptionSupplier;
77 import org.openecomp.sdc.be.components.validation.UserValidations;
78 import org.openecomp.sdc.be.config.ConfigurationManager;
79 import org.openecomp.sdc.be.dao.api.ActionStatus;
80 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
81 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
82 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
83 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
84 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
85 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathElementDataDefinition;
86 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
87 import org.openecomp.sdc.be.datatypes.elements.GetPolicyValueDataDefinition;
88 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
89 import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition;
90 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
91 import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition;
92 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
93 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
94 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
95 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
96 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
97 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
98 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
99 import org.openecomp.sdc.be.exception.BusinessException;
100 import org.openecomp.sdc.be.impl.ComponentsUtils;
101 import org.openecomp.sdc.be.model.ArtifactDefinition;
102 import org.openecomp.sdc.be.model.CapabilityDefinition;
103 import org.openecomp.sdc.be.model.CapabilityRequirementRelationship;
104 import org.openecomp.sdc.be.model.Component;
105 import org.openecomp.sdc.be.model.ComponentInstance;
106 import org.openecomp.sdc.be.model.ComponentInstanceAttribute;
107 import org.openecomp.sdc.be.model.ComponentInstanceInput;
108 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
109 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
110 import org.openecomp.sdc.be.model.ComponentParametersView;
111 import org.openecomp.sdc.be.model.DataTypeDefinition;
112 import org.openecomp.sdc.be.model.InputDefinition;
113 import org.openecomp.sdc.be.model.LifecycleStateEnum;
114 import org.openecomp.sdc.be.model.PolicyDefinition;
115 import org.openecomp.sdc.be.model.PropertyDefinition;
116 import org.openecomp.sdc.be.model.RelationshipImpl;
117 import org.openecomp.sdc.be.model.RelationshipInfo;
118 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
119 import org.openecomp.sdc.be.model.RequirementDefinition;
120 import org.openecomp.sdc.be.model.Resource;
121 import org.openecomp.sdc.be.model.Service;
122 import org.openecomp.sdc.be.model.User;
123 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
124 import org.openecomp.sdc.be.model.jsonjanusgraph.config.ContainerInstanceTypesData;
125 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ForwardingPathOperation;
126 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
127 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.OperationException;
128 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
129 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
130 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
131 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
132 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
133 import org.openecomp.sdc.common.api.ConfigurationSource;
134 import org.openecomp.sdc.common.impl.ExternalConfiguration;
135 import org.openecomp.sdc.common.impl.FSConfigurationSource;
136 import org.openecomp.sdc.common.util.ValidationUtils;
137 import org.openecomp.sdc.exception.ResponseFormat;
138
139 /**
140  * The test suite designed for test functionality of ComponentInstanceBusinessLogic class
141  */
142 class ComponentInstanceBusinessLogicTest {
143
144     private final static String USER_ID = "jh0003";
145     private final static String COMPONENT_ID = "componentId";
146     private final static String ORIGIN_COMPONENT_ID = "originComponentId";
147     private final static String ORIGIN_COMPONENT_VERSION = "1.0";
148     private final static String TO_INSTANCE_ID = "toInstanceId";
149     private final static String TO_INSTANCE_NAME = "toInstanceName";
150     private final static String COMPONENT_INSTANCE_ID = "componentInstanceId";
151     private final static String COMPONENT_INSTANCE_NAME = "componentInstanceName";
152     private final static String FROM_INSTANCE_ID = "fromInstanceId";
153     private final static String RELATION_ID = "relationId";
154     private final static String CAPABILITY_OWNER_ID = "capabilityOwnerId";
155     private final static String CAPABILITY_UID = "capabilityUid";
156     private final static String CAPABILITY_NAME = "capabilityName";
157     private final static String REQUIREMENT_OWNER_ID = "requirementOwnerId";
158     private final static String REQUIREMENT_UID = "requirementUid";
159     private final static String REQUIREMENT_NAME = "requirementName";
160     private final static String RELATIONSHIP_TYPE = "relationshipType";
161     private final static String ARTIFACT_1 = "cloudtech_k8s_charts.zip";
162     private final static String ARTIFACT_2 = "cloudtech_azure_day0.zip";
163     private final static String ARTIFACT_3 = "cloudtech_aws_configtemplate.zip";
164     private final static String ARTIFACT_4 = "k8s_charts.zip";
165     private final static String ARTIFACT_5 = "cloudtech_openstack_configtemplate.zip";
166     private final static String PROP_NAME = "propName";
167     private final static String NON_EXIST_NAME = "nonExistName";
168     private final static String INPUT_ID = "inputId";
169     private final static String ICON_NAME = "icon";
170
171     private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
172     @Mock
173     private ComponentInstancePropInput componentInstancePropInput;
174     @Mock
175     private ArtifactsBusinessLogic artifactsBusinessLogic;
176     @Mock
177     private ComponentsUtils componentsUtils;
178     @Mock
179     private ToscaOperationFacade toscaOperationFacade;
180     @Mock
181     private ForwardingPathOperation forwardingPathOperation;
182     @Mock
183     private User user;
184     @Mock
185     private UserValidations userValidations;
186     @Mock
187     private GraphLockOperation graphLockOperation;
188     @Mock
189     private JanusGraphDao janusGraphDao;
190     @Mock
191     private ApplicationDataTypeCache applicationDataTypeCache;
192     @Mock
193     private PropertyOperation propertyOperation;
194     @Mock
195     private ContainerInstanceTypesData containerInstanceTypeData;
196     @Mock
197     private CompositionBusinessLogic compositionBusinessLogic;
198
199     private Component service;
200     private Component resource;
201     private ComponentInstance toInstance;
202     private ComponentInstance fromInstance;
203     private RequirementCapabilityRelDef relation;
204     private List<ComponentInstanceProperty> ciPropertyList;
205     private List<ComponentInstanceInput> ciInputList;
206
207     @BeforeAll
208     static void beforeAll() {
209         initConfig();
210     }
211
212     private static void initConfig() {
213         final ConfigurationSource configurationSource = new FSConfigurationSource(
214             ExternalConfiguration.getChangeListener(),
215             "src/test/resources/config/catalog-be"
216         );
217         new ConfigurationManager(configurationSource);
218     }
219
220     @BeforeEach
221     void init() {
222         MockitoAnnotations.openMocks(this);
223         componentInstanceBusinessLogic = new ComponentInstanceBusinessLogic(null, null, null, null, null, null, null, artifactsBusinessLogic, null,
224             null, forwardingPathOperation, null, null);
225         componentInstanceBusinessLogic.setComponentsUtils(componentsUtils);
226         componentInstanceBusinessLogic.setToscaOperationFacade(toscaOperationFacade);
227         componentInstanceBusinessLogic.setUserValidations(userValidations);
228         componentInstanceBusinessLogic.setGraphLockOperation(graphLockOperation);
229         componentInstanceBusinessLogic.setJanusGraphDao(janusGraphDao);
230         componentInstanceBusinessLogic.setApplicationDataTypeCache(applicationDataTypeCache);
231         componentInstanceBusinessLogic.setPropertyOperation(propertyOperation);
232         componentInstanceBusinessLogic.setContainerInstanceTypesData(containerInstanceTypeData);
233         componentInstanceBusinessLogic.setCompositionBusinessLogic(compositionBusinessLogic);
234
235         stubMethods();
236         createComponents();
237     }
238
239     @Test
240     void testGetRelationByIdSuccess() {
241         getServiceRelationByIdSuccess(service);
242         getServiceRelationByIdSuccess(resource);
243     }
244
245     @Test
246     void testGetRelationByIdUserValidationFailure() {
247         getServiceRelationByIdUserValidationFailure(service);
248         getServiceRelationByIdUserValidationFailure(resource);
249     }
250
251     @Test
252     void testGetRelationByIdComponentNotFoundFailure() {
253         getRelationByIdComponentNotFoundFailure(service);
254         getRelationByIdComponentNotFoundFailure(resource);
255     }
256
257     @Test
258     void testForwardingPathOnVersionChange() {
259         getforwardingPathOnVersionChange();
260     }
261
262     @Test
263     void testIsCloudSpecificArtifact() {
264         assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_1)).isTrue();
265         assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_2)).isTrue();
266         assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_3)).isTrue();
267         assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_4)).isFalse();
268         assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_5)).isFalse();
269     }
270
271     private void getforwardingPathOnVersionChange() {
272         String containerComponentParam = "services";
273         String containerComponentID = "121-cont";
274         String componentInstanceID = "121-cont-1-comp";
275         Service component = new Service();
276         Map<String, ForwardingPathDataDefinition> forwardingPaths = generateForwardingPath(componentInstanceID);
277
278         //Add existing componentInstance to component
279         List<ComponentInstance> componentInstanceList = new ArrayList<>();
280         ComponentInstance oldComponentInstance = new ComponentInstance();
281         oldComponentInstance.setName("OLD_COMP_INSTANCE");
282         oldComponentInstance.setUniqueId(componentInstanceID);
283         oldComponentInstance.setName(componentInstanceID);
284         oldComponentInstance.setToscaPresentationValue(JsonPresentationFields.CI_COMPONENT_UID, "1-comp");
285         componentInstanceList.add(oldComponentInstance);
286         component.setComponentInstances(componentInstanceList);
287         component.setForwardingPaths(forwardingPaths);
288
289         List<ComponentInstance> componentInstanceListNew = new ArrayList<>();
290         ComponentInstance newComponentInstance = new ComponentInstance();
291         String new_Comp_UID = "2-comp";
292         newComponentInstance.setToscaPresentationValue(JsonPresentationFields.CI_COMPONENT_UID, new_Comp_UID);
293         newComponentInstance.setUniqueId(new_Comp_UID);
294         componentInstanceListNew.add(newComponentInstance);
295         Component component2 = new Service();
296         component2.setComponentInstances(componentInstanceListNew);
297
298         //Mock for getting component
299         when(toscaOperationFacade.getToscaElement(eq(containerComponentID), any(ComponentParametersView.class)))
300             .thenReturn(Either.left(component));
301         when(toscaOperationFacade.validateComponentExists(any(String.class))).thenReturn(Either.left(Boolean.TRUE));
302         when(toscaOperationFacade.getToscaFullElement(new_Comp_UID)).thenReturn(Either.left(component2));
303
304         Either<Set<String>, ResponseFormat> resultOp = componentInstanceBusinessLogic
305             .forwardingPathOnVersionChange(containerComponentParam,
306                 containerComponentID, componentInstanceID,
307                 newComponentInstance);
308         assertEquals(1, resultOp.left().value().size());
309         assertEquals("FP-ID-1", resultOp.left().value().iterator().next());
310
311     }
312
313     @Test
314     void testCreateOrUpdatePropertiesValues2() {
315         String containerComponentID = "containerId";
316         String resourceInstanceId = "resourceId";
317         String componentInstanceID = "componentInstance";
318         List<ComponentInstanceProperty> properties = new ArrayList<>();
319         ComponentInstanceProperty property = new ComponentInstanceProperty();
320         property.setName("property");
321         property.setValue("newVal");
322         property.setType("string");
323         properties.add(property);
324
325         List<ComponentInstanceProperty> origProperties = new ArrayList<>();
326         ComponentInstanceProperty origProperty = new ComponentInstanceProperty();
327         origProperty.setName("property");
328         origProperty.setValue("value");
329         origProperty.setType("string");
330         origProperties.add(origProperty);
331
332         Map<String, DataTypeDefinition> types = new HashMap<>();
333         DataTypeDefinition dataTypeDef = new DataTypeDefinition();
334         types.put("string", dataTypeDef);
335
336         Component component = new Service();
337         component.setLastUpdaterUserId("userId");
338         component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
339         Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
340         componentInstanceProps.put("resourceId", origProperties);
341         component.setComponentInstancesProperties(componentInstanceProps);
342         ComponentInstance ci = createComponentInstance("ci1");
343         ci.setUniqueId("resourceId");
344         component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
345             createComponentInstance(componentInstanceID)));
346
347         when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
348             .thenReturn(Either.left(component));
349         when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
350             .thenReturn(StorageOperationStatus.OK);
351         when(componentsUtils.getAllDataTypes(applicationDataTypeCache, component.getModel())).thenReturn(types);
352         when(propertyOperation.validateAndUpdatePropertyValue(property.getType(), "newVal", true, null, types))
353             .thenReturn(Either.left("newVal"));
354         when(propertyOperation.validateAndUpdateRules("string", property.getRules(),
355             null, types, true)).thenReturn(ImmutablePair.of("string", null));
356         when(toscaOperationFacade.updateComponentInstanceProperty(component, ci.getUniqueId(),
357             origProperty)).thenReturn(StorageOperationStatus.OK);
358         origProperties.get(0).setValue("newVal");
359         when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(component))
360             .thenReturn(Either.left(component));
361         when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
362         when(graphLockOperation.unlockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
363             .thenReturn(StorageOperationStatus.OK);
364
365         Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = componentInstanceBusinessLogic
366             .createOrUpdatePropertiesValues(
367                 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
368         assertThat(responseFormatEither.left().value()).isEqualTo(properties);
369     }
370
371     @Test
372     void testToscaGetFunctionValidation() {
373         final String userId = "userId";
374         final String containerComponentId = "containerComponentId";
375         final String containerComponentName = "containerComponentName";
376         final String resourceInstanceId = "resourceInstanceId";
377         final String inputName = "myInputToGet";
378         final String inputId = String.format("%s.%s", containerComponentId, inputName);
379         final String schemaType = "string";
380         //creating instance list of string property with get_input value
381         final ComponentInstanceProperty propertyGetInput = new ComponentInstanceProperty();
382         propertyGetInput.setName("getInputProperty");
383         propertyGetInput.setPropertyId(String.format("%s.%s", containerComponentId, "getInputProperty"));
384         propertyGetInput.setValue(String.format("get_input: [\"%s\"]", inputName));
385         propertyGetInput.setType("list");
386         final SchemaDefinition listStringPropertySchema = createSchema(schemaType);
387         propertyGetInput.setSchema(listStringPropertySchema);
388         propertyGetInput.setToscaGetFunction(
389             createGetToscaFunction(inputName, inputId, List.of(propertyGetInput.getName()), PropertySource.SELF, ToscaGetFunctionType.GET_INPUT,
390                 containerComponentId, containerComponentName)
391         );
392         //creating instance map of string property with get_input value to a second level property:
393         // get_input: ["property1", "subProperty1", "subProperty2"]
394         final String getPropertyPropertyName = "getPropertyProperty";
395         final List<String> containerPropertyPath = List.of("property1", "subProperty1", "subProperty2");
396         final String containerPropertyId = String.format("%s.%s", containerComponentId, containerPropertyPath.get(0));
397         final String mapToscaType = "map";
398         final ComponentInstanceProperty propertyGetProperty = createComponentInstanceProperty(
399             String.format("%s.%s", containerComponentId, getPropertyPropertyName),
400             getPropertyPropertyName,
401             mapToscaType,
402             "string",
403             String.format("\"get_property\": [\"%s\", \"%s\"]", PropertySource.SELF, String.join("\", \"", containerPropertyPath)),
404             createGetToscaFunction(containerPropertyPath.get(containerPropertyPath.size() - 1), containerPropertyId,
405                 containerPropertyPath, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, containerComponentId, containerComponentName)
406         );
407
408         //creating component that has the instance properties
409         final Component component = new Service();
410         component.setUniqueId(containerComponentId);
411         component.setName(containerComponentName);
412         component.setLastUpdaterUserId(userId);
413         component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
414         //adding instance properties to the component
415         final List<ComponentInstanceProperty> resourceInstanceProperties = List.of(propertyGetInput, propertyGetProperty);
416         final Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
417         componentInstanceProps.put(resourceInstanceId, resourceInstanceProperties);
418         component.setComponentInstancesProperties(componentInstanceProps);
419
420         //creating component input that will be gotten by the get_input instance property
421         final var inputDefinition = new InputDefinition();
422         inputDefinition.setName(inputName);
423         inputDefinition.setUniqueId(inputId);
424         inputDefinition.setType(propertyGetInput.getType());
425         inputDefinition.setSchema(listStringPropertySchema);
426         component.setInputs(List.of(inputDefinition));
427
428         //creating component property that contains the sub property that will be gotten by the get_property instance property
429         final var propertyDefinition = new PropertyDefinition();
430         propertyDefinition.setName(containerPropertyPath.get(0));
431         propertyDefinition.setUniqueId(containerPropertyId);
432         final String property1Type = "property1.datatype";
433         propertyDefinition.setType(property1Type);
434         component.setProperties(List.of(propertyDefinition));
435         //creating resource instance to be added to the component
436         final ComponentInstance resourceInstance = createComponentInstance("resourceInstance");
437         resourceInstance.setUniqueId(resourceInstanceId);
438         component.setComponentInstances(List.of(resourceInstance));
439
440         mockComponentForToscaGetFunctionValidation(component);
441
442         //creating data types for "map", and sub properties
443         final Map<String, DataTypeDefinition> allDataTypesMap = new HashMap<>();
444         allDataTypesMap.put(mapToscaType, new DataTypeDefinition());
445
446         final String subProperty1Type = "subProperty1.datatype";
447         allDataTypesMap.put(property1Type, createDataType(property1Type, Map.of(containerPropertyPath.get(1), subProperty1Type)));
448
449         final var subProperty2Property = new PropertyDefinition();
450         subProperty2Property.setName(containerPropertyPath.get(2));
451         subProperty2Property.setType(propertyGetProperty.getType());
452         subProperty2Property.setSchema(propertyGetProperty.getSchema());
453         allDataTypesMap.put(subProperty1Type, createDataType(subProperty1Type, List.of(subProperty2Property)));
454
455         when(applicationDataTypeCache.getAll(component.getModel())).thenReturn(Either.left(allDataTypesMap));
456         //when
457         final Either<List<ComponentInstanceProperty>, ResponseFormat> actualResponseFormat = componentInstanceBusinessLogic
458             .createOrUpdatePropertiesValues(
459                 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentId, resourceInstanceId, resourceInstanceProperties, userId);
460         //then
461         assertTrue(actualResponseFormat.isLeft());
462         assertThat(actualResponseFormat.left().value()).isEqualTo(resourceInstanceProperties);
463     }
464
465     @Test
466     void testToscaGetPropertyOnInstanceValidation() {
467         final String userId = "userId";
468         final String containerComponentId = "containerComponentId";
469         final String containerComponentName = "containerComponentName";
470         final String instanceUniqueId = String.format("%s.%s", containerComponentId, "instanceId");
471
472         final List<String> parentPropertyPath = List.of("property1");
473         final String containerPropertyId = String.format("%s.%s", containerComponentId, parentPropertyPath.get(0));
474         final ComponentInstanceProperty getPropertyOnInstanceProperty = createComponentInstanceProperty(
475             String.format("%s.%s", containerComponentId, "getPropertyOnInstanceProperty"),
476             "getPropertyOnInstanceProperty",
477             "string",
478             null,
479             String.format("\"get_property\": [\"%s\", \"%s\"]", PropertySource.INSTANCE, parentPropertyPath.get(0)),
480             createGetToscaFunction(parentPropertyPath.get(0), containerPropertyId, parentPropertyPath, PropertySource.INSTANCE,
481                 ToscaGetFunctionType.GET_PROPERTY, instanceUniqueId, containerComponentName)
482         );
483
484         //creating component that has the instance properties
485         final Component component = new Service();
486         component.setUniqueId(containerComponentId);
487         component.setName(containerComponentName);
488         component.setLastUpdaterUserId(userId);
489         component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
490         //adding instance properties to the component
491         final List<ComponentInstanceProperty> resourceInstanceProperties = List.of(getPropertyOnInstanceProperty);
492         final Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
493         componentInstanceProps.put(instanceUniqueId, resourceInstanceProperties);
494         component.setComponentInstancesProperties(componentInstanceProps);
495
496         //creating resource property that will be get
497         final var propertyDefinition = new PropertyDefinition();
498         propertyDefinition.setName(parentPropertyPath.get(0));
499         propertyDefinition.setUniqueId(containerPropertyId);
500         final String property1Type = "string";
501         propertyDefinition.setType(property1Type);
502         //creating resource instance to be added to the component
503         final ComponentInstance resourceInstance = createComponentInstance("resourceInstance");
504         resourceInstance.setUniqueId(instanceUniqueId);
505         resourceInstance.setProperties(List.of(propertyDefinition));
506         component.setComponentInstances(List.of(resourceInstance));
507
508         mockComponentForToscaGetFunctionValidation(component);
509
510         //when
511         final Either<List<ComponentInstanceProperty>, ResponseFormat> actualResponseFormat = componentInstanceBusinessLogic
512             .createOrUpdatePropertiesValues(
513                 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentId, instanceUniqueId, resourceInstanceProperties, userId);
514         //then
515         assertTrue(actualResponseFormat.isLeft());
516         assertThat(actualResponseFormat.left().value()).isEqualTo(resourceInstanceProperties);
517     }
518
519     private DataTypeDefinition createDataType(final String name, final Map<String, String> propertyNameAndTypeMap) {
520         final var dataTypeDefinition = new DataTypeDefinition();
521         dataTypeDefinition.setName(name);
522         if (MapUtils.isNotEmpty(propertyNameAndTypeMap)) {
523             for (final Entry<String, String> propertyEntry : propertyNameAndTypeMap.entrySet()) {
524                 final var propertyDefinition = new PropertyDefinition();
525                 propertyDefinition.setName(propertyEntry.getKey());
526                 propertyDefinition.setType(propertyEntry.getValue());
527                 dataTypeDefinition.setProperties(List.of(propertyDefinition));
528             }
529         }
530         return dataTypeDefinition;
531     }
532
533     private DataTypeDefinition createDataType(final String name, final List<PropertyDefinition> propertyList) {
534         final var dataTypeDefinition = new DataTypeDefinition();
535         dataTypeDefinition.setName(name);
536         if (CollectionUtils.isNotEmpty(propertyList)) {
537             dataTypeDefinition.setProperties(propertyList);
538         }
539         return dataTypeDefinition;
540     }
541
542     private ComponentInstanceProperty createComponentInstanceProperty(final String uniqueId, final String name, final String type,
543                                                                       final String schemaType, final String value,
544                                                                       final ToscaGetFunctionDataDefinition toscaGetFunction) {
545         final var componentInstanceProperty = new ComponentInstanceProperty();
546         componentInstanceProperty.setName(name);
547         componentInstanceProperty.setUniqueId(uniqueId);
548         componentInstanceProperty.setType(type);
549         componentInstanceProperty.setValue(value);
550         if (schemaType != null) {
551             final SchemaDefinition schemaDefinition = createSchema(schemaType);
552             componentInstanceProperty.setSchema(schemaDefinition);
553         }
554         if (toscaGetFunction != null) {
555             componentInstanceProperty.setToscaGetFunction(toscaGetFunction);
556         }
557
558         return componentInstanceProperty;
559     }
560
561     @Test
562     void testToscaGetFunctionValidation_schemaDivergeTest() {
563         final String userId = "userId";
564         final String containerComponentId = "containerComponentId";
565         final String containerComponentName = "containerComponentName";
566         final String resourceInstanceId = "resourceInstanceId";
567         final String inputName = "myInputToGet";
568         final String inputId = String.format("%s.%s", containerComponentId, inputName);
569         final String propertyName = "getInputProperty";
570         final String propertyId = String.format("%s.%s", containerComponentId, propertyName);
571         final String propertyType = "list";
572         final List<ComponentInstanceProperty> properties = new ArrayList<>();
573         final ComponentInstanceProperty propertyGetInput = createComponentInstanceProperty(
574             propertyId,
575             "getInputProperty",
576             propertyType,
577             "string",
578             String.format("get_input: [\"%s\"]", inputName),
579             createGetToscaFunction(inputName, inputId, List.of(propertyName), PropertySource.SELF, ToscaGetFunctionType.GET_INPUT,
580                 containerComponentId, containerComponentName)
581         );
582         properties.add(propertyGetInput);
583
584         final Component component = new Service();
585         component.setUniqueId(containerComponentId);
586         component.setName(containerComponentName);
587         component.setLastUpdaterUserId(userId);
588         component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
589
590         var inputDefinition = new InputDefinition();
591         inputDefinition.setName(inputName);
592         inputDefinition.setUniqueId(inputId);
593         inputDefinition.setType(propertyType);
594         inputDefinition.setSchema(createSchema("integer"));
595         component.setInputs(List.of(inputDefinition));
596
597         final Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
598         componentInstanceProps.put(resourceInstanceId, properties);
599         component.setComponentInstancesProperties(componentInstanceProps);
600
601         final ComponentInstance resourceInstance = createComponentInstance("componentInstance1");
602         resourceInstance.setUniqueId(resourceInstanceId);
603         component.setComponentInstances(List.of(resourceInstance));
604
605         mockComponentForToscaGetFunctionValidation(component);
606         //when
607         final Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither =
608             componentInstanceBusinessLogic
609                 .createOrUpdatePropertiesValues(ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentId, resourceInstanceId, properties, userId);
610         //then
611         assertTrue(responseFormatEither.isRight(), "Expecting an error");
612         final ResponseFormat actualResponse = responseFormatEither.right().value();
613         final ResponseFormat expectedResponse =
614             ToscaGetFunctionExceptionSupplier
615                 .propertySchemaDiverge(propertyGetInput.getToscaGetFunction().getFunctionType(), inputDefinition.getSchemaType(),
616                     propertyGetInput.getSchemaType())
617                 .get().getResponseFormat();
618         assertEquals(expectedResponse.getFormattedMessage(), actualResponse.getFormattedMessage());
619         assertEquals(expectedResponse.getStatus(), actualResponse.getStatus());
620     }
621
622     @Test
623     void testToscaGetFunctionValidation_propertyTypeDivergeTest() {
624         final String userId = "userId";
625         final String containerComponentId = "containerComponentId";
626         final String containerComponentName = "containerComponentName";
627         final String resourceInstanceId = "resourceInstanceId";
628         final String inputName = "myInputToGet";
629         final String inputId = String.format("%s.%s", containerComponentId, inputName);
630         final String propertyName = "getInputProperty";
631         final String propertyId = String.format("%s.%s", containerComponentId, propertyName);
632         final String propertyType = "string";
633         final List<ComponentInstanceProperty> properties = new ArrayList<>();
634         final ComponentInstanceProperty propertyGetInput = createComponentInstanceProperty(
635             propertyId,
636             "getInputProperty",
637             propertyType,
638             "string",
639             String.format("get_input: [\"%s\"]", inputName),
640             createGetToscaFunction(inputName, inputId, List.of(propertyName), PropertySource.SELF, ToscaGetFunctionType.GET_INPUT,
641                 containerComponentId, containerComponentName)
642         );
643         properties.add(propertyGetInput);
644
645         final Component component = new Service();
646         component.setName(containerComponentName);
647         component.setUniqueId(containerComponentId);
648         component.setLastUpdaterUserId(userId);
649         component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
650
651         var inputDefinition = new InputDefinition();
652         inputDefinition.setName(inputName);
653         inputDefinition.setUniqueId(inputId);
654         inputDefinition.setType("integer");
655         component.setInputs(List.of(inputDefinition));
656
657         final Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
658         componentInstanceProps.put(resourceInstanceId, properties);
659         component.setComponentInstancesProperties(componentInstanceProps);
660
661         final ComponentInstance resourceInstance = createComponentInstance("componentInstance1");
662         resourceInstance.setUniqueId(resourceInstanceId);
663         component.setComponentInstances(List.of(resourceInstance));
664
665         mockComponentForToscaGetFunctionValidation(component);
666         //when
667         final Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither =
668             componentInstanceBusinessLogic
669                 .createOrUpdatePropertiesValues(ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentId, resourceInstanceId, properties, userId);
670         //then
671         assertTrue(responseFormatEither.isRight(), "Expecting an error");
672         final ResponseFormat actualResponse = responseFormatEither.right().value();
673         final ResponseFormat expectedResponse =
674             ToscaGetFunctionExceptionSupplier
675                 .propertyTypeDiverge(propertyGetInput.getToscaGetFunction().getFunctionType(), inputDefinition.getType(), propertyGetInput.getType())
676                 .get().getResponseFormat();
677         assertEquals(expectedResponse.getFormattedMessage(), actualResponse.getFormattedMessage());
678         assertEquals(expectedResponse.getStatus(), actualResponse.getStatus());
679     }
680
681     @Test
682     void testToscaGetFunctionValidation_toscaFunctionNotSupportedTest() {
683         final String userId = "userId";
684         final String containerComponentId = "containerComponentId";
685         final String containerComponentName = "containerComponentName";
686         final String resourceInstanceId = "resourceInstanceId";
687         final List<ComponentInstanceProperty> properties = new ArrayList<>();
688         final ComponentInstanceProperty propertyGetInput = new ComponentInstanceProperty();
689         propertyGetInput.setName("anyName");
690         final var toscaGetFunction = new ToscaGetFunctionDataDefinition();
691         toscaGetFunction.setFunctionType(ToscaGetFunctionType.GET_ATTRIBUTE);
692         toscaGetFunction.setPropertySource(PropertySource.SELF);
693         toscaGetFunction.setPropertyPathFromSource(List.of("sourcePath"));
694         toscaGetFunction.setSourceName("sourceName");
695         toscaGetFunction.setSourceUniqueId("sourceUniqueId");
696         toscaGetFunction.setPropertyName("propertyName");
697         toscaGetFunction.setPropertyUniqueId("propertyId");
698         propertyGetInput.setToscaGetFunction(toscaGetFunction);
699         properties.add(propertyGetInput);
700
701         final Component component = new Service();
702         component.setName(containerComponentName);
703         component.setUniqueId(containerComponentId);
704         component.setLastUpdaterUserId(userId);
705         component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
706
707         final Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
708         componentInstanceProps.put(resourceInstanceId, properties);
709         component.setComponentInstancesProperties(componentInstanceProps);
710
711         final ComponentInstance resourceInstance = createComponentInstance("componentInstance1");
712         resourceInstance.setUniqueId(resourceInstanceId);
713         component.setComponentInstances(List.of(resourceInstance));
714
715         mockComponentForToscaGetFunctionValidation(component);
716         //when
717         final Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither =
718             componentInstanceBusinessLogic
719                 .createOrUpdatePropertiesValues(ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentId, resourceInstanceId, properties, userId);
720         //then
721         assertTrue(responseFormatEither.isRight(), "Expecting an error");
722         final ResponseFormat actualResponse = responseFormatEither.right().value();
723         final ResponseFormat expectedResponse = ToscaGetFunctionExceptionSupplier
724             .functionNotSupported(toscaGetFunction.getFunctionType()).get().getResponseFormat();
725         assertEquals(expectedResponse.getFormattedMessage(), actualResponse.getFormattedMessage());
726         assertEquals(expectedResponse.getStatus(), actualResponse.getStatus());
727     }
728
729     @ParameterizedTest
730     @MethodSource("getToscaFunctionForValidation")
731     void testToscaGetFunctionValidation_AttributesNotFoundTest(final ToscaGetFunctionDataDefinition toscaGetFunction,
732                                                                final ResponseFormat expectedValidationResponse) {
733         final String userId = "userId";
734         final String containerComponentId = "containerComponentId";
735         final String containerComponentName = "containerComponentName";
736         final String resourceInstanceId = "resourceInstanceId";
737         final List<ComponentInstanceProperty> properties = new ArrayList<>();
738         final ComponentInstanceProperty propertyGetInput = new ComponentInstanceProperty();
739         propertyGetInput.setName("anyName");
740         propertyGetInput.setToscaGetFunction(toscaGetFunction);
741         properties.add(propertyGetInput);
742
743         final Component component = new Service();
744         component.setName(containerComponentName);
745         component.setUniqueId(containerComponentId);
746         component.setLastUpdaterUserId(userId);
747         component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
748
749         final Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
750         componentInstanceProps.put(resourceInstanceId, properties);
751         component.setComponentInstancesProperties(componentInstanceProps);
752
753         final ComponentInstance resourceInstance = createComponentInstance("componentInstance1");
754         resourceInstance.setUniqueId(resourceInstanceId);
755         component.setComponentInstances(List.of(resourceInstance));
756
757         mockComponentForToscaGetFunctionValidation(component);
758         //when
759         final Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither =
760             componentInstanceBusinessLogic
761                 .createOrUpdatePropertiesValues(ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentId, resourceInstanceId, properties, userId);
762         //then
763         assertTrue(responseFormatEither.isRight(), "Expecting an error");
764         final ResponseFormat actualResponse = responseFormatEither.right().value();
765         final ResponseFormat expectedResponse = expectedValidationResponse;
766         assertEquals(expectedResponse.getFormattedMessage(), actualResponse.getFormattedMessage());
767         assertEquals(expectedResponse.getStatus(), actualResponse.getStatus());
768     }
769
770     @Test
771     void testToscaGetFunctionValidation_propertyNotFoundTest() {
772         final String userId = "userId";
773         final String containerComponentId = "containerComponentId";
774         final String containerComponentName = "containerComponentName";
775         final String resourceInstanceId = "resourceInstanceId";
776         final String inputName = "myInputToGet";
777         final String inputId = String.format("%s.%s", containerComponentId, inputName);
778         final String propertyName = "getInputProperty";
779         final String propertyId = String.format("%s.%s", containerComponentId, propertyName);
780         final String propertyType = "string";
781         final List<ComponentInstanceProperty> properties = new ArrayList<>();
782         final ComponentInstanceProperty propertyGetInput = createComponentInstanceProperty(
783             propertyId,
784             "getInputProperty",
785             propertyType,
786             "string",
787             String.format("get_input: [\"%s\"]", inputName),
788             createGetToscaFunction(inputName, inputId, List.of(propertyName), PropertySource.SELF, ToscaGetFunctionType.GET_INPUT,
789                 containerComponentId, containerComponentName)
790         );
791         properties.add(propertyGetInput);
792
793         final Component component = new Service();
794         component.setName(containerComponentName);
795         component.setUniqueId(containerComponentId);
796         component.setLastUpdaterUserId(userId);
797         component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
798
799         final Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
800         componentInstanceProps.put(resourceInstanceId, properties);
801         component.setComponentInstancesProperties(componentInstanceProps);
802
803         final ComponentInstance resourceInstance = createComponentInstance("componentInstance1");
804         resourceInstance.setUniqueId(resourceInstanceId);
805         component.setComponentInstances(List.of(resourceInstance));
806
807         mockComponentForToscaGetFunctionValidation(component);
808         //when
809         final Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither =
810             componentInstanceBusinessLogic
811                 .createOrUpdatePropertiesValues(ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentId, resourceInstanceId, properties, userId);
812         //then
813         assertTrue(responseFormatEither.isRight(), "Expecting an error");
814         final ResponseFormat actualResponse = responseFormatEither.right().value();
815         final ResponseFormat expectedResponse =
816             ToscaGetFunctionExceptionSupplier
817                 .propertyNotFoundOnTarget(inputName, PropertySource.SELF, ToscaGetFunctionType.GET_INPUT)
818                 .get().getResponseFormat();
819         assertEquals(expectedResponse.getFormattedMessage(), actualResponse.getFormattedMessage());
820         assertEquals(expectedResponse.getStatus(), actualResponse.getStatus());
821     }
822
823     @Test
824     void testCreateOrUpdatePropertiesValuesPropertyNotExists() {
825         String containerComponentID = "containerId";
826         String resourceInstanceId = "resourceId";
827         String componentInstanceID = "componentInstance";
828         List<ComponentInstanceProperty> properties = new ArrayList<>();
829         ComponentInstanceProperty property = new ComponentInstanceProperty();
830         property.setName("property");
831         property.setValue("newVal");
832         property.setType("string");
833         properties.add(property);
834
835         List<ComponentInstanceProperty> origProperties = new ArrayList<>();
836
837         Component component = new Service();
838         component.setLastUpdaterUserId("userId");
839         component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
840         Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
841         componentInstanceProps.put("resourceId", origProperties);
842         component.setComponentInstancesProperties(componentInstanceProps);
843         ComponentInstance ci = createComponentInstance("ci1");
844         ci.setUniqueId("resourceId");
845         component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
846             createComponentInstance(componentInstanceID)));
847
848         when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
849             .thenReturn(Either.left(component));
850         when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
851             .thenReturn(StorageOperationStatus.OK);
852         when(graphLockOperation.unlockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
853             .thenReturn(StorageOperationStatus.OK);
854
855         try {
856             componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
857                 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
858         } catch (ComponentException e) {
859             assertThat(e.getActionStatus()).isEqualTo(ActionStatus.PROPERTY_NOT_FOUND);
860         }
861
862     }
863
864     @Test
865     void testCreateOrUpdatePropertiesValuesValidationFailure() {
866         String containerComponentID = "containerId";
867         String resourceInstanceId = "resourceId";
868         String componentInstanceID = "componentInstance";
869         List<ComponentInstanceProperty> properties = new ArrayList<>();
870         ComponentInstanceProperty property = new ComponentInstanceProperty();
871         property.setName("property");
872         property.setValue("newVal");
873         property.setType("string");
874         properties.add(property);
875
876         List<ComponentInstanceProperty> origProperties = new ArrayList<>();
877         ComponentInstanceProperty origProperty = new ComponentInstanceProperty();
878         origProperty.setName("property");
879         origProperty.setValue("value");
880         origProperty.setType("string");
881         origProperties.add(origProperty);
882
883         Map<String, DataTypeDefinition> types = new HashMap<>();
884         DataTypeDefinition dataTypeDef = new DataTypeDefinition();
885         types.put("string", dataTypeDef);
886
887         Component component = new Service();
888         component.setLastUpdaterUserId("userId");
889         component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
890         Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
891         componentInstanceProps.put("resourceId", origProperties);
892         component.setComponentInstancesProperties(componentInstanceProps);
893         ComponentInstance ci = createComponentInstance("ci1");
894         ci.setUniqueId("resourceId");
895         component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
896             createComponentInstance(componentInstanceID)));
897
898         when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
899             .thenReturn(Either.left(component));
900         when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
901             .thenReturn(StorageOperationStatus.OK);
902         when(componentsUtils.getAllDataTypes(applicationDataTypeCache, component.getModel())).thenReturn(types);
903         when(propertyOperation.validateAndUpdatePropertyValue(property.getType(), "newVal", true, null, types))
904             .thenReturn(Either.right(false));
905         when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.BAD_REQUEST))
906             .thenReturn(ActionStatus.INVALID_CONTENT);
907
908         final Either<List<ComponentInstanceProperty>, ResponseFormat> response = componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
909             ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
910         assertThat(response.isRight()).as("Response should be an error").isTrue();
911         final ResponseFormat responseFormat = response.right().value();
912         assertThat(responseFormat.getStatus()).as("Response status should be as expected").isEqualTo(400);
913         assertThat(responseFormat.getMessageId()).as("Error message id should be as expected").isEqualTo("SVC4000");
914     }
915
916     @Test
917     void testCreateOrUpdatePropertiesValuesMissingFieldFailure() {
918         String containerComponentID = "containerId";
919         String resourceInstanceId = "resourceId";
920         String componentInstanceID = "componentInstance";
921         List<ComponentInstanceProperty> properties = new ArrayList<>();
922         ComponentInstanceProperty property = new ComponentInstanceProperty();
923         property.setValue("newVal");
924         property.setType("string");
925         properties.add(property);
926
927         List<ComponentInstanceProperty> origProperties = new ArrayList<>();
928         ComponentInstanceProperty origProperty = new ComponentInstanceProperty();
929         origProperty.setName("property");
930         origProperty.setValue("value");
931         origProperty.setType("string");
932         origProperties.add(origProperty);
933
934         Component component = new Service();
935         component.setLastUpdaterUserId("userId");
936         component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
937         Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
938         componentInstanceProps.put("resourceId", origProperties);
939         component.setComponentInstancesProperties(componentInstanceProps);
940         ComponentInstance ci = createComponentInstance("ci1");
941         ci.setUniqueId("resourceId");
942         component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
943             createComponentInstance(componentInstanceID)));
944
945         when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
946             .thenReturn(Either.left(component));
947         when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
948             .thenReturn(StorageOperationStatus.OK);
949
950         try {
951             componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
952                 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
953         } catch (ComponentException e) {
954             assertThat(e.getActionStatus()).isEqualTo(ActionStatus.MISSING_PROPERTY_NAME);
955         }
956     }
957
958     @Test
959     void testDeleteForwardingPathsWhenComponentinstanceDeleted() {
960
961         ComponentTypeEnum containerComponentType = ComponentTypeEnum.findByParamName("services");
962         String containerComponentID = "Service-comp";
963         String componentInstanceID = "NodeA1";
964         Service component = new Service();
965         component
966             .setComponentInstances(Arrays.asList(createComponentInstance("NodeA2"), createComponentInstance("NodeB2"),
967                 createComponentInstance(componentInstanceID)));
968
969         component.addForwardingPath(createPath("path1", componentInstanceID, "NodeB1", "1"));
970         component.addForwardingPath(createPath("Path2", "NodeA2", "NodeB2", "2"));
971         when(toscaOperationFacade.getToscaElement(eq(containerComponentID), any(ComponentParametersView.class)))
972             .thenReturn(Either.left(component));
973         when(toscaOperationFacade.getToscaElement(containerComponentID)).thenReturn(Either.left(component));
974         when(forwardingPathOperation.deleteForwardingPath(any(Service.class), anySet()))
975             .thenReturn(Either.left(new HashSet<>()));
976         final ComponentInstance ci = new ComponentInstance();
977         ci.setName(componentInstanceID);
978         ComponentInstance responseFormatEither = componentInstanceBusinessLogic
979             .deleteForwardingPathsRelatedTobeDeletedComponentInstance(
980                 containerComponentID, containerComponentType, ci);
981         assertFalse(responseFormatEither.isEmpty());
982     }
983
984     @Test
985     void testAddComponentInstanceDeploymentArtifacts() {
986
987         Component containerComponent = new Service();
988         ComponentInstance componentInstance = new ComponentInstance();
989         componentInstance.setUniqueId(COMPONENT_INSTANCE_ID);
990         Component originComponent = fillOriginComponent(new Resource());
991
992         Map<String, ArtifactDefinition> artifacts = new HashMap<>();
993         ArtifactDefinition deploymentArtifact1 = getArtifact("deploymentArtifact1", ArtifactTypeEnum.HEAT.getType());
994         artifacts.put(deploymentArtifact1.getArtifactLabel(), deploymentArtifact1);
995         ArtifactDefinition deploymentArtifact2 = getArtifact("deploymentArtifact2",
996             ArtifactTypeEnum.HEAT_ENV.getType());
997         artifacts.put(deploymentArtifact2.getArtifactLabel(), deploymentArtifact2);
998         ArtifactDefinition deploymentArtifact3 = getArtifact("deploymentArtifact3",
999             ArtifactTypeEnum.HEAT_VOL.getType());
1000         artifacts.put(deploymentArtifact3.getArtifactLabel(), deploymentArtifact3);
1001         ArtifactDefinition heatEnvPlaceHolder = getArtifact("deploymentArtifact4", ArtifactTypeEnum.HEAT_ENV.getType());
1002         ArtifactDefinition heatEnvPlaceHolder2 = getArtifact("deploymentArtifact5",
1003             ArtifactTypeEnum.HEAT_ENV.getType());
1004
1005         Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getResourceDeploymentArtifacts = Either
1006             .left(artifacts);
1007
1008         Map<String, ArtifactDefinition> finalDeploymentArtifacts = new HashMap<>();
1009         finalDeploymentArtifacts.put(deploymentArtifact1.getArtifactLabel(), deploymentArtifact1);
1010         finalDeploymentArtifacts.put(deploymentArtifact3.getArtifactLabel(), deploymentArtifact3);
1011         finalDeploymentArtifacts.put(heatEnvPlaceHolder.getArtifactLabel(), heatEnvPlaceHolder);
1012         finalDeploymentArtifacts.put(heatEnvPlaceHolder2.getArtifactLabel(), heatEnvPlaceHolder2);
1013         when(artifactsBusinessLogic.getArtifacts(componentInstance.getComponentUid(), NodeTypeEnum.Resource,
1014             ArtifactGroupTypeEnum.DEPLOYMENT, null)).thenReturn(getResourceDeploymentArtifacts);
1015         when(artifactsBusinessLogic.createHeatEnvPlaceHolder(new ArrayList<>(),
1016             deploymentArtifact1, ArtifactsBusinessLogic.HEAT_ENV_NAME, componentInstance.getUniqueId(),
1017             NodeTypeEnum.ResourceInstance, componentInstance.getName(), user, containerComponent,
1018             null)).thenReturn(heatEnvPlaceHolder);
1019         when(artifactsBusinessLogic.createHeatEnvPlaceHolder(new ArrayList<>(),
1020             deploymentArtifact3, ArtifactsBusinessLogic.HEAT_ENV_NAME, componentInstance.getUniqueId(),
1021             NodeTypeEnum.ResourceInstance, componentInstance.getName(), user, containerComponent,
1022             null)).thenReturn(heatEnvPlaceHolder2);
1023
1024         componentInstanceBusinessLogic.setToscaOperationFacade(toscaOperationFacade);
1025         when(toscaOperationFacade.addDeploymentArtifactsToInstance(containerComponent.getUniqueId(), componentInstance,
1026             finalDeploymentArtifacts)).thenReturn(StorageOperationStatus.OK);
1027         when(toscaOperationFacade
1028             .addGroupInstancesToComponentInstance(containerComponent, componentInstance, new ArrayList<>(),
1029                 new HashMap<>()))
1030             .thenReturn(StorageOperationStatus.OK);
1031         when(toscaOperationFacade
1032             .addInformationalArtifactsToInstance(containerComponent.getUniqueId(), componentInstance, null))
1033             .thenReturn(StorageOperationStatus.OK);
1034
1035         ActionStatus status = componentInstanceBusinessLogic.addComponentInstanceArtifacts(containerComponent,
1036             componentInstance, originComponent, user, null);
1037
1038         assertThat(status).isEqualTo(ActionStatus.OK);
1039
1040     }
1041
1042     private Component fillOriginComponent(Resource originComponent) {
1043         originComponent.setUniqueId("resourceId");
1044         originComponent.setUniqueId(ORIGIN_COMPONENT_ID);
1045         originComponent.setComponentInstances(Lists.newArrayList(toInstance, fromInstance));
1046         originComponent.setComponentType(ComponentTypeEnum.RESOURCE);
1047         originComponent.setState(LifecycleStateEnum.CERTIFIED);
1048         return originComponent;
1049     }
1050
1051     private ArtifactDefinition getArtifact(String artifactLabel, String artifactType) {
1052         ArtifactDefinition artifactDefinition = new ArtifactDefinition();
1053         artifactDefinition.setArtifactLabel(artifactLabel);
1054         artifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
1055         artifactDefinition.setEsId("esId" + artifactLabel);
1056         artifactDefinition.setArtifactType(artifactType);
1057         artifactDefinition.setArtifactName("artifactName");
1058         return artifactDefinition;
1059     }
1060
1061     private ComponentInstance createComponentInstance(String path1) {
1062         ComponentInstance componentInstance = new ComponentInstance();
1063         componentInstance.setName(path1);
1064         return componentInstance;
1065     }
1066
1067     private ForwardingPathDataDefinition createPath(String pathName, String fromNode, String toNode, String uniqueId) {
1068         ForwardingPathDataDefinition forwardingPath = new ForwardingPathDataDefinition(pathName);
1069         forwardingPath.setProtocol("protocol");
1070         forwardingPath.setDestinationPortNumber("port");
1071         forwardingPath.setUniqueId(uniqueId);
1072         ListDataDefinition<ForwardingPathElementDataDefinition> forwardingPathElementListDataDefinition =
1073             new ListDataDefinition<>();
1074         forwardingPathElementListDataDefinition
1075             .add(new ForwardingPathElementDataDefinition(fromNode, toNode, "nodeAcpType", "nodeBcpType",
1076                 "nodeDcpName", "nodeBcpName"));
1077         forwardingPath.setPathElements(forwardingPathElementListDataDefinition);
1078
1079         return forwardingPath;
1080     }
1081
1082     private Map<String, ForwardingPathDataDefinition> generateForwardingPath(String componentInstanceID) {
1083         ForwardingPathDataDefinition forwardingPath = new ForwardingPathDataDefinition("fpName");
1084         String protocol = "protocol";
1085         forwardingPath.setProtocol(protocol);
1086         forwardingPath.setDestinationPortNumber("DestinationPortNumber");
1087         forwardingPath.setUniqueId("FP-ID-1");
1088         ListDataDefinition<ForwardingPathElementDataDefinition> forwardingPathElementListDataDefinition =
1089             new ListDataDefinition<>();
1090         forwardingPathElementListDataDefinition
1091             .add(new ForwardingPathElementDataDefinition(componentInstanceID, "nodeB", "nodeA_FORWARDER_CAPABILITY",
1092                 "nodeBcpType", "nodeDcpName", "nodeBcpName"));
1093         forwardingPath.setPathElements(forwardingPathElementListDataDefinition);
1094         Map<String, ForwardingPathDataDefinition> forwardingPaths = new HashMap<>();
1095         forwardingPaths.put("1122", forwardingPath);
1096         return forwardingPaths;
1097     }
1098
1099     private void getServiceRelationByIdSuccess(Component component) {
1100         Either<Component, StorageOperationStatus> getComponentRes = Either.left(component);
1101         when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
1102             .thenReturn(getComponentRes);
1103         Either<RequirementCapabilityRelDef, ResponseFormat> response = componentInstanceBusinessLogic
1104             .getRelationById(COMPONENT_ID,
1105                 RELATION_ID, USER_ID,
1106                 component.getComponentType());
1107         assertThat(response.isLeft()).isTrue();
1108     }
1109
1110     private void getServiceRelationByIdUserValidationFailure(Component component) {
1111         doThrow(new ByActionStatusComponentException(ActionStatus.USER_NOT_FOUND)).when(userValidations).validateUserExists(USER_ID);
1112         try {
1113             componentInstanceBusinessLogic
1114                 .getRelationById(COMPONENT_ID, RELATION_ID, USER_ID, component.getComponentType());
1115         } catch (ByActionStatusComponentException e) {
1116             assertSame(ActionStatus.USER_NOT_FOUND, e.getActionStatus());
1117         }
1118     }
1119
1120     private void getRelationByIdComponentNotFoundFailure(Component component) {
1121         Either<Component, StorageOperationStatus> getComponentRes = Either.right(StorageOperationStatus.NOT_FOUND);
1122         when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
1123             .thenReturn(getComponentRes);
1124
1125         Either<RequirementCapabilityRelDef, ResponseFormat> response = componentInstanceBusinessLogic
1126             .getRelationById(COMPONENT_ID,
1127                 RELATION_ID, USER_ID,
1128                 component.getComponentType());
1129         assertThat(response.isRight()).isTrue();
1130     }
1131
1132     private void stubMethods() {
1133         Mockito.lenient().when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(user);
1134         Mockito.lenient().when(componentsUtils
1135             .convertFromStorageResponse(eq(StorageOperationStatus.GENERAL_ERROR), any(ComponentTypeEnum.class)))
1136             .thenReturn(ActionStatus.GENERAL_ERROR);
1137     }
1138
1139     private void createComponents() {
1140         createRelation();
1141         createInstances();
1142         createProperties();
1143         createInputs();
1144         createService();
1145         resource = createResource();
1146     }
1147
1148     private Resource createResource() {
1149         final Resource resource = new Resource();
1150         resource.setUniqueId(COMPONENT_ID);
1151         resource.setComponentInstancesRelations(Lists.newArrayList(relation));
1152         resource.setComponentInstances(Lists.newArrayList(toInstance, fromInstance));
1153         resource.setCapabilities(toInstance.getCapabilities());
1154         resource.setRequirements(fromInstance.getRequirements());
1155         resource.setComponentType(ComponentTypeEnum.RESOURCE);
1156         resource.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1157         return resource;
1158     }
1159
1160     private void createService() {
1161         service = new Service();
1162         service.setUniqueId(COMPONENT_ID);
1163         service.setComponentInstancesRelations(Lists.newArrayList(relation));
1164         service.setComponentInstances(Lists.newArrayList(toInstance, fromInstance));
1165         service.setCapabilities(toInstance.getCapabilities());
1166         service.setRequirements(fromInstance.getRequirements());
1167         service.setComponentType(ComponentTypeEnum.SERVICE);
1168         service.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1169         service.setLastUpdaterUserId(USER_ID);
1170
1171         Map<String, List<ComponentInstanceProperty>> ciPropertyMap = new HashMap<>();
1172         ciPropertyMap.put(TO_INSTANCE_ID, ciPropertyList);
1173         service.setComponentInstancesProperties(ciPropertyMap);
1174
1175         Map<String, List<ComponentInstanceInput>> ciInputMap = new HashMap<>();
1176         ciInputMap.put(TO_INSTANCE_ID, ciInputList);
1177         service.setComponentInstancesInputs(ciInputMap);
1178     }
1179
1180     private void createInstances() {
1181         toInstance = new ComponentInstance();
1182         toInstance.setUniqueId(TO_INSTANCE_ID);
1183         toInstance.setName(TO_INSTANCE_NAME);
1184
1185         fromInstance = new ComponentInstance();
1186         fromInstance.setUniqueId(FROM_INSTANCE_ID);
1187
1188         CapabilityDataDefinition capability = new CapabilityDataDefinition();
1189         capability.setOwnerId(CAPABILITY_OWNER_ID);
1190         capability.setUniqueId(CAPABILITY_UID);
1191         capability.setName(CAPABILITY_NAME);
1192
1193         Map<String, List<CapabilityDefinition>> capabilities = new HashMap<>();
1194         final CapabilityDefinition capabilityDefinition = new CapabilityDefinition(capability);
1195         final ArrayList<ComponentInstanceProperty> properties = new ArrayList<>();
1196         properties.add(componentInstancePropInput);
1197         capabilityDefinition.setProperties(properties);
1198         capabilities.put(capability.getName(), Lists.newArrayList(capabilityDefinition));
1199
1200         RequirementDataDefinition requirement = new RequirementDataDefinition();
1201         requirement.setOwnerId(REQUIREMENT_OWNER_ID);
1202         requirement.setUniqueId(REQUIREMENT_UID);
1203         requirement.setName(REQUIREMENT_NAME);
1204         requirement.setRelationship(RELATIONSHIP_TYPE);
1205
1206         Map<String, List<RequirementDefinition>> requirements = new HashMap<>();
1207         requirements.put(requirement.getCapability(), Lists.newArrayList(new RequirementDefinition(requirement)));
1208
1209         toInstance.setCapabilities(capabilities);
1210         fromInstance.setRequirements(requirements);
1211
1212     }
1213
1214     private void createRelation() {
1215
1216         relation = new RequirementCapabilityRelDef();
1217         CapabilityRequirementRelationship relationship = new CapabilityRequirementRelationship();
1218         RelationshipInfo relationInfo = new RelationshipInfo();
1219         relationInfo.setId(RELATION_ID);
1220         relationship.setRelation(relationInfo);
1221
1222         relation.setRelationships(Lists.newArrayList(relationship));
1223         relation.setToNode(TO_INSTANCE_ID);
1224         relation.setFromNode(FROM_INSTANCE_ID);
1225
1226         relationInfo.setCapabilityOwnerId(CAPABILITY_OWNER_ID);
1227         relationInfo.setCapabilityUid(CAPABILITY_UID);
1228         relationInfo.setCapability(CAPABILITY_NAME);
1229         relationInfo.setRequirementOwnerId(REQUIREMENT_OWNER_ID);
1230         relationInfo.setRequirementUid(REQUIREMENT_UID);
1231         relationInfo.setRequirement(REQUIREMENT_NAME);
1232         RelationshipImpl relationshipImpl = new RelationshipImpl();
1233         relationshipImpl.setType(RELATIONSHIP_TYPE);
1234         relationInfo.setRelationships(relationshipImpl);
1235     }
1236
1237     private void createProperties() {
1238         // Create GetInputValueData
1239         GetInputValueDataDefinition inputValueDef = new GetInputValueDataDefinition();
1240         inputValueDef.setInputId(INPUT_ID);
1241         List<GetInputValueDataDefinition> inputValueDefList = new ArrayList<>();
1242         inputValueDefList.add(inputValueDef);
1243         // Create ComponentInstanceProperty
1244         ComponentInstanceProperty ciProperty = new ComponentInstanceProperty();
1245         ciProperty.setGetInputValues(inputValueDefList);
1246         ciProperty.setName(PROP_NAME);
1247         // Create ComponentInstanceProperty list
1248         ciPropertyList = new ArrayList<>();
1249         ciPropertyList.add(ciProperty);
1250     }
1251
1252     private void createInputs() {
1253         // Create GetInputValueData
1254         GetInputValueDataDefinition inputValueDef = new GetInputValueDataDefinition();
1255         inputValueDef.setInputId(INPUT_ID);
1256         List<GetInputValueDataDefinition> inputValueDefList = new ArrayList<>();
1257         inputValueDefList.add(inputValueDef);
1258         // Create ComponentInstanceInput
1259         ComponentInstanceInput ciInput = new ComponentInstanceInput();
1260         ciInput.setUniqueId(INPUT_ID);
1261         ciInput.setName(PROP_NAME);
1262         ciInput.setGetInputValues(inputValueDefList);
1263         // Create ComponentInstanceInput list
1264         ciInputList = new ArrayList<>();
1265         ciInputList.add(ciInput);
1266     }
1267
1268     private ComponentInstanceBusinessLogic createTestSubject() {
1269         return componentInstanceBusinessLogic;
1270     }
1271
1272     @Test
1273     void testChangeServiceProxyVersion() {
1274         ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
1275
1276         Either<ComponentInstance, ResponseFormat> result;
1277
1278         // default test
1279         componentInstanceBusinessLogic = createTestSubject();
1280         result = componentInstanceBusinessLogic.changeServiceProxyVersion();
1281         assertNotNull(result);
1282     }
1283
1284     @Test
1285     void testCreateServiceProxy() {
1286         ComponentInstanceBusinessLogic testSubject;
1287         Either<ComponentInstance, ResponseFormat> result;
1288
1289         // default test
1290         testSubject = createTestSubject();
1291         result = testSubject.createServiceProxy();
1292         assertNotNull(result);
1293     }
1294
1295     @Test
1296     void testDeleteServiceProxy() {
1297         ComponentInstanceBusinessLogic testSubject;
1298
1299         Either<ComponentInstance, ResponseFormat> result;
1300
1301         // default test
1302         testSubject = createTestSubject();
1303         result = testSubject.deleteServiceProxy();
1304         assertNotNull(result);
1305     }
1306
1307     @Test
1308     void testGetComponentInstanceInputsByInputIdEmpty() {
1309         Component component = new Service();
1310         String inputId = "";
1311         List<ComponentInstanceInput> result;
1312
1313         result = componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(component, inputId);
1314         assertNotNull(result);
1315         assertThat(result.isEmpty()).isTrue();
1316     }
1317
1318     @Test
1319     void testGetComponentInstanceInputsByInputIdPresent() {
1320         List<ComponentInstanceInput> result;
1321
1322         result = componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(service, INPUT_ID);
1323         assertNotNull(result);
1324         assertThat(result.isEmpty()).isFalse();
1325         assertThat(result.size()).isOne();
1326         ComponentInstanceInput resultInput = result.get(0);
1327         assertThat(resultInput.getComponentInstanceId()).isEqualTo(TO_INSTANCE_ID);
1328         assertThat(resultInput.getComponentInstanceName()).isEqualTo(TO_INSTANCE_NAME);
1329     }
1330
1331     @Test
1332     void testGetComponentInstancePropertiesByInputIdEmpty() {
1333         Component component = new Service();
1334         String inputId = "";
1335         List<ComponentInstanceProperty> result;
1336
1337         result = componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(component, inputId);
1338         assertNotNull(result);
1339         assertThat(result.isEmpty()).isTrue();
1340     }
1341
1342     @Test
1343     void testGetComponentInstancePropertiesByInputIdPresent() {
1344         List<ComponentInstanceProperty> result;
1345
1346         result = componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(service, INPUT_ID);
1347         assertNotNull(result);
1348         assertThat(result.size()).isOne();
1349         ComponentInstanceProperty resultProperty = result.get(0);
1350         assertThat(resultProperty.getComponentInstanceId()).isEqualTo(TO_INSTANCE_ID);
1351         assertThat(resultProperty.getComponentInstanceName()).isEqualTo(TO_INSTANCE_NAME);
1352     }
1353
1354     @Test
1355     void testGetRelationById() {
1356         ComponentInstanceBusinessLogic testSubject;
1357         String componentId = "";
1358         String relationId = "";
1359         String userId = user.getUserId();
1360         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE_INSTANCE;
1361         Either<RequirementCapabilityRelDef, ResponseFormat> result;
1362
1363         // default test
1364         testSubject = createTestSubject();
1365         result = testSubject.getRelationById(componentId, relationId, userId, componentTypeEnum);
1366         assertNotNull(result);
1367     }
1368
1369     @Test
1370     void testValidateParent() {
1371         ComponentInstanceBusinessLogic testSubject;
1372         resource = createResource();
1373         String nodeTemplateId = "";
1374         boolean result;
1375
1376         // default test
1377         testSubject = createTestSubject();
1378         result = Deencapsulation.invoke(testSubject, "validateParent", new Object[]{resource, nodeTemplateId});
1379         assertFalse(result);
1380     }
1381
1382     @Test
1383     void testGetComponentType() {
1384         ComponentInstanceBusinessLogic testSubject;
1385         ComponentTypeEnum result;
1386
1387         // default test
1388         testSubject = createTestSubject();
1389         result = Deencapsulation.invoke(testSubject, "getComponentType", new Object[]{ComponentTypeEnum.class});
1390         assertNotNull(result);
1391     }
1392
1393     @Test
1394     void testGetNewGroupName() {
1395         ComponentInstanceBusinessLogic testSubject;
1396         String oldPrefix = "";
1397         String newNormailzedPrefix = "";
1398         String qualifiedGroupInstanceName = "";
1399         String result;
1400
1401         // test 1
1402         testSubject = createTestSubject();
1403         result = Deencapsulation.invoke(testSubject, "getNewGroupName",
1404             new Object[]{oldPrefix, newNormailzedPrefix, qualifiedGroupInstanceName});
1405         assertNotNull(result);
1406     }
1407
1408     @Test
1409     void testUpdateComponentInstanceMetadata_3() {
1410         ComponentInstanceBusinessLogic testSubject;
1411         createInstances();
1412         ComponentInstance result;
1413
1414         // default test
1415         testSubject = createTestSubject();
1416         result = Deencapsulation
1417             .invoke(testSubject, "updateComponentInstanceMetadata", new Object[]{toInstance, toInstance});
1418         assertNotNull(result);
1419     }
1420
1421     @Test
1422     void testFindRelation() {
1423         ComponentInstanceBusinessLogic testSubject;
1424         String relationId = "";
1425         List<RequirementCapabilityRelDef> requirementCapabilityRelations = new ArrayList<>();
1426         RequirementCapabilityRelDef result;
1427
1428         // default test
1429         testSubject = createTestSubject();
1430         result = Deencapsulation.invoke(testSubject, "findRelation",
1431             new Object[]{relationId, requirementCapabilityRelations});
1432         assertNull(result);
1433     }
1434
1435     @Test
1436     void testCreateOrUpdatePropertiesValues() {
1437         ComponentInstanceBusinessLogic testSubject;
1438         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1439         resource = createResource();
1440         String componentId = resource.getUniqueId();
1441         String resourceInstanceId = "";
1442         List<ComponentInstanceProperty> properties = new ArrayList<>();
1443         String userId = user.getUserId();
1444         Either<List<ComponentInstanceProperty>, ResponseFormat> result;
1445
1446         when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll))
1447             .thenReturn(Either.left(resource));
1448
1449         // test 1
1450         testSubject = createTestSubject();
1451         result = testSubject
1452             .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
1453                 userId);
1454         assertNotNull(result);
1455
1456         componentTypeEnum = null;
1457         result = testSubject
1458             .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
1459                 userId);
1460         assertNotNull(result);
1461
1462         result = testSubject
1463             .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
1464                 userId);
1465         assertNotNull(result);
1466     }
1467
1468     @Test
1469     void testUpdateCapabilityPropertyOnContainerComponent() {
1470         ComponentInstanceBusinessLogic testSubject;
1471         ComponentInstanceProperty property = new ComponentInstanceProperty();
1472         String newValue = "";
1473         resource = createResource();
1474         createInstances();
1475         String capabilityType = "";
1476         String capabilityName = "";
1477         ResponseFormat result;
1478
1479         // default test
1480         testSubject = createTestSubject();
1481         result = Deencapsulation.invoke(testSubject, "updateCapabilityPropertyOnContainerComponent",
1482             new Object[]{property, newValue, resource, toInstance, capabilityType, capabilityName});
1483         assertNull(result);
1484     }
1485
1486     @Test
1487     void testCreateOrUpdateInstanceInputValues() {
1488         ComponentInstanceBusinessLogic testSubject;
1489         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1490         resource = createResource();
1491         String componentId = resource.getUniqueId();
1492         String resourceInstanceId = "";
1493         List<ComponentInstanceInput> inputs = new ArrayList<>();
1494         String userId = user.getUserId();
1495         Either<List<ComponentInstanceInput>, ResponseFormat> result;
1496
1497         when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll))
1498             .thenReturn(Either.left(resource));
1499
1500         // test 1
1501         testSubject = createTestSubject();
1502         result = testSubject
1503             .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
1504                 userId);
1505         assertNotNull(result);
1506         componentTypeEnum = null;
1507         result = testSubject
1508             .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
1509                 userId);
1510         assertNotNull(result);
1511
1512         result = testSubject
1513             .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
1514                 userId);
1515         assertNotNull(result);
1516     }
1517
1518     @Test
1519     void testCreateOrUpdateGroupInstancePropertyValue() {
1520         ComponentInstanceBusinessLogic testSubject;
1521         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1522         resource = createResource();
1523         String componentId = resource.getUniqueId();
1524         String resourceInstanceId = "";
1525         String groupInstanceId = "";
1526         ComponentInstanceProperty property = new ComponentInstanceProperty();
1527         String userId = user.getUserId();
1528         Either<ComponentInstanceProperty, ResponseFormat> result;
1529
1530         when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseMetadata))
1531             .thenReturn(Either.left(resource));
1532
1533         // test 1
1534         testSubject = createTestSubject();
1535         result = testSubject
1536             .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1537                 groupInstanceId, property, userId);
1538         assertNotNull(result);
1539         componentTypeEnum = null;
1540         result = testSubject
1541             .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1542                 groupInstanceId, property, userId);
1543         assertNotNull(result);
1544
1545         result = testSubject
1546             .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1547                 groupInstanceId, property, userId);
1548         assertNotNull(result);
1549     }
1550
1551     @Test
1552     void testDeletePropertyValue() {
1553         ComponentInstanceBusinessLogic testSubject;
1554         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1555         createService();
1556         String serviceId = service.getUniqueId();
1557         String resourceInstanceId = "";
1558         String propertyValueId = "";
1559         String userId = user.getUserId();
1560         Either<ComponentInstanceProperty, ResponseFormat> result;
1561
1562         when(toscaOperationFacade.getToscaElement(serviceId, JsonParseFlagEnum.ParseMetadata))
1563             .thenReturn(Either.left(service));
1564
1565         // test 1
1566         testSubject = createTestSubject();
1567         result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1568             userId);
1569         assertNotNull(result);
1570         componentTypeEnum = null;
1571         result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1572             userId);
1573         assertNotNull(result);
1574
1575         result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1576             userId);
1577         assertNotNull(result);
1578     }
1579
1580     @Test
1581     void testGetComponentParametersViewForForwardingPath() {
1582         ComponentInstanceBusinessLogic testSubject;
1583         ComponentParametersView result;
1584
1585         // default test
1586         testSubject = createTestSubject();
1587         result = Deencapsulation.invoke(testSubject, "getComponentParametersViewForForwardingPath");
1588         assertNotNull(result);
1589     }
1590
1591     @Test
1592     void testGetResourceInstanceById() {
1593         ComponentInstanceBusinessLogic testSubject;
1594         resource = createResource();
1595         String instanceId = "";
1596         Either<ComponentInstance, StorageOperationStatus> result;
1597
1598         // default test
1599         testSubject = createTestSubject();
1600         result = Deencapsulation.invoke(testSubject, "getResourceInstanceById", new Object[]{resource, instanceId});
1601         assertNotNull(result);
1602     }
1603
1604     @Test
1605     void testUpdateInstanceCapabilityProperties_1() {
1606         ComponentInstanceBusinessLogic testSubject;
1607         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1608         resource = createResource();
1609         String containerComponentId = resource.getUniqueId();
1610         String componentInstanceUniqueId = "";
1611         String capabilityType = "";
1612         String capabilityName = "";
1613         List<ComponentInstanceProperty> properties = new ArrayList<>();
1614         String userId = user.getUserId();
1615         Either<List<ComponentInstanceProperty>, ResponseFormat> result;
1616
1617         when(toscaOperationFacade.getToscaFullElement(containerComponentId))
1618             .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
1619         // test 1
1620         testSubject = createTestSubject();
1621         result = testSubject.updateInstanceCapabilityProperties(componentTypeEnum, containerComponentId,
1622             componentInstanceUniqueId, capabilityType, capabilityName, properties, userId);
1623         assertNotNull(result);
1624         when(toscaOperationFacade.getToscaFullElement(containerComponentId)).thenReturn(Either.left(resource));
1625         result = testSubject.updateInstanceCapabilityProperties(componentTypeEnum, containerComponentId,
1626             componentInstanceUniqueId, capabilityType, capabilityName, properties, userId);
1627         assertNotNull(result);
1628     }
1629
1630     @Test
1631     void testUpdateInstanceRequirement() {
1632         ComponentInstanceBusinessLogic testSubject;
1633         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1634         createComponents();
1635         String userId = "userId";
1636         resource.setLastUpdaterUserId(userId);
1637         String containerComponentId = resource.getUniqueId();
1638         String componentInstanceUniqueId = TO_INSTANCE_ID;
1639         String capabilityType = "";
1640         String capabilityName = "";
1641         RequirementDefinition requirementDef = new RequirementDefinition();
1642
1643         Either<RequirementDefinition, ResponseFormat> result;
1644
1645         when(toscaOperationFacade.getToscaFullElement(containerComponentId)).thenReturn(Either.left(resource));
1646         testSubject = createTestSubject();
1647         when(toscaOperationFacade.updateComponentInstanceRequirement(containerComponentId, TO_INSTANCE_ID, requirementDef)).thenReturn(StorageOperationStatus.OK);
1648         when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(resource)).thenReturn(Either.left(resource));
1649         when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource)))
1650             .thenReturn(StorageOperationStatus.OK);
1651         when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource)))
1652             .thenReturn(StorageOperationStatus.OK);
1653
1654         result = testSubject.updateInstanceRequirement(componentTypeEnum, containerComponentId,
1655             componentInstanceUniqueId, requirementDef, userId);
1656         assertEquals(requirementDef, result.left().value());
1657
1658     }
1659
1660     @Test
1661     void testCopyComponentInstanceWrongUserId() {
1662
1663         Either<Map<String, ComponentInstance>, ResponseFormat> result;
1664         ComponentInstance inputComponentInstance = createComponetInstanceFromComponent(resource);
1665         String containerComponentId = service.getUniqueId();
1666         String componentInstanceId = resource.getUniqueId();
1667         String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1668         service.setLastUpdaterUserId("wrong user id");
1669
1670         Either<Component, StorageOperationStatus> leftServiceOp = Either.left(service);
1671         when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(leftServiceOp);
1672         when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1673             .thenReturn(leftServiceOp);
1674         when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
1675         when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1676             .thenReturn(StorageOperationStatus.OK);
1677         when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1678             .thenReturn(StorageOperationStatus.OK);
1679
1680         result = componentInstanceBusinessLogic
1681             .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId,
1682                 USER_ID);
1683         assertNotNull(result);
1684
1685         service.setLastUpdaterUserId(oldLastUpdatedUserId);
1686         assertThat(result.isRight()).isTrue();
1687     }
1688
1689     @Test
1690     void testCopyComponentInstanceComponentWrongState() {
1691         Either<Map<String, ComponentInstance>, ResponseFormat> result;
1692         ComponentInstance inputComponentInstance = createComponetInstanceFromComponent(resource);
1693         String containerComponentId = service.getUniqueId();
1694         String componentInstanceId = resource.getUniqueId();
1695         String oldServiceLastUpdatedUserId = service.getLastUpdaterUserId();
1696         service.setLastUpdaterUserId(USER_ID);
1697
1698         Either<Component, StorageOperationStatus> leftServiceOp = Either.left(service);
1699         when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(leftServiceOp);
1700         when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1701             .thenReturn(leftServiceOp);
1702         when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
1703         when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1704             .thenReturn(StorageOperationStatus.OK);
1705         when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1706             .thenReturn(StorageOperationStatus.OK);
1707         result = componentInstanceBusinessLogic
1708             .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId, USER_ID);
1709         assertNotNull(result);
1710         service.setLastUpdaterUserId(oldServiceLastUpdatedUserId);
1711         assertThat(result.isRight()).isTrue();
1712     }
1713
1714     @Test
1715     void testCopyComponentInstance() {
1716         Either<Map<String, ComponentInstance>, ResponseFormat> result;
1717         ComponentInstance inputComponentInstance = createComponetInstanceFromComponent(resource);
1718         String containerComponentId = service.getUniqueId();
1719         String componentInstanceId = resource.getUniqueId();
1720         String oldServiceLastUpdatedUserId = service.getLastUpdaterUserId();
1721         service.setLastUpdaterUserId(USER_ID);
1722         LifecycleStateEnum oldResourceLifeCycle = resource.getLifecycleState();
1723         resource.setLifecycleState(LifecycleStateEnum.CERTIFIED);
1724
1725         Either<Component, StorageOperationStatus> leftServiceOp = Either.left(service);
1726         when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(leftServiceOp);
1727         when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1728             .thenReturn(leftServiceOp);
1729         when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1730             .thenReturn(StorageOperationStatus.OK);
1731         when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1732             .thenReturn(StorageOperationStatus.OK);
1733
1734         result = componentInstanceBusinessLogic
1735             .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId,
1736                 USER_ID);
1737         assertNotNull(result);
1738
1739         service.setLastUpdaterUserId(oldServiceLastUpdatedUserId);
1740         resource.setLifecycleState(oldResourceLifeCycle);
1741
1742         assertThat(result.isLeft()).isFalse();
1743     }
1744
1745     @Test
1746     void testCreateOrUpdateAttributeValueForCopyPaste() {
1747         ComponentInstance serviceComponentInstance = createComponetInstanceFromComponent(service);
1748         ComponentInstanceAttribute attribute = new ComponentInstanceAttribute();
1749         attribute.setType("string");
1750         attribute.setUniqueId("testCreateOrUpdateAttributeValueForCopyPaste");
1751         LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1752         String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1753         service.setLastUpdaterUserId(USER_ID);
1754         service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1755
1756         Map<String, List<ComponentInstanceAttribute>> instAttrsMap = new HashMap<>();
1757         List<ComponentInstanceAttribute> instAttrsList = new ArrayList<>();
1758         ComponentInstanceAttribute prop = new ComponentInstanceAttribute();
1759         prop.setUniqueId(attribute.getUniqueId());
1760         instAttrsList.add(prop);
1761         instAttrsMap.put(toInstance.getUniqueId(), instAttrsList);
1762         service.setComponentInstancesAttributes(instAttrsMap);
1763
1764         Either<Component, StorageOperationStatus> serviceEitherLeft = Either.left(service);
1765         when(toscaOperationFacade.getToscaElement(serviceComponentInstance.getUniqueId(), JsonParseFlagEnum.ParseAll))
1766             .thenReturn(serviceEitherLeft);
1767         when(toscaOperationFacade.updateComponentInstanceAttribute(service, toInstance.getUniqueId(), attribute))
1768             .thenReturn(StorageOperationStatus.OK);
1769         when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(service))
1770             .thenReturn(serviceEitherLeft);
1771
1772         Either<ComponentInstanceAttribute, ResponseFormat> result = Deencapsulation
1773             .invoke(componentInstanceBusinessLogic,
1774                 "createOrUpdateAttributeValueForCopyPaste",
1775                 ComponentTypeEnum.SERVICE,
1776                 serviceComponentInstance
1777                     .getUniqueId(),
1778                 toInstance.getUniqueId(), attribute,
1779                 USER_ID);
1780         assertNotNull(result);
1781
1782         service.setLastUpdaterUserId(oldLastUpdatedUserId);
1783         service.setLifecycleState(oldLifeCycleState);
1784
1785         assertThat(result.isLeft()).isTrue();
1786         ComponentInstanceAttribute resultProp = result.left().value();
1787         assertEquals(1, resultProp.getPath().size());
1788         assertEquals(resultProp.getPath().get(0), toInstance.getUniqueId());
1789     }
1790
1791     @Test
1792     void testUpdateComponentInstanceProperty() {
1793
1794         String containerComponentId = service.getUniqueId();
1795         String componentInstanceId = "dummy_id";
1796         ComponentInstanceProperty property = Mockito.mock(ComponentInstanceProperty.class);
1797
1798         Either<Component, StorageOperationStatus> getComponent = Either.left(service);
1799         when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(getComponent);
1800         StorageOperationStatus status = StorageOperationStatus.OK;
1801         when(toscaOperationFacade.updateComponentInstanceProperty(service, componentInstanceId, property))
1802             .thenReturn(status);
1803         Either<Component, StorageOperationStatus> updateContainerRes = Either.left(service);
1804         when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(service))
1805             .thenReturn(updateContainerRes);
1806
1807         Either<String, ResponseFormat> result = Deencapsulation.invoke(componentInstanceBusinessLogic,
1808             "updateComponentInstanceProperty", containerComponentId, componentInstanceId, property);
1809         assertNotNull(result);
1810         assertThat(result.isLeft()).isTrue();
1811     }
1812
1813     @Test
1814     void testGetInputListDefaultValue() {
1815         Component component = service;
1816         String inputId = "dummy_id";
1817         String defaultValue = "dummy_default_value";
1818         List<InputDefinition> newInputs = new ArrayList<>();
1819         InputDefinition in = new InputDefinition();
1820         in.setUniqueId(inputId);
1821         in.setDefaultValue(defaultValue);
1822         newInputs.add(in);
1823         List<InputDefinition> oldInputs = service.getInputs();
1824         service.setInputs(newInputs);
1825
1826         Either<String, ResponseFormat> result =
1827             Deencapsulation.invoke(componentInstanceBusinessLogic, "getInputListDefaultValue", component, inputId);
1828
1829         service.setInputs(oldInputs);
1830
1831         assertEquals(result.left().value(), defaultValue);
1832     }
1833
1834     @Test
1835     void testBatchDeleteComponentInstanceFailureWrongType() {
1836         Map<String, List<String>> result;
1837         List<String> componentInstanceIdList = new ArrayList<>();
1838         String containerComponentParam = "WRONG_TYPE";
1839         String containerComponentId = "containerComponentId";
1840         String componentInstanceId = "componentInstanceId";
1841         componentInstanceIdList.add(componentInstanceId);
1842         Map<String, List<String>> deleteErrorMap = new HashMap<>();
1843         List<String> deleteErrorIds = new ArrayList<>();
1844         deleteErrorIds.add(componentInstanceId);
1845         deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
1846         Either<Component, StorageOperationStatus> cont = Either.left(service);
1847         when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND, null))
1848             .thenReturn(ActionStatus.GENERAL_ERROR);
1849         when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class)))
1850             .thenReturn(cont);
1851
1852         try {
1853             result = componentInstanceBusinessLogic
1854                 .batchDeleteComponentInstance(containerComponentParam, containerComponentId, componentInstanceIdList,
1855                     USER_ID);
1856             assertNotNull(result);
1857             assertEquals(deleteErrorMap, result);
1858         } catch (ComponentException e) {
1859             assertEquals(e.getActionStatus().toString(), StorageOperationStatus.GENERAL_ERROR.toString());
1860         }
1861     }
1862
1863     @Test
1864     void testBatchDeleteComponentInstanceFailureCompIds() {
1865         String containerComponentParam = ComponentTypeEnum.SERVICE_PARAM_NAME;
1866         String containerComponentId = "containerComponentId";
1867         String componentInstanceId = "componentInstanceId";
1868         List<String> componentInstanceIdList = new ArrayList<>();
1869         componentInstanceIdList.add(componentInstanceId);
1870         Map<String, List<String>> deleteErrorMap = new HashMap<>();
1871         List<String> deleteErrorIds = new ArrayList<>();
1872         deleteErrorIds.add(componentInstanceId);
1873         deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
1874
1875         Either<Component, StorageOperationStatus> err = Either.right(StorageOperationStatus.GENERAL_ERROR);
1876         when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1877             .thenReturn(err);
1878
1879         try {
1880             Map<String, List<String>> result = componentInstanceBusinessLogic.batchDeleteComponentInstance(
1881                 containerComponentParam, containerComponentId, componentInstanceIdList, USER_ID);
1882             assertNotNull(result);
1883             assertEquals(deleteErrorMap, result);
1884         } catch (ComponentException e) {
1885             assertEquals(e.getActionStatus().toString(), StorageOperationStatus.GENERAL_ERROR.toString());
1886         }
1887     }
1888
1889     @Test
1890     void testBatchDeleteComponentInstanceSuccess() {
1891         Map<String, List<String>> result;
1892         String containerComponentParam = ComponentTypeEnum.SERVICE_PARAM_NAME;
1893         LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1894         String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1895         service.setLastUpdaterUserId(USER_ID);
1896         service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1897         String containerComponentId = service.getUniqueId();
1898         String componentInstanceId = TO_INSTANCE_ID;
1899         List<String> componentInstanceIdList = new ArrayList<>();
1900         componentInstanceIdList.add(componentInstanceId);
1901         Map<String, List<String>> deleteErrorMap = new HashMap<>();
1902         List<String> deleteErrorIds = new ArrayList<>();
1903         deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
1904
1905         Either<Component, StorageOperationStatus> cont = Either.left(service);
1906         when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1907             .thenReturn(StorageOperationStatus.OK);
1908         when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1909             .thenReturn(StorageOperationStatus.OK);
1910         ImmutablePair<Component, String> pair = new ImmutablePair<>(resource, TO_INSTANCE_ID);
1911         Either<ImmutablePair<Component, String>, StorageOperationStatus> result2 = Either.left(pair);
1912         when(toscaOperationFacade.deleteComponentInstanceFromTopologyTemplate(service, componentInstanceId))
1913             .thenReturn(result2);
1914         when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
1915             .thenReturn(cont);
1916         when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
1917
1918         result = componentInstanceBusinessLogic
1919             .batchDeleteComponentInstance(containerComponentParam, containerComponentId,
1920                 componentInstanceIdList, USER_ID);
1921         assertNotNull(result);
1922
1923         service.setLastUpdaterUserId(oldLastUpdatedUserId);
1924         service.setLifecycleState(oldLifeCycleState);
1925         assertEquals(deleteErrorMap, result);
1926     }
1927
1928     @Test
1929     void testDissociateRIFromRIFailDissociate() {
1930
1931         List<RequirementCapabilityRelDef> result;
1932         RequirementCapabilityRelDef ref = new RequirementCapabilityRelDef();
1933         ref.setFromNode(FROM_INSTANCE_ID);
1934         ref.setToNode(TO_INSTANCE_ID);
1935         List<CapabilityRequirementRelationship> relationships = new ArrayList<>();
1936         CapabilityRequirementRelationship relationship = new CapabilityRequirementRelationship();
1937         RelationshipInfo ri = new RelationshipInfo();
1938         ri.setRequirement(REQUIREMENT_NAME);
1939         relationship.setRelation(ri);
1940         relationships.add(relationship);
1941         ref.setRelationships(relationships);
1942         List<RequirementCapabilityRelDef> requirementDefList = new ArrayList<>();
1943         requirementDefList.add(ref);
1944         ComponentTypeEnum componentTypeEnum = service.getComponentType();
1945         String componentId = service.getUniqueId();
1946         LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1947         String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1948         service.setLastUpdaterUserId(USER_ID);
1949         service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1950
1951         Either<Component, StorageOperationStatus> cont = Either.left(service);
1952         when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
1953             .thenReturn(cont);
1954         when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1955             .thenReturn(StorageOperationStatus.OK);
1956         when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1957             .thenReturn(StorageOperationStatus.OK);
1958         Either<RequirementCapabilityRelDef, StorageOperationStatus> resultEither;
1959         resultEither = Either.right(StorageOperationStatus.OK);
1960         when(componentsUtils.convertFromStorageResponseForResourceInstance(StorageOperationStatus.OK, true))
1961             .thenReturn(ActionStatus.GENERAL_ERROR);
1962         when(toscaOperationFacade.dissociateResourceInstances(componentId, ref)).thenReturn(resultEither);
1963
1964         try {
1965             result = componentInstanceBusinessLogic
1966                 .batchDissociateRIFromRI(componentId, USER_ID, requirementDefList, componentTypeEnum);
1967             assertNotNull(result);
1968             assertEquals(new ArrayList<>(), result);
1969         } catch (ComponentException e) {
1970             assertEquals(e.getActionStatus().toString(), StorageOperationStatus.GENERAL_ERROR.toString());
1971         }
1972
1973         service.setLastUpdaterUserId(oldLastUpdatedUserId);
1974         service.setLifecycleState(oldLifeCycleState);
1975
1976     }
1977
1978     @Test
1979     void testDissociateRIFromRISuccess() {
1980
1981         List<RequirementCapabilityRelDef> result;
1982         RequirementCapabilityRelDef ref = new RequirementCapabilityRelDef();
1983         List<RequirementCapabilityRelDef> requirementDefList = new ArrayList<>();
1984         requirementDefList.add(ref);
1985         ComponentTypeEnum componentTypeEnum = service.getComponentType();
1986         String componentId = service.getUniqueId();
1987         LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1988         String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1989         service.setLastUpdaterUserId(USER_ID);
1990         service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1991
1992         Either<Component, StorageOperationStatus> cont = Either.left(service);
1993         when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
1994             .thenReturn(cont);
1995         when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1996             .thenReturn(StorageOperationStatus.OK);
1997         when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1998             .thenReturn(StorageOperationStatus.OK);
1999         Either<RequirementCapabilityRelDef, StorageOperationStatus> resultEither;
2000         resultEither = Either.left(ref);
2001         when(toscaOperationFacade.dissociateResourceInstances(componentId, ref)).thenReturn(resultEither);
2002
2003         result = componentInstanceBusinessLogic
2004             .batchDissociateRIFromRI(componentId, USER_ID, requirementDefList, componentTypeEnum);
2005         assertNotNull(result);
2006
2007         service.setLastUpdaterUserId(oldLastUpdatedUserId);
2008         service.setLifecycleState(oldLifeCycleState);
2009
2010         assertEquals(requirementDefList, result);
2011     }
2012
2013     @Test
2014     void testGetComponentInstancePropertyByPolicyId_success() {
2015         Optional<ComponentInstanceProperty> propertyCandidate =
2016             getComponentInstanceProperty(PROP_NAME);
2017
2018         assertThat(propertyCandidate).isPresent();
2019         assertEquals(PROP_NAME, propertyCandidate.get().getName());
2020     }
2021
2022     @Test
2023     void testGetComponentInstancePropertyByPolicyId_failure() {
2024         Optional<ComponentInstanceProperty> propertyCandidate =
2025             getComponentInstanceProperty(NON_EXIST_NAME);
2026
2027         assertEquals(propertyCandidate, Optional.empty());
2028     }
2029
2030     private Optional<ComponentInstanceProperty> getComponentInstanceProperty(String propertyName) {
2031         ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
2032         componentInstanceProperty.setName(propertyName);
2033
2034         PolicyDefinition policyDefinition = getPolicyDefinition();
2035         componentInstanceProperty.setGetPolicyValues(policyDefinition.getGetPolicyValues());
2036
2037         service.setComponentInstancesProperties(
2038             Collections.singletonMap(COMPONENT_INSTANCE_ID, Collections.singletonList(componentInstanceProperty)));
2039
2040         return componentInstanceBusinessLogic.getComponentInstancePropertyByPolicyId(service, policyDefinition);
2041     }
2042
2043     private PolicyDefinition getPolicyDefinition() {
2044         PolicyDefinition policyDefinition = new PolicyDefinition();
2045         policyDefinition.setInstanceUniqueId(COMPONENT_INSTANCE_ID);
2046         policyDefinition.setName(PROP_NAME);
2047
2048         GetPolicyValueDataDefinition getPolicy = new GetPolicyValueDataDefinition();
2049         getPolicy.setPropertyName(PROP_NAME);
2050
2051         List<GetPolicyValueDataDefinition> getPolicies = new ArrayList<>();
2052         getPolicies.add(getPolicy);
2053         policyDefinition.setGetPolicyValues(getPolicies);
2054
2055         return policyDefinition;
2056     }
2057
2058     private ComponentInstance createComponetInstanceFromComponent(Component component) {
2059         ComponentInstance componentInst = new ComponentInstance();
2060         componentInst.setUniqueId(component.getUniqueId());
2061         componentInst.setComponentUid(component.getUniqueId() + "_test");
2062         componentInst.setPosX("10");
2063         componentInst.setPosY("10");
2064         componentInst.setCapabilities(component.getCapabilities());
2065         componentInst.setRequirements(component.getRequirements());
2066         componentInst.setArtifacts(component.getArtifacts());
2067         componentInst.setDeploymentArtifacts(component.getDeploymentArtifacts());
2068         return componentInst;
2069     }
2070
2071     // Prepare ComponentInstance & Resource objects used in createComponentInstance() tests
2072
2073     private Pair<ComponentInstance, Resource> prepareResourcesForCreateComponentInstanceTest() {
2074         ComponentInstance instanceToBeCreated = new ComponentInstance();
2075         instanceToBeCreated.setName(COMPONENT_INSTANCE_NAME);
2076         instanceToBeCreated.setUniqueId(COMPONENT_INSTANCE_ID);
2077         instanceToBeCreated.setComponentUid(ORIGIN_COMPONENT_ID);
2078         instanceToBeCreated.setOriginType(OriginTypeEnum.VF);
2079
2080         Resource originComponent = new Resource();
2081         originComponent.setLifecycleState(LifecycleStateEnum.CERTIFIED);
2082         originComponent.setResourceType(ResourceTypeEnum.VF);
2083         originComponent.setVersion(ORIGIN_COMPONENT_VERSION);
2084         originComponent.setIcon(ICON_NAME);
2085
2086         return Pair.of(instanceToBeCreated, originComponent);
2087     }
2088     // Common part for testing component instance name validation
2089
2090     private void testCreateComponentInstanceNameValidationFailure(String ciName) {
2091         ComponentInstance ci = new ComponentInstance();
2092         ci.setName(ciName);
2093
2094         // Stub for getting component
2095         when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
2096             .thenReturn(Either.left(service));
2097
2098         // Expecting ByActionStatusComponentException
2099         ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
2100             componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
2101         });
2102         assertEquals(ActionStatus.INVALID_COMPONENT_NAME, e.getActionStatus());
2103     }
2104     @TestFactory
2105     Iterable<DynamicTest> testCreateComponentInstanceNameValidationFailureFactory() {
2106         String longName = String.join("", Collections.nCopies(ValidationUtils.COMPONENT_NAME_MAX_LENGTH + 1, "x"));
2107         String invalidName = "componentInstance#name";
2108         return Arrays.asList(
2109             dynamicTest("instance name is empty", () ->
2110                 testCreateComponentInstanceNameValidationFailure("")),
2111             dynamicTest("instance name is too long", () ->
2112                 testCreateComponentInstanceNameValidationFailure(longName)),
2113             dynamicTest("instance name includes invalid character", () ->
2114                 testCreateComponentInstanceNameValidationFailure(invalidName))
2115         );
2116     }
2117
2118     @Test
2119     void testCreateComponentInstanceFailToGetComponent() {
2120         ComponentInstance ci = prepareResourcesForCreateComponentInstanceTest().getLeft();
2121
2122         // Stub for getting component
2123         when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
2124             .thenReturn(Either.left(service));
2125         when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
2126             .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
2127         when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND, ComponentTypeEnum.RESOURCE))
2128             .thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
2129
2130         ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
2131             componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
2132         });
2133         assertThat(e.getActionStatus()).isEqualTo(ActionStatus.RESOURCE_NOT_FOUND);
2134     }
2135
2136     @Test
2137     void testCreateComponentInstanceFailureInvalidState() {
2138         Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
2139         ComponentInstance ci = p.getLeft();
2140         Resource originComponent = p.getRight();
2141         originComponent.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
2142
2143         // Stub for getting component
2144         when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
2145             .thenReturn(Either.left(service));
2146         when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
2147             .thenReturn(Either.left(originComponent));
2148
2149         ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
2150             componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
2151         });
2152         assertThat(e.getActionStatus()).isEqualTo(ActionStatus.CONTAINER_CANNOT_CONTAIN_COMPONENT_IN_STATE);
2153     }
2154
2155     @Test
2156     void testCreateComponentInstanceFailureArchived() {
2157         Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
2158         ComponentInstance ci = p.getLeft();
2159         Resource originComponent = p.getRight();
2160         originComponent.setArchived(Boolean.TRUE);
2161
2162         // Stub for getting component
2163         when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
2164             .thenReturn(Either.left(service));
2165         when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
2166             .thenReturn(Either.left(originComponent));
2167
2168         ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
2169             componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
2170         });
2171         assertThat(e.getActionStatus()).isEqualTo(ActionStatus.COMPONENT_IS_ARCHIVED);
2172     }
2173
2174     @Test
2175     void testCreateComponentInstanceFailureInvalidOriginType() {
2176         Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
2177         ComponentInstance ci = p.getLeft();
2178         Resource originComponent = p.getRight();
2179         ci.setOriginType(OriginTypeEnum.VFC); // Set different type from origin
2180
2181         // Stub for getting component
2182         when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
2183             .thenReturn(Either.left(service));
2184         when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
2185             .thenReturn(Either.left(originComponent));
2186
2187         final ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
2188             componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
2189         });
2190         assertThat(e.getActionStatus()).isEqualTo(ActionStatus.INVALID_CONTENT);
2191     }
2192
2193     @Test
2194     void testCreateComponentInstanceFailureCannotContainInstance() {
2195         final Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
2196         final ComponentInstance ci = p.getLeft();
2197         final Resource originComponent = p.getRight();
2198
2199         // Stub for getting component
2200         when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
2201             .thenReturn(Either.left(service));
2202         when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
2203             .thenReturn(Either.left(originComponent));
2204         // Assume services cannot contain VF resource
2205         when(containerInstanceTypeData.isAllowedForServiceComponent(ResourceTypeEnum.VF, null))
2206             .thenReturn(false);
2207
2208         ByActionStatusComponentException actualException = assertThrows(ByActionStatusComponentException.class, () -> {
2209             componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
2210         });
2211         assertThat(actualException.getActionStatus()).isEqualTo(ActionStatus.CONTAINER_CANNOT_CONTAIN_INSTANCE);
2212         verify(containerInstanceTypeData, times(1)).isAllowedForServiceComponent(ResourceTypeEnum.VF, null);
2213
2214         //given
2215         final Resource resource = createResource();
2216         resource.setResourceType(ResourceTypeEnum.VF);
2217         resource.setLastUpdaterUserId(USER_ID);
2218         //when
2219         when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
2220             .thenReturn(Either.left(resource));
2221         when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
2222             .thenReturn(Either.left(originComponent));
2223         when(containerInstanceTypeData.isAllowedForResourceComponent(ResourceTypeEnum.VF, ResourceTypeEnum.VF))
2224             .thenReturn(false);
2225         actualException = assertThrows(ByActionStatusComponentException.class, () -> {
2226             componentInstanceBusinessLogic.createComponentInstance(RESOURCE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
2227         });
2228         //then
2229         assertThat(actualException.getActionStatus()).isEqualTo(ActionStatus.CONTAINER_CANNOT_CONTAIN_INSTANCE);
2230     }
2231
2232     @Test
2233     void testCreateComponentInstanceFailureAddToGraph() {
2234         final Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
2235         final ComponentInstance ci = p.getLeft();
2236         final Resource originComponent = p.getRight();
2237
2238         // TODO Refactor createComponentInstance() method and reduce these mocks
2239         //      not to target the internal details too much
2240         when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
2241             .thenReturn(Either.left(service));
2242         when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
2243             .thenReturn(Either.left(originComponent));
2244         when(containerInstanceTypeData.isAllowedForServiceComponent(ResourceTypeEnum.VF, null))
2245             .thenReturn(true);
2246         Mockito.doNothing().when(compositionBusinessLogic).validateAndSetDefaultCoordinates(ci);
2247         when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service))
2248             .thenReturn(StorageOperationStatus.OK);
2249         when(toscaOperationFacade.addComponentInstanceToTopologyTemplate(service, originComponent, ci, false, user))
2250             .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
2251         when(componentsUtils.convertFromStorageResponseForResourceInstance(StorageOperationStatus.BAD_REQUEST, true))
2252             .thenReturn(ActionStatus.INVALID_CONTENT);
2253         when(componentsUtils.getResponseFormatForResourceInstance(ActionStatus.INVALID_CONTENT, "", null))
2254             .thenReturn(new ResponseFormat());
2255         when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
2256         when(graphLockOperation.unlockComponent(COMPONENT_ID, NodeTypeEnum.Service))
2257             .thenReturn(StorageOperationStatus.OK);
2258
2259         assertThrows(ByResponseFormatComponentException.class, () -> {
2260             componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
2261         });
2262         verify(containerInstanceTypeData, times(1))
2263             .isAllowedForServiceComponent(ResourceTypeEnum.VF, null);
2264         verify(compositionBusinessLogic, times(1)).validateAndSetDefaultCoordinates(ci);
2265         verify(toscaOperationFacade, times(1))
2266             .addComponentInstanceToTopologyTemplate(service, originComponent, ci, false, user);
2267         verify(graphLockOperation, times(1)).unlockComponent(COMPONENT_ID, NodeTypeEnum.Service);
2268     }
2269
2270     @Test
2271     void testCreateComponentInstanceSuccess() {
2272         final Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
2273         final ComponentInstance instanceToBeCreated = p.getLeft();
2274         final Resource originComponent = p.getRight();
2275
2276         final Service updatedService = new Service();
2277         updatedService.setComponentInstances(Collections.singletonList(instanceToBeCreated));
2278         updatedService.setUniqueId(service.getUniqueId());
2279
2280         // TODO Refactor createComponentInstance() method and reduce these mocks
2281         //      not to target the internal details too much
2282         when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
2283             .thenReturn(Either.left(service));
2284         when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
2285             .thenReturn(Either.left(originComponent));
2286         when(containerInstanceTypeData.isAllowedForServiceComponent(ResourceTypeEnum.VF, null))
2287             .thenReturn(true);
2288         Mockito.doNothing().when(compositionBusinessLogic).validateAndSetDefaultCoordinates(instanceToBeCreated);
2289         when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service))
2290             .thenReturn(StorageOperationStatus.OK);
2291         when(toscaOperationFacade.addComponentInstanceToTopologyTemplate(service, originComponent, instanceToBeCreated, false, user))
2292             .thenReturn(Either.left(new ImmutablePair<>(updatedService, COMPONENT_INSTANCE_ID)));
2293         when(artifactsBusinessLogic.getArtifacts(
2294             ORIGIN_COMPONENT_ID, NodeTypeEnum.Resource, ArtifactGroupTypeEnum.DEPLOYMENT, null))
2295             .thenReturn(Either.left(new HashMap<>()));
2296         when(toscaOperationFacade
2297             .addInformationalArtifactsToInstance(service.getUniqueId(), instanceToBeCreated, originComponent.getArtifacts()))
2298             .thenReturn(StorageOperationStatus.OK);
2299         when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
2300         when(graphLockOperation.unlockComponent(COMPONENT_ID, NodeTypeEnum.Service))
2301             .thenReturn(StorageOperationStatus.OK);
2302
2303         final ComponentInstance result = componentInstanceBusinessLogic.createComponentInstance(
2304             ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, instanceToBeCreated);
2305         assertThat(result).isEqualTo(instanceToBeCreated);
2306         assertThat(instanceToBeCreated.getComponentVersion()).isEqualTo(originComponent.getVersion());
2307         assertThat(instanceToBeCreated.getIcon()).isEqualTo(originComponent.getIcon());
2308         verify(containerInstanceTypeData, times(1))
2309             .isAllowedForServiceComponent(ResourceTypeEnum.VF, null);
2310         verify(compositionBusinessLogic, times(1)).validateAndSetDefaultCoordinates(instanceToBeCreated);
2311         verify(toscaOperationFacade, times(1))
2312             .addComponentInstanceToTopologyTemplate(service, originComponent, instanceToBeCreated, false, user);
2313         // Check graph db change was committed
2314         verify(janusGraphDao, times(1)).commit();
2315     }
2316     
2317     @Test
2318     void testCreateComponentInstanceServiceSubstitutionSuccess() {
2319         ComponentInstance instanceToBeCreated = createServiceSubstitutionComponentInstance();
2320         Service originService = createServiceSubstitutionOriginService();
2321         Component serviceBaseComponent = createServiceSubstitutionServiceDerivedFromComponent();
2322
2323         Service updatedService = new Service();
2324         updatedService.setComponentInstances(Collections.singletonList(instanceToBeCreated));
2325         updatedService.setUniqueId(service.getUniqueId());
2326         
2327         when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
2328             .thenReturn(Either.left(service));
2329         when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
2330             .thenReturn(Either.left(originService));
2331         when(toscaOperationFacade.getLatestByToscaResourceName(eq(originService.getDerivedFromGenericType()), isNull()))
2332             .thenReturn(Either.left(serviceBaseComponent));
2333         when(toscaOperationFacade.getToscaElement(eq(ORIGIN_COMPONENT_ID), any(ComponentParametersView.class)))
2334             .thenReturn(Either.left(originService));
2335         Mockito.doNothing().when(compositionBusinessLogic).validateAndSetDefaultCoordinates(instanceToBeCreated);
2336         when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service))
2337             .thenReturn(StorageOperationStatus.OK);
2338         when(toscaOperationFacade.addComponentInstanceToTopologyTemplate(service, serviceBaseComponent, instanceToBeCreated, false, user))
2339             .thenReturn(Either.left(new ImmutablePair<>(updatedService, COMPONENT_INSTANCE_ID)));
2340         when(artifactsBusinessLogic.getArtifacts(
2341                 "baseComponentId", NodeTypeEnum.Resource, ArtifactGroupTypeEnum.DEPLOYMENT, null))
2342             .thenReturn(Either.left(new HashMap<>()));
2343         when(toscaOperationFacade
2344             .addInformationalArtifactsToInstance(service.getUniqueId(), instanceToBeCreated, originService.getArtifacts()))
2345             .thenReturn(StorageOperationStatus.OK);
2346         when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
2347         when(graphLockOperation.unlockComponent(COMPONENT_ID, NodeTypeEnum.Service))
2348             .thenReturn(StorageOperationStatus.OK);
2349
2350         ComponentInstance result = componentInstanceBusinessLogic.createComponentInstance(
2351             ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, instanceToBeCreated);
2352         assertThat(result).isEqualTo(instanceToBeCreated);
2353         assertThat(instanceToBeCreated.getComponentVersion()).isEqualTo(originService.getVersion());
2354         assertThat(instanceToBeCreated.getIcon()).isEqualTo(originService.getIcon());
2355         verify(compositionBusinessLogic, times(1)).validateAndSetDefaultCoordinates(instanceToBeCreated);
2356         verify(toscaOperationFacade, times(1))
2357             .addComponentInstanceToTopologyTemplate(service, serviceBaseComponent, instanceToBeCreated, false, user);
2358         // Check graph db change was committed
2359         verify(janusGraphDao, times(1)).commit();
2360     }
2361
2362     @Test
2363     void testGetComponentInstanceAttributesById_success() {
2364         final ComponentInstanceAttribute componentInstanceAttribute = new ComponentInstanceAttribute();
2365         componentInstanceAttribute.setComponentInstanceId(TO_INSTANCE_ID);
2366
2367         final HashMap<String, List<ComponentInstanceAttribute>> map = new HashMap<>();
2368         map.put(TO_INSTANCE_ID, Arrays.asList(componentInstanceAttribute));
2369         resource.setComponentInstancesAttributes(map);
2370
2371         final Either<Component, StorageOperationStatus> leftServiceOp = Either.left(resource);
2372         doReturn(leftServiceOp).when(toscaOperationFacade).getToscaElement(COMPONENT_ID);
2373
2374         final List<ComponentInstanceAttribute> result = componentInstanceBusinessLogic
2375             .getComponentInstanceAttributesById(RESOURCE_PARAM_NAME, COMPONENT_ID, TO_INSTANCE_ID, USER_ID);
2376         assertThat(result).isNotNull().isNotEmpty();
2377         verify(toscaOperationFacade, times(1)).getToscaElement(COMPONENT_ID);
2378     }
2379
2380     @Test
2381     void testGetComponentInstanceAttributesById_fail_missing_ComponentInstancesAttributes() {
2382         final Either<Component, StorageOperationStatus> leftServiceOp = Either.left(resource);
2383         doReturn(leftServiceOp).when(toscaOperationFacade).getToscaElement(COMPONENT_ID);
2384
2385         final List<ComponentInstanceAttribute> result = componentInstanceBusinessLogic
2386             .getComponentInstanceAttributesById(RESOURCE_PARAM_NAME, COMPONENT_ID, TO_INSTANCE_ID, USER_ID);
2387         assertThat(result).isNotNull().isEmpty();
2388         verify(toscaOperationFacade, times(1)).getToscaElement(COMPONENT_ID);
2389     }
2390
2391     @Test
2392     void testGetComponentInstanceAttributesById_fail_getToscaElement() {
2393         final ComponentInstanceAttribute componentInstanceAttribute = new ComponentInstanceAttribute();
2394         componentInstanceAttribute.setComponentInstanceId(TO_INSTANCE_ID);
2395
2396         final HashMap<String, List<ComponentInstanceAttribute>> map = new HashMap<>();
2397         map.put(TO_INSTANCE_ID, Arrays.asList(componentInstanceAttribute));
2398         resource.setComponentInstancesAttributes(map);
2399
2400         final Either<Object, StorageOperationStatus> right = Either.right(StorageOperationStatus.BAD_REQUEST);
2401         doReturn(right).when(toscaOperationFacade).getToscaElement(COMPONENT_ID);
2402         doReturn(ActionStatus.BAD_REQUEST_MISSING_RESOURCE).when(componentsUtils).convertFromStorageResponse(StorageOperationStatus.BAD_REQUEST);
2403
2404         assertThrows(ByActionStatusComponentException.class, () -> {
2405             final List<ComponentInstanceAttribute> result = componentInstanceBusinessLogic
2406                 .getComponentInstanceAttributesById(RESOURCE_PARAM_NAME, COMPONENT_ID, TO_INSTANCE_ID, USER_ID);
2407
2408         });
2409
2410     }
2411
2412     @Test
2413     void testGetComponentInstanceAttributesById_fail_getResourceInstanceById() {
2414         final ComponentInstanceAttribute componentInstanceAttribute = new ComponentInstanceAttribute();
2415         componentInstanceAttribute.setComponentInstanceId(TO_INSTANCE_ID);
2416
2417         final HashMap<String, List<ComponentInstanceAttribute>> map = new HashMap<>();
2418         map.put(TO_INSTANCE_ID, Arrays.asList(componentInstanceAttribute));
2419         resource.setComponentInstancesAttributes(map);
2420
2421         final Either<Component, StorageOperationStatus> leftServiceOp = Either.left(resource);
2422         doReturn(leftServiceOp).when(toscaOperationFacade).getToscaElement(COMPONENT_ID);
2423         doReturn(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE).when(componentsUtils).convertFromStorageResponse(StorageOperationStatus.PARENT_RESOURCE_NOT_FOUND);
2424
2425         assertThrows(ByActionStatusComponentException.class, () -> {
2426             final List<ComponentInstanceAttribute> result = componentInstanceBusinessLogic
2427                 .getComponentInstanceAttributesById(RESOURCE_PARAM_NAME, COMPONENT_ID, "", USER_ID);
2428
2429         });
2430
2431     }
2432
2433     @Test
2434     void updateInstanceCapabilitySuccessTest() {
2435         var containerComponentId = "containerComponentId";
2436         var componentInstanceUniqueId = "componentInstanceUniqueId";
2437         var capabilityDefinition = new CapabilityDefinition();
2438         capabilityDefinition.setUniqueId("uniqueId");
2439
2440         final Component component = new Service();
2441         component.setUniqueId(containerComponentId);
2442         component.setLastUpdaterUserId(USER_ID);
2443         component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
2444
2445         var componentInstance = new ComponentInstance();
2446         componentInstance.setUniqueId(componentInstanceUniqueId);
2447         component.setComponentInstances(Collections.singletonList(componentInstance));
2448
2449         when(toscaOperationFacade.getToscaFullElement(containerComponentId))
2450             .thenReturn(Either.left(component));
2451         when(toscaOperationFacade.updateComponentInstanceCapability(containerComponentId, componentInstanceUniqueId, capabilityDefinition))
2452             .thenReturn(capabilityDefinition);
2453         when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(component))
2454             .thenReturn(Either.left(component));
2455         when(graphLockOperation.lockComponent(containerComponentId, NodeTypeEnum.Service))
2456             .thenReturn(StorageOperationStatus.OK);
2457
2458         final Either<CapabilityDefinition, ResponseFormat> resultEither = componentInstanceBusinessLogic
2459             .updateInstanceCapability(ComponentTypeEnum.SERVICE, containerComponentId, componentInstanceUniqueId, capabilityDefinition, USER_ID);
2460         assertTrue(resultEither.isLeft());
2461         final CapabilityDefinition actualCapabilityDefinition = resultEither.left().value();
2462         assertNotEquals(capabilityDefinition, actualCapabilityDefinition);
2463         assertEquals(capabilityDefinition.getUniqueId(), actualCapabilityDefinition.getUniqueId());
2464     }
2465
2466     @Test
2467     void updateInstanceCapabilityNoContainerComponentTypeTest() {
2468         var responseFormat = new ResponseFormat();
2469         when(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED)).thenReturn(responseFormat);
2470         final Either<CapabilityDefinition, ResponseFormat> resultEither = componentInstanceBusinessLogic
2471             .updateInstanceCapability(null, "containerComponentId", "componentInstanceUniqueId", new CapabilityDefinition(), USER_ID);
2472         assertTrue(resultEither.isRight(), "Either return should be right");
2473         final ResponseFormat actualResponseFormat = resultEither.right().value();
2474         assertEquals(responseFormat, actualResponseFormat);
2475     }
2476
2477     @Test
2478     void updateInstanceCapabilityContainerComponentNotFoundTest() {
2479         var containerComponentId = "containerComponentId";
2480         when(toscaOperationFacade.getToscaFullElement(containerComponentId)).thenReturn(Either.right(null));
2481         var responseFormat = new ResponseFormat();
2482         when(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_NOT_FOUND, containerComponentId)).thenReturn(responseFormat);
2483         final Either<CapabilityDefinition, ResponseFormat> resultEither = componentInstanceBusinessLogic
2484             .updateInstanceCapability(ComponentTypeEnum.SERVICE, "containerComponentId", "componentInstanceUniqueId", new CapabilityDefinition(), USER_ID);
2485         assertTrue(resultEither.isRight(), "Either return should be right");
2486         final ResponseFormat actualResponseFormat = resultEither.right().value();
2487         assertEquals(responseFormat, actualResponseFormat);
2488     }
2489
2490     @Test
2491     void updateInstanceCapabilityCannotWorkOnComponentTest() {
2492         var containerComponentId = "containerComponentId";
2493         var componentInstanceUniqueId = "componentInstanceUniqueId";
2494
2495         final Component component = new Service();
2496         component.setUniqueId(containerComponentId);
2497         component.setLastUpdaterUserId("anotherUse");
2498         component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
2499
2500         var expectedResponseFormat = new ResponseFormat();
2501
2502         when(toscaOperationFacade.getToscaFullElement(containerComponentId))
2503             .thenReturn(Either.left(component));
2504         when(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION))
2505             .thenReturn(expectedResponseFormat);
2506
2507         final Either<CapabilityDefinition, ResponseFormat> resultEither = componentInstanceBusinessLogic
2508             .updateInstanceCapability(ComponentTypeEnum.SERVICE, containerComponentId, componentInstanceUniqueId, new CapabilityDefinition(), USER_ID);
2509         assertTrue(resultEither.isRight(), "Either return should be right");
2510         final ResponseFormat actualResponseFormat = resultEither.right().value();
2511         assertEquals(expectedResponseFormat, actualResponseFormat);
2512     }
2513
2514     @Test
2515     void updateInstanceCapabilityResourceInstanceNotFoundTest() {
2516         var containerComponentId = "containerComponentId";
2517         var componentInstanceUniqueId = "componentInstanceUniqueId";
2518
2519         final Component component = new Service();
2520         component.setUniqueId(containerComponentId);
2521         component.setLastUpdaterUserId(USER_ID);
2522         component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
2523
2524         var expectedResponseFormat = new ResponseFormat();
2525
2526         when(toscaOperationFacade.getToscaFullElement(containerComponentId))
2527             .thenReturn(Either.left(component));
2528         when(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceUniqueId, containerComponentId))
2529             .thenReturn(expectedResponseFormat);
2530
2531         final Either<CapabilityDefinition, ResponseFormat> resultEither = componentInstanceBusinessLogic
2532             .updateInstanceCapability(ComponentTypeEnum.SERVICE, containerComponentId, componentInstanceUniqueId, new CapabilityDefinition(), USER_ID);
2533         assertTrue(resultEither.isRight(), "Either return should be right");
2534         final ResponseFormat actualResponseFormat = resultEither.right().value();
2535         assertEquals(expectedResponseFormat, actualResponseFormat);
2536     }
2537
2538     @Test
2539     void updateInstanceCapabilityUpdateMetadataFailTest() {
2540         var containerComponentId = "containerComponentId";
2541         var componentInstanceUniqueId = "componentInstanceUniqueId";
2542         var capabilityDefinition = new CapabilityDefinition();
2543         capabilityDefinition.setUniqueId("uniqueId");
2544
2545         final Component component = new Service();
2546         component.setUniqueId(containerComponentId);
2547         component.setLastUpdaterUserId(USER_ID);
2548         component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
2549
2550         var componentInstance = new ComponentInstance();
2551         componentInstance.setUniqueId(componentInstanceUniqueId);
2552         component.setComponentInstances(Collections.singletonList(componentInstance));
2553
2554         var expectedResponseFormat = new ResponseFormat();
2555
2556         when(toscaOperationFacade.getToscaFullElement(containerComponentId))
2557             .thenReturn(Either.left(component));
2558         when(graphLockOperation.lockComponent(containerComponentId, NodeTypeEnum.Service))
2559             .thenReturn(StorageOperationStatus.OK);
2560         when(toscaOperationFacade.updateComponentInstanceCapability(containerComponentId, componentInstanceUniqueId, capabilityDefinition))
2561             .thenReturn(capabilityDefinition);
2562         when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(component))
2563             .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
2564         when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR, ComponentTypeEnum.SERVICE))
2565             .thenReturn(ActionStatus.GENERAL_ERROR);
2566         when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR))
2567             .thenReturn(expectedResponseFormat);
2568
2569         final Either<CapabilityDefinition, ResponseFormat> resultEither = componentInstanceBusinessLogic
2570             .updateInstanceCapability(ComponentTypeEnum.SERVICE, containerComponentId, componentInstanceUniqueId, capabilityDefinition, USER_ID);
2571         assertTrue(resultEither.isRight(), "Either return should be right");
2572         final ResponseFormat actualResponseFormat = resultEither.right().value();
2573         assertEquals(expectedResponseFormat, actualResponseFormat);
2574     }
2575
2576     @Test
2577     void updateInstanceCapabilityBusinessExceptionHandlingTest() {
2578         var containerComponentId = "containerComponentId";
2579         var componentInstanceUniqueId = "componentInstanceUniqueId";
2580         var capabilityDefinition = new CapabilityDefinition();
2581         capabilityDefinition.setUniqueId("uniqueId");
2582
2583         final Component component = new Service();
2584         component.setUniqueId(containerComponentId);
2585         component.setLastUpdaterUserId(USER_ID);
2586         component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
2587
2588         var componentInstance = new ComponentInstance();
2589         componentInstance.setUniqueId(componentInstanceUniqueId);
2590         component.setComponentInstances(Collections.singletonList(componentInstance));
2591
2592
2593         when(toscaOperationFacade.getToscaFullElement(containerComponentId))
2594             .thenReturn(Either.left(component));
2595         when(graphLockOperation.lockComponent(containerComponentId, NodeTypeEnum.Service))
2596             .thenReturn(StorageOperationStatus.OK);
2597         when(toscaOperationFacade.updateComponentInstanceCapability(containerComponentId, componentInstanceUniqueId, capabilityDefinition))
2598             .thenThrow(new OperationException(ActionStatus.GENERAL_ERROR));
2599
2600         final BusinessException businessException = assertThrows(BusinessException.class, () -> {
2601             componentInstanceBusinessLogic
2602                 .updateInstanceCapability(ComponentTypeEnum.SERVICE, containerComponentId, componentInstanceUniqueId, capabilityDefinition, USER_ID);
2603         });
2604         assertTrue(businessException instanceof OperationException);
2605         assertEquals(ActionStatus.GENERAL_ERROR, ((OperationException) businessException).getActionStatus());
2606     }
2607
2608     @Test
2609     void updateInstanceCapabilityUnknownExceptionHandlingTest() {
2610         var containerComponentId = "containerComponentId";
2611         var componentInstanceUniqueId = "componentInstanceUniqueId";
2612         var capabilityDefinition = new CapabilityDefinition();
2613         capabilityDefinition.setUniqueId("uniqueId");
2614
2615         final Component component = new Service();
2616         component.setUniqueId(containerComponentId);
2617         component.setLastUpdaterUserId(USER_ID);
2618         component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
2619
2620         var componentInstance = new ComponentInstance();
2621         componentInstance.setUniqueId(componentInstanceUniqueId);
2622         component.setComponentInstances(Collections.singletonList(componentInstance));
2623
2624         var expectedResponseFormat = new ResponseFormat();
2625
2626         when(toscaOperationFacade.getToscaFullElement(containerComponentId))
2627             .thenReturn(Either.left(component));
2628         when(graphLockOperation.lockComponent(containerComponentId, NodeTypeEnum.Service))
2629             .thenReturn(StorageOperationStatus.OK);
2630         when(toscaOperationFacade.updateComponentInstanceCapability(containerComponentId, componentInstanceUniqueId, capabilityDefinition))
2631             .thenThrow(new RuntimeException());
2632         when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR))
2633             .thenReturn(expectedResponseFormat);
2634
2635         final Exception exception = assertThrows(BusinessException.class, () ->
2636             componentInstanceBusinessLogic
2637                 .updateInstanceCapability(ComponentTypeEnum.SERVICE, containerComponentId, componentInstanceUniqueId, capabilityDefinition, USER_ID));
2638         assertTrue(exception instanceof ByResponseFormatComponentException);
2639         final ByResponseFormatComponentException actualException = (ByResponseFormatComponentException) exception;
2640         assertEquals(expectedResponseFormat, actualException.getResponseFormat());
2641     }
2642
2643     private ComponentInstance createServiceSubstitutionComponentInstance() {
2644         final ComponentInstance instanceToBeCreated = new ComponentInstance();
2645         instanceToBeCreated.setName(COMPONENT_INSTANCE_NAME);
2646         instanceToBeCreated.setUniqueId(COMPONENT_INSTANCE_ID);
2647         instanceToBeCreated.setComponentUid(ORIGIN_COMPONENT_ID);
2648         instanceToBeCreated.setOriginType(OriginTypeEnum.ServiceSubstitution);
2649
2650         return instanceToBeCreated;
2651     }
2652     
2653     private Service createServiceSubstitutionOriginService() {
2654         final Service originComponent = new Service();
2655         originComponent.setLifecycleState(LifecycleStateEnum.CERTIFIED);
2656         originComponent.setVersion(ORIGIN_COMPONENT_VERSION);
2657         originComponent.setIcon(ICON_NAME);
2658         originComponent.setDerivedFromGenericType("org.openecomp.resource.abstract.nodes.service");
2659         originComponent.setName("myService");
2660         return originComponent;
2661     }
2662     
2663     private Component createServiceSubstitutionServiceDerivedFromComponent() {
2664         final Resource component = new Resource();
2665         component.setLifecycleState(LifecycleStateEnum.CERTIFIED);
2666         component.setVersion(ORIGIN_COMPONENT_VERSION);
2667         component.setIcon(ICON_NAME);
2668         component.setToscaResourceName("org.openecomp.resource.abstract.nodes.service");
2669         component.setUniqueId("baseComponentId");
2670         return component;
2671     }
2672
2673     private void mockComponentForToscaGetFunctionValidation(final Component component) {
2674         when(toscaOperationFacade.getToscaElement(component.getUniqueId(), JsonParseFlagEnum.ParseAll))
2675             .thenReturn(Either.left(component));
2676         when(graphLockOperation.lockComponent(component.getUniqueId(), NodeTypeEnum.ResourceInstance))
2677             .thenReturn(StorageOperationStatus.OK);
2678         when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(component))
2679             .thenReturn(Either.left(component));
2680         when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
2681         when(graphLockOperation.unlockComponent(component.getUniqueId(), NodeTypeEnum.ResourceInstance))
2682             .thenReturn(StorageOperationStatus.OK);
2683     }
2684
2685     private ToscaGetFunctionDataDefinition createGetToscaFunction(final String propertyName, final String propertyUniqueId,
2686                                                                   final List<String> propertyPathFromSource,
2687                                                                   final PropertySource propertySource, final ToscaGetFunctionType functionType,
2688                                                                   final String sourceUniqueId,
2689                                                                   final String sourceName) {
2690         final var toscaGetFunction = new ToscaGetFunctionDataDefinition();
2691         toscaGetFunction.setFunctionType(functionType);
2692         toscaGetFunction.setPropertyUniqueId(propertyUniqueId);
2693         toscaGetFunction.setPropertyName(propertyName);
2694         toscaGetFunction.setPropertyPathFromSource(propertyPathFromSource);
2695         toscaGetFunction.setPropertySource(propertySource);
2696         toscaGetFunction.setSourceName(sourceName);
2697         toscaGetFunction.setSourceUniqueId(sourceUniqueId);
2698         return toscaGetFunction;
2699     }
2700
2701     private SchemaDefinition createSchema(final String schemaType) {
2702         final var schemaDefinition = new SchemaDefinition();
2703         final var schemaProperty = new PropertyDefinition();
2704         schemaProperty.setType(schemaType);
2705         schemaDefinition.setProperty(schemaProperty);
2706         return schemaDefinition;
2707     }
2708
2709     private static Stream<Arguments> getToscaFunctionForValidation() {
2710         final var toscaGetFunction1 = new ToscaGetFunctionDataDefinition();
2711         final ResponseFormat expectedResponse1 = ToscaGetFunctionExceptionSupplier
2712             .targetFunctionTypeNotFound().get().getResponseFormat();
2713
2714         final var toscaGetFunction2 = new ToscaGetFunctionDataDefinition();
2715         toscaGetFunction2.setFunctionType(ToscaGetFunctionType.GET_INPUT);
2716         final ResponseFormat expectedResponse2 = ToscaGetFunctionExceptionSupplier
2717             .targetPropertySourceNotFound(toscaGetFunction2.getFunctionType()).get().getResponseFormat();
2718
2719         final var toscaGetFunction3 = new ToscaGetFunctionDataDefinition();
2720         toscaGetFunction3.setFunctionType(ToscaGetFunctionType.GET_INPUT);
2721         toscaGetFunction3.setPropertySource(PropertySource.SELF);
2722         final ResponseFormat expectedResponse3 = ToscaGetFunctionExceptionSupplier
2723             .targetSourcePathNotFound(toscaGetFunction3.getFunctionType()).get().getResponseFormat();
2724
2725         final var toscaGetFunction4 = new ToscaGetFunctionDataDefinition();
2726         toscaGetFunction4.setFunctionType(ToscaGetFunctionType.GET_INPUT);
2727         toscaGetFunction4.setPropertySource(PropertySource.SELF);
2728         toscaGetFunction4.setPropertyPathFromSource(List.of("sourcePath"));
2729         final ResponseFormat expectedResponse4 = ToscaGetFunctionExceptionSupplier
2730             .sourceNameNotFound(toscaGetFunction4.getPropertySource()).get().getResponseFormat();
2731
2732         final var toscaGetFunction5 = new ToscaGetFunctionDataDefinition();
2733         toscaGetFunction5.setFunctionType(ToscaGetFunctionType.GET_INPUT);
2734         toscaGetFunction5.setPropertySource(PropertySource.SELF);
2735         toscaGetFunction5.setPropertyPathFromSource(List.of("sourcePath"));
2736         toscaGetFunction5.setSourceName("sourceName");
2737         final ResponseFormat expectedResponse5 = ToscaGetFunctionExceptionSupplier
2738             .sourceIdNotFound(toscaGetFunction5.getPropertySource()).get().getResponseFormat();
2739
2740         final var toscaGetFunction6 = new ToscaGetFunctionDataDefinition();
2741         toscaGetFunction6.setFunctionType(ToscaGetFunctionType.GET_PROPERTY);
2742         toscaGetFunction6.setPropertySource(PropertySource.SELF);
2743         toscaGetFunction6.setPropertyPathFromSource(List.of("sourcePath"));
2744         toscaGetFunction6.setSourceName("sourceName");
2745         toscaGetFunction6.setSourceUniqueId("sourceUniqueId");
2746         final ResponseFormat expectedResponse6 = ToscaGetFunctionExceptionSupplier
2747             .propertyNameNotFound(toscaGetFunction6.getPropertySource()).get().getResponseFormat();
2748
2749         final var toscaGetFunction7 = new ToscaGetFunctionDataDefinition();
2750         toscaGetFunction7.setFunctionType(ToscaGetFunctionType.GET_PROPERTY);
2751         toscaGetFunction7.setPropertySource(PropertySource.SELF);
2752         toscaGetFunction7.setPropertyPathFromSource(List.of("sourcePath"));
2753         toscaGetFunction7.setSourceName("sourceName");
2754         toscaGetFunction7.setSourceUniqueId("sourceUniqueId");
2755         toscaGetFunction7.setPropertyName("propertyName");
2756         final ResponseFormat expectedResponse7 = ToscaGetFunctionExceptionSupplier
2757             .propertyIdNotFound(toscaGetFunction7.getPropertySource()).get().getResponseFormat();
2758
2759         return Stream.of(
2760             Arguments.of(toscaGetFunction1, expectedResponse1),
2761             Arguments.of(toscaGetFunction2, expectedResponse2),
2762             Arguments.of(toscaGetFunction3, expectedResponse3),
2763             Arguments.of(toscaGetFunction4, expectedResponse4),
2764             Arguments.of(toscaGetFunction5, expectedResponse5),
2765             Arguments.of(toscaGetFunction6, expectedResponse6),
2766             Arguments.of(toscaGetFunction7, expectedResponse7)
2767         );
2768     }
2769
2770 }
2771