Set all cross references of policy/models
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / utils / ToscaUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021 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
179     @Test
180     public void testGetentityTypeAncestorsDataType() {
181
182         JpaToscaDataTypes dataTypes = new JpaToscaDataTypes();
183         JpaToscaDataType dt0 = new JpaToscaDataType();
184         dt0.setKey(new PfConceptKey("dt0", "0.0.1"));
185         dt0.setDescription("dt0 description");
186         BeanValidationResult result = new BeanValidationResult("", null);
187
188         assertThat(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result)).isEmpty();
189
190         dataTypes.getConceptMap().put(dt0.getKey(), dt0);
191         checkSingleEmptyEntityTypeAncestor(dataTypes, dt0, result);
192
193         dt0.setDerivedFrom(null);
194         checkSingleEmptyEntityTypeAncestor(dataTypes, dt0, result);
195
196         dt0.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION));
197         checkSingleEmptyEntityTypeAncestor(dataTypes, dt0, result);
198
199         dt0.setDerivedFrom(new PfConceptKey("some.thing.Else", PfKey.NULL_KEY_VERSION));
200         assertThat(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result)).isEmpty();
201         assertFalse(result.isValid());
202         assertThat(result.getResult()).contains("parent").contains("some.thing.Else:0.0.0")
203                         .contains(Validated.NOT_FOUND);
204
205         result = new BeanValidationResult("", null);
206         dt0.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION));
207
208         JpaToscaDataType dt1 = new JpaToscaDataType();
209         dt1.setKey(new PfConceptKey("dt1", "0.0.1"));
210         dt1.setDescription("dt1 description");
211         dataTypes.getConceptMap().put(dt1.getKey(), dt1);
212         checkSingleEmptyEntityTypeAncestor(dataTypes, dt0, result);
213         checkSingleEmptyEntityTypeAncestor(dataTypes, dt1, result);
214
215         dt1.setDerivedFrom(dt0.getKey());
216         checkMultipleEmptyEntityTypeAncestors(dataTypes, dt0, dt1, result, 1);
217
218         JpaToscaDataType dt2 = new JpaToscaDataType();
219         dt2.setKey(new PfConceptKey("dt2", "0.0.1"));
220         dt2.setDescription("dt2 description");
221         dataTypes.getConceptMap().put(dt2.getKey(), dt2);
222         checkMultipleEmptyEntityTypeAncestors(dataTypes, dt0, dt1, result, 1);
223
224         dt2.setDerivedFrom(dt1.getKey());
225         checkMultipleEmptyEntityTypeAncestors(dataTypes, dt0, dt1, result, 1);
226         checkMultipleEmptyEntityTypeAncestors(dataTypes, dt0, dt2, result, 2);
227
228         dt0.setDerivedFrom(dt0.getKey());
229         assertThatThrownBy(() -> {
230             ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, new BeanValidationResult("", null));
231         }).hasMessageContaining("entity type").hasMessageContaining("ancestor of itself");
232
233         dt0.setDerivedFrom(null);
234         assertEquals(2, ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).size());
235
236         dt1.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION));
237         checkSingleEmptyEntityTypeAncestor(dataTypes, dt0, result);
238         checkMultipleEmptyEntityTypeAncestors(dataTypes, dt1, dt2, result, 1, 0);
239
240         dataTypes.getConceptMap().remove(dt1.getKey());
241         assertThat(ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result)).isEmpty();
242         assertFalse(result.isValid());
243         assertThat(result.getResult()).contains("parent").contains("dt1:0.0.1").contains(Validated.NOT_FOUND);
244     }
245
246     private void checkSingleEmptyEntityTypeAncestor(JpaToscaDataTypes dataTypes, JpaToscaDataType emptydt,
247             BeanValidationResult result) {
248         assertThat(ToscaUtils.getEntityTypeAncestors(dataTypes, emptydt, result)).isEmpty();
249         assertTrue(result.isValid());
250     }
251
252     private void checkMultipleEmptyEntityTypeAncestors(JpaToscaDataTypes dataTypes, JpaToscaDataType emptydt,
253             JpaToscaDataType notemptydt, BeanValidationResult result, int size1) {
254         assertThat(ToscaUtils.getEntityTypeAncestors(dataTypes, emptydt, result)).isEmpty();
255         assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, notemptydt, result).isEmpty());
256         assertEquals(size1, ToscaUtils.getEntityTypeAncestors(dataTypes, notemptydt, result).size());
257         assertTrue(result.isValid());
258     }
259
260     private void checkMultipleEmptyEntityTypeAncestors(JpaToscaDataTypes dataTypes, JpaToscaDataType emptydt,
261             JpaToscaDataType notemptydt, BeanValidationResult result, int size1, int size2) {
262         assertThat(ToscaUtils.getEntityTypeAncestors(dataTypes, emptydt, result)).isEmpty();
263         assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, notemptydt, result).isEmpty());
264         assertEquals(size1, ToscaUtils.getEntityTypeAncestors(dataTypes, notemptydt, result).size());
265         assertEquals(size2, ToscaUtils.getEntityTypeAncestors(dataTypes, emptydt, result).size());
266         assertTrue(result.isValid());
267     }
268
269     @Test
270     public void testGetPredefinedDataTypes() {
271         assertTrue(ToscaUtils.getPredefinedDataTypes().contains(new PfConceptKey("string", PfKey.NULL_KEY_VERSION)));
272     }
273
274     @Test
275     public void testgetEntityTree() {
276         assertThatThrownBy(() -> {
277             ToscaUtils.getEntityTree(null, null, null);
278         }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
279
280         JpaToscaDataTypes dataTypes = new JpaToscaDataTypes(new PfConceptKey("datatypes", "0.0.1"));
281         JpaToscaDataTypes filteredDataTypes = new JpaToscaDataTypes(new PfConceptKey("datatypes", "0.0.1"));
282         ToscaUtils.getEntityTree(filteredDataTypes, "IDontExist", "0.0.0");
283         assertEquals(dataTypes, filteredDataTypes);
284
285         JpaToscaDataType dt0 = new JpaToscaDataType(new PfConceptKey("dt0", "0.0.1"));
286         dataTypes.getConceptMap().put(dt0.getKey(), dt0);
287         filteredDataTypes.getConceptMap().put(dt0.getKey(), dt0);
288         ToscaUtils.getEntityTree(filteredDataTypes, "IDontExist", "0.0.0");
289         assertNotEquals(dataTypes, filteredDataTypes);
290         assertThat(filteredDataTypes.getConceptMap()).isEmpty();
291
292         filteredDataTypes.getConceptMap().put(dt0.getKey(), dt0);
293         ToscaUtils.getEntityTree(filteredDataTypes, dt0.getKey().getName(), dt0.getKey().getVersion());
294         assertEquals(dataTypes, filteredDataTypes);
295
296         JpaToscaDataType dt1 = new JpaToscaDataType(new PfConceptKey("dt1", "0.0.1"));
297         dt1.setDerivedFrom(dt0.getKey());
298
299         JpaToscaDataType dt2 = new JpaToscaDataType(new PfConceptKey("dt2", "0.0.1"));
300         dt2.setDerivedFrom(dt0.getKey());
301
302         JpaToscaDataType dt3 = new JpaToscaDataType(new PfConceptKey("dt3", "0.0.1"));
303         dt3.setDerivedFrom(dt0.getKey());
304
305         JpaToscaDataType dt4 = new JpaToscaDataType(new PfConceptKey("dt4", "0.0.1"));
306         dt4.setDerivedFrom(dt3.getKey());
307
308         JpaToscaDataType dt5 = new JpaToscaDataType(new PfConceptKey("dt5", "0.0.1"));
309         dt5.setDerivedFrom(dt4.getKey());
310
311         JpaToscaDataType dt6 = new JpaToscaDataType(new PfConceptKey("dt6", "0.0.1"));
312         dt6.setDerivedFrom(dt5.getKey());
313
314         JpaToscaDataType dt7 = new JpaToscaDataType(new PfConceptKey("dt7", "0.0.1"));
315
316         JpaToscaDataType dt8 = new JpaToscaDataType(new PfConceptKey("dt8", "0.0.1"));
317         dt8.setDerivedFrom(dt7.getKey());
318
319         JpaToscaDataType dt9 = new JpaToscaDataType(new PfConceptKey("dt9", "0.0.1"));
320         dt9.setDerivedFrom(dt8.getKey());
321
322         dataTypes.getConceptMap().put(dt0.getKey(), dt0);
323         dataTypes.getConceptMap().put(dt1.getKey(), dt1);
324         dataTypes.getConceptMap().put(dt2.getKey(), dt2);
325         dataTypes.getConceptMap().put(dt3.getKey(), dt3);
326         dataTypes.getConceptMap().put(dt4.getKey(), dt4);
327         dataTypes.getConceptMap().put(dt5.getKey(), dt5);
328         dataTypes.getConceptMap().put(dt6.getKey(), dt6);
329         dataTypes.getConceptMap().put(dt7.getKey(), dt7);
330         dataTypes.getConceptMap().put(dt8.getKey(), dt8);
331         dataTypes.getConceptMap().put(dt9.getKey(), dt9);
332
333         ToscaUtils.getEntityTree(filteredDataTypes, dt0.getKey().getName(), dt0.getKey().getVersion());
334         assertEquals(1, filteredDataTypes.getConceptMap().size());
335
336         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
337         ToscaUtils.getEntityTree(filteredDataTypes, dt1.getKey().getName(), dt1.getKey().getVersion());
338         assertEquals(2, filteredDataTypes.getConceptMap().size());
339
340         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
341         ToscaUtils.getEntityTree(filteredDataTypes, dt2.getKey().getName(), dt2.getKey().getVersion());
342         assertEquals(2, filteredDataTypes.getConceptMap().size());
343
344         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
345         ToscaUtils.getEntityTree(filteredDataTypes, dt3.getKey().getName(), dt3.getKey().getVersion());
346         assertEquals(2, filteredDataTypes.getConceptMap().size());
347
348         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
349         ToscaUtils.getEntityTree(filteredDataTypes, dt4.getKey().getName(), dt4.getKey().getVersion());
350         assertEquals(3, filteredDataTypes.getConceptMap().size());
351
352         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
353         ToscaUtils.getEntityTree(filteredDataTypes, dt5.getKey().getName(), dt5.getKey().getVersion());
354         assertEquals(4, filteredDataTypes.getConceptMap().size());
355
356         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
357         ToscaUtils.getEntityTree(filteredDataTypes, dt6.getKey().getName(), dt6.getKey().getVersion());
358         assertEquals(5, filteredDataTypes.getConceptMap().size());
359         assertTrue(filteredDataTypes.getConceptMap().containsValue(dt0));
360         assertFalse(filteredDataTypes.getConceptMap().containsValue(dt1));
361         assertFalse(filteredDataTypes.getConceptMap().containsValue(dt2));
362         assertTrue(filteredDataTypes.getConceptMap().containsValue(dt3));
363         assertTrue(filteredDataTypes.getConceptMap().containsValue(dt4));
364         assertTrue(filteredDataTypes.getConceptMap().containsValue(dt5));
365         assertTrue(filteredDataTypes.getConceptMap().containsValue(dt6));
366
367         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
368         ToscaUtils.getEntityTree(filteredDataTypes, dt7.getKey().getName(), dt7.getKey().getVersion());
369         assertEquals(1, filteredDataTypes.getConceptMap().size());
370
371         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
372         ToscaUtils.getEntityTree(filteredDataTypes, dt8.getKey().getName(), dt8.getKey().getVersion());
373         assertEquals(2, filteredDataTypes.getConceptMap().size());
374
375         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
376         ToscaUtils.getEntityTree(filteredDataTypes, dt9.getKey().getName(), dt9.getKey().getVersion());
377         assertEquals(3, filteredDataTypes.getConceptMap().size());
378
379         dt9.setDerivedFrom(new PfConceptKey("i.dont.Exist", "0.0.0"));
380         filteredDataTypes = new JpaToscaDataTypes(dataTypes);
381
382         assertThatThrownBy(() -> {
383             final JpaToscaDataTypes badDataTypes = new JpaToscaDataTypes(dataTypes);
384             ToscaUtils.getEntityTree(badDataTypes, dt9.getKey().getName(), dt9.getKey().getVersion());
385         }).hasMessageContaining("parent").hasMessageContaining("i.dont.Exist:0.0.0")
386                         .hasMessageContaining(Validated.NOT_FOUND);
387     }
388 }