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