re base code
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / merge / property / ComponentInstancePropertiesMergeBLTest.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.ComponentInstancePropertiesMergeBL;
10 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
11 import org.openecomp.sdc.be.dao.api.ActionStatus;
12 import org.openecomp.sdc.be.impl.ComponentsUtils;
13 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
14 import org.openecomp.sdc.be.model.Resource;
15 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
16 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
17
18 import java.util.Collections;
19 import java.util.List;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.mockito.Mockito.*;
23
24 public class ComponentInstancePropertiesMergeBLTest {
25
26     private static final String INSTANCE1 = "instance1";
27     private static final String INSTANCE2 = "instance2";
28     private static final String OLD_INSTANCE1 = "old.instance1";
29     private static final String OLD_INSTANCE2 = "old.instance2";
30     private static final String NEW_INSTANCE1 = "new.instance1";
31     private static final String NEW_INSTANCE2 = "new.instance2";
32
33     @InjectMocks
34     private ComponentInstancePropertiesMergeBL 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         oldResource = new ResourceBuilder()
52                 .addComponentInstance(INSTANCE1, OLD_INSTANCE1)
53                 .addComponentInstance(INSTANCE2, OLD_INSTANCE2)
54                 .addInstanceProperty(OLD_INSTANCE1, "property1")
55                 .addInstanceProperty(OLD_INSTANCE1, "property2")
56                 .addInstanceProperty(OLD_INSTANCE2, "property3")
57                 .addInput("input1")
58                 .addInput("input2").build();
59
60         newResource = new ResourceBuilder()
61                 .addComponentInstance(INSTANCE1, NEW_INSTANCE1)
62                 .addComponentInstance(INSTANCE2, NEW_INSTANCE2)
63                 .addInstanceProperty(NEW_INSTANCE1, "property11")
64                 .addInstanceProperty(NEW_INSTANCE1, "property12")
65                 .addInstanceProperty(NEW_INSTANCE2, "property13")
66                 .addInput("input11")
67                 .addInput("input12").build();
68     }
69
70     @Test
71     public void mergeInstancesPropsAndInputs_mergeInstanceProps() throws Exception {
72         when(toscaOperationFacade.updateComponentInstancePropsToComponent(newResource.getComponentInstancesProperties(), newResource.getUniqueId()))
73                                  .thenReturn(Either.left(Collections.emptyMap()));
74         ActionStatus actionStatus = testInstance.mergeComponents(oldResource, newResource);
75         assertEquals(ActionStatus.OK, actionStatus);
76         verifyMergeBLCalled(oldResource, newResource);
77     }
78
79     @Test
80     public void mergeInstancesProps_failure() throws Exception {
81         when(toscaOperationFacade.updateComponentInstancePropsToComponent(newResource.getComponentInstancesProperties(), newResource.getUniqueId()))
82                                  .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() throws Exception {
91         List<ComponentInstanceProperty> newInstanceProps = newResource.safeGetComponentInstanceProperties(NEW_INSTANCE1);
92         List<ComponentInstanceProperty> oldInstProps = oldResource.safeGetComponentInstanceProperties(OLD_INSTANCE1);
93         when(toscaOperationFacade.updateComponentInstanceProperties(newResource, NEW_INSTANCE1, newInstanceProps))
94                                  .thenReturn(StorageOperationStatus.OK);
95         ActionStatus actionStatus = testInstance.mergeComponentInstanceProperties(oldInstProps, oldResource.getInputs(), newResource, NEW_INSTANCE1);
96         assertEquals(ActionStatus.OK, actionStatus);
97         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(oldInstProps, oldResource.getInputs(), newInstanceProps, newResource.getInputs());
98     }
99
100     @Test
101     public void mergeInstanceProps_failure() throws Exception {
102         List<ComponentInstanceProperty> newInstanceProps = newResource.safeGetComponentInstanceProperties(NEW_INSTANCE1);
103         List<ComponentInstanceProperty> oldInstProps = oldResource.safeGetComponentInstanceProperties(OLD_INSTANCE1);
104         when(toscaOperationFacade.updateComponentInstanceProperties(newResource, NEW_INSTANCE1, newInstanceProps))
105                 .thenReturn(StorageOperationStatus.GENERAL_ERROR);
106         when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
107         ActionStatus actionStatus = testInstance.mergeComponentInstanceProperties(oldInstProps, oldResource.getInputs(), newResource, NEW_INSTANCE1);
108         assertEquals(ActionStatus.GENERAL_ERROR, actionStatus);
109         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(oldInstProps, oldResource.getInputs(), newInstanceProps, newResource.getInputs());
110     }
111
112     private void verifyMergeBLCalled(Resource oldResource, Resource newResource) {
113         List<ComponentInstanceProperty> instance1oldProps = oldResource.getComponentInstancesProperties().get(OLD_INSTANCE1);
114         List<ComponentInstanceProperty> instance1newProps = newResource.getComponentInstancesProperties().get(NEW_INSTANCE1);
115         List<ComponentInstanceProperty> instance2oldProps = oldResource.getComponentInstancesProperties().get(OLD_INSTANCE2);
116         List<ComponentInstanceProperty> instance2newProps = newResource.getComponentInstancesProperties().get(NEW_INSTANCE2);
117         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(instance1oldProps, oldResource.getInputs(), instance1newProps, newResource.getInputs());
118         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(instance2oldProps, oldResource.getInputs(), instance2newProps, newResource.getInputs());
119     }
120
121 }