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.assertj.core.api.Assertions.assertThat;
78 import static org.junit.Assert.assertEquals;
79 import static org.junit.Assert.assertNull;
80 import static org.junit.Assert.fail;
81 import static org.mockito.ArgumentMatchers.any;
82 import static org.mockito.ArgumentMatchers.anyMap;
83 import static org.mockito.ArgumentMatchers.eq;
84 import static org.mockito.Mockito.times;
85 import static org.mockito.Mockito.verify;
86 import static org.mockito.Mockito.when;
89 public class InputsBusinessLogicTest {
91 private static final String COMPONENT_INSTANCE_ID = "instanceId";
92 private static final String COMPONENT_ID = "componentId";
93 private static final String USER_ID = "userId";
94 private static final String INPUT_ID = "inputId";
95 private static final String INPUT_TYPE = "string";
96 private static final String LISTINPUT_NAME = "listInput";
97 private static final String LISTINPUT_SCHEMA_TYPE = "org.onap.datatypes.listinput";
98 private static final String LISTINPUT_PROP1_NAME = "prop1";
99 private static final String LISTINPUT_PROP1_TYPE = "string";
100 private static final String LISTINPUT_PROP2_NAME = "prop2";
101 private static final String LISTINPUT_PROP2_TYPE = "integer";
102 private static final String OLD_VALUE = "old value";
103 private static final String NEW_VALUE = "new value";
104 static ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be");
105 static ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
108 private ComponentsUtils componentsUtilsMock;
111 private UserBusinessLogic userAdminMock;
114 private ToscaOperationFacade toscaOperationFacadeMock;
117 private UserValidations userValidations;
120 private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
123 private IGraphLockOperation graphLockOperation;
126 private PropertyDeclarationOrchestrator propertyDeclarationOrchestrator;
129 private ApplicationDataTypeCache applicationDataTypeCache;
132 private PropertyOperation propertyOperation;
135 private JanusGraphDao janusGraphDao;
138 private DataTypeBusinessLogic dataTypeBusinessLogic;
141 private InputsBusinessLogic testInstance;
143 private Service service;
146 ArgumentCaptor<Map<String, DataTypeDefinition>> dataTypesMapCaptor;
148 private Map<String, List<ComponentInstanceInput>> instanceInputMap;
149 private List<ComponentInstanceInput> inputsList;
152 public void setUp() {
153 MockitoAnnotations.initMocks(this);
154 service = new Service();
155 service.setUniqueId(COMPONENT_ID);
156 service.setLastUpdaterUserId(USER_ID);
157 service.setIsDeleted(false);
158 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
160 testInstance.setUserValidations(userValidations);
161 testInstance.setToscaOperationFacade(toscaOperationFacadeMock);
162 testInstance.setGraphLockOperation(graphLockOperation);
163 testInstance.setComponentsUtils(componentsUtilsMock);
164 testInstance.setJanusGraphDao(janusGraphDao);
165 testInstance.setApplicationDataTypeCache(applicationDataTypeCache);
166 testInstance.setPropertyOperation(propertyOperation);
168 // add a ComponentInstance
169 ComponentInstance componentInstance = new ComponentInstance();
170 componentInstance.setUniqueId(COMPONENT_INSTANCE_ID);
171 service.setComponentInstances(Collections.singletonList(componentInstance));
173 instanceInputMap = new HashMap<>();
174 ComponentInstanceInput componentInstanceInput = new ComponentInstanceInput();
175 componentInstanceInput.setInputId(INPUT_ID);
176 componentInstanceInput.setName(INPUT_ID);
177 inputsList = Collections.singletonList(componentInstanceInput);
178 instanceInputMap.put(COMPONENT_INSTANCE_ID, inputsList);
179 instanceInputMap.put("someInputId", Collections.singletonList(new ComponentInstanceInput()));
180 service.setComponentInstancesInputs(instanceInputMap);
181 when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(new User());
182 when(userAdminMock.getUser(USER_ID, false)).thenReturn(new User());
186 public void getComponentInstanceInputs_ComponentInstanceNotExist() {
187 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
188 Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, "nonExisting");
189 assertThat(componentInstanceInputs.isRight()).isTrue();
190 verify(componentsUtilsMock).getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
194 public void getComponentInstanceInputs_emptyInputsMap() {
195 service.setComponentInstancesInputs(Collections.emptyMap());
196 getComponents_emptyInputs(service);
200 public void getComponentInstanceInputs_nullInputsMap() {
201 service.setComponentInstancesInputs(null);
202 getComponents_emptyInputs(service);
206 public void getComponentInstanceInputs_instanceHasNoInputs() {
207 service.setComponentInstancesInputs(Collections.singletonMap("someInputId", new ArrayList<>()));
208 getComponents_emptyInputs(service);
212 public void getComponentInstanceInputs() {
213 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
214 Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, COMPONENT_INSTANCE_ID);
215 assertEquals("inputId", componentInstanceInputs.left().value().get(0).getInputId());
219 public void testGetInputs() {
220 String userId = "userId";
221 String componentId = "compId";
222 Component component = new Resource();
223 when(toscaOperationFacadeMock.getToscaElement(any(String.class), any(ComponentParametersView.class))).thenReturn(Either.left(component));
224 testInstance.getInputs(userId, componentId);
225 assertNull(component.getInputs());
229 public void testGetCIPropertiesByInputId() {
230 Either<List<ComponentInstanceProperty>, ResponseFormat> result;
231 String userId = "userId";
232 String componentId = "compId";
233 Component component = new Resource();
234 List<InputDefinition> listDef = new ArrayList<>();
235 InputDefinition inputDef = new InputDefinition();
236 inputDef.setUniqueId(componentId);
237 listDef.add(inputDef);
238 component.setInputs(listDef);
239 when(toscaOperationFacadeMock.getToscaElement(any(String.class), any(ComponentParametersView.class))).thenReturn(Either.left(component));
240 result = testInstance.getComponentInstancePropertiesByInputId(userId, componentId, componentId, componentId);
241 assertThat(result.isLeft()).isTrue();
245 public void testDeclareProperties() {
246 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
247 service.setLastUpdaterUserId(USER_ID);
248 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
249 Map<String, List<ComponentInstancePropInput>> propertiesForDeclaration = new HashMap<>();
250 propertiesForDeclaration.put(COMPONENT_ID, getPropertiesListForDeclaration());
251 componentInstInputsMap.setServiceProperties(propertiesForDeclaration);
253 List<InputDefinition> declaredPropertiesToInputs = getDeclaredProperties();
254 initMockitoStubbings(declaredPropertiesToInputs);
256 Either<List<InputDefinition>, ResponseFormat> declaredPropertiesEither =
257 testInstance.declareProperties(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, componentInstInputsMap);
259 assertThat(declaredPropertiesEither.isLeft()).isTrue();
261 List<InputDefinition> declaredProperties = declaredPropertiesEither.left().value();
262 assertThat(CollectionUtils.isNotEmpty(declaredProperties)).isTrue();
263 assertEquals(1, declaredProperties.size());
264 assertEquals(declaredProperties, declaredPropertiesToInputs);
267 private void initMockitoStubbings(List<InputDefinition> declaredPropertiesToInputs) {
268 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(
269 Either.left(service));
270 when(propertyDeclarationOrchestrator.declarePropertiesToInputs(any(), any())).thenReturn(Either.left(
271 declaredPropertiesToInputs));
272 when(toscaOperationFacadeMock.addInputsToComponent(any(), any())).thenReturn(Either.left(declaredPropertiesToInputs));
273 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
274 when(graphLockOperation.lockComponent(any(), any())).thenReturn(StorageOperationStatus.OK);
275 when(graphLockOperation.unlockComponent(any(), any())).thenReturn(StorageOperationStatus.OK);
276 when(componentInstanceBusinessLogic.setInputConstraint(any())).thenReturn(Collections.emptyList());
279 private void getComponents_emptyInputs(Service service) {
280 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
281 Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, COMPONENT_INSTANCE_ID);
282 assertEquals(Collections.emptyList(), componentInstanceInputs.left().value());
286 public void testgetInputs_ARTIFACT_NOT_FOUND() throws Exception {
288 when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.ARTIFACT_NOT_FOUND)).thenReturn(ActionStatus.ARTIFACT_NOT_FOUND);
289 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
290 Either<List<InputDefinition>, ResponseFormat> responseFormatEither = testInstance.getInputs("USR01", COMPONENT_ID);
291 assertThat(responseFormatEither.isRight()).isTrue();
296 public void testgetInputs_SUCCESS() throws Exception {
297 Component component = new Service();
298 InputDefinition input= new InputDefinition();
299 List inputlist = new ArrayList<>();
300 inputlist.add(input);
301 component.setInputs(inputlist);
302 when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.ARTIFACT_NOT_FOUND)).thenReturn(ActionStatus.ARTIFACT_NOT_FOUND);
303 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(component));
304 Either<List<InputDefinition>, ResponseFormat> responseFormatEither = testInstance.getInputs("USR01", COMPONENT_ID);
305 assertEquals(inputlist,responseFormatEither.left().value());
309 public void testgetComponentInstancePropertiesByInputId_Artifactnotfound() throws Exception
311 Component component = new Service();
312 InputDefinition input= new InputDefinition();
313 List inputlist = new ArrayList<>();
314 inputlist.add(input);
315 component.setInputs(inputlist);
316 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
317 Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = testInstance.getComponentInstancePropertiesByInputId("USR01", COMPONENT_ID,"INST0.1", "INPO1");
318 assertThat(responseFormatEither.isRight()).isTrue();
322 public void testgetComponentInstancePropertiesByInputId_PARENT_ARTIFACT_NOT_FOUND() throws Exception
324 Component component = new Service();
325 InputDefinition input= new InputDefinition();
326 List inputlist = new ArrayList<>();
327 inputlist.add(input);
328 component.setInputs(inputlist);
329 ComponentInstance componentInstance = new ComponentInstance();
330 componentInstance.setUniqueId("INST0.1");
331 componentInstance.setComponentUid("RES0.1");
332 List compinstancelist = new ArrayList<>();
333 compinstancelist.add(componentInstance);
334 component.setComponentInstances(compinstancelist);
335 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(component));
336 when(toscaOperationFacadeMock.getToscaElement(eq("RES0.1"), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
337 Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = testInstance.getComponentInstancePropertiesByInputId("USR01", COMPONENT_ID,"INST0.1", "INPO1");
338 assertThat(responseFormatEither.isRight()).isTrue();
342 public void testgetComponentInstancePropertiesByInputId() throws Exception
344 Component component = new Service();
345 InputDefinition input= new InputDefinition();
346 input.setUniqueId("INPO1");
347 List inputlist = new ArrayList<>();
348 inputlist.add(input);
349 component.setInputs(inputlist);
350 ComponentInstance componentInstance = new ComponentInstance();
351 componentInstance.setUniqueId("INST0.1");
352 componentInstance.setComponentUid("RES0.1");
353 List compinstancelist = new ArrayList<>();
354 compinstancelist.add(componentInstance);
355 component.setComponentInstances(compinstancelist);
356 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(component));
357 when(componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(any(Component.class),eq("INPO1"))).thenReturn(compinstancelist);
358 //when(toscaOperationFacadeMock.getToscaElement(eq("RES0.1"), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
359 when(toscaOperationFacadeMock.getToscaElement(eq("RES0.1"), any(ComponentParametersView.class))).thenReturn(Either.left(component));
360 Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = testInstance.getComponentInstancePropertiesByInputId("USR01", COMPONENT_ID,"INST0.1", "INPO1");
361 assertEquals(compinstancelist,responseFormatEither.left().value());
365 public void testgetInputsForComponentInput_ARTIFACT_NOT_FOUND() throws Exception
367 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
368 Either<List<ComponentInstanceInput>, ResponseFormat> result = testInstance.getInputsForComponentInput("USR01", COMPONENT_ID,"INPO1");
369 assertThat(result.isRight()).isTrue();
373 public void testgetInputsForComponentInput() throws Exception
375 Component component = new Service();
376 InputDefinition input= new InputDefinition();
377 input.setUniqueId("INPO1");
378 List inputlist = new ArrayList<>();
379 inputlist.add(input);
380 component.setInputs(inputlist);
381 ComponentInstance componentInstance = new ComponentInstance();
382 componentInstance.setUniqueId("INST0.1");
383 componentInstance.setComponentUid("RES0.1");
384 List compinstancelist = new ArrayList<>();
385 compinstancelist.add(componentInstance);
386 component.setComponentInstances(compinstancelist);
387 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(component));
388 when(componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(any(Component.class),eq("INPO1"))).thenReturn(compinstancelist);
389 Either<List<ComponentInstanceInput>, ResponseFormat> result = testInstance.getInputsForComponentInput("USR01", COMPONENT_ID,"INPO1");
390 assertThat(result.isLeft()).isTrue();
393 private List<ComponentInstancePropInput> getPropertiesListForDeclaration() {
394 return inputsList.stream().map(this::getPropertyForDeclaration).collect(Collectors.toList());
397 private ComponentInstancePropInput getPropertyForDeclaration(ComponentInstanceInput componentInstanceInput) {
398 ComponentInstancePropInput propInput = new ComponentInstancePropInput();
399 propInput.setInput(componentInstanceInput);
400 propInput.setPropertiesName(componentInstanceInput.getName());
405 private List<InputDefinition> getDeclaredProperties() {
406 return inputsList.stream().map(InputDefinition::new).collect(Collectors.toList());
410 private InputDefinition setUpListInput() {
411 InputDefinition listInput = new InputDefinition();
412 listInput.setName(LISTINPUT_NAME);
413 listInput.setType("list");
414 SchemaDefinition listInputSchema = new SchemaDefinition();
415 listInputSchema.setProperty(new PropertyDataDefinitionBuilder()
416 .setType(LISTINPUT_SCHEMA_TYPE)
417 .setIsRequired(false)
420 listInput.setSchema(listInputSchema);
424 private ComponentInstListInput setUpCreateListInputParams() {
425 ComponentInstListInput componentInstListInput = new ComponentInstListInput();
427 // Create a "list input"
428 InputDefinition listInput = setUpListInput();
429 componentInstListInput.setListInput(listInput);
431 // Create ComponentInstancePropInputs
432 // for inputs in the ComponentInstance
433 Map<String, List<ComponentInstancePropInput>> propInputsListMap = new HashMap<>();
434 // Add 2 PropInputs. property owner is COMPONENT_INSTANCE_ID
435 List<ComponentInstancePropInput> propInputsList = new ArrayList<>();
436 ComponentInstancePropInput propInput = new ComponentInstancePropInput();
437 propInput.setName(LISTINPUT_PROP1_NAME);
438 propInput.setType(LISTINPUT_PROP1_TYPE);
439 propInput.setUniqueId(COMPONENT_INSTANCE_ID+"."+LISTINPUT_PROP1_NAME);
440 propInputsList.add(propInput);
441 propInput = new ComponentInstancePropInput();
442 propInput.setName(LISTINPUT_PROP2_NAME);
443 propInput.setType(LISTINPUT_PROP2_TYPE);
444 propInput.setUniqueId(COMPONENT_INSTANCE_ID+"."+LISTINPUT_PROP2_NAME);
445 propInputsList.add(propInput);
446 propInputsListMap.put(COMPONENT_INSTANCE_ID, propInputsList);
447 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
448 componentInstInputsMap.setComponentInstanceInputsMap(propInputsListMap);
449 componentInstListInput.setComponentInstInputsMap(componentInstInputsMap);
451 return componentInstListInput;
455 public void test_createListInput_success() throws Exception {
456 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
457 ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();
458 List<ComponentInstancePropInput> propInputsList = componentInstInputsMap.getComponentInstanceInputsMap().get(COMPONENT_INSTANCE_ID);
459 InputDefinition listInput = createListInputParams.getListInput();
461 // set up mock returns
462 // for get component object:
463 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
464 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
465 // for data type creation (use captor):
466 when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
467 when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
468 when(propertyDeclarationOrchestrator.declarePropertiesToListInput(service, componentInstInputsMap, listInput)).thenReturn(Either.left(listInput));
469 // for BaseOperation.getAllDataTypes:
470 when(applicationDataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // don't use Collections.emptyMap
471 // for BaseOperation.validatePropertyDefaultValue:
472 when(propertyOperation.isPropertyTypeValid(any(), any())).thenReturn(true);
473 when(propertyOperation.isPropertyInnerTypeValid(any(),any())).thenReturn(new ImmutablePair<>(listInput.getSchemaType(), true));
474 when(propertyOperation.isPropertyDefaultValueValid(any(), any())).thenReturn(true);
475 // for createListInputsInGraph:
476 when(toscaOperationFacadeMock.addInputsToComponent(anyMap(), eq(COMPONENT_ID))).thenReturn(Either.left(Arrays.asList(listInput)));
477 // for rollback/commit:
478 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
479 // for unlock resource
480 when(graphLockOperation.unlockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
482 Either<List<InputDefinition>, ResponseFormat> result =
483 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
485 assertThat(result.isLeft()).isTrue();
486 List<InputDefinition> resultInputList = result.left().value();
487 assertEquals(1, resultInputList.size());
488 //InputDefinition resultInput = resultInputList.get(0);
489 Map<String, DataTypeDefinition> captoredDataTypeMap = dataTypesMapCaptor.getValue();
490 assertEquals(1, captoredDataTypeMap.size());
491 assertThat(captoredDataTypeMap.containsKey(LISTINPUT_SCHEMA_TYPE)).isTrue();
492 DataTypeDefinition captoredDataType = captoredDataTypeMap.get(LISTINPUT_SCHEMA_TYPE);
493 assertEquals("tosca.datatypes.Root", captoredDataType.getDerivedFromName());
494 assertEquals( propInputsList.size(), captoredDataType.getProperties().size());
495 // confirm if corresponding property exists in data type
496 captoredDataType.getProperties().forEach(dataTypeProp -> {
497 Optional<ComponentInstancePropInput> find = propInputsList.stream()
498 .filter(propInput -> propInput.getName().equals(dataTypeProp.getName())).findAny();
499 assertThat(find.isPresent()).isTrue();
504 public void test_createListInput_fail_getComponent() throws Exception {
505 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
506 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
507 when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND, ComponentTypeEnum.SERVICE)).thenReturn(ActionStatus.SERVICE_NOT_FOUND);
509 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
510 } catch (ByActionStatusComponentException e) {
511 assertEquals(ActionStatus.SERVICE_NOT_FOUND, e.getActionStatus());
519 public void test_createListInput_fail_lockComponent() throws Exception {
520 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
521 when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.FAILED_TO_LOCK_ELEMENT, ComponentTypeEnum.SERVICE)).thenReturn(ActionStatus.COMPONENT_IN_USE);
522 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
523 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.FAILED_TO_LOCK_ELEMENT);
525 Either<List<InputDefinition>, ResponseFormat> result =
526 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
527 } catch (ByActionStatusComponentException e) {
528 assertEquals(ActionStatus.COMPONENT_IN_USE, e.getActionStatus());
535 public void test_createListInput_fail_getAllDataTypes() throws Exception {
536 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
537 ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();
539 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
540 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
541 when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
542 when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
543 when(applicationDataTypeCache.getAll()).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
546 Either<List<InputDefinition>, ResponseFormat> result =
547 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
548 } catch (ByActionStatusComponentException e) {
549 assertEquals(ActionStatus.DATA_TYPE_CANNOT_BE_EMPTY, e.getActionStatus());
550 verify(applicationDataTypeCache, times(1)).getAll();
557 public void test_createListInput_fail_prepareAndValidateInput() throws Exception {
558 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
559 ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();
560 InputDefinition listInput = createListInputParams.getListInput();
562 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
563 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
564 when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
565 when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
566 when(applicationDataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // don't use Collections.emptyMap
567 // for BaseOperation.validatePropertyDefaultValue:
568 when(propertyOperation.isPropertyTypeValid(any(), any())).thenReturn(false);
570 Either<List<InputDefinition>, ResponseFormat> result =
571 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
572 assertThat(result.isRight()).isTrue();
573 verify(propertyOperation, times(1)).isPropertyTypeValid(any(), any());
577 public void test_createListInput_fail_addInputsToComponent() throws Exception {
578 ComponentInstListInput createListInputParams = setUpCreateListInputParams();
579 ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();
580 InputDefinition listInput = createListInputParams.getListInput();
582 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
583 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
584 when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
585 when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
586 when(applicationDataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // don't use Collections.emptyMap
587 // for BaseOperation.validatePropertyDefaultValue:
588 when(propertyOperation.isPropertyTypeValid(any(), any())).thenReturn(true);
589 when(propertyOperation.isPropertyInnerTypeValid(any(),any())).thenReturn(new ImmutablePair<>(listInput.getSchemaType(), true));
590 when(propertyOperation.isPropertyDefaultValueValid(any(), any())).thenReturn(true);
591 when(toscaOperationFacadeMock.addInputsToComponent(anyMap(), eq(COMPONENT_ID))).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
593 Either<List<InputDefinition>, ResponseFormat> result =
594 testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
595 assertThat(result.isRight()).isTrue();
596 verify(toscaOperationFacadeMock, times(1)).addInputsToComponent(anyMap(), eq(COMPONENT_ID));
600 public void test_deleteInput_listInput_fail_getComponent() throws Exception {
601 //ComponentInstListInput createListInputParams = setUpCreateListInputParams();
602 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
603 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
604 when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND)).thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
607 testInstance.deleteInput(COMPONENT_ID, USER_ID, LISTINPUT_NAME);
608 } catch (ComponentException e) {
609 assertEquals(ActionStatus.RESOURCE_NOT_FOUND, e.getActionStatus());
610 verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
618 public void test_deleteInput_listInput_fail_validateInput() throws Exception {
619 InputDefinition listInput = setUpListInput();
620 String inputId = COMPONENT_ID + "." + listInput.getName();
621 listInput.setUniqueId(inputId);
622 service.setInputs(Collections.singletonList(listInput));
623 //ComponentInstListInput createListInputParams = setUpCreateListInputParams();
624 final String NONEXIST_INPUT_NAME = "myInput";
626 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
627 .thenReturn(Either.left(service));
630 testInstance.deleteInput(COMPONENT_ID, USER_ID, NONEXIST_INPUT_NAME);
631 } catch (ComponentException e) {
632 assertEquals(ActionStatus.INPUT_IS_NOT_CHILD_OF_COMPONENT, e.getActionStatus());
633 verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
641 public void test_deleteInput_listInput_fail_lockComponent() throws Exception {
642 InputDefinition listInput = setUpListInput();
643 String inputId = COMPONENT_ID + "." + listInput.getName();
644 listInput.setUniqueId(inputId);
645 service.setInputs(Collections.singletonList(listInput));
647 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
648 .thenReturn(Either.left(service));
649 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.NOT_FOUND);
650 when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND, ComponentTypeEnum.SERVICE)).thenReturn(ActionStatus.SERVICE_NOT_FOUND);
653 testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
654 } catch (ComponentException e) {
655 assertEquals(ActionStatus.SERVICE_NOT_FOUND, e.getActionStatus());
656 verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
657 verify(graphLockOperation, times(1)).lockComponent(COMPONENT_ID, NodeTypeEnum.Service);
665 public void test_deleteInput_listInput_fail_deleteInput() throws Exception {
666 InputDefinition listInput = setUpListInput();
667 String inputId = COMPONENT_ID + "." + listInput.getName();
668 listInput.setUniqueId(inputId);
669 service.setInputs(Collections.singletonList(listInput));
671 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
672 .thenReturn(Either.left(service));
673 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
674 when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.BAD_REQUEST);
675 when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.BAD_REQUEST)).thenReturn(ActionStatus.INVALID_CONTENT);
678 testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
679 } catch (ComponentException e) {
680 assertEquals(ActionStatus.INVALID_CONTENT, e.getActionStatus());
681 verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
682 verify(graphLockOperation, times(1)).lockComponent(COMPONENT_ID, NodeTypeEnum.Service);
683 verify(toscaOperationFacadeMock, times(1)).deleteInputOfResource(service, listInput.getName());
691 public void test_deleteInput_listInput_success() throws Exception {
692 InputDefinition listInput = setUpListInput();
693 String inputId = COMPONENT_ID + "." + listInput.getName();
694 listInput.setUniqueId(inputId);
695 listInput.setIsDeclaredListInput(true);
696 service.setInputs(Collections.singletonList(listInput));
697 ArgumentCaptor<String> schemaTypeCaptor = ArgumentCaptor.forClass(String.class);
699 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
700 .thenReturn(Either.left(service));
701 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
702 when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.OK);
703 when(propertyDeclarationOrchestrator.unDeclarePropertiesAsListInputs(service, listInput)).thenReturn(StorageOperationStatus.OK);
704 when(dataTypeBusinessLogic.deletePrivateDataType(eq(service), schemaTypeCaptor.capture()))
705 .thenReturn(Either.left(new DataTypeDefinition()));
707 testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
708 verify(propertyDeclarationOrchestrator, times(1)).unDeclarePropertiesAsListInputs(service, listInput);
709 verify(dataTypeBusinessLogic, times(1)).deletePrivateDataType(service, listInput.getSchemaType());
710 assertEquals(listInput.getSchemaType(), schemaTypeCaptor.getValue());
715 public void test_deleteInput_input_fail_unDeclare() throws Exception {
716 InputDefinition listInput = setUpListInput();
717 String inputId = COMPONENT_ID + "." + listInput.getName();
718 listInput.setUniqueId(inputId);
719 listInput.setIsDeclaredListInput(false);
720 service.setInputs(Collections.singletonList(listInput));
722 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
723 .thenReturn(Either.left(service));
724 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
725 when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.OK);
726 when(propertyDeclarationOrchestrator.unDeclarePropertiesAsInputs(service, listInput)).thenReturn(StorageOperationStatus.BAD_REQUEST);
727 when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.BAD_REQUEST)).thenReturn(ActionStatus.INVALID_CONTENT);
730 testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
731 } catch (ComponentException e) {
732 assertEquals(ActionStatus.INVALID_CONTENT, e.getActionStatus());
733 verify(propertyDeclarationOrchestrator, times(1)).unDeclarePropertiesAsInputs(service, listInput);
741 public void test_deleteInput_input_success() throws Exception {
742 InputDefinition listInput = setUpListInput();
743 String inputId = COMPONENT_ID + "." + listInput.getName();
744 listInput.setUniqueId(inputId);
745 listInput.setIsDeclaredListInput(false);
746 service.setInputs(Collections.singletonList(listInput));
748 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
749 .thenReturn(Either.left(service));
750 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
751 when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.OK);
752 when(propertyDeclarationOrchestrator.unDeclarePropertiesAsInputs(service, listInput)).thenReturn(StorageOperationStatus.OK);
754 testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
755 verify(propertyDeclarationOrchestrator, times(1)).unDeclarePropertiesAsInputs(service, listInput);
760 public void test_updateInputsValue() throws Exception {
761 List<InputDefinition> oldInputDefs = new ArrayList<>();
762 InputDefinition oldInputDef = new InputDefinition();
763 oldInputDef.setUniqueId(INPUT_ID);
764 oldInputDef.setType(INPUT_TYPE);
765 oldInputDef.setDefaultValue(OLD_VALUE);
766 oldInputDef.setRequired(Boolean.FALSE);
767 Map<String, String> oldMetadata = new HashMap<>();
768 oldMetadata.put("key1", "value1");
769 oldInputDef.setMetadata(oldMetadata);
770 oldInputDefs.add(oldInputDef);
771 service.setInputs(oldInputDefs);
773 List<InputDefinition> newInputDefs = new ArrayList<>();
774 InputDefinition inputDef = new InputDefinition();
775 inputDef.setUniqueId(INPUT_ID);
776 inputDef.setType(INPUT_TYPE);
777 inputDef.setDefaultValue(NEW_VALUE); // update value
778 inputDef.setRequired(Boolean.TRUE); // update value
779 Map<String, String> newMetadata = new HashMap<>();
780 newMetadata.put("key2", "value2");
781 newMetadata.put("key3", "value3");
782 inputDef.setMetadata(newMetadata);
783 newInputDefs.add(inputDef);
785 // used in validateComponentExists
786 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
787 // used in lockComponent
788 when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
789 // used in validateInputValueConstraint
790 Map<String, DataTypeDefinition> dataTypeMap = new HashMap<>();
791 when(applicationDataTypeCache.getAll()).thenReturn(Either.left(dataTypeMap)); // don't use Collections.emptyMap
792 // used in updateInputObjectValue
793 when(propertyOperation.validateAndUpdatePropertyValue(INPUT_TYPE, NEW_VALUE, true, null, dataTypeMap))
794 .thenReturn(Either.left(NEW_VALUE));
795 when(toscaOperationFacadeMock.updateInputOfComponent(service, oldInputDef))
796 .thenReturn(Either.left(inputDef));
798 Either<List<InputDefinition>, ResponseFormat> result =
799 testInstance.updateInputsValue(service.getComponentType(), COMPONENT_ID, newInputDefs, USER_ID, true, false);
800 assertThat(result.isLeft()).isTrue();
801 // check if values are updated
802 assertEquals(NEW_VALUE, service.getInputs().get(0).getDefaultValue());
803 assertEquals(Boolean.TRUE, service.getInputs().get(0).isRequired());
804 assertEquals(2, service.getInputs().get(0).getMetadata().size());
805 assertEquals("value2", service.getInputs().get(0).getMetadata().get("key2"));
806 assertEquals("value3", service.getInputs().get(0).getMetadata().get("key3"));