Sync Integ to Master
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / merge / property / ComponentInstanceInputsMergeBLTest.java
1 package org.openecomp.sdc.be.components.merge.property;
2
3 import fj.data.Either;
4 import org.junit.Before;
5 import org.junit.Test;
6 import org.mockito.InjectMocks;
7 import org.mockito.Mock;
8 import org.mockito.MockitoAnnotations;
9 import org.openecomp.sdc.be.components.merge.instance.ComponentInstanceInputsMergeBL;
10 import org.openecomp.sdc.be.components.utils.ComponentInstanceBuilder;
11 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
12 import org.openecomp.sdc.be.dao.api.ActionStatus;
13 import org.openecomp.sdc.be.impl.ComponentsUtils;
14 import org.openecomp.sdc.be.model.ComponentInstance;
15 import org.openecomp.sdc.be.model.ComponentInstanceInput;
16 import org.openecomp.sdc.be.model.Resource;
17 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
18 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
19
20 import java.util.Collections;
21 import java.util.List;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.verifyNoMoreInteractions;
26 import static org.mockito.Mockito.when;
27
28 public class ComponentInstanceInputsMergeBLTest {
29
30     public static final String INSTANCE1 = "instance1";
31     public static final String INSTANCE2 = "instance2";
32     @InjectMocks
33     private ComponentInstanceInputsMergeBL testInstance;
34
35     @Mock
36     private DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic;
37
38     @Mock
39     private ToscaOperationFacade toscaOperationFacade;
40
41     @Mock
42     private ComponentsUtils componentsUtils;
43
44     private Resource oldResource;
45     private Resource newResource;
46
47     @Before
48     public void setUp() throws Exception {
49         MockitoAnnotations.initMocks(this);
50         ComponentInstance instance1 = new ComponentInstanceBuilder().setId(INSTANCE1).setName(INSTANCE1).build();
51         ComponentInstance instance2 = new ComponentInstanceBuilder().setId(INSTANCE2).setName(INSTANCE2).build();
52
53         oldResource = new ResourceBuilder()
54                 .addInstanceInput(INSTANCE1, "property1")
55                 .addInstanceInput(INSTANCE1, "property2")
56                 .addInstanceInput(INSTANCE2, "property3")
57                 .addComponentInstance(instance1)
58                 .addComponentInstance(instance2)
59                 .addInput("input1")
60                 .addInput("input2").build();
61
62         newResource = new ResourceBuilder()
63                 .addInstanceInput(INSTANCE1, "property11")
64                 .addInstanceInput(INSTANCE1, "property12")
65                 .addInstanceInput(INSTANCE2, "property13")
66                 .addComponentInstance(instance1)
67                 .addComponentInstance(instance2)
68                 .addInput("input11")
69                 .addInput("input12").build();
70     }
71
72     @Test
73     public void mergeInstancesInputs() throws Exception {
74         when(toscaOperationFacade.updateComponentInstanceInputsToComponent(newResource.getComponentInstancesInputs(), newResource.getUniqueId())).thenReturn(Either.left(Collections.emptyMap()));
75         ActionStatus actionStatus = testInstance.mergeComponents(oldResource, newResource);
76         assertEquals(actionStatus, ActionStatus.OK);
77         verifyMergeBLCalled(oldResource, newResource);
78     }
79
80     @Test
81     public void mergeInstancesInputs_failure() {
82         when(toscaOperationFacade.updateComponentInstanceInputsToComponent(newResource.getComponentInstancesInputs(), newResource.getUniqueId())).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
83         when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
84         verifyNoMoreInteractions(toscaOperationFacade, propertyValuesMergingBusinessLogic);
85         ActionStatus actionStatus = testInstance.mergeComponents(oldResource, newResource);
86         assertEquals(ActionStatus.GENERAL_ERROR, actionStatus);
87     }
88
89     @Test
90     public void mergeInstanceProps() {
91         List<ComponentInstanceInput> newInstanceInputs = newResource.safeGetComponentInstanceInput(INSTANCE1);
92         List<ComponentInstanceInput> oldInstInputs = oldResource.safeGetComponentInstanceInput(INSTANCE1);
93         when(toscaOperationFacade.updateComponentInstanceInputs(newResource, INSTANCE1, newInstanceInputs))
94                 .thenReturn(StorageOperationStatus.OK);
95         ActionStatus actionStatus = testInstance.mergeComponentInstanceInputs(oldInstInputs, oldResource.getInputs(), newResource, INSTANCE1);
96         assertEquals(actionStatus, ActionStatus.OK);
97         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(oldInstInputs, oldResource.getInputs(), newInstanceInputs, newResource.getInputs());
98     }
99
100     @Test
101     public void mergeInstanceProps_failure() {
102         List<ComponentInstanceInput> newInstanceInputs = newResource.safeGetComponentInstanceInput(INSTANCE1);
103         List<ComponentInstanceInput> oldInstInputs = oldResource.safeGetComponentInstanceInput(INSTANCE1);
104         when(toscaOperationFacade.updateComponentInstanceInputs(newResource, INSTANCE1, newInstanceInputs))
105                 .thenReturn(StorageOperationStatus.GENERAL_ERROR);
106         when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
107         ActionStatus actionStatus = testInstance.mergeComponentInstanceInputs(oldInstInputs, oldResource.getInputs(), newResource, INSTANCE1);
108         assertEquals(actionStatus, ActionStatus.GENERAL_ERROR);
109         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(oldInstInputs, oldResource.getInputs(), newInstanceInputs, newResource.getInputs());
110     }
111
112     private void verifyMergeBLCalled(Resource oldResource, Resource newResource) {
113         List<ComponentInstanceInput> instance1oldInputs = oldResource.getComponentInstancesInputs().get(INSTANCE1);
114         List<ComponentInstanceInput> instance1newInputs = newResource.getComponentInstancesInputs().get(INSTANCE1);
115         List<ComponentInstanceInput> instance2oldInputs = oldResource.getComponentInstancesInputs().get(INSTANCE2);
116         List<ComponentInstanceInput> instance2newInputs = newResource.getComponentInstancesInputs().get(INSTANCE2);
117         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(instance1oldInputs, oldResource.getInputs(), instance1newInputs, newResource.getInputs());
118         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(instance2oldInputs, oldResource.getInputs(), instance2newInputs, newResource.getInputs());
119     }
120
121 }