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