2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.components.impl;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.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;
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;
52 import java.util.Map.Entry;
53 import java.util.Optional;
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;
140 * The test suite designed for test functionality of ComponentInstanceBusinessLogic class
142 class ComponentInstanceBusinessLogicTest {
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";
171 private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
173 private ComponentInstancePropInput componentInstancePropInput;
175 private ArtifactsBusinessLogic artifactsBusinessLogic;
177 private ComponentsUtils componentsUtils;
179 private ToscaOperationFacade toscaOperationFacade;
181 private ForwardingPathOperation forwardingPathOperation;
185 private UserValidations userValidations;
187 private GraphLockOperation graphLockOperation;
189 private JanusGraphDao janusGraphDao;
191 private ApplicationDataTypeCache applicationDataTypeCache;
193 private PropertyOperation propertyOperation;
195 private ContainerInstanceTypesData containerInstanceTypeData;
197 private CompositionBusinessLogic compositionBusinessLogic;
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;
208 static void beforeAll() {
212 private static void initConfig() {
213 final ConfigurationSource configurationSource = new FSConfigurationSource(
214 ExternalConfiguration.getChangeListener(),
215 "src/test/resources/config/catalog-be"
217 new ConfigurationManager(configurationSource);
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);
240 void testGetRelationByIdSuccess() {
241 getServiceRelationByIdSuccess(service);
242 getServiceRelationByIdSuccess(resource);
246 void testGetRelationByIdUserValidationFailure() {
247 getServiceRelationByIdUserValidationFailure(service);
248 getServiceRelationByIdUserValidationFailure(resource);
252 void testGetRelationByIdComponentNotFoundFailure() {
253 getRelationByIdComponentNotFoundFailure(service);
254 getRelationByIdComponentNotFoundFailure(resource);
258 void testForwardingPathOnVersionChange() {
259 getforwardingPathOnVersionChange();
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();
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);
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);
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);
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));
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());
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);
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);
332 Map<String, DataTypeDefinition> types = new HashMap<>();
333 DataTypeDefinition dataTypeDef = new DataTypeDefinition();
334 types.put("string", dataTypeDef);
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)));
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);
365 Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = componentInstanceBusinessLogic
366 .createOrUpdatePropertiesValues(
367 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
368 assertThat(responseFormatEither.left().value()).isEqualTo(properties);
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)
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,
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)
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);
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));
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));
440 mockComponentForToscaGetFunctionValidation(component);
442 //creating data types for "map", and sub properties
443 final Map<String, DataTypeDefinition> allDataTypesMap = new HashMap<>();
444 allDataTypesMap.put(mapToscaType, new DataTypeDefinition());
446 final String subProperty1Type = "subProperty1.datatype";
447 allDataTypesMap.put(property1Type, createDataType(property1Type, Map.of(containerPropertyPath.get(1), subProperty1Type)));
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)));
455 when(applicationDataTypeCache.getAll(component.getModel())).thenReturn(Either.left(allDataTypesMap));
457 final Either<List<ComponentInstanceProperty>, ResponseFormat> actualResponseFormat = componentInstanceBusinessLogic
458 .createOrUpdatePropertiesValues(
459 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentId, resourceInstanceId, resourceInstanceProperties, userId);
461 assertTrue(actualResponseFormat.isLeft());
462 assertThat(actualResponseFormat.left().value()).isEqualTo(resourceInstanceProperties);
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");
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",
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)
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);
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));
508 mockComponentForToscaGetFunctionValidation(component);
511 final Either<List<ComponentInstanceProperty>, ResponseFormat> actualResponseFormat = componentInstanceBusinessLogic
512 .createOrUpdatePropertiesValues(
513 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentId, instanceUniqueId, resourceInstanceProperties, userId);
515 assertTrue(actualResponseFormat.isLeft());
516 assertThat(actualResponseFormat.left().value()).isEqualTo(resourceInstanceProperties);
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));
530 return dataTypeDefinition;
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);
539 return dataTypeDefinition;
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);
554 if (toscaGetFunction != null) {
555 componentInstanceProperty.setToscaGetFunction(toscaGetFunction);
558 return componentInstanceProperty;
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(
578 String.format("get_input: [\"%s\"]", inputName),
579 createGetToscaFunction(inputName, inputId, List.of(propertyName), PropertySource.SELF, ToscaGetFunctionType.GET_INPUT,
580 containerComponentId, containerComponentName)
582 properties.add(propertyGetInput);
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);
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));
597 final Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
598 componentInstanceProps.put(resourceInstanceId, properties);
599 component.setComponentInstancesProperties(componentInstanceProps);
601 final ComponentInstance resourceInstance = createComponentInstance("componentInstance1");
602 resourceInstance.setUniqueId(resourceInstanceId);
603 component.setComponentInstances(List.of(resourceInstance));
605 mockComponentForToscaGetFunctionValidation(component);
607 final Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither =
608 componentInstanceBusinessLogic
609 .createOrUpdatePropertiesValues(ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentId, resourceInstanceId, properties, userId);
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());
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(
639 String.format("get_input: [\"%s\"]", inputName),
640 createGetToscaFunction(inputName, inputId, List.of(propertyName), PropertySource.SELF, ToscaGetFunctionType.GET_INPUT,
641 containerComponentId, containerComponentName)
643 properties.add(propertyGetInput);
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);
651 var inputDefinition = new InputDefinition();
652 inputDefinition.setName(inputName);
653 inputDefinition.setUniqueId(inputId);
654 inputDefinition.setType("integer");
655 component.setInputs(List.of(inputDefinition));
657 final Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
658 componentInstanceProps.put(resourceInstanceId, properties);
659 component.setComponentInstancesProperties(componentInstanceProps);
661 final ComponentInstance resourceInstance = createComponentInstance("componentInstance1");
662 resourceInstance.setUniqueId(resourceInstanceId);
663 component.setComponentInstances(List.of(resourceInstance));
665 mockComponentForToscaGetFunctionValidation(component);
667 final Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither =
668 componentInstanceBusinessLogic
669 .createOrUpdatePropertiesValues(ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentId, resourceInstanceId, properties, userId);
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());
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);
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);
707 final Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
708 componentInstanceProps.put(resourceInstanceId, properties);
709 component.setComponentInstancesProperties(componentInstanceProps);
711 final ComponentInstance resourceInstance = createComponentInstance("componentInstance1");
712 resourceInstance.setUniqueId(resourceInstanceId);
713 component.setComponentInstances(List.of(resourceInstance));
715 mockComponentForToscaGetFunctionValidation(component);
717 final Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither =
718 componentInstanceBusinessLogic
719 .createOrUpdatePropertiesValues(ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentId, resourceInstanceId, properties, userId);
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());
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);
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);
749 final Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
750 componentInstanceProps.put(resourceInstanceId, properties);
751 component.setComponentInstancesProperties(componentInstanceProps);
753 final ComponentInstance resourceInstance = createComponentInstance("componentInstance1");
754 resourceInstance.setUniqueId(resourceInstanceId);
755 component.setComponentInstances(List.of(resourceInstance));
757 mockComponentForToscaGetFunctionValidation(component);
759 final Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither =
760 componentInstanceBusinessLogic
761 .createOrUpdatePropertiesValues(ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentId, resourceInstanceId, properties, userId);
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());
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(
787 String.format("get_input: [\"%s\"]", inputName),
788 createGetToscaFunction(inputName, inputId, List.of(propertyName), PropertySource.SELF, ToscaGetFunctionType.GET_INPUT,
789 containerComponentId, containerComponentName)
791 properties.add(propertyGetInput);
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);
799 final Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
800 componentInstanceProps.put(resourceInstanceId, properties);
801 component.setComponentInstancesProperties(componentInstanceProps);
803 final ComponentInstance resourceInstance = createComponentInstance("componentInstance1");
804 resourceInstance.setUniqueId(resourceInstanceId);
805 component.setComponentInstances(List.of(resourceInstance));
807 mockComponentForToscaGetFunctionValidation(component);
809 final Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither =
810 componentInstanceBusinessLogic
811 .createOrUpdatePropertiesValues(ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentId, resourceInstanceId, properties, userId);
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());
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);
835 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
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)));
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);
856 componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
857 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
858 } catch (ComponentException e) {
859 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.PROPERTY_NOT_FOUND);
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);
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);
883 Map<String, DataTypeDefinition> types = new HashMap<>();
884 DataTypeDefinition dataTypeDef = new DataTypeDefinition();
885 types.put("string", dataTypeDef);
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)));
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);
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");
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);
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);
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)));
945 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
946 .thenReturn(Either.left(component));
947 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
948 .thenReturn(StorageOperationStatus.OK);
951 componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
952 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
953 } catch (ComponentException e) {
954 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.MISSING_PROPERTY_NAME);
959 void testDeleteForwardingPathsWhenComponentinstanceDeleted() {
961 ComponentTypeEnum containerComponentType = ComponentTypeEnum.findByParamName("services");
962 String containerComponentID = "Service-comp";
963 String componentInstanceID = "NodeA1";
964 Service component = new Service();
966 .setComponentInstances(Arrays.asList(createComponentInstance("NodeA2"), createComponentInstance("NodeB2"),
967 createComponentInstance(componentInstanceID)));
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());
985 void testAddComponentInstanceDeploymentArtifacts() {
987 Component containerComponent = new Service();
988 ComponentInstance componentInstance = new ComponentInstance();
989 componentInstance.setUniqueId(COMPONENT_INSTANCE_ID);
990 Component originComponent = fillOriginComponent(new Resource());
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());
1005 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getResourceDeploymentArtifacts = Either
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);
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<>(),
1030 .thenReturn(StorageOperationStatus.OK);
1031 when(toscaOperationFacade
1032 .addInformationalArtifactsToInstance(containerComponent.getUniqueId(), componentInstance, null))
1033 .thenReturn(StorageOperationStatus.OK);
1035 ActionStatus status = componentInstanceBusinessLogic.addComponentInstanceArtifacts(containerComponent,
1036 componentInstance, originComponent, user, null);
1038 assertThat(status).isEqualTo(ActionStatus.OK);
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;
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;
1061 private ComponentInstance createComponentInstance(String path1) {
1062 ComponentInstance componentInstance = new ComponentInstance();
1063 componentInstance.setName(path1);
1064 return componentInstance;
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);
1079 return forwardingPath;
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;
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();
1110 private void getServiceRelationByIdUserValidationFailure(Component component) {
1111 doThrow(new ByActionStatusComponentException(ActionStatus.USER_NOT_FOUND)).when(userValidations).validateUserExists(USER_ID);
1113 componentInstanceBusinessLogic
1114 .getRelationById(COMPONENT_ID, RELATION_ID, USER_ID, component.getComponentType());
1115 } catch (ByActionStatusComponentException e) {
1116 assertSame(ActionStatus.USER_NOT_FOUND, e.getActionStatus());
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);
1125 Either<RequirementCapabilityRelDef, ResponseFormat> response = componentInstanceBusinessLogic
1126 .getRelationById(COMPONENT_ID,
1127 RELATION_ID, USER_ID,
1128 component.getComponentType());
1129 assertThat(response.isRight()).isTrue();
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);
1139 private void createComponents() {
1145 resource = createResource();
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);
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);
1171 Map<String, List<ComponentInstanceProperty>> ciPropertyMap = new HashMap<>();
1172 ciPropertyMap.put(TO_INSTANCE_ID, ciPropertyList);
1173 service.setComponentInstancesProperties(ciPropertyMap);
1175 Map<String, List<ComponentInstanceInput>> ciInputMap = new HashMap<>();
1176 ciInputMap.put(TO_INSTANCE_ID, ciInputList);
1177 service.setComponentInstancesInputs(ciInputMap);
1180 private void createInstances() {
1181 toInstance = new ComponentInstance();
1182 toInstance.setUniqueId(TO_INSTANCE_ID);
1183 toInstance.setName(TO_INSTANCE_NAME);
1185 fromInstance = new ComponentInstance();
1186 fromInstance.setUniqueId(FROM_INSTANCE_ID);
1188 CapabilityDataDefinition capability = new CapabilityDataDefinition();
1189 capability.setOwnerId(CAPABILITY_OWNER_ID);
1190 capability.setUniqueId(CAPABILITY_UID);
1191 capability.setName(CAPABILITY_NAME);
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));
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);
1206 Map<String, List<RequirementDefinition>> requirements = new HashMap<>();
1207 requirements.put(requirement.getCapability(), Lists.newArrayList(new RequirementDefinition(requirement)));
1209 toInstance.setCapabilities(capabilities);
1210 fromInstance.setRequirements(requirements);
1214 private void createRelation() {
1216 relation = new RequirementCapabilityRelDef();
1217 CapabilityRequirementRelationship relationship = new CapabilityRequirementRelationship();
1218 RelationshipInfo relationInfo = new RelationshipInfo();
1219 relationInfo.setId(RELATION_ID);
1220 relationship.setRelation(relationInfo);
1222 relation.setRelationships(Lists.newArrayList(relationship));
1223 relation.setToNode(TO_INSTANCE_ID);
1224 relation.setFromNode(FROM_INSTANCE_ID);
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);
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);
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);
1268 private ComponentInstanceBusinessLogic createTestSubject() {
1269 return componentInstanceBusinessLogic;
1273 void testChangeServiceProxyVersion() {
1274 ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
1276 Either<ComponentInstance, ResponseFormat> result;
1279 componentInstanceBusinessLogic = createTestSubject();
1280 result = componentInstanceBusinessLogic.changeServiceProxyVersion();
1281 assertNotNull(result);
1285 void testCreateServiceProxy() {
1286 ComponentInstanceBusinessLogic testSubject;
1287 Either<ComponentInstance, ResponseFormat> result;
1290 testSubject = createTestSubject();
1291 result = testSubject.createServiceProxy();
1292 assertNotNull(result);
1296 void testDeleteServiceProxy() {
1297 ComponentInstanceBusinessLogic testSubject;
1299 Either<ComponentInstance, ResponseFormat> result;
1302 testSubject = createTestSubject();
1303 result = testSubject.deleteServiceProxy();
1304 assertNotNull(result);
1308 void testGetComponentInstanceInputsByInputIdEmpty() {
1309 Component component = new Service();
1310 String inputId = "";
1311 List<ComponentInstanceInput> result;
1313 result = componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(component, inputId);
1314 assertNotNull(result);
1315 assertThat(result.isEmpty()).isTrue();
1319 void testGetComponentInstanceInputsByInputIdPresent() {
1320 List<ComponentInstanceInput> result;
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);
1332 void testGetComponentInstancePropertiesByInputIdEmpty() {
1333 Component component = new Service();
1334 String inputId = "";
1335 List<ComponentInstanceProperty> result;
1337 result = componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(component, inputId);
1338 assertNotNull(result);
1339 assertThat(result.isEmpty()).isTrue();
1343 void testGetComponentInstancePropertiesByInputIdPresent() {
1344 List<ComponentInstanceProperty> result;
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);
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;
1364 testSubject = createTestSubject();
1365 result = testSubject.getRelationById(componentId, relationId, userId, componentTypeEnum);
1366 assertNotNull(result);
1370 void testValidateParent() {
1371 ComponentInstanceBusinessLogic testSubject;
1372 resource = createResource();
1373 String nodeTemplateId = "";
1377 testSubject = createTestSubject();
1378 result = Deencapsulation.invoke(testSubject, "validateParent", new Object[]{resource, nodeTemplateId});
1379 assertFalse(result);
1383 void testGetComponentType() {
1384 ComponentInstanceBusinessLogic testSubject;
1385 ComponentTypeEnum result;
1388 testSubject = createTestSubject();
1389 result = Deencapsulation.invoke(testSubject, "getComponentType", new Object[]{ComponentTypeEnum.class});
1390 assertNotNull(result);
1394 void testGetNewGroupName() {
1395 ComponentInstanceBusinessLogic testSubject;
1396 String oldPrefix = "";
1397 String newNormailzedPrefix = "";
1398 String qualifiedGroupInstanceName = "";
1402 testSubject = createTestSubject();
1403 result = Deencapsulation.invoke(testSubject, "getNewGroupName",
1404 new Object[]{oldPrefix, newNormailzedPrefix, qualifiedGroupInstanceName});
1405 assertNotNull(result);
1409 void testUpdateComponentInstanceMetadata_3() {
1410 ComponentInstanceBusinessLogic testSubject;
1412 ComponentInstance result;
1415 testSubject = createTestSubject();
1416 result = Deencapsulation
1417 .invoke(testSubject, "updateComponentInstanceMetadata", new Object[]{toInstance, toInstance});
1418 assertNotNull(result);
1422 void testFindRelation() {
1423 ComponentInstanceBusinessLogic testSubject;
1424 String relationId = "";
1425 List<RequirementCapabilityRelDef> requirementCapabilityRelations = new ArrayList<>();
1426 RequirementCapabilityRelDef result;
1429 testSubject = createTestSubject();
1430 result = Deencapsulation.invoke(testSubject, "findRelation",
1431 new Object[]{relationId, requirementCapabilityRelations});
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;
1446 when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll))
1447 .thenReturn(Either.left(resource));
1450 testSubject = createTestSubject();
1451 result = testSubject
1452 .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
1454 assertNotNull(result);
1456 componentTypeEnum = null;
1457 result = testSubject
1458 .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
1460 assertNotNull(result);
1462 result = testSubject
1463 .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
1465 assertNotNull(result);
1469 void testUpdateCapabilityPropertyOnContainerComponent() {
1470 ComponentInstanceBusinessLogic testSubject;
1471 ComponentInstanceProperty property = new ComponentInstanceProperty();
1472 String newValue = "";
1473 resource = createResource();
1475 String capabilityType = "";
1476 String capabilityName = "";
1477 ResponseFormat result;
1480 testSubject = createTestSubject();
1481 result = Deencapsulation.invoke(testSubject, "updateCapabilityPropertyOnContainerComponent",
1482 new Object[]{property, newValue, resource, toInstance, capabilityType, capabilityName});
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;
1497 when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll))
1498 .thenReturn(Either.left(resource));
1501 testSubject = createTestSubject();
1502 result = testSubject
1503 .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
1505 assertNotNull(result);
1506 componentTypeEnum = null;
1507 result = testSubject
1508 .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
1510 assertNotNull(result);
1512 result = testSubject
1513 .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
1515 assertNotNull(result);
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;
1530 when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseMetadata))
1531 .thenReturn(Either.left(resource));
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);
1545 result = testSubject
1546 .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1547 groupInstanceId, property, userId);
1548 assertNotNull(result);
1552 void testDeletePropertyValue() {
1553 ComponentInstanceBusinessLogic testSubject;
1554 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1556 String serviceId = service.getUniqueId();
1557 String resourceInstanceId = "";
1558 String propertyValueId = "";
1559 String userId = user.getUserId();
1560 Either<ComponentInstanceProperty, ResponseFormat> result;
1562 when(toscaOperationFacade.getToscaElement(serviceId, JsonParseFlagEnum.ParseMetadata))
1563 .thenReturn(Either.left(service));
1566 testSubject = createTestSubject();
1567 result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1569 assertNotNull(result);
1570 componentTypeEnum = null;
1571 result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1573 assertNotNull(result);
1575 result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1577 assertNotNull(result);
1581 void testGetComponentParametersViewForForwardingPath() {
1582 ComponentInstanceBusinessLogic testSubject;
1583 ComponentParametersView result;
1586 testSubject = createTestSubject();
1587 result = Deencapsulation.invoke(testSubject, "getComponentParametersViewForForwardingPath");
1588 assertNotNull(result);
1592 void testGetResourceInstanceById() {
1593 ComponentInstanceBusinessLogic testSubject;
1594 resource = createResource();
1595 String instanceId = "";
1596 Either<ComponentInstance, StorageOperationStatus> result;
1599 testSubject = createTestSubject();
1600 result = Deencapsulation.invoke(testSubject, "getResourceInstanceById", new Object[]{resource, instanceId});
1601 assertNotNull(result);
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;
1617 when(toscaOperationFacade.getToscaFullElement(containerComponentId))
1618 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
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);
1631 void testUpdateInstanceRequirement() {
1632 ComponentInstanceBusinessLogic testSubject;
1633 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
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();
1643 Either<RequirementDefinition, ResponseFormat> result;
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);
1654 result = testSubject.updateInstanceRequirement(componentTypeEnum, containerComponentId,
1655 componentInstanceUniqueId, requirementDef, userId);
1656 assertEquals(requirementDef, result.left().value());
1661 void testCopyComponentInstanceWrongUserId() {
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");
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);
1680 result = componentInstanceBusinessLogic
1681 .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId,
1683 assertNotNull(result);
1685 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1686 assertThat(result.isRight()).isTrue();
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);
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();
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);
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);
1734 result = componentInstanceBusinessLogic
1735 .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId,
1737 assertNotNull(result);
1739 service.setLastUpdaterUserId(oldServiceLastUpdatedUserId);
1740 resource.setLifecycleState(oldResourceLifeCycle);
1742 assertThat(result.isLeft()).isFalse();
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);
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);
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);
1772 Either<ComponentInstanceAttribute, ResponseFormat> result = Deencapsulation
1773 .invoke(componentInstanceBusinessLogic,
1774 "createOrUpdateAttributeValueForCopyPaste",
1775 ComponentTypeEnum.SERVICE,
1776 serviceComponentInstance
1778 toInstance.getUniqueId(), attribute,
1780 assertNotNull(result);
1782 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1783 service.setLifecycleState(oldLifeCycleState);
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());
1792 void testUpdateComponentInstanceProperty() {
1794 String containerComponentId = service.getUniqueId();
1795 String componentInstanceId = "dummy_id";
1796 ComponentInstanceProperty property = Mockito.mock(ComponentInstanceProperty.class);
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);
1807 Either<String, ResponseFormat> result = Deencapsulation.invoke(componentInstanceBusinessLogic,
1808 "updateComponentInstanceProperty", containerComponentId, componentInstanceId, property);
1809 assertNotNull(result);
1810 assertThat(result.isLeft()).isTrue();
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);
1823 List<InputDefinition> oldInputs = service.getInputs();
1824 service.setInputs(newInputs);
1826 Either<String, ResponseFormat> result =
1827 Deencapsulation.invoke(componentInstanceBusinessLogic, "getInputListDefaultValue", component, inputId);
1829 service.setInputs(oldInputs);
1831 assertEquals(result.left().value(), defaultValue);
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)))
1853 result = componentInstanceBusinessLogic
1854 .batchDeleteComponentInstance(containerComponentParam, containerComponentId, componentInstanceIdList,
1856 assertNotNull(result);
1857 assertEquals(deleteErrorMap, result);
1858 } catch (ComponentException e) {
1859 assertEquals(e.getActionStatus().toString(), StorageOperationStatus.GENERAL_ERROR.toString());
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);
1875 Either<Component, StorageOperationStatus> err = Either.right(StorageOperationStatus.GENERAL_ERROR);
1876 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
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());
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);
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)))
1916 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
1918 result = componentInstanceBusinessLogic
1919 .batchDeleteComponentInstance(containerComponentParam, containerComponentId,
1920 componentInstanceIdList, USER_ID);
1921 assertNotNull(result);
1923 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1924 service.setLifecycleState(oldLifeCycleState);
1925 assertEquals(deleteErrorMap, result);
1929 void testDissociateRIFromRIFailDissociate() {
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);
1951 Either<Component, StorageOperationStatus> cont = Either.left(service);
1952 when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
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);
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());
1973 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1974 service.setLifecycleState(oldLifeCycleState);
1979 void testDissociateRIFromRISuccess() {
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);
1992 Either<Component, StorageOperationStatus> cont = Either.left(service);
1993 when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
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);
2003 result = componentInstanceBusinessLogic
2004 .batchDissociateRIFromRI(componentId, USER_ID, requirementDefList, componentTypeEnum);
2005 assertNotNull(result);
2007 service.setLastUpdaterUserId(oldLastUpdatedUserId);
2008 service.setLifecycleState(oldLifeCycleState);
2010 assertEquals(requirementDefList, result);
2014 void testGetComponentInstancePropertyByPolicyId_success() {
2015 Optional<ComponentInstanceProperty> propertyCandidate =
2016 getComponentInstanceProperty(PROP_NAME);
2018 assertThat(propertyCandidate).isPresent();
2019 assertEquals(PROP_NAME, propertyCandidate.get().getName());
2023 void testGetComponentInstancePropertyByPolicyId_failure() {
2024 Optional<ComponentInstanceProperty> propertyCandidate =
2025 getComponentInstanceProperty(NON_EXIST_NAME);
2027 assertEquals(propertyCandidate, Optional.empty());
2030 private Optional<ComponentInstanceProperty> getComponentInstanceProperty(String propertyName) {
2031 ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
2032 componentInstanceProperty.setName(propertyName);
2034 PolicyDefinition policyDefinition = getPolicyDefinition();
2035 componentInstanceProperty.setGetPolicyValues(policyDefinition.getGetPolicyValues());
2037 service.setComponentInstancesProperties(
2038 Collections.singletonMap(COMPONENT_INSTANCE_ID, Collections.singletonList(componentInstanceProperty)));
2040 return componentInstanceBusinessLogic.getComponentInstancePropertyByPolicyId(service, policyDefinition);
2043 private PolicyDefinition getPolicyDefinition() {
2044 PolicyDefinition policyDefinition = new PolicyDefinition();
2045 policyDefinition.setInstanceUniqueId(COMPONENT_INSTANCE_ID);
2046 policyDefinition.setName(PROP_NAME);
2048 GetPolicyValueDataDefinition getPolicy = new GetPolicyValueDataDefinition();
2049 getPolicy.setPropertyName(PROP_NAME);
2051 List<GetPolicyValueDataDefinition> getPolicies = new ArrayList<>();
2052 getPolicies.add(getPolicy);
2053 policyDefinition.setGetPolicyValues(getPolicies);
2055 return policyDefinition;
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;
2071 // Prepare ComponentInstance & Resource objects used in createComponentInstance() tests
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);
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);
2086 return Pair.of(instanceToBeCreated, originComponent);
2088 // Common part for testing component instance name validation
2090 private void testCreateComponentInstanceNameValidationFailure(String ciName) {
2091 ComponentInstance ci = new ComponentInstance();
2094 // Stub for getting component
2095 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
2096 .thenReturn(Either.left(service));
2098 // Expecting ByActionStatusComponentException
2099 ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
2100 componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
2102 assertEquals(ActionStatus.INVALID_COMPONENT_NAME, e.getActionStatus());
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))
2119 void testCreateComponentInstanceFailToGetComponent() {
2120 ComponentInstance ci = prepareResourcesForCreateComponentInstanceTest().getLeft();
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);
2130 ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
2131 componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
2133 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.RESOURCE_NOT_FOUND);
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);
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));
2149 ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
2150 componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
2152 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.CONTAINER_CANNOT_CONTAIN_COMPONENT_IN_STATE);
2156 void testCreateComponentInstanceFailureArchived() {
2157 Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
2158 ComponentInstance ci = p.getLeft();
2159 Resource originComponent = p.getRight();
2160 originComponent.setArchived(Boolean.TRUE);
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));
2168 ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
2169 componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
2171 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.COMPONENT_IS_ARCHIVED);
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
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));
2187 final ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
2188 componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
2190 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.INVALID_CONTENT);
2194 void testCreateComponentInstanceFailureCannotContainInstance() {
2195 final Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
2196 final ComponentInstance ci = p.getLeft();
2197 final Resource originComponent = p.getRight();
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))
2208 ByActionStatusComponentException actualException = assertThrows(ByActionStatusComponentException.class, () -> {
2209 componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
2211 assertThat(actualException.getActionStatus()).isEqualTo(ActionStatus.CONTAINER_CANNOT_CONTAIN_INSTANCE);
2212 verify(containerInstanceTypeData, times(1)).isAllowedForServiceComponent(ResourceTypeEnum.VF, null);
2215 final Resource resource = createResource();
2216 resource.setResourceType(ResourceTypeEnum.VF);
2217 resource.setLastUpdaterUserId(USER_ID);
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))
2225 actualException = assertThrows(ByActionStatusComponentException.class, () -> {
2226 componentInstanceBusinessLogic.createComponentInstance(RESOURCE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
2229 assertThat(actualException.getActionStatus()).isEqualTo(ActionStatus.CONTAINER_CANNOT_CONTAIN_INSTANCE);
2233 void testCreateComponentInstanceFailureAddToGraph() {
2234 final Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
2235 final ComponentInstance ci = p.getLeft();
2236 final Resource originComponent = p.getRight();
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))
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);
2259 assertThrows(ByResponseFormatComponentException.class, () -> {
2260 componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
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);
2271 void testCreateComponentInstanceSuccess() {
2272 final Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
2273 final ComponentInstance instanceToBeCreated = p.getLeft();
2274 final Resource originComponent = p.getRight();
2276 final Service updatedService = new Service();
2277 updatedService.setComponentInstances(Collections.singletonList(instanceToBeCreated));
2278 updatedService.setUniqueId(service.getUniqueId());
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))
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);
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();
2318 void testCreateComponentInstanceServiceSubstitutionSuccess() {
2319 ComponentInstance instanceToBeCreated = createServiceSubstitutionComponentInstance();
2320 Service originService = createServiceSubstitutionOriginService();
2321 Component serviceBaseComponent = createServiceSubstitutionServiceDerivedFromComponent();
2323 Service updatedService = new Service();
2324 updatedService.setComponentInstances(Collections.singletonList(instanceToBeCreated));
2325 updatedService.setUniqueId(service.getUniqueId());
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);
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();
2363 void testGetComponentInstanceAttributesById_success() {
2364 final ComponentInstanceAttribute componentInstanceAttribute = new ComponentInstanceAttribute();
2365 componentInstanceAttribute.setComponentInstanceId(TO_INSTANCE_ID);
2367 final HashMap<String, List<ComponentInstanceAttribute>> map = new HashMap<>();
2368 map.put(TO_INSTANCE_ID, Arrays.asList(componentInstanceAttribute));
2369 resource.setComponentInstancesAttributes(map);
2371 final Either<Component, StorageOperationStatus> leftServiceOp = Either.left(resource);
2372 doReturn(leftServiceOp).when(toscaOperationFacade).getToscaElement(COMPONENT_ID);
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);
2381 void testGetComponentInstanceAttributesById_fail_missing_ComponentInstancesAttributes() {
2382 final Either<Component, StorageOperationStatus> leftServiceOp = Either.left(resource);
2383 doReturn(leftServiceOp).when(toscaOperationFacade).getToscaElement(COMPONENT_ID);
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);
2392 void testGetComponentInstanceAttributesById_fail_getToscaElement() {
2393 final ComponentInstanceAttribute componentInstanceAttribute = new ComponentInstanceAttribute();
2394 componentInstanceAttribute.setComponentInstanceId(TO_INSTANCE_ID);
2396 final HashMap<String, List<ComponentInstanceAttribute>> map = new HashMap<>();
2397 map.put(TO_INSTANCE_ID, Arrays.asList(componentInstanceAttribute));
2398 resource.setComponentInstancesAttributes(map);
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);
2404 assertThrows(ByActionStatusComponentException.class, () -> {
2405 final List<ComponentInstanceAttribute> result = componentInstanceBusinessLogic
2406 .getComponentInstanceAttributesById(RESOURCE_PARAM_NAME, COMPONENT_ID, TO_INSTANCE_ID, USER_ID);
2413 void testGetComponentInstanceAttributesById_fail_getResourceInstanceById() {
2414 final ComponentInstanceAttribute componentInstanceAttribute = new ComponentInstanceAttribute();
2415 componentInstanceAttribute.setComponentInstanceId(TO_INSTANCE_ID);
2417 final HashMap<String, List<ComponentInstanceAttribute>> map = new HashMap<>();
2418 map.put(TO_INSTANCE_ID, Arrays.asList(componentInstanceAttribute));
2419 resource.setComponentInstancesAttributes(map);
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);
2425 assertThrows(ByActionStatusComponentException.class, () -> {
2426 final List<ComponentInstanceAttribute> result = componentInstanceBusinessLogic
2427 .getComponentInstanceAttributesById(RESOURCE_PARAM_NAME, COMPONENT_ID, "", USER_ID);
2434 void updateInstanceCapabilitySuccessTest() {
2435 var containerComponentId = "containerComponentId";
2436 var componentInstanceUniqueId = "componentInstanceUniqueId";
2437 var capabilityDefinition = new CapabilityDefinition();
2438 capabilityDefinition.setUniqueId("uniqueId");
2440 final Component component = new Service();
2441 component.setUniqueId(containerComponentId);
2442 component.setLastUpdaterUserId(USER_ID);
2443 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
2445 var componentInstance = new ComponentInstance();
2446 componentInstance.setUniqueId(componentInstanceUniqueId);
2447 component.setComponentInstances(Collections.singletonList(componentInstance));
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);
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());
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);
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);
2491 void updateInstanceCapabilityCannotWorkOnComponentTest() {
2492 var containerComponentId = "containerComponentId";
2493 var componentInstanceUniqueId = "componentInstanceUniqueId";
2495 final Component component = new Service();
2496 component.setUniqueId(containerComponentId);
2497 component.setLastUpdaterUserId("anotherUse");
2498 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
2500 var expectedResponseFormat = new ResponseFormat();
2502 when(toscaOperationFacade.getToscaFullElement(containerComponentId))
2503 .thenReturn(Either.left(component));
2504 when(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION))
2505 .thenReturn(expectedResponseFormat);
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);
2515 void updateInstanceCapabilityResourceInstanceNotFoundTest() {
2516 var containerComponentId = "containerComponentId";
2517 var componentInstanceUniqueId = "componentInstanceUniqueId";
2519 final Component component = new Service();
2520 component.setUniqueId(containerComponentId);
2521 component.setLastUpdaterUserId(USER_ID);
2522 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
2524 var expectedResponseFormat = new ResponseFormat();
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);
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);
2539 void updateInstanceCapabilityUpdateMetadataFailTest() {
2540 var containerComponentId = "containerComponentId";
2541 var componentInstanceUniqueId = "componentInstanceUniqueId";
2542 var capabilityDefinition = new CapabilityDefinition();
2543 capabilityDefinition.setUniqueId("uniqueId");
2545 final Component component = new Service();
2546 component.setUniqueId(containerComponentId);
2547 component.setLastUpdaterUserId(USER_ID);
2548 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
2550 var componentInstance = new ComponentInstance();
2551 componentInstance.setUniqueId(componentInstanceUniqueId);
2552 component.setComponentInstances(Collections.singletonList(componentInstance));
2554 var expectedResponseFormat = new ResponseFormat();
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);
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);
2577 void updateInstanceCapabilityBusinessExceptionHandlingTest() {
2578 var containerComponentId = "containerComponentId";
2579 var componentInstanceUniqueId = "componentInstanceUniqueId";
2580 var capabilityDefinition = new CapabilityDefinition();
2581 capabilityDefinition.setUniqueId("uniqueId");
2583 final Component component = new Service();
2584 component.setUniqueId(containerComponentId);
2585 component.setLastUpdaterUserId(USER_ID);
2586 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
2588 var componentInstance = new ComponentInstance();
2589 componentInstance.setUniqueId(componentInstanceUniqueId);
2590 component.setComponentInstances(Collections.singletonList(componentInstance));
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));
2600 final BusinessException businessException = assertThrows(BusinessException.class, () -> {
2601 componentInstanceBusinessLogic
2602 .updateInstanceCapability(ComponentTypeEnum.SERVICE, containerComponentId, componentInstanceUniqueId, capabilityDefinition, USER_ID);
2604 assertTrue(businessException instanceof OperationException);
2605 assertEquals(ActionStatus.GENERAL_ERROR, ((OperationException) businessException).getActionStatus());
2609 void updateInstanceCapabilityUnknownExceptionHandlingTest() {
2610 var containerComponentId = "containerComponentId";
2611 var componentInstanceUniqueId = "componentInstanceUniqueId";
2612 var capabilityDefinition = new CapabilityDefinition();
2613 capabilityDefinition.setUniqueId("uniqueId");
2615 final Component component = new Service();
2616 component.setUniqueId(containerComponentId);
2617 component.setLastUpdaterUserId(USER_ID);
2618 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
2620 var componentInstance = new ComponentInstance();
2621 componentInstance.setUniqueId(componentInstanceUniqueId);
2622 component.setComponentInstances(Collections.singletonList(componentInstance));
2624 var expectedResponseFormat = new ResponseFormat();
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);
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());
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);
2650 return instanceToBeCreated;
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;
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");
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);
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;
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;
2709 private static Stream<Arguments> getToscaFunctionForValidation() {
2710 final var toscaGetFunction1 = new ToscaGetFunctionDataDefinition();
2711 final ResponseFormat expectedResponse1 = ToscaGetFunctionExceptionSupplier
2712 .targetFunctionTypeNotFound().get().getResponseFormat();
2714 final var toscaGetFunction2 = new ToscaGetFunctionDataDefinition();
2715 toscaGetFunction2.setFunctionType(ToscaGetFunctionType.GET_INPUT);
2716 final ResponseFormat expectedResponse2 = ToscaGetFunctionExceptionSupplier
2717 .targetPropertySourceNotFound(toscaGetFunction2.getFunctionType()).get().getResponseFormat();
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();
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();
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();
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();
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();
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)