1 package org.openecomp.sdc.be.components.merge.property;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5 import static org.mockito.Mockito.when;
7 import java.util.Arrays;
8 import java.util.Collections;
11 import java.util.function.Function;
12 import java.util.stream.Collectors;
13 import java.util.stream.Stream;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.mockito.InjectMocks;
18 import org.mockito.Mock;
19 import org.mockito.MockitoAnnotations;
20 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
21 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
22 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
23 import org.openecomp.sdc.be.model.DataTypeDefinition;
24 import org.openecomp.sdc.be.model.PropertyDefinition;
25 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
26 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
28 import fj.data.Either;
30 public class PropertyDataValueMergeBusinessLogicTest {
33 private PropertyDataValueMergeBusinessLogic testInstance;
36 private ApplicationDataTypeCache applicationDataTypeCache;
39 public void setUp() throws Exception {
40 MockitoAnnotations.initMocks(this);
44 public void mergeProperties_emptyOldAndNewValues() throws Exception {
45 PropertyDataDefinition oldProp = createProp("prop1", ToscaPropertyType.STRING.getType(), null, null);
46 PropertyDataDefinition newProp = createProp("prop1", ToscaPropertyType.STRING.getType(), null, null);
47 testMergeProps(oldProp, newProp, null);
51 public void mergeProperties_emptyOldValue() throws Exception {
52 PropertyDataDefinition oldProp = createProp("prop1", ToscaPropertyType.STRING.getType(), null, null);
53 PropertyDataDefinition newProp = createProp("prop1", ToscaPropertyType.STRING.getType(), null, "newVal");
54 testMergeProps(oldProp, newProp, "newVal");
58 public void mergeSimpleStringType_copyOldValueIfNoNewValue() throws Exception {
59 PropertyDataDefinition oldProp = createProp("prop1", ToscaPropertyType.STRING.getType(), null, "val1");
60 PropertyDataDefinition newProp = createProp("prop1", ToscaPropertyType.STRING.getType(), null, null);
61 testMergeProps(oldProp, newProp, "val1");
65 public void mergeSimpleStringType_dontCopyOldValIfHasNewVal() throws Exception {
66 PropertyDataDefinition oldProp = createProp("prop1", ToscaPropertyType.STRING.getType(), null, "val1");
67 PropertyDataDefinition newProp = createProp("prop1", ToscaPropertyType.STRING.getType(), null, "newVal");
68 testMergeProps(oldProp, newProp, "newVal");
72 public void mergeSimpleIntType_copyOldValueIfNoNewValue() throws Exception {
73 PropertyDataDefinition oldProp = createProp("prop1", ToscaPropertyType.INTEGER.getType(), null, "44");
74 PropertyDataDefinition newProp = createProp("prop1", ToscaPropertyType.STRING.getType(), null, null);
75 testMergeProps(oldProp, newProp, "44");
79 public void mergeSimpleIntType_dontCopyOldValIfHasNewVal() throws Exception {
80 PropertyDataDefinition oldProp = createProp("prop1", ToscaPropertyType.INTEGER.getType(), null, "44");
81 PropertyDataDefinition newProp = createProp("prop1", ToscaPropertyType.STRING.getType(), null, "45");
82 testMergeProps(oldProp, newProp, "45");
86 public void mergeSimpleBooleanType_copyOldValueIfNoNewValue() throws Exception {
87 PropertyDataDefinition oldProp = createProp("prop1", ToscaPropertyType.BOOLEAN.getType(), null, "false");
88 PropertyDataDefinition newProp = createProp("prop1", ToscaPropertyType.BOOLEAN.getType(), null, null);
89 testMergeProps(oldProp, newProp, "false");
93 public void mergeSimpleBooleanType_dontCopyOldValIfHasNewVal() throws Exception {
94 PropertyDataDefinition oldProp = createProp("prop1", ToscaPropertyType.BOOLEAN.getType(), null, "false");
95 PropertyDataDefinition newProp = createProp("prop1", ToscaPropertyType.BOOLEAN.getType(), null, "true");
96 testMergeProps(oldProp, newProp, "true");
100 public void mergeSimpleListType_copyOldValuesByIndex() throws Exception {
101 PropertyDataDefinition oldProp = createProp("prop1", ToscaPropertyType.LIST.getType(), "string", "[\"a\", \"b\", \"c\"]");
102 PropertyDataDefinition newProp = createProp("prop1", ToscaPropertyType.LIST.getType(), "string", "[\"x\", \"\"]");
103 testMergeProps(oldProp, newProp, "[\"x\",\"b\",\"c\"]");
107 public void mergeSimpleListType_jsonList() throws Exception {
108 PropertyDataDefinition oldProp = createProp("prop1", ToscaPropertyType.LIST.getType(), "json", "[[\"a\", \"b\"], \"c\"]");
109 PropertyDataDefinition newProp = createProp("prop1", ToscaPropertyType.LIST.getType(), "json", "[[\"a\"], \"\"]");
110 testMergeProps(oldProp, newProp, "[[\"a\"],\"c\"]");
114 public void mergeComplexType() throws Exception {
115 PropertyDataDefinition oldProp = createProp("prop1", "myType", null, "{\"mac_range_plan\":\"y\", \"mac_count_required\":{\"is_required\":true,\"count\":44}}");
116 PropertyDataDefinition newProp = createProp("prop1", "myType", null, "{\"mac_count_required\":{\"is_required\":false, \"mac_address\":\"myAddress\"}}");
117 testMergeProps(oldProp, newProp, "{\"mac_range_plan\":\"y\",\"mac_count_required\":{\"is_required\":false,\"mac_address\":\"myAddress\",\"count\":44}}");
121 public void mergeListOfComplexType() throws Exception {
122 PropertyDataDefinition oldProp = createProp("prop1", "list", "myType", "[{\"prop1\":\"val1\", \"prop2\":{\"prop3\":true,\"prop4\":44}}, " +
123 "{\"prop1\":\"val2\", \"prop2\":{\"prop3\":true}}]");
124 PropertyDataDefinition newProp = createProp("prop1", "list", "myType", "[{\"prop2\":{\"prop3\":false}}]");
126 Map<String, DataTypeDefinition> dataTypes = buildDataTypes();
127 when(applicationDataTypeCache.getAll()).thenReturn(Either.left(dataTypes));
128 testInstance.mergePropertyValue(oldProp, newProp, Collections.emptyList());
129 String expectedValue = "[{\"prop2\":{\"prop4\":44,\"prop3\":false},\"prop1\":\"\\\"val1\\\"\"}," +
130 "{\"prop2\":{\"prop3\":true},\"prop1\":\"\\\"val2\\\"\"}]";
132 assertEquals(expectedValue, newProp.getValue());
136 public void mergeMapType() throws Exception {
137 PropertyDataDefinition oldProp = createProp("prop1", "map", "string", "{\"prop1\":\"val1\", \"prop2\":\"val2\", \"prop3\":\"val3\"}");
138 PropertyDataDefinition newProp = createProp("prop1", "map", "string", "{\"prop1\":\"newVal1\", \"prop2\":\"\"}");
139 testMergeProps(oldProp, newProp, "{\"prop2\":\"val2\",\"prop1\":\"newVal1\",\"prop3\":\"val3\"}");
143 public void mergeGetInputValue() throws Exception {
144 PropertyDataDefinition oldProp = createGetInputProp("prop1", "string", null, "input1");
145 PropertyDataDefinition newProp = createProp("prop1", "string", null, null);
146 testMergeProps(oldProp, newProp, oldProp.getValue(), Collections.singletonList("input1"));
147 assertGetInputValues(newProp, "input1");
151 public void mergeGetInputValue_inputNotForMerging() throws Exception {
152 PropertyDataDefinition oldProp = createGetInputProp("prop1", "string", null, "input1");
153 PropertyDataDefinition newProp = createProp("prop1", "string", null, null);
154 testMergeProps(oldProp, newProp,null, Collections.singletonList("input2"));
155 assertTrue(newProp.getGetInputValues().isEmpty());
159 public void mergeComplexGetInputValue_moreThanOneGetInput_copyOnlyInputsForMerging() throws Exception {
160 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();
161 PropertyDataDefinition newProp = createProp("prop1", "myType", null, "{\"mac_count_required\":{\"is_required\":true}}");
162 testMergeProps(oldProp, newProp,"{\"mac_range_plan\":{},\"mac_count_required\":{\"is_required\":true,\"count\":{\"get_input\":\"input2\"}}}", Collections.singletonList("input2"));
163 assertGetInputValues(newProp, "input2");
167 public void mergeListValueWithMultipleGetInputs() throws Exception {
168 PropertyDataDefinition oldProp = new PropertyDataDefinitionBuilder()
169 .addGetInputValue("input1").addGetInputValue("input2").addGetInputValue("input3")
171 .setType("list").setSchemaType("string")
172 .setValue("[{\"get_input\": \"input2\"},{\"get_input\": \"input3\"},{\"get_input\": \"input1\"}]")
175 PropertyDataDefinition newProp = new PropertyDataDefinitionBuilder()
176 .addGetInputValue("input3")
178 .setType("list").setSchemaType("string")
179 .setValue("[\"\", {\"get_input\": \"input3\"}]")
182 testMergeProps(oldProp, newProp,"[{},{\"get_input\":\"input3\"},{\"get_input\":\"input1\"}]", Arrays.asList("input3", "input1"));
183 assertGetInputValues(newProp, "input3", "input1");
186 private void assertGetInputValues(PropertyDataDefinition newProp, String ... expectedInputNames) {
187 assertTrue(newProp.isGetInputProperty());
188 assertEquals(newProp.getGetInputValues().size(), expectedInputNames.length);
189 for (int i = 0; i < expectedInputNames.length; i++) {
190 String expectedInputName = expectedInputNames[i];
191 GetInputValueDataDefinition getInputValueDataDefinition = newProp.getGetInputValues().get(i);
192 assertEquals(getInputValueDataDefinition.getInputName(), expectedInputName);
196 private void testMergeProps(PropertyDataDefinition oldProp, PropertyDataDefinition newProp, String expectedValue) {
197 testMergeProps(oldProp, newProp, expectedValue, Collections.emptyList());
200 private void testMergeProps(PropertyDataDefinition oldProp, PropertyDataDefinition newProp, String expectedValue, List<String> getInputsToMerge) {
201 when(applicationDataTypeCache.getAll()).thenReturn(Either.left(Collections.emptyMap()));
202 testInstance.mergePropertyValue(oldProp, newProp, getInputsToMerge);
203 assertEquals(expectedValue, newProp.getValue());
206 private PropertyDataDefinition createProp(String name, String type, String innerType, String val) {
207 return new PropertyDataDefinitionBuilder()
209 .setSchemaType(innerType)
215 private PropertyDataDefinition createGetInputProp(String name, String type, String innerType, String inputName) {
216 String val = String.format("{\"get_input\":\"%s\"}", inputName);
217 return new PropertyDataDefinitionBuilder()
219 .setSchemaType(innerType)
221 .addGetInputValue(inputName)
227 private Map<String, DataTypeDefinition> buildDataTypes() {
228 DataTypeDefinition myType = new DataTypeDefinition();
229 myType.setName("myType");
230 DataTypeDefinition myInnerType = new DataTypeDefinition();
231 myInnerType.setName("myInnerType");
233 PropertyDefinition prop1 = new PropertyDefinition();
234 prop1.setName("prop1");
236 PropertyDefinition prop2 = new PropertyDefinition();
237 prop2.setName("prop2");
238 prop2.setType("myInnerType");
240 PropertyDefinition prop3 = new PropertyDefinition();
241 prop3.setName("prop3");
243 PropertyDefinition prop4 = new PropertyDefinition();
244 prop4.setName("prop4");
246 myType.setProperties(Arrays.asList(prop1, prop2));
247 myInnerType.setProperties(Arrays.asList(prop3, prop4));
249 return Stream.of(myType, myInnerType).collect(Collectors.toMap(DataTypeDefinition::getName, Function.identity()));