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