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 static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.ArgumentMatchers.anyMap;
24 import static org.mockito.ArgumentMatchers.anyString;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
30 import fj.data.Either;
31 import org.apache.commons.collections.CollectionUtils;
32 import org.apache.commons.lang3.tuple.ImmutablePair;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.ArgumentCaptor;
36 import org.mockito.Captor;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.openecomp.sdc.be.components.property.PropertyDeclarationOrchestrator;
41 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
42 import org.openecomp.sdc.be.components.validation.UserValidations;
43 import org.openecomp.sdc.be.dao.api.ActionStatus;
44 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
45 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
46 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
47 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
48 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
49 import org.openecomp.sdc.be.impl.ComponentsUtils;
50 import org.openecomp.sdc.be.model.Component;
51 import org.openecomp.sdc.be.model.ComponentInstInputsMap;
52 import org.openecomp.sdc.be.model.ComponentInstListInput;
53 import org.openecomp.sdc.be.model.ComponentInstance;
54 import org.openecomp.sdc.be.model.ComponentInstanceInput;
55 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
56 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
57 import org.openecomp.sdc.be.model.ComponentParametersView;
58 import org.openecomp.sdc.be.model.DataTypeDefinition;
59 import org.openecomp.sdc.be.model.InputDefinition;
60 import org.openecomp.sdc.be.model.LifecycleStateEnum;
61 import org.openecomp.sdc.be.model.Resource;
62 import org.openecomp.sdc.be.model.Service;
63 import org.openecomp.sdc.be.model.User;
64 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
65 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
66 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
67 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
68 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
69 import org.openecomp.sdc.be.user.IUserBusinessLogic;
70 import org.openecomp.sdc.exception.ResponseFormat;
72 import java.util.ArrayList;
73 import java.util.Arrays;
74 import java.util.Collections;
75 import java.util.HashMap;
76 import java.util.List;
78 import java.util.Optional;
79 import java.util.stream.Collectors;
81 public class InputsBusinessLogicTest {
83 private static final String COMPONENT_INSTANCE_ID = "instanceId";
84 private static final String COMPONENT_ID = "componentId";
85 private static final String USER_ID = "userId";
86 private static final String INSTANCE_INPUT_ID = "inputId";
87 private static final String LISTINPUT_NAME = "listInput";
88 private static final String LISTINPUT_SCHEMA_TYPE = "org.onap.datatypes.listinput";
89 private static final String LISTINPUT_PROP1_NAME = "prop1";
90 private static final String LISTINPUT_PROP1_TYPE = "string";
91 private static final String LISTINPUT_PROP2_NAME = "prop2";
92 private static final String LISTINPUT_PROP2_TYPE = "integer";
95 private ComponentsUtils componentsUtilsMock;
98 private IUserBusinessLogic userAdminMock;
101 private ToscaOperationFacade toscaOperationFacadeMock;
104 private UserValidations userValidations;
107 private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
110 private IGraphLockOperation graphLockOperation;
113 private PropertyDeclarationOrchestrator propertyDeclarationOrchestrator;
116 private ApplicationDataTypeCache applicationDataTypeCache;
119 private PropertyOperation propertyOperation;
122 private JanusGraphDao janusGraphDao;
125 private DataTypeBusinessLogic dataTypeBusinessLogic;
128 private InputsBusinessLogic testInstance;
130 private Service service;
133 ArgumentCaptor<Map<String, DataTypeDefinition>> dataTypesMapCaptor;
135 private Map<String, List<ComponentInstanceInput>> instanceInputMap;
136 private List<ComponentInstanceInput> inputsList;
139 public void setUp() throws Exception {
140 MockitoAnnotations.initMocks(this);
141 service = new Service();
142 service.setUniqueId(COMPONENT_ID);
143 service.setLastUpdaterUserId(USER_ID);
144 service.setIsDeleted(false);
145 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
147 // add a ComponentInstance
148 ComponentInstance componentInstance = new ComponentInstance();
149 componentInstance.setUniqueId(COMPONENT_INSTANCE_ID);
150 service.setComponentInstances(Collections.singletonList(componentInstance));
152 instanceInputMap = new HashMap<>();
153 ComponentInstanceInput componentInstanceInput = new ComponentInstanceInput();
154 componentInstanceInput.setInputId(INSTANCE_INPUT_ID);
155 componentInstanceInput.setName(INSTANCE_INPUT_ID);
156 inputsList = Collections.singletonList(componentInstanceInput);
157 instanceInputMap.put(COMPONENT_INSTANCE_ID, inputsList);
158 instanceInputMap.put("someInputId", Collections.singletonList(new ComponentInstanceInput()));
159 service.setComponentInstancesInputs(instanceInputMap);
161 when(userValidations.validateUserExists(eq(USER_ID), anyString(), eq(false))).thenReturn(new User());
162 when(userAdminMock.getUser(USER_ID, false)).thenReturn(Either.left(new User()));
166 public void getComponentInstanceInputs_ComponentInstanceNotExist() throws Exception {
167 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
168 Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, "nonExisting");
169 assertTrue(componentInstanceInputs.isRight());
170 verify(componentsUtilsMock).getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
174 public void getComponentInstanceInputs_emptyInputsMap() throws Exception {
175 service.setComponentInstancesInputs(Collections.emptyMap());
176 getComponents_emptyInputs(service);
180 public void getComponentInstanceInputs_nullInputsMap() throws Exception {
181 service.setComponentInstancesInputs(null);
182 getComponents_emptyInputs(service);
186 public void getComponentInstanceInputs_instanceHasNoInputs() throws Exception {
187 service.setComponentInstancesInputs(Collections.singletonMap("someInputId", new ArrayList<>()));
188 getComponents_emptyInputs(service);
192 public void getComponentInstanceInputs() throws Exception {
193 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
194 Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, COMPONENT_INSTANCE_ID);
195 assertEquals("inputId", componentInstanceInputs.left().value().get(0).getInputId());
199 public void testGetInputs() {
200 String userId = "userId";
201 String componentId = "compId";
202 Component component = new Resource();
203 when(toscaOperationFacadeMock.getToscaElement(any(String.class), any(ComponentParametersView.class))).thenReturn(Either.left(component));
204 testInstance.getInputs(userId, componentId);
205 assertEquals(null, component.getInputs());
209 public void testGetCIPropertiesByInputId() {
210 Either<List<ComponentInstanceProperty>, ResponseFormat> result;
211 String userId = "userId";
212 String componentId = "compId";
213 Component component = new Resource();
214 List<InputDefinition> listDef = new ArrayList<>();
215 InputDefinition inputDef = new InputDefinition();
216 inputDef.setUniqueId(componentId);
217 listDef.add(inputDef);
218 component.setInputs(listDef);
219 when(toscaOperationFacadeMock.getToscaElement(any(String.class), any(ComponentParametersView.class))).thenReturn(Either.left(component));
220 result = testInstance.getComponentInstancePropertiesByInputId(userId, componentId, componentId, componentId);
221 assertTrue(result.isLeft());
225 public void testDeclareProperties() {
226 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
227 service.setLastUpdaterUserId(USER_ID);
228 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
229 Map<String, List<ComponentInstancePropInput>> propertiesForDeclaration = new HashMap<>();
230 propertiesForDeclaration.put(COMPONENT_ID, getPropertiesListForDeclaration());
231 componentInstInputsMap.setServiceProperties(propertiesForDeclaration);
233 List<InputDefinition> declaredPropertiesToInputs = getDeclaredProperties();
234 initMockitoStubbings(declaredPropertiesToInputs);
236 Either<List<InputDefinition>, ResponseFormat> declaredPropertiesEither =
237 testInstance.declareProperties(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, componentInstInputsMap);
239 assertTrue(declaredPropertiesEither.isLeft());
241 List<InputDefinition> declaredProperties = declaredPropertiesEither.left().value();
242 assertTrue(CollectionUtils.isNotEmpty(declaredProperties));
243 assertEquals(1, declaredProperties.size());
244 assertEquals(declaredProperties, declaredPropertiesToInputs);
247 private void initMockitoStubbings(List<InputDefinition> declaredPropertiesToInputs) {
248 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(
249 Either.left(service));
250 when(propertyDeclarationOrchestrator.declarePropertiesToInputs(any(), any())).thenReturn(Either.left(
251 declaredPropertiesToInputs));
252 when(toscaOperationFacadeMock.addInputsToComponent(any(), any())).thenReturn(Either.left(declaredPropertiesToInputs));
253 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
254 when(graphLockOperation.lockComponent(any(), any())).thenReturn(StorageOperationStatus.OK);
255 when(graphLockOperation.unlockComponent(any(), any())).thenReturn(StorageOperationStatus.OK);
256 when(componentInstanceBusinessLogic.setInputConstraint(any())).thenReturn(Collections.emptyList());
259 private void getComponents_emptyInputs(Service service) {
260 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
261 Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, COMPONENT_INSTANCE_ID);
262 assertEquals(Collections.emptyList(), componentInstanceInputs.left().value());
266 public void testgetInputs_ARTIFACT_NOT_FOUND() throws Exception {
268 when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.ARTIFACT_NOT_FOUND)).thenReturn(ActionStatus.ARTIFACT_NOT_FOUND);
269 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
270 Either<List<InputDefinition>, ResponseFormat> responseFormatEither = testInstance.getInputs("USR01", COMPONENT_ID);
271 assertEquals(true,responseFormatEither.isRight());
276 public void testgetInputs_SUCCESS() throws Exception {
277 Component component = new Service();
278 InputDefinition input= new InputDefinition();
279 List inputlist = new ArrayList<>();
280 inputlist.add(input);
281 component.setInputs(inputlist);
282 when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.ARTIFACT_NOT_FOUND)).thenReturn(ActionStatus.ARTIFACT_NOT_FOUND);
283 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(component));
284 Either<List<InputDefinition>, ResponseFormat> responseFormatEither = testInstance.getInputs("USR01", COMPONENT_ID);
285 assertEquals(inputlist,responseFormatEither.left().value());
289 public void testgetComponentInstancePropertiesByInputId_Artifactnotfound() throws Exception
291 Component component = new Service();
292 InputDefinition input= new InputDefinition();
293 List inputlist = new ArrayList<>();
294 inputlist.add(input);
295 component.setInputs(inputlist);
296 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
297 Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = testInstance.getComponentInstancePropertiesByInputId("USR01", COMPONENT_ID,"INST0.1", "INPO1");
298 assertEquals(true,responseFormatEither.isRight());
302 public void testgetComponentInstancePropertiesByInputId_PARENT_ARTIFACT_NOT_FOUND() throws Exception
304 Component component = new Service();
305 InputDefinition input= new InputDefinition();
306 List inputlist = new ArrayList<>();
307 inputlist.add(input);
308 component.setInputs(inputlist);
309 ComponentInstance componentInstance = new ComponentInstance();
310 componentInstance.setUniqueId("INST0.1");
311 componentInstance.setComponentUid("RES0.1");
312 List compinstancelist = new ArrayList<>();
313 compinstancelist.add(componentInstance);
314 component.setComponentInstances(compinstancelist);
315 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(component));
316 when(toscaOperationFacadeMock.getToscaElement(eq("RES0.1"), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
317 Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = testInstance.getComponentInstancePropertiesByInputId("USR01", COMPONENT_ID,"INST0.1", "INPO1");
318 assertEquals(true,responseFormatEither.isRight());
322 public void testgetComponentInstancePropertiesByInputId() throws Exception
324 Component component = new Service();
325 InputDefinition input= new InputDefinition();
326 input.setUniqueId("INPO1");
327 List inputlist = new ArrayList<>();
328 inputlist.add(input);
329 component.setInputs(inputlist);
330 ComponentInstance componentInstance = new ComponentInstance();
331 componentInstance.setUniqueId("INST0.1");
332 componentInstance.setComponentUid("RES0.1");
333 List compinstancelist = new ArrayList<>();
334 compinstancelist.add(componentInstance);
335 component.setComponentInstances(compinstancelist);
336 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(component));
337 when(componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(any(Component.class),eq("INPO1"))).thenReturn(compinstancelist);
338 //when(toscaOperationFacadeMock.getToscaElement(eq("RES0.1"), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
339 when(toscaOperationFacadeMock.getToscaElement(eq("RES0.1"), any(ComponentParametersView.class))).thenReturn(Either.left(component));
340 Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = testInstance.getComponentInstancePropertiesByInputId("USR01", COMPONENT_ID,"INST0.1", "INPO1");
341 assertEquals(compinstancelist,responseFormatEither.left().value());
345 public void testgetInputsForComponentInput_ARTIFACT_NOT_FOUND() throws Exception
347 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
348 Either<List<ComponentInstanceInput>, ResponseFormat> result = testInstance.getInputsForComponentInput("USR01", COMPONENT_ID,"INPO1");
349 assertEquals(true,result.isRight());
353 public void testgetInputsForComponentInput() throws Exception
355 Component component = new Service();
356 InputDefinition input= new InputDefinition();
357 input.setUniqueId("INPO1");
358 List inputlist = new ArrayList<>();
359 inputlist.add(input);
360 component.setInputs(inputlist);
361 ComponentInstance componentInstance = new ComponentInstance();
362 componentInstance.setUniqueId("INST0.1");
363 componentInstance.setComponentUid("RES0.1");
364 List compinstancelist = new ArrayList<>();
365 compinstancelist.add(componentInstance);
366 component.setComponentInstances(compinstancelist);
367 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(component));
368 when(componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(any(Component.class),eq("INPO1"))).thenReturn(compinstancelist);
369 Either<List<ComponentInstanceInput>, ResponseFormat> result = testInstance.getInputsForComponentInput("USR01", COMPONENT_ID,"INPO1");
370 assertEquals(true,result.isLeft());
373 private List<ComponentInstancePropInput> getPropertiesListForDeclaration() {
374 return inputsList.stream().map(this::getPropertyForDeclaration).collect(Collectors.toList());
377 private ComponentInstancePropInput getPropertyForDeclaration(ComponentInstanceInput componentInstanceInput) {
378 ComponentInstancePropInput propInput = new ComponentInstancePropInput();
379 propInput.setInput(componentInstanceInput);
380 propInput.setPropertiesName(componentInstanceInput.getName());
385 private List<InputDefinition> getDeclaredProperties() {
386 return inputsList.stream().map(InputDefinition::new).collect(Collectors.toList());
390 private InputDefinition setUpListInput() {
391 InputDefinition listInput = new InputDefinition();
392 listInput.setName(LISTINPUT_NAME);
393 listInput.setType("list");
394 SchemaDefinition listInputSchema = new SchemaDefinition();
395 listInputSchema.setProperty(new PropertyDataDefinitionBuilder()
396 .setType(LISTINPUT_SCHEMA_TYPE)
397 .setIsRequired(false)
400 listInput.setSchema(listInputSchema);
404 private ComponentInstListInput setUpCreateListInputParams() {
405 ComponentInstListInput componentInstListInput = new ComponentInstListInput();
407 // Create a "list input"
408 InputDefinition listInput = setUpListInput();
409 componentInstListInput.setListInput(listInput);
411 // Create ComponentInstancePropInputs
412 // for inputs in the ComponentInstance
413 Map<String, List<ComponentInstancePropInput>> propInputsListMap = new HashMap<>();
414 // Add 2 PropInputs. property owner is COMPONENT_INSTANCE_ID
415 List<ComponentInstancePropInput> propInputsList = new ArrayList<>();
416 ComponentInstancePropInput propInput = new ComponentInstancePropInput();
417 propInput.setName(LISTINPUT_PROP1_NAME);
418 propInput.setType(LISTINPUT_PROP1_TYPE);
419 propInput.setUniqueId(COMPONENT_INSTANCE_ID+"."+LISTINPUT_PROP1_NAME);
420 propInputsList.add(propInput);
421 propInput = new ComponentInstancePropInput();
422 propInput.setName(LISTINPUT_PROP2_NAME);
423 propInput.setType(LISTINPUT_PROP2_TYPE);
424 propInput.setUniqueId(COMPONENT_INSTANCE_ID+"."+LISTINPUT_PROP2_NAME);
425 propInputsList.add(propInput);
426 propInputsListMap.put(COMPONENT_INSTANCE_ID, propInputsList);
427 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
428 componentInstInputsMap.setComponentInstanceInputsMap(propInputsListMap);
429 componentInstListInput.setComponentInstInputsMap(componentInstInputsMap);
431 return componentInstListInput;
435 public void test_createListInput_success() throws Exception {
436 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
437 ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();
438 List<ComponentInstancePropInput> propInputsList = componentInstInputsMap.getComponentInstanceInputsMap().get(COMPONENT_INSTANCE_ID);
439 InputDefinition listInput = createListInputParams.getListInput();
441 // set up mock returns
442 // for get component object:
443 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
444 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
445 // for data type creation (use captor):
446 when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
447 when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
448 when(propertyDeclarationOrchestrator.declarePropertiesToListInput(service, componentInstInputsMap, listInput)).thenReturn(Either.left(listInput));
449 // for BaseOperation.getAllDataTypes:
450 when(applicationDataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // don't use Collections.emptyMap
451 // for BaseOperation.validatePropertyDefaultValue:
452 when(propertyOperation.isPropertyTypeValid(any())).thenReturn(true);
453 when(propertyOperation.isPropertyInnerTypeValid(any(),any())).thenReturn(new ImmutablePair<>(listInput.getSchemaType(), true));
454 when(propertyOperation.isPropertyDefaultValueValid(any(), any())).thenReturn(true);
455 // for createListInputsInGraph:
456 when(toscaOperationFacadeMock.addInputsToComponent(anyMap(), eq(COMPONENT_ID))).thenReturn(Either.left(Arrays.asList(listInput)));
457 // for rollback/commit:
458 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
459 // for unlock resource
460 when(graphLockOperation.unlockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
462 Either<List<InputDefinition>, ResponseFormat> result =
463 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
465 assertEquals(true, result.isLeft());
466 List<InputDefinition> resultInputList = result.left().value();
467 assertEquals(1, resultInputList.size());
468 //InputDefinition resultInput = resultInputList.get(0);
469 Map<String, DataTypeDefinition> captoredDataTypeMap = dataTypesMapCaptor.getValue();
470 assertEquals(1, captoredDataTypeMap.size());
471 assertEquals(true, captoredDataTypeMap.containsKey(LISTINPUT_SCHEMA_TYPE));
472 DataTypeDefinition captoredDataType = captoredDataTypeMap.get(LISTINPUT_SCHEMA_TYPE);
473 assertEquals("tosca.datatypes.Root", captoredDataType.getDerivedFromName());
474 assertEquals( propInputsList.size(), captoredDataType.getProperties().size());
475 // confirm if corresponding property exists in data type
476 captoredDataType.getProperties().forEach(dataTypeProp -> {
477 Optional<ComponentInstancePropInput> find = propInputsList.stream()
478 .filter(propInput -> propInput.getName().equals(dataTypeProp.getName())).findAny();
479 assertEquals(true, find.isPresent());
484 public void test_createListInput_fail_getComponent() throws Exception {
485 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
486 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
487 Either<List<InputDefinition>, ResponseFormat> result =
488 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
489 assertEquals(true, result.isRight());
494 public void test_createListInput_fail_lockComponent() throws Exception {
495 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
496 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
497 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.FAILED_TO_LOCK_ELEMENT);
498 Either<List<InputDefinition>, ResponseFormat> result =
499 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
500 assertEquals(true, result.isRight());
504 public void test_createListInput_fail_getAllDataTypes() throws Exception {
505 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
506 ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();
508 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
509 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
510 when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
511 when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
512 when(applicationDataTypeCache.getAll()).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
513 when(componentsUtilsMock.getResponseFormat(ActionStatus.DATA_TYPE_CANNOT_BE_EMPTY)).thenReturn(new ResponseFormat());
515 Either<List<InputDefinition>, ResponseFormat> result =
516 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
517 assertEquals(true, result.isRight());
518 verify(applicationDataTypeCache, times(1)).getAll();
519 verify(componentsUtilsMock, times(1)).getResponseFormat(ActionStatus.DATA_TYPE_CANNOT_BE_EMPTY);
523 public void test_createListInput_fail_prepareAndValidateInput() throws Exception {
524 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
525 ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();
526 InputDefinition listInput = createListInputParams.getListInput();
528 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
529 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
530 when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
531 when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
532 when(applicationDataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // don't use Collections.emptyMap
533 // for BaseOperation.validatePropertyDefaultValue:
534 when(propertyOperation.isPropertyTypeValid(any())).thenReturn(false);
536 Either<List<InputDefinition>, ResponseFormat> result =
537 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
538 assertEquals(true, result.isRight());
539 verify(propertyOperation, times(1)).isPropertyTypeValid(any());
543 public void test_createListInput_fail_addInputsToComponent() throws Exception {
544 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
545 ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();
546 InputDefinition listInput = createListInputParams.getListInput();
548 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
549 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
550 when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
551 when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
552 when(applicationDataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // don't use Collections.emptyMap
553 // for BaseOperation.validatePropertyDefaultValue:
554 when(propertyOperation.isPropertyTypeValid(any())).thenReturn(true);
555 when(propertyOperation.isPropertyInnerTypeValid(any(),any())).thenReturn(new ImmutablePair<>(listInput.getSchemaType(), true));
556 when(propertyOperation.isPropertyDefaultValueValid(any(), any())).thenReturn(true);
557 when(toscaOperationFacadeMock.addInputsToComponent(anyMap(), eq(COMPONENT_ID))).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
559 Either<List<InputDefinition>, ResponseFormat> result =
560 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
561 assertEquals(true, result.isRight());
562 verify(toscaOperationFacadeMock, times(1)).addInputsToComponent(anyMap(), eq(COMPONENT_ID));
566 public void test_deleteInput_listInput_fail_getComponent() throws Exception {
567 //ComponentInstListInput createListInputParams = setUpCreateListInputParams();
568 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
569 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
570 when(componentsUtilsMock.getResponseFormat(any(ActionStatus.class))).thenReturn(new ResponseFormat());
572 Either<InputDefinition, ResponseFormat> result = testInstance.deleteInput(COMPONENT_ID, USER_ID, LISTINPUT_NAME);
573 assertEquals(true, result.isRight());
574 verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
579 public void test_deleteInput_listInput_fail_validateInput() throws Exception {
580 InputDefinition listInput = setUpListInput();
581 String inputId = COMPONENT_ID + "." + listInput.getName();
582 listInput.setUniqueId(inputId);
583 service.setInputs(Collections.singletonList(listInput));
584 //ComponentInstListInput createListInputParams = setUpCreateListInputParams();
585 final String NONEXIST_INPUT_NAME = "myInput";
587 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
588 .thenReturn(Either.left(service));
589 when(componentsUtilsMock.getResponseFormat(any(ActionStatus.class))).thenReturn(new ResponseFormat());
591 Either<InputDefinition, ResponseFormat> result = testInstance.deleteInput(COMPONENT_ID, USER_ID, NONEXIST_INPUT_NAME);
592 assertEquals(true, result.isRight());
593 verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
598 public void test_deleteInput_listInput_fail_lockComponent() throws Exception {
599 InputDefinition listInput = setUpListInput();
600 String inputId = COMPONENT_ID + "." + listInput.getName();
601 listInput.setUniqueId(inputId);
602 service.setInputs(Collections.singletonList(listInput));
604 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
605 .thenReturn(Either.left(service));
606 //when(componentsUtilsMock.getResponseFormat(any())).thenReturn(new ResponseFormat());
607 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.NOT_FOUND);
609 Either<InputDefinition, ResponseFormat> result = testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
610 assertEquals(true, result.isRight());
611 verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
612 verify(graphLockOperation, times(1)).lockComponent(COMPONENT_ID, NodeTypeEnum.Service);
617 public void test_deleteInput_listInput_fail_deleteInput() throws Exception {
618 InputDefinition listInput = setUpListInput();
619 String inputId = COMPONENT_ID + "." + listInput.getName();
620 listInput.setUniqueId(inputId);
621 service.setInputs(Collections.singletonList(listInput));
623 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
624 .thenReturn(Either.left(service));
625 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
626 when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.BAD_REQUEST);
627 when(componentsUtilsMock.getResponseFormat(any(ActionStatus.class))).thenReturn(new ResponseFormat());
629 Either<InputDefinition, ResponseFormat> result = testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
630 assertEquals(true, result.isRight());
631 verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
632 verify(graphLockOperation, times(1)).lockComponent(COMPONENT_ID, NodeTypeEnum.Service);
633 verify(toscaOperationFacadeMock, times(1)).deleteInputOfResource(service, listInput.getName());
638 public void test_deleteInput_listInput_success() throws Exception {
639 InputDefinition listInput = setUpListInput();
640 String inputId = COMPONENT_ID + "." + listInput.getName();
641 listInput.setUniqueId(inputId);
642 listInput.setIsDeclaredListInput(true);
643 service.setInputs(Collections.singletonList(listInput));
644 ArgumentCaptor<String> schemaTypeCaptor = ArgumentCaptor.forClass(String.class);
646 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
647 .thenReturn(Either.left(service));
648 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
649 when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.OK);
650 //when(componentsUtilsMock.getResponseFormat(any())).thenReturn(new ResponseFormat());
651 when(propertyDeclarationOrchestrator.unDeclarePropertiesAsListInputs(service, listInput)).thenReturn(StorageOperationStatus.OK);
652 when(dataTypeBusinessLogic.deletePrivateDataType(eq(service), schemaTypeCaptor.capture()))
653 .thenReturn(Either.left(new DataTypeDefinition())); // TODO: replace to return proper datatype
654 //when(propertyDeclarationOrchestrator.unDeclarePropertiesAsInputs(service, listInput)).thenReturn(StorageOperationStatus.OK);
656 Either<InputDefinition, ResponseFormat> result = testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
657 assertEquals(true, result.isLeft());
658 verify(propertyDeclarationOrchestrator, times(1)).unDeclarePropertiesAsListInputs(service, listInput);
659 verify(dataTypeBusinessLogic, times(1)).deletePrivateDataType(service, listInput.getSchemaType());
660 assertEquals(listInput.getSchemaType(), schemaTypeCaptor.getValue());
665 public void test_deleteInput_input_fail_unDeclare() throws Exception {
666 InputDefinition listInput = setUpListInput();
667 String inputId = COMPONENT_ID + "." + listInput.getName();
668 listInput.setUniqueId(inputId);
669 listInput.setIsDeclaredListInput(false);
670 service.setInputs(Collections.singletonList(listInput));
672 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
673 .thenReturn(Either.left(service));
674 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
675 when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.OK);
676 //when(componentsUtilsMock.getResponseFormat(any())).thenReturn(new ResponseFormat());
677 when(propertyDeclarationOrchestrator.unDeclarePropertiesAsInputs(service, listInput)).thenReturn(StorageOperationStatus.BAD_REQUEST);
679 Either<InputDefinition, ResponseFormat> result = testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
680 assertEquals(true, result.isRight());
681 verify(propertyDeclarationOrchestrator, times(1)).unDeclarePropertiesAsInputs(service, listInput);
686 public void test_deleteInput_input_success() throws Exception {
687 InputDefinition listInput = setUpListInput();
688 String inputId = COMPONENT_ID + "." + listInput.getName();
689 listInput.setUniqueId(inputId);
690 listInput.setIsDeclaredListInput(false);
691 service.setInputs(Collections.singletonList(listInput));
693 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
694 .thenReturn(Either.left(service));
695 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
696 when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.OK);
697 //when(componentsUtilsMock.getResponseFormat(any())).thenReturn(new ResponseFormat());
698 when(propertyDeclarationOrchestrator.unDeclarePropertiesAsInputs(service, listInput)).thenReturn(StorageOperationStatus.OK);
700 Either<InputDefinition, ResponseFormat> result = testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
701 assertEquals(true, result.isLeft());
702 verify(propertyDeclarationOrchestrator, times(1)).unDeclarePropertiesAsInputs(service, listInput);