1 package org.openecomp.sdc.be.components.merge.property;
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;
8 import java.util.Collections;
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;
24 import fj.data.Either;
26 public class ComponentInstancePropertiesMergeBLTest {
28 private static final String INSTANCE1 = "instance1";
29 private static final String INSTANCE2 = "instance2";
32 private ComponentInstancePropertiesMergeBL testInstance;
35 private DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic;
38 private ToscaOperationFacade toscaOperationFacade;
41 private ComponentsUtils componentsUtils;
43 private Resource oldResource, newResource;
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")
53 .addInput("input2").build();
55 newResource = new ResourceBuilder()
56 .addInstanceProperty(INSTANCE1, "property11")
57 .addInstanceProperty(INSTANCE1, "property12")
58 .addInstanceProperty(INSTANCE2, "property13")
60 .addInput("input12").build();
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);
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);
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());
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());
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());