2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021, 2025 Nordix Foundation.
4 * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
5 * Modifications Copyright (C) 2024 Nordix Foundation
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.models.tosca.utils;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.assertj.core.api.Assertions.assertThatCode;
27 import static org.assertj.core.api.Assertions.assertThatThrownBy;
28 import static org.junit.jupiter.api.Assertions.assertEquals;
29 import static org.junit.jupiter.api.Assertions.assertFalse;
30 import static org.junit.jupiter.api.Assertions.assertNotEquals;
31 import static org.junit.jupiter.api.Assertions.assertNull;
32 import static org.junit.jupiter.api.Assertions.assertTrue;
34 import org.junit.jupiter.api.Test;
35 import org.onap.policy.common.parameters.BeanValidationResult;
36 import org.onap.policy.models.base.PfConceptKey;
37 import org.onap.policy.models.base.PfKey;
38 import org.onap.policy.models.base.Validated;
39 import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType;
40 import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes;
41 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
42 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes;
43 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
44 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
47 * Import the {@link ToscaUtils} class.
49 * @author Liam Fallon (liam.fallon@est.tech)
51 class ToscaUtilsTest {
54 void testAssertDataTypes() {
55 JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
57 assertFalse(ToscaUtils.doDataTypesExist(jpaToscaServiceTemplate));
58 assertEquals("no data types specified on service template",
59 ToscaUtils.checkDataTypesExist(jpaToscaServiceTemplate));
60 assertThatThrownBy(() -> {
61 ToscaUtils.assertDataTypesExist(jpaToscaServiceTemplate);
62 }).hasMessage("no data types specified on service template");
64 jpaToscaServiceTemplate.setDataTypes(new JpaToscaDataTypes());
66 assertFalse(ToscaUtils.doDataTypesExist(jpaToscaServiceTemplate));
67 assertEquals("list of data types specified on service template is empty",
68 ToscaUtils.checkDataTypesExist(jpaToscaServiceTemplate));
69 assertThatThrownBy(() -> {
70 ToscaUtils.assertDataTypesExist(jpaToscaServiceTemplate);
71 }).hasMessage("list of data types specified on service template is empty");
73 jpaToscaServiceTemplate.getDataTypes().getConceptMap().put(new PfConceptKey(), null);
75 assertTrue(ToscaUtils.doDataTypesExist(jpaToscaServiceTemplate));
76 assertNull(ToscaUtils.checkDataTypesExist(jpaToscaServiceTemplate));
77 assertThatCode(() -> ToscaUtils.assertDataTypesExist(jpaToscaServiceTemplate)).doesNotThrowAnyException();
82 void testAssertPolicyTypes() {
83 JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
85 assertFalse(ToscaUtils.doPolicyTypesExist(jpaToscaServiceTemplate));
86 assertEquals("no policy types specified on service template",
87 ToscaUtils.checkPolicyTypesExist(jpaToscaServiceTemplate));
88 assertThatThrownBy(() -> {
89 ToscaUtils.assertPolicyTypesExist(jpaToscaServiceTemplate);
90 }).hasMessage("no policy types specified on service template");
92 jpaToscaServiceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
94 assertFalse(ToscaUtils.doPolicyTypesExist(jpaToscaServiceTemplate));
95 assertEquals("list of policy types specified on service template is empty",
96 ToscaUtils.checkPolicyTypesExist(jpaToscaServiceTemplate));
97 assertThatThrownBy(() -> {
98 ToscaUtils.assertPolicyTypesExist(jpaToscaServiceTemplate);
99 }).hasMessage("list of policy types specified on service template is empty");
101 jpaToscaServiceTemplate.getPolicyTypes().getConceptMap().put(new PfConceptKey(), null);
103 assertTrue(ToscaUtils.doPolicyTypesExist(jpaToscaServiceTemplate));
104 assertNull(ToscaUtils.checkPolicyTypesExist(jpaToscaServiceTemplate));
105 assertThatCode(() -> ToscaUtils.assertPolicyTypesExist(jpaToscaServiceTemplate)).doesNotThrowAnyException();
109 void testAssertPolicies() {
110 JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
112 assertFalse(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate));
113 assertEquals("topology template not specified on service template",
114 ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate));
115 assertThatThrownBy(() -> {
116 ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate);
117 }).hasMessage("topology template not specified on service template");
119 jpaToscaServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
121 assertFalse(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate));
122 assertEquals("no policies specified on topology template of service template",
123 ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate));
124 assertThatThrownBy(() -> {
125 ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate);
126 }).hasMessage("no policies specified on topology template of service template");
128 jpaToscaServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
130 assertFalse(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate));
131 assertEquals("list of policies specified on topology template of service template is empty",
132 ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate));
133 assertThatThrownBy(() -> {
134 ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate);
135 }).hasMessage("list of policies specified on topology template of service template is empty");
137 jpaToscaServiceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(new PfConceptKey(), null);
139 assertTrue(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate));
140 assertNull(ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate));
141 assertThatCode(() -> ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate)).doesNotThrowAnyException();
145 void testGetEntityTypeAncestors() {
146 assertThatThrownBy(() -> {
147 ToscaUtils.getEntityTypeAncestors(null, null, null);
148 }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
150 assertThatThrownBy(() -> {
151 ToscaUtils.getEntityTypeAncestors(null, null, new BeanValidationResult("", null));
152 }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
154 assertThatThrownBy(() -> {
155 ToscaUtils.getEntityTypeAncestors(null, new JpaToscaDataType(), null);
156 }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
158 assertThatThrownBy(() -> {
159 ToscaUtils.getEntityTypeAncestors(null, new JpaToscaDataType(), new BeanValidationResult("", null));
160 }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
162 assertThatThrownBy(() -> {
163 ToscaUtils.getEntityTypeAncestors(new JpaToscaDataTypes(), null, null);
164 }).hasMessageMatching("entityType is marked .*on.*ull but is null");
166 assertThatThrownBy(() -> {
167 ToscaUtils.getEntityTypeAncestors(new JpaToscaDataTypes(), null, new BeanValidationResult("", null));
168 }).hasMessageMatching("entityType is marked .*on.*ull but is null");
170 assertThatThrownBy(() -> {
171 ToscaUtils.getEntityTypeAncestors(new JpaToscaDataTypes(), new JpaToscaDataType(), null);
172 }).hasMessageMatching("result is marked .*on.*ull but is null");
176 void testGetentityTypeAncestorsDataType() {
178 JpaToscaDataTypes dataTypes = new JpaToscaDataTypes();
179 JpaToscaDataType dt0 = new JpaToscaDataType();
180 dt0.setKey(new PfConceptKey("dt0", "0.0.1"));
181 dt0.setDescription("dt0 description");
182 BeanValidationResult result = new BeanValidationResult("", null);
184 assertThat(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result)).isEmpty();
186 dataTypes.getConceptMap().put(dt0.getKey(), dt0);
187 checkSingleEmptyEntityTypeAncestor(dataTypes, dt0, result);
189 dt0.setDerivedFrom(null);
190 checkSingleEmptyEntityTypeAncestor(dataTypes, dt0, result);
192 dt0.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION));
193 checkSingleEmptyEntityTypeAncestor(dataTypes, dt0, result);
195 dt0.setDerivedFrom(new PfConceptKey("some.thing.Else", PfKey.NULL_KEY_VERSION));
196 assertThat(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result)).isEmpty();
197 assertFalse(result.isValid());
198 assertThat(result.getResult()).contains("parent").contains("some.thing.Else:0.0.0")
199 .contains(Validated.NOT_FOUND);
201 result = new BeanValidationResult("", null);
202 dt0.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION));
204 JpaToscaDataType dt1 = new JpaToscaDataType();
205 dt1.setKey(new PfConceptKey("dt1", "0.0.1"));
206 dt1.setDescription("dt1 description");
207 dataTypes.getConceptMap().put(dt1.getKey(), dt1);
208 checkSingleEmptyEntityTypeAncestor(dataTypes, dt0, result);
209 checkSingleEmptyEntityTypeAncestor(dataTypes, dt1, result);
211 dt1.setDerivedFrom(dt0.getKey());
212 checkMultipleEmptyEntityTypeAncestors(dataTypes, dt0, dt1, result, 1);
214 JpaToscaDataType dt2 = new JpaToscaDataType();
215 dt2.setKey(new PfConceptKey("dt2", "0.0.1"));
216 dt2.setDescription("dt2 description");
217 dataTypes.getConceptMap().put(dt2.getKey(), dt2);
218 checkMultipleEmptyEntityTypeAncestors(dataTypes, dt0, dt1, result, 1);
220 dt2.setDerivedFrom(dt1.getKey());
221 checkMultipleEmptyEntityTypeAncestors(dataTypes, dt0, dt1, result, 1);
222 checkMultipleEmptyEntityTypeAncestors(dataTypes, dt0, dt2, result, 2);
224 dt0.setDerivedFrom(dt0.getKey());
225 assertThatThrownBy(() -> {
226 ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, new BeanValidationResult("", null));
227 }).hasMessageContaining("entity type").hasMessageContaining("ancestor of itself");
229 dt0.setDerivedFrom(null);
230 assertEquals(2, ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).size());
232 dt1.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION));
233 checkSingleEmptyEntityTypeAncestor(dataTypes, dt0, result);
234 checkMultipleEmptyEntityTypeAncestors(dataTypes, dt1, dt2, result);
236 dataTypes.getConceptMap().remove(dt1.getKey());
237 assertThat(ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result)).isEmpty();
238 assertFalse(result.isValid());
239 assertThat(result.getResult()).contains("parent").contains("dt1:0.0.1").contains(Validated.NOT_FOUND);
242 private void checkSingleEmptyEntityTypeAncestor(JpaToscaDataTypes dataTypes, JpaToscaDataType emptydt,
243 BeanValidationResult result) {
244 assertThat(ToscaUtils.getEntityTypeAncestors(dataTypes, emptydt, result)).isEmpty();
245 assertTrue(result.isValid());
248 private void checkMultipleEmptyEntityTypeAncestors(JpaToscaDataTypes dataTypes, JpaToscaDataType emptydt,
249 JpaToscaDataType notemptydt, BeanValidationResult result, int size1) {
250 assertThat(ToscaUtils.getEntityTypeAncestors(dataTypes, emptydt, result)).isEmpty();
251 assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, notemptydt, result).isEmpty());
252 assertEquals(size1, ToscaUtils.getEntityTypeAncestors(dataTypes, notemptydt, result).size());
253 assertTrue(result.isValid());
256 private void checkMultipleEmptyEntityTypeAncestors(JpaToscaDataTypes dataTypes, JpaToscaDataType emptydt,
257 JpaToscaDataType notemptydt, BeanValidationResult result) {
258 assertThat(ToscaUtils.getEntityTypeAncestors(dataTypes, emptydt, result)).isEmpty();
259 assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, notemptydt, result).isEmpty());
260 assertEquals(1, ToscaUtils.getEntityTypeAncestors(dataTypes, notemptydt, result).size());
261 assertEquals(0, ToscaUtils.getEntityTypeAncestors(dataTypes, emptydt, result).size());
262 assertTrue(result.isValid());
266 void testGetPredefinedDataTypes() {
267 assertTrue(ToscaUtils.getPredefinedDataTypes().contains(new PfConceptKey("string", PfKey.NULL_KEY_VERSION)));
271 void testGetEntityTree() {
272 assertThatThrownBy(() -> {
273 ToscaUtils.getEntityTree(null, null, null);
274 }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
276 JpaToscaDataTypes dataTypes = new JpaToscaDataTypes(new PfConceptKey("datatypes", "0.0.1"));
277 JpaToscaDataTypes filteredDataTypes = new JpaToscaDataTypes(new PfConceptKey("datatypes", "0.0.1"));
278 ToscaUtils.getEntityTree(filteredDataTypes, "IDontExist", "0.0.0");
279 assertEquals(dataTypes, filteredDataTypes);
281 JpaToscaDataType dt0 = new JpaToscaDataType(new PfConceptKey("dt0", "0.0.1"));
282 dataTypes.getConceptMap().put(dt0.getKey(), dt0);
283 filteredDataTypes.getConceptMap().put(dt0.getKey(), dt0);
284 ToscaUtils.getEntityTree(filteredDataTypes, "IDontExist", "0.0.0");
285 assertNotEquals(dataTypes, filteredDataTypes);
286 assertThat(filteredDataTypes.getConceptMap()).isEmpty();
288 filteredDataTypes.getConceptMap().put(dt0.getKey(), dt0);
289 ToscaUtils.getEntityTree(filteredDataTypes, dt0.getKey().getName(), dt0.getKey().getVersion());
290 assertEquals(dataTypes, filteredDataTypes);
292 JpaToscaDataType dt1 = new JpaToscaDataType(new PfConceptKey("dt1", "0.0.1"));
293 dt1.setDerivedFrom(dt0.getKey());
295 JpaToscaDataType dt2 = new JpaToscaDataType(new PfConceptKey("dt2", "0.0.1"));
296 dt2.setDerivedFrom(dt0.getKey());
298 JpaToscaDataType dt3 = new JpaToscaDataType(new PfConceptKey("dt3", "0.0.1"));
299 dt3.setDerivedFrom(dt0.getKey());
301 JpaToscaDataType dt4 = new JpaToscaDataType(new PfConceptKey("dt4", "0.0.1"));
302 dt4.setDerivedFrom(dt3.getKey());
304 JpaToscaDataType dt5 = new JpaToscaDataType(new PfConceptKey("dt5", "0.0.1"));
305 dt5.setDerivedFrom(dt4.getKey());
307 JpaToscaDataType dt6 = new JpaToscaDataType(new PfConceptKey("dt6", "0.0.1"));
308 dt6.setDerivedFrom(dt5.getKey());
310 JpaToscaDataType dt7 = new JpaToscaDataType(new PfConceptKey("dt7", "0.0.1"));
312 JpaToscaDataType dt8 = new JpaToscaDataType(new PfConceptKey("dt8", "0.0.1"));
313 dt8.setDerivedFrom(dt7.getKey());
315 JpaToscaDataType dt9 = new JpaToscaDataType(new PfConceptKey("dt9", "0.0.1"));
316 dt9.setDerivedFrom(dt8.getKey());
318 dataTypes.getConceptMap().put(dt0.getKey(), dt0);
319 dataTypes.getConceptMap().put(dt1.getKey(), dt1);
320 dataTypes.getConceptMap().put(dt2.getKey(), dt2);
321 dataTypes.getConceptMap().put(dt3.getKey(), dt3);
322 dataTypes.getConceptMap().put(dt4.getKey(), dt4);
323 dataTypes.getConceptMap().put(dt5.getKey(), dt5);
324 dataTypes.getConceptMap().put(dt6.getKey(), dt6);
325 dataTypes.getConceptMap().put(dt7.getKey(), dt7);
326 dataTypes.getConceptMap().put(dt8.getKey(), dt8);
327 dataTypes.getConceptMap().put(dt9.getKey(), dt9);
329 ToscaUtils.getEntityTree(filteredDataTypes, dt0.getKey().getName(), dt0.getKey().getVersion());
330 assertEquals(1, filteredDataTypes.getConceptMap().size());
332 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
333 ToscaUtils.getEntityTree(filteredDataTypes, dt1.getKey().getName(), dt1.getKey().getVersion());
334 assertEquals(2, filteredDataTypes.getConceptMap().size());
336 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
337 ToscaUtils.getEntityTree(filteredDataTypes, dt2.getKey().getName(), dt2.getKey().getVersion());
338 assertEquals(2, filteredDataTypes.getConceptMap().size());
340 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
341 ToscaUtils.getEntityTree(filteredDataTypes, dt3.getKey().getName(), dt3.getKey().getVersion());
342 assertEquals(2, filteredDataTypes.getConceptMap().size());
344 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
345 ToscaUtils.getEntityTree(filteredDataTypes, dt4.getKey().getName(), dt4.getKey().getVersion());
346 assertEquals(3, filteredDataTypes.getConceptMap().size());
348 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
349 ToscaUtils.getEntityTree(filteredDataTypes, dt5.getKey().getName(), dt5.getKey().getVersion());
350 assertEquals(4, filteredDataTypes.getConceptMap().size());
352 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
353 ToscaUtils.getEntityTree(filteredDataTypes, dt6.getKey().getName(), dt6.getKey().getVersion());
354 assertEquals(5, filteredDataTypes.getConceptMap().size());
355 assertTrue(filteredDataTypes.getConceptMap().containsValue(dt0));
356 assertFalse(filteredDataTypes.getConceptMap().containsValue(dt1));
357 assertFalse(filteredDataTypes.getConceptMap().containsValue(dt2));
358 assertTrue(filteredDataTypes.getConceptMap().containsValue(dt3));
359 assertTrue(filteredDataTypes.getConceptMap().containsValue(dt4));
360 assertTrue(filteredDataTypes.getConceptMap().containsValue(dt5));
361 assertTrue(filteredDataTypes.getConceptMap().containsValue(dt6));
363 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
364 ToscaUtils.getEntityTree(filteredDataTypes, dt7.getKey().getName(), dt7.getKey().getVersion());
365 assertEquals(1, filteredDataTypes.getConceptMap().size());
367 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
368 ToscaUtils.getEntityTree(filteredDataTypes, dt8.getKey().getName(), dt8.getKey().getVersion());
369 assertEquals(2, filteredDataTypes.getConceptMap().size());
371 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
372 ToscaUtils.getEntityTree(filteredDataTypes, dt9.getKey().getName(), dt9.getKey().getVersion());
373 assertEquals(3, filteredDataTypes.getConceptMap().size());
375 dt9.setDerivedFrom(new PfConceptKey("i.dont.Exist", "0.0.0"));
377 assertThatThrownBy(() -> {
378 final JpaToscaDataTypes badDataTypes = new JpaToscaDataTypes(dataTypes);
379 ToscaUtils.getEntityTree(badDataTypes, dt9.getKey().getName(), dt9.getKey().getVersion());
380 }).hasMessageContaining("parent").hasMessageContaining("i.dont.Exist:0.0.0")
381 .hasMessageContaining(Validated.NOT_FOUND);