0a858c9a29c2b7f9e08ce2259c62fd3f66c3fa58
[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.utils.ComponentInstanceBuilder;
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.ComponentInstance;
21 import org.openecomp.sdc.be.model.ComponentInstanceInput;
22 import org.openecomp.sdc.be.model.Resource;
23 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
24 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
25
26 import fj.data.Either;
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, newResource;
45
46     @Before
47     public void setUp() throws Exception {
48         MockitoAnnotations.initMocks(this);
49         ComponentInstance instance1 = new ComponentInstanceBuilder().setId(INSTANCE1).setName(INSTANCE1).build();
50         ComponentInstance instance2 = new ComponentInstanceBuilder().setId(INSTANCE2).setName(INSTANCE2).build();
51
52         oldResource = new ResourceBuilder()
53                 .addInstanceInput(INSTANCE1, "property1")
54                 .addInstanceInput(INSTANCE1, "property2")
55                 .addInstanceInput(INSTANCE2, "property3")
56                 .addComponentInstance(instance1)
57                 .addComponentInstance(instance2)
58                 .addInput("input1")
59                 .addInput("input2").build();
60
61         newResource = new ResourceBuilder()
62                 .addInstanceInput(INSTANCE1, "property11")
63                 .addInstanceInput(INSTANCE1, "property12")
64                 .addInstanceInput(INSTANCE2, "property13")
65                 .addComponentInstance(instance1)
66                 .addComponentInstance(instance2)
67                 .addInput("input11")
68                 .addInput("input12").build();
69     }
70
71     @Test
72     public void mergeInstancesInputs() throws Exception {
73         when(toscaOperationFacade.updateComponentInstanceInputsToComponent(newResource.getComponentInstancesInputs(), newResource.getUniqueId())).thenReturn(Either.left(Collections.emptyMap()));
74         ActionStatus actionStatus = testInstance.mergeComponentInstancesInputs(oldResource, newResource);
75         assertEquals(actionStatus, ActionStatus.OK);
76         verifyMergeBLCalled(oldResource, newResource);
77     }
78
79     @Test
80     public void mergeInstancesInputs_failure() throws Exception {
81         when(toscaOperationFacade.updateComponentInstanceInputsToComponent(newResource.getComponentInstancesInputs(), newResource.getUniqueId())).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
82         when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
83         verifyNoMoreInteractions(toscaOperationFacade, propertyValuesMergingBusinessLogic);
84         ActionStatus actionStatus = testInstance.mergeComponentInstancesInputs(oldResource, newResource);
85         assertEquals(ActionStatus.GENERAL_ERROR, actionStatus);
86     }
87
88     @Test
89     public void mergeInstanceProps() throws Exception {
90         List<ComponentInstanceInput> newInstanceInputs = newResource.safeGetComponentInstanceInput(INSTANCE1);
91         List<ComponentInstanceInput> oldInstInputs = oldResource.safeGetComponentInstanceInput(INSTANCE1);
92         when(toscaOperationFacade.updateComponentInstanceInputs(newResource, INSTANCE1, newInstanceInputs))
93                 .thenReturn(StorageOperationStatus.OK);
94         ActionStatus actionStatus = testInstance.mergeComponentInstanceInputs(oldInstInputs, oldResource.getInputs(), newResource, INSTANCE1);
95         assertEquals(actionStatus, ActionStatus.OK);
96         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(oldInstInputs, oldResource.getInputs(), newInstanceInputs, newResource.getInputs());
97     }
98
99     @Test
100     public void mergeInstanceProps_failure() throws Exception {
101         List<ComponentInstanceInput> newInstanceInputs = newResource.safeGetComponentInstanceInput(INSTANCE1);
102         List<ComponentInstanceInput> oldInstInputs = oldResource.safeGetComponentInstanceInput(INSTANCE1);
103         when(toscaOperationFacade.updateComponentInstanceInputs(newResource, INSTANCE1, newInstanceInputs))
104                 .thenReturn(StorageOperationStatus.GENERAL_ERROR);
105         when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
106         ActionStatus actionStatus = testInstance.mergeComponentInstanceInputs(oldInstInputs, oldResource.getInputs(), newResource, INSTANCE1);
107         assertEquals(actionStatus, ActionStatus.GENERAL_ERROR);
108         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(oldInstInputs, oldResource.getInputs(), newInstanceInputs, newResource.getInputs());
109     }
110
111     private void verifyMergeBLCalled(Resource oldResource, Resource newResource) {
112         List<ComponentInstanceInput> instance1oldInputs = oldResource.getComponentInstancesInputs().get(INSTANCE1);
113         List<ComponentInstanceInput> instance1newInputs = newResource.getComponentInstancesInputs().get(INSTANCE1);
114         List<ComponentInstanceInput> instance2oldInputs = oldResource.getComponentInstancesInputs().get(INSTANCE2);
115         List<ComponentInstanceInput> instance2newInputs = newResource.getComponentInstancesInputs().get(INSTANCE2);
116         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(instance1oldInputs, oldResource.getInputs(), instance1newInputs, newResource.getInputs());
117         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(instance2oldInputs, oldResource.getInputs(), instance2newInputs, newResource.getInputs());
118     }
119
120 }