Refactoring Consolidation Service
[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.Test;
5 import org.junit.runner.RunWith;
6 import org.mockito.ArgumentCaptor;
7 import org.mockito.Captor;
8 import org.mockito.InjectMocks;
9 import org.mockito.Mock;
10 import org.mockito.junit.MockitoJUnitRunner;
11 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
12 import org.openecomp.sdc.be.dao.utils.MapUtil;
13 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
14 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
15 import org.openecomp.sdc.be.model.*;
16 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
17 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
18 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
19
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.stream.Collectors;
25 import java.util.stream.Stream;
26
27 import static org.assertj.core.api.Assertions.assertThat;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.verifyZeroInteractions;
30 import static org.mockito.Mockito.when;
31
32
33 @RunWith(MockitoJUnitRunner.class)
34 public class ComponentInstancePropertyDeclaratorTest extends PropertyDeclaratorTestBase {
35
36     @InjectMocks
37     private ComponentInstancePropertyDeclarator testInstance;
38     @Mock
39     private ToscaOperationFacade toscaOperationFacade;
40     @Captor
41     private ArgumentCaptor<Map<String, List<ComponentInstanceProperty>>> instancePropertiesCaptor;
42
43     @Test
44     public void declarePropertiesAsInputs_componentInstanceNotExist() {
45         Component cmpt = new Resource();
46         Either<List<InputDefinition>, StorageOperationStatus> createdInputs = testInstance.declarePropertiesAsInputs(cmpt, "someCmptInstId", Collections.emptyList());
47         assertThat(createdInputs.right().value()).isEqualTo(StorageOperationStatus.NOT_FOUND);
48         verifyZeroInteractions(toscaOperationFacade);
49     }
50
51     @Test
52     public void declarePropertiesAsInputs_singleNonComplexProperty() {
53         List<PropertyDataDefinition> properties = Collections.singletonList(prop1);
54         List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
55         when(toscaOperationFacade.addComponentInstancePropertiesToComponent(eq(resource), instancePropertiesCaptor.capture())).thenReturn(Either.left(Collections.emptyMap()));
56         Either<List<InputDefinition>, StorageOperationStatus> createdInputs = testInstance.declarePropertiesAsInputs(resource, "inst1", propsToDeclare);
57         List<InputDefinition> inputs = createdInputs.left().value();
58         List<ComponentInstanceProperty> capturedInstanceProperties = instancePropertiesCaptor.getValue().get(INSTANCE_ID);
59         verifyCreatedInputs(properties, capturedInstanceProperties, inputs);
60         verifyUpdatedProperties(properties, capturedInstanceProperties, inputs);
61     }
62
63     @Test
64     public void declarePropertiesAsInputs_multipleNonComplexProperty() {
65         List<PropertyDataDefinition> properties = Arrays.asList(prop1, prop2);
66         List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
67         when(toscaOperationFacade.addComponentInstancePropertiesToComponent(eq(resource), instancePropertiesCaptor.capture())).thenReturn(Either.left(Collections.emptyMap()));
68         Either<List<InputDefinition>, StorageOperationStatus> createdInputs = testInstance.declarePropertiesAsInputs(resource, "inst1", propsToDeclare);
69
70         List<InputDefinition> inputs = createdInputs.left().value();
71         List<ComponentInstanceProperty> capturedInstanceProperties = instancePropertiesCaptor.getValue().get(INSTANCE_ID);
72         verifyCreatedInputs(properties, capturedInstanceProperties, inputs);
73         verifyUpdatedProperties(properties, capturedInstanceProperties, inputs);
74     }
75
76     @Test
77     public void declarePropertiesAsInputs_singleComplexProperty() {
78         PropertyDefinition innerProp1 = new PropertyDataDefinitionBuilder()
79                 .setName(INNER_PROP1)
80                 .setValue("true")
81                 .setType("boolean")
82                 .setUniqueId(complexProperty.getType() + ".datatype.ecomp_generated_naming")
83                 .build();
84         PropertyDefinition innerProp2 = new PropertyDataDefinitionBuilder()
85                 .setName(INNER_PROP2)
86                 .setValue("abc")
87                 .setType("string")
88                 .setUniqueId(complexProperty.getType() + ".datatype.ecomp_generated_naming")
89                 .build();
90         List<ComponentInstancePropInput> propsToDeclare = createComplexPropInputList(innerProp1, innerProp2);
91         when(toscaOperationFacade.addComponentInstancePropertiesToComponent(eq(resource), instancePropertiesCaptor.capture())).thenReturn(Either.left(Collections.emptyMap()));
92         Either<List<InputDefinition>, StorageOperationStatus> createdInputs = testInstance.declarePropertiesAsInputs(resource, "inst1", propsToDeclare);
93
94         List<InputDefinition> inputs = createdInputs.left().value();
95         List<ComponentInstanceProperty> capturedInstanceProperties = instancePropertiesCaptor.getValue().get(INSTANCE_ID);
96
97         verifyCreatedInputsFromComplexProperty(propsToDeclare, capturedInstanceProperties, inputs);
98         verifyUpdatedComplexProperty(capturedInstanceProperties, inputs);
99     }
100
101     private void verifyUpdatedProperties(List<PropertyDataDefinition> properties, List<ComponentInstanceProperty> capturedInstanceProperties, List<InputDefinition> inputs) {
102         assertThat(capturedInstanceProperties).hasSize(properties.size());
103         Map<String, ComponentInstanceProperty> updatedPropertiesByName = MapUtil.toMap(capturedInstanceProperties, ComponentInstanceProperty::getName);
104         properties.forEach(prop -> verifyUpdatedInstanceProperty(prop, updatedPropertiesByName.get(prop.getName()), inputs));
105     }
106
107     private void verifyUpdatedComplexProperty(List<ComponentInstanceProperty> capturedInstanceProperties, List<InputDefinition> inputs) {
108         assertThat(capturedInstanceProperties).hasSize(1);
109         verifyUpdatedInstanceComplexProperty(capturedInstanceProperties.get(0), inputs);
110     }
111
112     private void verifyCreatedInputs(List<PropertyDataDefinition> originalPropsToDeclare, List<ComponentInstanceProperty> capturedUpdatedProperties, List<InputDefinition> inputs) {
113         assertThat(inputs).hasSize(originalPropsToDeclare.size());
114         Map<String, InputDefinition> propertyIdToCreatedInput = MapUtil.toMap(inputs, InputDefinition::getPropertyId);
115         originalPropsToDeclare.forEach(propToDeclare -> verifyCreatedInput(propToDeclare, propertyIdToCreatedInput.get(propToDeclare.getUniqueId())));
116         capturedUpdatedProperties.forEach(updatedProperty -> verifyInputPropertiesList(updatedProperty, propertyIdToCreatedInput.get(updatedProperty.getUniqueId())));
117     }
118
119     private void verifyCreatedInputsFromComplexProperty(List<ComponentInstancePropInput> propsToDeclare, List<ComponentInstanceProperty> capturedInstanceProperties, List<InputDefinition> inputs) {
120         assertThat(inputs).hasSize(propsToDeclare.size());
121         Map<String, InputDefinition> inputsByName = MapUtil.toMap(inputs, InputDefinition::getName);
122         propsToDeclare.forEach(propToDeclare -> verifyCreatedInputFromComplexProperty(propToDeclare, inputsByName));
123         Map<String, List<InputDefinition>> propertyIdToCreatedInput = MapUtil.groupListBy(inputs, InputDefinition::getPropertyId);
124         capturedInstanceProperties.forEach(updatedProperty -> verifyInputPropertiesListFromComplexProperty(updatedProperty, propertyIdToCreatedInput.get(updatedProperty.getUniqueId())));
125     }
126
127     private void verifyInputPropertiesListFromComplexProperty(ComponentInstanceProperty updatedProperty, List<InputDefinition> inputs) {
128         inputs.forEach(input -> verifyInputPropertiesList(updatedProperty, input));
129     }
130
131     private void verifyCreatedInputFromComplexProperty(ComponentInstancePropInput parentProperty,  Map<String, InputDefinition> inputsByName) {
132         PropertyDefinition innerProperty = parentProperty.getInput();
133         String expectedInputName = generateExpectedInputName(parentProperty, innerProperty);
134         InputDefinition input = inputsByName.get(expectedInputName);
135         assertThat(input.getType()).isEqualTo(innerProperty.getType());
136         assertThat(input.getValue()).isEqualTo(null);
137 //        assertThat(input.getDefaultValue()).isEqualTo(innerProperty.getValue());//bug
138         assertThat(input.getUniqueId()).isEqualTo(UniqueIdBuilder.buildPropertyUniqueId(RESOURCE_ID, input.getName()));
139         assertThat(input.getPropertyId()).isEqualTo(parentProperty.getUniqueId());
140         assertThat(input.getInstanceUniqueId()).isEqualTo(INSTANCE_ID);
141
142     }
143
144     private void verifyInputPropertiesList(ComponentInstanceProperty updatedProperty, InputDefinition input) {
145         assertThat(input.getProperties()).hasSize(1);
146         assertThat(updatedProperty).isEqualTo(input.getProperties().get(0));
147     }
148
149
150     private List<ComponentInstancePropInput> createComplexPropInputList(PropertyDefinition ... innerProperties) {
151         return Stream.of(innerProperties).map(this::createComplexPropInput).collect(Collectors.toList());
152     }
153
154     private ComponentInstancePropInput createComplexPropInput(PropertyDefinition innerProp) {
155         ComponentInstancePropInput componentInstancePropInput = new ComponentInstancePropInput(new ComponentInstanceProperty(complexProperty));
156         componentInstancePropInput.setInput(innerProp);
157         componentInstancePropInput.setPropertiesName(complexProperty.getName() + "#" +  innerProp.getName());
158         return componentInstancePropInput;
159     }
160
161     private void verifyUpdatedInstanceProperty(PropertyDataDefinition originalProperty, ComponentInstanceProperty updatedProperty, List<InputDefinition> inputs) {
162         assertThat(updatedProperty.getValue()).isEqualTo(generateGetInputValue(generateExpectedInputName(originalProperty)));
163         assertThat(updatedProperty.isGetInputProperty()).isTrue();
164         assertThat(updatedProperty.getName()).isEqualTo(originalProperty.getName());
165         List<GetInputValueDataDefinition> getInputValues = updatedProperty.getGetInputValues();
166         verifyGetInputValues(getInputValues, inputs);
167     }
168
169     private void verifyUpdatedInstanceComplexProperty(ComponentInstanceProperty updatedComplexProperty, List<InputDefinition> inputs) {
170         assertThat(updatedComplexProperty.getValue()).isEqualTo(generateComplexGetInputValue(inputs));
171         assertThat(updatedComplexProperty.isGetInputProperty()).isTrue();
172         assertThat(updatedComplexProperty.getName()).isEqualTo(complexProperty.getName());
173         List<GetInputValueDataDefinition> getInputValues = updatedComplexProperty.getGetInputValues();
174         verifyGetInputValues(getInputValues, inputs);
175     }
176
177     private void verifyGetInputValues(List<GetInputValueDataDefinition> getInputValues, List<InputDefinition> inputs) {
178         Map<String, InputDefinition> inputsByName = MapUtil.toMap(inputs, InputDefinition::getName);
179         getInputValues.forEach(getInputVal -> {
180             InputDefinition input = inputsByName.get(getInputVal.getInputName());
181             assertThat(input.getUniqueId()).isEqualTo(getInputVal.getInputId());
182         });
183     }
184
185     private String generateComplexGetInputValue(List<InputDefinition> createdInputs) {
186         return String.format("{\"%s\":%s,\"%s\":%s}", INNER_PROP1, generateGetInputValue(createdInputs.get(0).getName()), INNER_PROP2, generateGetInputValue(createdInputs.get(1).getName()));
187     }
188
189     private String generateExpectedInputName(PropertyDataDefinition prop) {
190         return INSTANCE_ID + "_" + prop.getName();
191     }
192
193     private String generateExpectedInputName(PropertyDefinition parentProp, PropertyDefinition innerProperty) {
194         return INSTANCE_ID + "_" + parentProp.getName()+ "_" + innerProperty.getName();
195     }
196
197     private void verifyCreatedInput(PropertyDataDefinition property, InputDefinition input) {
198         assertThat(input.getType()).isEqualTo(property.getType());
199         assertThat(input.getName()).isEqualTo(generateExpectedInputName(property));
200         assertThat(input.getValue()).isEqualTo(null);
201         assertThat(input.getDefaultValue()).isEqualTo(property.getValue());
202         assertThat(input.getUniqueId()).isEqualTo(UniqueIdBuilder.buildPropertyUniqueId(RESOURCE_ID, input.getName()));
203         assertThat(input.getPropertyId()).isEqualTo(property.getUniqueId());
204         assertThat(input.getInstanceUniqueId()).isEqualTo(INSTANCE_ID);
205     }
206
207 }