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