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