2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.utils;
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotEquals;
28 import static org.junit.Assert.assertTrue;
30 import org.junit.Test;
31 import org.onap.policy.models.base.PfConceptKey;
32 import org.onap.policy.models.base.PfKey;
33 import org.onap.policy.models.base.PfValidationResult;
34 import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType;
35 import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes;
36 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes;
38 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
39 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
42 * Import the {@link ToscaUtils} class.
44 * @author Liam Fallon (liam.fallon@est.tech)
46 public class ToscaUtilsTest {
49 public void testAssertDataTypes() {
50 JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
52 assertFalse(ToscaUtils.doDataTypesExist(jpaToscaServiceTemplate));
53 assertEquals("no data types specified on service template",
54 ToscaUtils.checkDataTypesExist(jpaToscaServiceTemplate));
55 assertThatThrownBy(() -> {
56 ToscaUtils.assertDataTypesExist(jpaToscaServiceTemplate);
57 }).hasMessage("no data types specified on service template");
59 jpaToscaServiceTemplate.setDataTypes(new JpaToscaDataTypes());
61 assertFalse(ToscaUtils.doDataTypesExist(jpaToscaServiceTemplate));
62 assertEquals("list of data types specified on service template is empty",
63 ToscaUtils.checkDataTypesExist(jpaToscaServiceTemplate));
64 assertThatThrownBy(() -> {
65 ToscaUtils.assertDataTypesExist(jpaToscaServiceTemplate);
66 }).hasMessage("list of data types specified on service template is empty");
68 jpaToscaServiceTemplate.getDataTypes().getConceptMap().put(new PfConceptKey(), null);
70 assertTrue(ToscaUtils.doDataTypesExist(jpaToscaServiceTemplate));
71 assertEquals(null, ToscaUtils.checkDataTypesExist(jpaToscaServiceTemplate));
72 assertThatCode(() -> {
73 ToscaUtils.assertDataTypesExist(jpaToscaServiceTemplate);
74 }).doesNotThrowAnyException();
79 public void testAssertPolicyTypes() {
80 JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
82 assertFalse(ToscaUtils.doPolicyTypesExist(jpaToscaServiceTemplate));
83 assertEquals("no policy types specified on service template",
84 ToscaUtils.checkPolicyTypesExist(jpaToscaServiceTemplate));
85 assertThatThrownBy(() -> {
86 ToscaUtils.assertPolicyTypesExist(jpaToscaServiceTemplate);
87 }).hasMessage("no policy types specified on service template");
89 jpaToscaServiceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
91 assertFalse(ToscaUtils.doPolicyTypesExist(jpaToscaServiceTemplate));
92 assertEquals("list of policy types specified on service template is empty",
93 ToscaUtils.checkPolicyTypesExist(jpaToscaServiceTemplate));
94 assertThatThrownBy(() -> {
95 ToscaUtils.assertPolicyTypesExist(jpaToscaServiceTemplate);
96 }).hasMessage("list of policy types specified on service template is empty");
98 jpaToscaServiceTemplate.getPolicyTypes().getConceptMap().put(new PfConceptKey(), null);
100 assertTrue(ToscaUtils.doPolicyTypesExist(jpaToscaServiceTemplate));
101 assertEquals(null, ToscaUtils.checkPolicyTypesExist(jpaToscaServiceTemplate));
102 assertThatCode(() -> {
103 ToscaUtils.assertPolicyTypesExist(jpaToscaServiceTemplate);
104 }).doesNotThrowAnyException();
108 public void testAssertPolicies() {
109 JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
111 assertFalse(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate));
112 assertEquals("topology template not specified on service template",
113 ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate));
114 assertThatThrownBy(() -> {
115 ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate);
116 }).hasMessage("topology template not specified on service template");
118 jpaToscaServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
120 assertFalse(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate));
121 assertEquals("no policies specified on topology template of service template",
122 ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate));
123 assertThatThrownBy(() -> {
124 ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate);
125 }).hasMessage("no policies specified on topology template of service template");
127 jpaToscaServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
129 assertFalse(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate));
130 assertEquals("list of policies specified on topology template of service template is empty",
131 ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate));
132 assertThatThrownBy(() -> {
133 ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate);
134 }).hasMessage("list of policies specified on topology template of service template is empty");
136 jpaToscaServiceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(new PfConceptKey(), null);
138 assertTrue(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate));
139 assertEquals(null, ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate));
140 assertThatCode(() -> {
141 ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate);
142 }).doesNotThrowAnyException();
146 public void testGetentityTypeAncestors() {
147 assertThatThrownBy(() -> {
148 ToscaUtils.getEntityTypeAncestors(null, null, null);
149 }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
151 assertThatThrownBy(() -> {
152 ToscaUtils.getEntityTypeAncestors(null, null, new PfValidationResult());
153 }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
155 assertThatThrownBy(() -> {
156 ToscaUtils.getEntityTypeAncestors(null, new JpaToscaDataType(), null);
157 }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
159 assertThatThrownBy(() -> {
160 ToscaUtils.getEntityTypeAncestors(null, new JpaToscaDataType(), new PfValidationResult());
161 }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
163 assertThatThrownBy(() -> {
164 ToscaUtils.getEntityTypeAncestors(new JpaToscaDataTypes(), null, null);
165 }).hasMessageMatching("entityType is marked .*on.*ull but is null");
167 assertThatThrownBy(() -> {
168 ToscaUtils.getEntityTypeAncestors(new JpaToscaDataTypes(), null, new PfValidationResult());
169 }).hasMessageMatching("entityType is marked .*on.*ull but is null");
171 assertThatThrownBy(() -> {
172 ToscaUtils.getEntityTypeAncestors(new JpaToscaDataTypes(), new JpaToscaDataType(), null);
173 }).hasMessageMatching("result is marked .*on.*ull but is null");
175 JpaToscaDataTypes dataTypes = new JpaToscaDataTypes();
176 JpaToscaDataType dt0 = new JpaToscaDataType();
177 dt0.setKey(new PfConceptKey("dt0", "0.0.1"));
178 dt0.setDescription("dt0 description");
179 PfValidationResult result = new PfValidationResult();
181 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
183 dataTypes.getConceptMap().put(dt0.getKey(), dt0);
184 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
185 assertTrue(result.isValid());
187 dt0.setDerivedFrom(null);
188 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
189 assertTrue(result.isValid());
191 dt0.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION));
192 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
193 assertTrue(result.isValid());
195 dt0.setDerivedFrom(new PfConceptKey("some.thing.Else", PfKey.NULL_KEY_VERSION));
196 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
197 assertFalse(result.isValid());
198 assertTrue(result.toString().contains("parent some.thing.Else:0.0.0 of entity not found"));
200 result = new PfValidationResult();
201 dt0.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION));
203 JpaToscaDataType dt1 = new JpaToscaDataType();
204 dt1.setKey(new PfConceptKey("dt1", "0.0.1"));
205 dt1.setDescription("dt1 description");
206 dataTypes.getConceptMap().put(dt1.getKey(), dt1);
207 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
208 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).isEmpty());
209 assertTrue(result.isValid());
211 dt1.setDerivedFrom(dt0.getKey());
212 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
213 assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).isEmpty());
214 assertEquals(1, ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).size());
215 assertTrue(result.isValid());
217 JpaToscaDataType dt2 = new JpaToscaDataType();
218 dt2.setKey(new PfConceptKey("dt2", "0.0.1"));
219 dt2.setDescription("dt2 description");
220 dataTypes.getConceptMap().put(dt2.getKey(), dt2);
221 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
222 assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).isEmpty());
223 assertEquals(1, ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).size());
224 assertTrue(result.isValid());
226 dt2.setDerivedFrom(dt1.getKey());
227 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
228 assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).isEmpty());
229 assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).isEmpty());
230 assertEquals(1, ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).size());
231 assertEquals(2, ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).size());
232 assertTrue(result.isValid());
234 dt1.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION));
235 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
236 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).isEmpty());
237 assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).isEmpty());
238 assertEquals(0, ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).size());
239 assertEquals(1, ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).size());
240 assertTrue(result.isValid());
242 dataTypes.getConceptMap().remove(dt1.getKey());
243 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).isEmpty());
244 assertFalse(result.isValid());
245 assertTrue(result.toString().contains("parent dt1:0.0.1 of entity not found"));
249 public void testGetPredefinedDataTypes() {
250 assertTrue(ToscaUtils.getPredefinedDataTypes().contains(new PfConceptKey("string", PfKey.NULL_KEY_VERSION)));
254 public void testgetEntityTree() {
255 assertThatThrownBy(() -> {
256 ToscaUtils.getEntityTree(null, null);
257 }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
259 assertThatThrownBy(() -> {
260 ToscaUtils.getEntityTree(null, new PfConceptKey());
261 }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
263 assertThatThrownBy(() -> {
264 ToscaUtils.getEntityTree(new JpaToscaDataTypes(), null);
265 }).hasMessageMatching("searchKey is marked .*on.*ull but is null");
267 JpaToscaDataTypes dataTypes = new JpaToscaDataTypes(new PfConceptKey("datatypes", "0.0.1"));
268 JpaToscaDataTypes filteredDataTypes = new JpaToscaDataTypes(new PfConceptKey("datatypes", "0.0.1"));
269 ToscaUtils.getEntityTree(filteredDataTypes, new PfConceptKey());
270 assertEquals(dataTypes, filteredDataTypes);
272 JpaToscaDataType dt0 = new JpaToscaDataType(new PfConceptKey("dt0", "0.0.1"));
273 dataTypes.getConceptMap().put(dt0.getKey(), dt0);
274 filteredDataTypes.getConceptMap().put(dt0.getKey(), dt0);
275 ToscaUtils.getEntityTree(filteredDataTypes, new PfConceptKey());
276 assertNotEquals(dataTypes, filteredDataTypes);
277 assertTrue(filteredDataTypes.getConceptMap().isEmpty());
279 filteredDataTypes.getConceptMap().put(dt0.getKey(), dt0);
280 ToscaUtils.getEntityTree(filteredDataTypes, dt0.getKey());
281 assertEquals(dataTypes, filteredDataTypes);
283 JpaToscaDataType dt1 = new JpaToscaDataType(new PfConceptKey("dt1", "0.0.1"));
284 dt1.setDerivedFrom(dt0.getKey());
286 JpaToscaDataType dt2 = new JpaToscaDataType(new PfConceptKey("dt2", "0.0.1"));
287 dt2.setDerivedFrom(dt0.getKey());
289 JpaToscaDataType dt3 = new JpaToscaDataType(new PfConceptKey("dt3", "0.0.1"));
290 dt3.setDerivedFrom(dt0.getKey());
292 JpaToscaDataType dt4 = new JpaToscaDataType(new PfConceptKey("dt4", "0.0.1"));
293 dt4.setDerivedFrom(dt3.getKey());
295 JpaToscaDataType dt5 = new JpaToscaDataType(new PfConceptKey("dt5", "0.0.1"));
296 dt5.setDerivedFrom(dt4.getKey());
298 JpaToscaDataType dt6 = new JpaToscaDataType(new PfConceptKey("dt6", "0.0.1"));
299 dt6.setDerivedFrom(dt5.getKey());
301 JpaToscaDataType dt7 = new JpaToscaDataType(new PfConceptKey("dt7", "0.0.1"));
303 JpaToscaDataType dt8 = new JpaToscaDataType(new PfConceptKey("dt8", "0.0.1"));
304 dt8.setDerivedFrom(dt7.getKey());
306 JpaToscaDataType dt9 = new JpaToscaDataType(new PfConceptKey("dt9", "0.0.1"));
307 dt9.setDerivedFrom(dt8.getKey());
309 dataTypes.getConceptMap().put(dt0.getKey(), dt0);
310 dataTypes.getConceptMap().put(dt1.getKey(), dt1);
311 dataTypes.getConceptMap().put(dt2.getKey(), dt2);
312 dataTypes.getConceptMap().put(dt3.getKey(), dt3);
313 dataTypes.getConceptMap().put(dt4.getKey(), dt4);
314 dataTypes.getConceptMap().put(dt5.getKey(), dt5);
315 dataTypes.getConceptMap().put(dt6.getKey(), dt6);
316 dataTypes.getConceptMap().put(dt7.getKey(), dt7);
317 dataTypes.getConceptMap().put(dt8.getKey(), dt8);
318 dataTypes.getConceptMap().put(dt9.getKey(), dt9);
320 ToscaUtils.getEntityTree(filteredDataTypes, dt0.getKey());
321 assertEquals(1, filteredDataTypes.getConceptMap().size());
323 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
324 ToscaUtils.getEntityTree(filteredDataTypes, dt1.getKey());
325 assertEquals(2, filteredDataTypes.getConceptMap().size());
327 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
328 ToscaUtils.getEntityTree(filteredDataTypes, dt2.getKey());
329 assertEquals(2, filteredDataTypes.getConceptMap().size());
331 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
332 ToscaUtils.getEntityTree(filteredDataTypes, dt3.getKey());
333 assertEquals(2, filteredDataTypes.getConceptMap().size());
335 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
336 ToscaUtils.getEntityTree(filteredDataTypes, dt4.getKey());
337 assertEquals(3, filteredDataTypes.getConceptMap().size());
339 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
340 ToscaUtils.getEntityTree(filteredDataTypes, dt5.getKey());
341 assertEquals(4, filteredDataTypes.getConceptMap().size());
343 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
344 ToscaUtils.getEntityTree(filteredDataTypes, dt6.getKey());
345 assertEquals(5, filteredDataTypes.getConceptMap().size());
346 assertTrue(filteredDataTypes.getConceptMap().containsValue(dt0));
347 assertFalse(filteredDataTypes.getConceptMap().containsValue(dt1));
348 assertFalse(filteredDataTypes.getConceptMap().containsValue(dt2));
349 assertTrue(filteredDataTypes.getConceptMap().containsValue(dt3));
350 assertTrue(filteredDataTypes.getConceptMap().containsValue(dt4));
351 assertTrue(filteredDataTypes.getConceptMap().containsValue(dt5));
352 assertTrue(filteredDataTypes.getConceptMap().containsValue(dt6));
354 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
355 ToscaUtils.getEntityTree(filteredDataTypes, dt7.getKey());
356 assertEquals(1, filteredDataTypes.getConceptMap().size());
358 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
359 ToscaUtils.getEntityTree(filteredDataTypes, dt8.getKey());
360 assertEquals(2, filteredDataTypes.getConceptMap().size());
362 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
363 ToscaUtils.getEntityTree(filteredDataTypes, dt9.getKey());
364 assertEquals(3, filteredDataTypes.getConceptMap().size());
366 dt9.setDerivedFrom(new PfConceptKey("i.dont.Exist", "0.0.0"));
367 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
369 assertThatThrownBy(() -> {
370 final JpaToscaDataTypes badDataTypes = new JpaToscaDataTypes(dataTypes);
371 ToscaUtils.getEntityTree(badDataTypes, dt9.getKey());
372 }).hasMessageContaining("parent i.dont.Exist:0.0.0 of entity not found");