Improve testing stability
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / merge / property / ComponentInstanceInputsMergeBLTest.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.ComponentInstanceInputsMergeBL;
30 import org.openecomp.sdc.be.components.utils.ComponentInstanceBuilder;
31 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
32 import org.openecomp.sdc.be.dao.api.ActionStatus;
33 import org.openecomp.sdc.be.impl.ComponentsUtils;
34 import org.openecomp.sdc.be.model.ComponentInstance;
35 import org.openecomp.sdc.be.model.ComponentInstanceInput;
36 import org.openecomp.sdc.be.model.Resource;
37 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
38 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
39
40 import java.util.Collections;
41 import java.util.List;
42
43 import static org.junit.Assert.assertEquals;
44 import static org.mockito.Mockito.verify;
45 import static org.mockito.Mockito.verifyNoMoreInteractions;
46 import static org.mockito.Mockito.when;
47
48 public class ComponentInstanceInputsMergeBLTest {
49
50     public static final String INSTANCE1 = "instance1";
51     public static final String INSTANCE2 = "instance2";
52     @InjectMocks
53     private ComponentInstanceInputsMergeBL testInstance;
54
55     @Mock
56     private DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic;
57
58     @Mock
59     private ToscaOperationFacade toscaOperationFacade;
60
61     @Mock
62     private ComponentsUtils componentsUtils;
63
64     private Resource oldResource;
65     private Resource newResource;
66
67     @Before
68     public void setUp() throws Exception {
69         MockitoAnnotations.openMocks(this);
70         ComponentInstance instance1 = new ComponentInstanceBuilder().setId(INSTANCE1).setName(INSTANCE1).build();
71         ComponentInstance instance2 = new ComponentInstanceBuilder().setId(INSTANCE2).setName(INSTANCE2).build();
72
73         oldResource = new ResourceBuilder()
74                 .addInstanceInput(INSTANCE1, "property1")
75                 .addInstanceInput(INSTANCE1, "property2")
76                 .addInstanceInput(INSTANCE2, "property3")
77                 .addComponentInstance(instance1)
78                 .addComponentInstance(instance2)
79                 .addInput("input1")
80                 .addInput("input2").build();
81
82         newResource = new ResourceBuilder()
83                 .addInstanceInput(INSTANCE1, "property11")
84                 .addInstanceInput(INSTANCE1, "property12")
85                 .addInstanceInput(INSTANCE2, "property13")
86                 .addComponentInstance(instance1)
87                 .addComponentInstance(instance2)
88                 .addInput("input11")
89                 .addInput("input12").build();
90     }
91
92     @Test
93     public void mergeInstancesInputs() throws Exception {
94         when(toscaOperationFacade.updateComponentInstanceInputsToComponent(newResource.getComponentInstancesInputs(), newResource.getUniqueId())).thenReturn(Either.left(Collections.emptyMap()));
95         ActionStatus actionStatus = testInstance.mergeComponents(oldResource, newResource);
96         assertEquals(actionStatus, ActionStatus.OK);
97         verifyMergeBLCalled(oldResource, newResource);
98     }
99
100     @Test
101     public void mergeInstancesInputs_failure() {
102         when(toscaOperationFacade.updateComponentInstanceInputsToComponent(newResource.getComponentInstancesInputs(), newResource.getUniqueId())).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
103         when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
104         verifyNoMoreInteractions(toscaOperationFacade, propertyValuesMergingBusinessLogic);
105         ActionStatus actionStatus = testInstance.mergeComponents(oldResource, newResource);
106         assertEquals(ActionStatus.GENERAL_ERROR, actionStatus);
107     }
108
109     @Test
110     public void mergeInstanceProps() {
111         List<ComponentInstanceInput> newInstanceInputs = newResource.safeGetComponentInstanceInput(INSTANCE1);
112         List<ComponentInstanceInput> oldInstInputs = oldResource.safeGetComponentInstanceInput(INSTANCE1);
113         when(toscaOperationFacade.updateComponentInstanceInputs(newResource, INSTANCE1, newInstanceInputs))
114                 .thenReturn(StorageOperationStatus.OK);
115         ActionStatus actionStatus = testInstance.mergeComponentInstanceInputs(oldInstInputs, oldResource.getInputs(), newResource, INSTANCE1);
116         assertEquals(actionStatus, ActionStatus.OK);
117         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(oldInstInputs, oldResource.getInputs(), newInstanceInputs, newResource.getInputs());
118     }
119
120     @Test
121     public void mergeInstanceProps_failure() {
122         List<ComponentInstanceInput> newInstanceInputs = newResource.safeGetComponentInstanceInput(INSTANCE1);
123         List<ComponentInstanceInput> oldInstInputs = oldResource.safeGetComponentInstanceInput(INSTANCE1);
124         when(toscaOperationFacade.updateComponentInstanceInputs(newResource, INSTANCE1, newInstanceInputs))
125                 .thenReturn(StorageOperationStatus.GENERAL_ERROR);
126         when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
127         ActionStatus actionStatus = testInstance.mergeComponentInstanceInputs(oldInstInputs, oldResource.getInputs(), newResource, INSTANCE1);
128         assertEquals(actionStatus, ActionStatus.GENERAL_ERROR);
129         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(oldInstInputs, oldResource.getInputs(), newInstanceInputs, newResource.getInputs());
130     }
131
132     private void verifyMergeBLCalled(Resource oldResource, Resource newResource) {
133         List<ComponentInstanceInput> instance1oldInputs = oldResource.getComponentInstancesInputs().get(INSTANCE1);
134         List<ComponentInstanceInput> instance1newInputs = newResource.getComponentInstancesInputs().get(INSTANCE1);
135         List<ComponentInstanceInput> instance2oldInputs = oldResource.getComponentInstancesInputs().get(INSTANCE2);
136         List<ComponentInstanceInput> instance2newInputs = newResource.getComponentInstancesInputs().get(INSTANCE2);
137         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(instance1oldInputs, oldResource.getInputs(), instance1newInputs, newResource.getInputs());
138         verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(instance2oldInputs, oldResource.getInputs(), instance2newInputs, newResource.getInputs());
139     }
140
141 }