Change to enable SDC list type input
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / property / ComponentInstanceInputPropertyDeclaratorTest.java
1 package org.openecomp.sdc.be.components.property;
2
3 import fj.data.Either;
4 import org.junit.Assert;
5 import org.junit.Before;
6 import org.junit.Test;
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;
29
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
33
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;
41
42 @RunWith(MockitoJUnitRunner.class)
43 public class ComponentInstanceInputPropertyDeclaratorTest extends PropertyDeclaratorTestBase {
44
45
46     private ComponentInstanceInputPropertyDeclarator testInstance;
47
48     @Mock
49     private ToscaOperationFacade toscaOperationFacade;
50
51     @Mock
52     private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
53
54     @Mock
55     private PropertyOperation propertyOperation;
56
57     @Captor
58     private ArgumentCaptor<ComponentParametersView> inputsFilterCaptor;
59
60     private Annotation annotation1, annotation2;
61
62     @Override
63     @Before
64     public void setUp() throws Exception {
65         super.setUp();
66         testInstance = new ComponentInstanceInputPropertyDeclarator(mockComponentUtils(), propertyOperation,
67                 toscaOperationFacade, componentInstanceBusinessLogic, mockExceptionUtils());
68         annotation1 =  AnnotationBuilder.create()
69                 .setType("annotationType1")
70                 .setName("annotation1")
71                 .addProperty("prop1")
72                 .addProperty("prop2")
73                 .build();
74
75         annotation2 =  AnnotationBuilder.create()
76                 .setType("annotationType2")
77                 .setName("annotation2")
78                 .addProperty("prop3")
79                 .build();
80     }
81
82     @Test
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();
94     }
95
96     @Test
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));
102     }
103
104     @Test
105     public void unDeclarePropertiesAsListInputsTest() {
106         InputDefinition inputToDelete = new InputDefinition();
107         inputToDelete.setUniqueId(INPUT_ID);
108         inputToDelete.setName(INPUT_ID);
109         inputToDelete.setIsDeclaredListInput(true);
110
111         Component component = createComponentWithListInput(INPUT_ID, "innerPropName");
112         PropertyDefinition prop = new PropertyDataDefinitionBuilder()
113                 .setName("propName")
114                 .setValue(generateGetInputValueAsListInput(INPUT_ID, "innerPropName"))
115                 .setType("list")
116                 .setUniqueId("propName")
117                 .addGetInputValue(INPUT_ID)
118                 .build();
119         component.setProperties(Collections.singletonList(prop));
120
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);
132
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);
137
138         assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
139     }
140
141     @Test
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);
151     }
152
153     private void verifyInputAnnotations(InputDefinition inputDefinition) {
154         List<Annotation> annotations = inputDefinition.getAnnotations();
155         assertThat(annotations)
156                 .containsExactlyInAnyOrder(annotation1, annotation2);
157     }
158
159     private Component createComponentWithInputAndAnnotation(String inputName) {
160         InputDefinition input = InputsBuilder.create()
161                 .setName(inputName)
162                 .addAnnotation(annotation1)
163                 .addAnnotation(annotation2)
164                 .build();
165         return new ResourceBuilder()
166                 .addInput(input)
167                 .build();
168     }
169
170     private Component createComponentWithListInput(String inputName, String propName) {
171         InputDefinition input = InputsBuilder.create()
172                 .setName(inputName)
173                 .build();
174
175         input.setUniqueId(INPUT_ID);
176         input.setName(INPUT_ID);
177         input.setDefaultValue("defaultValue");
178         input.setValue(generateGetInputValueAsListInput(inputName, propName));
179
180         return new ResourceBuilder()
181                 .setUniqueId(RESOURCE_ID)
182                 .addInput(input)
183                 .build();
184     }
185 }