1 package org.openecomp.sdc.be.components.property;
4 import org.junit.Assert;
5 import org.junit.Before;
7 import org.junit.runner.RunWith;
8 import org.mockito.ArgumentCaptor;
9 import org.mockito.Captor;
10 import org.mockito.Mock;
11 import org.mockito.junit.MockitoJUnitRunner;
12 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
13 import org.openecomp.sdc.be.components.utils.AnnotationBuilder;
14 import org.openecomp.sdc.be.components.utils.InputsBuilder;
15 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
16 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
17 import org.openecomp.sdc.be.datatypes.elements.Annotation;
18 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
19 import org.openecomp.sdc.be.model.Component;
20 import org.openecomp.sdc.be.model.ComponentInstanceInput;
21 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
22 import org.openecomp.sdc.be.model.ComponentParametersView;
23 import org.openecomp.sdc.be.model.InputDefinition;
24 import org.openecomp.sdc.be.model.PropertyDefinition;
25 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
26 import org.openecomp.sdc.be.model.operations.StorageException;
27 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
28 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
36 import static org.mockito.ArgumentMatchers.anyMap;
37 import static org.mockito.ArgumentMatchers.eq;
38 import static org.mockito.Mockito.when;
39 import static org.openecomp.sdc.be.MockGenerator.mockComponentUtils;
40 import static org.openecomp.sdc.be.MockGenerator.mockExceptionUtils;
42 @RunWith(MockitoJUnitRunner.class)
43 public class ComponentInstanceInputPropertyDeclaratorTest extends PropertyDeclaratorTestBase {
46 private ComponentInstanceInputPropertyDeclarator testInstance;
49 private ToscaOperationFacade toscaOperationFacade;
52 private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
55 private PropertyOperation propertyOperation;
58 private ArgumentCaptor<ComponentParametersView> inputsFilterCaptor;
60 private Annotation annotation1, annotation2;
64 public void setUp() throws Exception {
66 testInstance = new ComponentInstanceInputPropertyDeclarator(mockComponentUtils(), propertyOperation,
67 toscaOperationFacade, componentInstanceBusinessLogic, mockExceptionUtils());
68 annotation1 = AnnotationBuilder.create()
69 .setType("annotationType1")
70 .setName("annotation1")
75 annotation2 = AnnotationBuilder.create()
76 .setType("annotationType2")
77 .setName("annotation2")
83 public void whenDeclaredPropertyOriginalInputContainsAnnotation_createNewInputWithSameAnnotations() {
84 List<PropertyDataDefinition> properties = Collections.singletonList(prop1);
85 List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
86 Component originInstanceNodeType = createComponentWithInputAndAnnotation(prop1.getName());
87 when(toscaOperationFacade.addComponentInstanceInputsToComponent(eq(resource), anyMap())).thenReturn(Either.left(Collections.emptyMap()));
88 when(toscaOperationFacade.getToscaElement(eq(ORIGIN_INSTANCE_ID), inputsFilterCaptor.capture())).thenReturn(Either.left(originInstanceNodeType));
89 Either<List<InputDefinition>, StorageOperationStatus> createdInputs = testInstance.declarePropertiesAsInputs(resource, "inst1", propsToDeclare);
90 List<InputDefinition> inputs = createdInputs.left().value();
91 assertThat(inputs).hasSize(1);
92 verifyInputAnnotations(inputs.get(0));
93 assertThat(inputsFilterCaptor.getValue().isIgnoreInputs()).isFalse();
97 public void throwExceptionWhenFailingToGetInstanceOriginType() {
98 List<PropertyDataDefinition> properties = Collections.singletonList(prop1);
99 List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
100 when(toscaOperationFacade.getToscaElement(eq(ORIGIN_INSTANCE_ID), inputsFilterCaptor.capture())).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
101 assertThatExceptionOfType(StorageException.class).isThrownBy(() -> testInstance.declarePropertiesAsInputs(resource, "inst1", propsToDeclare));
105 public void unDeclarePropertiesAsListInputsTest() {
106 InputDefinition inputToDelete = new InputDefinition();
107 inputToDelete.setUniqueId(INPUT_ID);
108 inputToDelete.setName(INPUT_ID);
109 inputToDelete.setIsDeclaredListInput(true);
111 Component component = createComponentWithListInput(INPUT_ID, "innerPropName");
112 PropertyDefinition prop = new PropertyDataDefinitionBuilder()
114 .setValue(generateGetInputValueAsListInput(INPUT_ID, "innerPropName"))
116 .setUniqueId("propName")
117 .addGetInputValue(INPUT_ID)
119 component.setProperties(Collections.singletonList(prop));
121 List<ComponentInstanceInput> ciPropList = new ArrayList<>();
122 ComponentInstanceInput ciProp = new ComponentInstanceInput();
123 List<String> pathOfComponentInstances = new ArrayList<>();
124 pathOfComponentInstances.add("pathOfComponentInstances");
125 ciProp.setPath(pathOfComponentInstances);
126 ciProp.setUniqueId("componentInstanceId");
127 ciProp.setDefaultValue("default value");
128 ciProp.setComponentInstanceId("componentInstanceId");
129 ciProp.setComponentInstanceName("componentInstanceName");
130 ciProp.setValue(generateGetInputValueAsListInput(INPUT_ID, "innerPropName"));
131 ciPropList.add(ciProp);
133 when(componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(eq(component), eq(INPUT_ID))).thenReturn(ciPropList);
134 when(propertyOperation.findDefaultValueFromSecondPosition(eq(pathOfComponentInstances), eq(ciProp.getUniqueId()), eq(ciProp.getDefaultValue()))).thenReturn(Either.left(ciProp.getDefaultValue()));
135 when(toscaOperationFacade.updateComponentInstanceInputs(eq(component), eq(ciProp.getComponentInstanceId()), eq(ciPropList))).thenReturn(StorageOperationStatus.OK);
136 StorageOperationStatus storageOperationStatus = testInstance.unDeclarePropertiesAsListInputs(component, inputToDelete);
138 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
142 public void unDeclarePropertiesAsListInputsTest_whenNoListInput_returnOk() {
143 InputDefinition input = new InputDefinition();
144 input.setUniqueId(INPUT_ID);
145 input.setName(INPUT_ID);
146 input.setValue("value");
147 List<ComponentInstanceInput> resList = new ArrayList<>();
148 when(componentInstanceBusinessLogic.getComponentInstanceInputsByInputId(eq(resource), eq(INPUT_ID))).thenReturn(resList);
149 StorageOperationStatus status = testInstance.unDeclarePropertiesAsListInputs(resource, input);
150 Assert.assertEquals(status, StorageOperationStatus.OK);
153 private void verifyInputAnnotations(InputDefinition inputDefinition) {
154 List<Annotation> annotations = inputDefinition.getAnnotations();
155 assertThat(annotations)
156 .containsExactlyInAnyOrder(annotation1, annotation2);
159 private Component createComponentWithInputAndAnnotation(String inputName) {
160 InputDefinition input = InputsBuilder.create()
162 .addAnnotation(annotation1)
163 .addAnnotation(annotation2)
165 return new ResourceBuilder()
170 private Component createComponentWithListInput(String inputName, String propName) {
171 InputDefinition input = InputsBuilder.create()
175 input.setUniqueId(INPUT_ID);
176 input.setName(INPUT_ID);
177 input.setDefaultValue("defaultValue");
178 input.setValue(generateGetInputValueAsListInput(inputName, propName));
180 return new ResourceBuilder()
181 .setUniqueId(RESOURCE_ID)