7948ab8a3d36073806d334b95eb370f6fa1de19b
[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.ResourceBuilder;
17 import org.openecomp.sdc.be.dao.api.ActionStatus;
18 import org.openecomp.sdc.be.impl.ComponentsUtils;
19 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
20 import org.openecomp.sdc.be.model.Resource;
21 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
22 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
23
24 import fj.data.Either;
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, newResource;
44
45     @Before
46     public void setUp() throws Exception {
47         MockitoAnnotations.initMocks(this);
48         oldResource = new ResourceBuilder()
49                 .addInstanceProperty(INSTANCE1, "property1")
50                 .addInstanceProperty(INSTANCE1, "property2")
51                 .addInstanceProperty(INSTANCE2, "property3")
52                 .addInput("input1")
53                 .addInput("input2").build();
54
55         newResource = new ResourceBuilder()
56                 .addInstanceProperty(INSTANCE1, "property11")
57                 .addInstanceProperty(INSTANCE1, "property12")
58                 .addInstanceProperty(INSTANCE2, "property13")
59                 .addInput("input11")
60                 .addInput("input12").build();
61     }
62
63     @Test
64     public void mergeInstancesPropsAndInputs_mergeInstanceProps() throws Exception {
65         when(toscaOperationFacade.updateComponentInstancePropsToComponent(newResource.getComponentInstancesProperties(), newResource.getUniqueId()))
66                                  .thenReturn(Either.left(Collections.emptyMap()));
67         ActionStatus actionStatus = testInstance.mergeComponentInstancesProperties(oldResource, newResource);
68         assertEquals(actionStatus, ActionStatus.OK);
69         verifyMergeBLCalled(oldResource, newResource);
70     }
71
72     @Test
73     public void mergeInstancesProps_failure() throws Exception {
74         when(toscaOperationFacade.updateComponentInstancePropsToComponent(newResource.getComponentInstancesProperties(), newResource.getUniqueId()))
75                                  .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
76         when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
77         verifyNoMoreInteractions(toscaOperationFacade, propertyValuesMergingBusinessLogic);
78         ActionStatus actionStatus = testInstance.mergeComponentInstancesProperties(oldResource, newResource);
79         assertEquals(ActionStatus.GENERAL_ERROR, actionStatus);
80     }
81
82     @Test
83     public void mergeInstanceProps() throws Exception {
84         List<ComponentInstanceProperty> newInstanceProps = newResource.safeGetComponentInstanceProperties(INSTANCE1);
85         List<ComponentInstanceProperty> oldInstProps = oldResource.safeGetComponentInstanceProperties(INSTANCE1);
86         when(toscaOperationFacade.updateComponentInstanceProperties(newResource, INSTANCE1, newInstanceProps))
87                                  .thenReturn(StorageOperationStatus.OK);
88         ActionStatus actionStatus = testInstance.mergeComponentInstanceProperties(oldInstProps, oldResource.getInputs(), newResource, INSTANCE1);
89         assertEquals(actionStatus, ActionStatus.OK);
90         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(oldInstProps, oldResource.getInputs(), newInstanceProps, newResource.getInputs());
91     }
92
93     @Test
94     public void mergeInstanceProps_failure() throws Exception {
95         List<ComponentInstanceProperty> newInstanceProps = newResource.safeGetComponentInstanceProperties(INSTANCE1);
96         List<ComponentInstanceProperty> oldInstProps = oldResource.safeGetComponentInstanceProperties(INSTANCE1);
97         when(toscaOperationFacade.updateComponentInstanceProperties(newResource, INSTANCE1, newInstanceProps))
98                 .thenReturn(StorageOperationStatus.GENERAL_ERROR);
99         when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
100         ActionStatus actionStatus = testInstance.mergeComponentInstanceProperties(oldInstProps, oldResource.getInputs(), newResource, INSTANCE1);
101         assertEquals(actionStatus, ActionStatus.GENERAL_ERROR);
102         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(oldInstProps, oldResource.getInputs(), newInstanceProps, newResource.getInputs());
103     }
104
105     private void verifyMergeBLCalled(Resource oldResource, Resource newResource) {
106         List<ComponentInstanceProperty> instance1oldProps = oldResource.getComponentInstancesProperties().get(INSTANCE1);
107         List<ComponentInstanceProperty> instance1newProps = newResource.getComponentInstancesProperties().get(INSTANCE1);
108         List<ComponentInstanceProperty> instance2oldProps = oldResource.getComponentInstancesProperties().get(INSTANCE2);
109         List<ComponentInstanceProperty> instance2newProps = newResource.getComponentInstancesProperties().get(INSTANCE2);
110         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(instance1oldProps, oldResource.getInputs(), instance1newProps, newResource.getInputs());
111         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(instance2oldProps, oldResource.getInputs(), instance2newProps, newResource.getInputs());
112     }
113
114 }