cf2172d2b08b75e3671564b1555849622e50e662
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / tosca / converters / DataTypePropertyConverterTest.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.model.tosca.converters;
22
23 import com.google.gson.JsonObject;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.openecomp.sdc.be.model.DataTypeDefinition;
27 import org.openecomp.sdc.be.model.PropertyDefinition;
28
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.Map;
33
34 import static org.junit.Assert.assertEquals;
35 import static org.junit.Assert.assertNull;
36
37 public class DataTypePropertyConverterTest {
38
39     private static final String EMPTY_JSON_STR = "{}";
40     public static final String PROPERTY2_DEFAULT = "{\"prop1\":\"def1\",\"prop3\":\"def3\"}";
41     private DataTypePropertyConverter testInstance = DataTypePropertyConverter.getInstance();
42     private Map<String, DataTypeDefinition> dataTypes;
43     private DataTypeDefinition noDefaultValue, dataType1, dataType2, dataType3;
44     private PropertyDefinition prop1, prop2, prop3, noDefaultProp;
45
46     @Before
47     public void setUp() throws Exception {
48         dataTypes = new HashMap<>();
49
50         prop1 = new PropertyDefinition();
51         prop1.setDefaultValue("def1");
52         prop1.setName("prop1");
53
54         prop2 = new PropertyDefinition();
55         prop2.setType("dataType1");
56         prop2.setName("prop2");
57
58         prop3 = new PropertyDefinition();
59         prop3.setDefaultValue("def3");
60         prop3.setName("prop3");
61
62         noDefaultProp = new PropertyDefinition();
63         noDefaultProp.setName("noDefaultProp");
64
65         noDefaultValue = new DataTypeDefinition();
66         noDefaultValue.setProperties(Collections.singletonList(noDefaultProp));
67
68         dataType1 = new DataTypeDefinition();
69         dataType1.setProperties(Arrays.asList(prop1, prop3));
70
71         dataType2 = new DataTypeDefinition();
72         dataType2.setDerivedFrom(dataType1);
73
74         dataType3 = new DataTypeDefinition();
75         dataType3.setProperties(Collections.singletonList(prop2));
76         dataType3.setDerivedFrom(noDefaultValue);
77
78         dataTypes.put("noDefault", noDefaultValue);
79         dataTypes.put("dataType1", dataType1);
80         dataTypes.put("dataType2", dataType2);
81         dataTypes.put("dataType3", dataType3);
82     }
83
84     @Test
85     public void testGetPropertyDefaultValuesRec_dataTypeNotExist() throws Exception {
86         String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("someType", dataTypes);
87         assertNull(defaultValue);
88     }
89
90     @Test
91     public void testGetPropertyDefaultValuesRec_NoDefaultValue() throws Exception {
92         String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("noDefault", dataTypes);
93         assertNull(defaultValue);
94     }
95
96     @Test
97     public void testGetPropertyDefaultValuesRec() throws Exception {
98         String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("dataType1", dataTypes);
99         assertEquals(PROPERTY2_DEFAULT, defaultValue);
100     }
101
102     @Test
103     public void testGetPropertyDefaultValuesRec_defaultFromDerivedDataType_derivedDataTypeHasNoDefaults() throws Exception {
104         dataType2.setDerivedFrom(noDefaultValue);
105         String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("dataType2", dataTypes);
106         assertNull(defaultValue);
107     }
108
109     @Test
110     public void testGetPropertyDefaultValuesRec_defaultFromDerivedDataType() throws Exception {
111         String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("dataType2", dataTypes);
112         assertEquals(PROPERTY2_DEFAULT, defaultValue);
113     }
114
115     @Test
116     public void testGetPropertyDefaultValuesRec_defaultFromDataTypesOfProperties_dataTypeOfPropertyHasNoDefault() throws Exception {
117         dataType3.getProperties().get(0).setType(noDefaultValue.getName());
118         String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("dataType3", dataTypes);
119         assertNull(defaultValue);
120     }
121
122     @Test
123     public void testGetPropertyDefaultValuesRec_defaultFromDataTypesOfProperties() throws Exception {
124         String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("dataType3", dataTypes);
125         assertEquals("{\"prop2\":" + PROPERTY2_DEFAULT + "}", defaultValue);//data type 3 has property prop2 which has a data type with property prop1 which has a default value
126     }
127
128     @Test
129     public void testMergeDefaultValues_allDefaultValuesAreOverridden() throws Exception {
130         JsonObject value = new JsonObject();
131         value.addProperty(noDefaultProp.getName(), "override1");
132
133         JsonObject prop1Val = new JsonObject();
134         prop1Val.addProperty(prop1.getName(), "prop1Override");
135
136         JsonObject prop3Val = new JsonObject();
137         prop3Val.addProperty(prop3.getName(), "prop3Override");
138
139         JsonObject prop2Value = new JsonObject();
140         prop2Value.add(prop3.getName(), prop3Val);
141         prop2Value.add(prop1.getName(), prop1Val);
142
143         value.add(prop2.getName(), prop2Value);
144
145         String valBeforeMerge = value.toString();
146
147         testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes);
148         assertEquals(valBeforeMerge, value.toString());
149     }
150
151     @Test
152     public void testMergeDefaultValues() throws Exception {
153         JsonObject value = new JsonObject();
154         value.addProperty(noDefaultProp.getName(), "override1");
155
156         JsonObject prop1Val = new JsonObject();
157         prop1Val.addProperty(prop1.getName(), "prop1Override");
158
159         value.add(prop2.getName(), prop1Val);
160
161         testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes);
162
163         assertEquals("{\"noDefaultProp\":\"override1\",\"prop2\":{\"prop1\":\"prop1Override\",\"prop3\":\"def3\"}}",
164                       value.toString());//expect to merge prop 3 default as it was not overridden
165     }
166
167     @Test
168     public void testMergeDefaultValues_mergeAll() throws Exception {
169         JsonObject value = new JsonObject();
170         testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes);
171
172         assertEquals("{\"prop2\":" + PROPERTY2_DEFAULT + "}",
173                      value.toString());//expect to merge prop 3 default as it was not overridden
174     }
175
176     @Test
177     public void testMergeDefaultValues_doNotAddDefaultsForGetInputValues() throws Exception {
178
179         JsonObject getInputValue = new JsonObject();
180         getInputValue.addProperty("get_input", "in1");
181
182         JsonObject value = new JsonObject();
183         value.add(prop2.getName(), getInputValue);
184
185         testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes);
186
187         assertEquals("{\"prop2\":{\"get_input\":\"in1\"}}", value.toString());
188     }
189
190     @Test
191     public void testMergeDefaultValues_doNotAddDefaultsForGetInputInnerValues() throws Exception {
192         JsonObject getInputValue = new JsonObject();
193         getInputValue.addProperty("get_input", "in1");
194
195         JsonObject prop1Val = new JsonObject();
196         prop1Val.add(prop1.getName(), getInputValue);
197
198         JsonObject value = new JsonObject();
199         value.add(prop2.getName(), prop1Val);
200
201         testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes);
202
203         assertEquals("{\"prop2\":{\"prop1\":{\"get_input\":\"in1\"},\"prop3\":\"def3\"}}", value.toString());
204
205     }
206 }