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.Assert;
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.mockito.InjectMocks;
51 import org.mockito.Mock;
52 import org.mockito.Mockito;
53 import org.mockito.MockitoAnnotations;
54 import org.mockito.junit.MockitoJUnitRunner;
55 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
56 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
57 import org.openecomp.sdc.be.components.validation.UserValidations;
58 import org.openecomp.sdc.be.config.ConfigurationManager;
59 import org.openecomp.sdc.be.dao.api.ActionStatus;
60 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
61 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
62 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
63 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
64 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
65 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathElementDataDefinition;
66 import org.openecomp.sdc.be.datatypes.elements.GetPolicyValueDataDefinition;
67 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
68 import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition;
69 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
70 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
71 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
72 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
73 import org.openecomp.sdc.be.impl.ComponentsUtils;
74 import org.openecomp.sdc.be.impl.ServletUtils;
75 import org.openecomp.sdc.be.model.ArtifactDefinition;
76 import org.openecomp.sdc.be.model.CapabilityDefinition;
77 import org.openecomp.sdc.be.model.CapabilityRequirementRelationship;
78 import org.openecomp.sdc.be.model.Component;
79 import org.openecomp.sdc.be.model.ComponentInstance;
80 import org.openecomp.sdc.be.model.ComponentInstanceInput;
81 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
82 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
83 import org.openecomp.sdc.be.model.ComponentParametersView;
84 import org.openecomp.sdc.be.model.DataTypeDefinition;
85 import org.openecomp.sdc.be.model.InputDefinition;
86 import org.openecomp.sdc.be.model.LifecycleStateEnum;
87 import org.openecomp.sdc.be.model.PolicyDefinition;
88 import org.openecomp.sdc.be.model.RelationshipImpl;
89 import org.openecomp.sdc.be.model.RelationshipInfo;
90 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
91 import org.openecomp.sdc.be.model.RequirementDefinition;
92 import org.openecomp.sdc.be.model.Resource;
93 import org.openecomp.sdc.be.model.Service;
94 import org.openecomp.sdc.be.model.User;
95 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
96 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ForwardingPathOperation;
97 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
98 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
99 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
100 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
101 import org.openecomp.sdc.be.user.UserBusinessLogic;
102 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
103 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
104 import org.openecomp.sdc.common.api.ConfigurationSource;
105 import org.openecomp.sdc.common.impl.ExternalConfiguration;
106 import org.openecomp.sdc.common.impl.FSConfigurationSource;
107 import org.openecomp.sdc.exception.ResponseFormat;
110 * The test suite designed for test functionality of ComponentInstanceBusinessLogic class
113 @RunWith(MockitoJUnitRunner.class)
114 public class ComponentInstanceBusinessLogicTest {
116 private final static String USER_ID = "jh0003";
117 private final static String COMPONENT_ID = "componentId";
118 private final static String ORIGIN_COMPONENT_ID = "originComponentId";
119 private final static String COMPONENT_INST_ID = "componentInstId";
120 private final static String TO_INSTANCE_ID = "toInstanceId";
121 private final static String TO_INSTANCE_NAME = "toInstanceName";
122 private final static String COMPONENT_INSTANCE_ID = "componentInstanceId";
123 private final static String FROM_INSTANCE_ID = "fromInstanceId";
124 private final static String RELATION_ID = "relationId";
125 private final static String CAPABILITY_OWNER_ID = "capabilityOwnerId";
126 private final static String CAPABILITY_UID = "capabilityUid";
127 private final static String CAPABILITY_NAME = "capabilityName";
128 private final static String REQUIREMENT_OWNER_ID = "requirementOwnerId";
129 private final static String REQUIREMENT_UID = "requirementUid";
130 private final static String REQUIREMENT_NAME = "requirementName";
131 private final static String RELATIONSHIP_TYPE = "relationshipType";
132 private final static String ARTIFACT_1 = "cloudtech_k8s_charts.zip";
133 private final static String ARTIFACT_2 = "cloudtech_azure_day0.zip";
134 private final static String ARTIFACT_3 = "cloudtech_aws_configtemplate.zip";
135 private final static String ARTIFACT_4 = "k8s_charts.zip";
136 private final static String ARTIFACT_5 = "cloudtech_openstack_configtemplate.zip";
137 private final static String PROP_NAME = "propName";
138 private final static String NON_EXIST_NAME = "nonExistName";
140 static ConfigurationSource configurationSource = new FSConfigurationSource(
141 ExternalConfiguration.getChangeListener(),
142 "src/test/resources/config/catalog-be");
143 static ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
146 private static ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
148 private ComponentInstancePropInput componentInstancePropInput;
150 ArtifactsBusinessLogic artifactsBusinessLogic;
152 private ComponentsUtils componentsUtils;
154 private ServletUtils servletUtils;
156 private ResponseFormat responseFormat;
158 private ToscaOperationFacade toscaOperationFacade;
160 private UserBusinessLogic userAdmin;
162 private ForwardingPathOperation forwardingPathOperation;
166 private UserValidations userValidations;
168 GraphLockOperation graphLockOperation;
170 private JanusGraphDao janusGraphDao;
172 ApplicationDataTypeCache dataTypeCache;
174 PropertyOperation propertyOperation;
176 ApplicationDataTypeCache applicationDataTypeCache;
178 private Component service;
179 private Component resource;
180 private ComponentInstance toInstance;
181 private ComponentInstance fromInstance;
182 private CapabilityDataDefinition capability;
183 private RequirementDataDefinition requirement;
184 private RequirementCapabilityRelDef relation;
188 MockitoAnnotations.initMocks(componentInstanceBusinessLogic);
194 public void testGetRelationByIdSuccess() {
195 getServiceRelationByIdSuccess(service);
196 getServiceRelationByIdSuccess(resource);
200 public void testGetRelationByIdUserValidationFailure() {
201 getServiceRelationByIdUserValidationFailure(service);
202 getServiceRelationByIdUserValidationFailure(resource);
206 public void testGetRelationByIdComponentNotFoundFailure() {
207 getRelationByIdComponentNotFoundFailure(service);
208 getRelationByIdComponentNotFoundFailure(resource);
212 public void testForwardingPathOnVersionChange() {
213 getforwardingPathOnVersionChange();
217 public void testIsCloudSpecificArtifact() {
218 assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_1)).isTrue();
219 assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_2)).isTrue();
220 assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_3)).isTrue();
221 assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_4)).isFalse();
222 assertThat(componentInstanceBusinessLogic.isCloudSpecificArtifact(ARTIFACT_5)).isFalse();
225 private void getforwardingPathOnVersionChange() {
226 String containerComponentParam = "services";
227 String containerComponentID = "121-cont";
228 String componentInstanceID = "121-cont-1-comp";
229 Service component = new Service();
230 Map<String, ForwardingPathDataDefinition> forwardingPaths = generateForwardingPath(componentInstanceID);
232 //Add existing componentInstance to component
233 List<ComponentInstance> componentInstanceList = new ArrayList<>();
234 ComponentInstance oldComponentInstance = new ComponentInstance();
235 oldComponentInstance.setName("OLD_COMP_INSTANCE");
236 oldComponentInstance.setUniqueId(componentInstanceID);
237 oldComponentInstance.setName(componentInstanceID);
238 oldComponentInstance.setToscaPresentationValue(JsonPresentationFields.CI_COMPONENT_UID, "1-comp");
239 componentInstanceList.add(oldComponentInstance);
240 component.setComponentInstances(componentInstanceList);
241 component.setForwardingPaths(forwardingPaths);
243 List<ComponentInstance> componentInstanceListNew = new ArrayList<>();
244 ComponentInstance newComponentInstance = new ComponentInstance();
245 String new_Comp_UID = "2-comp";
246 newComponentInstance.setToscaPresentationValue(JsonPresentationFields.CI_COMPONENT_UID, new_Comp_UID);
247 newComponentInstance.setUniqueId(new_Comp_UID);
248 componentInstanceListNew.add(newComponentInstance);
249 Component component2 = new Service();
250 component2.setComponentInstances(componentInstanceListNew);
252 //Mock for getting component
253 when(toscaOperationFacade.getToscaElement(eq(containerComponentID), any(ComponentParametersView.class)))
254 .thenReturn(Either.left(component));
255 when(toscaOperationFacade.validateComponentExists(any(String.class))).thenReturn(Either.left(Boolean.TRUE));
256 when(toscaOperationFacade.getToscaFullElement(eq(new_Comp_UID))).thenReturn(Either.left(component2));
258 Either<Set<String>, ResponseFormat> resultOp = componentInstanceBusinessLogic
259 .forwardingPathOnVersionChange(containerComponentParam,
260 containerComponentID, componentInstanceID,
261 newComponentInstance);
262 assertEquals(1, resultOp.left().value().size());
263 assertEquals("FP-ID-1", resultOp.left().value().iterator().next());
268 public void testCreateOrUpdatePropertiesValues2() {
269 String containerComponentID = "containerId";
270 String resourceInstanceId = "resourceId";
271 String componentInstanceID = "componentInstance";
272 List<ComponentInstanceProperty> properties = new ArrayList<>();
273 ComponentInstanceProperty property = new ComponentInstanceProperty();
274 property.setName("property");
275 property.setValue("newVal");
276 property.setType("string");
277 properties.add(property);
279 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
280 ComponentInstanceProperty origProperty = new ComponentInstanceProperty();
281 origProperty.setName("property");
282 origProperty.setValue("value");
283 origProperty.setType("string");
284 origProperties.add(origProperty);
286 Map<String, DataTypeDefinition> types = new HashMap<>();
287 DataTypeDefinition dataTypeDef = new DataTypeDefinition();
288 types.put("string", dataTypeDef);
290 Component component = new Service();
291 component.setLastUpdaterUserId("userId");
292 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
293 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
294 componentInstanceProps.put("resourceId", origProperties);
295 component.setComponentInstancesProperties(componentInstanceProps);
296 ComponentInstance ci = createComponentInstance("ci1");
297 ci.setUniqueId("resourceId");
298 component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
299 createComponentInstance(componentInstanceID)));
300 HashMap<String, DataTypeDefinition> dataTypeDefinitionHashMap = new HashMap<>();
301 DataTypeDefinition dataTypeDefinition = new DataTypeDefinition();
302 dataTypeDefinition.setName("string");
303 dataTypeDefinitionHashMap.put("string", dataTypeDefinition);
305 //when(userValidations.validateUserExists(user.getUserId(), false)).thenReturn(user);
306 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
307 .thenReturn(Either.left(component));
308 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
309 .thenReturn(StorageOperationStatus.OK);
310 when(dataTypeCache.getAll()).thenReturn(Either.left(types));
311 when(propertyOperation.validateAndUpdatePropertyValue(property.getType(), "newVal", true, null, types))
312 .thenReturn(Either.left("newVal"));
313 when(propertyOperation.validateAndUpdateRules("string", property.getRules(),
314 null, types, true)).thenReturn(ImmutablePair.of("string", null));
315 when(toscaOperationFacade.updateComponentInstanceProperty(component, ci.getUniqueId(),
316 origProperty)).thenReturn(StorageOperationStatus.OK);
317 origProperties.get(0).setValue("newVal");
318 when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(component))
319 .thenReturn(Either.left(component));
320 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
321 when(graphLockOperation.unlockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
322 .thenReturn(StorageOperationStatus.OK);
324 Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = componentInstanceBusinessLogic
325 .createOrUpdatePropertiesValues(
326 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
327 assertThat(responseFormatEither.left().value()).isEqualTo(properties);
331 public void testCreateOrUpdatePropertiesValuesPropertyNotExists() {
332 String containerComponentID = "containerId";
333 String resourceInstanceId = "resourceId";
334 String componentInstanceID = "componentInstance";
335 List<ComponentInstanceProperty> properties = new ArrayList<>();
336 ComponentInstanceProperty property = new ComponentInstanceProperty();
337 property.setName("property");
338 property.setValue("newVal");
339 property.setType("string");
340 properties.add(property);
342 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
344 Map<String, DataTypeDefinition> types = new HashMap<>();
345 DataTypeDefinition dataTypeDef = new DataTypeDefinition();
346 types.put("string", dataTypeDef);
348 Component component = new Service();
349 component.setLastUpdaterUserId("userId");
350 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
351 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
352 componentInstanceProps.put("resourceId", origProperties);
353 component.setComponentInstancesProperties(componentInstanceProps);
354 ComponentInstance ci = createComponentInstance("ci1");
355 ci.setUniqueId("resourceId");
356 component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
357 createComponentInstance(componentInstanceID)));
358 HashMap<String, DataTypeDefinition> dataTypeDefinitionHashMap = new HashMap<>();
359 DataTypeDefinition dataTypeDefinition = new DataTypeDefinition();
360 dataTypeDefinition.setName("string");
361 dataTypeDefinitionHashMap.put("string", dataTypeDefinition);
363 //when(userValidations.validateUserExists(user.getUserId(), false)).thenReturn(user);
364 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
365 .thenReturn(Either.left(component));
366 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
367 .thenReturn(StorageOperationStatus.OK);
368 //when(dataTypeCache.getAll()).thenReturn(Either.left(types));
369 //when (janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
370 when(graphLockOperation.unlockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
371 .thenReturn(StorageOperationStatus.OK);
374 componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
375 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
376 } catch (ComponentException e) {
377 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.PROPERTY_NOT_FOUND);
383 public void testCreateOrUpdatePropertiesValuesValidationFailure() {
384 String containerComponentID = "containerId";
385 String resourceInstanceId = "resourceId";
386 String componentInstanceID = "componentInstance";
387 List<ComponentInstanceProperty> properties = new ArrayList<>();
388 ComponentInstanceProperty property = new ComponentInstanceProperty();
389 property.setName("property");
390 property.setValue("newVal");
391 property.setType("string");
392 properties.add(property);
394 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
395 ComponentInstanceProperty origProperty = new ComponentInstanceProperty();
396 origProperty.setName("property");
397 origProperty.setValue("value");
398 origProperty.setType("string");
399 origProperties.add(origProperty);
401 Map<String, DataTypeDefinition> types = new HashMap<>();
402 DataTypeDefinition dataTypeDef = new DataTypeDefinition();
403 types.put("string", dataTypeDef);
405 Component component = new Service();
406 component.setLastUpdaterUserId("userId");
407 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
408 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
409 componentInstanceProps.put("resourceId", origProperties);
410 component.setComponentInstancesProperties(componentInstanceProps);
411 ComponentInstance ci = createComponentInstance("ci1");
412 ci.setUniqueId("resourceId");
413 component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
414 createComponentInstance(componentInstanceID)));
415 HashMap<String, DataTypeDefinition> dataTypeDefinitionHashMap = new HashMap<>();
416 DataTypeDefinition dataTypeDefinition = new DataTypeDefinition();
417 dataTypeDefinition.setName("string");
418 dataTypeDefinitionHashMap.put("string", dataTypeDefinition);
420 //when(userValidations.validateUserExists(user.getUserId(), false)).thenReturn(user);
421 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
422 .thenReturn(Either.left(component));
423 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
424 .thenReturn(StorageOperationStatus.OK);
425 when(dataTypeCache.getAll()).thenReturn(Either.left(types));
426 when(propertyOperation.validateAndUpdatePropertyValue(property.getType(), "newVal", true, null, types))
427 .thenReturn(Either.right(false));
428 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.BAD_REQUEST))
429 .thenReturn(ActionStatus.INVALID_CONTENT);
432 componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
433 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
434 } catch (ComponentException e) {
435 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.INVALID_CONTENT);
442 public void testCreateOrUpdatePropertiesValuesMissingFieldFailure() {
443 String containerComponentID = "containerId";
444 String resourceInstanceId = "resourceId";
445 String componentInstanceID = "componentInstance";
446 List<ComponentInstanceProperty> properties = new ArrayList<>();
447 ComponentInstanceProperty property = new ComponentInstanceProperty();
448 property.setValue("newVal");
449 property.setType("string");
450 properties.add(property);
452 List<ComponentInstanceProperty> origProperties = new ArrayList<>();
453 ComponentInstanceProperty origProperty = new ComponentInstanceProperty();
454 origProperty.setName("property");
455 origProperty.setValue("value");
456 origProperty.setType("string");
457 origProperties.add(origProperty);
459 Component component = new Service();
460 component.setLastUpdaterUserId("userId");
461 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
462 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
463 componentInstanceProps.put("resourceId", origProperties);
464 component.setComponentInstancesProperties(componentInstanceProps);
465 ComponentInstance ci = createComponentInstance("ci1");
466 ci.setUniqueId("resourceId");
467 component.setComponentInstances(Arrays.asList(ci, createComponentInstance("ci2"),
468 createComponentInstance(componentInstanceID)));
470 HashMap<String, DataTypeDefinition> dataTypeDefinitionHashMap = new HashMap<>();
471 DataTypeDefinition dataTypeDefinition = new DataTypeDefinition();
472 dataTypeDefinition.setName("string");
473 dataTypeDefinitionHashMap.put("string", dataTypeDefinition);
475 //when(userValidations.validateUserExists(user.getUserId(), false)).thenReturn(user);
476 when(toscaOperationFacade.getToscaElement(containerComponentID, JsonParseFlagEnum.ParseAll))
477 .thenReturn(Either.left(component));
478 when(graphLockOperation.lockComponent(containerComponentID, NodeTypeEnum.ResourceInstance))
479 .thenReturn(StorageOperationStatus.OK);
482 componentInstanceBusinessLogic.createOrUpdatePropertiesValues(
483 ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentID, resourceInstanceId, properties, "userId");
484 } catch (ComponentException e) {
485 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.MISSING_PROPERTY_NAME);
490 public void testDeleteForwardingPathsWhenComponentinstanceDeleted() {
492 ComponentTypeEnum containerComponentType = ComponentTypeEnum.findByParamName("services");
493 String containerComponentID = "Service-comp";
494 String componentInstanceID = "NodeA1";
495 Service component = new Service();
497 .setComponentInstances(Arrays.asList(createComponentInstance("NodeA2"), createComponentInstance("NodeB2"),
498 createComponentInstance(componentInstanceID)));
500 component.addForwardingPath(createPath("path1", componentInstanceID, "NodeB1", "1"));
501 component.addForwardingPath(createPath("Path2", "NodeA2", "NodeB2", "2"));
502 when(toscaOperationFacade.getToscaElement(eq(containerComponentID), any(ComponentParametersView.class)))
503 .thenReturn(Either.left(component));
504 when(toscaOperationFacade.getToscaElement(eq(containerComponentID))).thenReturn(Either.left(component));
505 when(forwardingPathOperation.deleteForwardingPath(any(Service.class), anySet()))
506 .thenReturn(Either.left(new HashSet<>()));
507 final ComponentInstance ci = new ComponentInstance();
508 ci.setName(componentInstanceID);
509 ComponentInstance responseFormatEither = componentInstanceBusinessLogic
510 .deleteForwardingPathsRelatedTobeDeletedComponentInstance(
511 containerComponentID, containerComponentType, ci);
512 assertThat(!responseFormatEither.isEmpty()).isEqualTo(true);
517 public void testAddComponentInstanceDeploymentArtifacts() {
519 Component containerComponent = new Service();
520 ComponentInstance componentInstance = new ComponentInstance();
521 componentInstance.setUniqueId(COMPONENT_INSTANCE_ID);
522 Component originComponent = fillOriginComponent(new Resource());
524 Map<String, ArtifactDefinition> artifacts = new HashMap<>();
525 ArtifactDefinition deploymentArtifact1 = getArtifact("deploymentArtifact1", ArtifactTypeEnum.HEAT.getType());
526 artifacts.put(deploymentArtifact1.getArtifactLabel(), deploymentArtifact1);
527 ArtifactDefinition deploymentArtifact2 = getArtifact("deploymentArtifact2",
528 ArtifactTypeEnum.HEAT_ENV.getType());
529 artifacts.put(deploymentArtifact2.getArtifactLabel(), deploymentArtifact2);
530 ArtifactDefinition deploymentArtifact3 = getArtifact("deploymentArtifact3",
531 ArtifactTypeEnum.HEAT_VOL.getType());
532 artifacts.put(deploymentArtifact3.getArtifactLabel(), deploymentArtifact3);
533 ArtifactDefinition heatEnvPlaceHolder = getArtifact("deploymentArtifact4", ArtifactTypeEnum.HEAT_ENV.getType());
534 ArtifactDefinition heatEnvPlaceHolder2 = getArtifact("deploymentArtifact5",
535 ArtifactTypeEnum.HEAT_ENV.getType());
537 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getResourceDeploymentArtifacts = Either
540 Map<String, ArtifactDefinition> finalDeploymentArtifacts = new HashMap<>();
541 finalDeploymentArtifacts.put(deploymentArtifact1.getArtifactLabel(), deploymentArtifact1);
542 finalDeploymentArtifacts.put(deploymentArtifact3.getArtifactLabel(), deploymentArtifact3);
543 finalDeploymentArtifacts.put(heatEnvPlaceHolder.getArtifactLabel(), heatEnvPlaceHolder);
544 finalDeploymentArtifacts.put(heatEnvPlaceHolder2.getArtifactLabel(), heatEnvPlaceHolder2);
546 when(artifactsBusinessLogic.getArtifacts(componentInstance.getComponentUid(), NodeTypeEnum.Resource,
547 ArtifactGroupTypeEnum.DEPLOYMENT, null)).thenReturn(getResourceDeploymentArtifacts);
548 when(artifactsBusinessLogic.createHeatEnvPlaceHolder(new ArrayList<>(),
549 deploymentArtifact1, ArtifactsBusinessLogic.HEAT_ENV_NAME, componentInstance.getUniqueId(),
550 NodeTypeEnum.ResourceInstance, componentInstance.getName(), user, containerComponent,
551 null)).thenReturn(heatEnvPlaceHolder);
552 when(artifactsBusinessLogic.createHeatEnvPlaceHolder(new ArrayList<>(),
553 deploymentArtifact3, ArtifactsBusinessLogic.HEAT_ENV_NAME, componentInstance.getUniqueId(),
554 NodeTypeEnum.ResourceInstance, componentInstance.getName(), user, containerComponent,
555 null)).thenReturn(heatEnvPlaceHolder2);
557 when(toscaOperationFacade.addDeploymentArtifactsToInstance(containerComponent.getUniqueId(), componentInstance,
558 finalDeploymentArtifacts)).thenReturn(StorageOperationStatus.OK);
559 when(toscaOperationFacade
560 .addGroupInstancesToComponentInstance(containerComponent, componentInstance, null, 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 return artifactDefinition;
591 private ComponentInstance createComponentInstance(String path1) {
592 ComponentInstance componentInstance = new ComponentInstance();
593 componentInstance.setName(path1);
594 return componentInstance;
597 private ForwardingPathDataDefinition createPath(String pathName, String fromNode, String toNode, String uniqueId) {
598 ForwardingPathDataDefinition forwardingPath = new ForwardingPathDataDefinition(pathName);
599 forwardingPath.setProtocol("protocol");
600 forwardingPath.setDestinationPortNumber("port");
601 forwardingPath.setUniqueId(uniqueId);
602 ListDataDefinition<ForwardingPathElementDataDefinition> forwardingPathElementListDataDefinition =
603 new ListDataDefinition<>();
604 forwardingPathElementListDataDefinition
605 .add(new ForwardingPathElementDataDefinition(fromNode, toNode, "nodeAcpType", "nodeBcpType",
606 "nodeDcpName", "nodeBcpName"));
607 forwardingPath.setPathElements(forwardingPathElementListDataDefinition);
609 return forwardingPath;
612 private Map<String, ForwardingPathDataDefinition> generateForwardingPath(String componentInstanceID) {
613 ForwardingPathDataDefinition forwardingPath = new ForwardingPathDataDefinition("fpName");
614 String protocol = "protocol";
615 forwardingPath.setProtocol(protocol);
616 forwardingPath.setDestinationPortNumber("DestinationPortNumber");
617 forwardingPath.setUniqueId("FP-ID-1");
618 ListDataDefinition<ForwardingPathElementDataDefinition> forwardingPathElementListDataDefinition =
619 new ListDataDefinition<>();
620 forwardingPathElementListDataDefinition
621 .add(new ForwardingPathElementDataDefinition(componentInstanceID, "nodeB", "nodeA_FORWARDER_CAPABILITY",
622 "nodeBcpType", "nodeDcpName", "nodeBcpName"));
623 forwardingPath.setPathElements(forwardingPathElementListDataDefinition);
624 Map<String, ForwardingPathDataDefinition> forwardingPaths = new HashMap<>();
625 forwardingPaths.put("1122", forwardingPath);
626 return forwardingPaths;
629 @SuppressWarnings("unchecked")
630 private void getServiceRelationByIdSuccess(Component component) {
631 Either<Component, StorageOperationStatus> getComponentRes = Either.left(component);
632 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
633 .thenReturn(getComponentRes);
634 Either<RequirementCapabilityRelDef, ResponseFormat> response = componentInstanceBusinessLogic
635 .getRelationById(COMPONENT_ID,
636 RELATION_ID, USER_ID,
637 component.getComponentType());
638 assertThat(response.isLeft()).isTrue();
641 private void getServiceRelationByIdUserValidationFailure(Component component) {
642 when(userValidations.validateUserExists(eq(USER_ID)))
643 .thenThrow(new ByActionStatusComponentException(ActionStatus.USER_NOT_FOUND));
645 componentInstanceBusinessLogic
646 .getRelationById(COMPONENT_ID, RELATION_ID, USER_ID, component.getComponentType());
647 } catch (ByActionStatusComponentException e) {
648 assertSame(e.getActionStatus(), ActionStatus.USER_NOT_FOUND);
652 private void getRelationByIdComponentNotFoundFailure(Component component) {
653 Either<User, ActionStatus> eitherCreator = Either.left(user);
654 Either<Component, StorageOperationStatus> getComponentRes = Either.right(StorageOperationStatus.NOT_FOUND);
655 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
656 .thenReturn(getComponentRes);
658 Either<RequirementCapabilityRelDef, ResponseFormat> response = componentInstanceBusinessLogic
659 .getRelationById(COMPONENT_ID,
660 RELATION_ID, USER_ID,
661 component.getComponentType());
662 assertThat(response.isRight()).isTrue();
665 private void stubMethods() {
666 when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(user);
668 .convertFromStorageResponse(eq(StorageOperationStatus.GENERAL_ERROR), any(ComponentTypeEnum.class)))
669 .thenReturn(ActionStatus.GENERAL_ERROR);
672 private void createComponents() {
679 private void createResource() {
680 resource = new Resource();
681 resource.setUniqueId(COMPONENT_ID);
682 resource.setComponentInstancesRelations(Lists.newArrayList(relation));
683 resource.setComponentInstances(Lists.newArrayList(toInstance, fromInstance));
684 resource.setCapabilities(toInstance.getCapabilities());
685 resource.setRequirements(fromInstance.getRequirements());
686 resource.setComponentType(ComponentTypeEnum.RESOURCE);
687 resource.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
690 private void createService() {
691 service = new Service();
692 service.setUniqueId(COMPONENT_ID);
693 service.setComponentInstancesRelations(Lists.newArrayList(relation));
694 service.setComponentInstances(Lists.newArrayList(toInstance, fromInstance));
695 service.setCapabilities(toInstance.getCapabilities());
696 service.setRequirements(fromInstance.getRequirements());
697 service.setComponentType(ComponentTypeEnum.SERVICE);
698 service.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
701 private void createInstances() {
702 toInstance = new ComponentInstance();
703 toInstance.setUniqueId(TO_INSTANCE_ID);
704 toInstance.setName(TO_INSTANCE_NAME);
706 fromInstance = new ComponentInstance();
707 fromInstance.setUniqueId(FROM_INSTANCE_ID);
709 capability = new CapabilityDataDefinition();
710 capability.setOwnerId(CAPABILITY_OWNER_ID);
711 capability.setUniqueId(CAPABILITY_UID);
712 capability.setName(CAPABILITY_NAME);
714 Map<String, List<CapabilityDefinition>> capabilities = new HashMap<>();
715 final CapabilityDefinition capabilityDefinition = new CapabilityDefinition(capability);
716 final ArrayList<ComponentInstanceProperty> properties = new ArrayList<>();
717 properties.add(componentInstancePropInput);
718 capabilityDefinition.setProperties(properties);
719 capabilities.put(capability.getName(), Lists.newArrayList(capabilityDefinition));
721 requirement = new RequirementDataDefinition();
722 requirement.setOwnerId(REQUIREMENT_OWNER_ID);
723 requirement.setUniqueId(REQUIREMENT_UID);
724 requirement.setName(REQUIREMENT_NAME);
725 requirement.setRelationship(RELATIONSHIP_TYPE);
727 Map<String, List<RequirementDefinition>> requirements = new HashMap<>();
728 requirements.put(requirement.getCapability(), Lists.newArrayList(new RequirementDefinition(requirement)));
730 toInstance.setCapabilities(capabilities);
731 fromInstance.setRequirements(requirements);
734 private void createRelation() {
736 relation = new RequirementCapabilityRelDef();
737 CapabilityRequirementRelationship relationship = new CapabilityRequirementRelationship();
738 RelationshipInfo relationInfo = new RelationshipInfo();
739 relationInfo.setId(RELATION_ID);
740 relationship.setRelation(relationInfo);
742 relation.setRelationships(Lists.newArrayList(relationship));
743 relation.setToNode(TO_INSTANCE_ID);
744 relation.setFromNode(FROM_INSTANCE_ID);
746 relationInfo.setCapabilityOwnerId(CAPABILITY_OWNER_ID);
747 relationInfo.setCapabilityUid(CAPABILITY_UID);
748 relationInfo.setCapability(CAPABILITY_NAME);
749 relationInfo.setRequirementOwnerId(REQUIREMENT_OWNER_ID);
750 relationInfo.setRequirementUid(REQUIREMENT_UID);
751 relationInfo.setRequirement(REQUIREMENT_NAME);
752 RelationshipImpl relationshipImpl = new RelationshipImpl();
753 relationshipImpl.setType(RELATIONSHIP_TYPE);
754 relationInfo.setRelationships(relationshipImpl);
757 private ComponentInstanceBusinessLogic createTestSubject() {
758 return componentInstanceBusinessLogic;
762 public void testChangeServiceProxyVersion() {
763 ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
765 Either<ComponentInstance, ResponseFormat> result;
768 componentInstanceBusinessLogic = createTestSubject();
769 result = componentInstanceBusinessLogic.changeServiceProxyVersion();
770 assertNotNull(result);
774 public void testCreateServiceProxy() {
775 ComponentInstanceBusinessLogic testSubject;
776 Either<ComponentInstance, ResponseFormat> result;
779 testSubject = createTestSubject();
780 result = testSubject.createServiceProxy();
781 assertNotNull(result);
785 public void testDeleteServiceProxy() {
786 ComponentInstanceBusinessLogic testSubject;
788 Either<ComponentInstance, ResponseFormat> result;
791 testSubject = createTestSubject();
792 result = testSubject.deleteServiceProxy();
793 assertNotNull(result);
797 public void testGetComponentInstanceInputsByInputId() {
798 ComponentInstanceBusinessLogic testSubject;
799 Component component = new Service();
801 List<ComponentInstanceInput> result;
804 testSubject = createTestSubject();
805 result = testSubject.getComponentInstanceInputsByInputId(component, inputId);
806 assertNotNull(result);
810 public void testGetComponentInstancePropertiesByInputId() {
811 ComponentInstanceBusinessLogic testSubject;
812 Component component = new Service();
814 List<ComponentInstanceProperty> result;
817 testSubject = createTestSubject();
818 result = testSubject.getComponentInstancePropertiesByInputId(component, inputId);
819 assertNotNull(result);
823 public void testGetRelationById() {
824 ComponentInstanceBusinessLogic testSubject;
825 String componentId = "";
826 String relationId = "";
827 String userId = user.getUserId();
828 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE_INSTANCE;
829 Either<RequirementCapabilityRelDef, ResponseFormat> result;
832 testSubject = createTestSubject();
833 result = testSubject.getRelationById(componentId, relationId, userId, componentTypeEnum);
834 assertNotNull(result);
838 public void testValidateParent() {
839 ComponentInstanceBusinessLogic testSubject;
841 String nodeTemplateId = "";
845 testSubject = createTestSubject();
846 result = Deencapsulation.invoke(testSubject, "validateParent", new Object[]{resource, nodeTemplateId});
847 assertNotNull(result);
851 public void testGetComponentType() {
852 ComponentInstanceBusinessLogic testSubject;
853 ComponentTypeEnum result;
856 testSubject = createTestSubject();
857 result = Deencapsulation.invoke(testSubject, "getComponentType", new Object[]{ComponentTypeEnum.class});
858 assertNotNull(result);
862 public void testGetNewGroupName() {
863 ComponentInstanceBusinessLogic testSubject;
864 String oldPrefix = "";
865 String newNormailzedPrefix = "";
866 String qualifiedGroupInstanceName = "";
870 testSubject = createTestSubject();
871 result = Deencapsulation.invoke(testSubject, "getNewGroupName",
872 new Object[]{oldPrefix, newNormailzedPrefix, qualifiedGroupInstanceName});
873 assertNotNull(result);
877 public void testUpdateComponentInstanceMetadata_3() {
878 ComponentInstanceBusinessLogic testSubject;
880 ComponentInstance newComponentInstance = null;
881 ComponentInstance result;
884 testSubject = createTestSubject();
885 result = Deencapsulation
886 .invoke(testSubject, "updateComponentInstanceMetadata", new Object[]{toInstance, toInstance});
887 assertNotNull(result);
891 public void testFindRelation() throws Exception {
892 ComponentInstanceBusinessLogic testSubject;
893 String relationId = "";
894 List<RequirementCapabilityRelDef> requirementCapabilityRelations = new ArrayList<>();
895 RequirementCapabilityRelDef result;
898 testSubject = createTestSubject();
899 result = Deencapsulation.invoke(testSubject, "findRelation",
900 new Object[]{relationId, requirementCapabilityRelations});
904 public void testCreateOrUpdatePropertiesValues() throws Exception {
905 ComponentInstanceBusinessLogic testSubject;
906 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
908 String componentId = resource.getUniqueId();
909 String resourceInstanceId = "";
910 List<ComponentInstanceProperty> properties = new ArrayList<>();
911 String userId = user.getUserId();
912 Either<List<ComponentInstanceProperty>, ResponseFormat> result;
914 when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll))
915 .thenReturn(Either.left(resource));
918 testSubject = createTestSubject();
920 .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
922 assertNotNull(result);
924 componentTypeEnum = null;
926 .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
928 assertNotNull(result);
931 .createOrUpdatePropertiesValues(componentTypeEnum, componentId, resourceInstanceId, properties,
933 assertNotNull(result);
937 public void testUpdateCapabilityPropertyOnContainerComponent() throws Exception {
938 ComponentInstanceBusinessLogic testSubject;
939 ComponentInstanceProperty property = new ComponentInstanceProperty();
940 String newValue = "";
943 String capabilityType = "";
944 String capabilityName = "";
945 ResponseFormat result;
948 testSubject = createTestSubject();
949 result = Deencapsulation.invoke(testSubject, "updateCapabilityPropertyOnContainerComponent",
950 new Object[]{property, newValue, resource, toInstance, capabilityType, capabilityName});
954 public void testCreateOrUpdateInstanceInputValues() throws Exception {
955 ComponentInstanceBusinessLogic testSubject;
956 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
958 String componentId = resource.getUniqueId();
959 String resourceInstanceId = "";
960 List<ComponentInstanceInput> inputs = new ArrayList<>();
961 String userId = user.getUserId();
962 Either<List<ComponentInstanceInput>, ResponseFormat> result;
964 when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll))
965 .thenReturn(Either.left(resource));
968 testSubject = createTestSubject();
970 .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
972 assertNotNull(result);
973 componentTypeEnum = null;
975 .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
977 assertNotNull(result);
980 .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, resourceInstanceId, inputs,
982 assertNotNull(result);
986 public void testCreateOrUpdateGroupInstancePropertyValue() throws Exception {
987 ComponentInstanceBusinessLogic testSubject;
988 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
990 String componentId = resource.getUniqueId();
991 String resourceInstanceId = "";
992 String groupInstanceId = "";
993 ComponentInstanceProperty property = new ComponentInstanceProperty();
994 String userId = user.getUserId();
995 Either<ComponentInstanceProperty, ResponseFormat> result;
997 when(toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseMetadata))
998 .thenReturn(Either.left(resource));
1001 testSubject = createTestSubject();
1002 result = testSubject
1003 .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1004 groupInstanceId, property, userId);
1005 assertNotNull(result);
1006 componentTypeEnum = null;
1007 result = testSubject
1008 .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1009 groupInstanceId, property, userId);
1010 assertNotNull(result);
1012 result = testSubject
1013 .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, resourceInstanceId,
1014 groupInstanceId, property, userId);
1015 assertNotNull(result);
1019 public void testDeletePropertyValue() throws Exception {
1020 ComponentInstanceBusinessLogic testSubject;
1021 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1023 String serviceId = service.getUniqueId();
1024 String resourceInstanceId = "";
1025 String propertyValueId = "";
1026 String userId = user.getUserId();
1027 Either<ComponentInstanceProperty, ResponseFormat> result;
1029 when(toscaOperationFacade.getToscaElement(serviceId, JsonParseFlagEnum.ParseMetadata))
1030 .thenReturn(Either.left(service));
1033 testSubject = createTestSubject();
1034 result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1036 assertNotNull(result);
1037 componentTypeEnum = null;
1038 result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1040 assertNotNull(result);
1042 result = testSubject.deletePropertyValue(componentTypeEnum, serviceId, resourceInstanceId, propertyValueId,
1044 assertNotNull(result);
1048 public void testGetComponentParametersViewForForwardingPath() throws Exception {
1049 ComponentInstanceBusinessLogic testSubject;
1050 ComponentParametersView result;
1053 testSubject = createTestSubject();
1054 result = Deencapsulation.invoke(testSubject, "getComponentParametersViewForForwardingPath");
1055 assertNotNull(result);
1059 public void testGetResourceInstanceById() throws Exception {
1060 ComponentInstanceBusinessLogic testSubject;
1062 String instanceId = "";
1063 Either<ComponentInstance, StorageOperationStatus> result;
1066 testSubject = createTestSubject();
1067 result = Deencapsulation.invoke(testSubject, "getResourceInstanceById", new Object[]{resource, instanceId});
1068 assertNotNull(result);
1072 public void testUpdateInstanceCapabilityProperties_1() throws Exception {
1073 ComponentInstanceBusinessLogic testSubject;
1074 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
1076 String containerComponentId = resource.getUniqueId();
1077 String componentInstanceUniqueId = "";
1078 String capabilityType = "";
1079 String capabilityName = "";
1080 List<ComponentInstanceProperty> properties = new ArrayList<>();
1081 String userId = user.getUserId();
1082 Either<List<ComponentInstanceProperty>, ResponseFormat> result;
1084 when(toscaOperationFacade.getToscaFullElement(containerComponentId))
1085 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
1087 testSubject = createTestSubject();
1088 result = testSubject.updateInstanceCapabilityProperties(componentTypeEnum, containerComponentId,
1089 componentInstanceUniqueId, capabilityType, capabilityName, properties, userId);
1090 assertNotNull(result);
1091 when(toscaOperationFacade.getToscaFullElement(containerComponentId)).thenReturn(Either.left(resource));
1092 result = testSubject.updateInstanceCapabilityProperties(componentTypeEnum, containerComponentId,
1093 componentInstanceUniqueId, capabilityType, capabilityName, properties, userId);
1094 assertNotNull(result);
1098 public void testCopyComponentInstanceWrongUserId() {
1100 Either<Map<String, ComponentInstance>, ResponseFormat> result;
1101 ComponentInstance inputComponentInstance = createComponetInstanceFromComponent(resource);
1102 String containerComponentId = service.getUniqueId();
1103 String componentInstanceId = resource.getUniqueId();
1104 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1105 service.setLastUpdaterUserId("wrong user id");
1107 Either<Component, StorageOperationStatus> leftServiceOp = Either.left(service);
1108 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(leftServiceOp);
1109 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1110 .thenReturn(leftServiceOp);
1111 when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
1112 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1113 .thenReturn(StorageOperationStatus.OK);
1114 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1115 .thenReturn(StorageOperationStatus.OK);
1117 result = componentInstanceBusinessLogic
1118 .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId,
1120 assertNotNull(result);
1122 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1123 assertThat(result.isRight()).isTrue();
1127 public void testCopyComponentInstanceComponentWrongState() {
1128 Either<Map<String, ComponentInstance>, ResponseFormat> result;
1129 ComponentInstance inputComponentInstance = createComponetInstanceFromComponent(resource);
1130 String containerComponentId = service.getUniqueId();
1131 String componentInstanceId = resource.getUniqueId();
1132 String oldServiceLastUpdatedUserId = service.getLastUpdaterUserId();
1133 service.setLastUpdaterUserId(USER_ID);
1135 Either<Component, StorageOperationStatus> leftServiceOp = Either.left(service);
1136 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(leftServiceOp);
1137 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1138 .thenReturn(leftServiceOp);
1139 when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
1140 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1141 .thenReturn(StorageOperationStatus.OK);
1142 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1143 .thenReturn(StorageOperationStatus.OK);
1144 Either<Component, StorageOperationStatus> getComponentRes = Either.left(resource);
1145 result = componentInstanceBusinessLogic
1146 .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId, USER_ID);
1147 assertNotNull(result);
1148 service.setLastUpdaterUserId(oldServiceLastUpdatedUserId);
1149 assertThat(result.isRight()).isTrue();
1153 public void testCopyComponentInstance() {
1154 Either<Map<String, ComponentInstance>, ResponseFormat> result;
1155 ComponentInstance inputComponentInstance = createComponetInstanceFromComponent(resource);
1156 String containerComponentId = service.getUniqueId();
1157 String componentInstanceId = resource.getUniqueId();
1158 String oldServiceLastUpdatedUserId = service.getLastUpdaterUserId();
1159 service.setLastUpdaterUserId(USER_ID);
1160 LifecycleStateEnum oldResourceLifeCycle = resource.getLifecycleState();
1161 resource.setLifecycleState(LifecycleStateEnum.CERTIFIED);
1163 Either<Component, StorageOperationStatus> leftServiceOp = Either.left(service);
1164 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(leftServiceOp);
1165 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1166 .thenReturn(leftServiceOp);
1167 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1168 .thenReturn(StorageOperationStatus.OK);
1169 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1170 .thenReturn(StorageOperationStatus.OK);
1171 Either<Component, StorageOperationStatus> getComponentRes = Either.left(resource);
1172 ImmutablePair<Component, String> pair = new ImmutablePair<>(resource, TO_INSTANCE_ID);
1173 Either<ImmutablePair<Component, String>, StorageOperationStatus> result2 = Either.left(pair);
1174 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getResourceDeploymentArtifacts = Either
1175 .left(new HashMap<String, ArtifactDefinition>());
1176 StorageOperationStatus artStatus = StorageOperationStatus.OK;
1178 result = componentInstanceBusinessLogic
1179 .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId,
1181 assertNotNull(result);
1183 service.setLastUpdaterUserId(oldServiceLastUpdatedUserId);
1184 resource.setLifecycleState(oldResourceLifeCycle);
1186 assertThat(result.isLeft());
1190 public void testCreateOrUpdateAttributeValueForCopyPaste() {
1191 ComponentInstance serviceComponentInstance = createComponetInstanceFromComponent(service);
1192 ComponentInstanceProperty attribute = new ComponentInstanceProperty();
1193 attribute.setType("string");
1194 attribute.setUniqueId("testCreateOrUpdateAttributeValueForCopyPaste");
1195 SchemaDefinition def = Mockito.mock(SchemaDefinition.class);
1196 attribute.setSchema(def);
1197 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1198 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1199 service.setLastUpdaterUserId(USER_ID);
1200 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1202 Map<String, List<ComponentInstanceProperty>> instAttrsMap =
1203 new HashMap<String, List<ComponentInstanceProperty>>();
1204 List<ComponentInstanceProperty> instAttrsList = new ArrayList<ComponentInstanceProperty>();
1205 ComponentInstanceProperty prop = new ComponentInstanceProperty();
1206 prop.setUniqueId(attribute.getUniqueId());
1207 instAttrsList.add(prop);
1208 instAttrsMap.put(toInstance.getUniqueId(), instAttrsList);
1209 service.setComponentInstancesAttributes(instAttrsMap);
1211 Either<Component, StorageOperationStatus> serviceEitherLeft = Either.left(service);
1212 when(toscaOperationFacade.getToscaElement(serviceComponentInstance.getUniqueId(), JsonParseFlagEnum.ParseAll))
1213 .thenReturn(serviceEitherLeft);
1214 when(toscaOperationFacade.updateComponentInstanceAttribute(service, toInstance.getUniqueId(), attribute))
1215 .thenReturn(StorageOperationStatus.OK);
1216 when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(service))
1217 .thenReturn(serviceEitherLeft);
1219 Either<ComponentInstanceProperty, ResponseFormat> result = Deencapsulation
1220 .invoke(componentInstanceBusinessLogic,
1221 "createOrUpdateAttributeValueForCopyPaste",
1222 ComponentTypeEnum.SERVICE,
1223 serviceComponentInstance
1225 toInstance.getUniqueId(), attribute,
1227 assertNotNull(result);
1229 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1230 service.setLifecycleState(oldLifeCycleState);
1232 assertThat(result.isLeft()).isTrue();
1233 ComponentInstanceProperty resultProp = result.left().value();
1234 assertEquals(resultProp.getPath().size(), 1);
1235 assertEquals(resultProp.getPath().get(0), toInstance.getUniqueId());
1239 public void testUpdateComponentInstanceProperty() {
1241 String containerComponentId = service.getUniqueId();
1242 String componentInstanceId = "dummy_id";
1243 ComponentInstanceProperty property = Mockito.mock(ComponentInstanceProperty.class);
1245 Either<Component, StorageOperationStatus> getComponent = Either.left(service);
1246 when(toscaOperationFacade.getToscaElement(containerComponentId)).thenReturn(getComponent);
1247 StorageOperationStatus status = StorageOperationStatus.OK;
1248 when(toscaOperationFacade.updateComponentInstanceProperty(service, componentInstanceId, property))
1249 .thenReturn(status);
1250 Either<Component, StorageOperationStatus> updateContainerRes = Either.left(service);
1251 when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(service))
1252 .thenReturn(updateContainerRes);
1254 Either<String, ResponseFormat> result = Deencapsulation.invoke(componentInstanceBusinessLogic,
1255 "updateComponentInstanceProperty", containerComponentId, componentInstanceId, property);
1256 assertNotNull(result);
1257 assertThat(result.isLeft()).isTrue();
1261 public void testGetInputListDefaultValue() {
1262 Component component = service;
1263 String inputId = "dummy_id";
1264 String defaultValue = "dummy_default_value";
1265 List<InputDefinition> newInputs = new ArrayList<InputDefinition>();
1266 InputDefinition in = new InputDefinition();
1267 in.setUniqueId(inputId);
1268 in.setDefaultValue(defaultValue);
1270 List<InputDefinition> oldInputs = service.getInputs();
1271 service.setInputs(newInputs);
1273 Either<String, ResponseFormat> result =
1274 Deencapsulation.invoke(componentInstanceBusinessLogic, "getInputListDefaultValue", component, inputId);
1276 service.setInputs(oldInputs);
1278 assertEquals(result.left().value(), defaultValue);
1282 public void testBatchDeleteComponentInstanceFailureWrongType() {
1283 Map<String, List<String>> result;
1284 List<String> componentInstanceIdList = new ArrayList<>();
1285 String containerComponentParam = "WRONG_TYPE";
1286 String containerComponentId = "containerComponentId";
1287 String componentInstanceId = "componentInstanceId";
1288 componentInstanceIdList.add(componentInstanceId);
1289 String userId = USER_ID;
1290 Map<String, List<String>> deleteErrorMap = new HashMap<>();
1291 List<String> deleteErrorIds = new ArrayList<>();
1292 deleteErrorIds.add(componentInstanceId);
1293 deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
1294 Either<Component, StorageOperationStatus> cont = Either.left(service);
1295 when(componentsUtils.convertFromStorageResponse(eq(StorageOperationStatus.NOT_FOUND), eq(null)))
1296 .thenReturn(ActionStatus.GENERAL_ERROR);
1297 when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class)))
1301 result = componentInstanceBusinessLogic
1302 .batchDeleteComponentInstance(containerComponentParam, containerComponentId, componentInstanceIdList,
1304 assertNotNull(result);
1305 assertEquals(deleteErrorMap, result);
1306 } catch (ComponentException e) {
1307 assertEquals(e.getActionStatus().toString(), StorageOperationStatus.GENERAL_ERROR.toString());
1312 public void testBatchDeleteComponentInstanceFailureCompIds() {
1313 Map<String, List<String>> result = new HashMap<>();
1314 String containerComponentParam = ComponentTypeEnum.SERVICE_PARAM_NAME;
1315 String containerComponentId = "containerComponentId";
1316 String componentInstanceId = "componentInstanceId";
1317 List<String> componentInstanceIdList = new ArrayList<>();
1318 componentInstanceIdList.add(componentInstanceId);
1319 String userId = USER_ID;
1320 Map<String, List<String>> deleteErrorMap = new HashMap<>();
1321 List<String> deleteErrorIds = new ArrayList<>();
1322 deleteErrorIds.add(componentInstanceId);
1323 deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
1325 Either<Component, StorageOperationStatus> err = Either.right(StorageOperationStatus.GENERAL_ERROR);
1326 when(toscaOperationFacade.getToscaElement(eq(containerComponentId), any(ComponentParametersView.class)))
1330 result = componentInstanceBusinessLogic
1331 .batchDeleteComponentInstance(containerComponentParam, containerComponentId, componentInstanceIdList,
1333 assertNotNull(result);
1334 assertEquals(deleteErrorMap, result);
1335 } catch (ComponentException e) {
1336 assertEquals(e.getActionStatus().toString(), StorageOperationStatus.GENERAL_ERROR.toString());
1341 public void testBatchDeleteComponentInstanceSuccess() {
1342 Map<String, List<String>> result;
1343 String containerComponentParam = ComponentTypeEnum.SERVICE_PARAM_NAME;
1344 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1345 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1346 service.setLastUpdaterUserId(USER_ID);
1347 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1348 String containerComponentId = service.getUniqueId();
1349 String componentInstanceId = TO_INSTANCE_ID;
1350 String userId = USER_ID;
1351 List<String> componentInstanceIdList = new ArrayList<>();
1352 componentInstanceIdList.add(componentInstanceId);
1353 Map<String, List<String>> deleteErrorMap = new HashMap<>();
1354 List<String> deleteErrorIds = new ArrayList<>();
1355 deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
1357 Either<Component, StorageOperationStatus> cont = Either.left(service);
1358 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1359 .thenReturn(StorageOperationStatus.OK);
1360 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1361 .thenReturn(StorageOperationStatus.OK);
1362 ImmutablePair<Component, String> pair = new ImmutablePair<>(resource, TO_INSTANCE_ID);
1363 Either<ImmutablePair<Component, String>, StorageOperationStatus> result2 = Either.left(pair);
1364 when(toscaOperationFacade.deleteComponentInstanceFromTopologyTemplate(service, componentInstanceId))
1365 .thenReturn(result2);
1366 when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
1368 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
1370 result = componentInstanceBusinessLogic
1371 .batchDeleteComponentInstance(containerComponentParam, containerComponentId,
1372 componentInstanceIdList, userId);
1373 assertNotNull(result);
1375 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1376 service.setLifecycleState(oldLifeCycleState);
1377 assertEquals(deleteErrorMap, result);
1381 public void testDissociateRIFromRIFailDissociate() {
1383 List<RequirementCapabilityRelDef> result;
1384 RequirementCapabilityRelDef ref = new RequirementCapabilityRelDef();
1385 ref.setFromNode(FROM_INSTANCE_ID);
1386 ref.setToNode(TO_INSTANCE_ID);
1387 List<CapabilityRequirementRelationship> relationships = new ArrayList<>();
1388 CapabilityRequirementRelationship relationship = new CapabilityRequirementRelationship();
1389 RelationshipInfo ri = new RelationshipInfo();
1390 ri.setRequirement(REQUIREMENT_NAME);
1391 relationship.setRelation(ri);
1392 relationships.add(relationship);
1393 ref.setRelationships(relationships);
1394 List<RequirementCapabilityRelDef> requirementDefList = new ArrayList<>();
1395 requirementDefList.add(ref);
1396 ComponentTypeEnum componentTypeEnum = service.getComponentType();
1397 String componentId = service.getUniqueId();
1398 String userId = USER_ID;
1399 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1400 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1401 service.setLastUpdaterUserId(USER_ID);
1402 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1404 Either<Component, StorageOperationStatus> cont = Either.left(service);
1405 when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
1407 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1408 .thenReturn(StorageOperationStatus.OK);
1409 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1410 .thenReturn(StorageOperationStatus.OK);
1411 Either<RequirementCapabilityRelDef, StorageOperationStatus> resultEither;
1412 resultEither = Either.right(StorageOperationStatus.OK);
1413 when(componentsUtils.convertFromStorageResponseForResourceInstance(eq(StorageOperationStatus.OK), eq(true)))
1414 .thenReturn(ActionStatus.GENERAL_ERROR);
1415 when(toscaOperationFacade.dissociateResourceInstances(componentId, ref)).thenReturn(resultEither);
1418 result = componentInstanceBusinessLogic
1419 .batchDissociateRIFromRI(componentId, userId, requirementDefList, componentTypeEnum);
1420 assertNotNull(result);
1421 assertEquals(new ArrayList<>(), result);
1422 } catch (ComponentException e) {
1423 assertEquals(e.getActionStatus().toString(), StorageOperationStatus.GENERAL_ERROR.toString());
1426 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1427 service.setLifecycleState(oldLifeCycleState);
1432 public void testDissociateRIFromRISuccess() {
1434 List<RequirementCapabilityRelDef> result;
1435 RequirementCapabilityRelDef ref = new RequirementCapabilityRelDef();
1436 List<RequirementCapabilityRelDef> requirementDefList = new ArrayList<>();
1437 requirementDefList.add(ref);
1438 ComponentTypeEnum componentTypeEnum = service.getComponentType();
1439 String componentId = service.getUniqueId();
1440 String userId = USER_ID;
1441 LifecycleStateEnum oldLifeCycleState = service.getLifecycleState();
1442 String oldLastUpdatedUserId = service.getLastUpdaterUserId();
1443 service.setLastUpdaterUserId(USER_ID);
1444 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1446 Either<Component, StorageOperationStatus> cont = Either.left(service);
1447 when(toscaOperationFacade.getToscaElement(eq(service.getUniqueId()), any(ComponentParametersView.class)))
1449 when(graphLockOperation.unlockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1450 .thenReturn(StorageOperationStatus.OK);
1451 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Service)))
1452 .thenReturn(StorageOperationStatus.OK);
1453 Either<RequirementCapabilityRelDef, StorageOperationStatus> resultEither;
1454 resultEither = Either.left(ref);
1455 when(toscaOperationFacade.dissociateResourceInstances(componentId, ref)).thenReturn(resultEither);
1457 result = componentInstanceBusinessLogic
1458 .batchDissociateRIFromRI(componentId, userId, requirementDefList, componentTypeEnum);
1459 assertNotNull(result);
1461 service.setLastUpdaterUserId(oldLastUpdatedUserId);
1462 service.setLifecycleState(oldLifeCycleState);
1464 assertEquals(requirementDefList, result);
1468 public void testGetComponentInstancePropertyByPolicyId_success() {
1469 Optional<ComponentInstanceProperty> propertyCandidate =
1470 getComponentInstanceProperty(PROP_NAME);
1472 assertThat(propertyCandidate.isPresent()).isTrue();
1473 assertEquals(propertyCandidate.get().getName(), PROP_NAME);
1477 public void testGetComponentInstancePropertyByPolicyId_failure() {
1478 Optional<ComponentInstanceProperty> propertyCandidate =
1479 getComponentInstanceProperty(NON_EXIST_NAME);
1481 assertEquals(propertyCandidate, Optional.empty());
1484 private Optional<ComponentInstanceProperty> getComponentInstanceProperty(String propertyName) {
1485 ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
1486 componentInstanceProperty.setName(propertyName);
1488 PolicyDefinition policyDefinition = getPolicyDefinition();
1489 componentInstanceProperty.setGetPolicyValues(policyDefinition.getGetPolicyValues());
1491 service.setComponentInstancesProperties(
1492 Collections.singletonMap(COMPONENT_INST_ID, Collections.singletonList(componentInstanceProperty)));
1494 return componentInstanceBusinessLogic.getComponentInstancePropertyByPolicyId(service, policyDefinition);
1497 private PolicyDefinition getPolicyDefinition() {
1498 PolicyDefinition policyDefinition = new PolicyDefinition();
1499 policyDefinition.setInstanceUniqueId(COMPONENT_INST_ID);
1500 policyDefinition.setName(PROP_NAME);
1502 GetPolicyValueDataDefinition getPolicy = new GetPolicyValueDataDefinition();
1503 getPolicy.setPropertyName(PROP_NAME);
1505 List<GetPolicyValueDataDefinition> getPolicies = new ArrayList<>();
1506 getPolicies.add(getPolicy);
1507 policyDefinition.setGetPolicyValues(getPolicies);
1509 return policyDefinition;
1512 private ComponentInstance createComponetInstanceFromComponent(Component component) {
1513 ComponentInstance componentInst = new ComponentInstance();
1514 componentInst.setUniqueId(component.getUniqueId());
1515 componentInst.setComponentUid(component.getUniqueId() + "_test");
1516 componentInst.setPosX("10");
1517 componentInst.setPosY("10");
1518 componentInst.setCapabilities(component.getCapabilities());
1519 componentInst.setRequirements(component.getRequirements());
1520 componentInst.setArtifacts(component.getArtifacts());
1521 componentInst.setDeploymentArtifacts(component.getDeploymentArtifacts());
1522 return componentInst;