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