925f89b5ebe03baae1332e80232f823d6b9e4a5e
[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.dao.api.ActionStatus;
35 import org.openecomp.sdc.be.impl.ComponentsUtils;
36 import org.openecomp.sdc.be.model.Component;
37 import org.openecomp.sdc.be.model.ComponentInstanceInput;
38 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
39 import org.openecomp.sdc.be.model.ComponentParametersView;
40 import org.openecomp.sdc.be.model.InputDefinition;
41 import org.openecomp.sdc.be.model.Resource;
42 import org.openecomp.sdc.be.model.Service;
43 import org.openecomp.sdc.be.model.User;
44 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
45 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
46 import org.openecomp.sdc.exception.ResponseFormat;
47
48 import java.util.List;
49
50 import static org.junit.Assert.assertEquals;
51 import static org.junit.Assert.assertFalse;
52 import static org.mockito.ArgumentMatchers.any;
53 import static org.mockito.ArgumentMatchers.anyList;
54 import static org.mockito.ArgumentMatchers.anyString;
55 import static org.mockito.Mockito.verifyZeroInteractions;
56 import static org.mockito.Mockito.when;
57 public class ComponentInstancePropsAndInputsMergeTest {
58
59     private static final String INSTANCE_ID1 = "inst1";
60     private static final User USER = new User();
61
62     @InjectMocks
63     private ComponentInstancePropsAndInputsMerge testInstance;
64
65     @Mock
66     private ToscaOperationFacade toscaOperationFacade;
67
68     @Mock
69     private ComponentsUtils componentsUtils;
70
71     @Mock
72     private ComponentInstancePropertiesMergeBL componentInstancePropertiesMergeBL;
73
74     @Mock
75     private ComponentInstanceInputsMergeBL componentInstanceInputsMergeBL;
76
77     @Mock
78     private ComponentInstanceInputsRedeclareHandler componentInstanceInputsRedeclareHandler;
79
80     private Resource resourceToUpdate;
81
82     private DataForMergeHolder oldDataHolder;
83
84     @Before
85     public void setUp() throws Exception {
86         MockitoAnnotations.initMocks(this);
87         resourceToUpdate =  new ResourceBuilder().addInstanceInput(INSTANCE_ID1, "instInput1")
88                 .addInstanceInput(INSTANCE_ID1, "instInput2")
89                 .addInstanceProperty(INSTANCE_ID1, "instProp1")
90                 .addInstanceProperty(INSTANCE_ID1, "instProp2")
91                 .addInput("input1")
92                 .addInput("input2")
93                 .setUniqueId("resourceId").build();
94
95         List<InputDefinition> oldInputs = ObjectGenerator.buildInputs("input1");
96         List<ComponentInstanceProperty> oldInstProps = ObjectGenerator.buildInstanceProperties("instProp1", "instProp3");
97         List<ComponentInstanceInput> oldInstInputs = ObjectGenerator.buildInstanceInputs("instInput1", "instInput3");
98
99         oldDataHolder = new DataForMergeHolder();
100         oldDataHolder.setOrigComponentInputs(oldInputs);
101         oldDataHolder.setOrigComponentInstanceProperties(oldInstProps);
102         oldDataHolder.setOrigComponentInstanceInputs(oldInstInputs);
103     }
104
105     @Test
106     public void mergeDataAfterCreate() throws Exception {
107         List<InputDefinition> oldInputs = ObjectGenerator.buildInputs("input1");
108         List<ComponentInstanceProperty> oldInstProps = ObjectGenerator.buildInstanceProperties("instProp1", "instProp3");
109         List<ComponentInstanceInput> oldInstInputs = ObjectGenerator.buildInstanceInputs("instInput1", "instInput3");
110
111         DataForMergeHolder dataForMergeHolder = new DataForMergeHolder();
112         dataForMergeHolder.setOrigComponentInputs(oldInputs);
113         dataForMergeHolder.setOrigComponentInstanceProperties(oldInstProps);
114         dataForMergeHolder.setOrigComponentInstanceInputs(oldInstInputs);
115         Resource currInstanceOriginType = new Resource();
116         dataForMergeHolder.setCurrInstanceNode(currInstanceOriginType);
117
118         ArgumentCaptor<ComponentParametersView> parametersViewCaptor = ArgumentCaptor.forClass(ComponentParametersView.class);
119
120         when(toscaOperationFacade.getToscaElement(Mockito.eq("resourceId"), parametersViewCaptor.capture())).thenReturn(Either.left(resourceToUpdate));
121         when(componentInstanceInputsMergeBL.mergeComponentInstanceInputs(oldInstInputs, oldInputs, resourceToUpdate, INSTANCE_ID1)).thenReturn(ActionStatus.OK);
122         when(componentInstancePropertiesMergeBL.mergeComponentInstanceProperties(oldInstProps, oldInputs, resourceToUpdate, INSTANCE_ID1)).thenReturn(ActionStatus.OK);
123         when(componentInstanceInputsRedeclareHandler.redeclareComponentInputsForInstance(resourceToUpdate, INSTANCE_ID1, currInstanceOriginType, oldInputs)).thenReturn(ActionStatus.OK);
124         Component mergeResult = testInstance.mergeDataAfterCreate(USER, dataForMergeHolder, resourceToUpdate, INSTANCE_ID1);
125         assertEquals(mergeResult, resourceToUpdate);
126         assertComponentFilter(parametersViewCaptor.getValue());
127     }
128
129     @Test(expected = ComponentException.class)
130     public void mergeDataAfterCreate_failedToMergeComponentInstanceInputs() throws Exception {
131         ResponseFormat errorResponse = new ResponseFormat();
132         when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(errorResponse);
133         when(componentInstanceInputsMergeBL.mergeComponentInstanceInputs(anyList(), anyList(), any(Component.class), anyString())).thenReturn(ActionStatus.GENERAL_ERROR);
134         testInstance.mergeDataAfterCreate(USER, new DataForMergeHolder(), new Service(), "inst1");
135         verifyZeroInteractions(componentInstanceInputsRedeclareHandler, componentInstancePropertiesMergeBL, toscaOperationFacade);
136     }
137
138     @Test(expected = ComponentException.class)
139     public void mergeDataAfterCreate_failedToMergeComponentInstProps() throws Exception {
140         ResponseFormat errorResponse = new ResponseFormat();
141         when(componentInstanceInputsMergeBL.mergeComponentInstanceInputs(anyList(), anyList(), any(Component.class), anyString())).thenReturn(ActionStatus.OK);
142         when(componentInstancePropertiesMergeBL.mergeComponentInstanceProperties(anyList(), anyList(), any(Component.class), anyString())).thenReturn(ActionStatus.GENERAL_ERROR);
143         when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(errorResponse);
144         testInstance.mergeDataAfterCreate(USER, new DataForMergeHolder(), new Service(), "inst1");
145         verifyZeroInteractions(componentInstanceInputsRedeclareHandler, toscaOperationFacade);
146     }
147
148
149     @Test(expected = ComponentException.class)
150     public void mergeDataAfterCreate_mergeInputs_FailedToFetchResource() throws Exception {
151         ResponseFormat errorResponse = new ResponseFormat();
152         when(componentInstanceInputsMergeBL.mergeComponentInstanceInputs(anyList(), anyList(), any(Component.class), anyString())).thenReturn(ActionStatus.OK);
153         when(componentInstancePropertiesMergeBL.mergeComponentInstanceProperties(anyList(), anyList(), any(Component.class), anyString())).thenReturn(ActionStatus.OK);
154         when(toscaOperationFacade.getToscaElement(any(), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
155         when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
156         when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(errorResponse);
157         DataForMergeHolder dataHolder = new DataForMergeHolder();
158         dataHolder.setOrigComponentInputs(ObjectGenerator.buildInputs("input1", "input2"));
159         testInstance.mergeDataAfterCreate(USER, dataHolder, new Service(), "inst1");
160         verifyZeroInteractions(componentInstanceInputsRedeclareHandler);
161     }
162
163     private void assertComponentFilter(ComponentParametersView value) {
164         assertFalse(value.isIgnoreComponentInstances());
165         assertFalse(value.isIgnoreComponentInstancesProperties());
166         assertFalse(value.isIgnoreComponentInstancesInputs());
167         assertFalse(value.isIgnoreArtifacts());
168     }
169 }