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