1 package org.openecomp.sdc.be.components.property;
4 import org.junit.Assert;
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;
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;
29 import java.util.stream.Collectors;
30 import java.util.stream.Stream;
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;
41 @RunWith(MockitoJUnitRunner.class)
42 public class ComponentInstancePropertyDeclaratorTest extends PropertyDeclaratorTestBase {
45 private ComponentInstancePropertyDeclarator testInstance;
47 private ToscaOperationFacade toscaOperationFacade;
49 private ArgumentCaptor<Map<String, List<ComponentInstanceProperty>>> instancePropertiesCaptor;
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);
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);
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()));
79 CapabilityDefinition capabilityDefinition = createCapabilityDefinition();
81 List<ComponentInstanceProperty> capPropList = new ArrayList<>();
82 ComponentInstanceProperty instanceProperty = createProperties();
83 capPropList.add(instanceProperty);
84 capabilityDefinition.setProperties(capPropList);
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);
91 Either<List<InputDefinition>, StorageOperationStatus> createdInputs = testInstance
92 .declarePropertiesAsInputs(resource, "inst1", propsToDeclare);
93 Assert.assertTrue(createdInputs.isLeft());
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");
104 CapabilityDefinition capabilityDefinition = createCapabilityDefinition();
106 List<ComponentInstanceProperty> properties = new ArrayList<>();
107 ComponentInstanceProperty instanceProperty = createProperties();
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);
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);
125 StorageOperationStatus result = testInstance.unDeclarePropertiesAsInputs(component, input);
126 Assert.assertEquals(StorageOperationStatus.OK, result);
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);
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);
143 public void declarePropertiesAsInputs_singleComplexProperty() {
144 PropertyDefinition innerProp1 = new PropertyDataDefinitionBuilder()
145 .setName(INNER_PROP1)
148 .setUniqueId(complexProperty.getType() + ".datatype.ecomp_generated_naming")
150 PropertyDefinition innerProp2 = new PropertyDataDefinitionBuilder()
151 .setName(INNER_PROP2)
154 .setUniqueId(complexProperty.getType() + ".datatype.ecomp_generated_naming")
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);
160 List<InputDefinition> inputs = createdInputs.left().value();
161 List<ComponentInstanceProperty> capturedInstanceProperties = instancePropertiesCaptor.getValue().get(INSTANCE_ID);
163 verifyCreatedInputsFromComplexProperty(propsToDeclare, capturedInstanceProperties, inputs);
164 verifyUpdatedComplexProperty(capturedInstanceProperties, inputs);
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));
173 private void verifyUpdatedComplexProperty(List<ComponentInstanceProperty> capturedInstanceProperties, List<InputDefinition> inputs) {
174 assertThat(capturedInstanceProperties).hasSize(1);
175 verifyUpdatedInstanceComplexProperty(capturedInstanceProperties.get(0), inputs);
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())));
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())));
193 private void verifyInputPropertiesListFromComplexProperty(ComponentInstanceProperty updatedProperty, List<InputDefinition> inputs) {
194 inputs.forEach(input -> verifyInputPropertiesList(updatedProperty, input));
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);
210 private void verifyInputPropertiesList(ComponentInstanceProperty updatedProperty, InputDefinition input) {
211 assertThat(input.getProperties()).hasSize(1);
212 assertThat(updatedProperty).isEqualTo(input.getProperties().get(0));
216 private List<ComponentInstancePropInput> createComplexPropInputList(PropertyDefinition ... innerProperties) {
217 return Stream.of(innerProperties).map(this::createComplexPropInput).collect(Collectors.toList());
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;
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);
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);
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());
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()));
255 private String generateExpectedInputName(PropertyDataDefinition prop) {
256 return INSTANCE_ID + "_" + prop.getName();
259 private String generateExpectedInputName(PropertyDefinition parentProp, PropertyDefinition innerProperty) {
260 return INSTANCE_ID + "_" + parentProp.getName()+ "_" + innerProperty.getName();
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);