c8dde95c2b8e5635f425c89989e12fc5387a429e
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.merge.instance;
22
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;
50
51 import java.util.List;
52
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 {
61
62     private static final String INSTANCE_ID1 = "inst1";
63     private static final User USER = new User();
64
65     @InjectMocks
66     private ComponentInstancePropsAndInputsMerge testInstance;
67
68     @Mock
69     private ToscaOperationFacade toscaOperationFacade;
70
71     @Mock
72     private ComponentsUtils componentsUtils;
73
74     @Mock
75     private ComponentInstancePropertiesMergeBL componentInstancePropertiesMergeBL;
76
77     @Mock
78     private ComponentInstanceInputsMergeBL componentInstanceInputsMergeBL;
79
80     @Mock
81     private ComponentInstanceInputsRedeclareHandler componentInstanceInputsRedeclareHandler;
82
83     private Resource resourceToUpdate;
84
85     private DataForMergeHolder oldDataHolder;
86
87     @Before
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")
94                 .addInput("input1")
95                 .addInput("input2")
96                 .setUniqueId("resourceId").build();
97
98         List<InputDefinition> oldInputs = ObjectGenerator.buildInputs("input1");
99         List<ComponentInstanceProperty> oldInstProps = ObjectGenerator.buildInstanceProperties("instProp1", "instProp3");
100         List<ComponentInstanceInput> oldInstInputs = ObjectGenerator.buildInstanceInputs("instInput1", "instInput3");
101
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"));
107     }
108
109     @Test
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");
114
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);
121
122         ArgumentCaptor<ComponentParametersView> parametersViewCaptor = ArgumentCaptor.forClass(ComponentParametersView.class);
123
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());
131     }
132
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);
140     }
141
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);
150     }
151
152
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);
165     }
166
167     private void assertComponentFilter(ComponentParametersView value) {
168         assertFalse(value.isIgnoreComponentInstances());
169         assertFalse(value.isIgnoreComponentInstancesProperties());
170         assertFalse(value.isIgnoreComponentInstancesInputs());
171         assertFalse(value.isIgnoreArtifacts());
172     }
173 }