Sync Integ to Master
[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.verify;
23 import static org.mockito.Mockito.verifyNoMoreInteractions;
24 import static org.mockito.Mockito.when;
25
26 public class ComponentInstancePropertiesMergeBLTest {
27
28     private static final String INSTANCE1 = "instance1";
29     private static final String INSTANCE2 = "instance2";
30
31     @InjectMocks
32     private ComponentInstancePropertiesMergeBL testInstance;
33
34     @Mock
35     private DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic;
36
37     @Mock
38     private ToscaOperationFacade toscaOperationFacade;
39
40     @Mock
41     private ComponentsUtils componentsUtils;
42
43     private Resource oldResource;
44     private Resource newResource;
45
46     @Before
47     public void setUp() throws Exception {
48         MockitoAnnotations.initMocks(this);
49         oldResource = new ResourceBuilder()
50                 .addInstanceProperty(INSTANCE1, "property1")
51                 .addInstanceProperty(INSTANCE1, "property2")
52                 .addInstanceProperty(INSTANCE2, "property3")
53                 .addInput("input1")
54                 .addInput("input2").build();
55
56         newResource = new ResourceBuilder()
57                 .addInstanceProperty(INSTANCE1, "property11")
58                 .addInstanceProperty(INSTANCE1, "property12")
59                 .addInstanceProperty(INSTANCE2, "property13")
60                 .addInput("input11")
61                 .addInput("input12").build();
62     }
63
64     @Test
65     public void mergeInstancesPropsAndInputs_mergeInstanceProps() throws Exception {
66         when(toscaOperationFacade.updateComponentInstancePropsToComponent(newResource.getComponentInstancesProperties(), newResource.getUniqueId()))
67                                  .thenReturn(Either.left(Collections.emptyMap()));
68         ActionStatus actionStatus = testInstance.mergeComponents(oldResource, newResource);
69         assertEquals(actionStatus, ActionStatus.OK);
70         verifyMergeBLCalled(oldResource, newResource);
71     }
72
73     @Test
74     public void mergeInstancesProps_failure() throws Exception {
75         when(toscaOperationFacade.updateComponentInstancePropsToComponent(newResource.getComponentInstancesProperties(), newResource.getUniqueId()))
76                                  .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
77         when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
78         verifyNoMoreInteractions(toscaOperationFacade, propertyValuesMergingBusinessLogic);
79         ActionStatus actionStatus = testInstance.mergeComponents(oldResource, newResource);
80         assertEquals(ActionStatus.GENERAL_ERROR, actionStatus);
81     }
82
83     @Test
84     public void mergeInstanceProps() throws Exception {
85         List<ComponentInstanceProperty> newInstanceProps = newResource.safeGetComponentInstanceProperties(INSTANCE1);
86         List<ComponentInstanceProperty> oldInstProps = oldResource.safeGetComponentInstanceProperties(INSTANCE1);
87         when(toscaOperationFacade.updateComponentInstanceProperties(newResource, INSTANCE1, newInstanceProps))
88                                  .thenReturn(StorageOperationStatus.OK);
89         ActionStatus actionStatus = testInstance.mergeComponentInstanceProperties(oldInstProps, oldResource.getInputs(), newResource, INSTANCE1);
90         assertEquals(actionStatus, ActionStatus.OK);
91         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(oldInstProps, oldResource.getInputs(), newInstanceProps, newResource.getInputs());
92     }
93
94     @Test
95     public void mergeInstanceProps_failure() throws Exception {
96         List<ComponentInstanceProperty> newInstanceProps = newResource.safeGetComponentInstanceProperties(INSTANCE1);
97         List<ComponentInstanceProperty> oldInstProps = oldResource.safeGetComponentInstanceProperties(INSTANCE1);
98         when(toscaOperationFacade.updateComponentInstanceProperties(newResource, INSTANCE1, newInstanceProps))
99                 .thenReturn(StorageOperationStatus.GENERAL_ERROR);
100         when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
101         ActionStatus actionStatus = testInstance.mergeComponentInstanceProperties(oldInstProps, oldResource.getInputs(), newResource, INSTANCE1);
102         assertEquals(actionStatus, ActionStatus.GENERAL_ERROR);
103         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(oldInstProps, oldResource.getInputs(), newInstanceProps, newResource.getInputs());
104     }
105
106     private void verifyMergeBLCalled(Resource oldResource, Resource newResource) {
107         List<ComponentInstanceProperty> instance1oldProps = oldResource.getComponentInstancesProperties().get(INSTANCE1);
108         List<ComponentInstanceProperty> instance1newProps = newResource.getComponentInstancesProperties().get(INSTANCE1);
109         List<ComponentInstanceProperty> instance2oldProps = oldResource.getComponentInstancesProperties().get(INSTANCE2);
110         List<ComponentInstanceProperty> instance2newProps = newResource.getComponentInstancesProperties().get(INSTANCE2);
111         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(instance1oldProps, oldResource.getInputs(), instance1newProps, newResource.getInputs());
112         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(instance2oldProps, oldResource.getInputs(), instance2newProps, newResource.getInputs());
113     }
114
115 }