5c23ec0e8cede49c717510ae4e9a0ed5fb3769fc
[sdc.git] /
1 package org.openecomp.sdc.be.components.merge.property;
2
3 import fj.data.Either;
4 import org.junit.Before;
5 import org.junit.Test;
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;
16
17 import java.util.Arrays;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.function.Function;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Mockito.when;
28
29 public class PropertyDataValueMergeBusinessLogicTest {
30
31     @InjectMocks
32     private PropertyDataValueMergeBusinessLogic testInstance;
33
34     @Mock
35     private ApplicationDataTypeCache applicationDataTypeCache;
36
37     @Before
38     public void setUp() throws Exception {
39         MockitoAnnotations.initMocks(this);
40     }
41
42     @Test
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);
47     }
48
49     @Test
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");
54     }
55
56     @Test
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");
61     }
62
63     @Test
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");
68     }
69
70     @Test
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");
75     }
76
77     @Test
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");
82     }
83
84     @Test
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");
89     }
90
91     @Test
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");
96     }
97
98     @Test
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\"]");
103     }
104
105     @Test
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\"]");
110     }
111
112     @Test
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}}");
117     }
118
119     @Test
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}}]");
124
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\\\"\"}]";
130
131         assertEquals(expectedValue, newProp.getValue());
132     }
133
134     @Test
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\"}");
139     }
140
141     @Test
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");
147     }
148
149     @Test
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());
155     }
156
157     @Test
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");
163     }
164
165     @Test
166     public void mergeListValueWithMultipleGetInputs() throws Exception {
167         PropertyDataDefinition oldProp = new PropertyDataDefinitionBuilder()
168                 .addGetInputValue("input1").addGetInputValue("input2").addGetInputValue("input3")
169                 .setName("prop1")
170                 .setType("list").setSchemaType("string")
171                 .setValue("[{\"get_input\": \"input2\"},{\"get_input\": \"input3\"},{\"get_input\": \"input1\"}]")
172                 .build();
173
174         PropertyDataDefinition newProp = new PropertyDataDefinitionBuilder()
175                 .addGetInputValue("input3")
176                 .setName("prop1")
177                 .setType("list").setSchemaType("string")
178                 .setValue("[\"\", {\"get_input\": \"input3\"}]")
179                 .build();
180
181         testMergeProps(oldProp, newProp,"[{},{\"get_input\":\"input3\"},{\"get_input\":\"input1\"}]", Arrays.asList("input3", "input1"));
182         assertGetInputValues(newProp, "input3", "input1");
183     }
184
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);
192         }
193     }
194
195     private void testMergeProps(PropertyDataDefinition oldProp, PropertyDataDefinition newProp, String expectedValue) {
196         testMergeProps(oldProp, newProp, expectedValue, Collections.emptyList());
197     }
198
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());
203     }
204
205     private PropertyDataDefinition createProp(String name, String type, String innerType, String val) {
206         return new PropertyDataDefinitionBuilder()
207                 .setType(type)
208                 .setSchemaType(innerType)
209                 .setValue(val)
210                 .setName(name)
211                 .build();
212     }
213
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()
217                 .setType(type)
218                 .setSchemaType(innerType)
219                 .setValue(val)
220                 .addGetInputValue(inputName)
221                 .setName(name)
222                 .build();
223
224     }
225
226     private Map<String, DataTypeDefinition> buildDataTypes() {
227         DataTypeDefinition myType = new DataTypeDefinition();
228         myType.setName("myType");
229         DataTypeDefinition myInnerType = new DataTypeDefinition();
230         myInnerType.setName("myInnerType");
231
232         PropertyDefinition prop1 = new PropertyDefinition();
233         prop1.setName("prop1");
234
235         PropertyDefinition prop2 = new PropertyDefinition();
236         prop2.setName("prop2");
237         prop2.setType("myInnerType");
238
239         PropertyDefinition prop3 = new PropertyDefinition();
240         prop3.setName("prop3");
241
242         PropertyDefinition prop4 = new PropertyDefinition();
243         prop4.setName("prop4");
244
245         myType.setProperties(Arrays.asList(prop1, prop2));
246         myInnerType.setProperties(Arrays.asList(prop3, prop4));
247
248         return Stream.of(myType, myInnerType).collect(Collectors.toMap(DataTypeDefinition::getName, Function.identity()));
249     }
250
251
252 }