2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.merge.property;
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;
38 import java.util.Collections;
39 import java.util.List;
41 import static org.junit.Assert.assertEquals;
42 import static org.mockito.Mockito.*;
44 public class ComponentInstancePropertiesMergeBLTest {
46 private static final String INSTANCE1 = "instance1";
47 private static final String INSTANCE2 = "instance2";
48 private static final String OLD_INSTANCE1 = "old.instance1";
49 private static final String OLD_INSTANCE2 = "old.instance2";
50 private static final String NEW_INSTANCE1 = "new.instance1";
51 private static final String NEW_INSTANCE2 = "new.instance2";
54 private ComponentInstancePropertiesMergeBL testInstance;
57 private DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic;
60 private ToscaOperationFacade toscaOperationFacade;
63 private ComponentsUtils componentsUtils;
65 private Resource oldResource;
66 private Resource newResource;
69 public void setUp() throws Exception {
70 MockitoAnnotations.initMocks(this);
71 oldResource = new ResourceBuilder()
72 .addComponentInstance(INSTANCE1, OLD_INSTANCE1)
73 .addComponentInstance(INSTANCE2, OLD_INSTANCE2)
74 .addInstanceProperty(OLD_INSTANCE1, "property1")
75 .addInstanceProperty(OLD_INSTANCE1, "property2")
76 .addInstanceProperty(OLD_INSTANCE2, "property3")
78 .addInput("input2").build();
80 newResource = new ResourceBuilder()
81 .addComponentInstance(INSTANCE1, NEW_INSTANCE1)
82 .addComponentInstance(INSTANCE2, NEW_INSTANCE2)
83 .addInstanceProperty(NEW_INSTANCE1, "property11")
84 .addInstanceProperty(NEW_INSTANCE1, "property12")
85 .addInstanceProperty(NEW_INSTANCE2, "property13")
87 .addInput("input12").build();
91 public void mergeInstancesPropsAndInputs_mergeInstanceProps() throws Exception {
92 when(toscaOperationFacade.updateComponentInstancePropsToComponent(newResource.getComponentInstancesProperties(), newResource.getUniqueId()))
93 .thenReturn(Either.left(Collections.emptyMap()));
94 ActionStatus actionStatus = testInstance.mergeComponents(oldResource, newResource);
95 assertEquals(ActionStatus.OK, actionStatus);
96 verifyMergeBLCalled(oldResource, newResource);
100 public void mergeInstancesProps_failure() throws Exception {
101 when(toscaOperationFacade.updateComponentInstancePropsToComponent(newResource.getComponentInstancesProperties(), newResource.getUniqueId()))
102 .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);
110 public void mergeInstanceProps() throws Exception {
111 List<ComponentInstanceProperty> newInstanceProps = newResource.safeGetComponentInstanceProperties(NEW_INSTANCE1);
112 List<ComponentInstanceProperty> oldInstProps = oldResource.safeGetComponentInstanceProperties(OLD_INSTANCE1);
113 when(toscaOperationFacade.updateComponentInstanceProperties(newResource, NEW_INSTANCE1, newInstanceProps))
114 .thenReturn(StorageOperationStatus.OK);
115 ActionStatus actionStatus = testInstance.mergeComponentInstanceProperties(oldInstProps, oldResource.getInputs(), newResource, NEW_INSTANCE1);
116 assertEquals(ActionStatus.OK, actionStatus);
117 verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(oldInstProps, oldResource.getInputs(), newInstanceProps, newResource.getInputs());
121 public void mergeInstanceProps_failure() throws Exception {
122 List<ComponentInstanceProperty> newInstanceProps = newResource.safeGetComponentInstanceProperties(NEW_INSTANCE1);
123 List<ComponentInstanceProperty> oldInstProps = oldResource.safeGetComponentInstanceProperties(OLD_INSTANCE1);
124 when(toscaOperationFacade.updateComponentInstanceProperties(newResource, NEW_INSTANCE1, newInstanceProps))
125 .thenReturn(StorageOperationStatus.GENERAL_ERROR);
126 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
127 ActionStatus actionStatus = testInstance.mergeComponentInstanceProperties(oldInstProps, oldResource.getInputs(), newResource, NEW_INSTANCE1);
128 assertEquals(ActionStatus.GENERAL_ERROR, actionStatus);
129 verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(oldInstProps, oldResource.getInputs(), newInstanceProps, newResource.getInputs());
132 private void verifyMergeBLCalled(Resource oldResource, Resource newResource) {
133 List<ComponentInstanceProperty> instance1oldProps = oldResource.getComponentInstancesProperties().get(OLD_INSTANCE1);
134 List<ComponentInstanceProperty> instance1newProps = newResource.getComponentInstancesProperties().get(NEW_INSTANCE1);
135 List<ComponentInstanceProperty> instance2oldProps = oldResource.getComponentInstancesProperties().get(OLD_INSTANCE2);
136 List<ComponentInstanceProperty> instance2newProps = newResource.getComponentInstancesProperties().get(NEW_INSTANCE2);
137 verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(instance1oldProps, oldResource.getInputs(), instance1newProps, newResource.getInputs());
138 verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(instance2oldProps, oldResource.getInputs(), instance2newProps, newResource.getInputs());