2 * Copyright © 2016-2019 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.openecomp.sdc.be.components.impl;
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;
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;
74 import java.util.Optional;
75 import java.util.stream.Collectors;
77 import static org.junit.Assert.assertEquals;
78 import static org.junit.Assert.assertTrue;
79 import static org.junit.Assert.fail;
80 import static org.mockito.ArgumentMatchers.any;
81 import static org.mockito.ArgumentMatchers.anyMap;
82 import static org.mockito.ArgumentMatchers.eq;
83 import static org.mockito.Mockito.times;
84 import static org.mockito.Mockito.verify;
85 import static org.mockito.Mockito.when;
88 public class InputsBusinessLogicTest {
90 private static final String COMPONENT_INSTANCE_ID = "instanceId";
91 private static final String COMPONENT_ID = "componentId";
92 private static final String USER_ID = "userId";
93 public static final String INSTANCE_INPUT_ID = "inputId";
94 private static final String LISTINPUT_NAME = "listInput";
95 private static final String LISTINPUT_SCHEMA_TYPE = "org.onap.datatypes.listinput";
96 private static final String LISTINPUT_PROP1_NAME = "prop1";
97 private static final String LISTINPUT_PROP1_TYPE = "string";
98 private static final String LISTINPUT_PROP2_NAME = "prop2";
99 private static final String LISTINPUT_PROP2_TYPE = "integer";
100 static ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be");
101 static ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
104 private ComponentsUtils componentsUtilsMock;
107 private UserBusinessLogic userAdminMock;
110 private ToscaOperationFacade toscaOperationFacadeMock;
113 private UserValidations userValidations;
116 private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
119 private IGraphLockOperation graphLockOperation;
122 private PropertyDeclarationOrchestrator propertyDeclarationOrchestrator;
125 private ApplicationDataTypeCache applicationDataTypeCache;
128 private PropertyOperation propertyOperation;
131 private JanusGraphDao janusGraphDao;
134 private DataTypeBusinessLogic dataTypeBusinessLogic;
137 private InputsBusinessLogic testInstance;
139 private Service service;
142 ArgumentCaptor<Map<String, DataTypeDefinition>> dataTypesMapCaptor;
144 private Map<String, List<ComponentInstanceInput>> instanceInputMap;
145 private List<ComponentInstanceInput> inputsList;
148 public void setUp() {
149 MockitoAnnotations.initMocks(this);
150 service = new Service();
151 service.setUniqueId(COMPONENT_ID);
152 service.setLastUpdaterUserId(USER_ID);
153 service.setIsDeleted(false);
154 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
156 testInstance.setUserValidations(userValidations);
157 testInstance.setToscaOperationFacade(toscaOperationFacadeMock);
158 testInstance.setGraphLockOperation(graphLockOperation);
159 testInstance.setComponentsUtils(componentsUtilsMock);
160 testInstance.setJanusGraphDao(janusGraphDao);
161 testInstance.setApplicationDataTypeCache(applicationDataTypeCache);
162 testInstance.setPropertyOperation(propertyOperation);
164 // add a ComponentInstance
165 ComponentInstance componentInstance = new ComponentInstance();
166 componentInstance.setUniqueId(COMPONENT_INSTANCE_ID);
167 service.setComponentInstances(Collections.singletonList(componentInstance));
169 instanceInputMap = new HashMap<>();
170 ComponentInstanceInput componentInstanceInput = new ComponentInstanceInput();
171 componentInstanceInput.setInputId(INSTANCE_INPUT_ID);
172 componentInstanceInput.setName(INSTANCE_INPUT_ID);
173 inputsList = Collections.singletonList(componentInstanceInput);
174 instanceInputMap.put(COMPONENT_INSTANCE_ID, inputsList);
175 instanceInputMap.put("someInputId", Collections.singletonList(new ComponentInstanceInput()));
176 service.setComponentInstancesInputs(instanceInputMap);
177 when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(new User());
178 when(userAdminMock.getUser(USER_ID, false)).thenReturn(new User());
182 public void getComponentInstanceInputs_ComponentInstanceNotExist() {
183 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
184 Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, "nonExisting");
185 assertTrue(componentInstanceInputs.isRight());
186 verify(componentsUtilsMock).getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
190 public void getComponentInstanceInputs_emptyInputsMap() {
191 service.setComponentInstancesInputs(Collections.emptyMap());
192 getComponents_emptyInputs(service);
196 public void getComponentInstanceInputs_nullInputsMap() {
197 service.setComponentInstancesInputs(null);
198 getComponents_emptyInputs(service);
202 public void getComponentInstanceInputs_instanceHasNoInputs() {
203 service.setComponentInstancesInputs(Collections.singletonMap("someInputId", new ArrayList<>()));
204 getComponents_emptyInputs(service);
208 public void getComponentInstanceInputs() {
209 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
210 Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, COMPONENT_INSTANCE_ID);
211 assertEquals("inputId", componentInstanceInputs.left().value().get(0).getInputId());
215 public void testGetInputs() {
216 String userId = "userId";
217 String componentId = "compId";
218 Component component = new Resource();
219 when(toscaOperationFacadeMock.getToscaElement(any(String.class), any(ComponentParametersView.class))).thenReturn(Either.left(component));
220 testInstance.getInputs(userId, componentId);
221 assertEquals(null, component.getInputs());
225 public void testGetCIPropertiesByInputId() {
226 Either<List<ComponentInstanceProperty>, ResponseFormat> result;
227 String userId = "userId";
228 String componentId = "compId";
229 Component component = new Resource();
230 List<InputDefinition> listDef = new ArrayList<>();
231 InputDefinition inputDef = new InputDefinition();
232 inputDef.setUniqueId(componentId);
233 listDef.add(inputDef);
234 component.setInputs(listDef);
235 when(toscaOperationFacadeMock.getToscaElement(any(String.class), any(ComponentParametersView.class))).thenReturn(Either.left(component));
236 result = testInstance.getComponentInstancePropertiesByInputId(userId, componentId, componentId, componentId);
237 assertTrue(result.isLeft());
241 public void testDeclareProperties() {
242 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
243 service.setLastUpdaterUserId(USER_ID);
244 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
245 Map<String, List<ComponentInstancePropInput>> propertiesForDeclaration = new HashMap<>();
246 propertiesForDeclaration.put(COMPONENT_ID, getPropertiesListForDeclaration());
247 componentInstInputsMap.setServiceProperties(propertiesForDeclaration);
249 List<InputDefinition> declaredPropertiesToInputs = getDeclaredProperties();
250 initMockitoStubbings(declaredPropertiesToInputs);
252 Either<List<InputDefinition>, ResponseFormat> declaredPropertiesEither =
253 testInstance.declareProperties(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, componentInstInputsMap);
255 assertTrue(declaredPropertiesEither.isLeft());
257 List<InputDefinition> declaredProperties = declaredPropertiesEither.left().value();
258 assertTrue(CollectionUtils.isNotEmpty(declaredProperties));
259 assertEquals(1, declaredProperties.size());
260 assertEquals(declaredProperties, declaredPropertiesToInputs);
263 private void initMockitoStubbings(List<InputDefinition> declaredPropertiesToInputs) {
264 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(
265 Either.left(service));
266 when(propertyDeclarationOrchestrator.declarePropertiesToInputs(any(), any())).thenReturn(Either.left(
267 declaredPropertiesToInputs));
268 when(toscaOperationFacadeMock.addInputsToComponent(any(), any())).thenReturn(Either.left(declaredPropertiesToInputs));
269 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
270 when(graphLockOperation.lockComponent(any(), any())).thenReturn(StorageOperationStatus.OK);
271 when(graphLockOperation.unlockComponent(any(), any())).thenReturn(StorageOperationStatus.OK);
272 when(componentInstanceBusinessLogic.setInputConstraint(any())).thenReturn(Collections.emptyList());
275 private void getComponents_emptyInputs(Service service) {
276 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
277 Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, COMPONENT_INSTANCE_ID);
278 assertEquals(Collections.emptyList(), componentInstanceInputs.left().value());
282 public void testgetInputs_ARTIFACT_NOT_FOUND() throws Exception {
284 when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.ARTIFACT_NOT_FOUND)).thenReturn(ActionStatus.ARTIFACT_NOT_FOUND);
285 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
286 Either<List<InputDefinition>, ResponseFormat> responseFormatEither = testInstance.getInputs("USR01", COMPONENT_ID);
287 assertEquals(true,responseFormatEither.isRight());
292 public void testgetInputs_SUCCESS() throws Exception {
293 Component component = new Service();
294 InputDefinition input= new InputDefinition();
295 List inputlist = new ArrayList<>();
296 inputlist.add(input);
297 component.setInputs(inputlist);
298 when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.ARTIFACT_NOT_FOUND)).thenReturn(ActionStatus.ARTIFACT_NOT_FOUND);
299 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(component));
300 Either<List<InputDefinition>, ResponseFormat> responseFormatEither = testInstance.getInputs("USR01", COMPONENT_ID);
301 assertEquals(inputlist,responseFormatEither.left().value());
305 public void testgetComponentInstancePropertiesByInputId_Artifactnotfound() throws Exception
307 Component component = new Service();
308 InputDefinition input= new InputDefinition();
309 List inputlist = new ArrayList<>();
310 inputlist.add(input);
311 component.setInputs(inputlist);
312 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
313 Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = testInstance.getComponentInstancePropertiesByInputId("USR01", COMPONENT_ID,"INST0.1", "INPO1");
314 assertEquals(true,responseFormatEither.isRight());
318 public void testgetComponentInstancePropertiesByInputId_PARENT_ARTIFACT_NOT_FOUND() throws Exception
320 Component component = new Service();
321 InputDefinition input= new InputDefinition();
322 List inputlist = new ArrayList<>();
323 inputlist.add(input);
324 component.setInputs(inputlist);
325 ComponentInstance componentInstance = new ComponentInstance();
326 componentInstance.setUniqueId("INST0.1");
327 componentInstance.setComponentUid("RES0.1");
328 List compinstancelist = new ArrayList<>();
329 compinstancelist.add(componentInstance);
330 component.setComponentInstances(compinstancelist);
331 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(component));
332 when(toscaOperationFacadeMock.getToscaElement(eq("RES0.1"), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
333 Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = testInstance.getComponentInstancePropertiesByInputId("USR01", COMPONENT_ID,"INST0.1", "INPO1");
334 assertEquals(true,responseFormatEither.isRight());
338 public void testgetComponentInstancePropertiesByInputId() throws Exception
340 Component component = new Service();
341 InputDefinition input= new InputDefinition();
342 input.setUniqueId("INPO1");
343 List inputlist = new ArrayList<>();
344 inputlist.add(input);
345 component.setInputs(inputlist);
346 ComponentInstance componentInstance = new ComponentInstance();
347 componentInstance.setUniqueId("INST0.1");
348 componentInstance.setComponentUid("RES0.1");
349 List compinstancelist = new ArrayList<>();
350 compinstancelist.add(componentInstance);
351 component.setComponentInstances(compinstancelist);
352 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(component));
353 when(componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(any(Component.class),eq("INPO1"))).thenReturn(compinstancelist);
354 //when(toscaOperationFacadeMock.getToscaElement(eq("RES0.1"), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
355 when(toscaOperationFacadeMock.getToscaElement(eq("RES0.1"), any(ComponentParametersView.class))).thenReturn(Either.left(component));
356 Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = testInstance.getComponentInstancePropertiesByInputId("USR01", COMPONENT_ID,"INST0.1", "INPO1");
357 assertEquals(compinstancelist,responseFormatEither.left().value());
361 public void testgetInputsForComponentInput_ARTIFACT_NOT_FOUND() throws Exception
363 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
364 Either<List<ComponentInstanceInput>, ResponseFormat> result = testInstance.getInputsForComponentInput("USR01", COMPONENT_ID,"INPO1");
365 assertEquals(true,result.isRight());
369 public void testgetInputsForComponentInput() throws Exception
371 Component component = new Service();
372 InputDefinition input= new InputDefinition();
373 input.setUniqueId("INPO1");
374 List inputlist = new ArrayList<>();
375 inputlist.add(input);
376 component.setInputs(inputlist);
377 ComponentInstance componentInstance = new ComponentInstance();
378 componentInstance.setUniqueId("INST0.1");
379 componentInstance.setComponentUid("RES0.1");
380 List compinstancelist = new ArrayList<>();
381 compinstancelist.add(componentInstance);
382 component.setComponentInstances(compinstancelist);
383 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(component));
384 when(componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(any(Component.class),eq("INPO1"))).thenReturn(compinstancelist);
385 Either<List<ComponentInstanceInput>, ResponseFormat> result = testInstance.getInputsForComponentInput("USR01", COMPONENT_ID,"INPO1");
386 assertEquals(true,result.isLeft());
389 private List<ComponentInstancePropInput> getPropertiesListForDeclaration() {
390 return inputsList.stream().map(this::getPropertyForDeclaration).collect(Collectors.toList());
393 private ComponentInstancePropInput getPropertyForDeclaration(ComponentInstanceInput componentInstanceInput) {
394 ComponentInstancePropInput propInput = new ComponentInstancePropInput();
395 propInput.setInput(componentInstanceInput);
396 propInput.setPropertiesName(componentInstanceInput.getName());
401 private List<InputDefinition> getDeclaredProperties() {
402 return inputsList.stream().map(InputDefinition::new).collect(Collectors.toList());
406 private InputDefinition setUpListInput() {
407 InputDefinition listInput = new InputDefinition();
408 listInput.setName(LISTINPUT_NAME);
409 listInput.setType("list");
410 SchemaDefinition listInputSchema = new SchemaDefinition();
411 listInputSchema.setProperty(new PropertyDataDefinitionBuilder()
412 .setType(LISTINPUT_SCHEMA_TYPE)
413 .setIsRequired(false)
416 listInput.setSchema(listInputSchema);
420 private ComponentInstListInput setUpCreateListInputParams() {
421 ComponentInstListInput componentInstListInput = new ComponentInstListInput();
423 // Create a "list input"
424 InputDefinition listInput = setUpListInput();
425 componentInstListInput.setListInput(listInput);
427 // Create ComponentInstancePropInputs
428 // for inputs in the ComponentInstance
429 Map<String, List<ComponentInstancePropInput>> propInputsListMap = new HashMap<>();
430 // Add 2 PropInputs. property owner is COMPONENT_INSTANCE_ID
431 List<ComponentInstancePropInput> propInputsList = new ArrayList<>();
432 ComponentInstancePropInput propInput = new ComponentInstancePropInput();
433 propInput.setName(LISTINPUT_PROP1_NAME);
434 propInput.setType(LISTINPUT_PROP1_TYPE);
435 propInput.setUniqueId(COMPONENT_INSTANCE_ID+"."+LISTINPUT_PROP1_NAME);
436 propInputsList.add(propInput);
437 propInput = new ComponentInstancePropInput();
438 propInput.setName(LISTINPUT_PROP2_NAME);
439 propInput.setType(LISTINPUT_PROP2_TYPE);
440 propInput.setUniqueId(COMPONENT_INSTANCE_ID+"."+LISTINPUT_PROP2_NAME);
441 propInputsList.add(propInput);
442 propInputsListMap.put(COMPONENT_INSTANCE_ID, propInputsList);
443 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
444 componentInstInputsMap.setComponentInstanceInputsMap(propInputsListMap);
445 componentInstListInput.setComponentInstInputsMap(componentInstInputsMap);
447 return componentInstListInput;
451 public void test_createListInput_success() throws Exception {
452 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
453 ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();
454 List<ComponentInstancePropInput> propInputsList = componentInstInputsMap.getComponentInstanceInputsMap().get(COMPONENT_INSTANCE_ID);
455 InputDefinition listInput = createListInputParams.getListInput();
457 // set up mock returns
458 // for get component object:
459 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
460 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
461 // for data type creation (use captor):
462 when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
463 when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
464 when(propertyDeclarationOrchestrator.declarePropertiesToListInput(service, componentInstInputsMap, listInput)).thenReturn(Either.left(listInput));
465 // for BaseOperation.getAllDataTypes:
466 when(applicationDataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // don't use Collections.emptyMap
467 // for BaseOperation.validatePropertyDefaultValue:
468 when(propertyOperation.isPropertyTypeValid(any())).thenReturn(true);
469 when(propertyOperation.isPropertyInnerTypeValid(any(),any())).thenReturn(new ImmutablePair<>(listInput.getSchemaType(), true));
470 when(propertyOperation.isPropertyDefaultValueValid(any(), any())).thenReturn(true);
471 // for createListInputsInGraph:
472 when(toscaOperationFacadeMock.addInputsToComponent(anyMap(), eq(COMPONENT_ID))).thenReturn(Either.left(Arrays.asList(listInput)));
473 // for rollback/commit:
474 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
475 // for unlock resource
476 when(graphLockOperation.unlockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
478 Either<List<InputDefinition>, ResponseFormat> result =
479 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
481 assertEquals(true, result.isLeft());
482 List<InputDefinition> resultInputList = result.left().value();
483 assertEquals(1, resultInputList.size());
484 //InputDefinition resultInput = resultInputList.get(0);
485 Map<String, DataTypeDefinition> captoredDataTypeMap = dataTypesMapCaptor.getValue();
486 assertEquals(1, captoredDataTypeMap.size());
487 assertEquals(true, captoredDataTypeMap.containsKey(LISTINPUT_SCHEMA_TYPE));
488 DataTypeDefinition captoredDataType = captoredDataTypeMap.get(LISTINPUT_SCHEMA_TYPE);
489 assertEquals("tosca.datatypes.Root", captoredDataType.getDerivedFromName());
490 assertEquals( propInputsList.size(), captoredDataType.getProperties().size());
491 // confirm if corresponding property exists in data type
492 captoredDataType.getProperties().forEach(dataTypeProp -> {
493 Optional<ComponentInstancePropInput> find = propInputsList.stream()
494 .filter(propInput -> propInput.getName().equals(dataTypeProp.getName())).findAny();
495 assertEquals(true, find.isPresent());
500 public void test_createListInput_fail_getComponent() throws Exception {
501 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
502 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
503 when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND, ComponentTypeEnum.SERVICE)).thenReturn(ActionStatus.SERVICE_NOT_FOUND);
505 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
506 } catch (ByActionStatusComponentException e) {
507 assertEquals(ActionStatus.SERVICE_NOT_FOUND, e.getActionStatus());
515 public void test_createListInput_fail_lockComponent() throws Exception {
516 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
517 when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.FAILED_TO_LOCK_ELEMENT, ComponentTypeEnum.SERVICE)).thenReturn(ActionStatus.COMPONENT_IN_USE);
518 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
519 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.FAILED_TO_LOCK_ELEMENT);
521 Either<List<InputDefinition>, ResponseFormat> result =
522 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
523 } catch (ByActionStatusComponentException e) {
524 assertEquals(ActionStatus.COMPONENT_IN_USE, e.getActionStatus());
531 public void test_createListInput_fail_getAllDataTypes() throws Exception {
532 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
533 ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();
535 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
536 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
537 when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
538 when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
539 when(applicationDataTypeCache.getAll()).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
542 Either<List<InputDefinition>, ResponseFormat> result =
543 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
544 } catch (ByActionStatusComponentException e) {
545 assertEquals(ActionStatus.DATA_TYPE_CANNOT_BE_EMPTY, e.getActionStatus());
546 verify(applicationDataTypeCache, times(1)).getAll();
553 public void test_createListInput_fail_prepareAndValidateInput() throws Exception {
554 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
555 ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();
556 InputDefinition listInput = createListInputParams.getListInput();
558 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
559 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
560 when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
561 when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
562 when(applicationDataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // don't use Collections.emptyMap
563 // for BaseOperation.validatePropertyDefaultValue:
564 when(propertyOperation.isPropertyTypeValid(any())).thenReturn(false);
566 Either<List<InputDefinition>, ResponseFormat> result =
567 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
568 assertEquals(true, result.isRight());
569 verify(propertyOperation, times(1)).isPropertyTypeValid(any());
573 public void test_createListInput_fail_addInputsToComponent() throws Exception {
574 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
575 ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();
576 InputDefinition listInput = createListInputParams.getListInput();
578 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
579 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
580 when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
581 when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
582 when(applicationDataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // don't use Collections.emptyMap
583 // for BaseOperation.validatePropertyDefaultValue:
584 when(propertyOperation.isPropertyTypeValid(any())).thenReturn(true);
585 when(propertyOperation.isPropertyInnerTypeValid(any(),any())).thenReturn(new ImmutablePair<>(listInput.getSchemaType(), true));
586 when(propertyOperation.isPropertyDefaultValueValid(any(), any())).thenReturn(true);
587 when(toscaOperationFacadeMock.addInputsToComponent(anyMap(), eq(COMPONENT_ID))).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
589 Either<List<InputDefinition>, ResponseFormat> result =
590 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
591 assertEquals(true, result.isRight());
592 verify(toscaOperationFacadeMock, times(1)).addInputsToComponent(anyMap(), eq(COMPONENT_ID));
596 public void test_deleteInput_listInput_fail_getComponent() throws Exception {
597 //ComponentInstListInput createListInputParams = setUpCreateListInputParams();
598 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
599 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
600 when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND)).thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
603 testInstance.deleteInput(COMPONENT_ID, USER_ID, LISTINPUT_NAME);
604 } catch (ComponentException e) {
605 assertEquals(ActionStatus.RESOURCE_NOT_FOUND, e.getActionStatus());
606 verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
614 public void test_deleteInput_listInput_fail_validateInput() throws Exception {
615 InputDefinition listInput = setUpListInput();
616 String inputId = COMPONENT_ID + "." + listInput.getName();
617 listInput.setUniqueId(inputId);
618 service.setInputs(Collections.singletonList(listInput));
619 //ComponentInstListInput createListInputParams = setUpCreateListInputParams();
620 final String NONEXIST_INPUT_NAME = "myInput";
622 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
623 .thenReturn(Either.left(service));
626 testInstance.deleteInput(COMPONENT_ID, USER_ID, NONEXIST_INPUT_NAME);
627 } catch (ComponentException e) {
628 assertEquals(ActionStatus.INPUT_IS_NOT_CHILD_OF_COMPONENT, e.getActionStatus());
629 verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
637 public void test_deleteInput_listInput_fail_lockComponent() throws Exception {
638 InputDefinition listInput = setUpListInput();
639 String inputId = COMPONENT_ID + "." + listInput.getName();
640 listInput.setUniqueId(inputId);
641 service.setInputs(Collections.singletonList(listInput));
643 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
644 .thenReturn(Either.left(service));
645 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.NOT_FOUND);
646 when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND, ComponentTypeEnum.SERVICE)).thenReturn(ActionStatus.SERVICE_NOT_FOUND);
649 testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
650 } catch (ComponentException e) {
651 assertEquals(ActionStatus.SERVICE_NOT_FOUND, e.getActionStatus());
652 verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
653 verify(graphLockOperation, times(1)).lockComponent(COMPONENT_ID, NodeTypeEnum.Service);
661 public void test_deleteInput_listInput_fail_deleteInput() throws Exception {
662 InputDefinition listInput = setUpListInput();
663 String inputId = COMPONENT_ID + "." + listInput.getName();
664 listInput.setUniqueId(inputId);
665 service.setInputs(Collections.singletonList(listInput));
667 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
668 .thenReturn(Either.left(service));
669 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
670 when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.BAD_REQUEST);
671 when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.BAD_REQUEST)).thenReturn(ActionStatus.INVALID_CONTENT);
674 testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
675 } catch (ComponentException e) {
676 assertEquals(ActionStatus.INVALID_CONTENT, e.getActionStatus());
677 verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
678 verify(graphLockOperation, times(1)).lockComponent(COMPONENT_ID, NodeTypeEnum.Service);
679 verify(toscaOperationFacadeMock, times(1)).deleteInputOfResource(service, listInput.getName());
687 public void test_deleteInput_listInput_success() throws Exception {
688 InputDefinition listInput = setUpListInput();
689 String inputId = COMPONENT_ID + "." + listInput.getName();
690 listInput.setUniqueId(inputId);
691 listInput.setIsDeclaredListInput(true);
692 service.setInputs(Collections.singletonList(listInput));
693 ArgumentCaptor<String> schemaTypeCaptor = ArgumentCaptor.forClass(String.class);
695 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
696 .thenReturn(Either.left(service));
697 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
698 when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.OK);
699 when(propertyDeclarationOrchestrator.unDeclarePropertiesAsListInputs(service, listInput)).thenReturn(StorageOperationStatus.OK);
700 when(dataTypeBusinessLogic.deletePrivateDataType(eq(service), schemaTypeCaptor.capture()))
701 .thenReturn(Either.left(new DataTypeDefinition()));
703 testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
704 verify(propertyDeclarationOrchestrator, times(1)).unDeclarePropertiesAsListInputs(service, listInput);
705 verify(dataTypeBusinessLogic, times(1)).deletePrivateDataType(service, listInput.getSchemaType());
706 assertEquals(listInput.getSchemaType(), schemaTypeCaptor.getValue());
711 public void test_deleteInput_input_fail_unDeclare() throws Exception {
712 InputDefinition listInput = setUpListInput();
713 String inputId = COMPONENT_ID + "." + listInput.getName();
714 listInput.setUniqueId(inputId);
715 listInput.setIsDeclaredListInput(false);
716 service.setInputs(Collections.singletonList(listInput));
718 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
719 .thenReturn(Either.left(service));
720 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
721 when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.OK);
722 when(propertyDeclarationOrchestrator.unDeclarePropertiesAsInputs(service, listInput)).thenReturn(StorageOperationStatus.BAD_REQUEST);
723 when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.BAD_REQUEST)).thenReturn(ActionStatus.INVALID_CONTENT);
726 testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
727 } catch (ComponentException e) {
728 assertEquals(ActionStatus.INVALID_CONTENT, e.getActionStatus());
729 verify(propertyDeclarationOrchestrator, times(1)).unDeclarePropertiesAsInputs(service, listInput);
737 public void test_deleteInput_input_success() throws Exception {
738 InputDefinition listInput = setUpListInput();
739 String inputId = COMPONENT_ID + "." + listInput.getName();
740 listInput.setUniqueId(inputId);
741 listInput.setIsDeclaredListInput(false);
742 service.setInputs(Collections.singletonList(listInput));
744 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
745 .thenReturn(Either.left(service));
746 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
747 when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.OK);
748 when(propertyDeclarationOrchestrator.unDeclarePropertiesAsInputs(service, listInput)).thenReturn(StorageOperationStatus.OK);
750 testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
751 verify(propertyDeclarationOrchestrator, times(1)).unDeclarePropertiesAsInputs(service, listInput);