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