0880fd97bf997271f36b249625371a028a3a5066
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / utils / ToscaUtilsTest.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.utils;
23
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;
31
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;
43
44 /**
45  * Import the {@link ToscaUtils} class.
46  *
47  * @author Liam Fallon (liam.fallon@est.tech)
48  */
49 public class ToscaUtilsTest {
50
51     @Test
52     public void testAssertDataTypes() {
53         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
54
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");
61
62         jpaToscaServiceTemplate.setDataTypes(new JpaToscaDataTypes());
63
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");
70
71         jpaToscaServiceTemplate.getDataTypes().getConceptMap().put(new PfConceptKey(), null);
72
73         assertTrue(ToscaUtils.doDataTypesExist(jpaToscaServiceTemplate));
74         assertEquals(null, ToscaUtils.checkDataTypesExist(jpaToscaServiceTemplate));
75         assertThatCode(() -> {
76             ToscaUtils.assertDataTypesExist(jpaToscaServiceTemplate);
77         }).doesNotThrowAnyException();
78
79     }
80
81     @Test
82     public void testAssertPolicyTypes() {
83         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
84
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");
91
92         jpaToscaServiceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
93
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");
100
101         jpaToscaServiceTemplate.getPolicyTypes().getConceptMap().put(new PfConceptKey(), null);
102
103         assertTrue(ToscaUtils.doPolicyTypesExist(jpaToscaServiceTemplate));
104         assertEquals(null, ToscaUtils.checkPolicyTypesExist(jpaToscaServiceTemplate));
105         assertThatCode(() -> {
106             ToscaUtils.assertPolicyTypesExist(jpaToscaServiceTemplate);
107         }).doesNotThrowAnyException();
108     }
109
110     @Test
111     public void testAssertPolicies() {
112         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
113
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");
120
121         jpaToscaServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
122
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");
129
130         jpaToscaServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
131
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");
138
139         jpaToscaServiceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(new PfConceptKey(), null);
140
141         assertTrue(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate));
142         assertEquals(null, ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate));
143         assertThatCode(() -> {
144             ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate);
145         }).doesNotThrowAnyException();
146     }
147
148     @Test
149     public void testGetentityTypeAncestors() {
150         assertThatThrownBy(() -> {
151             ToscaUtils.getEntityTypeAncestors(null, null, null);
152         }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
153
154         assertThatThrownBy(() -> {
155             ToscaUtils.getEntityTypeAncestors(null, null, new BeanValidationResult("", null));
156         }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
157
158         assertThatThrownBy(() -> {
159             ToscaUtils.getEntityTypeAncestors(null, new JpaToscaDataType(), null);
160         }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
161
162         assertThatThrownBy(() -> {
163             ToscaUtils.getEntityTypeAncestors(null, new JpaToscaDataType(), new BeanValidationResult("", null));
164         }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
165
166         assertThatThrownBy(() -> {
167             ToscaUtils.getEntityTypeAncestors(new JpaToscaDataTypes(), null, null);
168         }).hasMessageMatching("entityType is marked .*on.*ull but is null");
169
170         assertThatThrownBy(() -> {
171             ToscaUtils.getEntityTypeAncestors(new JpaToscaDataTypes(), null, new BeanValidationResult("", null));
172         }).hasMessageMatching("entityType is marked .*on.*ull but is null");
173
174         assertThatThrownBy(() -> {
175             ToscaUtils.getEntityTypeAncestors(new JpaToscaDataTypes(), new JpaToscaDataType(), null);
176         }).hasMessageMatching("result is marked .*on.*ull but is null");
177
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);
183
184         assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
185
186         dataTypes.getConceptMap().put(dt0.getKey(), dt0);
187         assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
188         assertTrue(result.isValid());
189
190         dt0.setDerivedFrom(null);
191         assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
192         assertTrue(result.isValid());
193
194         dt0.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION));
195         assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
196         assertTrue(result.isValid());
197
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);
203
204         result = new BeanValidationResult("", null);
205         dt0.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION));
206
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());
214
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());
220
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());
229
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());
237
238         dt0.setDerivedFrom(dt0.getKey());
239         assertThatThrownBy(() -> {
240             ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, new BeanValidationResult("", null));
241         }).hasMessageContaining("entity type").hasMessageContaining("ancestor of itself");
242
243         dt0.setDerivedFrom(null);
244         assertEquals(2, ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).size());
245
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());
253
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);
258     }
259
260     @Test
261     public void testGetPredefinedDataTypes() {
262         assertTrue(ToscaUtils.getPredefinedDataTypes().contains(new PfConceptKey("string", PfKey.NULL_KEY_VERSION)));
263     }
264
265     @Test
266     public void testgetEntityTree() {
267         assertThatThrownBy(() -> {
268             ToscaUtils.getEntityTree(null, null, null);
269         }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
270
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);
275
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());
282
283         filteredDataTypes.getConceptMap().put(dt0.getKey(), dt0);
284         ToscaUtils.getEntityTree(filteredDataTypes, dt0.getKey().getName(), dt0.getKey().getVersion());
285         assertEquals(dataTypes, filteredDataTypes);
286
287         JpaToscaDataType dt1 = new JpaToscaDataType(new PfConceptKey("dt1", "0.0.1"));
288         dt1.setDerivedFrom(dt0.getKey());
289
290         JpaToscaDataType dt2 = new JpaToscaDataType(new PfConceptKey("dt2", "0.0.1"));
291         dt2.setDerivedFrom(dt0.getKey());
292
293         JpaToscaDataType dt3 = new JpaToscaDataType(new PfConceptKey("dt3", "0.0.1"));
294         dt3.setDerivedFrom(dt0.getKey());
295
296         JpaToscaDataType dt4 = new JpaToscaDataType(new PfConceptKey("dt4", "0.0.1"));
297         dt4.setDerivedFrom(dt3.getKey());
298
299         JpaToscaDataType dt5 = new JpaToscaDataType(new PfConceptKey("dt5", "0.0.1"));
300         dt5.setDerivedFrom(dt4.getKey());
301
302         JpaToscaDataType dt6 = new JpaToscaDataType(new PfConceptKey("dt6", "0.0.1"));
303         dt6.setDerivedFrom(dt5.getKey());
304
305         JpaToscaDataType dt7 = new JpaToscaDataType(new PfConceptKey("dt7", "0.0.1"));
306
307         JpaToscaDataType dt8 = new JpaToscaDataType(new PfConceptKey("dt8", "0.0.1"));
308         dt8.setDerivedFrom(dt7.getKey());
309
310         JpaToscaDataType dt9 = new JpaToscaDataType(new PfConceptKey("dt9", "0.0.1"));
311         dt9.setDerivedFrom(dt8.getKey());
312
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);
323
324         ToscaUtils.getEntityTree(filteredDataTypes, dt0.getKey().getName(), dt0.getKey().getVersion());
325         assertEquals(1, filteredDataTypes.getConceptMap().size());
326
327         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
328         ToscaUtils.getEntityTree(filteredDataTypes, dt1.getKey().getName(), dt1.getKey().getVersion());
329         assertEquals(2, filteredDataTypes.getConceptMap().size());
330
331         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
332         ToscaUtils.getEntityTree(filteredDataTypes, dt2.getKey().getName(), dt2.getKey().getVersion());
333         assertEquals(2, filteredDataTypes.getConceptMap().size());
334
335         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
336         ToscaUtils.getEntityTree(filteredDataTypes, dt3.getKey().getName(), dt3.getKey().getVersion());
337         assertEquals(2, filteredDataTypes.getConceptMap().size());
338
339         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
340         ToscaUtils.getEntityTree(filteredDataTypes, dt4.getKey().getName(), dt4.getKey().getVersion());
341         assertEquals(3, filteredDataTypes.getConceptMap().size());
342
343         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
344         ToscaUtils.getEntityTree(filteredDataTypes, dt5.getKey().getName(), dt5.getKey().getVersion());
345         assertEquals(4, filteredDataTypes.getConceptMap().size());
346
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));
357
358         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
359         ToscaUtils.getEntityTree(filteredDataTypes, dt7.getKey().getName(), dt7.getKey().getVersion());
360         assertEquals(1, filteredDataTypes.getConceptMap().size());
361
362         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
363         ToscaUtils.getEntityTree(filteredDataTypes, dt8.getKey().getName(), dt8.getKey().getVersion());
364         assertEquals(2, filteredDataTypes.getConceptMap().size());
365
366         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
367         ToscaUtils.getEntityTree(filteredDataTypes, dt9.getKey().getName(), dt9.getKey().getVersion());
368         assertEquals(3, filteredDataTypes.getConceptMap().size());
369
370         dt9.setDerivedFrom(new PfConceptKey("i.dont.Exist", "0.0.0"));
371         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
372
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);
378     }
379 }