2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.model.tosca.converters;
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;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.HashMap;
34 import static org.junit.Assert.assertEquals;
35 import static org.junit.Assert.assertNull;
37 public class DataTypePropertyConverterTest {
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;
47 public void setUp() throws Exception {
48 dataTypes = new HashMap<>();
50 prop1 = new PropertyDefinition();
51 prop1.setDefaultValue("def1");
52 prop1.setName("prop1");
54 prop2 = new PropertyDefinition();
55 prop2.setType("dataType1");
56 prop2.setName("prop2");
58 prop3 = new PropertyDefinition();
59 prop3.setDefaultValue("def3");
60 prop3.setName("prop3");
62 noDefaultProp = new PropertyDefinition();
63 noDefaultProp.setName("noDefaultProp");
65 noDefaultValue = new DataTypeDefinition();
66 noDefaultValue.setProperties(Collections.singletonList(noDefaultProp));
68 dataType1 = new DataTypeDefinition();
69 dataType1.setProperties(Arrays.asList(prop1, prop3));
71 dataType2 = new DataTypeDefinition();
72 dataType2.setDerivedFrom(dataType1);
74 dataType3 = new DataTypeDefinition();
75 dataType3.setProperties(Collections.singletonList(prop2));
76 dataType3.setDerivedFrom(noDefaultValue);
78 dataTypes.put("noDefault", noDefaultValue);
79 dataTypes.put("dataType1", dataType1);
80 dataTypes.put("dataType2", dataType2);
81 dataTypes.put("dataType3", dataType3);
85 public void testGetPropertyDefaultValuesRec_dataTypeNotExist() throws Exception {
86 String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("someType", dataTypes);
87 assertNull(defaultValue);
91 public void testGetPropertyDefaultValuesRec_NoDefaultValue() throws Exception {
92 String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("noDefault", dataTypes);
93 assertNull(defaultValue);
97 public void testGetPropertyDefaultValuesRec() throws Exception {
98 String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("dataType1", dataTypes);
99 assertEquals(PROPERTY2_DEFAULT, defaultValue);
103 public void testGetPropertyDefaultValuesRec_defaultFromDerivedDataType_derivedDataTypeHasNoDefaults() throws Exception {
104 dataType2.setDerivedFrom(noDefaultValue);
105 String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("dataType2", dataTypes);
106 assertNull(defaultValue);
110 public void testGetPropertyDefaultValuesRec_defaultFromDerivedDataType() throws Exception {
111 String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("dataType2", dataTypes);
112 assertEquals(PROPERTY2_DEFAULT, defaultValue);
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);
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
129 public void testMergeDefaultValues_allDefaultValuesAreOverridden() throws Exception {
130 JsonObject value = new JsonObject();
131 value.addProperty(noDefaultProp.getName(), "override1");
133 JsonObject prop1Val = new JsonObject();
134 prop1Val.addProperty(prop1.getName(), "prop1Override");
136 JsonObject prop3Val = new JsonObject();
137 prop3Val.addProperty(prop3.getName(), "prop3Override");
139 JsonObject prop2Value = new JsonObject();
140 prop2Value.add(prop3.getName(), prop3Val);
141 prop2Value.add(prop1.getName(), prop1Val);
143 value.add(prop2.getName(), prop2Value);
145 String valBeforeMerge = value.toString();
147 testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes);
148 assertEquals(valBeforeMerge, value.toString());
152 public void testMergeDefaultValues() throws Exception {
153 JsonObject value = new JsonObject();
154 value.addProperty(noDefaultProp.getName(), "override1");
156 JsonObject prop1Val = new JsonObject();
157 prop1Val.addProperty(prop1.getName(), "prop1Override");
159 value.add(prop2.getName(), prop1Val);
161 testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes);
163 assertEquals("{\"noDefaultProp\":\"override1\",\"prop2\":{\"prop1\":\"prop1Override\",\"prop3\":\"def3\"}}",
164 value.toString());//expect to merge prop 3 default as it was not overridden
168 public void testMergeDefaultValues_mergeAll() throws Exception {
169 JsonObject value = new JsonObject();
170 testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes);
172 assertEquals("{\"prop2\":" + PROPERTY2_DEFAULT + "}",
173 value.toString());//expect to merge prop 3 default as it was not overridden
177 public void testMergeDefaultValues_doNotAddDefaultsForGetInputValues() throws Exception {
179 JsonObject getInputValue = new JsonObject();
180 getInputValue.addProperty("get_input", "in1");
182 JsonObject value = new JsonObject();
183 value.add(prop2.getName(), getInputValue);
185 testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes);
187 assertEquals("{\"prop2\":{\"get_input\":\"in1\"}}", value.toString());
191 public void testMergeDefaultValues_doNotAddDefaultsForGetInputInnerValues() throws Exception {
192 JsonObject getInputValue = new JsonObject();
193 getInputValue.addProperty("get_input", "in1");
195 JsonObject prop1Val = new JsonObject();
196 prop1Val.add(prop1.getName(), getInputValue);
198 JsonObject value = new JsonObject();
199 value.add(prop2.getName(), prop1Val);
201 testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes);
203 assertEquals("{\"prop2\":{\"prop1\":{\"get_input\":\"in1\"},\"prop3\":\"def3\"}}", value.toString());