summaryrefslogtreecommitdiffstats
path: root/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/converters/DataTypePropertyConverterTest.java
blob: ebb7566611691150bc2833817ff015f2c0773d47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package org.openecomp.sdc.be.model.tosca.converters;

import static org.junit.Assert.*;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.junit.Before;
import org.junit.Test;
import org.openecomp.sdc.be.model.DataTypeDefinition;
import org.openecomp.sdc.be.model.PropertyDefinition;

import javax.json.Json;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.*;
import org.junit.Assert;

public class DataTypePropertyConverterTest {

	private static final String EMPTY_JSON_STR = "{}";
	public static final String PROPERTY2_DEFAULT = "{\"prop1\":\"def1\",\"prop3\":\"def3\"}";
	private DataTypePropertyConverter testInstance = DataTypePropertyConverter.getInstance();
	private Map<String, DataTypeDefinition> dataTypes;
	private DataTypeDefinition noDefaultValue, dataType1, dataType2, dataType3;
	private PropertyDefinition prop1, prop2, prop3, noDefaultProp;

	@Before
	public void setUp() throws Exception {
		dataTypes = new HashMap<>();

		prop1 = new PropertyDefinition();
		prop1.setDefaultValue("def1");
		prop1.setName("prop1");

		prop2 = new PropertyDefinition();
		prop2.setType("dataType1");
		prop2.setName("prop2");

		prop3 = new PropertyDefinition();
		prop3.setDefaultValue("def3");
		prop3.setName("prop3");

		noDefaultProp = new PropertyDefinition();
		noDefaultProp.setName("noDefaultProp");

		noDefaultValue = new DataTypeDefinition();
		noDefaultValue.setProperties(Collections.singletonList(noDefaultProp));

		dataType1 = new DataTypeDefinition();
		dataType1.setProperties(Arrays.asList(prop1, prop3));

		dataType2 = new DataTypeDefinition();
		dataType2.setDerivedFrom(dataType1);

		dataType3 = new DataTypeDefinition();
		dataType3.setProperties(Collections.singletonList(prop2));
		dataType3.setDerivedFrom(noDefaultValue);

		dataTypes.put("noDefault", noDefaultValue);
		dataTypes.put("dataType1", dataType1);
		dataTypes.put("dataType2", dataType2);
		dataTypes.put("dataType3", dataType3);
	}

	@Test
	public void testGetPropertyDefaultValuesRec_dataTypeNotExist() throws Exception {
		String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("someType", dataTypes);
		assertEquals(EMPTY_JSON_STR, defaultValue);
	}

	@Test
	public void testGetPropertyDefaultValuesRec_NoDefaultValue() throws Exception {
		String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("noDefault", dataTypes);
		assertEquals(EMPTY_JSON_STR, defaultValue);
	}

	@Test
	public void testGetPropertyDefaultValuesRec() throws Exception {
		String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("dataType1", dataTypes);
		assertEquals(PROPERTY2_DEFAULT, defaultValue);
	}

	@Test
	public void testGetPropertyDefaultValuesRec_defaultFromDerivedDataType_derivedDataTypeHasNoDefaults()
			throws Exception {
		dataType2.setDerivedFrom(noDefaultValue);
		String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("dataType2", dataTypes);
		assertEquals(EMPTY_JSON_STR, defaultValue);
	}

	@Test
	public void testGetPropertyDefaultValuesRec_defaultFromDerivedDataType() throws Exception {
		String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("dataType2", dataTypes);
		assertEquals(PROPERTY2_DEFAULT, defaultValue);
	}

	@Test
	public void testGetPropertyDefaultValuesRec_defaultFromDataTypesOfProperties_dataTypeOfPropertyHasNoDefault()
			throws Exception {
		dataType3.getProperties().get(0).setType(noDefaultValue.getName());
		String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("dataType3", dataTypes);
		assertEquals(EMPTY_JSON_STR, defaultValue);
	}

	@Test
	public void testGetPropertyDefaultValuesRec_defaultFromDataTypesOfProperties() throws Exception {
		String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("dataType3", dataTypes);
		assertEquals("{\"prop2\":" + PROPERTY2_DEFAULT + "}", defaultValue);// data
																			// type
																			// 3
																			// has
																			// property
																			// prop2
																			// which
																			// has
																			// a
																			// data
																			// type
																			// with
																			// property
																			// prop1
																			// which
																			// has
																			// a
																			// default
																			// value
	}

	@Test
	public void testMergeDefaultValues_allDefaultValuesAreOverridden() throws Exception {
		JsonObject value = new JsonObject();
		value.addProperty(noDefaultProp.getName(), "override1");

		JsonObject prop1Val = new JsonObject();
		prop1Val.addProperty(prop1.getName(), "prop1Override");

		JsonObject prop3Val = new JsonObject();
		prop3Val.addProperty(prop3.getName(), "prop3Override");

		JsonObject prop2Value = new JsonObject();
		prop2Value.add(prop3.getName(), prop3Val);
		prop2Value.add(prop1.getName(), prop1Val);

		value.add(prop2.getName(), prop2Value);

		String valBeforeMerge = value.toString();

		testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes);
		assertEquals(valBeforeMerge, value.toString());
	}

	@Test
	public void testMergeDefaultValues() throws Exception {
		JsonObject value = new JsonObject();
		value.addProperty(noDefaultProp.getName(), "override1");

		JsonObject prop1Val = new JsonObject();
		prop1Val.addProperty(prop1.getName(), "prop1Override");

		value.add(prop2.getName(), prop1Val);

		testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes);

		assertEquals("{\"noDefaultProp\":\"override1\",\"prop2\":{\"prop1\":\"prop1Override\",\"prop3\":\"def3\"}}",
				value.toString());// expect to merge prop 3 default as it was
									// not overridden
	}

	@Test
	public void testMergeDefaultValues_mergeAll() throws Exception {
		JsonObject value = new JsonObject();
		testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes);

		assertEquals("{\"prop2\":" + PROPERTY2_DEFAULT + "}", value.toString());// expect
																				// to
																				// merge
																				// prop
																				// 3
																				// default
																				// as
																				// it
																				// was
																				// not
																				// overridden
	}

	@Test
	public void testMergeDefaultValues_doNotAddDefaultsForGetInputValues() throws Exception {

		JsonObject getInputValue = new JsonObject();
		getInputValue.addProperty("get_input", "in1");

		JsonObject value = new JsonObject();
		value.add(prop2.getName(), getInputValue);

		testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes);

		assertEquals("{\"prop2\":{\"get_input\":\"in1\"}}", value.toString());
	}

	@Test
	public void testMergeDefaultValues_doNotAddDefaultsForGetInputInnerValues() throws Exception {
		JsonObject getInputValue = new JsonObject();
		getInputValue.addProperty("get_input", "in1");

		JsonObject prop1Val = new JsonObject();
		prop1Val.add(prop1.getName(), getInputValue);

		JsonObject value = new JsonObject();
		value.add(prop2.getName(), prop1Val);

		testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes);

		assertEquals("{\"prop2\":{\"prop1\":{\"get_input\":\"in1\"},\"prop3\":\"def3\"}}", value.toString());

	}

	private DataTypePropertyConverter createTestSubject() {
		return DataTypePropertyConverter.getInstance();
	}

	
	@Test
	public void testGetInstance() throws Exception {
		DataTypePropertyConverter result;

		// default test
		result = DataTypePropertyConverter.getInstance();
	}

	


	

}