Implement validation and hierarchical get
[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  * ================================================================================
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.utils;
22
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;
29
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;
40
41 /**
42  * Import the {@link ToscaUtils} class.
43  *
44  * @author Liam Fallon (liam.fallon@est.tech)
45  */
46 public class ToscaUtilsTest {
47
48     @Test
49     public void testAssertDataTypes() {
50         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
51
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");
58
59         jpaToscaServiceTemplate.setDataTypes(new JpaToscaDataTypes());
60
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");
67
68         jpaToscaServiceTemplate.getDataTypes().getConceptMap().put(new PfConceptKey(), null);
69
70         assertTrue(ToscaUtils.doDataTypesExist(jpaToscaServiceTemplate));
71         assertEquals(null, ToscaUtils.checkDataTypesExist(jpaToscaServiceTemplate));
72         assertThatCode(() -> {
73             ToscaUtils.assertDataTypesExist(jpaToscaServiceTemplate);
74         }).doesNotThrowAnyException();
75
76     }
77
78     @Test
79     public void testAssertPolicyTypes() {
80         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
81
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");
88
89         jpaToscaServiceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
90
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");
97
98         jpaToscaServiceTemplate.getPolicyTypes().getConceptMap().put(new PfConceptKey(), null);
99
100         assertTrue(ToscaUtils.doPolicyTypesExist(jpaToscaServiceTemplate));
101         assertEquals(null, ToscaUtils.checkPolicyTypesExist(jpaToscaServiceTemplate));
102         assertThatCode(() -> {
103             ToscaUtils.assertPolicyTypesExist(jpaToscaServiceTemplate);
104         }).doesNotThrowAnyException();
105     }
106
107     @Test
108     public void testAssertPolicies() {
109         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
110
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");
117
118         jpaToscaServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
119
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");
126
127         jpaToscaServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
128
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");
135
136         jpaToscaServiceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(new PfConceptKey(), null);
137
138         assertTrue(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate));
139         assertEquals(null, ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate));
140         assertThatCode(() -> {
141             ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate);
142         }).doesNotThrowAnyException();
143     }
144
145     @Test
146     public void testGetentityTypeAncestors() {
147         assertThatThrownBy(() -> {
148             ToscaUtils.getEntityTypeAncestors(null, null, null);
149         }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
150
151         assertThatThrownBy(() -> {
152             ToscaUtils.getEntityTypeAncestors(null, null, new PfValidationResult());
153         }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
154
155         assertThatThrownBy(() -> {
156             ToscaUtils.getEntityTypeAncestors(null, new JpaToscaDataType(), null);
157         }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
158
159         assertThatThrownBy(() -> {
160             ToscaUtils.getEntityTypeAncestors(null, new JpaToscaDataType(), new PfValidationResult());
161         }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
162
163         assertThatThrownBy(() -> {
164             ToscaUtils.getEntityTypeAncestors(new JpaToscaDataTypes(), null, null);
165         }).hasMessageMatching("entityType is marked .*on.*ull but is null");
166
167         assertThatThrownBy(() -> {
168             ToscaUtils.getEntityTypeAncestors(new JpaToscaDataTypes(), null, new PfValidationResult());
169         }).hasMessageMatching("entityType is marked .*on.*ull but is null");
170
171         assertThatThrownBy(() -> {
172             ToscaUtils.getEntityTypeAncestors(new JpaToscaDataTypes(), new JpaToscaDataType(), null);
173         }).hasMessageMatching("result is marked .*on.*ull but is null");
174
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();
180
181         assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
182
183         dataTypes.getConceptMap().put(dt0.getKey(), dt0);
184         assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
185         assertTrue(result.isValid());
186
187         dt0.setDerivedFrom(null);
188         assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
189         assertTrue(result.isValid());
190
191         dt0.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION));
192         assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
193         assertTrue(result.isValid());
194
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"));
199
200         result = new PfValidationResult();
201         dt0.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION));
202
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());
210
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());
216
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());
225
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());
233
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());
241
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"));
246     }
247
248     @Test
249     public void testGetPredefinedDataTypes() {
250         assertTrue(ToscaUtils.getPredefinedDataTypes().contains(new PfConceptKey("string", PfKey.NULL_KEY_VERSION)));
251     }
252
253     @Test
254     public void testgetEntityTree() {
255         assertThatThrownBy(() -> {
256             ToscaUtils.getEntityTree(null, null, null);
257         }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
258
259         JpaToscaDataTypes dataTypes = new JpaToscaDataTypes(new PfConceptKey("datatypes", "0.0.1"));
260         JpaToscaDataTypes filteredDataTypes = new JpaToscaDataTypes(new PfConceptKey("datatypes", "0.0.1"));
261         ToscaUtils.getEntityTree(filteredDataTypes, "IDontExist", "0.0.0");
262         assertEquals(dataTypes, filteredDataTypes);
263
264         JpaToscaDataType dt0 = new JpaToscaDataType(new PfConceptKey("dt0", "0.0.1"));
265         dataTypes.getConceptMap().put(dt0.getKey(), dt0);
266         filteredDataTypes.getConceptMap().put(dt0.getKey(), dt0);
267         ToscaUtils.getEntityTree(filteredDataTypes, "IDontExist", "0.0.0");
268         assertNotEquals(dataTypes, filteredDataTypes);
269         assertTrue(filteredDataTypes.getConceptMap().isEmpty());
270
271         filteredDataTypes.getConceptMap().put(dt0.getKey(), dt0);
272         ToscaUtils.getEntityTree(filteredDataTypes, dt0.getKey().getName(), dt0.getKey().getVersion());
273         assertEquals(dataTypes, filteredDataTypes);
274
275         JpaToscaDataType dt1 = new JpaToscaDataType(new PfConceptKey("dt1", "0.0.1"));
276         dt1.setDerivedFrom(dt0.getKey());
277
278         JpaToscaDataType dt2 = new JpaToscaDataType(new PfConceptKey("dt2", "0.0.1"));
279         dt2.setDerivedFrom(dt0.getKey());
280
281         JpaToscaDataType dt3 = new JpaToscaDataType(new PfConceptKey("dt3", "0.0.1"));
282         dt3.setDerivedFrom(dt0.getKey());
283
284         JpaToscaDataType dt4 = new JpaToscaDataType(new PfConceptKey("dt4", "0.0.1"));
285         dt4.setDerivedFrom(dt3.getKey());
286
287         JpaToscaDataType dt5 = new JpaToscaDataType(new PfConceptKey("dt5", "0.0.1"));
288         dt5.setDerivedFrom(dt4.getKey());
289
290         JpaToscaDataType dt6 = new JpaToscaDataType(new PfConceptKey("dt6", "0.0.1"));
291         dt6.setDerivedFrom(dt5.getKey());
292
293         JpaToscaDataType dt7 = new JpaToscaDataType(new PfConceptKey("dt7", "0.0.1"));
294
295         JpaToscaDataType dt8 = new JpaToscaDataType(new PfConceptKey("dt8", "0.0.1"));
296         dt8.setDerivedFrom(dt7.getKey());
297
298         JpaToscaDataType dt9 = new JpaToscaDataType(new PfConceptKey("dt9", "0.0.1"));
299         dt9.setDerivedFrom(dt8.getKey());
300
301         dataTypes.getConceptMap().put(dt0.getKey(), dt0);
302         dataTypes.getConceptMap().put(dt1.getKey(), dt1);
303         dataTypes.getConceptMap().put(dt2.getKey(), dt2);
304         dataTypes.getConceptMap().put(dt3.getKey(), dt3);
305         dataTypes.getConceptMap().put(dt4.getKey(), dt4);
306         dataTypes.getConceptMap().put(dt5.getKey(), dt5);
307         dataTypes.getConceptMap().put(dt6.getKey(), dt6);
308         dataTypes.getConceptMap().put(dt7.getKey(), dt7);
309         dataTypes.getConceptMap().put(dt8.getKey(), dt8);
310         dataTypes.getConceptMap().put(dt9.getKey(), dt9);
311
312         ToscaUtils.getEntityTree(filteredDataTypes, dt0.getKey().getName(), dt0.getKey().getVersion());
313         assertEquals(1, filteredDataTypes.getConceptMap().size());
314
315         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
316         ToscaUtils.getEntityTree(filteredDataTypes, dt1.getKey().getName(), dt1.getKey().getVersion());
317         assertEquals(2, filteredDataTypes.getConceptMap().size());
318
319         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
320         ToscaUtils.getEntityTree(filteredDataTypes, dt2.getKey().getName(), dt2.getKey().getVersion());
321         assertEquals(2, filteredDataTypes.getConceptMap().size());
322
323         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
324         ToscaUtils.getEntityTree(filteredDataTypes, dt3.getKey().getName(), dt3.getKey().getVersion());
325         assertEquals(2, filteredDataTypes.getConceptMap().size());
326
327         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
328         ToscaUtils.getEntityTree(filteredDataTypes, dt4.getKey().getName(), dt4.getKey().getVersion());
329         assertEquals(3, filteredDataTypes.getConceptMap().size());
330
331         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
332         ToscaUtils.getEntityTree(filteredDataTypes, dt5.getKey().getName(), dt5.getKey().getVersion());
333         assertEquals(4, filteredDataTypes.getConceptMap().size());
334
335         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
336         ToscaUtils.getEntityTree(filteredDataTypes, dt6.getKey().getName(), dt6.getKey().getVersion());
337         assertEquals(5, filteredDataTypes.getConceptMap().size());
338         assertTrue(filteredDataTypes.getConceptMap().containsValue(dt0));
339         assertFalse(filteredDataTypes.getConceptMap().containsValue(dt1));
340         assertFalse(filteredDataTypes.getConceptMap().containsValue(dt2));
341         assertTrue(filteredDataTypes.getConceptMap().containsValue(dt3));
342         assertTrue(filteredDataTypes.getConceptMap().containsValue(dt4));
343         assertTrue(filteredDataTypes.getConceptMap().containsValue(dt5));
344         assertTrue(filteredDataTypes.getConceptMap().containsValue(dt6));
345
346         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
347         ToscaUtils.getEntityTree(filteredDataTypes, dt7.getKey().getName(), dt7.getKey().getVersion());
348         assertEquals(1, filteredDataTypes.getConceptMap().size());
349
350         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
351         ToscaUtils.getEntityTree(filteredDataTypes, dt8.getKey().getName(), dt8.getKey().getVersion());
352         assertEquals(2, filteredDataTypes.getConceptMap().size());
353
354         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
355         ToscaUtils.getEntityTree(filteredDataTypes, dt9.getKey().getName(), dt9.getKey().getVersion());
356         assertEquals(3, filteredDataTypes.getConceptMap().size());
357
358         dt9.setDerivedFrom(new PfConceptKey("i.dont.Exist", "0.0.0"));
359         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
360
361         assertThatThrownBy(() -> {
362             final JpaToscaDataTypes badDataTypes = new JpaToscaDataTypes(dataTypes);
363             ToscaUtils.getEntityTree(badDataTypes, dt9.getKey().getName(), dt9.getKey().getVersion());
364         }).hasMessageContaining("parent i.dont.Exist:0.0.0 of entity not found");
365     }
366 }