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.*;
46 public class ComponentInstanceInputsMergeBLTest {
48 public static final String INSTANCE1 = "instance1";
49 public static final String INSTANCE2 = "instance2";
51 private ComponentInstanceInputsMergeBL testInstance;
54 private DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic;
57 private ToscaOperationFacade toscaOperationFacade;
60 private ComponentsUtils componentsUtils;
62 private Resource oldResource;
63 private Resource newResource;
66 public void setUp() throws Exception {
67 MockitoAnnotations.initMocks(this);
68 ComponentInstance instance1 = new ComponentInstanceBuilder().setId(INSTANCE1).setName(INSTANCE1).build();
69 ComponentInstance instance2 = new ComponentInstanceBuilder().setId(INSTANCE2).setName(INSTANCE2).build();
71 oldResource = new ResourceBuilder()
72 .addInstanceInput(INSTANCE1, "property1")
73 .addInstanceInput(INSTANCE1, "property2")
74 .addInstanceInput(INSTANCE2, "property3")
75 .addComponentInstance(instance1)
76 .addComponentInstance(instance2)
78 .addInput("input2").build();
80 newResource = new ResourceBuilder()
81 .addInstanceInput(INSTANCE1, "property11")
82 .addInstanceInput(INSTANCE1, "property12")
83 .addInstanceInput(INSTANCE2, "property13")
84 .addComponentInstance(instance1)
85 .addComponentInstance(instance2)
87 .addInput("input12").build();
91 public void mergeInstancesInputs() throws Exception {
92 when(toscaOperationFacade.updateComponentInstanceInputsToComponent(newResource.getComponentInstancesInputs(), newResource.getUniqueId())).thenReturn(Either.left(Collections.emptyMap()));
93 ActionStatus actionStatus = testInstance.mergeComponents(oldResource, newResource);
94 assertEquals(actionStatus, ActionStatus.OK);
95 verifyMergeBLCalled(oldResource, newResource);
99 public void mergeInstancesInputs_failure() {
100 when(toscaOperationFacade.updateComponentInstanceInputsToComponent(newResource.getComponentInstancesInputs(), newResource.getUniqueId())).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
101 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
102 verifyNoMoreInteractions(toscaOperationFacade, propertyValuesMergingBusinessLogic);
103 ActionStatus actionStatus = testInstance.mergeComponents(oldResource, newResource);
104 assertEquals(ActionStatus.GENERAL_ERROR, actionStatus);
108 public void mergeInstanceProps() {
109 List<ComponentInstanceInput> newInstanceInputs = newResource.safeGetComponentInstanceInput(INSTANCE1);
110 List<ComponentInstanceInput> oldInstInputs = oldResource.safeGetComponentInstanceInput(INSTANCE1);
111 when(toscaOperationFacade.updateComponentInstanceInputs(newResource, INSTANCE1, newInstanceInputs))
112 .thenReturn(StorageOperationStatus.OK);
113 ActionStatus actionStatus = testInstance.mergeComponentInstanceInputs(oldInstInputs, oldResource.getInputs(), newResource, INSTANCE1);
114 assertEquals(actionStatus, ActionStatus.OK);
115 verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(oldInstInputs, oldResource.getInputs(), newInstanceInputs, newResource.getInputs());
119 public void mergeInstanceProps_failure() {
120 List<ComponentInstanceInput> newInstanceInputs = newResource.safeGetComponentInstanceInput(INSTANCE1);
121 List<ComponentInstanceInput> oldInstInputs = oldResource.safeGetComponentInstanceInput(INSTANCE1);
122 when(toscaOperationFacade.updateComponentInstanceInputs(newResource, INSTANCE1, newInstanceInputs))
123 .thenReturn(StorageOperationStatus.GENERAL_ERROR);
124 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
125 ActionStatus actionStatus = testInstance.mergeComponentInstanceInputs(oldInstInputs, oldResource.getInputs(), newResource, INSTANCE1);
126 assertEquals(actionStatus, ActionStatus.GENERAL_ERROR);
127 verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(oldInstInputs, oldResource.getInputs(), newInstanceInputs, newResource.getInputs());
130 private void verifyMergeBLCalled(Resource oldResource, Resource newResource) {
131 List<ComponentInstanceInput> instance1oldInputs = oldResource.getComponentInstancesInputs().get(INSTANCE1);
132 List<ComponentInstanceInput> instance1newInputs = newResource.getComponentInstancesInputs().get(INSTANCE1);
133 List<ComponentInstanceInput> instance2oldInputs = oldResource.getComponentInstancesInputs().get(INSTANCE2);
134 List<ComponentInstanceInput> instance2newInputs = newResource.getComponentInstancesInputs().get(INSTANCE2);
135 verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(instance1oldInputs, oldResource.getInputs(), instance1newInputs, newResource.getInputs());
136 verify(propertyValuesMergingBusinessLogic).mergeInstanceDataDefinitions(instance2oldInputs, oldResource.getInputs(), instance2newInputs, newResource.getInputs());