1 package org.openecomp.sdc.be.components.merge.property;
4 import org.junit.Before;
6 import org.mockito.InjectMocks;
7 import org.mockito.Mock;
8 import org.mockito.MockitoAnnotations;
9 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
10 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
11 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
12 import org.openecomp.sdc.be.model.DataTypeDefinition;
13 import org.openecomp.sdc.be.model.PropertyDefinition;
14 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
15 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
17 import java.util.Arrays;
18 import java.util.Collections;
19 import java.util.List;
21 import java.util.function.Function;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Mockito.when;
29 public class PropertyDataValueMergeBusinessLogicTest {
32 private PropertyDataValueMergeBusinessLogic testInstance;
35 private ApplicationDataTypeCache applicationDataTypeCache;
38 public void setUp() throws Exception {
39 MockitoAnnotations.initMocks(this);
43 public void mergeProperties_emptyOldAndNewValues() throws Exception {
44 PropertyDataDefinition oldProp = createProp("prop1", ToscaPropertyType.STRING.getType(), null, null);
45 PropertyDataDefinition newProp = createProp("prop1", ToscaPropertyType.STRING.getType(), null, null);
46 testMergeProps(oldProp, newProp, null);
50 public void mergeProperties_emptyOldValue() throws Exception {
51 PropertyDataDefinition oldProp = createProp("prop1", ToscaPropertyType.STRING.getType(), null, null);
52 PropertyDataDefinition newProp = createProp("prop1", ToscaPropertyType.STRING.getType(), null, "newVal");
53 testMergeProps(oldProp, newProp, "newVal");
57 public void mergeSimpleStringType_copyOldValueIfNoNewValue() throws Exception {
58 PropertyDataDefinition oldProp = createProp("prop1", ToscaPropertyType.STRING.getType(), null, "val1");
59 PropertyDataDefinition newProp = createProp("prop1", ToscaPropertyType.STRING.getType(), null, null);
60 testMergeProps(oldProp, newProp, "val1");
64 public void mergeSimpleStringType_dontCopyOldValIfHasNewVal() throws Exception {
65 PropertyDataDefinition oldProp = createProp("prop1", ToscaPropertyType.STRING.getType(), null, "val1");
66 PropertyDataDefinition newProp = createProp("prop1", ToscaPropertyType.STRING.getType(), null, "newVal");
67 testMergeProps(oldProp, newProp, "newVal");
71 public void mergeSimpleIntType_copyOldValueIfNoNewValue() throws Exception {
72 PropertyDataDefinition oldProp = createProp("prop1", ToscaPropertyType.INTEGER.getType(), null, "44");
73 PropertyDataDefinition newProp = createProp("prop1", ToscaPropertyType.STRING.getType(), null, null);
74 testMergeProps(oldProp, newProp, "44");
78 public void mergeSimpleIntType_dontCopyOldValIfHasNewVal() throws Exception {
79 PropertyDataDefinition oldProp = createProp("prop1", ToscaPropertyType.INTEGER.getType(), null, "44");
80 PropertyDataDefinition newProp = createProp("prop1", ToscaPropertyType.STRING.getType(), null, "45");
81 testMergeProps(oldProp, newProp, "45");
85 public void mergeSimpleBooleanType_copyOldValueIfNoNewValue() throws Exception {
86 PropertyDataDefinition oldProp = createProp("prop1", ToscaPropertyType.BOOLEAN.getType(), null, "false");
87 PropertyDataDefinition newProp = createProp("prop1", ToscaPropertyType.BOOLEAN.getType(), null, null);
88 testMergeProps(oldProp, newProp, "false");
92 public void mergeSimpleBooleanType_dontCopyOldValIfHasNewVal() throws Exception {
93 PropertyDataDefinition oldProp = createProp("prop1", ToscaPropertyType.BOOLEAN.getType(), null, "false");
94 PropertyDataDefinition newProp = createProp("prop1", ToscaPropertyType.BOOLEAN.getType(), null, "true");
95 testMergeProps(oldProp, newProp, "true");
99 public void mergeSimpleListType_copyOldValuesByIndex() throws Exception {
100 PropertyDataDefinition oldProp = createProp("prop1", ToscaPropertyType.LIST.getType(), "string", "[\"a\", \"b\", \"c\"]");
101 PropertyDataDefinition newProp = createProp("prop1", ToscaPropertyType.LIST.getType(), "string", "[\"x\", \"\"]");
102 testMergeProps(oldProp, newProp, "[\"x\",\"b\",\"c\"]");
106 public void mergeSimpleListType_jsonList() throws Exception {
107 PropertyDataDefinition oldProp = createProp("prop1", ToscaPropertyType.LIST.getType(), "json", "[[\"a\", \"b\"], \"c\"]");
108 PropertyDataDefinition newProp = createProp("prop1", ToscaPropertyType.LIST.getType(), "json", "[[\"a\"], \"\"]");
109 testMergeProps(oldProp, newProp, "[[\"a\"],\"c\"]");
113 public void mergeComplexType() throws Exception {
114 PropertyDataDefinition oldProp = createProp("prop1", "myType", null, "{\"mac_range_plan\":\"y\", \"mac_count_required\":{\"is_required\":true,\"count\":44}}");
115 PropertyDataDefinition newProp = createProp("prop1", "myType", null, "{\"mac_count_required\":{\"is_required\":false, \"mac_address\":\"myAddress\"}}");
116 testMergeProps(oldProp, newProp, "{\"mac_range_plan\":\"y\",\"mac_count_required\":{\"is_required\":false,\"mac_address\":\"myAddress\",\"count\":44}}");
120 public void mergeListOfComplexType() throws Exception {
121 PropertyDataDefinition oldProp = createProp("prop1", "list", "myType", "[{\"prop1\":\"val1\", \"prop2\":{\"prop3\":true,\"prop4\":44}}, " +
122 "{\"prop1\":\"val2\", \"prop2\":{\"prop3\":true}}]");
123 PropertyDataDefinition newProp = createProp("prop1", "list", "myType", "[{\"prop2\":{\"prop3\":false}}]");
125 Map<String, DataTypeDefinition> dataTypes = buildDataTypes();
126 when(applicationDataTypeCache.getAll()).thenReturn(Either.left(dataTypes));
127 testInstance.mergePropertyValue(oldProp, newProp, Collections.emptyList());
128 String expectedValue = "[{\"prop2\":{\"prop4\":44,\"prop3\":false},\"prop1\":\"\\\"val1\\\"\"}," +
129 "{\"prop2\":{\"prop3\":true},\"prop1\":\"\\\"val2\\\"\"}]";
131 assertEquals(expectedValue, newProp.getValue());
135 public void mergeMapType() throws Exception {
136 PropertyDataDefinition oldProp = createProp("prop1", "map", "string", "{\"prop1\":\"val1\", \"prop2\":\"val2\", \"prop3\":\"val3\"}");
137 PropertyDataDefinition newProp = createProp("prop1", "map", "string", "{\"prop1\":\"newVal1\", \"prop2\":\"\"}");
138 testMergeProps(oldProp, newProp, "{\"prop2\":\"val2\",\"prop1\":\"newVal1\",\"prop3\":\"val3\"}");
142 public void mergeGetInputValue() throws Exception {
143 PropertyDataDefinition oldProp = createGetInputProp("prop1", "string", null, "input1");
144 PropertyDataDefinition newProp = createProp("prop1", "string", null, null);
145 testMergeProps(oldProp, newProp, oldProp.getValue(), Collections.singletonList("input1"));
146 assertGetInputValues(newProp, "input1");
150 public void mergeGetInputValue_inputNotForMerging() throws Exception {
151 PropertyDataDefinition oldProp = createGetInputProp("prop1", "string", null, "input1");
152 PropertyDataDefinition newProp = createProp("prop1", "string", null, null);
153 testMergeProps(oldProp, newProp,null, Collections.singletonList("input2"));
154 assertTrue(newProp.getGetInputValues().isEmpty());
158 public void mergeComplexGetInputValue_moreThanOneGetInput_copyOnlyInputsForMerging() throws Exception {
159 PropertyDataDefinition oldProp = new PropertyDataDefinitionBuilder().addGetInputValue("input1").addGetInputValue("input2").setName("prop1").setType("myType").setValue("{\"mac_range_plan\":{\"get_input\": \"input1\"}, \"mac_count_required\":{\"is_required\":true,\"count\":{\"get_input\": \"input2\"}}}").build();
160 PropertyDataDefinition newProp = createProp("prop1", "myType", null, "{\"mac_count_required\":{\"is_required\":true}}");
161 testMergeProps(oldProp, newProp,"{\"mac_range_plan\":{},\"mac_count_required\":{\"is_required\":true,\"count\":{\"get_input\":\"input2\"}}}", Collections.singletonList("input2"));
162 assertGetInputValues(newProp, "input2");
166 public void mergeListValueWithMultipleGetInputs() throws Exception {
167 PropertyDataDefinition oldProp = new PropertyDataDefinitionBuilder()
168 .addGetInputValue("input1").addGetInputValue("input2").addGetInputValue("input3")
170 .setType("list").setSchemaType("string")
171 .setValue("[{\"get_input\": \"input2\"},{\"get_input\": \"input3\"},{\"get_input\": \"input1\"}]")
174 PropertyDataDefinition newProp = new PropertyDataDefinitionBuilder()
175 .addGetInputValue("input3")
177 .setType("list").setSchemaType("string")
178 .setValue("[\"\", {\"get_input\": \"input3\"}]")
181 testMergeProps(oldProp, newProp,"[{},{\"get_input\":\"input3\"},{\"get_input\":\"input1\"}]", Arrays.asList("input3", "input1"));
182 assertGetInputValues(newProp, "input3", "input1");
185 private void assertGetInputValues(PropertyDataDefinition newProp, String ... expectedInputNames) {
186 assertTrue(newProp.isGetInputProperty());
187 assertEquals(newProp.getGetInputValues().size(), expectedInputNames.length);
188 for (int i = 0; i < expectedInputNames.length; i++) {
189 String expectedInputName = expectedInputNames[i];
190 GetInputValueDataDefinition getInputValueDataDefinition = newProp.getGetInputValues().get(i);
191 assertEquals(getInputValueDataDefinition.getInputName(), expectedInputName);
195 private void testMergeProps(PropertyDataDefinition oldProp, PropertyDataDefinition newProp, String expectedValue) {
196 testMergeProps(oldProp, newProp, expectedValue, Collections.emptyList());
199 private void testMergeProps(PropertyDataDefinition oldProp, PropertyDataDefinition newProp, String expectedValue, List<String> getInputsToMerge) {
200 when(applicationDataTypeCache.getAll()).thenReturn(Either.left(Collections.emptyMap()));
201 testInstance.mergePropertyValue(oldProp, newProp, getInputsToMerge);
202 assertEquals(expectedValue, newProp.getValue());
205 private PropertyDataDefinition createProp(String name, String type, String innerType, String val) {
206 return new PropertyDataDefinitionBuilder()
208 .setSchemaType(innerType)
214 private PropertyDataDefinition createGetInputProp(String name, String type, String innerType, String inputName) {
215 String val = String.format("{\"get_input\":\"%s\"}", inputName);
216 return new PropertyDataDefinitionBuilder()
218 .setSchemaType(innerType)
220 .addGetInputValue(inputName)
226 private Map<String, DataTypeDefinition> buildDataTypes() {
227 DataTypeDefinition myType = new DataTypeDefinition();
228 myType.setName("myType");
229 DataTypeDefinition myInnerType = new DataTypeDefinition();
230 myInnerType.setName("myInnerType");
232 PropertyDefinition prop1 = new PropertyDefinition();
233 prop1.setName("prop1");
235 PropertyDefinition prop2 = new PropertyDefinition();
236 prop2.setName("prop2");
237 prop2.setType("myInnerType");
239 PropertyDefinition prop3 = new PropertyDefinition();
240 prop3.setName("prop3");
242 PropertyDefinition prop4 = new PropertyDefinition();
243 prop4.setName("prop4");
245 myType.setProperties(Arrays.asList(prop1, prop2));
246 myInnerType.setProperties(Arrays.asList(prop3, prop4));
248 return Stream.of(myType, myInnerType).collect(Collectors.toMap(DataTypeDefinition::getName, Function.identity()));