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.instance;
23 import fj.data.Either;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.ArgumentCaptor;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.mockito.MockitoAnnotations;
31 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
32 import org.openecomp.sdc.be.components.utils.ObjectGenerator;
33 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
34 import org.openecomp.sdc.be.config.ConfigurationManager;
35 import org.openecomp.sdc.be.dao.api.ActionStatus;
36 import org.openecomp.sdc.be.impl.ComponentsUtils;
37 import org.openecomp.sdc.be.model.Component;
38 import org.openecomp.sdc.be.model.ComponentInstanceInput;
39 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
40 import org.openecomp.sdc.be.model.ComponentParametersView;
41 import org.openecomp.sdc.be.model.InputDefinition;
42 import org.openecomp.sdc.be.model.Resource;
43 import org.openecomp.sdc.be.model.Service;
44 import org.openecomp.sdc.be.model.User;
45 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
46 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
47 import org.openecomp.sdc.common.impl.ExternalConfiguration;
48 import org.openecomp.sdc.common.impl.FSConfigurationSource;
49 import org.openecomp.sdc.exception.ResponseFormat;
51 import java.util.List;
53 import static org.junit.Assert.assertEquals;
54 import static org.junit.Assert.assertFalse;
55 import static org.mockito.ArgumentMatchers.any;
56 import static org.mockito.ArgumentMatchers.anyList;
57 import static org.mockito.ArgumentMatchers.anyString;
58 import static org.mockito.Mockito.verifyZeroInteractions;
59 import static org.mockito.Mockito.when;
60 public class ComponentInstancePropsAndInputsMergeTest {
62 private static final String INSTANCE_ID1 = "inst1";
63 private static final User USER = new User();
66 private ComponentInstancePropsAndInputsMerge testInstance;
69 private ToscaOperationFacade toscaOperationFacade;
72 private ComponentsUtils componentsUtils;
75 private ComponentInstancePropertiesMergeBL componentInstancePropertiesMergeBL;
78 private ComponentInstanceInputsMergeBL componentInstanceInputsMergeBL;
81 private ComponentInstanceInputsRedeclareHandler componentInstanceInputsRedeclareHandler;
83 private Resource resourceToUpdate;
85 private DataForMergeHolder oldDataHolder;
88 public void setUp() throws Exception {
89 MockitoAnnotations.initMocks(this);
90 resourceToUpdate = new ResourceBuilder().addInstanceInput(INSTANCE_ID1, "instInput1")
91 .addInstanceInput(INSTANCE_ID1, "instInput2")
92 .addInstanceProperty(INSTANCE_ID1, "instProp1")
93 .addInstanceProperty(INSTANCE_ID1, "instProp2")
96 .setUniqueId("resourceId").build();
98 List<InputDefinition> oldInputs = ObjectGenerator.buildInputs("input1");
99 List<ComponentInstanceProperty> oldInstProps = ObjectGenerator.buildInstanceProperties("instProp1", "instProp3");
100 List<ComponentInstanceInput> oldInstInputs = ObjectGenerator.buildInstanceInputs("instInput1", "instInput3");
102 oldDataHolder = new DataForMergeHolder();
103 oldDataHolder.setOrigComponentInputs(oldInputs);
104 oldDataHolder.setOrigComponentInstanceProperties(oldInstProps);
105 oldDataHolder.setOrigComponentInstanceInputs(oldInstInputs);
106 new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be"));
110 public void mergeDataAfterCreate() throws Exception {
111 List<InputDefinition> oldInputs = ObjectGenerator.buildInputs("input1");
112 List<ComponentInstanceProperty> oldInstProps = ObjectGenerator.buildInstanceProperties("instProp1", "instProp3");
113 List<ComponentInstanceInput> oldInstInputs = ObjectGenerator.buildInstanceInputs("instInput1", "instInput3");
115 DataForMergeHolder dataForMergeHolder = new DataForMergeHolder();
116 dataForMergeHolder.setOrigComponentInputs(oldInputs);
117 dataForMergeHolder.setOrigComponentInstanceProperties(oldInstProps);
118 dataForMergeHolder.setOrigComponentInstanceInputs(oldInstInputs);
119 Resource currInstanceOriginType = new Resource();
120 dataForMergeHolder.setCurrInstanceNode(currInstanceOriginType);
122 ArgumentCaptor<ComponentParametersView> parametersViewCaptor = ArgumentCaptor.forClass(ComponentParametersView.class);
124 when(toscaOperationFacade.getToscaElement(Mockito.eq("resourceId"), parametersViewCaptor.capture())).thenReturn(Either.left(resourceToUpdate));
125 when(componentInstanceInputsMergeBL.mergeComponentInstanceInputs(oldInstInputs, oldInputs, resourceToUpdate, INSTANCE_ID1)).thenReturn(ActionStatus.OK);
126 when(componentInstancePropertiesMergeBL.mergeComponentInstanceProperties(oldInstProps, oldInputs, resourceToUpdate, INSTANCE_ID1)).thenReturn(ActionStatus.OK);
127 when(componentInstanceInputsRedeclareHandler.redeclareComponentInputsForInstance(resourceToUpdate, INSTANCE_ID1, currInstanceOriginType, oldInputs)).thenReturn(ActionStatus.OK);
128 Component mergeResult = testInstance.mergeDataAfterCreate(USER, dataForMergeHolder, resourceToUpdate, INSTANCE_ID1);
129 assertEquals(mergeResult, resourceToUpdate);
130 assertComponentFilter(parametersViewCaptor.getValue());
133 @Test(expected = ComponentException.class)
134 public void mergeDataAfterCreate_failedToMergeComponentInstanceInputs() throws Exception {
135 ResponseFormat errorResponse = new ResponseFormat();
136 when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(errorResponse);
137 when(componentInstanceInputsMergeBL.mergeComponentInstanceInputs(anyList(), anyList(), any(Component.class), anyString())).thenReturn(ActionStatus.GENERAL_ERROR);
138 testInstance.mergeDataAfterCreate(USER, new DataForMergeHolder(), new Service(), "inst1");
139 verifyZeroInteractions(componentInstanceInputsRedeclareHandler, componentInstancePropertiesMergeBL, toscaOperationFacade);
142 @Test(expected = ComponentException.class)
143 public void mergeDataAfterCreate_failedToMergeComponentInstProps() throws Exception {
144 ResponseFormat errorResponse = new ResponseFormat();
145 when(componentInstanceInputsMergeBL.mergeComponentInstanceInputs(anyList(), anyList(), any(Component.class), anyString())).thenReturn(ActionStatus.OK);
146 when(componentInstancePropertiesMergeBL.mergeComponentInstanceProperties(anyList(), anyList(), any(Component.class), anyString())).thenReturn(ActionStatus.GENERAL_ERROR);
147 when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(errorResponse);
148 testInstance.mergeDataAfterCreate(USER, new DataForMergeHolder(), new Service(), "inst1");
149 verifyZeroInteractions(componentInstanceInputsRedeclareHandler, toscaOperationFacade);
153 @Test(expected = ComponentException.class)
154 public void mergeDataAfterCreate_mergeInputs_FailedToFetchResource() throws Exception {
155 ResponseFormat errorResponse = new ResponseFormat();
156 when(componentInstanceInputsMergeBL.mergeComponentInstanceInputs(anyList(), anyList(), any(Component.class), anyString())).thenReturn(ActionStatus.OK);
157 when(componentInstancePropertiesMergeBL.mergeComponentInstanceProperties(anyList(), anyList(), any(Component.class), anyString())).thenReturn(ActionStatus.OK);
158 when(toscaOperationFacade.getToscaElement(any(), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
159 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
160 when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(errorResponse);
161 DataForMergeHolder dataHolder = new DataForMergeHolder();
162 dataHolder.setOrigComponentInputs(ObjectGenerator.buildInputs("input1", "input2"));
163 testInstance.mergeDataAfterCreate(USER, dataHolder, new Service(), "inst1");
164 verifyZeroInteractions(componentInstanceInputsRedeclareHandler);
167 private void assertComponentFilter(ComponentParametersView value) {
168 assertFalse(value.isIgnoreComponentInstances());
169 assertFalse(value.isIgnoreComponentInstancesProperties());
170 assertFalse(value.isIgnoreComponentInstancesInputs());
171 assertFalse(value.isIgnoreArtifacts());