Change to enable SDC list type input
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / InputsBusinessLogicTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.impl;
22
23 import fj.data.Either;
24 import org.apache.commons.lang3.tuple.ImmutablePair;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.ArgumentCaptor;
28 import org.mockito.Captor;
29 import org.mockito.InjectMocks;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.mockito.MockitoAnnotations;
33 import org.openecomp.sdc.be.components.property.PropertyDeclarationOrchestrator;
34 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
35 import org.openecomp.sdc.be.components.validation.UserValidations;
36 import org.openecomp.sdc.be.dao.api.ActionStatus;
37 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
38 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
39 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
40 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
41 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
42 import org.openecomp.sdc.be.impl.ComponentsUtils;
43 import org.openecomp.sdc.be.model.*;
44 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
45 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
46 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
47 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
48 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
49 import org.openecomp.sdc.be.user.IUserBusinessLogic;
50 import org.openecomp.sdc.exception.ResponseFormat;
51
52 import java.util.*;
53
54 import static org.junit.Assert.assertEquals;
55 import static org.junit.Assert.assertTrue;
56 import static org.mockito.ArgumentMatchers.*;
57 import static org.mockito.Mockito.times;
58 import static org.mockito.Mockito.verify;
59 import static org.mockito.Mockito.when;
60
61 public class InputsBusinessLogicTest {
62
63     private static final String COMPONENT_INSTANCE_ID = "instanceId";
64     private static final String COMPONENT_ID = "componentId";
65     private static final String USER_ID = "userId";
66     private static final String INSTANCE_INPUT_ID = "inputId";
67     private static final String LISTINPUT_NAME = "listInput";
68     private static final String LISTINPUT_SCHEMA_TYPE = "org.onap.datatypes.listinput";
69     private static final String LISTINPUT_PROP1_NAME = "prop1";
70     private static final String LISTINPUT_PROP1_TYPE = "string";
71     private static final String LISTINPUT_PROP2_NAME = "prop2";
72     private static final String LISTINPUT_PROP2_TYPE = "integer";
73
74     @Mock
75     private ComponentsUtils componentsUtilsMock;
76
77     @Mock
78     private IUserBusinessLogic userAdminMock;
79
80     @Mock
81     private ToscaOperationFacade toscaOperationFacadeMock;
82
83     @Mock
84     private UserValidations userValidations;
85
86     @Mock
87     private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
88
89     @Mock
90     private IGraphLockOperation graphLockOperation;
91
92     @Mock
93     private PropertyDeclarationOrchestrator propertyDeclarationOrchestrator;
94
95     @Mock
96     private ApplicationDataTypeCache applicationDataTypeCache;
97
98     @Mock
99     private PropertyOperation propertyOperation;
100
101     @Mock
102     private TitanDao titanDao;
103
104     @Mock
105     private DataTypeBusinessLogic dataTypeBusinessLogic;
106
107     @InjectMocks
108     private InputsBusinessLogic testInstance;
109
110     private Service service;
111
112     @Captor
113     ArgumentCaptor<Map<String, DataTypeDefinition>> dataTypesMapCaptor;
114
115     @Before
116     public void setUp() throws Exception {
117         MockitoAnnotations.initMocks(this);
118         service = new Service();
119         service.setUniqueId(COMPONENT_ID);
120         service.setLastUpdaterUserId(USER_ID);
121         service.setIsDeleted(false);
122         service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
123
124         // add a ComponentInstance
125         ComponentInstance componentInstance = new ComponentInstance();
126         componentInstance.setUniqueId(COMPONENT_INSTANCE_ID);
127         service.setComponentInstances(Collections.singletonList(componentInstance));
128
129         // add inputs to the ComponentInstance
130         Map<String, List<ComponentInstanceInput>> instanceInputMap = new HashMap<>();
131         ComponentInstanceInput componentInstanceInput = new ComponentInstanceInput();
132         componentInstanceInput.setInputId(INSTANCE_INPUT_ID);
133         instanceInputMap.put(COMPONENT_INSTANCE_ID, Collections.singletonList(componentInstanceInput));
134         instanceInputMap.put("someInputId", Collections.singletonList(new ComponentInstanceInput()));
135         service.setComponentInstancesInputs(instanceInputMap);
136
137         when(userValidations.validateUserExists(eq(USER_ID), anyString(), eq(false))).thenReturn(new User());
138         when(userAdminMock.getUser(USER_ID, false)).thenReturn(Either.left(new User()));
139     }
140
141     @Test
142     public void getComponentInstanceInputs_ComponentInstanceNotExist() throws Exception {
143         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
144         Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, "nonExisting");
145         assertTrue(componentInstanceInputs.isRight());
146         verify(componentsUtilsMock).getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
147     }
148
149     @Test
150     public void getComponentInstanceInputs_emptyInputsMap() throws Exception {
151         service.setComponentInstancesInputs(Collections.emptyMap());
152         getComponents_emptyInputs(service);
153     }
154
155     @Test
156     public void getComponentInstanceInputs_nullInputsMap() throws Exception {
157         service.setComponentInstancesInputs(null);
158         getComponents_emptyInputs(service);
159     }
160
161     @Test
162     public void getComponentInstanceInputs_instanceHasNoInputs() throws Exception {
163         service.setComponentInstancesInputs(Collections.singletonMap("someInputId", new ArrayList<>()));
164         getComponents_emptyInputs(service);
165     }
166
167     @Test
168     public void getComponentInstanceInputs() throws Exception {
169         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
170         Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, COMPONENT_INSTANCE_ID);
171         assertEquals("inputId", componentInstanceInputs.left().value().get(0).getInputId());
172     }
173
174     @Test
175     public void testGetInputs() {
176         String userId = "userId";
177         String componentId = "compId";
178         Component component = new Resource();
179         when(toscaOperationFacadeMock.getToscaElement(Mockito.any(String.class), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(component));
180         testInstance.getInputs(userId, componentId);
181         assertEquals(null, component.getInputs());
182     }
183
184     @Test
185     public void testGetCIPropertiesByInputId() {
186         Either<List<ComponentInstanceProperty>, ResponseFormat> result;
187         String userId = "userId";
188         String componentId = "compId";
189         Component component = new Resource();
190         List<InputDefinition> listDef = new ArrayList<>();
191         InputDefinition inputDef = new InputDefinition();
192         inputDef.setUniqueId(componentId);
193         listDef.add(inputDef);
194         component.setInputs(listDef);
195         when(toscaOperationFacadeMock.getToscaElement(Mockito.any(String.class), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(component));
196         result = testInstance.getComponentInstancePropertiesByInputId(userId, componentId, componentId, componentId);
197         assertTrue(result.isLeft());
198     }
199
200     private void getComponents_emptyInputs(Service service) {
201         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
202         Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, COMPONENT_INSTANCE_ID);
203         assertEquals(Collections.emptyList(), componentInstanceInputs.left().value());
204     }
205
206     @Test
207     public void testgetInputs_ARTIFACT_NOT_FOUND() throws Exception {
208
209         when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.ARTIFACT_NOT_FOUND)).thenReturn(ActionStatus.ARTIFACT_NOT_FOUND);
210         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
211        Either<List<InputDefinition>, ResponseFormat> responseFormatEither = testInstance.getInputs("USR01", COMPONENT_ID);
212         assertEquals(true,responseFormatEither.isRight());
213
214     }
215
216     @Test
217     public void testgetInputs_SUCCESS() throws Exception {
218         Component component = new Service();
219         InputDefinition input= new InputDefinition();
220         List inputlist = new ArrayList<>();
221         inputlist.add(input);
222         component.setInputs(inputlist);
223         when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.ARTIFACT_NOT_FOUND)).thenReturn(ActionStatus.ARTIFACT_NOT_FOUND);
224         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(component));
225         Either<List<InputDefinition>, ResponseFormat> responseFormatEither = testInstance.getInputs("USR01", COMPONENT_ID);
226         assertEquals(inputlist,responseFormatEither.left().value());
227     }
228
229     @Test
230     public void testgetComponentInstancePropertiesByInputId_Artifactnotfound() throws Exception
231     {
232         Component component = new Service();
233         InputDefinition input= new InputDefinition();
234         List inputlist = new ArrayList<>();
235         inputlist.add(input);
236         component.setInputs(inputlist);
237         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
238         Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = testInstance.getComponentInstancePropertiesByInputId("USR01", COMPONENT_ID,"INST0.1", "INPO1");
239         assertEquals(true,responseFormatEither.isRight());
240     }
241
242     @Test
243     public void testgetComponentInstancePropertiesByInputId_PARENT_ARTIFACT_NOT_FOUND() throws Exception
244     {
245         Component component = new Service();
246         InputDefinition input= new InputDefinition();
247         List inputlist = new ArrayList<>();
248         inputlist.add(input);
249         component.setInputs(inputlist);
250         ComponentInstance componentInstance = new ComponentInstance();
251         componentInstance.setUniqueId("INST0.1");
252         componentInstance.setComponentUid("RES0.1");
253         List compinstancelist = new ArrayList<>();
254         compinstancelist.add(componentInstance);
255         component.setComponentInstances(compinstancelist);
256         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(component));
257         when(toscaOperationFacadeMock.getToscaElement(eq("RES0.1"), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
258         Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = testInstance.getComponentInstancePropertiesByInputId("USR01", COMPONENT_ID,"INST0.1", "INPO1");
259         assertEquals(true,responseFormatEither.isRight());
260     }
261
262     @Test
263     public void testgetComponentInstancePropertiesByInputId() throws Exception
264     {
265         Component component = new Service();
266         InputDefinition input= new InputDefinition();
267         input.setUniqueId("INPO1");
268         List inputlist = new ArrayList<>();
269         inputlist.add(input);
270         component.setInputs(inputlist);
271         ComponentInstance componentInstance = new ComponentInstance();
272         componentInstance.setUniqueId("INST0.1");
273         componentInstance.setComponentUid("RES0.1");
274         List compinstancelist = new ArrayList<>();
275         compinstancelist.add(componentInstance);
276         component.setComponentInstances(compinstancelist);
277         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(component));
278         when(componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(Mockito.any(Component.class),eq("INPO1"))).thenReturn(compinstancelist);
279         //when(toscaOperationFacadeMock.getToscaElement(eq("RES0.1"), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
280         when(toscaOperationFacadeMock.getToscaElement(eq("RES0.1"), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(component));
281         Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = testInstance.getComponentInstancePropertiesByInputId("USR01", COMPONENT_ID,"INST0.1", "INPO1");
282         assertEquals(compinstancelist,responseFormatEither.left().value());
283     }
284
285     @Test
286     public void testgetInputsForComponentInput_ARTIFACT_NOT_FOUND() throws Exception
287     {
288         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
289         Either<List<ComponentInstanceInput>, ResponseFormat> result = testInstance.getInputsForComponentInput("USR01", COMPONENT_ID,"INPO1");
290         assertEquals(true,result.isRight());
291     }
292
293     @Test
294     public void testgetInputsForComponentInput() throws Exception
295     {
296         Component component = new Service();
297         InputDefinition input= new InputDefinition();
298         input.setUniqueId("INPO1");
299         List inputlist = new ArrayList<>();
300         inputlist.add(input);
301         component.setInputs(inputlist);
302         ComponentInstance componentInstance = new ComponentInstance();
303         componentInstance.setUniqueId("INST0.1");
304         componentInstance.setComponentUid("RES0.1");
305         List compinstancelist = new ArrayList<>();
306         compinstancelist.add(componentInstance);
307         component.setComponentInstances(compinstancelist);
308         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(component));
309         when(componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(Mockito.any(Component.class),eq("INPO1"))).thenReturn(compinstancelist);
310         Either<List<ComponentInstanceInput>, ResponseFormat> result = testInstance.getInputsForComponentInput("USR01", COMPONENT_ID,"INPO1");
311         assertEquals(true,result.isLeft());
312     }
313
314
315     private InputDefinition setUpListInput() {
316         InputDefinition listInput = new InputDefinition();
317         listInput.setName(LISTINPUT_NAME);
318         listInput.setType("list");
319         SchemaDefinition listInputSchema = new SchemaDefinition();
320         listInputSchema.setProperty(new PropertyDataDefinitionBuilder()
321                 .setType(LISTINPUT_SCHEMA_TYPE)
322                 .setIsRequired(false)
323                 .build()
324         );
325         listInput.setSchema(listInputSchema);
326         return listInput;
327     }
328
329     private ComponentInstListInput setUpCreateListInputParams() {
330         ComponentInstListInput componentInstListInput = new ComponentInstListInput();
331
332         // Create a "list input"
333         InputDefinition listInput = setUpListInput();
334         componentInstListInput.setListInput(listInput);
335
336         // Create ComponentInstancePropInputs
337         // for inputs in the ComponentInstance
338         Map<String, List<ComponentInstancePropInput>> propInputsListMap = new HashMap<>();
339         // Add 2 PropInputs. property owner is COMPONENT_INSTANCE_ID
340         List<ComponentInstancePropInput> propInputsList = new ArrayList<>();
341         ComponentInstancePropInput propInput = new ComponentInstancePropInput();
342         propInput.setName(LISTINPUT_PROP1_NAME);
343         propInput.setType(LISTINPUT_PROP1_TYPE);
344         propInput.setUniqueId(COMPONENT_INSTANCE_ID+"."+LISTINPUT_PROP1_NAME);
345         propInputsList.add(propInput);
346         propInput = new ComponentInstancePropInput();
347         propInput.setName(LISTINPUT_PROP2_NAME);
348         propInput.setType(LISTINPUT_PROP2_TYPE);
349         propInput.setUniqueId(COMPONENT_INSTANCE_ID+"."+LISTINPUT_PROP2_NAME);
350         propInputsList.add(propInput);
351         propInputsListMap.put(COMPONENT_INSTANCE_ID, propInputsList);
352         ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
353         componentInstInputsMap.setComponentInstanceInputsMap(propInputsListMap);
354         componentInstListInput.setComponentInstInputsMap(componentInstInputsMap);
355
356         return componentInstListInput;
357     }
358
359     @Test
360     public void test_createListInput_success() throws Exception {
361         ComponentInstListInput createListInputParams = setUpCreateListInputParams();
362         ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();
363         List<ComponentInstancePropInput> propInputsList = componentInstInputsMap.getComponentInstanceInputsMap().get(COMPONENT_INSTANCE_ID);
364         InputDefinition listInput = createListInputParams.getListInput();
365
366         // set up mock returns
367         // for get component object:
368         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
369         when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
370         // for data type creation (use captor):
371         when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
372         when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
373         when(propertyDeclarationOrchestrator.declarePropertiesToListInput(service, componentInstInputsMap, listInput)).thenReturn(Either.left(listInput));
374         // for BaseOperation.getAllDataTypes:
375         when(applicationDataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // don't use Collections.emptyMap
376         // for BaseOperation.validatePropertyDefaultValue:
377         when(propertyOperation.isPropertyTypeValid(any())).thenReturn(true);
378         when(propertyOperation.isPropertyInnerTypeValid(any(),any())).thenReturn(new ImmutablePair<>(listInput.getSchemaType(), true));
379         when(propertyOperation.isPropertyDefaultValueValid(any(), any())).thenReturn(true);
380         // for createListInputsInGraph:
381         when(toscaOperationFacadeMock.addInputsToComponent(anyMap(), eq(COMPONENT_ID))).thenReturn(Either.left(Arrays.asList(listInput)));
382         // for rollback/commit:
383         when(titanDao.commit()).thenReturn(TitanOperationStatus.OK);
384         // for unlock resource
385         when(graphLockOperation.unlockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
386
387         Either<List<InputDefinition>, ResponseFormat> result =
388                 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
389         // validate result
390         assertEquals(true, result.isLeft());
391         List<InputDefinition> resultInputList = result.left().value();
392         assertEquals(1, resultInputList.size());
393         //InputDefinition resultInput = resultInputList.get(0);
394         Map<String, DataTypeDefinition> captoredDataTypeMap = dataTypesMapCaptor.getValue();
395         assertEquals(1, captoredDataTypeMap.size());
396         assertEquals(true, captoredDataTypeMap.containsKey(LISTINPUT_SCHEMA_TYPE));
397         DataTypeDefinition captoredDataType = captoredDataTypeMap.get(LISTINPUT_SCHEMA_TYPE);
398         assertEquals("tosca.datatypes.Root", captoredDataType.getDerivedFromName());
399         assertEquals( propInputsList.size(), captoredDataType.getProperties().size());
400         // confirm if corresponding property exists in data type
401         captoredDataType.getProperties().forEach(dataTypeProp -> {
402             Optional<ComponentInstancePropInput> find = propInputsList.stream()
403                     .filter(propInput -> propInput.getName().equals(dataTypeProp.getName())).findAny();
404             assertEquals(true, find.isPresent());
405         });
406     }
407
408     @Test
409     public void test_createListInput_fail_getComponent() throws Exception {
410         ComponentInstListInput createListInputParams = setUpCreateListInputParams();
411         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
412         Either<List<InputDefinition>, ResponseFormat> result =
413                 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
414         assertEquals(true, result.isRight());
415     }
416
417
418     @Test
419     public void test_createListInput_fail_lockComponent() throws Exception {
420         ComponentInstListInput createListInputParams = setUpCreateListInputParams();
421         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
422         when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.FAILED_TO_LOCK_ELEMENT);
423         Either<List<InputDefinition>, ResponseFormat> result =
424                 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
425         assertEquals(true, result.isRight());
426     }
427
428     @Test
429     public void test_createListInput_fail_getAllDataTypes() throws Exception {
430         ComponentInstListInput createListInputParams = setUpCreateListInputParams();
431         ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();
432
433         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
434         when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
435         when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
436         when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
437         when(applicationDataTypeCache.getAll()).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
438         when(componentsUtilsMock.getResponseFormat(ActionStatus.DATA_TYPE_CANNOT_BE_EMPTY)).thenReturn(new ResponseFormat());
439
440         Either<List<InputDefinition>, ResponseFormat> result =
441                 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
442         assertEquals(true, result.isRight());
443         verify(applicationDataTypeCache, times(1)).getAll();
444         verify(componentsUtilsMock, times(1)).getResponseFormat(ActionStatus.DATA_TYPE_CANNOT_BE_EMPTY);
445     }
446
447     @Test
448     public void test_createListInput_fail_prepareAndValidateInput() throws Exception {
449         ComponentInstListInput createListInputParams = setUpCreateListInputParams();
450         ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();
451         InputDefinition listInput = createListInputParams.getListInput();
452
453         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
454         when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
455         when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
456         when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
457         when(applicationDataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // don't use Collections.emptyMap
458         // for BaseOperation.validatePropertyDefaultValue:
459         when(propertyOperation.isPropertyTypeValid(any())).thenReturn(false);
460
461         Either<List<InputDefinition>, ResponseFormat> result =
462                 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
463         assertEquals(true, result.isRight());
464         verify(propertyOperation, times(1)).isPropertyTypeValid(any());
465     }
466
467     @Test
468     public void test_createListInput_fail_addInputsToComponent() throws Exception {
469         ComponentInstListInput createListInputParams = setUpCreateListInputParams();
470         ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();
471         InputDefinition listInput = createListInputParams.getListInput();
472
473         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
474         when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
475         when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
476         when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
477         when(applicationDataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // don't use Collections.emptyMap
478         // for BaseOperation.validatePropertyDefaultValue:
479         when(propertyOperation.isPropertyTypeValid(any())).thenReturn(true);
480         when(propertyOperation.isPropertyInnerTypeValid(any(),any())).thenReturn(new ImmutablePair<>(listInput.getSchemaType(), true));
481         when(propertyOperation.isPropertyDefaultValueValid(any(), any())).thenReturn(true);
482         when(toscaOperationFacadeMock.addInputsToComponent(anyMap(), eq(COMPONENT_ID))).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
483
484         Either<List<InputDefinition>, ResponseFormat> result =
485                 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
486         assertEquals(true, result.isRight());
487         verify(toscaOperationFacadeMock, times(1)).addInputsToComponent(anyMap(), eq(COMPONENT_ID));
488     }
489
490     @Test
491     public void test_deleteInput_listInput_fail_getComponent() throws Exception {
492         //ComponentInstListInput createListInputParams = setUpCreateListInputParams();
493         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
494                 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
495         when(componentsUtilsMock.getResponseFormat(any())).thenReturn(new ResponseFormat());
496
497         Either<InputDefinition, ResponseFormat> result = testInstance.deleteInput(COMPONENT_ID, USER_ID, LISTINPUT_NAME);
498         assertEquals(true, result.isRight());
499         verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
500     }
501
502
503     @Test
504     public void test_deleteInput_listInput_fail_validateInput() throws Exception {
505         InputDefinition listInput = setUpListInput();
506         String inputId = COMPONENT_ID + "." + listInput.getName();
507         listInput.setUniqueId(inputId);
508         service.setInputs(Collections.singletonList(listInput));
509         //ComponentInstListInput createListInputParams = setUpCreateListInputParams();
510         final String NONEXIST_INPUT_NAME = "myInput";
511
512         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
513                 .thenReturn(Either.left(service));
514         when(componentsUtilsMock.getResponseFormat(any())).thenReturn(new ResponseFormat());
515
516         Either<InputDefinition, ResponseFormat> result = testInstance.deleteInput(COMPONENT_ID, USER_ID, NONEXIST_INPUT_NAME);
517         assertEquals(true, result.isRight());
518         verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
519     }
520
521
522     @Test
523     public void test_deleteInput_listInput_fail_lockComponent() throws Exception {
524         InputDefinition listInput = setUpListInput();
525         String inputId = COMPONENT_ID + "." + listInput.getName();
526         listInput.setUniqueId(inputId);
527         service.setInputs(Collections.singletonList(listInput));
528
529         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
530                 .thenReturn(Either.left(service));
531         //when(componentsUtilsMock.getResponseFormat(any())).thenReturn(new ResponseFormat());
532         when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.NOT_FOUND);
533
534         Either<InputDefinition, ResponseFormat> result = testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
535         assertEquals(true, result.isRight());
536         verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
537         verify(graphLockOperation, times(1)).lockComponent(COMPONENT_ID, NodeTypeEnum.Service);
538     }
539
540
541     @Test
542     public void test_deleteInput_listInput_fail_deleteInput() throws Exception {
543         InputDefinition listInput = setUpListInput();
544         String inputId = COMPONENT_ID + "." + listInput.getName();
545         listInput.setUniqueId(inputId);
546         service.setInputs(Collections.singletonList(listInput));
547
548         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
549                 .thenReturn(Either.left(service));
550         when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
551         when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.BAD_REQUEST);
552         when(componentsUtilsMock.getResponseFormat(any())).thenReturn(new ResponseFormat());
553
554         Either<InputDefinition, ResponseFormat> result = testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
555         assertEquals(true, result.isRight());
556         verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
557         verify(graphLockOperation, times(1)).lockComponent(COMPONENT_ID, NodeTypeEnum.Service);
558         verify(toscaOperationFacadeMock, times(1)).deleteInputOfResource(service, listInput.getName());
559     }
560
561
562     @Test
563     public void test_deleteInput_listInput_success() throws Exception {
564         InputDefinition listInput = setUpListInput();
565         String inputId = COMPONENT_ID + "." + listInput.getName();
566         listInput.setUniqueId(inputId);
567         listInput.setIsDeclaredListInput(true);
568         service.setInputs(Collections.singletonList(listInput));
569         ArgumentCaptor<String> schemaTypeCaptor = ArgumentCaptor.forClass(String.class);
570
571         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
572                 .thenReturn(Either.left(service));
573         when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
574         when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.OK);
575         //when(componentsUtilsMock.getResponseFormat(any())).thenReturn(new ResponseFormat());
576         when(propertyDeclarationOrchestrator.unDeclarePropertiesAsListInputs(service, listInput)).thenReturn(StorageOperationStatus.OK);
577         when(dataTypeBusinessLogic.deletePrivateDataType(eq(service), schemaTypeCaptor.capture()))
578                 .thenReturn(Either.left(new DataTypeDefinition())); // TODO: replace to return proper datatype
579         //when(propertyDeclarationOrchestrator.unDeclarePropertiesAsInputs(service, listInput)).thenReturn(StorageOperationStatus.OK);
580
581         Either<InputDefinition, ResponseFormat> result = testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
582         assertEquals(true, result.isLeft());
583         verify(propertyDeclarationOrchestrator, times(1)).unDeclarePropertiesAsListInputs(service, listInput);
584         verify(dataTypeBusinessLogic, times(1)).deletePrivateDataType(service, listInput.getSchemaType());
585         assertEquals(listInput.getSchemaType(), schemaTypeCaptor.getValue());
586     }
587
588
589     @Test
590     public void test_deleteInput_input_fail_unDeclare() throws Exception {
591         InputDefinition listInput = setUpListInput();
592         String inputId = COMPONENT_ID + "." + listInput.getName();
593         listInput.setUniqueId(inputId);
594         listInput.setIsDeclaredListInput(false);
595         service.setInputs(Collections.singletonList(listInput));
596
597         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
598                 .thenReturn(Either.left(service));
599         when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
600         when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.OK);
601         //when(componentsUtilsMock.getResponseFormat(any())).thenReturn(new ResponseFormat());
602         when(propertyDeclarationOrchestrator.unDeclarePropertiesAsInputs(service, listInput)).thenReturn(StorageOperationStatus.BAD_REQUEST);
603
604         Either<InputDefinition, ResponseFormat> result = testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
605         assertEquals(true, result.isRight());
606         verify(propertyDeclarationOrchestrator, times(1)).unDeclarePropertiesAsInputs(service, listInput);
607     }
608
609
610     @Test
611     public void test_deleteInput_input_success() throws Exception {
612         InputDefinition listInput = setUpListInput();
613         String inputId = COMPONENT_ID + "." + listInput.getName();
614         listInput.setUniqueId(inputId);
615         listInput.setIsDeclaredListInput(false);
616         service.setInputs(Collections.singletonList(listInput));
617
618         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
619                 .thenReturn(Either.left(service));
620         when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
621         when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.OK);
622         //when(componentsUtilsMock.getResponseFormat(any())).thenReturn(new ResponseFormat());
623         when(propertyDeclarationOrchestrator.unDeclarePropertiesAsInputs(service, listInput)).thenReturn(StorageOperationStatus.OK);
624
625         Either<InputDefinition, ResponseFormat> result = testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
626         assertEquals(true, result.isLeft());
627         verify(propertyDeclarationOrchestrator, times(1)).unDeclarePropertiesAsInputs(service, listInput);
628     }
629
630 }