fd61133714efcb765d85fe7181c435c387785f1f
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / property / ComponentInstancePropertyDeclaratorTest.java
1 package org.openecomp.sdc.be.components.property;
2
3 import fj.data.Either;
4 import org.junit.Assert;
5 import org.junit.Test;
6 import org.junit.runner.RunWith;
7 import org.mockito.ArgumentCaptor;
8 import org.mockito.Captor;
9 import org.mockito.InjectMocks;
10 import org.mockito.Mock;
11 import org.mockito.junit.MockitoJUnitRunner;
12 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
13 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
14 import org.openecomp.sdc.be.dao.utils.MapUtil;
15 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
16 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
17 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
18 import org.openecomp.sdc.be.model.*;
19 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
20 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
21 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
22
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.stream.Collectors;
30 import java.util.stream.Stream;
31
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.mockito.ArgumentMatchers.any;
34 import static org.mockito.ArgumentMatchers.eq;
35 import static org.mockito.Mockito.verifyZeroInteractions;
36 import static org.mockito.Mockito.when;
37 import static org.openecomp.sdc.be.components.property.CapabilityTestUtils.createCapabilityDefinition;
38 import static org.openecomp.sdc.be.components.property.CapabilityTestUtils.createProperties;
39
40
41 @RunWith(MockitoJUnitRunner.class)
42 public class ComponentInstancePropertyDeclaratorTest extends PropertyDeclaratorTestBase {
43
44     @InjectMocks
45     private ComponentInstancePropertyDeclarator testInstance;
46     @Mock
47     private ToscaOperationFacade toscaOperationFacade;
48     @Captor
49     private ArgumentCaptor<Map<String, List<ComponentInstanceProperty>>> instancePropertiesCaptor;
50
51     @Test
52     public void declarePropertiesAsInputs_componentInstanceNotExist() {
53         Component cmpt = new Resource();
54         Either<List<InputDefinition>, StorageOperationStatus> createdInputs = testInstance.declarePropertiesAsInputs(cmpt, "someCmptInstId", Collections.emptyList());
55         assertThat(createdInputs.right().value()).isEqualTo(StorageOperationStatus.NOT_FOUND);
56         verifyZeroInteractions(toscaOperationFacade);
57     }
58
59     @Test
60     public void declarePropertiesAsInputs_singleNonComplexProperty() {
61         List<PropertyDataDefinition> properties = Collections.singletonList(prop1);
62         List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
63         when(toscaOperationFacade.addComponentInstancePropertiesToComponent(eq(resource), instancePropertiesCaptor.capture())).thenReturn(Either.left(Collections.emptyMap()));
64         Either<List<InputDefinition>, StorageOperationStatus> createdInputs = testInstance.declarePropertiesAsInputs(resource, "inst1", propsToDeclare);
65         List<InputDefinition> inputs = createdInputs.left().value();
66         List<ComponentInstanceProperty> capturedInstanceProperties = instancePropertiesCaptor.getValue().get(INSTANCE_ID);
67         verifyCreatedInputs(properties, capturedInstanceProperties, inputs);
68         verifyUpdatedProperties(properties, capturedInstanceProperties, inputs);
69     }
70
71     @Test
72     public void declareCapabilitiesPropertiesAsInputs() {
73         prop1.setParentUniqueId("capUniqueId");
74         List<PropertyDataDefinition> properties = Collections.singletonList(prop1);
75         List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
76         when(toscaOperationFacade.addComponentInstancePropertiesToComponent(eq(resource), instancePropertiesCaptor
77                 .capture())).thenReturn(Either.left(Collections.emptyMap()));
78
79         CapabilityDefinition capabilityDefinition = createCapabilityDefinition();
80
81         List<ComponentInstanceProperty> capPropList = new ArrayList<>();
82         ComponentInstanceProperty instanceProperty = createProperties();
83         capPropList.add(instanceProperty);
84         capabilityDefinition.setProperties(capPropList);
85
86         capabilityDefinition.setPath(Collections.singletonList("path"));
87         Map<String, List<CapabilityDefinition>> capabilityMap = new HashMap<>();
88         capabilityMap.put(capabilityDefinition.getType(), Collections.singletonList(capabilityDefinition));
89         resource.setCapabilities(capabilityMap);
90
91         Either<List<InputDefinition>, StorageOperationStatus> createdInputs = testInstance
92                 .declarePropertiesAsInputs(resource, "inst1", propsToDeclare);
93         Assert.assertTrue(createdInputs.isLeft());
94     }
95
96     @Test
97     public void testUnDeclarePropertiesAsInputs() throws Exception {
98         Component component = new ResourceBuilder().setComponentType(ComponentTypeEnum.RESOURCE).setUniqueId("resourceId")
99                 .setName("resourceName").build();
100         InputDefinition input = new InputDefinition();
101         input.setUniqueId("ComponentInput1_uniqueId");
102         input.setPropertyId("ComponentInput1_uniqueId");
103
104         CapabilityDefinition capabilityDefinition = createCapabilityDefinition();
105
106         List<ComponentInstanceProperty> properties = new ArrayList<>();
107         ComponentInstanceProperty instanceProperty = createProperties();
108
109         List<GetInputValueDataDefinition> valueDataDefinitionList = new ArrayList<>();
110         GetInputValueDataDefinition getInputValueDataDefinition = new GetInputValueDataDefinition();
111         getInputValueDataDefinition.setInputId("ComponentInput1_uniqueId");
112         getInputValueDataDefinition.setPropName("prop_name");
113         valueDataDefinitionList.add(getInputValueDataDefinition);
114
115         instanceProperty.setGetInputValues(valueDataDefinitionList);
116         properties.add(instanceProperty);
117         capabilityDefinition.setProperties(properties);
118         Map<String, List<CapabilityDefinition>> capabilityMap = new HashMap<>();
119         capabilityMap.put(capabilityDefinition.getType(), Collections.singletonList(capabilityDefinition));
120         component.setCapabilities(capabilityMap);
121         component.setInputs(Collections.singletonList(input));
122         when(toscaOperationFacade.updateInstanceCapabilityProperty(any(Resource.class), any(),
123                 any(ComponentInstanceProperty.class), any(CapabilityDefinition.class))).thenReturn(StorageOperationStatus.OK);
124
125         StorageOperationStatus result = testInstance.unDeclarePropertiesAsInputs(component, input);
126         Assert.assertEquals(StorageOperationStatus.OK, result);
127     }
128
129     @Test
130     public void declarePropertiesAsInputs_multipleNonComplexProperty() {
131         List<PropertyDataDefinition> properties = Arrays.asList(prop1, prop2);
132         List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
133         when(toscaOperationFacade.addComponentInstancePropertiesToComponent(eq(resource), instancePropertiesCaptor.capture())).thenReturn(Either.left(Collections.emptyMap()));
134         Either<List<InputDefinition>, StorageOperationStatus> createdInputs = testInstance.declarePropertiesAsInputs(resource, "inst1", propsToDeclare);
135
136         List<InputDefinition> inputs = createdInputs.left().value();
137         List<ComponentInstanceProperty> capturedInstanceProperties = instancePropertiesCaptor.getValue().get(INSTANCE_ID);
138         verifyCreatedInputs(properties, capturedInstanceProperties, inputs);
139         verifyUpdatedProperties(properties, capturedInstanceProperties, inputs);
140     }
141
142     @Test
143     public void declarePropertiesAsInputs_singleComplexProperty() {
144         PropertyDefinition innerProp1 = new PropertyDataDefinitionBuilder()
145                 .setName(INNER_PROP1)
146                 .setValue("true")
147                 .setType("boolean")
148                 .setUniqueId(complexProperty.getType() + ".datatype.ecomp_generated_naming")
149                 .build();
150         PropertyDefinition innerProp2 = new PropertyDataDefinitionBuilder()
151                 .setName(INNER_PROP2)
152                 .setValue("abc")
153                 .setType("string")
154                 .setUniqueId(complexProperty.getType() + ".datatype.ecomp_generated_naming")
155                 .build();
156         List<ComponentInstancePropInput> propsToDeclare = createComplexPropInputList(innerProp1, innerProp2);
157         when(toscaOperationFacade.addComponentInstancePropertiesToComponent(eq(resource), instancePropertiesCaptor.capture())).thenReturn(Either.left(Collections.emptyMap()));
158         Either<List<InputDefinition>, StorageOperationStatus> createdInputs = testInstance.declarePropertiesAsInputs(resource, "inst1", propsToDeclare);
159
160         List<InputDefinition> inputs = createdInputs.left().value();
161         List<ComponentInstanceProperty> capturedInstanceProperties = instancePropertiesCaptor.getValue().get(INSTANCE_ID);
162
163         verifyCreatedInputsFromComplexProperty(propsToDeclare, capturedInstanceProperties, inputs);
164         verifyUpdatedComplexProperty(capturedInstanceProperties, inputs);
165     }
166
167     private void verifyUpdatedProperties(List<PropertyDataDefinition> properties, List<ComponentInstanceProperty> capturedInstanceProperties, List<InputDefinition> inputs) {
168         assertThat(capturedInstanceProperties).hasSize(properties.size());
169         Map<String, ComponentInstanceProperty> updatedPropertiesByName = MapUtil.toMap(capturedInstanceProperties, ComponentInstanceProperty::getName);
170         properties.forEach(prop -> verifyUpdatedInstanceProperty(prop, updatedPropertiesByName.get(prop.getName()), inputs));
171     }
172
173     private void verifyUpdatedComplexProperty(List<ComponentInstanceProperty> capturedInstanceProperties, List<InputDefinition> inputs) {
174         assertThat(capturedInstanceProperties).hasSize(1);
175         verifyUpdatedInstanceComplexProperty(capturedInstanceProperties.get(0), inputs);
176     }
177
178     private void verifyCreatedInputs(List<PropertyDataDefinition> originalPropsToDeclare, List<ComponentInstanceProperty> capturedUpdatedProperties, List<InputDefinition> inputs) {
179         assertThat(inputs).hasSize(originalPropsToDeclare.size());
180         Map<String, InputDefinition> propertyIdToCreatedInput = MapUtil.toMap(inputs, InputDefinition::getPropertyId);
181         originalPropsToDeclare.forEach(propToDeclare -> verifyCreatedInput(propToDeclare, propertyIdToCreatedInput.get(propToDeclare.getUniqueId())));
182         capturedUpdatedProperties.forEach(updatedProperty -> verifyInputPropertiesList(updatedProperty, propertyIdToCreatedInput.get(updatedProperty.getUniqueId())));
183     }
184
185     private void verifyCreatedInputsFromComplexProperty(List<ComponentInstancePropInput> propsToDeclare, List<ComponentInstanceProperty> capturedInstanceProperties, List<InputDefinition> inputs) {
186         assertThat(inputs).hasSize(propsToDeclare.size());
187         Map<String, InputDefinition> inputsByName = MapUtil.toMap(inputs, InputDefinition::getName);
188         propsToDeclare.forEach(propToDeclare -> verifyCreatedInputFromComplexProperty(propToDeclare, inputsByName));
189         Map<String, List<InputDefinition>> propertyIdToCreatedInput = MapUtil.groupListBy(inputs, InputDefinition::getPropertyId);
190         capturedInstanceProperties.forEach(updatedProperty -> verifyInputPropertiesListFromComplexProperty(updatedProperty, propertyIdToCreatedInput.get(updatedProperty.getUniqueId())));
191     }
192
193     private void verifyInputPropertiesListFromComplexProperty(ComponentInstanceProperty updatedProperty, List<InputDefinition> inputs) {
194         inputs.forEach(input -> verifyInputPropertiesList(updatedProperty, input));
195     }
196
197     private void verifyCreatedInputFromComplexProperty(ComponentInstancePropInput parentProperty,  Map<String, InputDefinition> inputsByName) {
198         PropertyDefinition innerProperty = parentProperty.getInput();
199         String expectedInputName = generateExpectedInputName(parentProperty, innerProperty);
200         InputDefinition input = inputsByName.get(expectedInputName);
201         assertThat(input.getType()).isEqualTo(innerProperty.getType());
202         assertThat(input.getValue()).isEqualTo(innerProperty.getValue());
203 //        assertThat(input.getDefaultValue()).isEqualTo(innerProperty.getValue());//bug
204         assertThat(input.getUniqueId()).isEqualTo(UniqueIdBuilder.buildPropertyUniqueId(RESOURCE_ID, input.getName()));
205         assertThat(input.getPropertyId()).isEqualTo(parentProperty.getUniqueId());
206         assertThat(input.getInstanceUniqueId()).isEqualTo(INSTANCE_ID);
207
208     }
209
210     private void verifyInputPropertiesList(ComponentInstanceProperty updatedProperty, InputDefinition input) {
211         assertThat(input.getProperties()).hasSize(1);
212         assertThat(updatedProperty).isEqualTo(input.getProperties().get(0));
213     }
214
215
216     private List<ComponentInstancePropInput> createComplexPropInputList(PropertyDefinition ... innerProperties) {
217         return Stream.of(innerProperties).map(this::createComplexPropInput).collect(Collectors.toList());
218     }
219
220     private ComponentInstancePropInput createComplexPropInput(PropertyDefinition innerProp) {
221         ComponentInstancePropInput componentInstancePropInput = new ComponentInstancePropInput(new ComponentInstanceProperty(complexProperty));
222         componentInstancePropInput.setInput(innerProp);
223         componentInstancePropInput.setPropertiesName(complexProperty.getName() + "#" +  innerProp.getName());
224         return componentInstancePropInput;
225     }
226
227     private void verifyUpdatedInstanceProperty(PropertyDataDefinition originalProperty, ComponentInstanceProperty updatedProperty, List<InputDefinition> inputs) {
228         assertThat(updatedProperty.getValue()).isEqualTo(generateGetInputValue(generateExpectedInputName(originalProperty)));
229         assertThat(updatedProperty.isGetInputProperty()).isTrue();
230         assertThat(updatedProperty.getName()).isEqualTo(originalProperty.getName());
231         List<GetInputValueDataDefinition> getInputValues = updatedProperty.getGetInputValues();
232         verifyGetInputValues(getInputValues, inputs);
233     }
234
235     private void verifyUpdatedInstanceComplexProperty(ComponentInstanceProperty updatedComplexProperty, List<InputDefinition> inputs) {
236         assertThat(updatedComplexProperty.getValue()).isEqualTo(generateComplexGetInputValue(inputs));
237         assertThat(updatedComplexProperty.isGetInputProperty()).isTrue();
238         assertThat(updatedComplexProperty.getName()).isEqualTo(complexProperty.getName());
239         List<GetInputValueDataDefinition> getInputValues = updatedComplexProperty.getGetInputValues();
240         verifyGetInputValues(getInputValues, inputs);
241     }
242
243     private void verifyGetInputValues(List<GetInputValueDataDefinition> getInputValues, List<InputDefinition> inputs) {
244         Map<String, InputDefinition> inputsByName = MapUtil.toMap(inputs, InputDefinition::getName);
245         getInputValues.forEach(getInputVal -> {
246             InputDefinition input = inputsByName.get(getInputVal.getInputName());
247             assertThat(input.getUniqueId()).isEqualTo(getInputVal.getInputId());
248         });
249     }
250
251     private String generateComplexGetInputValue(List<InputDefinition> createdInputs) {
252         return String.format("{\"%s\":%s,\"%s\":%s}", INNER_PROP1, generateGetInputValue(createdInputs.get(0).getName()), INNER_PROP2, generateGetInputValue(createdInputs.get(1).getName()));
253     }
254
255     private String generateExpectedInputName(PropertyDataDefinition prop) {
256         return INSTANCE_ID + "_" + prop.getName();
257     }
258
259     private String generateExpectedInputName(PropertyDefinition parentProp, PropertyDefinition innerProperty) {
260         return INSTANCE_ID + "_" + parentProp.getName()+ "_" + innerProperty.getName();
261     }
262
263     private void verifyCreatedInput(PropertyDataDefinition property, InputDefinition input) {
264         assertThat(input.getType()).isEqualTo(property.getType());
265         assertThat(input.getName()).isEqualTo(generateExpectedInputName(property));
266         assertThat(input.getValue()).isEqualTo(property.getValue());
267         assertThat(input.getDefaultValue()).isEqualTo(property.getValue());
268         assertThat(input.getUniqueId()).isEqualTo(UniqueIdBuilder.buildPropertyUniqueId(RESOURCE_ID, input.getName()));
269         assertThat(input.getPropertyId()).isEqualTo(property.getUniqueId());
270         assertThat(input.getInstanceUniqueId()).isEqualTo(INSTANCE_ID);
271     }
272
273 }