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 when(toscaOperationFacade.addDeploymentArtifactsToInstance(containerComponent.getUniqueId(), componentInstance,
557 finalDeploymentArtifacts)).thenReturn(StorageOperationStatus.OK);
558 when(toscaOperationFacade
559 .addGroupInstancesToComponentInstance(containerComponent, componentInstance, null, new HashMap<>()))
560 .thenReturn(StorageOperationStatus.OK);
561 when(toscaOperationFacade
562 .addInformationalArtifactsToInstance(containerComponent.getUniqueId(), componentInstance, null))
563 .thenReturn(StorageOperationStatus.OK);
565 ActionStatus status = componentInstanceBusinessLogic.addComponentInstanceArtifacts(containerComponent,
566 componentInstance, originComponent, user, null);
568 assertThat(status).isEqualTo(ActionStatus.OK);
572 private Component fillOriginComponent(Resource originComponent) {
573 originComponent.setUniqueId("resourceId");
574 originComponent.setUniqueId(ORIGIN_COMPONENT_ID);
575 originComponent.setComponentInstances(Lists.newArrayList(toInstance, fromInstance));
576 originComponent.setComponentType(ComponentTypeEnum.RESOURCE);
577 originComponent.setState(LifecycleStateEnum.CERTIFIED);
578 return originComponent;
581 private ArtifactDefinition getArtifact(String artifactLabel, String artifactType) {
582 ArtifactDefinition artifactDefinition = new ArtifactDefinition();
583 artifactDefinition.setArtifactLabel(artifactLabel);
584 artifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
585 artifactDefinition.setEsId("esId" + artifactLabel);
586 artifactDefinition.setArtifactType(artifactType);
587 return artifactDefinition;
590 private ComponentInstance createComponentInstance(String path1) {
591 ComponentInstance componentInstance = new ComponentInstance();
592 componentInstance.setName(path1);
593 return componentInstance;
596 private ForwardingPathDataDefinition createPath(String pathName, String fromNode, String toNode, String uniqueId) {
597 ForwardingPathDataDefinition forwardingPath = new ForwardingPathDataDefinition(pathName);
598 forwardingPath.setProtocol("protocol");
599 forwardingPath.setDestinationPortNumber("port");
600 forwardingPath.setUniqueId(uniqueId);
601 ListDataDefinition<ForwardingPathElementDataDefinition> forwardingPathElementListDataDefinition =
602 new ListDataDefinition<>();
603 forwardingPathElementListDataDefinition
604 .add(new ForwardingPathElementDataDefinition(fromNode, toNode, "nodeAcpType", "nodeBcpType",
605 "nodeDcpName", "nodeBcpName"));
606 forwardingPath.setPathElements(forwardingPathElementListDataDefinition);
608 return forwardingPath;
611 private Map<String, ForwardingPathDataDefinition> generateForwardingPath(String componentInstanceID) {
612 ForwardingPathDataDefinition forwardingPath = new ForwardingPathDataDefinition("fpName");
613 String protocol = "protocol";
614 forwardingPath.setProtocol(protocol);
615 forwardingPath.setDestinationPortNumber("DestinationPortNumber");
616 forwardingPath.setUniqueId("FP-ID-1");
617 ListDataDefinition<ForwardingPathElementDataDefinition> forwardingPathElementListDataDefinition =
618 new ListDataDefinition<>();
619 forwardingPathElementListDataDefinition
620 .add(new ForwardingPathElementDataDefinition(componentInstanceID, "nodeB", "nodeA_FORWARDER_CAPABILITY",
621 "nodeBcpType", "nodeDcpName", "nodeBcpName"));
622 forwardingPath.setPathElements(forwardingPathElementListDataDefinition);
623 Map<String, ForwardingPathDataDefinition> forwardingPaths = new HashMap<>();
624 forwardingPaths.put("1122", forwardingPath);
625 return forwardingPaths;
628 @SuppressWarnings("unchecked")
629 private void getServiceRelationByIdSuccess(Component component) {
630 Either<Component, StorageOperationStatus> getComponentRes = Either.left(component);
631 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
632 .thenReturn(getComponentRes);
633 Either<RequirementCapabilityRelDef, ResponseFormat> response = componentInstanceBusinessLogic
634 .getRelationById(COMPONENT_ID,
635 RELATION_ID, USER_ID,
636 component.getComponentType());
637 assertThat(response.isLeft()).isTrue();
640 private void getServiceRelationByIdUserValidationFailure(Component component) {
641 when(userValidations.validateUserExists(eq(USER_ID)))
642 .thenThrow(new ByActionStatusComponentException(ActionStatus.USER_NOT_FOUND));
644 componentInstanceBusinessLogic
645 .getRelationById(COMPONENT_ID, RELATION_ID, USER_ID, component.getComponentType());
646 } catch (ByActionStatusComponentException e) {
647 assertSame(e.getActionStatus(), ActionStatus.USER_NOT_FOUND);
651 private void getRelationByIdComponentNotFoundFailure(Component component) {
652 Either<User, ActionStatus> eitherCreator = Either.left(user);
653 Either<Component, StorageOperationStatus> getComponentRes = Either.right(StorageOperationStatus.NOT_FOUND);
654 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
655 .thenReturn(getComponentRes);
657 Either<RequirementCapabilityRelDef, ResponseFormat> response = componentInstanceBusinessLogic
658 .getRelationById(COMPONENT_ID,
659 RELATION_ID, USER_ID,
660 component.getComponentType());
661 assertThat(response.isRight()).isTrue();
664 private void stubMethods() {
665 when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(user);
667 .convertFromStorageResponse(eq(StorageOperationStatus.GENERAL_ERROR), any(ComponentTypeEnum.class)))
668 .thenReturn(ActionStatus.GENERAL_ERROR);
671 private void createComponents() {
678 private void createResource() {
679 resource = new Resource();
680 resource.setUniqueId(COMPONENT_ID);
681 resource.setComponentInstancesRelations(Lists.newArrayList(relation));
682 resource.setComponentInstances(Lists.newArrayList(toInstance, fromInstance));
683 resource.setCapabilities(toInstance.getCapabilities());
684 resource.setRequirements(fromInstance.getRequirements());
685 resource.setComponentType(ComponentTypeEnum.RESOURCE);
686 resource.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
689 private void createService() {
690 service = new Service();
691 service.setUniqueId(COMPONENT_ID);
692 service.setComponentInstancesRelations(Lists.newArrayList(relation));
693 service.setComponentInstances(Lists.newArrayList(toInstance, fromInstance));
694 service.setCapabilities(toInstance.getCapabilities());
695 service.setRequirements(fromInstance.getRequirements());
696 service.setComponentType(ComponentTypeEnum.SERVICE);
697 service.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
700 private void createInstances() {
701 toInstance = new ComponentInstance();
702 toInstance.setUniqueId(TO_INSTANCE_ID);
703 toInstance.setName(TO_INSTANCE_NAME);
705 fromInstance = new ComponentInstance();
706 fromInstance.setUniqueId(FROM_INSTANCE_ID);
708 capability = new CapabilityDataDefinition();
709 capability.setOwnerId(CAPABILITY_OWNER_ID);
710 capability.setUniqueId(CAPABILITY_UID);
711 capability.setName(CAPABILITY_NAME);
713 Map<String, List<CapabilityDefinition>> capabilities = new HashMap<>();
714 final CapabilityDefinition capabilityDefinition = new CapabilityDefinition(capability);
715 final ArrayList<ComponentInstanceProperty> properties = new ArrayList<>();
716 properties.add(componentInstancePropInput);
717 capabilityDefinition.setProperties(properties);
718 capabilities.put(capability.getName(), Lists.newArrayList(capabilityDefinition));
720 requirement = new RequirementDataDefinition();
721 requirement.setOwnerId(REQUIREMENT_OWNER_ID);
722 requirement.setUniqueId(REQUIREMENT_UID);
723 requirement.setName(REQUIREMENT_NAME);
724 requirement.setRelationship(RELATIONSHIP_TYPE);
726 Map<String, List<RequirementDefinition>> requirements = new HashMap<>();
727 requirements.put(requirement.getCapability(), Lists.newArrayList(new RequirementDefinition(requirement)));
729 toInstance.setCapabilities(capabilities);
730 fromInstance.setRequirements(requirements);
733 private void createRelation() {
735 relation = new RequirementCapabilityRelDef();
736 CapabilityRequirementRelationship relationship = new CapabilityRequirementRelationship();
737 RelationshipInfo relationInfo = new RelationshipInfo();
738 relationInfo.setId(RELATION_ID);
739 relationship.setRelation(relationInfo);
741 relation.setRelationships(Lists.newArrayList(relationship));
742 relation.setToNode(TO_INSTANCE_ID);
743 relation.setFromNode(FROM_INSTANCE_ID);
745 relationInfo.setCapabilityOwnerId(CAPABILITY_OWNER_ID);
746 relationInfo.setCapabilityUid(CAPABILITY_UID);
747 relationInfo.setCapability(CAPABILITY_NAME);
748 relationInfo.setRequirementOwnerId(REQUIREMENT_OWNER_ID);
749 relationInfo.setRequirementUid(REQUIREMENT_UID);
750 relationInfo.setRequirement(REQUIREMENT_NAME);
751 RelationshipImpl relationshipImpl = new RelationshipImpl();
752 relationshipImpl.setType(RELATIONSHIP_TYPE);
753 relationInfo.setRelationships(relationshipImpl);
756 private ComponentInstanceBusinessLogic createTestSubject() {
757 return componentInstanceBusinessLogic;
761 public void testChangeServiceProxyVersion() {
762 ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
764 Either<ComponentInstance, ResponseFormat> result;
767 componentInstanceBusinessLogic = createTestSubject();
768 result = componentInstanceBusinessLogic.changeServiceProxyVersion();
769 assertNotNull(result);
773 public void testCreateServiceProxy() {
774 ComponentInstanceBusinessLogic testSubject;
775 Either<ComponentInstance, ResponseFormat> result;
778 testSubject = createTestSubject();
779 result = testSubject.createServiceProxy();
780 assertNotNull(result);
784 public void testDeleteServiceProxy() {
785 ComponentInstanceBusinessLogic testSubject;
787 Either<ComponentInstance, ResponseFormat> result;
790 testSubject = createTestSubject();
791 result = testSubject.deleteServiceProxy();
792 assertNotNull(result);
796 public void testGetComponentInstanceInputsByInputId() {
797 ComponentInstanceBusinessLogic testSubject;
798 Component component = new Service();
800 List<ComponentInstanceInput> result;
803 testSubject = createTestSubject();
804 result = testSubject.getComponentInstanceInputsByInputId(component, inputId);
805 assertNotNull(result);
809 public void testGetComponentInstancePropertiesByInputId() {
810 ComponentInstanceBusinessLogic testSubject;
811 Component component = new Service();
813 List<ComponentInstanceProperty> result;
816 testSubject = createTestSubject();
817 result = testSubject.getComponentInstancePropertiesByInputId(component, inputId);
818 assertNotNull(result);
822 public void testGetRelationById() {
823 ComponentInstanceBusinessLogic testSubject;
824 String componentId = "";
825 String relationId = "";
826 String userId = user.getUserId();
827 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE_INSTANCE;
828 Either<RequirementCapabilityRelDef, ResponseFormat> result;
831 testSubject = createTestSubject();
832 result = testSubject.getRelationById(componentId, relationId, userId, componentTypeEnum);
833 assertNotNull(result);
837 public void testValidateParent() {
838 ComponentInstanceBusinessLogic testSubject;
840 String nodeTemplateId = "";
844 testSubject = createTestSubject();
845 result = Deencapsulation.invoke(testSubject, "validateParent", new Object[]{resource, nodeTemplateId});
846 assertNotNull(result);
850 public void testGetComponentType() {
851 ComponentInstanceBusinessLogic testSubject;
852 ComponentTypeEnum result;
855 testSubject = createTestSubject();
856 result = Deencapsulation.invoke(testSubject, "getComponentType", new Object[]{ComponentTypeEnum.class});
857 assertNotNull(result);
861 public void testGetNewGroupName() {
862 ComponentInstanceBusinessLogic testSubject;
863 String oldPrefix = "";
864 String newNormailzedPrefix = "";
865 String qualifiedGroupInstanceName = "";
869 testSubject = createTestSubject();
870 result = Deencapsulation.invoke(testSubject, "getNewGroupName",
871 new Object[]{oldPrefix, newNormailzedPrefix, qualifiedGroupInstanceName});
872 assertNotNull(result);
876 public void testUpdateComponentInstanceMetadata_3() {
877 ComponentInstanceBusinessLogic testSubject;
879 ComponentInstance newComponentInstance = null;
880 ComponentInstance result;
883 testSubject = createTestSubject();
884 result = Deencapsulation
885 .invoke(testSubject, "updateComponentInstanceMetadata", new Object[]{toInstance, toInstance});
886 assertNotNull(result);
890 public void testFindRelation() throws Exception {
891 ComponentInstanceBusinessLogic testSubject;
892 String relationId = "";
893 List<RequirementCapabilityRelDef> requirementCapabilityRelations = new ArrayList<>();
894 RequirementCapabilityRelDef result;
897 testSubject = createTestSubject();
898 result = Deencapsulation.invoke(testSubject, "findRelation",
899 new Object[]{relationId, requirementCapabilityRelations});
903 public void testCreateOrUpdatePropertiesValues() throws Exception {
904 ComponentInstanceBusinessLogic testSubject;
905 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
907 String componentId = resource.getUniqueId();
908 String resourceInstanceId = "";
909 List<ComponentInstanceProperty> properties = new ArrayList<>();
910 String userId = user.getUserId();
911 Either<List<ComponentInstanceProperty>, ResponseFormat> result;
913 when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll))
914 .thenReturn(Either.left(resource));
917 testSubject = createTestSubject();
919 .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
921 assertNotNull(result);
923 componentTypeEnum = null;
925 .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
927 assertNotNull(result);
930 .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
932 assertNotNull(result);
936 public void testUpdateCapabilityPropertyOnContainerComponent() throws Exception {
937 ComponentInstanceBusinessLogic testSubject;
938 ComponentInstanceProperty property = new ComponentInstanceProperty();
939 String newValue = "";
942 String capabilityType = "";
943 String capabilityName = "";
944 ResponseFormat result;
947 testSubject = createTestSubject();
948 result = Deencapsulation.invoke(testSubject, "updateCapabilityPropertyOnContainerComponent",
949 new Object[]{property, newValue, resource, toInstance, capabilityType, capabilityName});
953 public void testCreateOrUpdateInstanceInputValues() throws Exception {
954 ComponentInstanceBusinessLogic testSubject;
955 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
957 String componentId = resource.getUniqueId();
958 String resourceInstanceId = "";
959 List<ComponentInstanceInput> inputs = new ArrayList<>();
960 String userId = user.getUserId();
961 Either<List<ComponentInstanceInput>, ResponseFormat> result;
963 when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll))
964 .thenReturn(Either.left(resource));
967 testSubject = createTestSubject();
969 .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
971 assertNotNull(result);
972 componentTypeEnum = null;
974 .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
976 assertNotNull(result);
979 .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
981 assertNotNull(result);
985 public void testCreateOrUpdateGroupInstancePropertyValue() throws Exception {
986 ComponentInstanceBusinessLogic testSubject;
987 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
989 String componentId = resource.getUniqueId();
990 String resourceInstanceId = "";
991 String groupInstanceId = "";
992 ComponentInstanceProperty property = new ComponentInstanceProperty();
993 String userId = user.getUserId();
994 Either<ComponentInstanceProperty, ResponseFormat> result;
996 when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseMetadata))
997 .thenReturn(Either.left(resource));
1000 testSubject = createTestSubject();
1001 result = testSubject
1002 .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1003 groupInstanceId, property, userId);
1004 assertNotNull(result);
1005 componentTypeEnum = null;
1006 result = testSubject
1007 .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1008 groupInstanceId, property, userId);
1009 assertNotNull(result);
1011 result = testSubject
1012 .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1013 groupInstanceId, property, userId);
1014 assertNotNull(result);
1018 public void testDeletePropertyValue() throws Exception {
1019 ComponentInstanceBusinessLogic testSubject;
1020 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1022 String serviceId = service.getUniqueId();
1023 String resourceInstanceId = "";
1024 String propertyValueId = "";
1025 String userId = user.getUserId();
1026 Either<ComponentInstanceProperty, ResponseFormat> result;
1028 when(toscaOperationFacade.getToscaElement(serviceId, JsonParseFlagEnum.ParseMetadata))
1029 .thenReturn(Either.left(service));
1032 testSubject = createTestSubject();
1033 result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1035 assertNotNull(result);
1036 componentTypeEnum = null;
1037 result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1039 assertNotNull(result);
1041 result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1043 assertNotNull(result);
1047 public void testGetComponentParametersViewForForwardingPath() throws Exception {
1048 ComponentInstanceBusinessLogic testSubject;
1049 ComponentParametersView result;
1052 testSubject = createTestSubject();
1053 result = Deencapsulation.invoke(testSubject, "getComponentParametersViewForForwardingPath");
1054 assertNotNull(result);
1058 public void testGetResourceInstanceById() throws Exception {
1059 ComponentInstanceBusinessLogic testSubject;
1061 String instanceId = "";
1062 Either<ComponentInstance, StorageOperationStatus> result;
1065 testSubject = createTestSubject();
1066 result = Deencapsulation.invoke(testSubject, "getResourceInstanceById", new Object[]{resource, instanceId});
1067 assertNotNull(result);
1071 public void testUpdateInstanceCapabilityProperties_1() throws Exception {
1072 ComponentInstanceBusinessLogic testSubject;
1073 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1075 String containerComponentId = resource.getUniqueId();
1076 String componentInstanceUniqueId = "";
1077 String capabilityType = "";
1078 String capabilityName = "";
1079 List<ComponentInstanceProperty> properties = new ArrayList<>();
1080 String userId = user.getUserId();
1081 Either<List<ComponentInstanceProperty>, ResponseFormat> result;
1083 when(toscaOperationFacade.getToscaFullElement(containerComponentId))
1084 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
1086 testSubject = createTestSubject();
1087 result = testSubject.updateInstanceCapabilityProperties(componentTypeEnum, containerComponentId,
1088 componentInstanceUniqueId, capabilityType, capabilityName, properties, userId);
1089 assertNotNull(result);
1090 when(toscaOperationFacade.getToscaFullElement(containerComponentId)).thenReturn(Either.left(resource));
1091 result = testSubject.updateInstanceCapabilityProperties(componentTypeEnum, containerComponentId,
1092 componentInstanceUniqueId, capabilityType, capabilityName, properties, userId);
1093 assertNotNull(result);
1097 public void testCopyComponentInstanceWrongUserId() {
1099 Either<Map<String, ComponentInstance>, ResponseFormat> result;
1100 ComponentInstance inputComponentInstance = createComponetInstanceFromComponent(resource);
1101 String containerComponentId = service.getUniqueId();
1102 String componentInstanceId = resource.getUniqueId();
1103 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1104 service.setLastUpdaterUserId("wrong user id");
1106 Either<Component, StorageOperationStatus> leftServiceOp = Either.left(service);
1107 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(leftServiceOp);
1108 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1109 .thenReturn(leftServiceOp);
1110 when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
1111 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1112 .thenReturn(StorageOperationStatus.OK);
1113 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1114 .thenReturn(StorageOperationStatus.OK);
1116 result = componentInstanceBusinessLogic
1117 .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId,
1119 assertNotNull(result);
1121 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1122 assertThat(result.isRight()).isTrue();
1126 public void testCopyComponentInstanceComponentWrongState() {
1127 Either<Map<String, ComponentInstance>, ResponseFormat> result;
1128 ComponentInstance inputComponentInstance = createComponetInstanceFromComponent(resource);
1129 String containerComponentId = service.getUniqueId();
1130 String componentInstanceId = resource.getUniqueId();
1131 String oldServiceLastUpdatedUserId = service.getLastUpdaterUserId();
1132 service.setLastUpdaterUserId(USER_ID);
1134 Either<Component, StorageOperationStatus> leftServiceOp = Either.left(service);
1135 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(leftServiceOp);
1136 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1137 .thenReturn(leftServiceOp);
1138 when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
1139 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1140 .thenReturn(StorageOperationStatus.OK);
1141 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1142 .thenReturn(StorageOperationStatus.OK);
1143 Either<Component, StorageOperationStatus> getComponentRes = Either.left(resource);
1144 result = componentInstanceBusinessLogic
1145 .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId, USER_ID);
1146 assertNotNull(result);
1147 service.setLastUpdaterUserId(oldServiceLastUpdatedUserId);
1148 assertThat(result.isRight()).isTrue();
1152 public void testCopyComponentInstance() {
1153 Either<Map<String, ComponentInstance>, ResponseFormat> result;
1154 ComponentInstance inputComponentInstance = createComponetInstanceFromComponent(resource);
1155 String containerComponentId = service.getUniqueId();
1156 String componentInstanceId = resource.getUniqueId();
1157 String oldServiceLastUpdatedUserId = service.getLastUpdaterUserId();
1158 service.setLastUpdaterUserId(USER_ID);
1159 LifecycleStateEnum oldResourceLifeCycle = resource.getLifecycleState();
1160 resource.setLifecycleState(LifecycleStateEnum.CERTIFIED);
1162 Either<Component, StorageOperationStatus> leftServiceOp = Either.left(service);
1163 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(leftServiceOp);
1164 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1165 .thenReturn(leftServiceOp);
1166 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1167 .thenReturn(StorageOperationStatus.OK);
1168 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1169 .thenReturn(StorageOperationStatus.OK);
1170 Either<Component, StorageOperationStatus> getComponentRes = Either.left(resource);
1171 ImmutablePair<Component, String> pair = new ImmutablePair<>(resource, TO_INSTANCE_ID);
1172 Either<ImmutablePair<Component, String>, StorageOperationStatus> result2 = Either.left(pair);
1173 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getResourceDeploymentArtifacts = Either
1174 .left(new HashMap<String, ArtifactDefinition>());
1175 StorageOperationStatus artStatus = StorageOperationStatus.OK;
1177 result = componentInstanceBusinessLogic
1178 .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId,
1180 assertNotNull(result);
1182 service.setLastUpdaterUserId(oldServiceLastUpdatedUserId);
1183 resource.setLifecycleState(oldResourceLifeCycle);
1185 assertThat(result.isLeft()).isFalse();
1189 public void testCreateOrUpdateAttributeValueForCopyPaste() {
1190 ComponentInstance serviceComponentInstance = createComponetInstanceFromComponent(service);
1191 ComponentInstanceProperty attribute = new ComponentInstanceProperty();
1192 attribute.setType("string");
1193 attribute.setUniqueId("testCreateOrUpdateAttributeValueForCopyPaste");
1194 SchemaDefinition def = Mockito.mock(SchemaDefinition.class);
1195 attribute.setSchema(def);
1196 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1197 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1198 service.setLastUpdaterUserId(USER_ID);
1199 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1201 Map<String, List<ComponentInstanceProperty>> instAttrsMap =
1202 new HashMap<String, List<ComponentInstanceProperty>>();
1203 List<ComponentInstanceProperty> instAttrsList = new ArrayList<ComponentInstanceProperty>();
1204 ComponentInstanceProperty prop = new ComponentInstanceProperty();
1205 prop.setUniqueId(attribute.getUniqueId());
1206 instAttrsList.add(prop);
1207 instAttrsMap.put(toInstance.getUniqueId(), instAttrsList);
1208 service.setComponentInstancesAttributes(instAttrsMap);
1210 Either<Component, StorageOperationStatus> serviceEitherLeft = Either.left(service);
1211 when(toscaOperationFacade.getToscaElement(serviceComponentInstance.getUniqueId(), JsonParseFlagEnum.ParseAll))
1212 .thenReturn(serviceEitherLeft);
1213 when(toscaOperationFacade.updateComponentInstanceAttribute(service, toInstance.getUniqueId(), attribute))
1214 .thenReturn(StorageOperationStatus.OK);
1215 when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(service))
1216 .thenReturn(serviceEitherLeft);
1218 Either<ComponentInstanceProperty, ResponseFormat> result = Deencapsulation
1219 .invoke(componentInstanceBusinessLogic,
1220 "createOrUpdateAttributeValueForCopyPaste",
1221 ComponentTypeEnum.SERVICE,
1222 serviceComponentInstance
1224 toInstance.getUniqueId(), attribute,
1226 assertNotNull(result);
1228 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1229 service.setLifecycleState(oldLifeCycleState);
1231 assertThat(result.isLeft()).isTrue();
1232 ComponentInstanceProperty resultProp = result.left().value();
1233 assertEquals(resultProp.getPath().size(), 1);
1234 assertEquals(resultProp.getPath().get(0), toInstance.getUniqueId());
1238 public void testUpdateComponentInstanceProperty() {
1240 String containerComponentId = service.getUniqueId();
1241 String componentInstanceId = "dummy_id";
1242 ComponentInstanceProperty property = Mockito.mock(ComponentInstanceProperty.class);
1244 Either<Component, StorageOperationStatus> getComponent = Either.left(service);
1245 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(getComponent);
1246 StorageOperationStatus status = StorageOperationStatus.OK;
1247 when(toscaOperationFacade.updateComponentInstanceProperty(service, componentInstanceId, property))
1248 .thenReturn(status);
1249 Either<Component, StorageOperationStatus> updateContainerRes = Either.left(service);
1250 when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(service))
1251 .thenReturn(updateContainerRes);
1253 Either<String, ResponseFormat> result = Deencapsulation.invoke(componentInstanceBusinessLogic,
1254 "updateComponentInstanceProperty", containerComponentId, componentInstanceId, property);
1255 assertNotNull(result);
1256 assertThat(result.isLeft()).isTrue();
1260 public void testGetInputListDefaultValue() {
1261 Component component = service;
1262 String inputId = "dummy_id";
1263 String defaultValue = "dummy_default_value";
1264 List<InputDefinition> newInputs = new ArrayList<InputDefinition>();
1265 InputDefinition in = new InputDefinition();
1266 in.setUniqueId(inputId);
1267 in.setDefaultValue(defaultValue);
1269 List<InputDefinition> oldInputs = service.getInputs();
1270 service.setInputs(newInputs);
1272 Either<String, ResponseFormat> result =
1273 Deencapsulation.invoke(componentInstanceBusinessLogic, "getInputListDefaultValue", component, inputId);
1275 service.setInputs(oldInputs);
1277 assertEquals(result.left().value(), defaultValue);
1281 public void testBatchDeleteComponentInstanceFailureWrongType() {
1282 Map<String, List<String>> result;
1283 List<String> componentInstanceIdList = new ArrayList<>();
1284 String containerComponentParam = "WRONG_TYPE";
1285 String containerComponentId = "containerComponentId";
1286 String componentInstanceId = "componentInstanceId";
1287 componentInstanceIdList.add(componentInstanceId);
1288 String userId = USER_ID;
1289 Map<String, List<String>> deleteErrorMap = new HashMap<>();
1290 List<String> deleteErrorIds = new ArrayList<>();
1291 deleteErrorIds.add(componentInstanceId);
1292 deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
1293 Either<Component, StorageOperationStatus> cont = Either.left(service);
1294 when(componentsUtils.convertFromStorageResponse(eq(StorageOperationStatus.NOT_FOUND), eq(null)))
1295 .thenReturn(ActionStatus.GENERAL_ERROR);
1296 when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class)))
1300 result = componentInstanceBusinessLogic
1301 .batchDeleteComponentInstance(containerComponentParam, containerComponentId, componentInstanceIdList,
1303 assertNotNull(result);
1304 assertEquals(deleteErrorMap, result);
1305 } catch (ComponentException e) {
1306 assertEquals(e.getActionStatus().toString(), StorageOperationStatus.GENERAL_ERROR.toString());
1311 public void testBatchDeleteComponentInstanceFailureCompIds() {
1312 Map<String, List<String>> result = new HashMap<>();
1313 String containerComponentParam = ComponentTypeEnum.SERVICE_PARAM_NAME;
1314 String containerComponentId = "containerComponentId";
1315 String componentInstanceId = "componentInstanceId";
1316 List<String> componentInstanceIdList = new ArrayList<>();
1317 componentInstanceIdList.add(componentInstanceId);
1318 String userId = USER_ID;
1319 Map<String, List<String>> deleteErrorMap = new HashMap<>();
1320 List<String> deleteErrorIds = new ArrayList<>();
1321 deleteErrorIds.add(componentInstanceId);
1322 deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
1324 Either<Component, StorageOperationStatus> err = Either.right(StorageOperationStatus.GENERAL_ERROR);
1325 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1329 result = componentInstanceBusinessLogic
1330 .batchDeleteComponentInstance(containerComponentParam, containerComponentId, componentInstanceIdList,
1332 assertNotNull(result);
1333 assertEquals(deleteErrorMap, result);
1334 } catch (ComponentException e) {
1335 assertEquals(e.getActionStatus().toString(), StorageOperationStatus.GENERAL_ERROR.toString());
1340 public void testBatchDeleteComponentInstanceSuccess() {
1341 Map<String, List<String>> result;
1342 String containerComponentParam = ComponentTypeEnum.SERVICE_PARAM_NAME;
1343 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1344 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1345 service.setLastUpdaterUserId(USER_ID);
1346 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1347 String containerComponentId = service.getUniqueId();
1348 String componentInstanceId = TO_INSTANCE_ID;
1349 String userId = USER_ID;
1350 List<String> componentInstanceIdList = new ArrayList<>();
1351 componentInstanceIdList.add(componentInstanceId);
1352 Map<String, List<String>> deleteErrorMap = new HashMap<>();
1353 List<String> deleteErrorIds = new ArrayList<>();
1354 deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
1356 Either<Component, StorageOperationStatus> cont = Either.left(service);
1357 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1358 .thenReturn(StorageOperationStatus.OK);
1359 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1360 .thenReturn(StorageOperationStatus.OK);
1361 ImmutablePair<Component, String> pair = new ImmutablePair<>(resource, TO_INSTANCE_ID);
1362 Either<ImmutablePair<Component, String>, StorageOperationStatus> result2 = Either.left(pair);
1363 when(toscaOperationFacade.deleteComponentInstanceFromTopologyTemplate(service, componentInstanceId))
1364 .thenReturn(result2);
1365 when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
1367 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
1369 result = componentInstanceBusinessLogic
1370 .batchDeleteComponentInstance(containerComponentParam, containerComponentId,
1371 componentInstanceIdList, userId);
1372 assertNotNull(result);
1374 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1375 service.setLifecycleState(oldLifeCycleState);
1376 assertEquals(deleteErrorMap, result);
1380 public void testDissociateRIFromRIFailDissociate() {
1382 List<RequirementCapabilityRelDef> result;
1383 RequirementCapabilityRelDef ref = new RequirementCapabilityRelDef();
1384 ref.setFromNode(FROM_INSTANCE_ID);
1385 ref.setToNode(TO_INSTANCE_ID);
1386 List<CapabilityRequirementRelationship> relationships = new ArrayList<>();
1387 CapabilityRequirementRelationship relationship = new CapabilityRequirementRelationship();
1388 RelationshipInfo ri = new RelationshipInfo();
1389 ri.setRequirement(REQUIREMENT_NAME);
1390 relationship.setRelation(ri);
1391 relationships.add(relationship);
1392 ref.setRelationships(relationships);
1393 List<RequirementCapabilityRelDef> requirementDefList = new ArrayList<>();
1394 requirementDefList.add(ref);
1395 ComponentTypeEnum componentTypeEnum = service.getComponentType();
1396 String componentId = service.getUniqueId();
1397 String userId = USER_ID;
1398 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1399 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1400 service.setLastUpdaterUserId(USER_ID);
1401 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1403 Either<Component, StorageOperationStatus> cont = Either.left(service);
1404 when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
1406 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1407 .thenReturn(StorageOperationStatus.OK);
1408 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1409 .thenReturn(StorageOperationStatus.OK);
1410 Either<RequirementCapabilityRelDef, StorageOperationStatus> resultEither;
1411 resultEither = Either.right(StorageOperationStatus.OK);
1412 when(componentsUtils.convertFromStorageResponseForResourceInstance(eq(StorageOperationStatus.OK), eq(true)))
1413 .thenReturn(ActionStatus.GENERAL_ERROR);
1414 when(toscaOperationFacade.dissociateResourceInstances(componentId, ref)).thenReturn(resultEither);
1417 result = componentInstanceBusinessLogic
1418 .batchDissociateRIFromRI(componentId, userId, requirementDefList, componentTypeEnum);
1419 assertNotNull(result);
1420 assertEquals(new ArrayList<>(), result);
1421 } catch (ComponentException e) {
1422 assertEquals(e.getActionStatus().toString(), StorageOperationStatus.GENERAL_ERROR.toString());
1425 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1426 service.setLifecycleState(oldLifeCycleState);
1431 public void testDissociateRIFromRISuccess() {
1433 List<RequirementCapabilityRelDef> result;
1434 RequirementCapabilityRelDef ref = new RequirementCapabilityRelDef();
1435 List<RequirementCapabilityRelDef> requirementDefList = new ArrayList<>();
1436 requirementDefList.add(ref);
1437 ComponentTypeEnum componentTypeEnum = service.getComponentType();
1438 String componentId = service.getUniqueId();
1439 String userId = USER_ID;
1440 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1441 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1442 service.setLastUpdaterUserId(USER_ID);
1443 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1445 Either<Component, StorageOperationStatus> cont = Either.left(service);
1446 when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
1448 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1449 .thenReturn(StorageOperationStatus.OK);
1450 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1451 .thenReturn(StorageOperationStatus.OK);
1452 Either<RequirementCapabilityRelDef, StorageOperationStatus> resultEither;
1453 resultEither = Either.left(ref);
1454 when(toscaOperationFacade.dissociateResourceInstances(componentId, ref)).thenReturn(resultEither);
1456 result = componentInstanceBusinessLogic
1457 .batchDissociateRIFromRI(componentId, userId, requirementDefList, componentTypeEnum);
1458 assertNotNull(result);
1460 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1461 service.setLifecycleState(oldLifeCycleState);
1463 assertEquals(requirementDefList, result);
1467 public void testGetComponentInstancePropertyByPolicyId_success() {
1468 Optional<ComponentInstanceProperty> propertyCandidate =
1469 getComponentInstanceProperty(PROP_NAME);
1471 assertThat(propertyCandidate.isPresent()).isTrue();
1472 assertEquals(propertyCandidate.get().getName(), PROP_NAME);
1476 public void testGetComponentInstancePropertyByPolicyId_failure() {
1477 Optional<ComponentInstanceProperty> propertyCandidate =
1478 getComponentInstanceProperty(NON_EXIST_NAME);
1480 assertEquals(propertyCandidate, Optional.empty());
1483 private Optional<ComponentInstanceProperty> getComponentInstanceProperty(String propertyName) {
1484 ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
1485 componentInstanceProperty.setName(propertyName);
1487 PolicyDefinition policyDefinition = getPolicyDefinition();
1488 componentInstanceProperty.setGetPolicyValues(policyDefinition.getGetPolicyValues());
1490 service.setComponentInstancesProperties(
1491 Collections.singletonMap(COMPONENT_INST_ID, Collections.singletonList(componentInstanceProperty)));
1493 return componentInstanceBusinessLogic.getComponentInstancePropertyByPolicyId(service, policyDefinition);
1496 private PolicyDefinition getPolicyDefinition() {
1497 PolicyDefinition policyDefinition = new PolicyDefinition();
1498 policyDefinition.setInstanceUniqueId(COMPONENT_INST_ID);
1499 policyDefinition.setName(PROP_NAME);
1501 GetPolicyValueDataDefinition getPolicy = new GetPolicyValueDataDefinition();
1502 getPolicy.setPropertyName(PROP_NAME);
1504 List<GetPolicyValueDataDefinition> getPolicies = new ArrayList<>();
1505 getPolicies.add(getPolicy);
1506 policyDefinition.setGetPolicyValues(getPolicies);
1508 return policyDefinition;
1511 private ComponentInstance createComponetInstanceFromComponent(Component component) {
1512 ComponentInstance componentInst = new ComponentInstance();
1513 componentInst.setUniqueId(component.getUniqueId());
1514 componentInst.setComponentUid(component.getUniqueId() + "_test");
1515 componentInst.setPosX("10");
1516 componentInst.setPosY("10");
1517 componentInst.setCapabilities(component.getCapabilities());
1518 componentInst.setRequirements(component.getRequirements());
1519 componentInst.setArtifacts(component.getArtifacts());
1520 componentInst.setDeploymentArtifacts(component.getDeploymentArtifacts());
1521 return componentInst;