2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.utils;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatCode;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertFalse;
29 import static org.junit.Assert.assertNotEquals;
30 import static org.junit.Assert.assertTrue;
32 import org.junit.Test;
33 import org.onap.policy.common.parameters.BeanValidationResult;
34 import org.onap.policy.models.base.PfConceptKey;
35 import org.onap.policy.models.base.PfKey;
36 import org.onap.policy.models.base.Validated;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType;
38 import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes;
39 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
40 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes;
41 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
42 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
45 * Import the {@link ToscaUtils} class.
47 * @author Liam Fallon (liam.fallon@est.tech)
49 public class ToscaUtilsTest {
52 public void testAssertDataTypes() {
53 JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
55 assertFalse(ToscaUtils.doDataTypesExist(jpaToscaServiceTemplate));
56 assertEquals("no data types specified on service template",
57 ToscaUtils.checkDataTypesExist(jpaToscaServiceTemplate));
58 assertThatThrownBy(() -> {
59 ToscaUtils.assertDataTypesExist(jpaToscaServiceTemplate);
60 }).hasMessage("no data types specified on service template");
62 jpaToscaServiceTemplate.setDataTypes(new JpaToscaDataTypes());
64 assertFalse(ToscaUtils.doDataTypesExist(jpaToscaServiceTemplate));
65 assertEquals("list of data types specified on service template is empty",
66 ToscaUtils.checkDataTypesExist(jpaToscaServiceTemplate));
67 assertThatThrownBy(() -> {
68 ToscaUtils.assertDataTypesExist(jpaToscaServiceTemplate);
69 }).hasMessage("list of data types specified on service template is empty");
71 jpaToscaServiceTemplate.getDataTypes().getConceptMap().put(new PfConceptKey(), null);
73 assertTrue(ToscaUtils.doDataTypesExist(jpaToscaServiceTemplate));
74 assertEquals(null, ToscaUtils.checkDataTypesExist(jpaToscaServiceTemplate));
75 assertThatCode(() -> {
76 ToscaUtils.assertDataTypesExist(jpaToscaServiceTemplate);
77 }).doesNotThrowAnyException();
82 public 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 assertEquals(null, ToscaUtils.checkPolicyTypesExist(jpaToscaServiceTemplate));
105 assertThatCode(() -> {
106 ToscaUtils.assertPolicyTypesExist(jpaToscaServiceTemplate);
107 }).doesNotThrowAnyException();
111 public void testAssertPolicies() {
112 JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
114 assertFalse(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate));
115 assertEquals("topology template not specified on service template",
116 ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate));
117 assertThatThrownBy(() -> {
118 ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate);
119 }).hasMessage("topology template not specified on service template");
121 jpaToscaServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
123 assertFalse(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate));
124 assertEquals("no policies specified on topology template of service template",
125 ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate));
126 assertThatThrownBy(() -> {
127 ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate);
128 }).hasMessage("no policies specified on topology template of service template");
130 jpaToscaServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
132 assertFalse(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate));
133 assertEquals("list of policies specified on topology template of service template is empty",
134 ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate));
135 assertThatThrownBy(() -> {
136 ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate);
137 }).hasMessage("list of policies specified on topology template of service template is empty");
139 jpaToscaServiceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(new PfConceptKey(), null);
141 assertTrue(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate));
142 assertEquals(null, ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate));
143 assertThatCode(() -> {
144 ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate);
145 }).doesNotThrowAnyException();
149 public void testGetentityTypeAncestors() {
150 assertThatThrownBy(() -> {
151 ToscaUtils.getEntityTypeAncestors(null, null, null);
152 }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
154 assertThatThrownBy(() -> {
155 ToscaUtils.getEntityTypeAncestors(null, null, new BeanValidationResult("", null));
156 }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
158 assertThatThrownBy(() -> {
159 ToscaUtils.getEntityTypeAncestors(null, new JpaToscaDataType(), null);
160 }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
162 assertThatThrownBy(() -> {
163 ToscaUtils.getEntityTypeAncestors(null, new JpaToscaDataType(), new BeanValidationResult("", null));
164 }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
166 assertThatThrownBy(() -> {
167 ToscaUtils.getEntityTypeAncestors(new JpaToscaDataTypes(), null, null);
168 }).hasMessageMatching("entityType is marked .*on.*ull but is null");
170 assertThatThrownBy(() -> {
171 ToscaUtils.getEntityTypeAncestors(new JpaToscaDataTypes(), null, new BeanValidationResult("", null));
172 }).hasMessageMatching("entityType is marked .*on.*ull but is null");
174 assertThatThrownBy(() -> {
175 ToscaUtils.getEntityTypeAncestors(new JpaToscaDataTypes(), new JpaToscaDataType(), null);
176 }).hasMessageMatching("result is marked .*on.*ull but is null");
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 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
186 dataTypes.getConceptMap().put(dt0.getKey(), dt0);
187 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
188 assertTrue(result.isValid());
190 dt0.setDerivedFrom(null);
191 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
192 assertTrue(result.isValid());
194 dt0.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION));
195 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
196 assertTrue(result.isValid());
198 dt0.setDerivedFrom(new PfConceptKey("some.thing.Else", PfKey.NULL_KEY_VERSION));
199 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
200 assertFalse(result.isValid());
201 assertThat(result.getResult()).contains("parent").contains("some.thing.Else:0.0.0")
202 .contains(Validated.NOT_FOUND);
204 result = new BeanValidationResult("", null);
205 dt0.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION));
207 JpaToscaDataType dt1 = new JpaToscaDataType();
208 dt1.setKey(new PfConceptKey("dt1", "0.0.1"));
209 dt1.setDescription("dt1 description");
210 dataTypes.getConceptMap().put(dt1.getKey(), dt1);
211 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
212 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).isEmpty());
213 assertTrue(result.isValid());
215 dt1.setDerivedFrom(dt0.getKey());
216 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
217 assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).isEmpty());
218 assertEquals(1, ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).size());
219 assertTrue(result.isValid());
221 JpaToscaDataType dt2 = new JpaToscaDataType();
222 dt2.setKey(new PfConceptKey("dt2", "0.0.1"));
223 dt2.setDescription("dt2 description");
224 dataTypes.getConceptMap().put(dt2.getKey(), dt2);
225 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
226 assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).isEmpty());
227 assertEquals(1, ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).size());
228 assertTrue(result.isValid());
230 dt2.setDerivedFrom(dt1.getKey());
231 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
232 assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).isEmpty());
233 assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).isEmpty());
234 assertEquals(1, ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).size());
235 assertEquals(2, ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).size());
236 assertTrue(result.isValid());
238 dt0.setDerivedFrom(dt0.getKey());
239 assertThatThrownBy(() -> {
240 ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, new BeanValidationResult("", null));
241 }).hasMessageContaining("entity type").hasMessageContaining("ancestor of itself");
243 dt0.setDerivedFrom(null);
244 assertEquals(2, ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).size());
246 dt1.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION));
247 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
248 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).isEmpty());
249 assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).isEmpty());
250 assertEquals(0, ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).size());
251 assertEquals(1, ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).size());
252 assertTrue(result.isValid());
254 dataTypes.getConceptMap().remove(dt1.getKey());
255 assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).isEmpty());
256 assertFalse(result.isValid());
257 assertThat(result.getResult()).contains("parent").contains("dt1:0.0.1").contains(Validated.NOT_FOUND);
261 public void testGetPredefinedDataTypes() {
262 assertTrue(ToscaUtils.getPredefinedDataTypes().contains(new PfConceptKey("string", PfKey.NULL_KEY_VERSION)));
266 public void testgetEntityTree() {
267 assertThatThrownBy(() -> {
268 ToscaUtils.getEntityTree(null, null, null);
269 }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
271 JpaToscaDataTypes dataTypes = new JpaToscaDataTypes(new PfConceptKey("datatypes", "0.0.1"));
272 JpaToscaDataTypes filteredDataTypes = new JpaToscaDataTypes(new PfConceptKey("datatypes", "0.0.1"));
273 ToscaUtils.getEntityTree(filteredDataTypes, "IDontExist", "0.0.0");
274 assertEquals(dataTypes, filteredDataTypes);
276 JpaToscaDataType dt0 = new JpaToscaDataType(new PfConceptKey("dt0", "0.0.1"));
277 dataTypes.getConceptMap().put(dt0.getKey(), dt0);
278 filteredDataTypes.getConceptMap().put(dt0.getKey(), dt0);
279 ToscaUtils.getEntityTree(filteredDataTypes, "IDontExist", "0.0.0");
280 assertNotEquals(dataTypes, filteredDataTypes);
281 assertTrue(filteredDataTypes.getConceptMap().isEmpty());
283 filteredDataTypes.getConceptMap().put(dt0.getKey(), dt0);
284 ToscaUtils.getEntityTree(filteredDataTypes, dt0.getKey().getName(), dt0.getKey().getVersion());
285 assertEquals(dataTypes, filteredDataTypes);
287 JpaToscaDataType dt1 = new JpaToscaDataType(new PfConceptKey("dt1", "0.0.1"));
288 dt1.setDerivedFrom(dt0.getKey());
290 JpaToscaDataType dt2 = new JpaToscaDataType(new PfConceptKey("dt2", "0.0.1"));
291 dt2.setDerivedFrom(dt0.getKey());
293 JpaToscaDataType dt3 = new JpaToscaDataType(new PfConceptKey("dt3", "0.0.1"));
294 dt3.setDerivedFrom(dt0.getKey());
296 JpaToscaDataType dt4 = new JpaToscaDataType(new PfConceptKey("dt4", "0.0.1"));
297 dt4.setDerivedFrom(dt3.getKey());
299 JpaToscaDataType dt5 = new JpaToscaDataType(new PfConceptKey("dt5", "0.0.1"));
300 dt5.setDerivedFrom(dt4.getKey());
302 JpaToscaDataType dt6 = new JpaToscaDataType(new PfConceptKey("dt6", "0.0.1"));
303 dt6.setDerivedFrom(dt5.getKey());
305 JpaToscaDataType dt7 = new JpaToscaDataType(new PfConceptKey("dt7", "0.0.1"));
307 JpaToscaDataType dt8 = new JpaToscaDataType(new PfConceptKey("dt8", "0.0.1"));
308 dt8.setDerivedFrom(dt7.getKey());
310 JpaToscaDataType dt9 = new JpaToscaDataType(new PfConceptKey("dt9", "0.0.1"));
311 dt9.setDerivedFrom(dt8.getKey());
313 dataTypes.getConceptMap().put(dt0.getKey(), dt0);
314 dataTypes.getConceptMap().put(dt1.getKey(), dt1);
315 dataTypes.getConceptMap().put(dt2.getKey(), dt2);
316 dataTypes.getConceptMap().put(dt3.getKey(), dt3);
317 dataTypes.getConceptMap().put(dt4.getKey(), dt4);
318 dataTypes.getConceptMap().put(dt5.getKey(), dt5);
319 dataTypes.getConceptMap().put(dt6.getKey(), dt6);
320 dataTypes.getConceptMap().put(dt7.getKey(), dt7);
321 dataTypes.getConceptMap().put(dt8.getKey(), dt8);
322 dataTypes.getConceptMap().put(dt9.getKey(), dt9);
324 ToscaUtils.getEntityTree(filteredDataTypes, dt0.getKey().getName(), dt0.getKey().getVersion());
325 assertEquals(1, filteredDataTypes.getConceptMap().size());
327 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
328 ToscaUtils.getEntityTree(filteredDataTypes, dt1.getKey().getName(), dt1.getKey().getVersion());
329 assertEquals(2, filteredDataTypes.getConceptMap().size());
331 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
332 ToscaUtils.getEntityTree(filteredDataTypes, dt2.getKey().getName(), dt2.getKey().getVersion());
333 assertEquals(2, filteredDataTypes.getConceptMap().size());
335 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
336 ToscaUtils.getEntityTree(filteredDataTypes, dt3.getKey().getName(), dt3.getKey().getVersion());
337 assertEquals(2, filteredDataTypes.getConceptMap().size());
339 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
340 ToscaUtils.getEntityTree(filteredDataTypes, dt4.getKey().getName(), dt4.getKey().getVersion());
341 assertEquals(3, filteredDataTypes.getConceptMap().size());
343 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
344 ToscaUtils.getEntityTree(filteredDataTypes, dt5.getKey().getName(), dt5.getKey().getVersion());
345 assertEquals(4, filteredDataTypes.getConceptMap().size());
347 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
348 ToscaUtils.getEntityTree(filteredDataTypes, dt6.getKey().getName(), dt6.getKey().getVersion());
349 assertEquals(5, filteredDataTypes.getConceptMap().size());
350 assertTrue(filteredDataTypes.getConceptMap().containsValue(dt0));
351 assertFalse(filteredDataTypes.getConceptMap().containsValue(dt1));
352 assertFalse(filteredDataTypes.getConceptMap().containsValue(dt2));
353 assertTrue(filteredDataTypes.getConceptMap().containsValue(dt3));
354 assertTrue(filteredDataTypes.getConceptMap().containsValue(dt4));
355 assertTrue(filteredDataTypes.getConceptMap().containsValue(dt5));
356 assertTrue(filteredDataTypes.getConceptMap().containsValue(dt6));
358 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
359 ToscaUtils.getEntityTree(filteredDataTypes, dt7.getKey().getName(), dt7.getKey().getVersion());
360 assertEquals(1, filteredDataTypes.getConceptMap().size());
362 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
363 ToscaUtils.getEntityTree(filteredDataTypes, dt8.getKey().getName(), dt8.getKey().getVersion());
364 assertEquals(2, filteredDataTypes.getConceptMap().size());
366 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
367 ToscaUtils.getEntityTree(filteredDataTypes, dt9.getKey().getName(), dt9.getKey().getVersion());
368 assertEquals(3, filteredDataTypes.getConceptMap().size());
370 dt9.setDerivedFrom(new PfConceptKey("i.dont.Exist", "0.0.0"));
371 filteredDataTypes = new JpaToscaDataTypes(dataTypes);
373 assertThatThrownBy(() -> {
374 final JpaToscaDataTypes badDataTypes = new JpaToscaDataTypes(dataTypes);
375 ToscaUtils.getEntityTree(badDataTypes, dt9.getKey().getName(), dt9.getKey().getVersion());
376 }).hasMessageContaining("parent").hasMessageContaining("i.dont.Exist:0.0.0")
377 .hasMessageContaining(Validated.NOT_FOUND);