1 package org.openecomp.sdc.be.components.merge.property;
4 import org.junit.Before;
6 import org.mockito.InjectMocks;
7 import org.mockito.Mock;
8 import org.mockito.MockitoAnnotations;
9 import org.openecomp.sdc.be.components.merge.instance.ComponentInstanceInputsMergeBL;
10 import org.openecomp.sdc.be.components.utils.ComponentInstanceBuilder;
11 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
12 import org.openecomp.sdc.be.dao.api.ActionStatus;
13 import org.openecomp.sdc.be.impl.ComponentsUtils;
14 import org.openecomp.sdc.be.model.ComponentInstance;
15 import org.openecomp.sdc.be.model.ComponentInstanceInput;
16 import org.openecomp.sdc.be.model.Resource;
17 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
18 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
20 import java.util.Collections;
21 import java.util.List;
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.Mockito.*;
26 public class ComponentInstanceInputsMergeBLTest {
28 public static final String INSTANCE1 = "instance1";
29 public static final String INSTANCE2 = "instance2";
31 private ComponentInstanceInputsMergeBL testInstance;
34 private DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic;
37 private ToscaOperationFacade toscaOperationFacade;
40 private ComponentsUtils componentsUtils;
42 private Resource oldResource;
43 private Resource newResource;
46 public void setUp() throws Exception {
47 MockitoAnnotations.initMocks(this);
48 ComponentInstance instance1 = new ComponentInstanceBuilder().setId(INSTANCE1).setName(INSTANCE1).build();
49 ComponentInstance instance2 = new ComponentInstanceBuilder().setId(INSTANCE2).setName(INSTANCE2).build();
51 oldResource = new ResourceBuilder()
52 .addInstanceInput(INSTANCE1, "property1")
53 .addInstanceInput(INSTANCE1, "property2")
54 .addInstanceInput(INSTANCE2, "property3")
55 .addComponentInstance(instance1)
56 .addComponentInstance(instance2)
58 .addInput("input2").build();
60 newResource = new ResourceBuilder()
61 .addInstanceInput(INSTANCE1, "property11")
62 .addInstanceInput(INSTANCE1, "property12")
63 .addInstanceInput(INSTANCE2, "property13")
64 .addComponentInstance(instance1)
65 .addComponentInstance(instance2)
67 .addInput("input12").build();
71 public void mergeInstancesInputs() throws Exception {
72 when(toscaOperationFacade.updateComponentInstanceInputsToComponent(newResource.getComponentInstancesInputs(), newResource.getUniqueId())).thenReturn(Either.left(Collections.emptyMap()));
73 ActionStatus actionStatus = testInstance.mergeComponents(oldResource, newResource);
74 assertEquals(actionStatus, ActionStatus.OK);
75 verifyMergeBLCalled(oldResource, newResource);
79 public void mergeInstancesInputs_failure() {
80 when(toscaOperationFacade.updateComponentInstanceInputsToComponent(newResource.getComponentInstancesInputs(), newResource.getUniqueId())).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
81 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
82 verifyNoMoreInteractions(toscaOperationFacade, propertyValuesMergingBusinessLogic);
83 ActionStatus actionStatus = testInstance.mergeComponents(oldResource, newResource);
84 assertEquals(ActionStatus.GENERAL_ERROR, actionStatus);
88 public void mergeInstanceProps() {
89 List<ComponentInstanceInput> newInstanceInputs = newResource.safeGetComponentInstanceInput(INSTANCE1);
90 List<ComponentInstanceInput> oldInstInputs = oldResource.safeGetComponentInstanceInput(INSTANCE1);
91 when(toscaOperationFacade.updateComponentInstanceInputs(newResource, INSTANCE1, newInstanceInputs))
92 .thenReturn(StorageOperationStatus.OK);
93 ActionStatus actionStatus = testInstance.mergeComponentInstanceInputs(oldInstInputs, oldResource.getInputs(), newResource, INSTANCE1);
94 assertEquals(actionStatus, ActionStatus.OK);
95 verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(oldInstInputs, oldResource.getInputs(), newInstanceInputs, newResource.getInputs());
99 public void mergeInstanceProps_failure() {
100 List<ComponentInstanceInput> newInstanceInputs = newResource.safeGetComponentInstanceInput(INSTANCE1);
101 List<ComponentInstanceInput> oldInstInputs = oldResource.safeGetComponentInstanceInput(INSTANCE1);
102 when(toscaOperationFacade.updateComponentInstanceInputs(newResource, INSTANCE1, newInstanceInputs))
103 .thenReturn(StorageOperationStatus.GENERAL_ERROR);
104 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
105 ActionStatus actionStatus = testInstance.mergeComponentInstanceInputs(oldInstInputs, oldResource.getInputs(), newResource, INSTANCE1);
106 assertEquals(actionStatus, ActionStatus.GENERAL_ERROR);
107 verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(oldInstInputs, oldResource.getInputs(), newInstanceInputs, newResource.getInputs());
110 private void verifyMergeBLCalled(Resource oldResource, Resource newResource) {
111 List<ComponentInstanceInput> instance1oldInputs = oldResource.getComponentInstancesInputs().get(INSTANCE1);
112 List<ComponentInstanceInput> instance1newInputs = newResource.getComponentInstancesInputs().get(INSTANCE1);
113 List<ComponentInstanceInput> instance2oldInputs = oldResource.getComponentInstancesInputs().get(INSTANCE2);
114 List<ComponentInstanceInput> instance2newInputs = newResource.getComponentInstancesInputs().get(INSTANCE2);
115 verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(instance1oldInputs, oldResource.getInputs(), instance1newInputs, newResource.getInputs());
116 verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(instance2oldInputs, oldResource.getInputs(), instance2newInputs, newResource.getInputs());