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