2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.components.impl;
23 import fj.data.Either;
24 import org.apache.commons.lang3.tuple.ImmutablePair;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.ArgumentCaptor;
28 import org.mockito.Captor;
29 import org.mockito.InjectMocks;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.mockito.MockitoAnnotations;
33 import org.openecomp.sdc.be.components.property.PropertyDeclarationOrchestrator;
34 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
35 import org.openecomp.sdc.be.components.validation.UserValidations;
36 import org.openecomp.sdc.be.dao.api.ActionStatus;
37 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
38 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
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.*;
44 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
45 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
46 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
47 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
48 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
49 import org.openecomp.sdc.be.user.IUserBusinessLogic;
50 import org.openecomp.sdc.exception.ResponseFormat;
54 import static org.junit.Assert.assertEquals;
55 import static org.junit.Assert.assertTrue;
56 import static org.mockito.ArgumentMatchers.*;
57 import static org.mockito.Mockito.times;
58 import static org.mockito.Mockito.verify;
59 import static org.mockito.Mockito.when;
61 public class InputsBusinessLogicTest {
63 private static final String COMPONENT_INSTANCE_ID = "instanceId";
64 private static final String COMPONENT_ID = "componentId";
65 private static final String USER_ID = "userId";
66 private static final String INSTANCE_INPUT_ID = "inputId";
67 private static final String LISTINPUT_NAME = "listInput";
68 private static final String LISTINPUT_SCHEMA_TYPE = "org.onap.datatypes.listinput";
69 private static final String LISTINPUT_PROP1_NAME = "prop1";
70 private static final String LISTINPUT_PROP1_TYPE = "string";
71 private static final String LISTINPUT_PROP2_NAME = "prop2";
72 private static final String LISTINPUT_PROP2_TYPE = "integer";
75 private ComponentsUtils componentsUtilsMock;
78 private IUserBusinessLogic userAdminMock;
81 private ToscaOperationFacade toscaOperationFacadeMock;
84 private UserValidations userValidations;
87 private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
90 private IGraphLockOperation graphLockOperation;
93 private PropertyDeclarationOrchestrator propertyDeclarationOrchestrator;
96 private ApplicationDataTypeCache applicationDataTypeCache;
99 private PropertyOperation propertyOperation;
102 private TitanDao titanDao;
105 private DataTypeBusinessLogic dataTypeBusinessLogic;
108 private InputsBusinessLogic testInstance;
110 private Service service;
113 ArgumentCaptor<Map<String, DataTypeDefinition>> dataTypesMapCaptor;
116 public void setUp() throws Exception {
117 MockitoAnnotations.initMocks(this);
118 service = new Service();
119 service.setUniqueId(COMPONENT_ID);
120 service.setLastUpdaterUserId(USER_ID);
121 service.setIsDeleted(false);
122 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
124 // add a ComponentInstance
125 ComponentInstance componentInstance = new ComponentInstance();
126 componentInstance.setUniqueId(COMPONENT_INSTANCE_ID);
127 service.setComponentInstances(Collections.singletonList(componentInstance));
129 // add inputs to the ComponentInstance
130 Map<String, List<ComponentInstanceInput>> instanceInputMap = new HashMap<>();
131 ComponentInstanceInput componentInstanceInput = new ComponentInstanceInput();
132 componentInstanceInput.setInputId(INSTANCE_INPUT_ID);
133 instanceInputMap.put(COMPONENT_INSTANCE_ID, Collections.singletonList(componentInstanceInput));
134 instanceInputMap.put("someInputId", Collections.singletonList(new ComponentInstanceInput()));
135 service.setComponentInstancesInputs(instanceInputMap);
137 when(userValidations.validateUserExists(eq(USER_ID), anyString(), eq(false))).thenReturn(new User());
138 when(userAdminMock.getUser(USER_ID, false)).thenReturn(Either.left(new User()));
142 public void getComponentInstanceInputs_ComponentInstanceNotExist() throws Exception {
143 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
144 Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, "nonExisting");
145 assertTrue(componentInstanceInputs.isRight());
146 verify(componentsUtilsMock).getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
150 public void getComponentInstanceInputs_emptyInputsMap() throws Exception {
151 service.setComponentInstancesInputs(Collections.emptyMap());
152 getComponents_emptyInputs(service);
156 public void getComponentInstanceInputs_nullInputsMap() throws Exception {
157 service.setComponentInstancesInputs(null);
158 getComponents_emptyInputs(service);
162 public void getComponentInstanceInputs_instanceHasNoInputs() throws Exception {
163 service.setComponentInstancesInputs(Collections.singletonMap("someInputId", new ArrayList<>()));
164 getComponents_emptyInputs(service);
168 public void getComponentInstanceInputs() throws Exception {
169 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
170 Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, COMPONENT_INSTANCE_ID);
171 assertEquals("inputId", componentInstanceInputs.left().value().get(0).getInputId());
175 public void testGetInputs() {
176 String userId = "userId";
177 String componentId = "compId";
178 Component component = new Resource();
179 when(toscaOperationFacadeMock.getToscaElement(Mockito.any(String.class), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(component));
180 testInstance.getInputs(userId, componentId);
181 assertEquals(null, component.getInputs());
185 public void testGetCIPropertiesByInputId() {
186 Either<List<ComponentInstanceProperty>, ResponseFormat> result;
187 String userId = "userId";
188 String componentId = "compId";
189 Component component = new Resource();
190 List<InputDefinition> listDef = new ArrayList<>();
191 InputDefinition inputDef = new InputDefinition();
192 inputDef.setUniqueId(componentId);
193 listDef.add(inputDef);
194 component.setInputs(listDef);
195 when(toscaOperationFacadeMock.getToscaElement(Mockito.any(String.class), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(component));
196 result = testInstance.getComponentInstancePropertiesByInputId(userId, componentId, componentId, componentId);
197 assertTrue(result.isLeft());
200 private void getComponents_emptyInputs(Service service) {
201 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
202 Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, COMPONENT_INSTANCE_ID);
203 assertEquals(Collections.emptyList(), componentInstanceInputs.left().value());
207 public void testgetInputs_ARTIFACT_NOT_FOUND() throws Exception {
209 when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.ARTIFACT_NOT_FOUND)).thenReturn(ActionStatus.ARTIFACT_NOT_FOUND);
210 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
211 Either<List<InputDefinition>, ResponseFormat> responseFormatEither = testInstance.getInputs("USR01", COMPONENT_ID);
212 assertEquals(true,responseFormatEither.isRight());
217 public void testgetInputs_SUCCESS() throws Exception {
218 Component component = new Service();
219 InputDefinition input= new InputDefinition();
220 List inputlist = new ArrayList<>();
221 inputlist.add(input);
222 component.setInputs(inputlist);
223 when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.ARTIFACT_NOT_FOUND)).thenReturn(ActionStatus.ARTIFACT_NOT_FOUND);
224 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(component));
225 Either<List<InputDefinition>, ResponseFormat> responseFormatEither = testInstance.getInputs("USR01", COMPONENT_ID);
226 assertEquals(inputlist,responseFormatEither.left().value());
230 public void testgetComponentInstancePropertiesByInputId_Artifactnotfound() throws Exception
232 Component component = new Service();
233 InputDefinition input= new InputDefinition();
234 List inputlist = new ArrayList<>();
235 inputlist.add(input);
236 component.setInputs(inputlist);
237 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
238 Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = testInstance.getComponentInstancePropertiesByInputId("USR01", COMPONENT_ID,"INST0.1", "INPO1");
239 assertEquals(true,responseFormatEither.isRight());
243 public void testgetComponentInstancePropertiesByInputId_PARENT_ARTIFACT_NOT_FOUND() throws Exception
245 Component component = new Service();
246 InputDefinition input= new InputDefinition();
247 List inputlist = new ArrayList<>();
248 inputlist.add(input);
249 component.setInputs(inputlist);
250 ComponentInstance componentInstance = new ComponentInstance();
251 componentInstance.setUniqueId("INST0.1");
252 componentInstance.setComponentUid("RES0.1");
253 List compinstancelist = new ArrayList<>();
254 compinstancelist.add(componentInstance);
255 component.setComponentInstances(compinstancelist);
256 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(component));
257 when(toscaOperationFacadeMock.getToscaElement(eq("RES0.1"), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
258 Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = testInstance.getComponentInstancePropertiesByInputId("USR01", COMPONENT_ID,"INST0.1", "INPO1");
259 assertEquals(true,responseFormatEither.isRight());
263 public void testgetComponentInstancePropertiesByInputId() throws Exception
265 Component component = new Service();
266 InputDefinition input= new InputDefinition();
267 input.setUniqueId("INPO1");
268 List inputlist = new ArrayList<>();
269 inputlist.add(input);
270 component.setInputs(inputlist);
271 ComponentInstance componentInstance = new ComponentInstance();
272 componentInstance.setUniqueId("INST0.1");
273 componentInstance.setComponentUid("RES0.1");
274 List compinstancelist = new ArrayList<>();
275 compinstancelist.add(componentInstance);
276 component.setComponentInstances(compinstancelist);
277 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(component));
278 when(componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(Mockito.any(Component.class),eq("INPO1"))).thenReturn(compinstancelist);
279 //when(toscaOperationFacadeMock.getToscaElement(eq("RES0.1"), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
280 when(toscaOperationFacadeMock.getToscaElement(eq("RES0.1"), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(component));
281 Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = testInstance.getComponentInstancePropertiesByInputId("USR01", COMPONENT_ID,"INST0.1", "INPO1");
282 assertEquals(compinstancelist,responseFormatEither.left().value());
286 public void testgetInputsForComponentInput_ARTIFACT_NOT_FOUND() throws Exception
288 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
289 Either<List<ComponentInstanceInput>, ResponseFormat> result = testInstance.getInputsForComponentInput("USR01", COMPONENT_ID,"INPO1");
290 assertEquals(true,result.isRight());
294 public void testgetInputsForComponentInput() throws Exception
296 Component component = new Service();
297 InputDefinition input= new InputDefinition();
298 input.setUniqueId("INPO1");
299 List inputlist = new ArrayList<>();
300 inputlist.add(input);
301 component.setInputs(inputlist);
302 ComponentInstance componentInstance = new ComponentInstance();
303 componentInstance.setUniqueId("INST0.1");
304 componentInstance.setComponentUid("RES0.1");
305 List compinstancelist = new ArrayList<>();
306 compinstancelist.add(componentInstance);
307 component.setComponentInstances(compinstancelist);
308 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(component));
309 when(componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(Mockito.any(Component.class),eq("INPO1"))).thenReturn(compinstancelist);
310 Either<List<ComponentInstanceInput>, ResponseFormat> result = testInstance.getInputsForComponentInput("USR01", COMPONENT_ID,"INPO1");
311 assertEquals(true,result.isLeft());
315 private InputDefinition setUpListInput() {
316 InputDefinition listInput = new InputDefinition();
317 listInput.setName(LISTINPUT_NAME);
318 listInput.setType("list");
319 SchemaDefinition listInputSchema = new SchemaDefinition();
320 listInputSchema.setProperty(new PropertyDataDefinitionBuilder()
321 .setType(LISTINPUT_SCHEMA_TYPE)
322 .setIsRequired(false)
325 listInput.setSchema(listInputSchema);
329 private ComponentInstListInput setUpCreateListInputParams() {
330 ComponentInstListInput componentInstListInput = new ComponentInstListInput();
332 // Create a "list input"
333 InputDefinition listInput = setUpListInput();
334 componentInstListInput.setListInput(listInput);
336 // Create ComponentInstancePropInputs
337 // for inputs in the ComponentInstance
338 Map<String, List<ComponentInstancePropInput>> propInputsListMap = new HashMap<>();
339 // Add 2 PropInputs. property owner is COMPONENT_INSTANCE_ID
340 List<ComponentInstancePropInput> propInputsList = new ArrayList<>();
341 ComponentInstancePropInput propInput = new ComponentInstancePropInput();
342 propInput.setName(LISTINPUT_PROP1_NAME);
343 propInput.setType(LISTINPUT_PROP1_TYPE);
344 propInput.setUniqueId(COMPONENT_INSTANCE_ID+"."+LISTINPUT_PROP1_NAME);
345 propInputsList.add(propInput);
346 propInput = new ComponentInstancePropInput();
347 propInput.setName(LISTINPUT_PROP2_NAME);
348 propInput.setType(LISTINPUT_PROP2_TYPE);
349 propInput.setUniqueId(COMPONENT_INSTANCE_ID+"."+LISTINPUT_PROP2_NAME);
350 propInputsList.add(propInput);
351 propInputsListMap.put(COMPONENT_INSTANCE_ID, propInputsList);
352 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
353 componentInstInputsMap.setComponentInstanceInputsMap(propInputsListMap);
354 componentInstListInput.setComponentInstInputsMap(componentInstInputsMap);
356 return componentInstListInput;
360 public void test_createListInput_success() throws Exception {
361 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
362 ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();
363 List<ComponentInstancePropInput> propInputsList = componentInstInputsMap.getComponentInstanceInputsMap().get(COMPONENT_INSTANCE_ID);
364 InputDefinition listInput = createListInputParams.getListInput();
366 // set up mock returns
367 // for get component object:
368 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
369 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
370 // for data type creation (use captor):
371 when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
372 when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
373 when(propertyDeclarationOrchestrator.declarePropertiesToListInput(service, componentInstInputsMap, listInput)).thenReturn(Either.left(listInput));
374 // for BaseOperation.getAllDataTypes:
375 when(applicationDataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // don't use Collections.emptyMap
376 // for BaseOperation.validatePropertyDefaultValue:
377 when(propertyOperation.isPropertyTypeValid(any())).thenReturn(true);
378 when(propertyOperation.isPropertyInnerTypeValid(any(),any())).thenReturn(new ImmutablePair<>(listInput.getSchemaType(), true));
379 when(propertyOperation.isPropertyDefaultValueValid(any(), any())).thenReturn(true);
380 // for createListInputsInGraph:
381 when(toscaOperationFacadeMock.addInputsToComponent(anyMap(), eq(COMPONENT_ID))).thenReturn(Either.left(Arrays.asList(listInput)));
382 // for rollback/commit:
383 when(titanDao.commit()).thenReturn(TitanOperationStatus.OK);
384 // for unlock resource
385 when(graphLockOperation.unlockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
387 Either<List<InputDefinition>, ResponseFormat> result =
388 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
390 assertEquals(true, result.isLeft());
391 List<InputDefinition> resultInputList = result.left().value();
392 assertEquals(1, resultInputList.size());
393 //InputDefinition resultInput = resultInputList.get(0);
394 Map<String, DataTypeDefinition> captoredDataTypeMap = dataTypesMapCaptor.getValue();
395 assertEquals(1, captoredDataTypeMap.size());
396 assertEquals(true, captoredDataTypeMap.containsKey(LISTINPUT_SCHEMA_TYPE));
397 DataTypeDefinition captoredDataType = captoredDataTypeMap.get(LISTINPUT_SCHEMA_TYPE);
398 assertEquals("tosca.datatypes.Root", captoredDataType.getDerivedFromName());
399 assertEquals( propInputsList.size(), captoredDataType.getProperties().size());
400 // confirm if corresponding property exists in data type
401 captoredDataType.getProperties().forEach(dataTypeProp -> {
402 Optional<ComponentInstancePropInput> find = propInputsList.stream()
403 .filter(propInput -> propInput.getName().equals(dataTypeProp.getName())).findAny();
404 assertEquals(true, find.isPresent());
409 public void test_createListInput_fail_getComponent() throws Exception {
410 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
411 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
412 Either<List<InputDefinition>, ResponseFormat> result =
413 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
414 assertEquals(true, result.isRight());
419 public void test_createListInput_fail_lockComponent() throws Exception {
420 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
421 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
422 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.FAILED_TO_LOCK_ELEMENT);
423 Either<List<InputDefinition>, ResponseFormat> result =
424 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
425 assertEquals(true, result.isRight());
429 public void test_createListInput_fail_getAllDataTypes() throws Exception {
430 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
431 ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();
433 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
434 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
435 when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
436 when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
437 when(applicationDataTypeCache.getAll()).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
438 when(componentsUtilsMock.getResponseFormat(ActionStatus.DATA_TYPE_CANNOT_BE_EMPTY)).thenReturn(new ResponseFormat());
440 Either<List<InputDefinition>, ResponseFormat> result =
441 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
442 assertEquals(true, result.isRight());
443 verify(applicationDataTypeCache, times(1)).getAll();
444 verify(componentsUtilsMock, times(1)).getResponseFormat(ActionStatus.DATA_TYPE_CANNOT_BE_EMPTY);
448 public void test_createListInput_fail_prepareAndValidateInput() throws Exception {
449 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
450 ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();
451 InputDefinition listInput = createListInputParams.getListInput();
453 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
454 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
455 when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
456 when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
457 when(applicationDataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // don't use Collections.emptyMap
458 // for BaseOperation.validatePropertyDefaultValue:
459 when(propertyOperation.isPropertyTypeValid(any())).thenReturn(false);
461 Either<List<InputDefinition>, ResponseFormat> result =
462 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
463 assertEquals(true, result.isRight());
464 verify(propertyOperation, times(1)).isPropertyTypeValid(any());
468 public void test_createListInput_fail_addInputsToComponent() throws Exception {
469 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
470 ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();
471 InputDefinition listInput = createListInputParams.getListInput();
473 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
474 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
475 when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
476 when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
477 when(applicationDataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // don't use Collections.emptyMap
478 // for BaseOperation.validatePropertyDefaultValue:
479 when(propertyOperation.isPropertyTypeValid(any())).thenReturn(true);
480 when(propertyOperation.isPropertyInnerTypeValid(any(),any())).thenReturn(new ImmutablePair<>(listInput.getSchemaType(), true));
481 when(propertyOperation.isPropertyDefaultValueValid(any(), any())).thenReturn(true);
482 when(toscaOperationFacadeMock.addInputsToComponent(anyMap(), eq(COMPONENT_ID))).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
484 Either<List<InputDefinition>, ResponseFormat> result =
485 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
486 assertEquals(true, result.isRight());
487 verify(toscaOperationFacadeMock, times(1)).addInputsToComponent(anyMap(), eq(COMPONENT_ID));
491 public void test_deleteInput_listInput_fail_getComponent() throws Exception {
492 //ComponentInstListInput createListInputParams = setUpCreateListInputParams();
493 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
494 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
495 when(componentsUtilsMock.getResponseFormat(any())).thenReturn(new ResponseFormat());
497 Either<InputDefinition, ResponseFormat> result = testInstance.deleteInput(COMPONENT_ID, USER_ID, LISTINPUT_NAME);
498 assertEquals(true, result.isRight());
499 verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
504 public void test_deleteInput_listInput_fail_validateInput() throws Exception {
505 InputDefinition listInput = setUpListInput();
506 String inputId = COMPONENT_ID + "." + listInput.getName();
507 listInput.setUniqueId(inputId);
508 service.setInputs(Collections.singletonList(listInput));
509 //ComponentInstListInput createListInputParams = setUpCreateListInputParams();
510 final String NONEXIST_INPUT_NAME = "myInput";
512 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
513 .thenReturn(Either.left(service));
514 when(componentsUtilsMock.getResponseFormat(any())).thenReturn(new ResponseFormat());
516 Either<InputDefinition, ResponseFormat> result = testInstance.deleteInput(COMPONENT_ID, USER_ID, NONEXIST_INPUT_NAME);
517 assertEquals(true, result.isRight());
518 verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
523 public void test_deleteInput_listInput_fail_lockComponent() throws Exception {
524 InputDefinition listInput = setUpListInput();
525 String inputId = COMPONENT_ID + "." + listInput.getName();
526 listInput.setUniqueId(inputId);
527 service.setInputs(Collections.singletonList(listInput));
529 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
530 .thenReturn(Either.left(service));
531 //when(componentsUtilsMock.getResponseFormat(any())).thenReturn(new ResponseFormat());
532 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.NOT_FOUND);
534 Either<InputDefinition, ResponseFormat> result = testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
535 assertEquals(true, result.isRight());
536 verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
537 verify(graphLockOperation, times(1)).lockComponent(COMPONENT_ID, NodeTypeEnum.Service);
542 public void test_deleteInput_listInput_fail_deleteInput() throws Exception {
543 InputDefinition listInput = setUpListInput();
544 String inputId = COMPONENT_ID + "." + listInput.getName();
545 listInput.setUniqueId(inputId);
546 service.setInputs(Collections.singletonList(listInput));
548 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
549 .thenReturn(Either.left(service));
550 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
551 when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.BAD_REQUEST);
552 when(componentsUtilsMock.getResponseFormat(any())).thenReturn(new ResponseFormat());
554 Either<InputDefinition, ResponseFormat> result = testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
555 assertEquals(true, result.isRight());
556 verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
557 verify(graphLockOperation, times(1)).lockComponent(COMPONENT_ID, NodeTypeEnum.Service);
558 verify(toscaOperationFacadeMock, times(1)).deleteInputOfResource(service, listInput.getName());
563 public void test_deleteInput_listInput_success() throws Exception {
564 InputDefinition listInput = setUpListInput();
565 String inputId = COMPONENT_ID + "." + listInput.getName();
566 listInput.setUniqueId(inputId);
567 listInput.setIsDeclaredListInput(true);
568 service.setInputs(Collections.singletonList(listInput));
569 ArgumentCaptor<String> schemaTypeCaptor = ArgumentCaptor.forClass(String.class);
571 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
572 .thenReturn(Either.left(service));
573 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
574 when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.OK);
575 //when(componentsUtilsMock.getResponseFormat(any())).thenReturn(new ResponseFormat());
576 when(propertyDeclarationOrchestrator.unDeclarePropertiesAsListInputs(service, listInput)).thenReturn(StorageOperationStatus.OK);
577 when(dataTypeBusinessLogic.deletePrivateDataType(eq(service), schemaTypeCaptor.capture()))
578 .thenReturn(Either.left(new DataTypeDefinition())); // TODO: replace to return proper datatype
579 //when(propertyDeclarationOrchestrator.unDeclarePropertiesAsInputs(service, listInput)).thenReturn(StorageOperationStatus.OK);
581 Either<InputDefinition, ResponseFormat> result = testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
582 assertEquals(true, result.isLeft());
583 verify(propertyDeclarationOrchestrator, times(1)).unDeclarePropertiesAsListInputs(service, listInput);
584 verify(dataTypeBusinessLogic, times(1)).deletePrivateDataType(service, listInput.getSchemaType());
585 assertEquals(listInput.getSchemaType(), schemaTypeCaptor.getValue());
590 public void test_deleteInput_input_fail_unDeclare() throws Exception {
591 InputDefinition listInput = setUpListInput();
592 String inputId = COMPONENT_ID + "." + listInput.getName();
593 listInput.setUniqueId(inputId);
594 listInput.setIsDeclaredListInput(false);
595 service.setInputs(Collections.singletonList(listInput));
597 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
598 .thenReturn(Either.left(service));
599 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
600 when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.OK);
601 //when(componentsUtilsMock.getResponseFormat(any())).thenReturn(new ResponseFormat());
602 when(propertyDeclarationOrchestrator.unDeclarePropertiesAsInputs(service, listInput)).thenReturn(StorageOperationStatus.BAD_REQUEST);
604 Either<InputDefinition, ResponseFormat> result = testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
605 assertEquals(true, result.isRight());
606 verify(propertyDeclarationOrchestrator, times(1)).unDeclarePropertiesAsInputs(service, listInput);
611 public void test_deleteInput_input_success() throws Exception {
612 InputDefinition listInput = setUpListInput();
613 String inputId = COMPONENT_ID + "." + listInput.getName();
614 listInput.setUniqueId(inputId);
615 listInput.setIsDeclaredListInput(false);
616 service.setInputs(Collections.singletonList(listInput));
618 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
619 .thenReturn(Either.left(service));
620 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
621 when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.OK);
622 //when(componentsUtilsMock.getResponseFormat(any())).thenReturn(new ResponseFormat());
623 when(propertyDeclarationOrchestrator.unDeclarePropertiesAsInputs(service, listInput)).thenReturn(StorageOperationStatus.OK);
625 Either<InputDefinition, ResponseFormat> result = testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
626 assertEquals(true, result.isLeft());
627 verify(propertyDeclarationOrchestrator, times(1)).unDeclarePropertiesAsInputs(service, listInput);