6faee487f49b235641798ae604b07c43d62623a4
[sdc.git] /
1 package org.openecomp.sdc.be.components.merge.property;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5 import static org.mockito.Mockito.when;
6
7 import java.util.Arrays;
8 import java.util.Collections;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.function.Function;
12 import java.util.stream.Collectors;
13 import java.util.stream.Stream;
14
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;
27
28 import fj.data.Either;
29
30 public class PropertyDataValueMergeBusinessLogicTest {
31
32     @InjectMocks
33     private PropertyDataValueMergeBusinessLogic testInstance;
34
35     @Mock
36     private ApplicationDataTypeCache applicationDataTypeCache;
37
38     @Before
39     public void setUp() throws Exception {
40         MockitoAnnotations.initMocks(this);
41     }
42
43     @Test
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);
48     }
49
50     @Test
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");
55     }
56
57     @Test
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");
62     }
63
64     @Test
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");
69     }
70
71     @Test
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");
76     }
77
78     @Test
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");
83     }
84
85     @Test
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");
90     }
91
92     @Test
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");
97     }
98
99     @Test
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\"]");
104     }
105
106     @Test
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\"]");
111     }
112
113     @Test
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}}");
118     }
119
120     @Test
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}}]");
125
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\\\"\"}]";
131
132         assertEquals(expectedValue, newProp.getValue());
133     }
134
135     @Test
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\"}");
140     }
141
142     @Test
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");
148     }
149
150     @Test
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());
156     }
157
158     @Test
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");
164     }
165
166     @Test
167     public void mergeListValueWithMultipleGetInputs() throws Exception {
168         PropertyDataDefinition oldProp = new PropertyDataDefinitionBuilder()
169                 .addGetInputValue("input1").addGetInputValue("input2").addGetInputValue("input3")
170                 .setName("prop1")
171                 .setType("list").setSchemaType("string")
172                 .setValue("[{\"get_input\": \"input2\"},{\"get_input\": \"input3\"},{\"get_input\": \"input1\"}]")
173                 .build();
174
175         PropertyDataDefinition newProp = new PropertyDataDefinitionBuilder()
176                 .addGetInputValue("input3")
177                 .setName("prop1")
178                 .setType("list").setSchemaType("string")
179                 .setValue("[\"\", {\"get_input\": \"input3\"}]")
180                 .build();
181
182         testMergeProps(oldProp, newProp,"[{},{\"get_input\":\"input3\"},{\"get_input\":\"input1\"}]", Arrays.asList("input3", "input1"));
183         assertGetInputValues(newProp, "input3", "input1");
184     }
185
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);
193         }
194     }
195
196     private void testMergeProps(PropertyDataDefinition oldProp, PropertyDataDefinition newProp, String expectedValue) {
197         testMergeProps(oldProp, newProp, expectedValue, Collections.emptyList());
198     }
199
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());
204     }
205
206     private PropertyDataDefinition createProp(String name, String type, String innerType, String val) {
207         return new PropertyDataDefinitionBuilder()
208                 .setType(type)
209                 .setSchemaType(innerType)
210                 .setValue(val)
211                 .setName(name)
212                 .build();
213     }
214
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()
218                 .setType(type)
219                 .setSchemaType(innerType)
220                 .setValue(val)
221                 .addGetInputValue(inputName)
222                 .setName(name)
223                 .build();
224
225     }
226
227     private Map<String, DataTypeDefinition> buildDataTypes() {
228         DataTypeDefinition myType = new DataTypeDefinition();
229         myType.setName("myType");
230         DataTypeDefinition myInnerType = new DataTypeDefinition();
231         myInnerType.setName("myInnerType");
232
233         PropertyDefinition prop1 = new PropertyDefinition();
234         prop1.setName("prop1");
235
236         PropertyDefinition prop2 = new PropertyDefinition();
237         prop2.setName("prop2");
238         prop2.setType("myInnerType");
239
240         PropertyDefinition prop3 = new PropertyDefinition();
241         prop3.setName("prop3");
242
243         PropertyDefinition prop4 = new PropertyDefinition();
244         prop4.setName("prop4");
245
246         myType.setProperties(Arrays.asList(prop1, prop2));
247         myInnerType.setProperties(Arrays.asList(prop3, prop4));
248
249         return Stream.of(myType, myInnerType).collect(Collectors.toMap(DataTypeDefinition::getName, Function.identity()));
250     }
251
252
253 }