Improve testing stability
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / merge / property / ComponentInstancePropertiesMergeBLTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.merge.property;
22
23 import fj.data.Either;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.openecomp.sdc.be.components.merge.instance.ComponentInstancePropertiesMergeBL;
30 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
31 import org.openecomp.sdc.be.dao.api.ActionStatus;
32 import org.openecomp.sdc.be.impl.ComponentsUtils;
33 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
34 import org.openecomp.sdc.be.model.Resource;
35 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
36 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
37
38 import java.util.Collections;
39 import java.util.List;
40
41 import static org.junit.Assert.assertEquals;
42 import static org.mockito.Mockito.verify;
43 import static org.mockito.Mockito.verifyNoMoreInteractions;
44 import static org.mockito.Mockito.when;
45
46 public class ComponentInstancePropertiesMergeBLTest {
47
48     private static final String INSTANCE1 = "instance1";
49     private static final String INSTANCE2 = "instance2";
50     private static final String OLD_INSTANCE1 = "old.instance1";
51     private static final String OLD_INSTANCE2 = "old.instance2";
52     private static final String NEW_INSTANCE1 = "new.instance1";
53     private static final String NEW_INSTANCE2 = "new.instance2";
54
55     @InjectMocks
56     private ComponentInstancePropertiesMergeBL testInstance;
57
58     @Mock
59     private DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic;
60
61     @Mock
62     private ToscaOperationFacade toscaOperationFacade;
63
64     @Mock
65     private ComponentsUtils componentsUtils;
66
67     private Resource oldResource;
68     private Resource newResource;
69
70     @Before
71     public void setUp() throws Exception {
72         MockitoAnnotations.openMocks(this);
73         oldResource = new ResourceBuilder()
74                 .addComponentInstance(INSTANCE1, OLD_INSTANCE1)
75                 .addComponentInstance(INSTANCE2, OLD_INSTANCE2)
76                 .addInstanceProperty(OLD_INSTANCE1, "property1")
77                 .addInstanceProperty(OLD_INSTANCE1, "property2")
78                 .addInstanceProperty(OLD_INSTANCE2, "property3")
79                 .addInput("input1")
80                 .addInput("input2").build();
81
82         newResource = new ResourceBuilder()
83                 .addComponentInstance(INSTANCE1, NEW_INSTANCE1)
84                 .addComponentInstance(INSTANCE2, NEW_INSTANCE2)
85                 .addInstanceProperty(NEW_INSTANCE1, "property11")
86                 .addInstanceProperty(NEW_INSTANCE1, "property12")
87                 .addInstanceProperty(NEW_INSTANCE2, "property13")
88                 .addInput("input11")
89                 .addInput("input12").build();
90     }
91
92     @Test
93     public void mergeInstancesPropsAndInputs_mergeInstanceProps() throws Exception {
94         when(toscaOperationFacade.updateComponentInstancePropsToComponent(newResource.getComponentInstancesProperties(), newResource.getUniqueId()))
95                                  .thenReturn(Either.left(Collections.emptyMap()));
96         ActionStatus actionStatus = testInstance.mergeComponents(oldResource, newResource);
97         assertEquals(ActionStatus.OK, actionStatus);
98         verifyMergeBLCalled(oldResource, newResource);
99     }
100
101     @Test
102     public void mergeInstancesProps_failure() throws Exception {
103         when(toscaOperationFacade.updateComponentInstancePropsToComponent(newResource.getComponentInstancesProperties(), newResource.getUniqueId()))
104                                  .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
105         when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
106         verifyNoMoreInteractions(toscaOperationFacade, propertyValuesMergingBusinessLogic);
107         ActionStatus actionStatus = testInstance.mergeComponents(oldResource, newResource);
108         assertEquals(ActionStatus.GENERAL_ERROR, actionStatus);
109     }
110
111     @Test
112     public void mergeInstanceProps() throws Exception {
113         List<ComponentInstanceProperty> newInstanceProps = newResource.safeGetComponentInstanceProperties(NEW_INSTANCE1);
114         List<ComponentInstanceProperty> oldInstProps = oldResource.safeGetComponentInstanceProperties(OLD_INSTANCE1);
115         when(toscaOperationFacade.updateComponentInstanceProperties(newResource, NEW_INSTANCE1, newInstanceProps))
116                                  .thenReturn(StorageOperationStatus.OK);
117         ActionStatus actionStatus = testInstance.mergeComponentInstanceProperties(oldInstProps, oldResource.getInputs(), newResource, NEW_INSTANCE1);
118         assertEquals(ActionStatus.OK, actionStatus);
119         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(oldInstProps, oldResource.getInputs(), newInstanceProps, newResource.getInputs());
120     }
121
122     @Test
123     public void mergeInstanceProps_failure() throws Exception {
124         List<ComponentInstanceProperty> newInstanceProps = newResource.safeGetComponentInstanceProperties(NEW_INSTANCE1);
125         List<ComponentInstanceProperty> oldInstProps = oldResource.safeGetComponentInstanceProperties(OLD_INSTANCE1);
126         when(toscaOperationFacade.updateComponentInstanceProperties(newResource, NEW_INSTANCE1, newInstanceProps))
127                 .thenReturn(StorageOperationStatus.GENERAL_ERROR);
128         when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
129         ActionStatus actionStatus = testInstance.mergeComponentInstanceProperties(oldInstProps, oldResource.getInputs(), newResource, NEW_INSTANCE1);
130         assertEquals(ActionStatus.GENERAL_ERROR, actionStatus);
131         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(oldInstProps, oldResource.getInputs(), newInstanceProps, newResource.getInputs());
132     }
133
134     private void verifyMergeBLCalled(Resource oldResource, Resource newResource) {
135         List<ComponentInstanceProperty> instance1oldProps = oldResource.getComponentInstancesProperties().get(OLD_INSTANCE1);
136         List<ComponentInstanceProperty> instance1newProps = newResource.getComponentInstancesProperties().get(NEW_INSTANCE1);
137         List<ComponentInstanceProperty> instance2oldProps = oldResource.getComponentInstancesProperties().get(OLD_INSTANCE2);
138         List<ComponentInstanceProperty> instance2newProps = newResource.getComponentInstancesProperties().get(NEW_INSTANCE2);
139         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(instance1oldProps, oldResource.getInputs(), instance1newProps, newResource.getInputs());
140         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(instance2oldProps, oldResource.getInputs(), instance2newProps, newResource.getInputs());
141     }
142
143 }