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.assertNotNull;
26 import static org.junit.Assert.assertSame;
27 import static org.junit.Assert.fail;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.ArgumentMatchers.anySet;
30 import static org.mockito.ArgumentMatchers.eq;
31 import static org.mockito.Mockito.when;
33 import fj.data.Either;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.HashSet;
39 import java.util.List;
41 import java.util.Optional;
43 import mockit.Deencapsulation;
44 import org.apache.commons.lang3.tuple.ImmutablePair;
45 import org.assertj.core.util.Lists;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 import org.mockito.InjectMocks;
50 import org.mockito.Mock;
51 import org.mockito.Mockito;
52 import org.mockito.MockitoAnnotations;
53 import org.mockito.junit.MockitoJUnitRunner;
54 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
55 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
56 import org.openecomp.sdc.be.components.validation.UserValidations;
57 import org.openecomp.sdc.be.config.ConfigurationManager;
58 import org.openecomp.sdc.be.dao.api.ActionStatus;
59 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
60 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
61 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
62 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
63 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
64 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathElementDataDefinition;
65 import org.openecomp.sdc.be.datatypes.elements.GetPolicyValueDataDefinition;
66 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
67 import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition;
68 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
69 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
70 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
71 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
72 import org.openecomp.sdc.be.impl.ComponentsUtils;
73 import org.openecomp.sdc.be.impl.ServletUtils;
74 import org.openecomp.sdc.be.model.ArtifactDefinition;
75 import org.openecomp.sdc.be.model.CapabilityDefinition;
76 import org.openecomp.sdc.be.model.CapabilityRequirementRelationship;
77 import org.openecomp.sdc.be.model.Component;
78 import org.openecomp.sdc.be.model.ComponentInstance;
79 import org.openecomp.sdc.be.model.ComponentInstanceInput;
80 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
81 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
82 import org.openecomp.sdc.be.model.ComponentParametersView;
83 import org.openecomp.sdc.be.model.DataTypeDefinition;
84 import org.openecomp.sdc.be.model.InputDefinition;
85 import org.openecomp.sdc.be.model.LifecycleStateEnum;
86 import org.openecomp.sdc.be.model.PolicyDefinition;
87 import org.openecomp.sdc.be.model.RelationshipImpl;
88 import org.openecomp.sdc.be.model.RelationshipInfo;
89 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
90 import org.openecomp.sdc.be.model.RequirementDefinition;
91 import org.openecomp.sdc.be.model.Resource;
92 import org.openecomp.sdc.be.model.Service;
93 import org.openecomp.sdc.be.model.User;
94 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
95 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ForwardingPathOperation;
96 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
97 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
98 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
99 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
100 import org.openecomp.sdc.be.user.UserBusinessLogic;
101 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
102 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
103 import org.openecomp.sdc.common.api.ConfigurationSource;
104 import org.openecomp.sdc.common.impl.ExternalConfiguration;
105 import org.openecomp.sdc.common.impl.FSConfigurationSource;
106 import org.openecomp.sdc.exception.ResponseFormat;
109 * The test suite designed for test functionality of ComponentInstanceBusinessLogic class
112 @RunWith(MockitoJUnitRunner.class)
113 public class ComponentInstanceBusinessLogicTest {
115 private final static String USER_ID = "jh0003";
116 private final static String COMPONENT_ID = "componentId";
117 private final static String ORIGIN_COMPONENT_ID = "originComponentId";
118 private final static String COMPONENT_INST_ID = "componentInstId";
119 private final static String TO_INSTANCE_ID = "toInstanceId";
120 private final static String TO_INSTANCE_NAME = "toInstanceName";
121 private final static String COMPONENT_INSTANCE_ID = "componentInstanceId";
122 private final static String FROM_INSTANCE_ID = "fromInstanceId";
123 private final static String RELATION_ID = "relationId";
124 private final static String CAPABILITY_OWNER_ID = "capabilityOwnerId";
125 private final static String CAPABILITY_UID = "capabilityUid";
126 private final static String CAPABILITY_NAME = "capabilityName";
127 private final static String REQUIREMENT_OWNER_ID = "requirementOwnerId";
128 private final static String REQUIREMENT_UID = "requirementUid";
129 private final static String REQUIREMENT_NAME = "requirementName";
130 private final static String RELATIONSHIP_TYPE = "relationshipType";
131 private final static String ARTIFACT_1 = "cloudtech_k8s_charts.zip";
132 private final static String ARTIFACT_2 = "cloudtech_azure_day0.zip";
133 private final static String ARTIFACT_3 = "cloudtech_aws_configtemplate.zip";
134 private final static String ARTIFACT_4 = "k8s_charts.zip";
135 private final static String ARTIFACT_5 = "cloudtech_openstack_configtemplate.zip";
136 private final static String PROP_NAME = "propName";
137 private final static String NON_EXIST_NAME = "nonExistName";
139 static ConfigurationSource configurationSource = new FSConfigurationSource(
140 ExternalConfiguration.getChangeListener(),
141 "src/test/resources/config/catalog-be");
142 static ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
145 private static ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
147 private ComponentInstancePropInput componentInstancePropInput;
149 ArtifactsBusinessLogic artifactsBusinessLogic;
151 private ComponentsUtils componentsUtils;
153 private ServletUtils servletUtils;
155 private ResponseFormat responseFormat;
157 private ToscaOperationFacade toscaOperationFacade;
159 private UserBusinessLogic userAdmin;
161 private ForwardingPathOperation forwardingPathOperation;
165 private UserValidations userValidations;
167 GraphLockOperation graphLockOperation;
169 private JanusGraphDao janusGraphDao;
171 ApplicationDataTypeCache dataTypeCache;
173 PropertyOperation propertyOperation;
175 ApplicationDataTypeCache applicationDataTypeCache;
177 private Component service;
178 private Component resource;
179 private ComponentInstance toInstance;
180 private ComponentInstance fromInstance;
181 private CapabilityDataDefinition capability;
182 private RequirementDataDefinition requirement;
183 private RequirementCapabilityRelDef relation;
187 MockitoAnnotations.initMocks(componentInstanceBusinessLogic);
193 public void testGetRelationByIdSuccess() {
194 getServiceRelationByIdSuccess(service);
195 getServiceRelationByIdSuccess(resource);
199 public void testGetRelationByIdUserValidationFailure() {
200 getServiceRelationByIdUserValidationFailure(service);
201 getServiceRelationByIdUserValidationFailure(resource);
205 public void testGetRelationByIdComponentNotFoundFailure() {
206 getRelationByIdComponentNotFoundFailure(service);
207 getRelationByIdComponentNotFoundFailure(resource);
211 public void testForwardingPathOnVersionChange() {
212 getforwardingPathOnVersionChange();
216 public void testIsCloudSpecificArtifact() {
217 assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_1)).isTrue();
218 assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_2)).isTrue();
219 assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_3)).isTrue();
220 assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_4)).isFalse();
221 assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_5)).isFalse();
224 private void getforwardingPathOnVersionChange() {
225 String containerComponentParam = "services";
226 String containerComponentID = "121-cont";
227 String componentInstanceID = "121-cont-1-comp";
228 Service component = new Service();
229 Map<String, ForwardingPathDataDefinition> forwardingPaths = generateForwardingPath(componentInstanceID);
231 //Add existing componentInstance to component
232 List<ComponentInstance> componentInstanceList = new ArrayList<>();
233 ComponentInstance oldComponentInstance = new ComponentInstance();
234 oldComponentInstance.setName("OLD_COMP_INSTANCE");
235 oldComponentInstance.setUniqueId(componentInstanceID);
236 oldComponentInstance.setName(componentInstanceID);
237 oldComponentInstance.setToscaPresentationValue(JsonPresentationFields.CI_COMPONENT_UID, "1-comp");
238 componentInstanceList.add(oldComponentInstance);
239 component.setComponentInstances(componentInstanceList);
240 component.setForwardingPaths(forwardingPaths);
242 List<ComponentInstance> componentInstanceListNew = new ArrayList<>();
243 ComponentInstance newComponentInstance = new ComponentInstance();
244 String new_Comp_UID = "2-comp";
245 newComponentInstance.setToscaPresentationValue(JsonPresentationFields.CI_COMPONENT_UID, new_Comp_UID);
246 newComponentInstance.setUniqueId(new_Comp_UID);
247 componentInstanceListNew.add(newComponentInstance);
248 Component component2 = new Service();
249 component2.setComponentInstances(componentInstanceListNew);
251 //Mock for getting component
252 when(toscaOperationFacade.getToscaElement(eq(containerComponentID), any(ComponentParametersView.class)))
253 .thenReturn(Either.left(component));
254 when(toscaOperationFacade.validateComponentExists(any(String.class))).thenReturn(Either.left(Boolean.TRUE));
255 when(toscaOperationFacade.getToscaFullElement(eq(new_Comp_UID))).thenReturn(Either.left(component2));
257 Either<Set<String>, ResponseFormat> resultOp = componentInstanceBusinessLogic
258 .forwardingPathOnVersionChange(containerComponentParam,
259 containerComponentID, componentInstanceID,
260 newComponentInstance);
261 assertEquals(1, resultOp.left().value().size());
262 assertEquals("FP-ID-1", resultOp.left().value().iterator().next());
267 public void testCreateOrUpdatePropertiesValues2() {
268 String containerComponentID = "containerId";
269 String resourceInstanceId = "resourceId";
270 String componentInstanceID = "componentInstance";
271 List<ComponentInstanceProperty> properties = new ArrayList<>();
272 ComponentInstanceProperty property = new ComponentInstanceProperty();
273 property.setName("property");
274 property.setValue("newVal");
275 property.setType("string");
276 properties.add(property);
278 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
279 ComponentInstanceProperty origProperty = new ComponentInstanceProperty();
280 origProperty.setName("property");
281 origProperty.setValue("value");
282 origProperty.setType("string");
283 origProperties.add(origProperty);
285 Map<String, DataTypeDefinition> types = new HashMap<>();
286 DataTypeDefinition dataTypeDef = new DataTypeDefinition();
287 types.put("string", dataTypeDef);
289 Component component = new Service();
290 component.setLastUpdaterUserId("userId");
291 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
292 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
293 componentInstanceProps.put("resourceId", origProperties);
294 component.setComponentInstancesProperties(componentInstanceProps);
295 ComponentInstance ci = createComponentInstance("ci1");
296 ci.setUniqueId("resourceId");
297 component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
298 createComponentInstance(componentInstanceID)));
299 HashMap<String, DataTypeDefinition> dataTypeDefinitionHashMap = new HashMap<>();
300 DataTypeDefinition dataTypeDefinition = new DataTypeDefinition();
301 dataTypeDefinition.setName("string");
302 dataTypeDefinitionHashMap.put("string", dataTypeDefinition);
304 //when(userValidations.validateUserExists(user.getUserId(), false)).thenReturn(user);
305 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
306 .thenReturn(Either.left(component));
307 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
308 .thenReturn(StorageOperationStatus.OK);
309 when(dataTypeCache.getAll()).thenReturn(Either.left(types));
310 when(propertyOperation.validateAndUpdatePropertyValue(property.getType(), "newVal", true, null, types))
311 .thenReturn(Either.left("newVal"));
312 when(propertyOperation.validateAndUpdateRules("string", property.getRules(),
313 null, types, true)).thenReturn(ImmutablePair.of("string", null));
314 when(toscaOperationFacade.updateComponentInstanceProperty(component, ci.getUniqueId(),
315 origProperty)).thenReturn(StorageOperationStatus.OK);
316 origProperties.get(0).setValue("newVal");
317 when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(component))
318 .thenReturn(Either.left(component));
319 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
320 when(graphLockOperation.unlockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
321 .thenReturn(StorageOperationStatus.OK);
323 Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = componentInstanceBusinessLogic
324 .createOrUpdatePropertiesValues(
325 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
326 assertThat(responseFormatEither.left().value()).isEqualTo(properties);
330 public void testCreateOrUpdatePropertiesValuesPropertyNotExists() {
331 String containerComponentID = "containerId";
332 String resourceInstanceId = "resourceId";
333 String componentInstanceID = "componentInstance";
334 List<ComponentInstanceProperty> properties = new ArrayList<>();
335 ComponentInstanceProperty property = new ComponentInstanceProperty();
336 property.setName("property");
337 property.setValue("newVal");
338 property.setType("string");
339 properties.add(property);
341 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
343 Map<String, DataTypeDefinition> types = new HashMap<>();
344 DataTypeDefinition dataTypeDef = new DataTypeDefinition();
345 types.put("string", dataTypeDef);
347 Component component = new Service();
348 component.setLastUpdaterUserId("userId");
349 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
350 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
351 componentInstanceProps.put("resourceId", origProperties);
352 component.setComponentInstancesProperties(componentInstanceProps);
353 ComponentInstance ci = createComponentInstance("ci1");
354 ci.setUniqueId("resourceId");
355 component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
356 createComponentInstance(componentInstanceID)));
357 HashMap<String, DataTypeDefinition> dataTypeDefinitionHashMap = new HashMap<>();
358 DataTypeDefinition dataTypeDefinition = new DataTypeDefinition();
359 dataTypeDefinition.setName("string");
360 dataTypeDefinitionHashMap.put("string", dataTypeDefinition);
362 //when(userValidations.validateUserExists(user.getUserId(), false)).thenReturn(user);
363 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
364 .thenReturn(Either.left(component));
365 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
366 .thenReturn(StorageOperationStatus.OK);
367 //when(dataTypeCache.getAll()).thenReturn(Either.left(types));
368 //when (janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
369 when(graphLockOperation.unlockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
370 .thenReturn(StorageOperationStatus.OK);
373 componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
374 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
375 } catch (ComponentException e) {
376 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.PROPERTY_NOT_FOUND);
382 public void testCreateOrUpdatePropertiesValuesValidationFailure() {
383 String containerComponentID = "containerId";
384 String resourceInstanceId = "resourceId";
385 String componentInstanceID = "componentInstance";
386 List<ComponentInstanceProperty> properties = new ArrayList<>();
387 ComponentInstanceProperty property = new ComponentInstanceProperty();
388 property.setName("property");
389 property.setValue("newVal");
390 property.setType("string");
391 properties.add(property);
393 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
394 ComponentInstanceProperty origProperty = new ComponentInstanceProperty();
395 origProperty.setName("property");
396 origProperty.setValue("value");
397 origProperty.setType("string");
398 origProperties.add(origProperty);
400 Map<String, DataTypeDefinition> types = new HashMap<>();
401 DataTypeDefinition dataTypeDef = new DataTypeDefinition();
402 types.put("string", dataTypeDef);
404 Component component = new Service();
405 component.setLastUpdaterUserId("userId");
406 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
407 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
408 componentInstanceProps.put("resourceId", origProperties);
409 component.setComponentInstancesProperties(componentInstanceProps);
410 ComponentInstance ci = createComponentInstance("ci1");
411 ci.setUniqueId("resourceId");
412 component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
413 createComponentInstance(componentInstanceID)));
414 HashMap<String, DataTypeDefinition> dataTypeDefinitionHashMap = new HashMap<>();
415 DataTypeDefinition dataTypeDefinition = new DataTypeDefinition();
416 dataTypeDefinition.setName("string");
417 dataTypeDefinitionHashMap.put("string", dataTypeDefinition);
419 //when(userValidations.validateUserExists(user.getUserId(), false)).thenReturn(user);
420 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
421 .thenReturn(Either.left(component));
422 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
423 .thenReturn(StorageOperationStatus.OK);
424 when(dataTypeCache.getAll()).thenReturn(Either.left(types));
425 when(propertyOperation.validateAndUpdatePropertyValue(property.getType(), "newVal", true, null, types))
426 .thenReturn(Either.right(false));
427 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.BAD_REQUEST))
428 .thenReturn(ActionStatus.INVALID_CONTENT);
431 componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
432 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
433 } catch (ComponentException e) {
434 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.INVALID_CONTENT);
441 public void testCreateOrUpdatePropertiesValuesMissingFieldFailure() {
442 String containerComponentID = "containerId";
443 String resourceInstanceId = "resourceId";
444 String componentInstanceID = "componentInstance";
445 List<ComponentInstanceProperty> properties = new ArrayList<>();
446 ComponentInstanceProperty property = new ComponentInstanceProperty();
447 property.setValue("newVal");
448 property.setType("string");
449 properties.add(property);
451 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
452 ComponentInstanceProperty origProperty = new ComponentInstanceProperty();
453 origProperty.setName("property");
454 origProperty.setValue("value");
455 origProperty.setType("string");
456 origProperties.add(origProperty);
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 HashMap<String, DataTypeDefinition> dataTypeDefinitionHashMap = new HashMap<>();
470 DataTypeDefinition dataTypeDefinition = new DataTypeDefinition();
471 dataTypeDefinition.setName("string");
472 dataTypeDefinitionHashMap.put("string", dataTypeDefinition);
474 //when(userValidations.validateUserExists(user.getUserId(), false)).thenReturn(user);
475 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
476 .thenReturn(Either.left(component));
477 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
478 .thenReturn(StorageOperationStatus.OK);
481 componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
482 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
483 } catch (ComponentException e) {
484 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.MISSING_PROPERTY_NAME);
489 public void testDeleteForwardingPathsWhenComponentinstanceDeleted() {
491 ComponentTypeEnum containerComponentType = ComponentTypeEnum.findByParamName("services");
492 String containerComponentID = "Service-comp";
493 String componentInstanceID = "NodeA1";
494 Service component = new Service();
496 .setComponentInstances(Arrays.asList(createComponentInstance("NodeA2"), createComponentInstance("NodeB2"),
497 createComponentInstance(componentInstanceID)));
499 component.addForwardingPath(createPath("path1", componentInstanceID, "NodeB1", "1"));
500 component.addForwardingPath(createPath("Path2", "NodeA2", "NodeB2", "2"));
501 when(toscaOperationFacade.getToscaElement(eq(containerComponentID), any(ComponentParametersView.class)))
502 .thenReturn(Either.left(component));
503 when(toscaOperationFacade.getToscaElement(eq(containerComponentID))).thenReturn(Either.left(component));
504 when(forwardingPathOperation.deleteForwardingPath(any(Service.class), anySet()))
505 .thenReturn(Either.left(new HashSet<>()));
506 final ComponentInstance ci = new ComponentInstance();
507 ci.setName(componentInstanceID);
508 ComponentInstance responseFormatEither = componentInstanceBusinessLogic
509 .deleteForwardingPathsRelatedTobeDeletedComponentInstance(
510 containerComponentID, containerComponentType, ci);
511 assertThat(!responseFormatEither.isEmpty()).isEqualTo(true);
516 public void testAddComponentInstanceDeploymentArtifacts() {
518 Component containerComponent = new Service();
519 ComponentInstance componentInstance = new ComponentInstance();
520 componentInstance.setUniqueId(COMPONENT_INSTANCE_ID);
521 Component originComponent = fillOriginComponent(new Resource());
523 Map<String, ArtifactDefinition> artifacts = new HashMap<>();
524 ArtifactDefinition deploymentArtifact1 = getArtifact("deploymentArtifact1", ArtifactTypeEnum.HEAT.getType());
525 artifacts.put(deploymentArtifact1.getArtifactLabel(), deploymentArtifact1);
526 ArtifactDefinition deploymentArtifact2 = getArtifact("deploymentArtifact2",
527 ArtifactTypeEnum.HEAT_ENV.getType());
528 artifacts.put(deploymentArtifact2.getArtifactLabel(), deploymentArtifact2);
529 ArtifactDefinition deploymentArtifact3 = getArtifact("deploymentArtifact3",
530 ArtifactTypeEnum.HEAT_VOL.getType());
531 artifacts.put(deploymentArtifact3.getArtifactLabel(), deploymentArtifact3);
532 ArtifactDefinition heatEnvPlaceHolder = getArtifact("deploymentArtifact4", ArtifactTypeEnum.HEAT_ENV.getType());
533 ArtifactDefinition heatEnvPlaceHolder2 = getArtifact("deploymentArtifact5",
534 ArtifactTypeEnum.HEAT_ENV.getType());
536 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getResourceDeploymentArtifacts = Either
539 Map<String, ArtifactDefinition> finalDeploymentArtifacts = new HashMap<>();
540 finalDeploymentArtifacts.put(deploymentArtifact1.getArtifactLabel(), deploymentArtifact1);
541 finalDeploymentArtifacts.put(deploymentArtifact3.getArtifactLabel(), deploymentArtifact3);
542 finalDeploymentArtifacts.put(heatEnvPlaceHolder.getArtifactLabel(), heatEnvPlaceHolder);
543 finalDeploymentArtifacts.put(heatEnvPlaceHolder2.getArtifactLabel(), heatEnvPlaceHolder2);
545 when(artifactsBusinessLogic.getArtifacts(componentInstance.getComponentUid(), NodeTypeEnum.Resource,
546 ArtifactGroupTypeEnum.DEPLOYMENT, null)).thenReturn(getResourceDeploymentArtifacts);
547 when(artifactsBusinessLogic.createHeatEnvPlaceHolder(new ArrayList<>(),
548 deploymentArtifact1, ArtifactsBusinessLogic.HEAT_ENV_NAME, componentInstance.getUniqueId(),
549 NodeTypeEnum.ResourceInstance, componentInstance.getName(), user, containerComponent,
550 null)).thenReturn(heatEnvPlaceHolder);
551 when(artifactsBusinessLogic.createHeatEnvPlaceHolder(new ArrayList<>(),
552 deploymentArtifact3, ArtifactsBusinessLogic.HEAT_ENV_NAME, componentInstance.getUniqueId(),
553 NodeTypeEnum.ResourceInstance, componentInstance.getName(), user, containerComponent,
554 null)).thenReturn(heatEnvPlaceHolder2);
556 componentInstanceBusinessLogic.setToscaOperationFacade(toscaOperationFacade);
557 when(toscaOperationFacade.addDeploymentArtifactsToInstance(containerComponent.getUniqueId(), componentInstance,
558 finalDeploymentArtifacts)).thenReturn(StorageOperationStatus.OK);
559 when(toscaOperationFacade
560 .addGroupInstancesToComponentInstance(containerComponent, componentInstance, new ArrayList<>(), new HashMap<>()))
561 .thenReturn(StorageOperationStatus.OK);
562 when(toscaOperationFacade
563 .addInformationalArtifactsToInstance(containerComponent.getUniqueId(), componentInstance, null))
564 .thenReturn(StorageOperationStatus.OK);
566 ActionStatus status = componentInstanceBusinessLogic.addComponentInstanceArtifacts(containerComponent,
567 componentInstance, originComponent, user, null);
569 assertThat(status).isEqualTo(ActionStatus.OK);
573 private Component fillOriginComponent(Resource originComponent) {
574 originComponent.setUniqueId("resourceId");
575 originComponent.setUniqueId(ORIGIN_COMPONENT_ID);
576 originComponent.setComponentInstances(Lists.newArrayList(toInstance, fromInstance));
577 originComponent.setComponentType(ComponentTypeEnum.RESOURCE);
578 originComponent.setState(LifecycleStateEnum.CERTIFIED);
579 return originComponent;
582 private ArtifactDefinition getArtifact(String artifactLabel, String artifactType) {
583 ArtifactDefinition artifactDefinition = new ArtifactDefinition();
584 artifactDefinition.setArtifactLabel(artifactLabel);
585 artifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
586 artifactDefinition.setEsId("esId" + artifactLabel);
587 artifactDefinition.setArtifactType(artifactType);
588 artifactDefinition.setArtifactName("artifactName");
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 assertThat(response.isLeft()).isTrue();
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 assertThat(response.isRight()).isTrue();
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 assertNotNull(result);
775 public void testCreateServiceProxy() {
776 ComponentInstanceBusinessLogic testSubject;
777 Either<ComponentInstance, ResponseFormat> result;
780 testSubject = createTestSubject();
781 result = testSubject.createServiceProxy();
782 assertNotNull(result);
786 public void testDeleteServiceProxy() {
787 ComponentInstanceBusinessLogic testSubject;
789 Either<ComponentInstance, ResponseFormat> result;
792 testSubject = createTestSubject();
793 result = testSubject.deleteServiceProxy();
794 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 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 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 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 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 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 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 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 assertNotNull(result);
925 componentTypeEnum = null;
927 .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
929 assertNotNull(result);
932 .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
934 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 assertNotNull(result);
974 componentTypeEnum = null;
976 .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
978 assertNotNull(result);
981 .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
983 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 assertNotNull(result);
1007 componentTypeEnum = null;
1008 result = testSubject
1009 .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1010 groupInstanceId, property, userId);
1011 assertNotNull(result);
1013 result = testSubject
1014 .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1015 groupInstanceId, property, userId);
1016 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 assertNotNull(result);
1038 componentTypeEnum = null;
1039 result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1041 assertNotNull(result);
1043 result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1045 assertNotNull(result);
1049 public void testGetComponentParametersViewForForwardingPath() throws Exception {
1050 ComponentInstanceBusinessLogic testSubject;
1051 ComponentParametersView result;
1054 testSubject = createTestSubject();
1055 result = Deencapsulation.invoke(testSubject, "getComponentParametersViewForForwardingPath");
1056 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 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 assertNotNull(result);
1092 when(toscaOperationFacade.getToscaFullElement(containerComponentId)).thenReturn(Either.left(resource));
1093 result = testSubject.updateInstanceCapabilityProperties(componentTypeEnum, containerComponentId,
1094 componentInstanceUniqueId, capabilityType, capabilityName, properties, userId);
1095 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 assertNotNull(result);
1123 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1124 assertThat(result.isRight()).isTrue();
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 assertNotNull(result);
1149 service.setLastUpdaterUserId(oldServiceLastUpdatedUserId);
1150 assertThat(result.isRight()).isTrue();
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 assertNotNull(result);
1184 service.setLastUpdaterUserId(oldServiceLastUpdatedUserId);
1185 resource.setLifecycleState(oldResourceLifeCycle);
1187 assertThat(result.isLeft()).isFalse();
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 assertNotNull(result);
1230 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1231 service.setLifecycleState(oldLifeCycleState);
1233 assertThat(result.isLeft()).isTrue();
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 assertNotNull(result);
1258 assertThat(result.isLeft()).isTrue();
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 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 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 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 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 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 assertThat(propertyCandidate.isPresent()).isTrue();
1474 assertEquals(propertyCandidate.get().getName(), PROP_NAME);
1478 public void testGetComponentInstancePropertyByPolicyId_failure() {
1479 Optional<ComponentInstanceProperty> propertyCandidate =
1480 getComponentInstanceProperty(NON_EXIST_NAME);
1482 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;