2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 Nordix Foundation.
4 * Modifications Copyright (C) 2019-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.simple.concepts;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertFalse;
28 import static org.junit.jupiter.api.Assertions.assertNotEquals;
29 import static org.junit.jupiter.api.Assertions.assertNotNull;
30 import static org.junit.jupiter.api.Assertions.assertTrue;
32 import java.util.ArrayList;
33 import java.util.LinkedHashMap;
34 import java.util.List;
36 import org.junit.jupiter.api.Test;
37 import org.onap.policy.models.base.PfConceptKey;
38 import org.onap.policy.models.base.PfKey;
39 import org.onap.policy.models.base.PfReferenceKey;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
44 * DAO test for JpaToscaDatatype.
46 * @author Liam Fallon (liam.fallon@est.tech)
48 class JpaToscaDataTypeTest {
50 private static final String VERSION_001 = "0.0.1";
53 void testDataTypePojo() {
54 assertNotNull(new JpaToscaDataType());
55 assertNotNull(new JpaToscaDataType(new PfConceptKey()));
56 assertNotNull(new JpaToscaDataType(new JpaToscaDataType()));
57 assertNotNull(new JpaToscaDataType(new ToscaDataType()));
59 assertThatThrownBy(() -> {
60 new JpaToscaDataType((PfConceptKey) null);
61 }).hasMessageMatching("key is marked .*on.*ull but is null");
63 assertThatThrownBy(() -> new JpaToscaDataType((JpaToscaDataType) null))
64 .isInstanceOf(NullPointerException.class);
68 void testDataTypeProperties() {
69 PfConceptKey dtKey = new PfConceptKey("tdt", VERSION_001);
70 JpaToscaDataType tdt = new JpaToscaDataType(dtKey);
72 List<JpaToscaConstraint> constraints = new ArrayList<>();
73 JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
75 tdt.setConstraints(constraints);
76 assertEquals(constraints, tdt.getConstraints());
78 Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
80 new JpaToscaProperty(new PfReferenceKey(dtKey, "pr"), new PfConceptKey("type", VERSION_001));
81 properties.put(tp.getKey().getLocalName(), tp);
82 tdt.setProperties(properties);
83 assertEquals(properties, tdt.getProperties());
85 JpaToscaDataType tdtClone0 = new JpaToscaDataType(tdt);
86 assertEquals(tdt, tdtClone0);
87 assertEquals(0, tdt.compareTo(tdtClone0));
89 JpaToscaDataType tdtClone1 = new JpaToscaDataType(tdt);
90 assertEquals(tdt, tdtClone1);
91 assertEquals(0, tdt.compareTo(tdtClone1));
93 assertEquals(-1, tdt.compareTo(null));
94 assertEquals(0, tdt.compareTo(tdt));
95 assertNotEquals(0, tdt.compareTo(tdt.getKey()));
97 PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
98 JpaToscaDataType otherDt = new JpaToscaDataType(otherDtKey);
100 assertNotEquals(0, tdt.compareTo(otherDt));
101 otherDt.setKey(dtKey);
102 assertNotEquals(0, tdt.compareTo(otherDt));
103 otherDt.setConstraints(constraints);
104 assertNotEquals(0, tdt.compareTo(otherDt));
105 otherDt.setProperties(properties);
106 assertEquals(0, tdt.compareTo(otherDt));
108 assertEquals(3, tdt.getKeys().size());
109 assertEquals(1, new JpaToscaDataType().getKeys().size());
111 new JpaToscaDataType().clean();
113 assertEquals(tdtClone0, tdt);
115 assertFalse(new JpaToscaDataType().validate("").isValid());
116 validateJpaToscaDataTypeOperations(tdt);
118 assertThatThrownBy(() -> {
120 }).hasMessageMatching("fieldName is marked .*on.*ull but is null");
123 private void validateJpaToscaDataTypeOperations(JpaToscaDataType tdt) {
124 assertTrue(tdt.validate("").isValid());
126 tdt.getConstraints().add(null);
127 assertFalse(tdt.validate("").isValid());
128 tdt.getConstraints().remove(null);
129 assertTrue(tdt.validate("").isValid());
131 tdt.getProperties().put(null, null);
132 assertFalse(tdt.validate("").isValid());
133 tdt.getProperties().remove(null);
134 assertTrue(tdt.validate("").isValid());
138 void testDataTypeConstraints() {
139 ToscaDataType dat = new ToscaDataType();
141 dat.setVersion("1.2.3");
142 dat.setConstraints(new ArrayList<>());
143 ToscaConstraint constraint = new ToscaConstraint();
144 constraint.setEqual("EqualTo");
145 dat.getConstraints().add(constraint);
147 JpaToscaDataType tdta = new JpaToscaDataType();
148 tdta.fromAuthorative(dat);
149 assertEquals("name", tdta.getKey().getName());
151 ToscaDataType datOut = tdta.toAuthorative();
152 assertNotNull(datOut);
156 void testGetReferencedDataTypes() {
157 JpaToscaDataType dt0 = new JpaToscaDataType(new PfConceptKey("dt0", "0.0.1"));
159 assertTrue(dt0.getReferencedDataTypes().isEmpty());
161 dt0.setProperties(new LinkedHashMap<>());
162 assertTrue(dt0.getReferencedDataTypes().isEmpty());
164 JpaToscaProperty prop0 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop0"));
165 prop0.setType(new PfConceptKey("string", PfKey.NULL_KEY_VERSION));
166 assertTrue(prop0.validate("").isValid());
168 dt0.getProperties().put(prop0.getKey().getLocalName(), prop0);
169 assertTrue(dt0.getReferencedDataTypes().isEmpty());
171 JpaToscaProperty prop1 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop1"));
172 prop1.setType(new PfConceptKey("the.property.Type0", "0.0.1"));
173 assertTrue(prop1.validate("").isValid());
175 dt0.getProperties().put(prop1.getKey().getLocalName(), prop1);
176 assertEquals(1, dt0.getReferencedDataTypes().size());
178 JpaToscaProperty prop2 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop2"));
179 prop2.setType(new PfConceptKey("the.property.Type0", "0.0.1"));
180 assertTrue(prop2.validate("").isValid());
182 dt0.getProperties().put(prop2.getKey().getLocalName(), prop2);
183 assertEquals(1, dt0.getReferencedDataTypes().size());
185 JpaToscaProperty prop3 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop4"));
186 prop3.setType(new PfConceptKey("the.property.Type1", "0.0.1"));
187 prop3.setEntrySchema(new JpaToscaSchemaDefinition());
188 prop3.getEntrySchema().setType(new PfConceptKey("the.property.Type3", "0.0.1"));
189 assertTrue(prop3.validate("").isValid());
191 dt0.getProperties().put(prop3.getKey().getLocalName(), prop3);
192 assertEquals(3, dt0.getReferencedDataTypes().size());
194 JpaToscaProperty prop4 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop4"));
195 prop4.setType(new PfConceptKey("the.property.Type1", "0.0.1"));
196 prop4.setEntrySchema(new JpaToscaSchemaDefinition());
197 prop4.getEntrySchema().setType(new PfConceptKey("the.property.Type2", "0.0.1"));
198 assertTrue(prop4.validate("").isValid());
200 dt0.getProperties().put(prop4.getKey().getLocalName(), prop4);
201 assertEquals(3, dt0.getReferencedDataTypes().size());