8669a781b41a990cf7e122bf0110b4ae87117289
[sdc.git] /
1 package org.openecomp.sdc.be.components.merge.property;
2
3 import static org.mockito.Mockito.verify;
4
5 import java.util.Arrays;
6 import java.util.Collections;
7 import java.util.List;
8
9 import org.junit.Before;
10 import org.junit.Test;
11 import org.mockito.InjectMocks;
12 import org.mockito.Mock;
13 import org.mockito.Mockito;
14 import org.mockito.MockitoAnnotations;
15 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
16 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
17 import org.openecomp.sdc.be.model.InputDefinition;
18
19 public class DataDefinitionsValuesMergingBusinessLogicTest {
20
21     private static final String DEFAULT_PROP_TYPE = "string";
22
23     @InjectMocks
24     private DataDefinitionsValuesMergingBusinessLogic testInstance;
25
26     @Mock
27     private PropertyDataValueMergeBusinessLogic propertyDataValueMergeBusinessLogicMock;
28
29     @Before
30     public void setUp() throws Exception {
31         MockitoAnnotations.initMocks(this);
32     }
33
34     @Test
35     public void mergePropDataDefinition_propertiesNotOfSameType_dontMerge() throws Exception {
36         PropertyDataDefinition oldProp1 = createPropertyDataDefinition("prop1", "oldVal1");
37
38         PropertyDataDefinition newProp1 = createPropertyDataDefinition("prop1", null);
39         newProp1.setType("int");
40
41         testInstance.mergeInstanceDataDefinitions(Collections.singletonList(oldProp1), Collections.emptyList(), Collections.singletonList(newProp1), Collections.emptyList());
42         Mockito.verifyZeroInteractions(propertyDataValueMergeBusinessLogicMock);
43     }
44
45     @Test
46     public void mergePropDataDefinition_propertiesInnerTypesNotSame_dontMerge() throws Exception {
47         PropertyDataDefinition oldProp1 = new PropertyDataDefinitionBuilder()
48                 .setName("prop1")
49                 .setType("list")
50                 .setSchemaType("string")
51                 .setValue("val1").build();
52
53         PropertyDataDefinition newProp1 = new PropertyDataDefinitionBuilder()
54                 .setName("prop1")
55                 .setType("list")
56                 .setSchemaType("int")
57                 .setValue("val1").build();
58
59         testInstance.mergeInstanceDataDefinitions(Collections.singletonList(oldProp1), Collections.emptyList(), Collections.singletonList(newProp1), Collections.emptyList());
60         Mockito.verifyZeroInteractions(propertyDataValueMergeBusinessLogicMock);
61     }
62
63     @Test
64     public void mergePropDataDefinition_getInputsToMerge_mergeInputsThatExistInNewVersion_mergeInputsThatDeclaredByUserInPrevVersion() throws Exception {
65         PropertyDataDefinition userDeclaredGetInput = createGetInputPropertyDataDefinition("prop1", "input1");
66         PropertyDataDefinition nonUserDeclaredGetInput = createGetInputPropertyDataDefinition("prop2", "input2", "input3");
67
68         PropertyDataDefinition newProp1 = createPropertyDataDefinition("prop1", "");
69         PropertyDataDefinition newProp2 = createPropertyDataDefinition("prop2", null);
70
71         InputDefinition oldDeclaredByUserInput1 = new InputDefinition();
72         oldDeclaredByUserInput1.setName("input1");
73         oldDeclaredByUserInput1.setInstanceUniqueId("instanceId");
74
75         InputDefinition oldNotDeclaredByUserInput2 = new InputDefinition();
76         oldNotDeclaredByUserInput2.setName("input2");
77
78         InputDefinition oldNotDeclaredByUserInput3 = new InputDefinition();
79         oldNotDeclaredByUserInput3.setName("input3");
80
81         InputDefinition newInput3 = new InputDefinition();
82         newInput3.setName("input3");
83
84         List<PropertyDataDefinition> oldProps = Arrays.asList(userDeclaredGetInput, nonUserDeclaredGetInput);
85         List<PropertyDataDefinition> newProps = Arrays.asList(newProp1, newProp2);
86
87         List<InputDefinition> oldInputs = Arrays.asList(oldDeclaredByUserInput1, oldNotDeclaredByUserInput2, oldNotDeclaredByUserInput3);
88         List<InputDefinition> newInputs = Collections.singletonList(newInput3);
89
90         testInstance.mergeInstanceDataDefinitions(oldProps, oldInputs, newProps, newInputs);
91         //get input prop was declared by user - ok to merge it although its input not exist (it will be added later)
92         verify(propertyDataValueMergeBusinessLogicMock).mergePropertyValue(userDeclaredGetInput, newProp1, Collections.singletonList("input1"));
93         //input 2 not exist in new version - dont merge it, input 3 exist in new version - ok to merge it
94         verify(propertyDataValueMergeBusinessLogicMock).mergePropertyValue(nonUserDeclaredGetInput, newProp2, Collections.singletonList("input3"));
95     }
96
97     @Test
98     public void mergePropDataDefinition_dontMergeOldPropsIfNotExistInNewVersion() throws Exception {
99         PropertyDataDefinition oldProp = createPropertyDataDefinition("prop1", "oldVal1");
100         PropertyDataDefinition newProp = createPropertyDataDefinition("prop2", null);
101         testInstance.mergeInstanceDataDefinitions(Collections.singletonList(oldProp), Collections.emptyList(), Collections.singletonList(newProp), Collections.emptyList());
102         Mockito.verifyZeroInteractions(propertyDataValueMergeBusinessLogicMock);
103     }
104
105     private PropertyDataDefinition createPropertyDataDefinition(String name, String value) {
106         return new PropertyDataDefinitionBuilder()
107                 .setName(name)
108                 .setType(DEFAULT_PROP_TYPE)
109                 .setValue(value).build();
110     }
111
112     private PropertyDataDefinition createGetInputPropertyDataDefinition(String name, String ... inputsNames) {
113         PropertyDataDefinitionBuilder propertyBuilder = new PropertyDataDefinitionBuilder()
114                 .setName(name)
115                 .setType(DEFAULT_PROP_TYPE);
116         for (String inputName : inputsNames) {
117             propertyBuilder.addGetInputValue(inputName);
118         }
119         return propertyBuilder.build();
120     }
121
122 }