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.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;
40 import java.util.Collections;
41 import java.util.List;
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;
48 public class ComponentInstanceInputsMergeBLTest {
50 public static final String INSTANCE1 = "instance1";
51 public static final String INSTANCE2 = "instance2";
53 private ComponentInstanceInputsMergeBL testInstance;
56 private DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic;
59 private ToscaOperationFacade toscaOperationFacade;
62 private ComponentsUtils componentsUtils;
64 private Resource oldResource;
65 private Resource newResource;
68 public void setUp() throws Exception {
69 MockitoAnnotations.initMocks(this);
70 ComponentInstance instance1 = new ComponentInstanceBuilder().setId(INSTANCE1).setName(INSTANCE1).build();
71 ComponentInstance instance2 = new ComponentInstanceBuilder().setId(INSTANCE2).setName(INSTANCE2).build();
73 oldResource = new ResourceBuilder()
74 .addInstanceInput(INSTANCE1, "property1")
75 .addInstanceInput(INSTANCE1, "property2")
76 .addInstanceInput(INSTANCE2, "property3")
77 .addComponentInstance(instance1)
78 .addComponentInstance(instance2)
80 .addInput("input2").build();
82 newResource = new ResourceBuilder()
83 .addInstanceInput(INSTANCE1, "property11")
84 .addInstanceInput(INSTANCE1, "property12")
85 .addInstanceInput(INSTANCE2, "property13")
86 .addComponentInstance(instance1)
87 .addComponentInstance(instance2)
89 .addInput("input12").build();
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);
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);
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());
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());
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());