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.Optional;
54 import lombok.SneakyThrows;
55 import mockit.Deencapsulation;
56 import org.apache.commons.lang3.tuple.ImmutablePair;
57 import org.apache.commons.lang3.tuple.Pair;
58 import org.assertj.core.util.Lists;
59 import org.junit.jupiter.api.BeforeAll;
60 import org.junit.jupiter.api.BeforeEach;
61 import org.junit.jupiter.api.DynamicTest;
62 import org.junit.jupiter.api.Test;
63 import org.junit.jupiter.api.TestFactory;
64 import org.mockito.Mock;
65 import org.mockito.Mockito;
66 import org.mockito.MockitoAnnotations;
67 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
68 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
69 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
70 import org.openecomp.sdc.be.components.validation.UserValidations;
71 import org.openecomp.sdc.be.config.ConfigurationManager;
72 import org.openecomp.sdc.be.dao.api.ActionStatus;
73 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
74 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
75 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
76 import org.openecomp.sdc.be.datamodel.utils.PropertyValueConstraintValidationUtil;
77 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
78 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
79 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathElementDataDefinition;
80 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
81 import org.openecomp.sdc.be.datatypes.elements.GetPolicyValueDataDefinition;
82 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
83 import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition;
84 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
85 import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition;
86 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
87 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
88 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
89 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
90 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
91 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
92 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
93 import org.openecomp.sdc.be.exception.BusinessException;
94 import org.openecomp.sdc.be.impl.ComponentsUtils;
95 import org.openecomp.sdc.be.model.ArtifactDefinition;
96 import org.openecomp.sdc.be.model.CapabilityDefinition;
97 import org.openecomp.sdc.be.model.CapabilityRequirementRelationship;
98 import org.openecomp.sdc.be.model.Component;
99 import org.openecomp.sdc.be.model.ComponentInstance;
100 import org.openecomp.sdc.be.model.ComponentInstanceAttribute;
101 import org.openecomp.sdc.be.model.ComponentInstanceInput;
102 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
103 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
104 import org.openecomp.sdc.be.model.ComponentParametersView;
105 import org.openecomp.sdc.be.model.DataTypeDefinition;
106 import org.openecomp.sdc.be.model.InputDefinition;
107 import org.openecomp.sdc.be.model.LifecycleStateEnum;
108 import org.openecomp.sdc.be.model.PolicyDefinition;
109 import org.openecomp.sdc.be.model.PropertyDefinition;
110 import org.openecomp.sdc.be.model.RelationshipImpl;
111 import org.openecomp.sdc.be.model.RelationshipInfo;
112 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
113 import org.openecomp.sdc.be.model.RequirementDefinition;
114 import org.openecomp.sdc.be.model.Resource;
115 import org.openecomp.sdc.be.model.Service;
116 import org.openecomp.sdc.be.model.User;
117 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
118 import org.openecomp.sdc.be.model.jsonjanusgraph.config.ContainerInstanceTypesData;
119 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ForwardingPathOperation;
120 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
121 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.OperationException;
122 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
123 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
124 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
125 import org.openecomp.sdc.be.model.validation.ToscaFunctionValidator;
126 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
127 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
128 import org.openecomp.sdc.common.api.ConfigurationSource;
129 import org.openecomp.sdc.common.impl.ExternalConfiguration;
130 import org.openecomp.sdc.common.impl.FSConfigurationSource;
131 import org.openecomp.sdc.common.util.ValidationUtils;
132 import org.openecomp.sdc.exception.ResponseFormat;
135 * The test suite designed for test functionality of ComponentInstanceBusinessLogic class
137 class ComponentInstanceBusinessLogicTest {
139 private final static String USER_ID = "jh0003";
140 private final static String COMPONENT_ID = "componentId";
141 private final static String ORIGIN_COMPONENT_ID = "originComponentId";
142 private final static String ORIGIN_COMPONENT_VERSION = "1.0";
143 private final static String TO_INSTANCE_ID = "toInstanceId";
144 private final static String TO_INSTANCE_NAME = "toInstanceName";
145 private final static String COMPONENT_INSTANCE_ID = "componentInstanceId";
146 private final static String COMPONENT_INSTANCE_NAME = "componentInstanceName";
147 private final static String FROM_INSTANCE_ID = "fromInstanceId";
148 private final static String RELATION_ID = "relationId";
149 private final static String CAPABILITY_OWNER_ID = "capabilityOwnerId";
150 private final static String CAPABILITY_UID = "capabilityUid";
151 private final static String CAPABILITY_NAME = "capabilityName";
152 private final static String REQUIREMENT_OWNER_ID = "requirementOwnerId";
153 private final static String REQUIREMENT_UID = "requirementUid";
154 private final static String REQUIREMENT_NAME = "requirementName";
155 private final static String RELATIONSHIP_TYPE = "relationshipType";
156 private final static String ARTIFACT_1 = "cloudtech_k8s_charts.zip";
157 private final static String ARTIFACT_2 = "cloudtech_azure_day0.zip";
158 private final static String ARTIFACT_3 = "cloudtech_aws_configtemplate.zip";
159 private final static String ARTIFACT_4 = "k8s_charts.zip";
160 private final static String ARTIFACT_5 = "cloudtech_openstack_configtemplate.zip";
161 private final static String PROP_NAME = "propName";
162 private final static String NON_EXIST_NAME = "nonExistName";
163 private final static String INPUT_ID = "inputId";
164 private final static String ICON_NAME = "icon";
166 private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
168 private ComponentInstancePropInput componentInstancePropInput;
170 private ArtifactsBusinessLogic artifactsBusinessLogic;
172 private ComponentsUtils componentsUtils;
174 private ToscaOperationFacade toscaOperationFacade;
176 private ForwardingPathOperation forwardingPathOperation;
180 private UserValidations userValidations;
182 private GraphLockOperation graphLockOperation;
184 private JanusGraphDao janusGraphDao;
186 private ApplicationDataTypeCache applicationDataTypeCache;
188 private PropertyOperation propertyOperation;
190 private ContainerInstanceTypesData containerInstanceTypeData;
192 private CompositionBusinessLogic compositionBusinessLogic;
194 private ToscaFunctionValidator toscaFunctionValidator;
196 private PropertyBusinessLogic propertyBusinessLogic;
197 private Component service;
198 private Component resource;
199 private ComponentInstance toInstance;
200 private ComponentInstance fromInstance;
201 private RequirementCapabilityRelDef relation;
202 private List<ComponentInstanceProperty> ciPropertyList;
203 private List<ComponentInstanceInput> ciInputList;
206 static void beforeAll() {
210 private static void initConfig() {
211 final ConfigurationSource configurationSource = new FSConfigurationSource(
212 ExternalConfiguration.getChangeListener(),
213 "src/test/resources/config/catalog-be"
215 new ConfigurationManager(configurationSource);
220 MockitoAnnotations.openMocks(this);
221 componentInstanceBusinessLogic = new ComponentInstanceBusinessLogic(null, null, null, null, null, null, null, artifactsBusinessLogic, null,
222 null, forwardingPathOperation, null, null, toscaFunctionValidator, propertyBusinessLogic);
223 componentInstanceBusinessLogic.setComponentsUtils(componentsUtils);
224 componentInstanceBusinessLogic.setToscaOperationFacade(toscaOperationFacade);
225 componentInstanceBusinessLogic.setUserValidations(userValidations);
226 componentInstanceBusinessLogic.setGraphLockOperation(graphLockOperation);
227 componentInstanceBusinessLogic.setJanusGraphDao(janusGraphDao);
228 componentInstanceBusinessLogic.setApplicationDataTypeCache(applicationDataTypeCache);
229 componentInstanceBusinessLogic.setPropertyOperation(propertyOperation);
230 componentInstanceBusinessLogic.setContainerInstanceTypesData(containerInstanceTypeData);
231 componentInstanceBusinessLogic.setCompositionBusinessLogic(compositionBusinessLogic);
238 void testGetRelationByIdSuccess() {
239 getServiceRelationByIdSuccess(service);
240 getServiceRelationByIdSuccess(resource);
244 void testGetRelationByIdUserValidationFailure() {
245 getServiceRelationByIdUserValidationFailure(service);
246 getServiceRelationByIdUserValidationFailure(resource);
250 void testGetRelationByIdComponentNotFoundFailure() {
251 getRelationByIdComponentNotFoundFailure(service);
252 getRelationByIdComponentNotFoundFailure(resource);
256 void testForwardingPathOnVersionChange() {
257 getforwardingPathOnVersionChange();
261 void testIsCloudSpecificArtifact() {
262 assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_1)).isTrue();
263 assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_2)).isTrue();
264 assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_3)).isTrue();
265 assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_4)).isFalse();
266 assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_5)).isFalse();
269 private void getforwardingPathOnVersionChange() {
270 String containerComponentParam = "services";
271 String containerComponentID = "121-cont";
272 String componentInstanceID = "121-cont-1-comp";
273 Service component = new Service();
274 Map<String, ForwardingPathDataDefinition> forwardingPaths = generateForwardingPath(componentInstanceID);
276 //Add existing componentInstance to component
277 List<ComponentInstance> componentInstanceList = new ArrayList<>();
278 ComponentInstance oldComponentInstance = new ComponentInstance();
279 oldComponentInstance.setName("OLD_COMP_INSTANCE");
280 oldComponentInstance.setUniqueId(componentInstanceID);
281 oldComponentInstance.setName(componentInstanceID);
282 oldComponentInstance.setToscaPresentationValue(JsonPresentationFields.CI_COMPONENT_UID, "1-comp");
283 componentInstanceList.add(oldComponentInstance);
284 component.setComponentInstances(componentInstanceList);
285 component.setForwardingPaths(forwardingPaths);
287 List<ComponentInstance> componentInstanceListNew = new ArrayList<>();
288 ComponentInstance newComponentInstance = new ComponentInstance();
289 String new_Comp_UID = "2-comp";
290 newComponentInstance.setToscaPresentationValue(JsonPresentationFields.CI_COMPONENT_UID, new_Comp_UID);
291 newComponentInstance.setUniqueId(new_Comp_UID);
292 componentInstanceListNew.add(newComponentInstance);
293 Component component2 = new Service();
294 component2.setComponentInstances(componentInstanceListNew);
296 //Mock for getting component
297 when(toscaOperationFacade.getToscaElement(eq(containerComponentID), any(ComponentParametersView.class)))
298 .thenReturn(Either.left(component));
299 when(toscaOperationFacade.validateComponentExists(any(String.class))).thenReturn(Either.left(Boolean.TRUE));
300 when(toscaOperationFacade.getToscaFullElement(new_Comp_UID)).thenReturn(Either.left(component2));
302 Either<Set<String>, ResponseFormat> resultOp = componentInstanceBusinessLogic
303 .forwardingPathOnVersionChange(containerComponentParam,
304 containerComponentID, componentInstanceID,
305 newComponentInstance);
306 assertEquals(1, resultOp.left().value().size());
307 assertEquals("FP-ID-1", resultOp.left().value().iterator().next());
313 void testCreateOrUpdatePropertiesValues2() {
314 String containerComponentID = "containerId";
315 String resourceInstanceId = "resourceId";
316 String componentInstanceID = "componentInstance";
317 List<ComponentInstanceProperty> properties = new ArrayList<>();
318 ComponentInstanceProperty property = new ComponentInstanceProperty();
319 property.setName("property");
320 property.setValue("newVal");
321 property.setType("string");
322 property.setUniqueId("propId");
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 List<PropertyDefinition> ciProperties = new ArrayList<>();
333 PropertyDefinition ciProperty = new ComponentInstanceProperty();
334 ciProperty.setName("property");
335 ciProperty.setValue("value");
336 ciProperty.setType("string");
337 ciProperty.setUniqueId("propId");
338 ciProperties.add(ciProperty);
340 Map<String, DataTypeDefinition> types = new HashMap<>();
341 DataTypeDefinition dataTypeDef = new DataTypeDefinition();
342 types.put("string", dataTypeDef);
344 Component component = new Service();
345 component.setLastUpdaterUserId("userId");
346 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
347 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
348 componentInstanceProps.put("resourceId", origProperties);
349 component.setComponentInstancesProperties(componentInstanceProps);
350 ComponentInstance ci = createComponentInstance("ci1");
351 ci.setUniqueId("resourceId");
352 ci.setProperties(ciProperties);
353 component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
354 createComponentInstance(componentInstanceID)));
356 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
357 .thenReturn(Either.left(component));
358 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
359 .thenReturn(StorageOperationStatus.OK);
360 when(componentsUtils.getAllDataTypes(applicationDataTypeCache, component.getModel())).thenReturn(types);
361 when(propertyOperation.validateAndUpdatePropertyValue(property.getType(), "newVal", true, null, types))
362 .thenReturn(Either.left("newVal"));
363 when(propertyOperation.validateAndUpdateRules("string", property.getRules(),
364 null, types, true)).thenReturn(ImmutablePair.of("string", null));
365 when(toscaOperationFacade.updateComponentInstanceProperty(component, ci.getUniqueId(),
366 origProperty)).thenReturn(StorageOperationStatus.OK);
367 origProperties.get(0).setValue("newVal");
368 when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(component))
369 .thenReturn(Either.left(component));
370 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
371 when(graphLockOperation.unlockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
372 .thenReturn(StorageOperationStatus.OK);
373 when(propertyBusinessLogic.getComponentModelByComponentId(any())).thenReturn(component.getModel());
374 when(applicationDataTypeCache.getAll(any())).thenReturn(Either.left(types));
375 PropertyValueConstraintValidationUtil validationUtil = Mockito.mock(PropertyValueConstraintValidationUtil.class);
376 when(validationUtil.validatePropertyConstraints(any(),any(),any())).thenReturn(Either.left(any()));
378 Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = componentInstanceBusinessLogic
379 .createOrUpdatePropertiesValues(
380 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
381 assertThat(responseFormatEither.left().value()).isEqualTo(properties);
388 void testCreateOrUpdatePropertiesValuesPropertyNotExists() {
389 String containerComponentID = "containerId";
390 String resourceInstanceId = "resourceId";
391 String componentInstanceID = "componentInstance";
392 List<ComponentInstanceProperty> properties = new ArrayList<>();
393 ComponentInstanceProperty property = new ComponentInstanceProperty();
394 property.setName("property");
395 property.setValue("newVal");
396 property.setType("string");
397 properties.add(property);
399 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
401 Component component = new Service();
402 component.setLastUpdaterUserId("userId");
403 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
404 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
405 componentInstanceProps.put("resourceId", origProperties);
406 component.setComponentInstancesProperties(componentInstanceProps);
407 ComponentInstance ci = createComponentInstance("ci1");
408 ci.setUniqueId("resourceId");
409 component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
410 createComponentInstance(componentInstanceID)));
412 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
413 .thenReturn(Either.left(component));
414 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
415 .thenReturn(StorageOperationStatus.OK);
416 when(graphLockOperation.unlockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
417 .thenReturn(StorageOperationStatus.OK);
418 when(propertyBusinessLogic.getComponentModelByComponentId(any())).thenReturn(component.getModel());
419 Map<String, DataTypeDefinition> dataTypeMap = new HashMap<>();
420 when(applicationDataTypeCache.getAll(any())).thenReturn(Either.left(dataTypeMap));
421 PropertyValueConstraintValidationUtil validationUtil = Mockito.mock(PropertyValueConstraintValidationUtil.class);
422 when(validationUtil.validatePropertyConstraints(any(),any(),any()))
423 .thenReturn(Either.left(any()));
426 componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
427 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
428 } catch (ComponentException e) {
429 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.PROPERTY_NOT_FOUND);
436 void testCreateOrUpdatePropertiesValuesValidationFailure() {
437 String containerComponentID = "containerId";
438 String resourceInstanceId = "resourceId";
439 String componentInstanceID = "componentInstance";
440 List<ComponentInstanceProperty> properties = new ArrayList<>();
441 ComponentInstanceProperty property = new ComponentInstanceProperty();
442 property.setName("property");
443 property.setValue("newVal");
444 property.setType("string");
445 properties.add(property);
447 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
448 ComponentInstanceProperty origProperty = new ComponentInstanceProperty();
449 origProperty.setName("property");
450 origProperty.setValue("value");
451 origProperty.setType("string");
452 origProperties.add(origProperty);
454 Map<String, DataTypeDefinition> types = new HashMap<>();
455 DataTypeDefinition dataTypeDef = new DataTypeDefinition();
456 types.put("string", dataTypeDef);
458 Component component = new Service();
459 component.setLastUpdaterUserId("userId");
460 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
461 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
462 componentInstanceProps.put("resourceId", origProperties);
463 component.setComponentInstancesProperties(componentInstanceProps);
464 ComponentInstance ci = createComponentInstance("ci1");
465 ci.setUniqueId("resourceId");
466 component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
467 createComponentInstance(componentInstanceID)));
469 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
470 .thenReturn(Either.left(component));
471 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
472 .thenReturn(StorageOperationStatus.OK);
473 when(componentsUtils.getAllDataTypes(applicationDataTypeCache, component.getModel())).thenReturn(types);
474 when(propertyOperation.validateAndUpdatePropertyValue(property.getType(), "newVal", true, null, types))
475 .thenReturn(Either.right(false));
476 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.BAD_REQUEST))
477 .thenReturn(ActionStatus.INVALID_CONTENT);
478 when(propertyBusinessLogic.getComponentModelByComponentId(any())).thenReturn(component.getModel());
479 Map<String, DataTypeDefinition> dataTypeMap = new HashMap<>();
480 when(applicationDataTypeCache.getAll(any())).thenReturn(Either.left(dataTypeMap));
481 PropertyValueConstraintValidationUtil validationUtil = Mockito.mock(PropertyValueConstraintValidationUtil.class);
482 when(validationUtil.validatePropertyConstraints(any(),any(),any()))
483 .thenReturn(Either.left(any()));
485 final Either<List<ComponentInstanceProperty>, ResponseFormat> response = componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
486 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
487 assertThat(response.isRight()).as("Response should be an error").isTrue();
488 final ResponseFormat responseFormat = response.right().value();
489 assertThat(responseFormat.getStatus()).as("Response status should be as expected").isEqualTo(400);
490 assertThat(responseFormat.getMessageId()).as("Error message id should be as expected").isEqualTo("SVC4726");
495 void testCreateOrUpdatePropertiesValuesMissingFieldFailure() {
496 String containerComponentID = "containerId";
497 String resourceInstanceId = "resourceId";
498 String componentInstanceID = "componentInstance";
499 List<ComponentInstanceProperty> properties = new ArrayList<>();
500 ComponentInstanceProperty property = new ComponentInstanceProperty();
501 property.setValue("newVal");
502 property.setType("string");
503 properties.add(property);
505 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
506 ComponentInstanceProperty origProperty = new ComponentInstanceProperty();
507 origProperty.setName("property");
508 origProperty.setValue("value");
509 origProperty.setType("string");
510 origProperties.add(origProperty);
512 Component component = new Service();
513 component.setLastUpdaterUserId("userId");
514 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
515 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
516 componentInstanceProps.put("resourceId", origProperties);
517 component.setComponentInstancesProperties(componentInstanceProps);
518 ComponentInstance ci = createComponentInstance("ci1");
519 ci.setUniqueId("resourceId");
520 component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
521 createComponentInstance(componentInstanceID)));
523 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
524 .thenReturn(Either.left(component));
525 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
526 .thenReturn(StorageOperationStatus.OK);
527 when(propertyBusinessLogic.getComponentModelByComponentId(any())).thenReturn(component.getModel());
528 Map<String, DataTypeDefinition> dataTypeMap = new HashMap<>();
529 when(applicationDataTypeCache.getAll(any())).thenReturn(Either.left(dataTypeMap));
530 PropertyValueConstraintValidationUtil validationUtil = Mockito.mock(PropertyValueConstraintValidationUtil.class);
531 when(validationUtil.validatePropertyConstraints(any(),any(),any()))
532 .thenReturn(Either.left(any()));
535 componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
536 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
537 } catch (ComponentException e) {
538 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.MISSING_PROPERTY_NAME);
543 void testDeleteForwardingPathsWhenComponentinstanceDeleted() {
545 ComponentTypeEnum containerComponentType = ComponentTypeEnum.findByParamName("services");
546 String containerComponentID = "Service-comp";
547 String componentInstanceID = "NodeA1";
548 Service component = new Service();
550 .setComponentInstances(Arrays.asList(createComponentInstance("NodeA2"), createComponentInstance("NodeB2"),
551 createComponentInstance(componentInstanceID)));
553 component.addForwardingPath(createPath("path1", componentInstanceID, "NodeB1", "1"));
554 component.addForwardingPath(createPath("Path2", "NodeA2", "NodeB2", "2"));
555 when(toscaOperationFacade.getToscaElement(eq(containerComponentID), any(ComponentParametersView.class)))
556 .thenReturn(Either.left(component));
557 when(toscaOperationFacade.getToscaElement(containerComponentID)).thenReturn(Either.left(component));
558 when(forwardingPathOperation.deleteForwardingPath(any(Service.class), anySet()))
559 .thenReturn(Either.left(new HashSet<>()));
560 final ComponentInstance ci = new ComponentInstance();
561 ci.setName(componentInstanceID);
562 ComponentInstance responseFormatEither = componentInstanceBusinessLogic
563 .deleteForwardingPathsRelatedTobeDeletedComponentInstance(
564 containerComponentID, containerComponentType, ci);
565 assertFalse(responseFormatEither.isEmpty());
569 void testAddComponentInstanceDeploymentArtifacts() {
571 Component containerComponent = new Service();
572 ComponentInstance componentInstance = new ComponentInstance();
573 componentInstance.setUniqueId(COMPONENT_INSTANCE_ID);
574 Component originComponent = fillOriginComponent(new Resource());
576 Map<String, ArtifactDefinition> artifacts = new HashMap<>();
577 ArtifactDefinition deploymentArtifact1 = getArtifact("deploymentArtifact1", ArtifactTypeEnum.HEAT.getType());
578 artifacts.put(deploymentArtifact1.getArtifactLabel(), deploymentArtifact1);
579 ArtifactDefinition deploymentArtifact2 = getArtifact("deploymentArtifact2",
580 ArtifactTypeEnum.HEAT_ENV.getType());
581 artifacts.put(deploymentArtifact2.getArtifactLabel(), deploymentArtifact2);
582 ArtifactDefinition deploymentArtifact3 = getArtifact("deploymentArtifact3",
583 ArtifactTypeEnum.HEAT_VOL.getType());
584 artifacts.put(deploymentArtifact3.getArtifactLabel(), deploymentArtifact3);
585 ArtifactDefinition heatEnvPlaceHolder = getArtifact("deploymentArtifact4", ArtifactTypeEnum.HEAT_ENV.getType());
586 ArtifactDefinition heatEnvPlaceHolder2 = getArtifact("deploymentArtifact5",
587 ArtifactTypeEnum.HEAT_ENV.getType());
589 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getResourceDeploymentArtifacts = Either
592 Map<String, ArtifactDefinition> finalDeploymentArtifacts = new HashMap<>();
593 finalDeploymentArtifacts.put(deploymentArtifact1.getArtifactLabel(), deploymentArtifact1);
594 finalDeploymentArtifacts.put(deploymentArtifact3.getArtifactLabel(), deploymentArtifact3);
595 finalDeploymentArtifacts.put(heatEnvPlaceHolder.getArtifactLabel(), heatEnvPlaceHolder);
596 finalDeploymentArtifacts.put(heatEnvPlaceHolder2.getArtifactLabel(), heatEnvPlaceHolder2);
597 when(artifactsBusinessLogic.getArtifacts(componentInstance.getComponentUid(), NodeTypeEnum.Resource,
598 ArtifactGroupTypeEnum.DEPLOYMENT, null)).thenReturn(getResourceDeploymentArtifacts);
599 when(artifactsBusinessLogic.createHeatEnvPlaceHolder(new ArrayList<>(),
600 deploymentArtifact1, ArtifactsBusinessLogic.HEAT_ENV_NAME, componentInstance.getUniqueId(),
601 NodeTypeEnum.ResourceInstance, componentInstance.getName(), user, containerComponent,
602 null)).thenReturn(heatEnvPlaceHolder);
603 when(artifactsBusinessLogic.createHeatEnvPlaceHolder(new ArrayList<>(),
604 deploymentArtifact3, ArtifactsBusinessLogic.HEAT_ENV_NAME, componentInstance.getUniqueId(),
605 NodeTypeEnum.ResourceInstance, componentInstance.getName(), user, containerComponent,
606 null)).thenReturn(heatEnvPlaceHolder2);
608 componentInstanceBusinessLogic.setToscaOperationFacade(toscaOperationFacade);
609 when(toscaOperationFacade.addDeploymentArtifactsToInstance(containerComponent.getUniqueId(), componentInstance,
610 finalDeploymentArtifacts)).thenReturn(StorageOperationStatus.OK);
611 when(toscaOperationFacade
612 .addGroupInstancesToComponentInstance(containerComponent, componentInstance, new ArrayList<>(),
614 .thenReturn(StorageOperationStatus.OK);
615 when(toscaOperationFacade
616 .addInformationalArtifactsToInstance(containerComponent.getUniqueId(), componentInstance, null))
617 .thenReturn(StorageOperationStatus.OK);
619 ActionStatus status = componentInstanceBusinessLogic.addComponentInstanceArtifacts(containerComponent,
620 componentInstance, originComponent, user, null);
622 assertThat(status).isEqualTo(ActionStatus.OK);
626 private Component fillOriginComponent(Resource originComponent) {
627 originComponent.setUniqueId("resourceId");
628 originComponent.setUniqueId(ORIGIN_COMPONENT_ID);
629 originComponent.setComponentInstances(Lists.newArrayList(toInstance, fromInstance));
630 originComponent.setComponentType(ComponentTypeEnum.RESOURCE);
631 originComponent.setState(LifecycleStateEnum.CERTIFIED);
632 return originComponent;
635 private ArtifactDefinition getArtifact(String artifactLabel, String artifactType) {
636 ArtifactDefinition artifactDefinition = new ArtifactDefinition();
637 artifactDefinition.setArtifactLabel(artifactLabel);
638 artifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
639 artifactDefinition.setEsId("esId" + artifactLabel);
640 artifactDefinition.setArtifactType(artifactType);
641 artifactDefinition.setArtifactName("artifactName");
642 return artifactDefinition;
645 private ComponentInstance createComponentInstance(String path1) {
646 ComponentInstance componentInstance = new ComponentInstance();
647 componentInstance.setName(path1);
648 return componentInstance;
651 private ForwardingPathDataDefinition createPath(String pathName, String fromNode, String toNode, String uniqueId) {
652 ForwardingPathDataDefinition forwardingPath = new ForwardingPathDataDefinition(pathName);
653 forwardingPath.setProtocol("protocol");
654 forwardingPath.setDestinationPortNumber("port");
655 forwardingPath.setUniqueId(uniqueId);
656 ListDataDefinition<ForwardingPathElementDataDefinition> forwardingPathElementListDataDefinition =
657 new ListDataDefinition<>();
658 forwardingPathElementListDataDefinition
659 .add(new ForwardingPathElementDataDefinition(fromNode, toNode, "nodeAcpType", "nodeBcpType",
660 "nodeDcpName", "nodeBcpName"));
661 forwardingPath.setPathElements(forwardingPathElementListDataDefinition);
663 return forwardingPath;
666 private Map<String, ForwardingPathDataDefinition> generateForwardingPath(String componentInstanceID) {
667 ForwardingPathDataDefinition forwardingPath = new ForwardingPathDataDefinition("fpName");
668 String protocol = "protocol";
669 forwardingPath.setProtocol(protocol);
670 forwardingPath.setDestinationPortNumber("DestinationPortNumber");
671 forwardingPath.setUniqueId("FP-ID-1");
672 ListDataDefinition<ForwardingPathElementDataDefinition> forwardingPathElementListDataDefinition =
673 new ListDataDefinition<>();
674 forwardingPathElementListDataDefinition
675 .add(new ForwardingPathElementDataDefinition(componentInstanceID, "nodeB", "nodeA_FORWARDER_CAPABILITY",
676 "nodeBcpType", "nodeDcpName", "nodeBcpName"));
677 forwardingPath.setPathElements(forwardingPathElementListDataDefinition);
678 Map<String, ForwardingPathDataDefinition> forwardingPaths = new HashMap<>();
679 forwardingPaths.put("1122", forwardingPath);
680 return forwardingPaths;
683 private void getServiceRelationByIdSuccess(Component component) {
684 Either<Component, StorageOperationStatus> getComponentRes = Either.left(component);
685 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
686 .thenReturn(getComponentRes);
687 Either<RequirementCapabilityRelDef, ResponseFormat> response = componentInstanceBusinessLogic
688 .getRelationById(COMPONENT_ID,
689 RELATION_ID, USER_ID,
690 component.getComponentType());
691 assertThat(response.isLeft()).isTrue();
694 private void getServiceRelationByIdUserValidationFailure(Component component) {
695 doThrow(new ByActionStatusComponentException(ActionStatus.USER_NOT_FOUND)).when(userValidations).validateUserExists(USER_ID);
697 componentInstanceBusinessLogic
698 .getRelationById(COMPONENT_ID, RELATION_ID, USER_ID, component.getComponentType());
699 } catch (ByActionStatusComponentException e) {
700 assertSame(ActionStatus.USER_NOT_FOUND, e.getActionStatus());
704 private void getRelationByIdComponentNotFoundFailure(Component component) {
705 Either<Component, StorageOperationStatus> getComponentRes = Either.right(StorageOperationStatus.NOT_FOUND);
706 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
707 .thenReturn(getComponentRes);
709 Either<RequirementCapabilityRelDef, ResponseFormat> response = componentInstanceBusinessLogic
710 .getRelationById(COMPONENT_ID,
711 RELATION_ID, USER_ID,
712 component.getComponentType());
713 assertThat(response.isRight()).isTrue();
716 private void stubMethods() {
717 Mockito.lenient().when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(user);
718 Mockito.lenient().when(componentsUtils
719 .convertFromStorageResponse(eq(StorageOperationStatus.GENERAL_ERROR), any(ComponentTypeEnum.class)))
720 .thenReturn(ActionStatus.GENERAL_ERROR);
723 private void createComponents() {
729 resource = createResource();
732 private Resource createResource() {
733 final Resource resource = new Resource();
734 resource.setUniqueId(COMPONENT_ID);
735 resource.setComponentInstancesRelations(Lists.newArrayList(relation));
736 resource.setComponentInstances(Lists.newArrayList(toInstance, fromInstance));
737 resource.setCapabilities(toInstance.getCapabilities());
738 resource.setRequirements(fromInstance.getRequirements());
739 resource.setComponentType(ComponentTypeEnum.RESOURCE);
740 resource.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
744 private void createService() {
745 service = new Service();
746 service.setUniqueId(COMPONENT_ID);
747 service.setComponentInstancesRelations(Lists.newArrayList(relation));
748 service.setComponentInstances(Lists.newArrayList(toInstance, fromInstance));
749 service.setCapabilities(toInstance.getCapabilities());
750 service.setRequirements(fromInstance.getRequirements());
751 service.setComponentType(ComponentTypeEnum.SERVICE);
752 service.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
753 service.setLastUpdaterUserId(USER_ID);
755 Map<String, List<ComponentInstanceProperty>> ciPropertyMap = new HashMap<>();
756 ciPropertyMap.put(TO_INSTANCE_ID, ciPropertyList);
757 service.setComponentInstancesProperties(ciPropertyMap);
759 Map<String, List<ComponentInstanceInput>> ciInputMap = new HashMap<>();
760 ciInputMap.put(TO_INSTANCE_ID, ciInputList);
761 service.setComponentInstancesInputs(ciInputMap);
764 private void createInstances() {
765 toInstance = new ComponentInstance();
766 toInstance.setUniqueId(TO_INSTANCE_ID);
767 toInstance.setName(TO_INSTANCE_NAME);
769 fromInstance = new ComponentInstance();
770 fromInstance.setUniqueId(FROM_INSTANCE_ID);
772 CapabilityDataDefinition capability = new CapabilityDataDefinition();
773 capability.setOwnerId(CAPABILITY_OWNER_ID);
774 capability.setUniqueId(CAPABILITY_UID);
775 capability.setName(CAPABILITY_NAME);
777 Map<String, List<CapabilityDefinition>> capabilities = new HashMap<>();
778 final CapabilityDefinition capabilityDefinition = new CapabilityDefinition(capability);
779 final ArrayList<ComponentInstanceProperty> properties = new ArrayList<>();
780 properties.add(componentInstancePropInput);
781 capabilityDefinition.setProperties(properties);
782 capabilities.put(capability.getName(), Lists.newArrayList(capabilityDefinition));
784 RequirementDataDefinition requirement = new RequirementDataDefinition();
785 requirement.setOwnerId(REQUIREMENT_OWNER_ID);
786 requirement.setUniqueId(REQUIREMENT_UID);
787 requirement.setName(REQUIREMENT_NAME);
788 requirement.setRelationship(RELATIONSHIP_TYPE);
790 Map<String, List<RequirementDefinition>> requirements = new HashMap<>();
791 requirements.put(requirement.getCapability(), Lists.newArrayList(new RequirementDefinition(requirement)));
793 toInstance.setCapabilities(capabilities);
794 fromInstance.setRequirements(requirements);
798 private void createRelation() {
800 relation = new RequirementCapabilityRelDef();
801 CapabilityRequirementRelationship relationship = new CapabilityRequirementRelationship();
802 RelationshipInfo relationInfo = new RelationshipInfo();
803 relationInfo.setId(RELATION_ID);
804 relationship.setRelation(relationInfo);
806 relation.setRelationships(Lists.newArrayList(relationship));
807 relation.setToNode(TO_INSTANCE_ID);
808 relation.setFromNode(FROM_INSTANCE_ID);
810 relationInfo.setCapabilityOwnerId(CAPABILITY_OWNER_ID);
811 relationInfo.setCapabilityUid(CAPABILITY_UID);
812 relationInfo.setCapability(CAPABILITY_NAME);
813 relationInfo.setRequirementOwnerId(REQUIREMENT_OWNER_ID);
814 relationInfo.setRequirementUid(REQUIREMENT_UID);
815 relationInfo.setRequirement(REQUIREMENT_NAME);
816 RelationshipImpl relationshipImpl = new RelationshipImpl();
817 relationshipImpl.setType(RELATIONSHIP_TYPE);
818 relationInfo.setRelationships(relationshipImpl);
821 private void createProperties() {
822 // Create GetInputValueData
823 GetInputValueDataDefinition inputValueDef = new GetInputValueDataDefinition();
824 inputValueDef.setInputId(INPUT_ID);
825 List<GetInputValueDataDefinition> inputValueDefList = new ArrayList<>();
826 inputValueDefList.add(inputValueDef);
827 // Create ComponentInstanceProperty
828 ComponentInstanceProperty ciProperty = new ComponentInstanceProperty();
829 ciProperty.setGetInputValues(inputValueDefList);
830 ciProperty.setName(PROP_NAME);
831 // Create ComponentInstanceProperty list
832 ciPropertyList = new ArrayList<>();
833 ciPropertyList.add(ciProperty);
836 private void createInputs() {
837 // Create GetInputValueData
838 GetInputValueDataDefinition inputValueDef = new GetInputValueDataDefinition();
839 inputValueDef.setInputId(INPUT_ID);
840 List<GetInputValueDataDefinition> inputValueDefList = new ArrayList<>();
841 inputValueDefList.add(inputValueDef);
842 // Create ComponentInstanceInput
843 ComponentInstanceInput ciInput = new ComponentInstanceInput();
844 ciInput.setUniqueId(INPUT_ID);
845 ciInput.setName(PROP_NAME);
846 ciInput.setGetInputValues(inputValueDefList);
847 // Create ComponentInstanceInput list
848 ciInputList = new ArrayList<>();
849 ciInputList.add(ciInput);
852 private ComponentInstanceBusinessLogic createTestSubject() {
853 return componentInstanceBusinessLogic;
857 void testChangeServiceProxyVersion() {
858 ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
860 Either<ComponentInstance, ResponseFormat> result;
863 componentInstanceBusinessLogic = createTestSubject();
864 result = componentInstanceBusinessLogic.changeServiceProxyVersion();
865 assertNotNull(result);
869 void testCreateServiceProxy() {
870 ComponentInstanceBusinessLogic testSubject;
871 Either<ComponentInstance, ResponseFormat> result;
874 testSubject = createTestSubject();
875 result = testSubject.createServiceProxy();
876 assertNotNull(result);
880 void testDeleteServiceProxy() {
881 ComponentInstanceBusinessLogic testSubject;
883 Either<ComponentInstance, ResponseFormat> result;
886 testSubject = createTestSubject();
887 result = testSubject.deleteServiceProxy();
888 assertNotNull(result);
892 void testGetComponentInstanceInputsByInputIdEmpty() {
893 Component component = new Service();
895 List<ComponentInstanceInput> result;
897 result = componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(component, inputId);
898 assertNotNull(result);
899 assertThat(result.isEmpty()).isTrue();
903 void testGetComponentInstanceInputsByInputIdPresent() {
904 List<ComponentInstanceInput> result;
906 result = componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(service, INPUT_ID);
907 assertNotNull(result);
908 assertThat(result.isEmpty()).isFalse();
909 assertThat(result.size()).isOne();
910 ComponentInstanceInput resultInput = result.get(0);
911 assertThat(resultInput.getComponentInstanceId()).isEqualTo(TO_INSTANCE_ID);
912 assertThat(resultInput.getComponentInstanceName()).isEqualTo(TO_INSTANCE_NAME);
916 void testGetComponentInstancePropertiesByInputIdEmpty() {
917 Component component = new Service();
919 List<ComponentInstanceProperty> result;
921 result = componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(component, inputId);
922 assertNotNull(result);
923 assertThat(result.isEmpty()).isTrue();
927 void testGetComponentInstancePropertiesByInputIdPresent() {
928 List<ComponentInstanceProperty> result;
930 result = componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(service, INPUT_ID);
931 assertNotNull(result);
932 assertThat(result.size()).isOne();
933 ComponentInstanceProperty resultProperty = result.get(0);
934 assertThat(resultProperty.getComponentInstanceId()).isEqualTo(TO_INSTANCE_ID);
935 assertThat(resultProperty.getComponentInstanceName()).isEqualTo(TO_INSTANCE_NAME);
939 void testGetRelationById() {
940 ComponentInstanceBusinessLogic testSubject;
941 String componentId = "";
942 String relationId = "";
943 String userId = user.getUserId();
944 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE_INSTANCE;
945 Either<RequirementCapabilityRelDef, ResponseFormat> result;
948 testSubject = createTestSubject();
949 result = testSubject.getRelationById(componentId, relationId, userId, componentTypeEnum);
950 assertNotNull(result);
954 void testValidateParent() {
955 ComponentInstanceBusinessLogic testSubject;
956 resource = createResource();
957 String nodeTemplateId = "";
961 testSubject = createTestSubject();
962 result = Deencapsulation.invoke(testSubject, "validateParent", new Object[]{resource, nodeTemplateId});
967 void testGetComponentType() {
968 ComponentInstanceBusinessLogic testSubject;
969 ComponentTypeEnum result;
972 testSubject = createTestSubject();
973 result = Deencapsulation.invoke(testSubject, "getComponentType", new Object[]{ComponentTypeEnum.class});
974 assertNotNull(result);
978 void testGetNewGroupName() {
979 ComponentInstanceBusinessLogic testSubject;
980 String oldPrefix = "";
981 String newNormailzedPrefix = "";
982 String qualifiedGroupInstanceName = "";
986 testSubject = createTestSubject();
987 result = Deencapsulation.invoke(testSubject, "getNewGroupName",
988 new Object[]{oldPrefix, newNormailzedPrefix, qualifiedGroupInstanceName});
989 assertNotNull(result);
993 void testUpdateComponentInstanceMetadata_3() {
994 ComponentInstanceBusinessLogic testSubject;
996 ComponentInstance result;
999 testSubject = createTestSubject();
1000 result = Deencapsulation
1001 .invoke(testSubject, "updateComponentInstanceMetadata", new Object[]{toInstance, toInstance});
1002 assertNotNull(result);
1006 void testFindRelation() {
1007 ComponentInstanceBusinessLogic testSubject;
1008 String relationId = "";
1009 List<RequirementCapabilityRelDef> requirementCapabilityRelations = new ArrayList<>();
1010 RequirementCapabilityRelDef result;
1013 testSubject = createTestSubject();
1014 result = Deencapsulation.invoke(testSubject, "findRelation",
1015 new Object[]{relationId, requirementCapabilityRelations});
1020 void testCreateOrUpdatePropertiesValues() {
1021 ComponentInstanceBusinessLogic testSubject;
1022 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1023 resource = createResource();
1024 String componentId = resource.getUniqueId();
1025 String resourceInstanceId = "";
1026 List<ComponentInstanceProperty> properties = new ArrayList<>();
1027 String userId = user.getUserId();
1028 Either<List<ComponentInstanceProperty>, ResponseFormat> result;
1030 when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll))
1031 .thenReturn(Either.left(resource));
1034 testSubject = createTestSubject();
1035 result = testSubject
1036 .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
1038 assertNotNull(result);
1040 componentTypeEnum = null;
1041 result = testSubject
1042 .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
1044 assertNotNull(result);
1046 result = testSubject
1047 .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
1049 assertNotNull(result);
1053 void testUpdateCapabilityPropertyOnContainerComponent() {
1054 ComponentInstanceBusinessLogic testSubject;
1055 ComponentInstanceProperty property = new ComponentInstanceProperty();
1056 String newValue = "";
1057 resource = createResource();
1059 String capabilityType = "";
1060 String capabilityName = "";
1061 ResponseFormat result;
1064 testSubject = createTestSubject();
1065 result = Deencapsulation.invoke(testSubject, "updateCapabilityPropertyOnContainerComponent",
1066 new Object[]{property, newValue, resource, toInstance, capabilityType, capabilityName});
1071 void testCreateOrUpdateInstanceInputValues() {
1072 ComponentInstanceBusinessLogic testSubject;
1073 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1074 resource = createResource();
1075 String componentId = resource.getUniqueId();
1076 String resourceInstanceId = "";
1077 List<ComponentInstanceInput> inputs = new ArrayList<>();
1078 String userId = user.getUserId();
1079 Either<List<ComponentInstanceInput>, ResponseFormat> result;
1081 when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll))
1082 .thenReturn(Either.left(resource));
1085 testSubject = createTestSubject();
1086 result = testSubject
1087 .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
1089 assertNotNull(result);
1090 componentTypeEnum = null;
1091 result = testSubject
1092 .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
1094 assertNotNull(result);
1096 result = testSubject
1097 .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
1099 assertNotNull(result);
1103 void testCreateOrUpdateGroupInstancePropertyValue() {
1104 ComponentInstanceBusinessLogic testSubject;
1105 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1106 resource = createResource();
1107 String componentId = resource.getUniqueId();
1108 String resourceInstanceId = "";
1109 String groupInstanceId = "";
1110 ComponentInstanceProperty property = new ComponentInstanceProperty();
1111 String userId = user.getUserId();
1112 Either<ComponentInstanceProperty, ResponseFormat> result;
1114 when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseMetadata))
1115 .thenReturn(Either.left(resource));
1118 testSubject = createTestSubject();
1119 result = testSubject
1120 .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1121 groupInstanceId, property, userId);
1122 assertNotNull(result);
1123 componentTypeEnum = null;
1124 result = testSubject
1125 .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1126 groupInstanceId, property, userId);
1127 assertNotNull(result);
1129 result = testSubject
1130 .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1131 groupInstanceId, property, userId);
1132 assertNotNull(result);
1136 void testDeletePropertyValue() {
1137 ComponentInstanceBusinessLogic testSubject;
1138 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1140 String serviceId = service.getUniqueId();
1141 String resourceInstanceId = "";
1142 String propertyValueId = "";
1143 String userId = user.getUserId();
1144 Either<ComponentInstanceProperty, ResponseFormat> result;
1146 when(toscaOperationFacade.getToscaElement(serviceId, JsonParseFlagEnum.ParseMetadata))
1147 .thenReturn(Either.left(service));
1150 testSubject = createTestSubject();
1151 result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1153 assertNotNull(result);
1154 componentTypeEnum = null;
1155 result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1157 assertNotNull(result);
1159 result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1161 assertNotNull(result);
1165 void testGetComponentParametersViewForForwardingPath() {
1166 ComponentInstanceBusinessLogic testSubject;
1167 ComponentParametersView result;
1170 testSubject = createTestSubject();
1171 result = Deencapsulation.invoke(testSubject, "getComponentParametersViewForForwardingPath");
1172 assertNotNull(result);
1176 void testGetResourceInstanceById() {
1177 ComponentInstanceBusinessLogic testSubject;
1178 resource = createResource();
1179 String instanceId = "";
1180 Either<ComponentInstance, StorageOperationStatus> result;
1183 testSubject = createTestSubject();
1184 result = Deencapsulation.invoke(testSubject, "getResourceInstanceById", new Object[]{resource, instanceId});
1185 assertNotNull(result);
1189 void testUpdateInstanceCapabilityProperties_1() {
1190 ComponentInstanceBusinessLogic testSubject;
1191 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1192 resource = createResource();
1193 String containerComponentId = resource.getUniqueId();
1194 String componentInstanceUniqueId = "";
1195 String capabilityType = "";
1196 String capabilityName = "";
1197 List<ComponentInstanceProperty> properties = new ArrayList<>();
1198 String userId = user.getUserId();
1199 Either<List<ComponentInstanceProperty>, ResponseFormat> result;
1201 when(toscaOperationFacade.getToscaFullElement(containerComponentId))
1202 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
1204 testSubject = createTestSubject();
1205 result = testSubject.updateInstanceCapabilityProperties(componentTypeEnum, containerComponentId,
1206 componentInstanceUniqueId, capabilityType, capabilityName, properties, userId);
1207 assertNotNull(result);
1208 when(toscaOperationFacade.getToscaFullElement(containerComponentId)).thenReturn(Either.left(resource));
1209 result = testSubject.updateInstanceCapabilityProperties(componentTypeEnum, containerComponentId,
1210 componentInstanceUniqueId, capabilityType, capabilityName, properties, userId);
1211 assertNotNull(result);
1215 void testUpdateInstanceRequirement() {
1216 ComponentInstanceBusinessLogic testSubject;
1217 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1219 String userId = "userId";
1220 resource.setLastUpdaterUserId(userId);
1221 String containerComponentId = resource.getUniqueId();
1222 String componentInstanceUniqueId = TO_INSTANCE_ID;
1223 String capabilityType = "";
1224 String capabilityName = "";
1225 RequirementDefinition requirementDef = new RequirementDefinition();
1227 Either<RequirementDefinition, ResponseFormat> result;
1229 when(toscaOperationFacade.getToscaFullElement(containerComponentId)).thenReturn(Either.left(resource));
1230 testSubject = createTestSubject();
1231 when(toscaOperationFacade.updateComponentInstanceRequirement(containerComponentId, TO_INSTANCE_ID, requirementDef)).thenReturn(StorageOperationStatus.OK);
1232 when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(resource)).thenReturn(Either.left(resource));
1233 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource)))
1234 .thenReturn(StorageOperationStatus.OK);
1235 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource)))
1236 .thenReturn(StorageOperationStatus.OK);
1238 result = testSubject.updateInstanceRequirement(componentTypeEnum, containerComponentId,
1239 componentInstanceUniqueId, requirementDef, userId);
1240 assertEquals(requirementDef, result.left().value());
1245 void testCopyComponentInstanceWrongUserId() {
1247 Either<Map<String, ComponentInstance>, ResponseFormat> result;
1248 ComponentInstance inputComponentInstance = createComponetInstanceFromComponent(resource);
1249 String containerComponentId = service.getUniqueId();
1250 String componentInstanceId = resource.getUniqueId();
1251 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1252 service.setLastUpdaterUserId("wrong user id");
1254 Either<Component, StorageOperationStatus> leftServiceOp = Either.left(service);
1255 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(leftServiceOp);
1256 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1257 .thenReturn(leftServiceOp);
1258 when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
1259 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1260 .thenReturn(StorageOperationStatus.OK);
1261 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1262 .thenReturn(StorageOperationStatus.OK);
1264 result = componentInstanceBusinessLogic
1265 .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId,
1267 assertNotNull(result);
1269 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1270 assertThat(result.isRight()).isTrue();
1274 void testCopyComponentInstanceComponentWrongState() {
1275 Either<Map<String, ComponentInstance>, ResponseFormat> result;
1276 ComponentInstance inputComponentInstance = createComponetInstanceFromComponent(resource);
1277 String containerComponentId = service.getUniqueId();
1278 String componentInstanceId = resource.getUniqueId();
1279 String oldServiceLastUpdatedUserId = service.getLastUpdaterUserId();
1280 service.setLastUpdaterUserId(USER_ID);
1282 Either<Component, StorageOperationStatus> leftServiceOp = Either.left(service);
1283 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(leftServiceOp);
1284 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1285 .thenReturn(leftServiceOp);
1286 when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
1287 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1288 .thenReturn(StorageOperationStatus.OK);
1289 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1290 .thenReturn(StorageOperationStatus.OK);
1291 result = componentInstanceBusinessLogic
1292 .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId, USER_ID);
1293 assertNotNull(result);
1294 service.setLastUpdaterUserId(oldServiceLastUpdatedUserId);
1295 assertThat(result.isRight()).isTrue();
1299 void testCopyComponentInstance() {
1300 Either<Map<String, ComponentInstance>, ResponseFormat> result;
1301 ComponentInstance inputComponentInstance = createComponetInstanceFromComponent(resource);
1302 String containerComponentId = service.getUniqueId();
1303 String componentInstanceId = resource.getUniqueId();
1304 String oldServiceLastUpdatedUserId = service.getLastUpdaterUserId();
1305 service.setLastUpdaterUserId(USER_ID);
1306 LifecycleStateEnum oldResourceLifeCycle = resource.getLifecycleState();
1307 resource.setLifecycleState(LifecycleStateEnum.CERTIFIED);
1309 Either<Component, StorageOperationStatus> leftServiceOp = Either.left(service);
1310 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(leftServiceOp);
1311 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1312 .thenReturn(leftServiceOp);
1313 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1314 .thenReturn(StorageOperationStatus.OK);
1315 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1316 .thenReturn(StorageOperationStatus.OK);
1318 result = componentInstanceBusinessLogic
1319 .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId,
1321 assertNotNull(result);
1323 service.setLastUpdaterUserId(oldServiceLastUpdatedUserId);
1324 resource.setLifecycleState(oldResourceLifeCycle);
1326 assertThat(result.isLeft()).isFalse();
1330 void testCreateOrUpdateAttributeValueForCopyPaste() {
1331 ComponentInstance serviceComponentInstance = createComponetInstanceFromComponent(service);
1332 ComponentInstanceAttribute attribute = new ComponentInstanceAttribute();
1333 attribute.setType("string");
1334 attribute.setUniqueId("testCreateOrUpdateAttributeValueForCopyPaste");
1335 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1336 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1337 service.setLastUpdaterUserId(USER_ID);
1338 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1340 Map<String, List<ComponentInstanceAttribute>> instAttrsMap = new HashMap<>();
1341 List<ComponentInstanceAttribute> instAttrsList = new ArrayList<>();
1342 ComponentInstanceAttribute prop = new ComponentInstanceAttribute();
1343 prop.setUniqueId(attribute.getUniqueId());
1344 instAttrsList.add(prop);
1345 instAttrsMap.put(toInstance.getUniqueId(), instAttrsList);
1346 service.setComponentInstancesAttributes(instAttrsMap);
1348 Either<Component, StorageOperationStatus> serviceEitherLeft = Either.left(service);
1349 when(toscaOperationFacade.getToscaElement(serviceComponentInstance.getUniqueId(), JsonParseFlagEnum.ParseAll))
1350 .thenReturn(serviceEitherLeft);
1351 when(toscaOperationFacade.updateComponentInstanceAttribute(service, toInstance.getUniqueId(), attribute))
1352 .thenReturn(StorageOperationStatus.OK);
1353 when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(service))
1354 .thenReturn(serviceEitherLeft);
1356 Either<ComponentInstanceAttribute, ResponseFormat> result = Deencapsulation
1357 .invoke(componentInstanceBusinessLogic,
1358 "createOrUpdateAttributeValueForCopyPaste",
1359 ComponentTypeEnum.SERVICE,
1360 serviceComponentInstance
1362 toInstance.getUniqueId(), attribute,
1364 assertNotNull(result);
1366 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1367 service.setLifecycleState(oldLifeCycleState);
1369 assertThat(result.isLeft()).isTrue();
1370 ComponentInstanceAttribute resultProp = result.left().value();
1371 assertEquals(1, resultProp.getPath().size());
1372 assertEquals(resultProp.getPath().get(0), toInstance.getUniqueId());
1376 void testUpdateComponentInstanceProperty() {
1378 String containerComponentId = service.getUniqueId();
1379 String componentInstanceId = "dummy_id";
1380 ComponentInstanceProperty property = Mockito.mock(ComponentInstanceProperty.class);
1382 Either<Component, StorageOperationStatus> getComponent = Either.left(service);
1383 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(getComponent);
1384 StorageOperationStatus status = StorageOperationStatus.OK;
1385 when(toscaOperationFacade.updateComponentInstanceProperty(service, componentInstanceId, property))
1386 .thenReturn(status);
1387 Either<Component, StorageOperationStatus> updateContainerRes = Either.left(service);
1388 when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(service))
1389 .thenReturn(updateContainerRes);
1391 Either<String, ResponseFormat> result = Deencapsulation.invoke(componentInstanceBusinessLogic,
1392 "updateComponentInstanceProperty", containerComponentId, componentInstanceId, property);
1393 assertNotNull(result);
1394 assertThat(result.isLeft()).isTrue();
1398 void testGetInputListDefaultValue() {
1399 Component component = service;
1400 String inputId = "dummy_id";
1401 String defaultValue = "dummy_default_value";
1402 List<InputDefinition> newInputs = new ArrayList<>();
1403 InputDefinition in = new InputDefinition();
1404 in.setUniqueId(inputId);
1405 in.setDefaultValue(defaultValue);
1407 List<InputDefinition> oldInputs = service.getInputs();
1408 service.setInputs(newInputs);
1410 Either<String, ResponseFormat> result =
1411 Deencapsulation.invoke(componentInstanceBusinessLogic, "getInputListDefaultValue", component, inputId);
1413 service.setInputs(oldInputs);
1415 assertEquals(result.left().value(), defaultValue);
1419 void testBatchDeleteComponentInstanceFailureWrongType() {
1420 Map<String, List<String>> result;
1421 List<String> componentInstanceIdList = new ArrayList<>();
1422 String containerComponentParam = "WRONG_TYPE";
1423 String containerComponentId = "containerComponentId";
1424 String componentInstanceId = "componentInstanceId";
1425 componentInstanceIdList.add(componentInstanceId);
1426 Map<String, List<String>> deleteErrorMap = new HashMap<>();
1427 List<String> deleteErrorIds = new ArrayList<>();
1428 deleteErrorIds.add(componentInstanceId);
1429 deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
1430 Either<Component, StorageOperationStatus> cont = Either.left(service);
1431 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND, null))
1432 .thenReturn(ActionStatus.GENERAL_ERROR);
1433 when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class)))
1437 result = componentInstanceBusinessLogic
1438 .batchDeleteComponentInstance(containerComponentParam, containerComponentId, componentInstanceIdList,
1440 assertNotNull(result);
1441 assertEquals(deleteErrorMap, result);
1442 } catch (ComponentException e) {
1443 assertEquals(e.getActionStatus().toString(), StorageOperationStatus.GENERAL_ERROR.toString());
1448 void testBatchDeleteComponentInstanceFailureCompIds() {
1449 String containerComponentParam = ComponentTypeEnum.SERVICE_PARAM_NAME;
1450 String containerComponentId = "containerComponentId";
1451 String componentInstanceId = "componentInstanceId";
1452 List<String> componentInstanceIdList = new ArrayList<>();
1453 componentInstanceIdList.add(componentInstanceId);
1454 Map<String, List<String>> deleteErrorMap = new HashMap<>();
1455 List<String> deleteErrorIds = new ArrayList<>();
1456 deleteErrorIds.add(componentInstanceId);
1457 deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
1459 Either<Component, StorageOperationStatus> err = Either.right(StorageOperationStatus.GENERAL_ERROR);
1460 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1464 Map<String, List<String>> result = componentInstanceBusinessLogic.batchDeleteComponentInstance(
1465 containerComponentParam, containerComponentId, componentInstanceIdList, USER_ID);
1466 assertNotNull(result);
1467 assertEquals(deleteErrorMap, result);
1468 } catch (ComponentException e) {
1469 assertEquals(e.getActionStatus().toString(), StorageOperationStatus.GENERAL_ERROR.toString());
1474 void testBatchDeleteComponentInstanceSuccess() {
1475 Map<String, List<String>> result;
1476 String containerComponentParam = ComponentTypeEnum.SERVICE_PARAM_NAME;
1477 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1478 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1479 service.setLastUpdaterUserId(USER_ID);
1480 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1481 String containerComponentId = service.getUniqueId();
1482 String componentInstanceId = TO_INSTANCE_ID;
1483 List<String> componentInstanceIdList = new ArrayList<>();
1484 componentInstanceIdList.add(componentInstanceId);
1485 Map<String, List<String>> deleteErrorMap = new HashMap<>();
1486 List<String> deleteErrorIds = new ArrayList<>();
1487 deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
1489 Either<Component, StorageOperationStatus> cont = Either.left(service);
1490 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1491 .thenReturn(StorageOperationStatus.OK);
1492 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1493 .thenReturn(StorageOperationStatus.OK);
1494 ImmutablePair<Component, String> pair = new ImmutablePair<>(resource, TO_INSTANCE_ID);
1495 Either<ImmutablePair<Component, String>, StorageOperationStatus> result2 = Either.left(pair);
1496 when(toscaOperationFacade.deleteComponentInstanceFromTopologyTemplate(service, componentInstanceId))
1497 .thenReturn(result2);
1498 when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
1500 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
1502 result = componentInstanceBusinessLogic
1503 .batchDeleteComponentInstance(containerComponentParam, containerComponentId,
1504 componentInstanceIdList, USER_ID);
1505 assertNotNull(result);
1507 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1508 service.setLifecycleState(oldLifeCycleState);
1509 assertEquals(deleteErrorMap, result);
1513 void testDissociateRIFromRIFailDissociate() {
1515 List<RequirementCapabilityRelDef> result;
1516 RequirementCapabilityRelDef ref = new RequirementCapabilityRelDef();
1517 ref.setFromNode(FROM_INSTANCE_ID);
1518 ref.setToNode(TO_INSTANCE_ID);
1519 List<CapabilityRequirementRelationship> relationships = new ArrayList<>();
1520 CapabilityRequirementRelationship relationship = new CapabilityRequirementRelationship();
1521 RelationshipInfo ri = new RelationshipInfo();
1522 ri.setRequirement(REQUIREMENT_NAME);
1523 relationship.setRelation(ri);
1524 relationships.add(relationship);
1525 ref.setRelationships(relationships);
1526 List<RequirementCapabilityRelDef> requirementDefList = new ArrayList<>();
1527 requirementDefList.add(ref);
1528 ComponentTypeEnum componentTypeEnum = service.getComponentType();
1529 String componentId = service.getUniqueId();
1530 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1531 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1532 service.setLastUpdaterUserId(USER_ID);
1533 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1535 Either<Component, StorageOperationStatus> cont = Either.left(service);
1536 when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
1538 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1539 .thenReturn(StorageOperationStatus.OK);
1540 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1541 .thenReturn(StorageOperationStatus.OK);
1542 Either<RequirementCapabilityRelDef, StorageOperationStatus> resultEither;
1543 resultEither = Either.right(StorageOperationStatus.OK);
1544 when(componentsUtils.convertFromStorageResponseForResourceInstance(StorageOperationStatus.OK, true))
1545 .thenReturn(ActionStatus.GENERAL_ERROR);
1546 when(toscaOperationFacade.dissociateResourceInstances(componentId, ref)).thenReturn(resultEither);
1549 result = componentInstanceBusinessLogic
1550 .batchDissociateRIFromRI(componentId, USER_ID, requirementDefList, componentTypeEnum);
1551 assertNotNull(result);
1552 assertEquals(new ArrayList<>(), result);
1553 } catch (ComponentException e) {
1554 assertEquals(e.getActionStatus().toString(), StorageOperationStatus.GENERAL_ERROR.toString());
1557 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1558 service.setLifecycleState(oldLifeCycleState);
1563 void testDissociateRIFromRISuccess() {
1565 List<RequirementCapabilityRelDef> result;
1566 RequirementCapabilityRelDef ref = new RequirementCapabilityRelDef();
1567 List<RequirementCapabilityRelDef> requirementDefList = new ArrayList<>();
1568 requirementDefList.add(ref);
1569 ComponentTypeEnum componentTypeEnum = service.getComponentType();
1570 String componentId = service.getUniqueId();
1571 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1572 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1573 service.setLastUpdaterUserId(USER_ID);
1574 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1576 Either<Component, StorageOperationStatus> cont = Either.left(service);
1577 when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
1579 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1580 .thenReturn(StorageOperationStatus.OK);
1581 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1582 .thenReturn(StorageOperationStatus.OK);
1583 Either<RequirementCapabilityRelDef, StorageOperationStatus> resultEither;
1584 resultEither = Either.left(ref);
1585 when(toscaOperationFacade.dissociateResourceInstances(componentId, ref)).thenReturn(resultEither);
1587 result = componentInstanceBusinessLogic
1588 .batchDissociateRIFromRI(componentId, USER_ID, requirementDefList, componentTypeEnum);
1589 assertNotNull(result);
1591 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1592 service.setLifecycleState(oldLifeCycleState);
1594 assertEquals(requirementDefList, result);
1598 void testGetComponentInstancePropertyByPolicyId_success() {
1599 Optional<ComponentInstanceProperty> propertyCandidate =
1600 getComponentInstanceProperty(PROP_NAME);
1602 assertThat(propertyCandidate).isPresent();
1603 assertEquals(PROP_NAME, propertyCandidate.get().getName());
1607 void testGetComponentInstancePropertyByPolicyId_failure() {
1608 Optional<ComponentInstanceProperty> propertyCandidate =
1609 getComponentInstanceProperty(NON_EXIST_NAME);
1611 assertEquals(propertyCandidate, Optional.empty());
1614 private Optional<ComponentInstanceProperty> getComponentInstanceProperty(String propertyName) {
1615 ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
1616 componentInstanceProperty.setName(propertyName);
1618 PolicyDefinition policyDefinition = getPolicyDefinition();
1619 componentInstanceProperty.setGetPolicyValues(policyDefinition.getGetPolicyValues());
1621 service.setComponentInstancesProperties(
1622 Collections.singletonMap(COMPONENT_INSTANCE_ID, Collections.singletonList(componentInstanceProperty)));
1624 return componentInstanceBusinessLogic.getComponentInstancePropertyByPolicyId(service, policyDefinition);
1627 private PolicyDefinition getPolicyDefinition() {
1628 PolicyDefinition policyDefinition = new PolicyDefinition();
1629 policyDefinition.setInstanceUniqueId(COMPONENT_INSTANCE_ID);
1630 policyDefinition.setName(PROP_NAME);
1632 GetPolicyValueDataDefinition getPolicy = new GetPolicyValueDataDefinition();
1633 getPolicy.setPropertyName(PROP_NAME);
1635 List<GetPolicyValueDataDefinition> getPolicies = new ArrayList<>();
1636 getPolicies.add(getPolicy);
1637 policyDefinition.setGetPolicyValues(getPolicies);
1639 return policyDefinition;
1642 private ComponentInstance createComponetInstanceFromComponent(Component component) {
1643 ComponentInstance componentInst = new ComponentInstance();
1644 componentInst.setUniqueId(component.getUniqueId());
1645 componentInst.setComponentUid(component.getUniqueId() + "_test");
1646 componentInst.setPosX("10");
1647 componentInst.setPosY("10");
1648 componentInst.setCapabilities(component.getCapabilities());
1649 componentInst.setRequirements(component.getRequirements());
1650 componentInst.setArtifacts(component.getArtifacts());
1651 componentInst.setDeploymentArtifacts(component.getDeploymentArtifacts());
1652 return componentInst;
1655 // Prepare ComponentInstance & Resource objects used in createComponentInstance() tests
1657 private Pair<ComponentInstance, Resource> prepareResourcesForCreateComponentInstanceTest() {
1658 ComponentInstance instanceToBeCreated = new ComponentInstance();
1659 instanceToBeCreated.setName(COMPONENT_INSTANCE_NAME);
1660 instanceToBeCreated.setUniqueId(COMPONENT_INSTANCE_ID);
1661 instanceToBeCreated.setComponentUid(ORIGIN_COMPONENT_ID);
1662 instanceToBeCreated.setOriginType(OriginTypeEnum.VF);
1664 Resource originComponent = new Resource();
1665 originComponent.setLifecycleState(LifecycleStateEnum.CERTIFIED);
1666 originComponent.setResourceType(ResourceTypeEnum.VF);
1667 originComponent.setVersion(ORIGIN_COMPONENT_VERSION);
1668 originComponent.setIcon(ICON_NAME);
1670 return Pair.of(instanceToBeCreated, originComponent);
1672 // Common part for testing component instance name validation
1674 private void testCreateComponentInstanceNameValidationFailure(String ciName) {
1675 ComponentInstance ci = new ComponentInstance();
1678 // Stub for getting component
1679 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
1680 .thenReturn(Either.left(service));
1682 // Expecting ByActionStatusComponentException
1683 ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
1684 componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1686 assertEquals(ActionStatus.INVALID_COMPONENT_NAME, e.getActionStatus());
1689 Iterable<DynamicTest> testCreateComponentInstanceNameValidationFailureFactory() {
1690 String longName = String.join("", Collections.nCopies(ValidationUtils.COMPONENT_NAME_MAX_LENGTH + 1, "x"));
1691 String invalidName = "componentInstance#name";
1692 return Arrays.asList(
1693 dynamicTest("instance name is empty", () ->
1694 testCreateComponentInstanceNameValidationFailure("")),
1695 dynamicTest("instance name is too long", () ->
1696 testCreateComponentInstanceNameValidationFailure(longName)),
1697 dynamicTest("instance name includes invalid character", () ->
1698 testCreateComponentInstanceNameValidationFailure(invalidName))
1703 void testCreateComponentInstanceFailToGetComponent() {
1704 ComponentInstance ci = prepareResourcesForCreateComponentInstanceTest().getLeft();
1706 // Stub for getting component
1707 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
1708 .thenReturn(Either.left(service));
1709 when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
1710 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1711 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND, ComponentTypeEnum.RESOURCE))
1712 .thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
1714 ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
1715 componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1717 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.RESOURCE_NOT_FOUND);
1721 void testCreateComponentInstanceFailureInvalidState() {
1722 Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
1723 ComponentInstance ci = p.getLeft();
1724 Resource originComponent = p.getRight();
1725 originComponent.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1727 // Stub for getting component
1728 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
1729 .thenReturn(Either.left(service));
1730 when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
1731 .thenReturn(Either.left(originComponent));
1733 ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
1734 componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1736 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.CONTAINER_CANNOT_CONTAIN_COMPONENT_IN_STATE);
1740 void testCreateComponentInstanceFailureArchived() {
1741 Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
1742 ComponentInstance ci = p.getLeft();
1743 Resource originComponent = p.getRight();
1744 originComponent.setArchived(Boolean.TRUE);
1746 // Stub for getting component
1747 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
1748 .thenReturn(Either.left(service));
1749 when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
1750 .thenReturn(Either.left(originComponent));
1752 ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
1753 componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1755 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.COMPONENT_IS_ARCHIVED);
1759 void testCreateComponentInstanceFailureInvalidOriginType() {
1760 Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
1761 ComponentInstance ci = p.getLeft();
1762 Resource originComponent = p.getRight();
1763 ci.setOriginType(OriginTypeEnum.VFC); // Set different type from origin
1765 // Stub for getting component
1766 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
1767 .thenReturn(Either.left(service));
1768 when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
1769 .thenReturn(Either.left(originComponent));
1771 final ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
1772 componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1774 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.INVALID_CONTENT);
1778 void testCreateComponentInstanceFailureCannotContainInstance() {
1779 final Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
1780 final ComponentInstance ci = p.getLeft();
1781 final Resource originComponent = p.getRight();
1783 // Stub for getting component
1784 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
1785 .thenReturn(Either.left(service));
1786 when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
1787 .thenReturn(Either.left(originComponent));
1788 // Assume services cannot contain VF resource
1789 when(containerInstanceTypeData.isAllowedForServiceComponent(ResourceTypeEnum.VF, null))
1792 ByActionStatusComponentException actualException = assertThrows(ByActionStatusComponentException.class, () -> {
1793 componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1795 assertThat(actualException.getActionStatus()).isEqualTo(ActionStatus.CONTAINER_CANNOT_CONTAIN_INSTANCE);
1796 verify(containerInstanceTypeData, times(1)).isAllowedForServiceComponent(ResourceTypeEnum.VF, null);
1799 final Resource resource = createResource();
1800 resource.setResourceType(ResourceTypeEnum.VF);
1801 resource.setLastUpdaterUserId(USER_ID);
1803 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
1804 .thenReturn(Either.left(resource));
1805 when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
1806 .thenReturn(Either.left(originComponent));
1807 when(containerInstanceTypeData.isAllowedForResourceComponent(ResourceTypeEnum.VF, ResourceTypeEnum.VF))
1809 actualException = assertThrows(ByActionStatusComponentException.class, () -> {
1810 componentInstanceBusinessLogic.createComponentInstance(RESOURCE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1813 assertThat(actualException.getActionStatus()).isEqualTo(ActionStatus.CONTAINER_CANNOT_CONTAIN_INSTANCE);
1817 void testCreateComponentInstanceFailureAddToGraph() {
1818 final Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
1819 final ComponentInstance ci = p.getLeft();
1820 final Resource originComponent = p.getRight();
1822 // TODO Refactor createComponentInstance() method and reduce these mocks
1823 // not to target the internal details too much
1824 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
1825 .thenReturn(Either.left(service));
1826 when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
1827 .thenReturn(Either.left(originComponent));
1828 when(containerInstanceTypeData.isAllowedForServiceComponent(ResourceTypeEnum.VF, null))
1830 Mockito.doNothing().when(compositionBusinessLogic).validateAndSetDefaultCoordinates(ci);
1831 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service))
1832 .thenReturn(StorageOperationStatus.OK);
1833 when(toscaOperationFacade.addComponentInstanceToTopologyTemplate(service, originComponent, ci, false, user))
1834 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
1835 when(componentsUtils.convertFromStorageResponseForResourceInstance(StorageOperationStatus.BAD_REQUEST, true))
1836 .thenReturn(ActionStatus.INVALID_CONTENT);
1837 when(componentsUtils.getResponseFormatForResourceInstance(ActionStatus.INVALID_CONTENT, "", null))
1838 .thenReturn(new ResponseFormat());
1839 when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
1840 when(graphLockOperation.unlockComponent(COMPONENT_ID, NodeTypeEnum.Service))
1841 .thenReturn(StorageOperationStatus.OK);
1843 assertThrows(ByResponseFormatComponentException.class, () -> {
1844 componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
1846 verify(containerInstanceTypeData, times(1))
1847 .isAllowedForServiceComponent(ResourceTypeEnum.VF, null);
1848 verify(compositionBusinessLogic, times(1)).validateAndSetDefaultCoordinates(ci);
1849 verify(toscaOperationFacade, times(1))
1850 .addComponentInstanceToTopologyTemplate(service, originComponent, ci, false, user);
1851 verify(graphLockOperation, times(1)).unlockComponent(COMPONENT_ID, NodeTypeEnum.Service);
1855 void testCreateComponentInstanceSuccess() {
1856 final Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
1857 final ComponentInstance instanceToBeCreated = p.getLeft();
1858 final Resource originComponent = p.getRight();
1860 final Service updatedService = new Service();
1861 updatedService.setComponentInstances(Collections.singletonList(instanceToBeCreated));
1862 updatedService.setUniqueId(service.getUniqueId());
1864 // TODO Refactor createComponentInstance() method and reduce these mocks
1865 // not to target the internal details too much
1866 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
1867 .thenReturn(Either.left(service));
1868 when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
1869 .thenReturn(Either.left(originComponent));
1870 when(containerInstanceTypeData.isAllowedForServiceComponent(ResourceTypeEnum.VF, null))
1872 Mockito.doNothing().when(compositionBusinessLogic).validateAndSetDefaultCoordinates(instanceToBeCreated);
1873 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service))
1874 .thenReturn(StorageOperationStatus.OK);
1875 when(toscaOperationFacade.addComponentInstanceToTopologyTemplate(service, originComponent, instanceToBeCreated, false, user))
1876 .thenReturn(Either.left(new ImmutablePair<>(updatedService, COMPONENT_INSTANCE_ID)));
1877 when(artifactsBusinessLogic.getArtifacts(
1878 ORIGIN_COMPONENT_ID, NodeTypeEnum.Resource, ArtifactGroupTypeEnum.DEPLOYMENT, null))
1879 .thenReturn(Either.left(new HashMap<>()));
1880 when(toscaOperationFacade
1881 .addInformationalArtifactsToInstance(service.getUniqueId(), instanceToBeCreated, originComponent.getArtifacts()))
1882 .thenReturn(StorageOperationStatus.OK);
1883 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
1884 when(graphLockOperation.unlockComponent(COMPONENT_ID, NodeTypeEnum.Service))
1885 .thenReturn(StorageOperationStatus.OK);
1887 final ComponentInstance result = componentInstanceBusinessLogic.createComponentInstance(
1888 ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, instanceToBeCreated);
1889 assertThat(result).isEqualTo(instanceToBeCreated);
1890 assertThat(instanceToBeCreated.getComponentVersion()).isEqualTo(originComponent.getVersion());
1891 assertThat(instanceToBeCreated.getIcon()).isEqualTo(originComponent.getIcon());
1892 verify(containerInstanceTypeData, times(1))
1893 .isAllowedForServiceComponent(ResourceTypeEnum.VF, null);
1894 verify(compositionBusinessLogic, times(1)).validateAndSetDefaultCoordinates(instanceToBeCreated);
1895 verify(toscaOperationFacade, times(1))
1896 .addComponentInstanceToTopologyTemplate(service, originComponent, instanceToBeCreated, false, user);
1897 // Check graph db change was committed
1898 verify(janusGraphDao, times(1)).commit();
1902 void testCreateComponentInstanceServiceSubstitutionSuccess() {
1903 ComponentInstance instanceToBeCreated = createServiceSubstitutionComponentInstance();
1904 Service originService = createServiceSubstitutionOriginService();
1905 Component serviceBaseComponent = createServiceSubstitutionServiceDerivedFromComponent();
1907 Service updatedService = new Service();
1908 updatedService.setComponentInstances(Collections.singletonList(instanceToBeCreated));
1909 updatedService.setUniqueId(service.getUniqueId());
1911 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
1912 .thenReturn(Either.left(service));
1913 when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
1914 .thenReturn(Either.left(originService));
1915 when(toscaOperationFacade.getLatestByToscaResourceName(eq(originService.getDerivedFromGenericType()), isNull()))
1916 .thenReturn(Either.left(serviceBaseComponent));
1917 when(toscaOperationFacade.getToscaElement(eq(ORIGIN_COMPONENT_ID), any(ComponentParametersView.class)))
1918 .thenReturn(Either.left(originService));
1919 Mockito.doNothing().when(compositionBusinessLogic).validateAndSetDefaultCoordinates(instanceToBeCreated);
1920 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service))
1921 .thenReturn(StorageOperationStatus.OK);
1922 when(toscaOperationFacade.addComponentInstanceToTopologyTemplate(service, serviceBaseComponent, instanceToBeCreated, false, user))
1923 .thenReturn(Either.left(new ImmutablePair<>(updatedService, COMPONENT_INSTANCE_ID)));
1924 when(artifactsBusinessLogic.getArtifacts(
1925 "baseComponentId", NodeTypeEnum.Resource, ArtifactGroupTypeEnum.DEPLOYMENT, null))
1926 .thenReturn(Either.left(new HashMap<>()));
1927 when(toscaOperationFacade
1928 .addInformationalArtifactsToInstance(service.getUniqueId(), instanceToBeCreated, originService.getArtifacts()))
1929 .thenReturn(StorageOperationStatus.OK);
1930 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
1931 when(graphLockOperation.unlockComponent(COMPONENT_ID, NodeTypeEnum.Service))
1932 .thenReturn(StorageOperationStatus.OK);
1934 ComponentInstance result = componentInstanceBusinessLogic.createComponentInstance(
1935 ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, instanceToBeCreated);
1936 assertThat(result).isEqualTo(instanceToBeCreated);
1937 assertThat(instanceToBeCreated.getComponentVersion()).isEqualTo(originService.getVersion());
1938 assertThat(instanceToBeCreated.getIcon()).isEqualTo(originService.getIcon());
1939 verify(compositionBusinessLogic, times(1)).validateAndSetDefaultCoordinates(instanceToBeCreated);
1940 verify(toscaOperationFacade, times(1))
1941 .addComponentInstanceToTopologyTemplate(service, serviceBaseComponent, instanceToBeCreated, false, user);
1942 // Check graph db change was committed
1943 verify(janusGraphDao, times(1)).commit();
1947 void testGetComponentInstanceAttributesById_success() {
1948 final ComponentInstanceAttribute componentInstanceAttribute = new ComponentInstanceAttribute();
1949 componentInstanceAttribute.setComponentInstanceId(TO_INSTANCE_ID);
1951 final HashMap<String, List<ComponentInstanceAttribute>> map = new HashMap<>();
1952 map.put(TO_INSTANCE_ID, Arrays.asList(componentInstanceAttribute));
1953 resource.setComponentInstancesAttributes(map);
1955 final Either<Component, StorageOperationStatus> leftServiceOp = Either.left(resource);
1956 doReturn(leftServiceOp).when(toscaOperationFacade).getToscaElement(COMPONENT_ID);
1958 final List<ComponentInstanceAttribute> result = componentInstanceBusinessLogic
1959 .getComponentInstanceAttributesById(RESOURCE_PARAM_NAME, COMPONENT_ID, TO_INSTANCE_ID, USER_ID);
1960 assertThat(result).isNotNull().isNotEmpty();
1961 verify(toscaOperationFacade, times(1)).getToscaElement(COMPONENT_ID);
1965 void testGetComponentInstanceAttributesById_fail_missing_ComponentInstancesAttributes() {
1966 final Either<Component, StorageOperationStatus> leftServiceOp = Either.left(resource);
1967 doReturn(leftServiceOp).when(toscaOperationFacade).getToscaElement(COMPONENT_ID);
1969 final List<ComponentInstanceAttribute> result = componentInstanceBusinessLogic
1970 .getComponentInstanceAttributesById(RESOURCE_PARAM_NAME, COMPONENT_ID, TO_INSTANCE_ID, USER_ID);
1971 assertThat(result).isNotNull().isEmpty();
1972 verify(toscaOperationFacade, times(1)).getToscaElement(COMPONENT_ID);
1976 void testGetComponentInstanceAttributesById_fail_getToscaElement() {
1977 final ComponentInstanceAttribute componentInstanceAttribute = new ComponentInstanceAttribute();
1978 componentInstanceAttribute.setComponentInstanceId(TO_INSTANCE_ID);
1980 final HashMap<String, List<ComponentInstanceAttribute>> map = new HashMap<>();
1981 map.put(TO_INSTANCE_ID, Arrays.asList(componentInstanceAttribute));
1982 resource.setComponentInstancesAttributes(map);
1984 final Either<Object, StorageOperationStatus> right = Either.right(StorageOperationStatus.BAD_REQUEST);
1985 doReturn(right).when(toscaOperationFacade).getToscaElement(COMPONENT_ID);
1986 doReturn(ActionStatus.BAD_REQUEST_MISSING_RESOURCE).when(componentsUtils).convertFromStorageResponse(StorageOperationStatus.BAD_REQUEST);
1988 assertThrows(ByActionStatusComponentException.class, () -> {
1989 final List<ComponentInstanceAttribute> result = componentInstanceBusinessLogic
1990 .getComponentInstanceAttributesById(RESOURCE_PARAM_NAME, COMPONENT_ID, TO_INSTANCE_ID, USER_ID);
1997 void testGetComponentInstanceAttributesById_fail_getResourceInstanceById() {
1998 final ComponentInstanceAttribute componentInstanceAttribute = new ComponentInstanceAttribute();
1999 componentInstanceAttribute.setComponentInstanceId(TO_INSTANCE_ID);
2001 final HashMap<String, List<ComponentInstanceAttribute>> map = new HashMap<>();
2002 map.put(TO_INSTANCE_ID, Arrays.asList(componentInstanceAttribute));
2003 resource.setComponentInstancesAttributes(map);
2005 final Either<Component, StorageOperationStatus> leftServiceOp = Either.left(resource);
2006 doReturn(leftServiceOp).when(toscaOperationFacade).getToscaElement(COMPONENT_ID);
2007 doReturn(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE).when(componentsUtils).convertFromStorageResponse(StorageOperationStatus.PARENT_RESOURCE_NOT_FOUND);
2009 assertThrows(ByActionStatusComponentException.class, () -> {
2010 final List<ComponentInstanceAttribute> result = componentInstanceBusinessLogic
2011 .getComponentInstanceAttributesById(RESOURCE_PARAM_NAME, COMPONENT_ID, "", USER_ID);
2018 void updateInstanceCapabilitySuccessTest() {
2019 var containerComponentId = "containerComponentId";
2020 var componentInstanceUniqueId = "componentInstanceUniqueId";
2021 var capabilityDefinition = new CapabilityDefinition();
2022 capabilityDefinition.setUniqueId("uniqueId");
2024 final Component component = new Service();
2025 component.setUniqueId(containerComponentId);
2026 component.setLastUpdaterUserId(USER_ID);
2027 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
2029 var componentInstance = new ComponentInstance();
2030 componentInstance.setUniqueId(componentInstanceUniqueId);
2031 component.setComponentInstances(Collections.singletonList(componentInstance));
2033 when(toscaOperationFacade.getToscaFullElement(containerComponentId))
2034 .thenReturn(Either.left(component));
2035 when(toscaOperationFacade.updateComponentInstanceCapability(containerComponentId, componentInstanceUniqueId, capabilityDefinition))
2036 .thenReturn(capabilityDefinition);
2037 when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(component))
2038 .thenReturn(Either.left(component));
2039 when(graphLockOperation.lockComponent(containerComponentId, NodeTypeEnum.Service))
2040 .thenReturn(StorageOperationStatus.OK);
2042 final Either<CapabilityDefinition, ResponseFormat> resultEither = componentInstanceBusinessLogic
2043 .updateInstanceCapability(ComponentTypeEnum.SERVICE, containerComponentId, componentInstanceUniqueId, capabilityDefinition, USER_ID);
2044 assertTrue(resultEither.isLeft());
2045 final CapabilityDefinition actualCapabilityDefinition = resultEither.left().value();
2046 assertNotEquals(capabilityDefinition, actualCapabilityDefinition);
2047 assertEquals(capabilityDefinition.getUniqueId(), actualCapabilityDefinition.getUniqueId());
2051 void updateInstanceCapabilityNoContainerComponentTypeTest() {
2052 var responseFormat = new ResponseFormat();
2053 when(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED)).thenReturn(responseFormat);
2054 final Either<CapabilityDefinition, ResponseFormat> resultEither = componentInstanceBusinessLogic
2055 .updateInstanceCapability(null, "containerComponentId", "componentInstanceUniqueId", new CapabilityDefinition(), USER_ID);
2056 assertTrue(resultEither.isRight(), "Either return should be right");
2057 final ResponseFormat actualResponseFormat = resultEither.right().value();
2058 assertEquals(responseFormat, actualResponseFormat);
2062 void updateInstanceCapabilityContainerComponentNotFoundTest() {
2063 var containerComponentId = "containerComponentId";
2064 when(toscaOperationFacade.getToscaFullElement(containerComponentId)).thenReturn(Either.right(null));
2065 var responseFormat = new ResponseFormat();
2066 when(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_NOT_FOUND, containerComponentId)).thenReturn(responseFormat);
2067 final Either<CapabilityDefinition, ResponseFormat> resultEither = componentInstanceBusinessLogic
2068 .updateInstanceCapability(ComponentTypeEnum.SERVICE, "containerComponentId", "componentInstanceUniqueId", new CapabilityDefinition(), USER_ID);
2069 assertTrue(resultEither.isRight(), "Either return should be right");
2070 final ResponseFormat actualResponseFormat = resultEither.right().value();
2071 assertEquals(responseFormat, actualResponseFormat);
2075 void updateInstanceCapabilityCannotWorkOnComponentTest() {
2076 var containerComponentId = "containerComponentId";
2077 var componentInstanceUniqueId = "componentInstanceUniqueId";
2079 final Component component = new Service();
2080 component.setUniqueId(containerComponentId);
2081 component.setLastUpdaterUserId("anotherUse");
2082 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
2084 var expectedResponseFormat = new ResponseFormat();
2086 when(toscaOperationFacade.getToscaFullElement(containerComponentId))
2087 .thenReturn(Either.left(component));
2088 when(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION))
2089 .thenReturn(expectedResponseFormat);
2091 final Either<CapabilityDefinition, ResponseFormat> resultEither = componentInstanceBusinessLogic
2092 .updateInstanceCapability(ComponentTypeEnum.SERVICE, containerComponentId, componentInstanceUniqueId, new CapabilityDefinition(), USER_ID);
2093 assertTrue(resultEither.isRight(), "Either return should be right");
2094 final ResponseFormat actualResponseFormat = resultEither.right().value();
2095 assertEquals(expectedResponseFormat, actualResponseFormat);
2099 void updateInstanceCapabilityResourceInstanceNotFoundTest() {
2100 var containerComponentId = "containerComponentId";
2101 var componentInstanceUniqueId = "componentInstanceUniqueId";
2103 final Component component = new Service();
2104 component.setUniqueId(containerComponentId);
2105 component.setLastUpdaterUserId(USER_ID);
2106 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
2108 var expectedResponseFormat = new ResponseFormat();
2110 when(toscaOperationFacade.getToscaFullElement(containerComponentId))
2111 .thenReturn(Either.left(component));
2112 when(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceUniqueId, containerComponentId))
2113 .thenReturn(expectedResponseFormat);
2115 final Either<CapabilityDefinition, ResponseFormat> resultEither = componentInstanceBusinessLogic
2116 .updateInstanceCapability(ComponentTypeEnum.SERVICE, containerComponentId, componentInstanceUniqueId, new CapabilityDefinition(), USER_ID);
2117 assertTrue(resultEither.isRight(), "Either return should be right");
2118 final ResponseFormat actualResponseFormat = resultEither.right().value();
2119 assertEquals(expectedResponseFormat, actualResponseFormat);
2123 void updateInstanceCapabilityUpdateMetadataFailTest() {
2124 var containerComponentId = "containerComponentId";
2125 var componentInstanceUniqueId = "componentInstanceUniqueId";
2126 var capabilityDefinition = new CapabilityDefinition();
2127 capabilityDefinition.setUniqueId("uniqueId");
2129 final Component component = new Service();
2130 component.setUniqueId(containerComponentId);
2131 component.setLastUpdaterUserId(USER_ID);
2132 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
2134 var componentInstance = new ComponentInstance();
2135 componentInstance.setUniqueId(componentInstanceUniqueId);
2136 component.setComponentInstances(Collections.singletonList(componentInstance));
2138 var expectedResponseFormat = new ResponseFormat();
2140 when(toscaOperationFacade.getToscaFullElement(containerComponentId))
2141 .thenReturn(Either.left(component));
2142 when(graphLockOperation.lockComponent(containerComponentId, NodeTypeEnum.Service))
2143 .thenReturn(StorageOperationStatus.OK);
2144 when(toscaOperationFacade.updateComponentInstanceCapability(containerComponentId, componentInstanceUniqueId, capabilityDefinition))
2145 .thenReturn(capabilityDefinition);
2146 when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(component))
2147 .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
2148 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR, ComponentTypeEnum.SERVICE))
2149 .thenReturn(ActionStatus.GENERAL_ERROR);
2150 when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR))
2151 .thenReturn(expectedResponseFormat);
2153 final Either<CapabilityDefinition, ResponseFormat> resultEither = componentInstanceBusinessLogic
2154 .updateInstanceCapability(ComponentTypeEnum.SERVICE, containerComponentId, componentInstanceUniqueId, capabilityDefinition, USER_ID);
2155 assertTrue(resultEither.isRight(), "Either return should be right");
2156 final ResponseFormat actualResponseFormat = resultEither.right().value();
2157 assertEquals(expectedResponseFormat, actualResponseFormat);
2161 void updateInstanceCapabilityBusinessExceptionHandlingTest() {
2162 var containerComponentId = "containerComponentId";
2163 var componentInstanceUniqueId = "componentInstanceUniqueId";
2164 var capabilityDefinition = new CapabilityDefinition();
2165 capabilityDefinition.setUniqueId("uniqueId");
2167 final Component component = new Service();
2168 component.setUniqueId(containerComponentId);
2169 component.setLastUpdaterUserId(USER_ID);
2170 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
2172 var componentInstance = new ComponentInstance();
2173 componentInstance.setUniqueId(componentInstanceUniqueId);
2174 component.setComponentInstances(Collections.singletonList(componentInstance));
2177 when(toscaOperationFacade.getToscaFullElement(containerComponentId))
2178 .thenReturn(Either.left(component));
2179 when(graphLockOperation.lockComponent(containerComponentId, NodeTypeEnum.Service))
2180 .thenReturn(StorageOperationStatus.OK);
2181 when(toscaOperationFacade.updateComponentInstanceCapability(containerComponentId, componentInstanceUniqueId, capabilityDefinition))
2182 .thenThrow(new OperationException(ActionStatus.GENERAL_ERROR));
2184 final BusinessException businessException = assertThrows(BusinessException.class, () -> {
2185 componentInstanceBusinessLogic
2186 .updateInstanceCapability(ComponentTypeEnum.SERVICE, containerComponentId, componentInstanceUniqueId, capabilityDefinition, USER_ID);
2188 assertTrue(businessException instanceof OperationException);
2189 assertEquals(ActionStatus.GENERAL_ERROR, ((OperationException) businessException).getActionStatus());
2193 void updateInstanceCapabilityUnknownExceptionHandlingTest() {
2194 var containerComponentId = "containerComponentId";
2195 var componentInstanceUniqueId = "componentInstanceUniqueId";
2196 var capabilityDefinition = new CapabilityDefinition();
2197 capabilityDefinition.setUniqueId("uniqueId");
2199 final Component component = new Service();
2200 component.setUniqueId(containerComponentId);
2201 component.setLastUpdaterUserId(USER_ID);
2202 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
2204 var componentInstance = new ComponentInstance();
2205 componentInstance.setUniqueId(componentInstanceUniqueId);
2206 component.setComponentInstances(Collections.singletonList(componentInstance));
2208 var expectedResponseFormat = new ResponseFormat();
2210 when(toscaOperationFacade.getToscaFullElement(containerComponentId))
2211 .thenReturn(Either.left(component));
2212 when(graphLockOperation.lockComponent(containerComponentId, NodeTypeEnum.Service))
2213 .thenReturn(StorageOperationStatus.OK);
2214 when(toscaOperationFacade.updateComponentInstanceCapability(containerComponentId, componentInstanceUniqueId, capabilityDefinition))
2215 .thenThrow(new RuntimeException());
2216 when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR))
2217 .thenReturn(expectedResponseFormat);
2219 final Exception exception = assertThrows(BusinessException.class, () ->
2220 componentInstanceBusinessLogic
2221 .updateInstanceCapability(ComponentTypeEnum.SERVICE, containerComponentId, componentInstanceUniqueId, capabilityDefinition, USER_ID));
2222 assertTrue(exception instanceof ByResponseFormatComponentException);
2223 final ByResponseFormatComponentException actualException = (ByResponseFormatComponentException) exception;
2224 assertEquals(expectedResponseFormat, actualException.getResponseFormat());
2227 private ComponentInstance createServiceSubstitutionComponentInstance() {
2228 final ComponentInstance instanceToBeCreated = new ComponentInstance();
2229 instanceToBeCreated.setName(COMPONENT_INSTANCE_NAME);
2230 instanceToBeCreated.setUniqueId(COMPONENT_INSTANCE_ID);
2231 instanceToBeCreated.setComponentUid(ORIGIN_COMPONENT_ID);
2232 instanceToBeCreated.setOriginType(OriginTypeEnum.ServiceSubstitution);
2234 return instanceToBeCreated;
2237 private Service createServiceSubstitutionOriginService() {
2238 final Service originComponent = new Service();
2239 originComponent.setLifecycleState(LifecycleStateEnum.CERTIFIED);
2240 originComponent.setVersion(ORIGIN_COMPONENT_VERSION);
2241 originComponent.setIcon(ICON_NAME);
2242 originComponent.setDerivedFromGenericType("org.openecomp.resource.abstract.nodes.service");
2243 originComponent.setName("myService");
2244 return originComponent;
2247 private Component createServiceSubstitutionServiceDerivedFromComponent() {
2248 final Resource component = new Resource();
2249 component.setLifecycleState(LifecycleStateEnum.CERTIFIED);
2250 component.setVersion(ORIGIN_COMPONENT_VERSION);
2251 component.setIcon(ICON_NAME);
2252 component.setToscaResourceName("org.openecomp.resource.abstract.nodes.service");
2253 component.setUniqueId("baseComponentId");