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.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertSame;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.ArgumentMatchers.anySet;
31 import static org.mockito.ArgumentMatchers.eq;
32 import static org.mockito.Mockito.when;
34 import fj.data.Either;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.HashSet;
40 import java.util.List;
42 import java.util.Optional;
44 import mockit.Deencapsulation;
45 import org.apache.commons.lang3.tuple.ImmutablePair;
46 import org.assertj.core.util.Lists;
47 import org.junit.Assert;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.mockito.InjectMocks;
52 import org.mockito.Mock;
53 import org.mockito.Mockito;
54 import org.mockito.MockitoAnnotations;
55 import org.mockito.junit.MockitoJUnitRunner;
56 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
57 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
58 import org.openecomp.sdc.be.components.validation.UserValidations;
59 import org.openecomp.sdc.be.config.ConfigurationManager;
60 import org.openecomp.sdc.be.dao.api.ActionStatus;
61 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
62 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
63 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
64 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
65 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
66 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathElementDataDefinition;
67 import org.openecomp.sdc.be.datatypes.elements.GetPolicyValueDataDefinition;
68 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
69 import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition;
70 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
71 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
72 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
73 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
74 import org.openecomp.sdc.be.impl.ComponentsUtils;
75 import org.openecomp.sdc.be.impl.ServletUtils;
76 import org.openecomp.sdc.be.model.ArtifactDefinition;
77 import org.openecomp.sdc.be.model.CapabilityDefinition;
78 import org.openecomp.sdc.be.model.CapabilityRequirementRelationship;
79 import org.openecomp.sdc.be.model.Component;
80 import org.openecomp.sdc.be.model.ComponentInstance;
81 import org.openecomp.sdc.be.model.ComponentInstanceInput;
82 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
83 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
84 import org.openecomp.sdc.be.model.ComponentParametersView;
85 import org.openecomp.sdc.be.model.DataTypeDefinition;
86 import org.openecomp.sdc.be.model.InputDefinition;
87 import org.openecomp.sdc.be.model.LifecycleStateEnum;
88 import org.openecomp.sdc.be.model.PolicyDefinition;
89 import org.openecomp.sdc.be.model.RelationshipImpl;
90 import org.openecomp.sdc.be.model.RelationshipInfo;
91 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
92 import org.openecomp.sdc.be.model.RequirementDefinition;
93 import org.openecomp.sdc.be.model.Resource;
94 import org.openecomp.sdc.be.model.Service;
95 import org.openecomp.sdc.be.model.User;
96 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
97 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ForwardingPathOperation;
98 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
99 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
100 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
101 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
102 import org.openecomp.sdc.be.user.UserBusinessLogic;
103 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
104 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
105 import org.openecomp.sdc.common.api.ConfigurationSource;
106 import org.openecomp.sdc.common.impl.ExternalConfiguration;
107 import org.openecomp.sdc.common.impl.FSConfigurationSource;
108 import org.openecomp.sdc.exception.ResponseFormat;
111 * The test suite designed for test functionality of ComponentInstanceBusinessLogic class
114 @RunWith(MockitoJUnitRunner.class)
115 public class ComponentInstanceBusinessLogicTest {
117 private final static String USER_ID = "jh0003";
118 private final static String COMPONENT_ID = "componentId";
119 private final static String ORIGIN_COMPONENT_ID = "originComponentId";
120 private final static String COMPONENT_INST_ID = "componentInstId";
121 private final static String TO_INSTANCE_ID = "toInstanceId";
122 private final static String TO_INSTANCE_NAME = "toInstanceName";
123 private final static String COMPONENT_INSTANCE_ID = "componentInstanceId";
124 private final static String FROM_INSTANCE_ID = "fromInstanceId";
125 private final static String RELATION_ID = "relationId";
126 private final static String CAPABILITY_OWNER_ID = "capabilityOwnerId";
127 private final static String CAPABILITY_UID = "capabilityUid";
128 private final static String CAPABILITY_NAME = "capabilityName";
129 private final static String REQUIREMENT_OWNER_ID = "requirementOwnerId";
130 private final static String REQUIREMENT_UID = "requirementUid";
131 private final static String REQUIREMENT_NAME = "requirementName";
132 private final static String RELATIONSHIP_TYPE = "relationshipType";
133 private final static String ARTIFACT_1 = "cloudtech_k8s_charts.zip";
134 private final static String ARTIFACT_2 = "cloudtech_azure_day0.zip";
135 private final static String ARTIFACT_3 = "cloudtech_aws_configtemplate.zip";
136 private final static String ARTIFACT_4 = "k8s_charts.zip";
137 private final static String ARTIFACT_5 = "cloudtech_openstack_configtemplate.zip";
138 private final static String PROP_NAME = "propName";
139 private final static String NON_EXIST_NAME = "nonExistName";
141 static ConfigurationSource configurationSource = new FSConfigurationSource(
142 ExternalConfiguration.getChangeListener(),
143 "src/test/resources/config/catalog-be");
144 static ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
147 private static ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
149 private ComponentInstancePropInput componentInstancePropInput;
151 ArtifactsBusinessLogic artifactsBusinessLogic;
153 private ComponentsUtils componentsUtils;
155 private ServletUtils servletUtils;
157 private ResponseFormat responseFormat;
159 private ToscaOperationFacade toscaOperationFacade;
161 private UserBusinessLogic userAdmin;
163 private ForwardingPathOperation forwardingPathOperation;
167 private UserValidations userValidations;
169 GraphLockOperation graphLockOperation;
171 private JanusGraphDao janusGraphDao;
173 ApplicationDataTypeCache dataTypeCache;
175 PropertyOperation propertyOperation;
177 ApplicationDataTypeCache applicationDataTypeCache;
179 private Component service;
180 private Component resource;
181 private ComponentInstance toInstance;
182 private ComponentInstance fromInstance;
183 private CapabilityDataDefinition capability;
184 private RequirementDataDefinition requirement;
185 private RequirementCapabilityRelDef relation;
189 MockitoAnnotations.initMocks(componentInstanceBusinessLogic);
195 public void testGetRelationByIdSuccess() {
196 getServiceRelationByIdSuccess(service);
197 getServiceRelationByIdSuccess(resource);
201 public void testGetRelationByIdUserValidationFailure() {
202 getServiceRelationByIdUserValidationFailure(service);
203 getServiceRelationByIdUserValidationFailure(resource);
207 public void testGetRelationByIdComponentNotFoundFailure() {
208 getRelationByIdComponentNotFoundFailure(service);
209 getRelationByIdComponentNotFoundFailure(resource);
213 public void testForwardingPathOnVersionChange() {
214 getforwardingPathOnVersionChange();
218 public void testIsCloudSpecificArtifact() {
219 assertTrue(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_1));
220 assertTrue(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_2));
221 assertTrue(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_3));
222 assertFalse(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_4));
223 assertFalse(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_5));
226 private void getforwardingPathOnVersionChange() {
227 String containerComponentParam = "services";
228 String containerComponentID = "121-cont";
229 String componentInstanceID = "121-cont-1-comp";
230 Service component = new Service();
231 Map<String, ForwardingPathDataDefinition> forwardingPaths = generateForwardingPath(componentInstanceID);
233 //Add existing componentInstance to component
234 List<ComponentInstance> componentInstanceList = new ArrayList<>();
235 ComponentInstance oldComponentInstance = new ComponentInstance();
236 oldComponentInstance.setName("OLD_COMP_INSTANCE");
237 oldComponentInstance.setUniqueId(componentInstanceID);
238 oldComponentInstance.setName(componentInstanceID);
239 oldComponentInstance.setToscaPresentationValue(JsonPresentationFields.CI_COMPONENT_UID, "1-comp");
240 componentInstanceList.add(oldComponentInstance);
241 component.setComponentInstances(componentInstanceList);
242 component.setForwardingPaths(forwardingPaths);
244 List<ComponentInstance> componentInstanceListNew = new ArrayList<>();
245 ComponentInstance newComponentInstance = new ComponentInstance();
246 String new_Comp_UID = "2-comp";
247 newComponentInstance.setToscaPresentationValue(JsonPresentationFields.CI_COMPONENT_UID, new_Comp_UID);
248 newComponentInstance.setUniqueId(new_Comp_UID);
249 componentInstanceListNew.add(newComponentInstance);
250 Component component2 = new Service();
251 component2.setComponentInstances(componentInstanceListNew);
253 //Mock for getting component
254 when(toscaOperationFacade.getToscaElement(eq(containerComponentID), any(ComponentParametersView.class)))
255 .thenReturn(Either.left(component));
256 when(toscaOperationFacade.validateComponentExists(any(String.class))).thenReturn(Either.left(Boolean.TRUE));
257 when(toscaOperationFacade.getToscaFullElement(eq(new_Comp_UID))).thenReturn(Either.left(component2));
259 Either<Set<String>, ResponseFormat> resultOp = componentInstanceBusinessLogic
260 .forwardingPathOnVersionChange(containerComponentParam,
261 containerComponentID, componentInstanceID,
262 newComponentInstance);
263 assertEquals(1, resultOp.left().value().size());
264 assertEquals("FP-ID-1", resultOp.left().value().iterator().next());
269 public void testCreateOrUpdatePropertiesValues2() {
270 String containerComponentID = "containerId";
271 String resourceInstanceId = "resourceId";
272 String componentInstanceID = "componentInstance";
273 List<ComponentInstanceProperty> properties = new ArrayList<>();
274 ComponentInstanceProperty property = new ComponentInstanceProperty();
275 property.setName("property");
276 property.setValue("newVal");
277 property.setType("string");
278 properties.add(property);
280 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
281 ComponentInstanceProperty origProperty = new ComponentInstanceProperty();
282 origProperty.setName("property");
283 origProperty.setValue("value");
284 origProperty.setType("string");
285 origProperties.add(origProperty);
287 Map<String, DataTypeDefinition> types = new HashMap<>();
288 DataTypeDefinition dataTypeDef = new DataTypeDefinition();
289 types.put("string", dataTypeDef);
291 Component component = new Service();
292 component.setLastUpdaterUserId("userId");
293 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
294 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
295 componentInstanceProps.put("resourceId", origProperties);
296 component.setComponentInstancesProperties(componentInstanceProps);
297 ComponentInstance ci = createComponentInstance("ci1");
298 ci.setUniqueId("resourceId");
299 component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
300 createComponentInstance(componentInstanceID)));
301 HashMap<String, DataTypeDefinition> dataTypeDefinitionHashMap = new HashMap<>();
302 DataTypeDefinition dataTypeDefinition = new DataTypeDefinition();
303 dataTypeDefinition.setName("string");
304 dataTypeDefinitionHashMap.put("string", dataTypeDefinition);
306 //when(userValidations.validateUserExists(user.getUserId(), false)).thenReturn(user);
307 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
308 .thenReturn(Either.left(component));
309 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
310 .thenReturn(StorageOperationStatus.OK);
311 when(dataTypeCache.getAll()).thenReturn(Either.left(types));
312 when(propertyOperation.validateAndUpdatePropertyValue(property.getType(), "newVal", true, null, types))
313 .thenReturn(Either.left("newVal"));
314 when(propertyOperation.validateAndUpdateRules("string", property.getRules(),
315 null, types, true)).thenReturn(ImmutablePair.of("string", null));
316 when(toscaOperationFacade.updateComponentInstanceProperty(component, ci.getUniqueId(),
317 origProperty)).thenReturn(StorageOperationStatus.OK);
318 origProperties.get(0).setValue("newVal");
319 when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(component))
320 .thenReturn(Either.left(component));
321 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
322 when(graphLockOperation.unlockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
323 .thenReturn(StorageOperationStatus.OK);
325 Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = componentInstanceBusinessLogic
326 .createOrUpdatePropertiesValues(
327 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
328 assertThat(responseFormatEither.left().value()).isEqualTo(properties);
332 public void testCreateOrUpdatePropertiesValuesPropertyNotExists() {
333 String containerComponentID = "containerId";
334 String resourceInstanceId = "resourceId";
335 String componentInstanceID = "componentInstance";
336 List<ComponentInstanceProperty> properties = new ArrayList<>();
337 ComponentInstanceProperty property = new ComponentInstanceProperty();
338 property.setName("property");
339 property.setValue("newVal");
340 property.setType("string");
341 properties.add(property);
343 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
345 Map<String, DataTypeDefinition> types = new HashMap<>();
346 DataTypeDefinition dataTypeDef = new DataTypeDefinition();
347 types.put("string", dataTypeDef);
349 Component component = new Service();
350 component.setLastUpdaterUserId("userId");
351 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
352 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
353 componentInstanceProps.put("resourceId", origProperties);
354 component.setComponentInstancesProperties(componentInstanceProps);
355 ComponentInstance ci = createComponentInstance("ci1");
356 ci.setUniqueId("resourceId");
357 component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
358 createComponentInstance(componentInstanceID)));
359 HashMap<String, DataTypeDefinition> dataTypeDefinitionHashMap = new HashMap<>();
360 DataTypeDefinition dataTypeDefinition = new DataTypeDefinition();
361 dataTypeDefinition.setName("string");
362 dataTypeDefinitionHashMap.put("string", dataTypeDefinition);
364 //when(userValidations.validateUserExists(user.getUserId(), false)).thenReturn(user);
365 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
366 .thenReturn(Either.left(component));
367 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
368 .thenReturn(StorageOperationStatus.OK);
369 //when(dataTypeCache.getAll()).thenReturn(Either.left(types));
370 //when (janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
371 when(graphLockOperation.unlockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
372 .thenReturn(StorageOperationStatus.OK);
375 componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
376 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
377 } catch (ComponentException e) {
378 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.PROPERTY_NOT_FOUND);
384 public void testCreateOrUpdatePropertiesValuesValidationFailure() {
385 String containerComponentID = "containerId";
386 String resourceInstanceId = "resourceId";
387 String componentInstanceID = "componentInstance";
388 List<ComponentInstanceProperty> properties = new ArrayList<>();
389 ComponentInstanceProperty property = new ComponentInstanceProperty();
390 property.setName("property");
391 property.setValue("newVal");
392 property.setType("string");
393 properties.add(property);
395 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
396 ComponentInstanceProperty origProperty = new ComponentInstanceProperty();
397 origProperty.setName("property");
398 origProperty.setValue("value");
399 origProperty.setType("string");
400 origProperties.add(origProperty);
402 Map<String, DataTypeDefinition> types = new HashMap<>();
403 DataTypeDefinition dataTypeDef = new DataTypeDefinition();
404 types.put("string", dataTypeDef);
406 Component component = new Service();
407 component.setLastUpdaterUserId("userId");
408 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
409 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
410 componentInstanceProps.put("resourceId", origProperties);
411 component.setComponentInstancesProperties(componentInstanceProps);
412 ComponentInstance ci = createComponentInstance("ci1");
413 ci.setUniqueId("resourceId");
414 component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
415 createComponentInstance(componentInstanceID)));
416 HashMap<String, DataTypeDefinition> dataTypeDefinitionHashMap = new HashMap<>();
417 DataTypeDefinition dataTypeDefinition = new DataTypeDefinition();
418 dataTypeDefinition.setName("string");
419 dataTypeDefinitionHashMap.put("string", dataTypeDefinition);
421 //when(userValidations.validateUserExists(user.getUserId(), false)).thenReturn(user);
422 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
423 .thenReturn(Either.left(component));
424 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
425 .thenReturn(StorageOperationStatus.OK);
426 when(dataTypeCache.getAll()).thenReturn(Either.left(types));
427 when(propertyOperation.validateAndUpdatePropertyValue(property.getType(), "newVal", true, null, types))
428 .thenReturn(Either.right(false));
429 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.BAD_REQUEST))
430 .thenReturn(ActionStatus.INVALID_CONTENT);
433 componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
434 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
435 } catch (ComponentException e) {
436 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.INVALID_CONTENT);
443 public void testCreateOrUpdatePropertiesValuesMissingFieldFailure() {
444 String containerComponentID = "containerId";
445 String resourceInstanceId = "resourceId";
446 String componentInstanceID = "componentInstance";
447 List<ComponentInstanceProperty> properties = new ArrayList<>();
448 ComponentInstanceProperty property = new ComponentInstanceProperty();
449 property.setValue("newVal");
450 property.setType("string");
451 properties.add(property);
453 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
454 ComponentInstanceProperty origProperty = new ComponentInstanceProperty();
455 origProperty.setName("property");
456 origProperty.setValue("value");
457 origProperty.setType("string");
458 origProperties.add(origProperty);
460 Component component = new Service();
461 component.setLastUpdaterUserId("userId");
462 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
463 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
464 componentInstanceProps.put("resourceId", origProperties);
465 component.setComponentInstancesProperties(componentInstanceProps);
466 ComponentInstance ci = createComponentInstance("ci1");
467 ci.setUniqueId("resourceId");
468 component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
469 createComponentInstance(componentInstanceID)));
471 HashMap<String, DataTypeDefinition> dataTypeDefinitionHashMap = new HashMap<>();
472 DataTypeDefinition dataTypeDefinition = new DataTypeDefinition();
473 dataTypeDefinition.setName("string");
474 dataTypeDefinitionHashMap.put("string", dataTypeDefinition);
476 //when(userValidations.validateUserExists(user.getUserId(), false)).thenReturn(user);
477 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
478 .thenReturn(Either.left(component));
479 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
480 .thenReturn(StorageOperationStatus.OK);
483 componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
484 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
485 } catch (ComponentException e) {
486 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.MISSING_PROPERTY_NAME);
491 public void testDeleteForwardingPathsWhenComponentinstanceDeleted() {
493 ComponentTypeEnum containerComponentType = ComponentTypeEnum.findByParamName("services");
494 String containerComponentID = "Service-comp";
495 String componentInstanceID = "NodeA1";
496 Service component = new Service();
498 .setComponentInstances(Arrays.asList(createComponentInstance("NodeA2"), createComponentInstance("NodeB2"),
499 createComponentInstance(componentInstanceID)));
501 component.addForwardingPath(createPath("path1", componentInstanceID, "NodeB1", "1"));
502 component.addForwardingPath(createPath("Path2", "NodeA2", "NodeB2", "2"));
503 when(toscaOperationFacade.getToscaElement(eq(containerComponentID), any(ComponentParametersView.class)))
504 .thenReturn(Either.left(component));
505 when(toscaOperationFacade.getToscaElement(eq(containerComponentID))).thenReturn(Either.left(component));
506 when(forwardingPathOperation.deleteForwardingPath(any(Service.class), anySet()))
507 .thenReturn(Either.left(new HashSet<>()));
508 final ComponentInstance ci = new ComponentInstance();
509 ci.setName(componentInstanceID);
510 ComponentInstance responseFormatEither = componentInstanceBusinessLogic
511 .deleteForwardingPathsRelatedTobeDeletedComponentInstance(
512 containerComponentID, containerComponentType, ci);
513 assertThat(!responseFormatEither.isEmpty()).isEqualTo(true);
518 public void testAddComponentInstanceDeploymentArtifacts() {
520 Component containerComponent = new Service();
521 ComponentInstance componentInstance = new ComponentInstance();
522 componentInstance.setUniqueId(COMPONENT_INSTANCE_ID);
523 Component originComponent = fillOriginComponent(new Resource());
525 Map<String, ArtifactDefinition> artifacts = new HashMap<>();
526 ArtifactDefinition deploymentArtifact1 = getArtifact("deploymentArtifact1", ArtifactTypeEnum.HEAT.getType());
527 artifacts.put(deploymentArtifact1.getArtifactLabel(), deploymentArtifact1);
528 ArtifactDefinition deploymentArtifact2 = getArtifact("deploymentArtifact2",
529 ArtifactTypeEnum.HEAT_ENV.getType());
530 artifacts.put(deploymentArtifact2.getArtifactLabel(), deploymentArtifact2);
531 ArtifactDefinition deploymentArtifact3 = getArtifact("deploymentArtifact3",
532 ArtifactTypeEnum.HEAT_VOL.getType());
533 artifacts.put(deploymentArtifact3.getArtifactLabel(), deploymentArtifact3);
534 ArtifactDefinition heatEnvPlaceHolder = getArtifact("deploymentArtifact4", ArtifactTypeEnum.HEAT_ENV.getType());
535 ArtifactDefinition heatEnvPlaceHolder2 = getArtifact("deploymentArtifact5",
536 ArtifactTypeEnum.HEAT_ENV.getType());
538 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getResourceDeploymentArtifacts = Either
541 Map<String, ArtifactDefinition> finalDeploymentArtifacts = new HashMap<>();
542 finalDeploymentArtifacts.put(deploymentArtifact1.getArtifactLabel(), deploymentArtifact1);
543 finalDeploymentArtifacts.put(deploymentArtifact3.getArtifactLabel(), deploymentArtifact3);
544 finalDeploymentArtifacts.put(heatEnvPlaceHolder.getArtifactLabel(), heatEnvPlaceHolder);
545 finalDeploymentArtifacts.put(heatEnvPlaceHolder2.getArtifactLabel(), heatEnvPlaceHolder2);
547 when(artifactsBusinessLogic.getArtifacts(componentInstance.getComponentUid(), NodeTypeEnum.Resource,
548 ArtifactGroupTypeEnum.DEPLOYMENT, null)).thenReturn(getResourceDeploymentArtifacts);
549 when(artifactsBusinessLogic.createHeatEnvPlaceHolder(new ArrayList<>(),
550 deploymentArtifact1, ArtifactsBusinessLogic.HEAT_ENV_NAME, componentInstance.getUniqueId(),
551 NodeTypeEnum.ResourceInstance, componentInstance.getName(), user, containerComponent,
552 null)).thenReturn(heatEnvPlaceHolder);
553 when(artifactsBusinessLogic.createHeatEnvPlaceHolder(new ArrayList<>(),
554 deploymentArtifact3, ArtifactsBusinessLogic.HEAT_ENV_NAME, componentInstance.getUniqueId(),
555 NodeTypeEnum.ResourceInstance, componentInstance.getName(), user, containerComponent,
556 null)).thenReturn(heatEnvPlaceHolder2);
558 when(toscaOperationFacade.addDeploymentArtifactsToInstance(containerComponent.getUniqueId(), componentInstance,
559 finalDeploymentArtifacts)).thenReturn(StorageOperationStatus.OK);
560 when(toscaOperationFacade
561 .addGroupInstancesToComponentInstance(containerComponent, componentInstance, null, new HashMap<>()))
562 .thenReturn(StorageOperationStatus.OK);
563 when(toscaOperationFacade
564 .addInformationalArtifactsToInstance(containerComponent.getUniqueId(), componentInstance, null))
565 .thenReturn(StorageOperationStatus.OK);
567 ActionStatus status = componentInstanceBusinessLogic.addComponentInstanceArtifacts(containerComponent,
568 componentInstance, originComponent, user, null);
570 assertThat(status).isEqualTo(ActionStatus.OK);
574 private Component fillOriginComponent(Resource originComponent) {
575 originComponent.setUniqueId("resourceId");
576 originComponent.setUniqueId(ORIGIN_COMPONENT_ID);
577 originComponent.setComponentInstances(Lists.newArrayList(toInstance, fromInstance));
578 originComponent.setComponentType(ComponentTypeEnum.RESOURCE);
579 originComponent.setState(LifecycleStateEnum.CERTIFIED);
580 return originComponent;
583 private ArtifactDefinition getArtifact(String artifactLabel, String artifactType) {
584 ArtifactDefinition artifactDefinition = new ArtifactDefinition();
585 artifactDefinition.setArtifactLabel(artifactLabel);
586 artifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
587 artifactDefinition.setEsId("esId" + artifactLabel);
588 artifactDefinition.setArtifactType(artifactType);
589 return artifactDefinition;
592 private ComponentInstance createComponentInstance(String path1) {
593 ComponentInstance componentInstance = new ComponentInstance();
594 componentInstance.setName(path1);
595 return componentInstance;
598 private ForwardingPathDataDefinition createPath(String pathName, String fromNode, String toNode, String uniqueId) {
599 ForwardingPathDataDefinition forwardingPath = new ForwardingPathDataDefinition(pathName);
600 forwardingPath.setProtocol("protocol");
601 forwardingPath.setDestinationPortNumber("port");
602 forwardingPath.setUniqueId(uniqueId);
603 ListDataDefinition<ForwardingPathElementDataDefinition> forwardingPathElementListDataDefinition =
604 new ListDataDefinition<>();
605 forwardingPathElementListDataDefinition
606 .add(new ForwardingPathElementDataDefinition(fromNode, toNode, "nodeAcpType", "nodeBcpType",
607 "nodeDcpName", "nodeBcpName"));
608 forwardingPath.setPathElements(forwardingPathElementListDataDefinition);
610 return forwardingPath;
613 private Map<String, ForwardingPathDataDefinition> generateForwardingPath(String componentInstanceID) {
614 ForwardingPathDataDefinition forwardingPath = new ForwardingPathDataDefinition("fpName");
615 String protocol = "protocol";
616 forwardingPath.setProtocol(protocol);
617 forwardingPath.setDestinationPortNumber("DestinationPortNumber");
618 forwardingPath.setUniqueId("FP-ID-1");
619 ListDataDefinition<ForwardingPathElementDataDefinition> forwardingPathElementListDataDefinition =
620 new ListDataDefinition<>();
621 forwardingPathElementListDataDefinition
622 .add(new ForwardingPathElementDataDefinition(componentInstanceID, "nodeB", "nodeA_FORWARDER_CAPABILITY",
623 "nodeBcpType", "nodeDcpName", "nodeBcpName"));
624 forwardingPath.setPathElements(forwardingPathElementListDataDefinition);
625 Map<String, ForwardingPathDataDefinition> forwardingPaths = new HashMap<>();
626 forwardingPaths.put("1122", forwardingPath);
627 return forwardingPaths;
630 @SuppressWarnings("unchecked")
631 private void getServiceRelationByIdSuccess(Component component) {
632 Either<Component, StorageOperationStatus> getComponentRes = Either.left(component);
633 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
634 .thenReturn(getComponentRes);
635 Either<RequirementCapabilityRelDef, ResponseFormat> response = componentInstanceBusinessLogic
636 .getRelationById(COMPONENT_ID,
637 RELATION_ID, USER_ID,
638 component.getComponentType());
639 assertTrue(response.isLeft());
642 private void getServiceRelationByIdUserValidationFailure(Component component) {
643 when(userValidations.validateUserExists(eq(USER_ID)))
644 .thenThrow(new ByActionStatusComponentException(ActionStatus.USER_NOT_FOUND));
646 componentInstanceBusinessLogic
647 .getRelationById(COMPONENT_ID, RELATION_ID, USER_ID, component.getComponentType());
648 } catch (ByActionStatusComponentException e) {
649 assertSame(e.getActionStatus(), ActionStatus.USER_NOT_FOUND);
653 private void getRelationByIdComponentNotFoundFailure(Component component) {
654 Either<User, ActionStatus> eitherCreator = Either.left(user);
655 Either<Component, StorageOperationStatus> getComponentRes = Either.right(StorageOperationStatus.NOT_FOUND);
656 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
657 .thenReturn(getComponentRes);
659 Either<RequirementCapabilityRelDef, ResponseFormat> response = componentInstanceBusinessLogic
660 .getRelationById(COMPONENT_ID,
661 RELATION_ID, USER_ID,
662 component.getComponentType());
663 assertTrue(response.isRight());
666 private void stubMethods() {
667 when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(user);
669 .convertFromStorageResponse(eq(StorageOperationStatus.GENERAL_ERROR), any(ComponentTypeEnum.class)))
670 .thenReturn(ActionStatus.GENERAL_ERROR);
673 private void createComponents() {
680 private void createResource() {
681 resource = new Resource();
682 resource.setUniqueId(COMPONENT_ID);
683 resource.setComponentInstancesRelations(Lists.newArrayList(relation));
684 resource.setComponentInstances(Lists.newArrayList(toInstance, fromInstance));
685 resource.setCapabilities(toInstance.getCapabilities());
686 resource.setRequirements(fromInstance.getRequirements());
687 resource.setComponentType(ComponentTypeEnum.RESOURCE);
688 resource.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
691 private void createService() {
692 service = new Service();
693 service.setUniqueId(COMPONENT_ID);
694 service.setComponentInstancesRelations(Lists.newArrayList(relation));
695 service.setComponentInstances(Lists.newArrayList(toInstance, fromInstance));
696 service.setCapabilities(toInstance.getCapabilities());
697 service.setRequirements(fromInstance.getRequirements());
698 service.setComponentType(ComponentTypeEnum.SERVICE);
699 service.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
702 private void createInstances() {
703 toInstance = new ComponentInstance();
704 toInstance.setUniqueId(TO_INSTANCE_ID);
705 toInstance.setName(TO_INSTANCE_NAME);
707 fromInstance = new ComponentInstance();
708 fromInstance.setUniqueId(FROM_INSTANCE_ID);
710 capability = new CapabilityDataDefinition();
711 capability.setOwnerId(CAPABILITY_OWNER_ID);
712 capability.setUniqueId(CAPABILITY_UID);
713 capability.setName(CAPABILITY_NAME);
715 Map<String, List<CapabilityDefinition>> capabilities = new HashMap<>();
716 final CapabilityDefinition capabilityDefinition = new CapabilityDefinition(capability);
717 final ArrayList<ComponentInstanceProperty> properties = new ArrayList<>();
718 properties.add(componentInstancePropInput);
719 capabilityDefinition.setProperties(properties);
720 capabilities.put(capability.getName(), Lists.newArrayList(capabilityDefinition));
722 requirement = new RequirementDataDefinition();
723 requirement.setOwnerId(REQUIREMENT_OWNER_ID);
724 requirement.setUniqueId(REQUIREMENT_UID);
725 requirement.setName(REQUIREMENT_NAME);
726 requirement.setRelationship(RELATIONSHIP_TYPE);
728 Map<String, List<RequirementDefinition>> requirements = new HashMap<>();
729 requirements.put(requirement.getCapability(), Lists.newArrayList(new RequirementDefinition(requirement)));
731 toInstance.setCapabilities(capabilities);
732 fromInstance.setRequirements(requirements);
735 private void createRelation() {
737 relation = new RequirementCapabilityRelDef();
738 CapabilityRequirementRelationship relationship = new CapabilityRequirementRelationship();
739 RelationshipInfo relationInfo = new RelationshipInfo();
740 relationInfo.setId(RELATION_ID);
741 relationship.setRelation(relationInfo);
743 relation.setRelationships(Lists.newArrayList(relationship));
744 relation.setToNode(TO_INSTANCE_ID);
745 relation.setFromNode(FROM_INSTANCE_ID);
747 relationInfo.setCapabilityOwnerId(CAPABILITY_OWNER_ID);
748 relationInfo.setCapabilityUid(CAPABILITY_UID);
749 relationInfo.setCapability(CAPABILITY_NAME);
750 relationInfo.setRequirementOwnerId(REQUIREMENT_OWNER_ID);
751 relationInfo.setRequirementUid(REQUIREMENT_UID);
752 relationInfo.setRequirement(REQUIREMENT_NAME);
753 RelationshipImpl relationshipImpl = new RelationshipImpl();
754 relationshipImpl.setType(RELATIONSHIP_TYPE);
755 relationInfo.setRelationships(relationshipImpl);
758 private ComponentInstanceBusinessLogic createTestSubject() {
759 return componentInstanceBusinessLogic;
763 public void testChangeServiceProxyVersion() {
764 ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
766 Either<ComponentInstance, ResponseFormat> result;
769 componentInstanceBusinessLogic = createTestSubject();
770 result = componentInstanceBusinessLogic.changeServiceProxyVersion();
771 Assert.assertNotNull(result);
775 public void testCreateServiceProxy() {
776 ComponentInstanceBusinessLogic testSubject;
777 Either<ComponentInstance, ResponseFormat> result;
780 testSubject = createTestSubject();
781 result = testSubject.createServiceProxy();
782 Assert.assertNotNull(result);
786 public void testDeleteServiceProxy() {
787 ComponentInstanceBusinessLogic testSubject;
789 Either<ComponentInstance, ResponseFormat> result;
792 testSubject = createTestSubject();
793 result = testSubject.deleteServiceProxy();
794 Assert.assertNotNull(result);
798 public void testGetComponentInstanceInputsByInputId() {
799 ComponentInstanceBusinessLogic testSubject;
800 Component component = new Service();
802 List<ComponentInstanceInput> result;
805 testSubject = createTestSubject();
806 result = testSubject.getComponentInstanceInputsByInputId(component, inputId);
807 Assert.assertNotNull(result);
811 public void testGetComponentInstancePropertiesByInputId() {
812 ComponentInstanceBusinessLogic testSubject;
813 Component component = new Service();
815 List<ComponentInstanceProperty> result;
818 testSubject = createTestSubject();
819 result = testSubject.getComponentInstancePropertiesByInputId(component, inputId);
820 Assert.assertNotNull(result);
824 public void testGetRelationById() {
825 ComponentInstanceBusinessLogic testSubject;
826 String componentId = "";
827 String relationId = "";
828 String userId = user.getUserId();
829 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE_INSTANCE;
830 Either<RequirementCapabilityRelDef, ResponseFormat> result;
833 testSubject = createTestSubject();
834 result = testSubject.getRelationById(componentId, relationId, userId, componentTypeEnum);
835 Assert.assertNotNull(result);
839 public void testValidateParent() {
840 ComponentInstanceBusinessLogic testSubject;
842 String nodeTemplateId = "";
846 testSubject = createTestSubject();
847 result = Deencapsulation.invoke(testSubject, "validateParent", new Object[]{resource, nodeTemplateId});
848 Assert.assertNotNull(result);
852 public void testGetComponentType() {
853 ComponentInstanceBusinessLogic testSubject;
854 ComponentTypeEnum result;
857 testSubject = createTestSubject();
858 result = Deencapsulation.invoke(testSubject, "getComponentType", new Object[]{ComponentTypeEnum.class});
859 Assert.assertNotNull(result);
863 public void testGetNewGroupName() {
864 ComponentInstanceBusinessLogic testSubject;
865 String oldPrefix = "";
866 String newNormailzedPrefix = "";
867 String qualifiedGroupInstanceName = "";
871 testSubject = createTestSubject();
872 result = Deencapsulation.invoke(testSubject, "getNewGroupName",
873 new Object[]{oldPrefix, newNormailzedPrefix, qualifiedGroupInstanceName});
874 Assert.assertNotNull(result);
878 public void testUpdateComponentInstanceMetadata_3() {
879 ComponentInstanceBusinessLogic testSubject;
881 ComponentInstance newComponentInstance = null;
882 ComponentInstance result;
885 testSubject = createTestSubject();
886 result = Deencapsulation
887 .invoke(testSubject, "updateComponentInstanceMetadata", new Object[]{toInstance, toInstance});
888 Assert.assertNotNull(result);
892 public void testFindRelation() throws Exception {
893 ComponentInstanceBusinessLogic testSubject;
894 String relationId = "";
895 List<RequirementCapabilityRelDef> requirementCapabilityRelations = new ArrayList<>();
896 RequirementCapabilityRelDef result;
899 testSubject = createTestSubject();
900 result = Deencapsulation.invoke(testSubject, "findRelation",
901 new Object[]{relationId, requirementCapabilityRelations});
905 public void testCreateOrUpdatePropertiesValues() throws Exception {
906 ComponentInstanceBusinessLogic testSubject;
907 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
909 String componentId = resource.getUniqueId();
910 String resourceInstanceId = "";
911 List<ComponentInstanceProperty> properties = new ArrayList<>();
912 String userId = user.getUserId();
913 Either<List<ComponentInstanceProperty>, ResponseFormat> result;
915 when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll))
916 .thenReturn(Either.left(resource));
919 testSubject = createTestSubject();
921 .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
923 Assert.assertNotNull(result);
925 componentTypeEnum = null;
927 .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
929 Assert.assertNotNull(result);
932 .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
934 Assert.assertNotNull(result);
938 public void testUpdateCapabilityPropertyOnContainerComponent() throws Exception {
939 ComponentInstanceBusinessLogic testSubject;
940 ComponentInstanceProperty property = new ComponentInstanceProperty();
941 String newValue = "";
944 String capabilityType = "";
945 String capabilityName = "";
946 ResponseFormat result;
949 testSubject = createTestSubject();
950 result = Deencapsulation.invoke(testSubject, "updateCapabilityPropertyOnContainerComponent",
951 new Object[]{property, newValue, resource, toInstance, capabilityType, capabilityName});
955 public void testCreateOrUpdateInstanceInputValues() throws Exception {
956 ComponentInstanceBusinessLogic testSubject;
957 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
959 String componentId = resource.getUniqueId();
960 String resourceInstanceId = "";
961 List<ComponentInstanceInput> inputs = new ArrayList<>();
962 String userId = user.getUserId();
963 Either<List<ComponentInstanceInput>, ResponseFormat> result;
965 when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll))
966 .thenReturn(Either.left(resource));
969 testSubject = createTestSubject();
971 .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
973 Assert.assertNotNull(result);
974 componentTypeEnum = null;
976 .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
978 Assert.assertNotNull(result);
981 .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
983 Assert.assertNotNull(result);
987 public void testCreateOrUpdateGroupInstancePropertyValue() throws Exception {
988 ComponentInstanceBusinessLogic testSubject;
989 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
991 String componentId = resource.getUniqueId();
992 String resourceInstanceId = "";
993 String groupInstanceId = "";
994 ComponentInstanceProperty property = new ComponentInstanceProperty();
995 String userId = user.getUserId();
996 Either<ComponentInstanceProperty, ResponseFormat> result;
998 when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseMetadata))
999 .thenReturn(Either.left(resource));
1002 testSubject = createTestSubject();
1003 result = testSubject
1004 .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1005 groupInstanceId, property, userId);
1006 Assert.assertNotNull(result);
1007 componentTypeEnum = null;
1008 result = testSubject
1009 .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1010 groupInstanceId, property, userId);
1011 Assert.assertNotNull(result);
1013 result = testSubject
1014 .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1015 groupInstanceId, property, userId);
1016 Assert.assertNotNull(result);
1020 public void testDeletePropertyValue() throws Exception {
1021 ComponentInstanceBusinessLogic testSubject;
1022 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1024 String serviceId = service.getUniqueId();
1025 String resourceInstanceId = "";
1026 String propertyValueId = "";
1027 String userId = user.getUserId();
1028 Either<ComponentInstanceProperty, ResponseFormat> result;
1030 when(toscaOperationFacade.getToscaElement(serviceId, JsonParseFlagEnum.ParseMetadata))
1031 .thenReturn(Either.left(service));
1034 testSubject = createTestSubject();
1035 result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1037 Assert.assertNotNull(result);
1038 componentTypeEnum = null;
1039 result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1041 Assert.assertNotNull(result);
1043 result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1045 Assert.assertNotNull(result);
1049 public void testGetComponentParametersViewForForwardingPath() throws Exception {
1050 ComponentInstanceBusinessLogic testSubject;
1051 ComponentParametersView result;
1054 testSubject = createTestSubject();
1055 result = Deencapsulation.invoke(testSubject, "getComponentParametersViewForForwardingPath");
1056 Assert.assertNotNull(result);
1060 public void testGetResourceInstanceById() throws Exception {
1061 ComponentInstanceBusinessLogic testSubject;
1063 String instanceId = "";
1064 Either<ComponentInstance, StorageOperationStatus> result;
1067 testSubject = createTestSubject();
1068 result = Deencapsulation.invoke(testSubject, "getResourceInstanceById", new Object[]{resource, instanceId});
1069 Assert.assertNotNull(result);
1073 public void testUpdateInstanceCapabilityProperties_1() throws Exception {
1074 ComponentInstanceBusinessLogic testSubject;
1075 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1077 String containerComponentId = resource.getUniqueId();
1078 String componentInstanceUniqueId = "";
1079 String capabilityType = "";
1080 String capabilityName = "";
1081 List<ComponentInstanceProperty> properties = new ArrayList<>();
1082 String userId = user.getUserId();
1083 Either<List<ComponentInstanceProperty>, ResponseFormat> result;
1085 when(toscaOperationFacade.getToscaFullElement(containerComponentId))
1086 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
1088 testSubject = createTestSubject();
1089 result = testSubject.updateInstanceCapabilityProperties(componentTypeEnum, containerComponentId,
1090 componentInstanceUniqueId, capabilityType, capabilityName, properties, userId);
1091 Assert.assertNotNull(result);
1092 when(toscaOperationFacade.getToscaFullElement(containerComponentId)).thenReturn(Either.left(resource));
1093 result = testSubject.updateInstanceCapabilityProperties(componentTypeEnum, containerComponentId,
1094 componentInstanceUniqueId, capabilityType, capabilityName, properties, userId);
1095 Assert.assertNotNull(result);
1099 public void testCopyComponentInstanceWrongUserId() {
1101 Either<Map<String, ComponentInstance>, ResponseFormat> result;
1102 ComponentInstance inputComponentInstance = createComponetInstanceFromComponent(resource);
1103 String containerComponentId = service.getUniqueId();
1104 String componentInstanceId = resource.getUniqueId();
1105 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1106 service.setLastUpdaterUserId("wrong user id");
1108 Either<Component, StorageOperationStatus> leftServiceOp = Either.left(service);
1109 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(leftServiceOp);
1110 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1111 .thenReturn(leftServiceOp);
1112 when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
1113 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1114 .thenReturn(StorageOperationStatus.OK);
1115 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1116 .thenReturn(StorageOperationStatus.OK);
1118 result = componentInstanceBusinessLogic
1119 .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId,
1121 Assert.assertNotNull(result);
1123 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1124 assertThat(result.isRight());
1128 public void testCopyComponentInstanceComponentWrongState() {
1129 Either<Map<String, ComponentInstance>, ResponseFormat> result;
1130 ComponentInstance inputComponentInstance = createComponetInstanceFromComponent(resource);
1131 String containerComponentId = service.getUniqueId();
1132 String componentInstanceId = resource.getUniqueId();
1133 String oldServiceLastUpdatedUserId = service.getLastUpdaterUserId();
1134 service.setLastUpdaterUserId(USER_ID);
1136 Either<Component, StorageOperationStatus> leftServiceOp = Either.left(service);
1137 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(leftServiceOp);
1138 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1139 .thenReturn(leftServiceOp);
1140 when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
1141 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1142 .thenReturn(StorageOperationStatus.OK);
1143 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1144 .thenReturn(StorageOperationStatus.OK);
1145 Either<Component, StorageOperationStatus> getComponentRes = Either.left(resource);
1146 result = componentInstanceBusinessLogic
1147 .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId, USER_ID);
1148 Assert.assertNotNull(result);
1149 service.setLastUpdaterUserId(oldServiceLastUpdatedUserId);
1150 assertThat(result.isRight());
1154 public void testCopyComponentInstance() {
1155 Either<Map<String, ComponentInstance>, ResponseFormat> result;
1156 ComponentInstance inputComponentInstance = createComponetInstanceFromComponent(resource);
1157 String containerComponentId = service.getUniqueId();
1158 String componentInstanceId = resource.getUniqueId();
1159 String oldServiceLastUpdatedUserId = service.getLastUpdaterUserId();
1160 service.setLastUpdaterUserId(USER_ID);
1161 LifecycleStateEnum oldResourceLifeCycle = resource.getLifecycleState();
1162 resource.setLifecycleState(LifecycleStateEnum.CERTIFIED);
1164 Either<Component, StorageOperationStatus> leftServiceOp = Either.left(service);
1165 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(leftServiceOp);
1166 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1167 .thenReturn(leftServiceOp);
1168 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1169 .thenReturn(StorageOperationStatus.OK);
1170 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1171 .thenReturn(StorageOperationStatus.OK);
1172 Either<Component, StorageOperationStatus> getComponentRes = Either.left(resource);
1173 ImmutablePair<Component, String> pair = new ImmutablePair<>(resource, TO_INSTANCE_ID);
1174 Either<ImmutablePair<Component, String>, StorageOperationStatus> result2 = Either.left(pair);
1175 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getResourceDeploymentArtifacts = Either
1176 .left(new HashMap<String, ArtifactDefinition>());
1177 StorageOperationStatus artStatus = StorageOperationStatus.OK;
1179 result = componentInstanceBusinessLogic
1180 .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId,
1182 Assert.assertNotNull(result);
1184 service.setLastUpdaterUserId(oldServiceLastUpdatedUserId);
1185 resource.setLifecycleState(oldResourceLifeCycle);
1187 assertThat(result.isLeft());
1191 public void testCreateOrUpdateAttributeValueForCopyPaste() {
1192 ComponentInstance serviceComponentInstance = createComponetInstanceFromComponent(service);
1193 ComponentInstanceProperty attribute = new ComponentInstanceProperty();
1194 attribute.setType("string");
1195 attribute.setUniqueId("testCreateOrUpdateAttributeValueForCopyPaste");
1196 SchemaDefinition def = Mockito.mock(SchemaDefinition.class);
1197 attribute.setSchema(def);
1198 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1199 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1200 service.setLastUpdaterUserId(USER_ID);
1201 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1203 Map<String, List<ComponentInstanceProperty>> instAttrsMap =
1204 new HashMap<String, List<ComponentInstanceProperty>>();
1205 List<ComponentInstanceProperty> instAttrsList = new ArrayList<ComponentInstanceProperty>();
1206 ComponentInstanceProperty prop = new ComponentInstanceProperty();
1207 prop.setUniqueId(attribute.getUniqueId());
1208 instAttrsList.add(prop);
1209 instAttrsMap.put(toInstance.getUniqueId(), instAttrsList);
1210 service.setComponentInstancesAttributes(instAttrsMap);
1212 Either<Component, StorageOperationStatus> serviceEitherLeft = Either.left(service);
1213 when(toscaOperationFacade.getToscaElement(serviceComponentInstance.getUniqueId(), JsonParseFlagEnum.ParseAll))
1214 .thenReturn(serviceEitherLeft);
1215 when(toscaOperationFacade.updateComponentInstanceAttribute(service, toInstance.getUniqueId(), attribute))
1216 .thenReturn(StorageOperationStatus.OK);
1217 when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(service))
1218 .thenReturn(serviceEitherLeft);
1220 Either<ComponentInstanceProperty, ResponseFormat> result = Deencapsulation
1221 .invoke(componentInstanceBusinessLogic,
1222 "createOrUpdateAttributeValueForCopyPaste",
1223 ComponentTypeEnum.SERVICE,
1224 serviceComponentInstance
1226 toInstance.getUniqueId(), attribute,
1228 Assert.assertNotNull(result);
1230 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1231 service.setLifecycleState(oldLifeCycleState);
1233 assertTrue(result.isLeft());
1234 ComponentInstanceProperty resultProp = result.left().value();
1235 assertEquals(resultProp.getPath().size(), 1);
1236 assertEquals(resultProp.getPath().get(0), toInstance.getUniqueId());
1240 public void testUpdateComponentInstanceProperty() {
1242 String containerComponentId = service.getUniqueId();
1243 String componentInstanceId = "dummy_id";
1244 ComponentInstanceProperty property = Mockito.mock(ComponentInstanceProperty.class);
1246 Either<Component, StorageOperationStatus> getComponent = Either.left(service);
1247 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(getComponent);
1248 StorageOperationStatus status = StorageOperationStatus.OK;
1249 when(toscaOperationFacade.updateComponentInstanceProperty(service, componentInstanceId, property))
1250 .thenReturn(status);
1251 Either<Component, StorageOperationStatus> updateContainerRes = Either.left(service);
1252 when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(service))
1253 .thenReturn(updateContainerRes);
1255 Either<String, ResponseFormat> result = Deencapsulation.invoke(componentInstanceBusinessLogic,
1256 "updateComponentInstanceProperty", containerComponentId, componentInstanceId, property);
1257 Assert.assertNotNull(result);
1258 assertTrue(result.isLeft());
1262 public void testGetInputListDefaultValue() {
1263 Component component = service;
1264 String inputId = "dummy_id";
1265 String defaultValue = "dummy_default_value";
1266 List<InputDefinition> newInputs = new ArrayList<InputDefinition>();
1267 InputDefinition in = new InputDefinition();
1268 in.setUniqueId(inputId);
1269 in.setDefaultValue(defaultValue);
1271 List<InputDefinition> oldInputs = service.getInputs();
1272 service.setInputs(newInputs);
1274 Either<String, ResponseFormat> result =
1275 Deencapsulation.invoke(componentInstanceBusinessLogic, "getInputListDefaultValue", component, inputId);
1277 service.setInputs(oldInputs);
1279 assertEquals(result.left().value(), defaultValue);
1283 public void testBatchDeleteComponentInstanceFailureWrongType() {
1284 Map<String, List<String>> result;
1285 List<String> componentInstanceIdList = new ArrayList<>();
1286 String containerComponentParam = "WRONG_TYPE";
1287 String containerComponentId = "containerComponentId";
1288 String componentInstanceId = "componentInstanceId";
1289 componentInstanceIdList.add(componentInstanceId);
1290 String userId = USER_ID;
1291 Map<String, List<String>> deleteErrorMap = new HashMap<>();
1292 List<String> deleteErrorIds = new ArrayList<>();
1293 deleteErrorIds.add(componentInstanceId);
1294 deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
1295 Either<Component, StorageOperationStatus> cont = Either.left(service);
1296 when(componentsUtils.convertFromStorageResponse(eq(StorageOperationStatus.NOT_FOUND), eq(null)))
1297 .thenReturn(ActionStatus.GENERAL_ERROR);
1298 when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class)))
1302 result = componentInstanceBusinessLogic
1303 .batchDeleteComponentInstance(containerComponentParam, containerComponentId, componentInstanceIdList,
1305 Assert.assertNotNull(result);
1306 assertEquals(deleteErrorMap, result);
1307 } catch (ComponentException e) {
1308 assertEquals(e.getActionStatus().toString(), StorageOperationStatus.GENERAL_ERROR.toString());
1313 public void testBatchDeleteComponentInstanceFailureCompIds() {
1314 Map<String, List<String>> result = new HashMap<>();
1315 String containerComponentParam = ComponentTypeEnum.SERVICE_PARAM_NAME;
1316 String containerComponentId = "containerComponentId";
1317 String componentInstanceId = "componentInstanceId";
1318 List<String> componentInstanceIdList = new ArrayList<>();
1319 componentInstanceIdList.add(componentInstanceId);
1320 String userId = USER_ID;
1321 Map<String, List<String>> deleteErrorMap = new HashMap<>();
1322 List<String> deleteErrorIds = new ArrayList<>();
1323 deleteErrorIds.add(componentInstanceId);
1324 deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
1326 Either<Component, StorageOperationStatus> err = Either.right(StorageOperationStatus.GENERAL_ERROR);
1327 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1331 result = componentInstanceBusinessLogic
1332 .batchDeleteComponentInstance(containerComponentParam, containerComponentId, componentInstanceIdList,
1334 Assert.assertNotNull(result);
1335 assertEquals(deleteErrorMap, result);
1336 } catch (ComponentException e) {
1337 assertEquals(e.getActionStatus().toString(), StorageOperationStatus.GENERAL_ERROR.toString());
1342 public void testBatchDeleteComponentInstanceSuccess() {
1343 Map<String, List<String>> result;
1344 String containerComponentParam = ComponentTypeEnum.SERVICE_PARAM_NAME;
1345 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1346 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1347 service.setLastUpdaterUserId(USER_ID);
1348 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1349 String containerComponentId = service.getUniqueId();
1350 String componentInstanceId = TO_INSTANCE_ID;
1351 String userId = USER_ID;
1352 List<String> componentInstanceIdList = new ArrayList<>();
1353 componentInstanceIdList.add(componentInstanceId);
1354 Map<String, List<String>> deleteErrorMap = new HashMap<>();
1355 List<String> deleteErrorIds = new ArrayList<>();
1356 deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
1358 Either<Component, StorageOperationStatus> cont = Either.left(service);
1359 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1360 .thenReturn(StorageOperationStatus.OK);
1361 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1362 .thenReturn(StorageOperationStatus.OK);
1363 ImmutablePair<Component, String> pair = new ImmutablePair<>(resource, TO_INSTANCE_ID);
1364 Either<ImmutablePair<Component, String>, StorageOperationStatus> result2 = Either.left(pair);
1365 when(toscaOperationFacade.deleteComponentInstanceFromTopologyTemplate(service, componentInstanceId))
1366 .thenReturn(result2);
1367 when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
1369 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
1371 result = componentInstanceBusinessLogic
1372 .batchDeleteComponentInstance(containerComponentParam, containerComponentId,
1373 componentInstanceIdList, userId);
1374 Assert.assertNotNull(result);
1376 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1377 service.setLifecycleState(oldLifeCycleState);
1378 assertEquals(deleteErrorMap, result);
1382 public void testDissociateRIFromRIFailDissociate() {
1384 List<RequirementCapabilityRelDef> result;
1385 RequirementCapabilityRelDef ref = new RequirementCapabilityRelDef();
1386 ref.setFromNode(FROM_INSTANCE_ID);
1387 ref.setToNode(TO_INSTANCE_ID);
1388 List<CapabilityRequirementRelationship> relationships = new ArrayList<>();
1389 CapabilityRequirementRelationship relationship = new CapabilityRequirementRelationship();
1390 RelationshipInfo ri = new RelationshipInfo();
1391 ri.setRequirement(REQUIREMENT_NAME);
1392 relationship.setRelation(ri);
1393 relationships.add(relationship);
1394 ref.setRelationships(relationships);
1395 List<RequirementCapabilityRelDef> requirementDefList = new ArrayList<>();
1396 requirementDefList.add(ref);
1397 ComponentTypeEnum componentTypeEnum = service.getComponentType();
1398 String componentId = service.getUniqueId();
1399 String userId = USER_ID;
1400 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1401 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1402 service.setLastUpdaterUserId(USER_ID);
1403 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1405 Either<Component, StorageOperationStatus> cont = Either.left(service);
1406 when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
1408 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1409 .thenReturn(StorageOperationStatus.OK);
1410 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1411 .thenReturn(StorageOperationStatus.OK);
1412 Either<RequirementCapabilityRelDef, StorageOperationStatus> resultEither;
1413 resultEither = Either.right(StorageOperationStatus.OK);
1414 when(componentsUtils.convertFromStorageResponseForResourceInstance(eq(StorageOperationStatus.OK), eq(true)))
1415 .thenReturn(ActionStatus.GENERAL_ERROR);
1416 when(toscaOperationFacade.dissociateResourceInstances(componentId, ref)).thenReturn(resultEither);
1419 result = componentInstanceBusinessLogic
1420 .batchDissociateRIFromRI(componentId, userId, requirementDefList, componentTypeEnum);
1421 Assert.assertNotNull(result);
1422 assertEquals(new ArrayList<>(), result);
1423 } catch (ComponentException e) {
1424 assertEquals(e.getActionStatus().toString(), StorageOperationStatus.GENERAL_ERROR.toString());
1427 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1428 service.setLifecycleState(oldLifeCycleState);
1433 public void testDissociateRIFromRISuccess() {
1435 List<RequirementCapabilityRelDef> result;
1436 RequirementCapabilityRelDef ref = new RequirementCapabilityRelDef();
1437 List<RequirementCapabilityRelDef> requirementDefList = new ArrayList<>();
1438 requirementDefList.add(ref);
1439 ComponentTypeEnum componentTypeEnum = service.getComponentType();
1440 String componentId = service.getUniqueId();
1441 String userId = USER_ID;
1442 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1443 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1444 service.setLastUpdaterUserId(USER_ID);
1445 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1447 Either<Component, StorageOperationStatus> cont = Either.left(service);
1448 when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
1450 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1451 .thenReturn(StorageOperationStatus.OK);
1452 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1453 .thenReturn(StorageOperationStatus.OK);
1454 Either<RequirementCapabilityRelDef, StorageOperationStatus> resultEither;
1455 resultEither = Either.left(ref);
1456 when(toscaOperationFacade.dissociateResourceInstances(componentId, ref)).thenReturn(resultEither);
1458 result = componentInstanceBusinessLogic
1459 .batchDissociateRIFromRI(componentId, userId, requirementDefList, componentTypeEnum);
1460 Assert.assertNotNull(result);
1462 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1463 service.setLifecycleState(oldLifeCycleState);
1465 assertEquals(requirementDefList, result);
1469 public void testGetComponentInstancePropertyByPolicyId_success() {
1470 Optional<ComponentInstanceProperty> propertyCandidate =
1471 getComponentInstanceProperty(PROP_NAME);
1473 Assert.assertTrue(propertyCandidate.isPresent());
1474 Assert.assertEquals(propertyCandidate.get().getName(), PROP_NAME);
1478 public void testGetComponentInstancePropertyByPolicyId_failure() {
1479 Optional<ComponentInstanceProperty> propertyCandidate =
1480 getComponentInstanceProperty(NON_EXIST_NAME);
1482 Assert.assertEquals(propertyCandidate, Optional.empty());
1485 private Optional<ComponentInstanceProperty> getComponentInstanceProperty(String propertyName) {
1486 ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
1487 componentInstanceProperty.setName(propertyName);
1489 PolicyDefinition policyDefinition = getPolicyDefinition();
1490 componentInstanceProperty.setGetPolicyValues(policyDefinition.getGetPolicyValues());
1492 service.setComponentInstancesProperties(
1493 Collections.singletonMap(COMPONENT_INST_ID, Collections.singletonList(componentInstanceProperty)));
1495 return componentInstanceBusinessLogic.getComponentInstancePropertyByPolicyId(service, policyDefinition);
1498 private PolicyDefinition getPolicyDefinition() {
1499 PolicyDefinition policyDefinition = new PolicyDefinition();
1500 policyDefinition.setInstanceUniqueId(COMPONENT_INST_ID);
1501 policyDefinition.setName(PROP_NAME);
1503 GetPolicyValueDataDefinition getPolicy = new GetPolicyValueDataDefinition();
1504 getPolicy.setPropertyName(PROP_NAME);
1506 List<GetPolicyValueDataDefinition> getPolicies = new ArrayList<>();
1507 getPolicies.add(getPolicy);
1508 policyDefinition.setGetPolicyValues(getPolicies);
1510 return policyDefinition;
1513 private ComponentInstance createComponetInstanceFromComponent(Component component) {
1514 ComponentInstance componentInst = new ComponentInstance();
1515 componentInst.setUniqueId(component.getUniqueId());
1516 componentInst.setComponentUid(component.getUniqueId() + "_test");
1517 componentInst.setPosX("10");
1518 componentInst.setPosY("10");
1519 componentInst.setCapabilities(component.getCapabilities());
1520 componentInst.setRequirements(component.getRequirements());
1521 componentInst.setArtifacts(component.getArtifacts());
1522 componentInst.setDeploymentArtifacts(component.getDeploymentArtifacts());
1523 return componentInst;