aa7f606a94dfaaf86282b8db905b76fad380f7b3
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / merge / property / DataDefinitionsValuesMergingBusinessLogicTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.merge.property;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.MockitoAnnotations;
29 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionAbstractBuilder;
30 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
31 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
32 import org.openecomp.sdc.be.model.InputDefinition;
33
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.List;
37
38 import static org.mockito.Mockito.verify;
39
40 public class DataDefinitionsValuesMergingBusinessLogicTest {
41
42     private static final String DEFAULT_PROP_TYPE = "string";
43
44     @InjectMocks
45     private DataDefinitionsValuesMergingBusinessLogic testInstance;
46
47     @Mock
48     private PropertyDataValueMergeBusinessLogic propertyDataValueMergeBusinessLogicMock;
49
50     @Before
51     public void setUp() throws Exception {
52         MockitoAnnotations.openMocks(this);
53     }
54
55     @Test
56     public void mergePropDataDefinition_propertiesNotOfSameType_dontMerge() throws Exception {
57         PropertyDataDefinition oldProp1 = createPropertyDataDefinition("prop1", "oldVal1");
58
59         PropertyDataDefinition newProp1 = createPropertyDataDefinition("prop1", null);
60         newProp1.setType("int");
61
62         testInstance.mergeInstanceDataDefinitions(Collections.singletonList(oldProp1), Collections.emptyList(), Collections.singletonList(newProp1), Collections.emptyList());
63         Mockito.verifyZeroInteractions(propertyDataValueMergeBusinessLogicMock);
64     }
65
66     @Test
67     public void mergePropDataDefinition_propertiesInnerTypesNotSame_dontMerge() throws Exception {
68         PropertyDataDefinition oldProp1 = new PropertyDataDefinitionBuilder()
69                 .setName("prop1")
70                 .setType("list")
71                 .setSchemaType("string")
72                 .setValue("val1").build();
73
74         PropertyDataDefinition newProp1 = new PropertyDataDefinitionBuilder()
75                 .setName("prop1")
76                 .setType("list")
77                 .setSchemaType("int")
78                 .setValue("val1").build();
79
80         testInstance.mergeInstanceDataDefinitions(Collections.singletonList(oldProp1), Collections.emptyList(), Collections.singletonList(newProp1), Collections.emptyList());
81         Mockito.verifyZeroInteractions(propertyDataValueMergeBusinessLogicMock);
82     }
83
84     @Test
85     public void mergePropDataDefinition_getInputsToMerge_mergeInputsThatExistInNewVersion_mergeInputsThatDeclaredByUserInPrevVersion() throws Exception {
86         PropertyDataDefinition userDeclaredGetInput = createGetInputPropertyDataDefinition("prop1", "input1");
87         PropertyDataDefinition nonUserDeclaredGetInput = createGetInputPropertyDataDefinition("prop2", "input2", "input3");
88
89         PropertyDataDefinition newProp1 = createPropertyDataDefinition("prop1", "");
90         PropertyDataDefinition newProp2 = createPropertyDataDefinition("prop2", null);
91
92         InputDefinition oldDeclaredByUserInput1 = new InputDefinition();
93         oldDeclaredByUserInput1.setName("input1");
94         oldDeclaredByUserInput1.setInstanceUniqueId("instanceId");
95
96         InputDefinition oldNotDeclaredByUserInput2 = new InputDefinition();
97         oldNotDeclaredByUserInput2.setName("input2");
98
99         InputDefinition oldNotDeclaredByUserInput3 = new InputDefinition();
100         oldNotDeclaredByUserInput3.setName("input3");
101
102         InputDefinition newInput3 = new InputDefinition();
103         newInput3.setName("input3");
104
105         List<PropertyDataDefinition> oldProps = Arrays.asList(userDeclaredGetInput, nonUserDeclaredGetInput);
106         List<PropertyDataDefinition> newProps = Arrays.asList(newProp1, newProp2);
107
108         List<InputDefinition> oldInputs = Arrays.asList(oldDeclaredByUserInput1, oldNotDeclaredByUserInput2, oldNotDeclaredByUserInput3);
109         List<InputDefinition> newInputs = Collections.singletonList(newInput3);
110
111         testInstance.mergeInstanceDataDefinitions(oldProps, oldInputs, newProps, newInputs);
112         //get input prop was declared by user - ok to merge it although its input not exist (it will be added later)
113         verify(propertyDataValueMergeBusinessLogicMock).mergePropertyValue(userDeclaredGetInput, newProp1, Collections.singletonList("input1"));
114         //input 2 not exist in new version - dont merge it, input 3 exist in new version - ok to merge it
115         verify(propertyDataValueMergeBusinessLogicMock).mergePropertyValue(nonUserDeclaredGetInput, newProp2, Collections.singletonList("input3"));
116     }
117
118     @Test
119     public void mergePropDataDefinition_dontMergeOldPropsIfNotExistInNewVersion() throws Exception {
120         PropertyDataDefinition oldProp = createPropertyDataDefinition("prop1", "oldVal1");
121         PropertyDataDefinition newProp = createPropertyDataDefinition("prop2", null);
122         testInstance.mergeInstanceDataDefinitions(Collections.singletonList(oldProp), Collections.emptyList(), Collections.singletonList(newProp), Collections.emptyList());
123         Mockito.verifyZeroInteractions(propertyDataValueMergeBusinessLogicMock);
124     }
125
126     private PropertyDataDefinition createPropertyDataDefinition(String name, String value) {
127         return new PropertyDataDefinitionBuilder()
128                 .setName(name)
129                 .setType(DEFAULT_PROP_TYPE)
130                 .setValue(value).build();
131     }
132
133     private PropertyDataDefinition createGetInputPropertyDataDefinition(String name, String ... inputsNames) {
134         PropertyDataDefinitionAbstractBuilder propertyBuilder = new PropertyDataDefinitionBuilder()
135                 .setName(name)
136                 .setType(DEFAULT_PROP_TYPE);
137
138         for (String inputName : inputsNames) {
139             propertyBuilder.addGetInputValue(inputName);
140         }
141         return propertyBuilder.build();
142     }
143
144 }