1 package org.openecomp.sdc.be.components.merge.property;
3 import static org.junit.Assert.assertEquals;
4 import static org.mockito.Mockito.verify;
5 import static org.mockito.Mockito.verifyNoMoreInteractions;
6 import static org.mockito.Mockito.when;
8 import java.util.Collections;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.mockito.InjectMocks;
14 import org.mockito.Mock;
15 import org.mockito.MockitoAnnotations;
16 import org.openecomp.sdc.be.components.merge.instance.ComponentInstancePropertiesMergeBL;
17 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
18 import org.openecomp.sdc.be.dao.api.ActionStatus;
19 import org.openecomp.sdc.be.impl.ComponentsUtils;
20 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
21 import org.openecomp.sdc.be.model.Resource;
22 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
23 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
25 import fj.data.Either;
27 public class ComponentInstancePropertiesMergeBLTest {
29 private static final String INSTANCE1 = "instance1";
30 private static final String INSTANCE2 = "instance2";
33 private ComponentInstancePropertiesMergeBL testInstance;
36 private DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic;
39 private ToscaOperationFacade toscaOperationFacade;
42 private ComponentsUtils componentsUtils;
44 private Resource oldResource;
45 private Resource newResource;
48 public void setUp() throws Exception {
49 MockitoAnnotations.initMocks(this);
50 oldResource = new ResourceBuilder()
51 .addInstanceProperty(INSTANCE1, "property1")
52 .addInstanceProperty(INSTANCE1, "property2")
53 .addInstanceProperty(INSTANCE2, "property3")
55 .addInput("input2").build();
57 newResource = new ResourceBuilder()
58 .addInstanceProperty(INSTANCE1, "property11")
59 .addInstanceProperty(INSTANCE1, "property12")
60 .addInstanceProperty(INSTANCE2, "property13")
62 .addInput("input12").build();
66 public void mergeInstancesPropsAndInputs_mergeInstanceProps() throws Exception {
67 when(toscaOperationFacade.updateComponentInstancePropsToComponent(newResource.getComponentInstancesProperties(), newResource.getUniqueId()))
68 .thenReturn(Either.left(Collections.emptyMap()));
69 ActionStatus actionStatus = testInstance.mergeComponents(oldResource, newResource);
70 assertEquals(actionStatus, ActionStatus.OK);
71 verifyMergeBLCalled(oldResource, newResource);
75 public void mergeInstancesProps_failure() throws Exception {
76 when(toscaOperationFacade.updateComponentInstancePropsToComponent(newResource.getComponentInstancesProperties(), newResource.getUniqueId()))
77 .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
78 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
79 verifyNoMoreInteractions(toscaOperationFacade, propertyValuesMergingBusinessLogic);
80 ActionStatus actionStatus = testInstance.mergeComponents(oldResource, newResource);
81 assertEquals(ActionStatus.GENERAL_ERROR, actionStatus);
85 public void mergeInstanceProps() throws Exception {
86 List<ComponentInstanceProperty> newInstanceProps = newResource.safeGetComponentInstanceProperties(INSTANCE1);
87 List<ComponentInstanceProperty> oldInstProps = oldResource.safeGetComponentInstanceProperties(INSTANCE1);
88 when(toscaOperationFacade.updateComponentInstanceProperties(newResource, INSTANCE1, newInstanceProps))
89 .thenReturn(StorageOperationStatus.OK);
90 ActionStatus actionStatus = testInstance.mergeComponentInstanceProperties(oldInstProps, oldResource.getInputs(), newResource, INSTANCE1);
91 assertEquals(actionStatus, ActionStatus.OK);
92 verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(oldInstProps, oldResource.getInputs(), newInstanceProps, newResource.getInputs());
96 public void mergeInstanceProps_failure() throws Exception {
97 List<ComponentInstanceProperty> newInstanceProps = newResource.safeGetComponentInstanceProperties(INSTANCE1);
98 List<ComponentInstanceProperty> oldInstProps = oldResource.safeGetComponentInstanceProperties(INSTANCE1);
99 when(toscaOperationFacade.updateComponentInstanceProperties(newResource, INSTANCE1, newInstanceProps))
100 .thenReturn(StorageOperationStatus.GENERAL_ERROR);
101 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
102 ActionStatus actionStatus = testInstance.mergeComponentInstanceProperties(oldInstProps, oldResource.getInputs(), newResource, INSTANCE1);
103 assertEquals(actionStatus, ActionStatus.GENERAL_ERROR);
104 verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(oldInstProps, oldResource.getInputs(), newInstanceProps, newResource.getInputs());
107 private void verifyMergeBLCalled(Resource oldResource, Resource newResource) {
108 List<ComponentInstanceProperty> instance1oldProps = oldResource.getComponentInstancesProperties().get(INSTANCE1);
109 List<ComponentInstanceProperty> instance1newProps = newResource.getComponentInstancesProperties().get(INSTANCE1);
110 List<ComponentInstanceProperty> instance2oldProps = oldResource.getComponentInstancesProperties().get(INSTANCE2);
111 List<ComponentInstanceProperty> instance2newProps = newResource.getComponentInstancesProperties().get(INSTANCE2);
112 verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(instance1oldProps, oldResource.getInputs(), instance1newProps, newResource.getInputs());
113 verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(instance2oldProps, oldResource.getInputs(), instance2newProps, newResource.getInputs());