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