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