2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021, 2023 Nordix Foundation.
4 * Modifications Copyright (C) 2019-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.simple.concepts;
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.assertNotNull;
29 import static org.junit.Assert.assertNotSame;
30 import static org.junit.Assert.assertSame;
31 import static org.junit.Assert.assertTrue;
33 import java.util.ArrayList;
34 import java.util.List;
36 import java.util.TreeMap;
37 import org.junit.Test;
38 import org.onap.policy.models.base.PfConceptKey;
39 import org.onap.policy.models.base.PfReferenceKey;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
43 * DAO test for ToscaProperty.
45 * @author Liam Fallon (liam.fallon@est.tech)
47 public class JpaToscaPropertyTest {
49 private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
50 private static final String DEFAULT_KEY = "defaultKey";
51 private static final String A_DESCRIPTION = "A Description";
52 private static final String VERSION_001 = "0.0.1";
55 public void testPropertyNull() {
56 assertNotNull(new JpaToscaProperty());
57 assertNotNull(new JpaToscaProperty(new PfReferenceKey()));
58 assertNotNull(new JpaToscaProperty(new PfReferenceKey(), new PfConceptKey()));
59 assertNotNull(new JpaToscaProperty(new JpaToscaProperty()));
61 assertThatThrownBy(() -> new JpaToscaProperty((PfReferenceKey) null)).hasMessageMatching(KEY_IS_NULL);
63 assertThatThrownBy(() -> new JpaToscaProperty(null, null)).hasMessageMatching(KEY_IS_NULL);
65 assertThatThrownBy(() -> new JpaToscaProperty(null, new PfConceptKey())).hasMessageMatching(KEY_IS_NULL);
67 assertThatThrownBy(() -> new JpaToscaProperty(new PfReferenceKey(), null))
68 .hasMessageMatching("type is marked .*on.*ull but is null");
72 public void testProperty() {
73 PfConceptKey pparentKey = new PfConceptKey("tParentKey", VERSION_001);
74 PfReferenceKey pkey = new PfReferenceKey(pparentKey, "trigger0");
75 PfConceptKey ptypeKey = new PfConceptKey("TTypeKey", VERSION_001);
76 JpaToscaProperty tp = new JpaToscaProperty(pkey, ptypeKey);
78 assertEquals(tp, new JpaToscaProperty(tp));
80 tp.setDescription(A_DESCRIPTION);
81 assertEquals(A_DESCRIPTION, tp.getDescription());
83 tp.setRequired(false);
84 assertFalse(tp.isRequired());
86 tp.setDefaultValue(DEFAULT_KEY);
88 tp.setStatus(ToscaProperty.Status.SUPPORTED);
90 List<JpaToscaConstraint> constraints = new ArrayList<>();
91 JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
93 tp.setConstraints(constraints);
94 assertEquals(constraints, tp.getConstraints());
96 PfConceptKey typeKey = new PfConceptKey("type", VERSION_001);
97 JpaToscaSchemaDefinition tes = new JpaToscaSchemaDefinition(typeKey);
98 tp.setEntrySchema(tes);
100 TreeMap<String, String> metadata = new TreeMap<>();
101 metadata.put("metaA", "dataA");
102 metadata.put("metaB", "dataB");
103 tp.setMetadata(metadata);
104 assertSame(metadata, tp.getMetadata());
106 JpaToscaProperty tdtClone0 = new JpaToscaProperty(tp);
107 assertEquals(tp, tdtClone0);
108 assertEquals(0, tp.compareTo(tdtClone0));
110 assertNotSame(tdtClone0.getMetadata(), tp.getMetadata());
112 JpaToscaProperty tdtClone1 = new JpaToscaProperty(tp);
113 assertEquals(tp, tdtClone1);
114 assertEquals(0, tp.compareTo(tdtClone1));
116 assertThatThrownBy(() -> tp.compareTo(null)).isInstanceOf(NullPointerException.class);
117 assertEquals(0, tp.compareTo(tp));
118 assertNotEquals(0, tp.compareTo(tp.getKey()));
122 public void testPropertyCompareData() {
123 JpaToscaProperty tp = setUpJpaToscaProperty();
125 PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", VERSION_001, "OtherProperty");
126 JpaToscaProperty otherDt = new JpaToscaProperty(otherDtKey);
128 assertNotEquals(0, tp.compareTo(otherDt));
130 PfConceptKey pparentKey = new PfConceptKey("tParentKey", VERSION_001);
131 PfReferenceKey pkey = new PfReferenceKey(pparentKey, "trigger0");
132 otherDt.setKey(pkey);
133 assertNotEquals(0, tp.compareTo(otherDt));
135 PfConceptKey ptypeKey = new PfConceptKey("TTypeKey", VERSION_001);
136 otherDt.setType(ptypeKey);
137 assertNotEquals(0, tp.compareTo(otherDt));
138 otherDt.setDescription(A_DESCRIPTION);
139 assertNotEquals(0, tp.compareTo(otherDt));
140 otherDt.setRequired(false);
141 assertNotEquals(0, tp.compareTo(otherDt));
142 otherDt.setDefaultValue(DEFAULT_KEY);
143 assertNotEquals(0, tp.compareTo(otherDt));
144 otherDt.setStatus(ToscaProperty.Status.SUPPORTED);
145 assertNotEquals(0, tp.compareTo(otherDt));
146 assertNotEquals(0, tp.compareTo(otherDt));
148 List<JpaToscaConstraint> constraints = new ArrayList<>();
149 JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
150 constraints.add(lsc);
151 otherDt.setConstraints(constraints);
152 assertNotEquals(0, tp.compareTo(otherDt));
154 PfConceptKey typeKey = new PfConceptKey("type", VERSION_001);
155 JpaToscaSchemaDefinition tes = new JpaToscaSchemaDefinition(typeKey);
156 otherDt.setEntrySchema(tes);
157 assertNotEquals(0, tp.compareTo(otherDt));
159 TreeMap<String, String> metadata = new TreeMap<>();
160 metadata.put("metaA", "dataA");
161 metadata.put("metaB", "dataB");
162 otherDt.setMetadata(metadata);
163 assertEquals(0, tp.compareTo(otherDt));
165 otherDt.setRequired(true);
166 assertNotEquals(0, tp.compareTo(otherDt));
167 otherDt.setRequired(false);
168 assertEquals(0, tp.compareTo(otherDt));
170 otherDt.setStatus(ToscaProperty.Status.UNSUPPORTED);
171 assertNotEquals(0, tp.compareTo(otherDt));
172 otherDt.setStatus(ToscaProperty.Status.SUPPORTED);
173 assertEquals(0, tp.compareTo(otherDt));
175 assertThatThrownBy(() -> new JpaToscaProperty((JpaToscaProperty) null))
176 .isInstanceOf(NullPointerException.class);
178 assertEquals(3, tp.getKeys().size());
179 assertEquals(2, new JpaToscaProperty().getKeys().size());
183 public void testPropertyCompareDescription() {
184 JpaToscaProperty tp = setUpJpaToscaProperty();
185 JpaToscaProperty tdtClone0 = new JpaToscaProperty(tp);
187 new JpaToscaProperty().clean();
189 assertEquals(tdtClone0, tp);
191 assertFalse(new JpaToscaProperty().validate("").isValid());
192 assertTrue(tp.validate("").isValid());
194 tp.setDescription(null);
195 assertTrue(tp.validate("").isValid());
196 tp.setDescription("");
197 assertFalse(tp.validate("").isValid());
198 tp.setDescription(A_DESCRIPTION);
199 assertTrue(tp.validate("").isValid());
202 assertFalse(tp.validate("").isValid());
206 public void testPropertyCompareMetadata() {
207 JpaToscaProperty tp = setUpJpaToscaProperty();
208 PfConceptKey typeKey = new PfConceptKey("type", VERSION_001);
210 assertTrue(tp.validate("").isValid());
212 tp.setType(PfConceptKey.getNullKey());
213 assertFalse(tp.validate("").isValid());
215 assertTrue(tp.validate("").isValid());
217 tp.setDefaultValue(null);
218 assertTrue(tp.validate("").isValid());
219 tp.setDefaultValue("");
220 assertFalse(tp.validate("").isValid());
221 tp.setDefaultValue(DEFAULT_KEY);
222 assertTrue(tp.validate("").isValid());
224 tp.getConstraints().add(null);
225 assertFalse(tp.validate("").isValid());
226 tp.getConstraints().remove(null);
227 assertTrue(tp.validate("").isValid());
229 tp.setMetadata(null);
230 assertTrue(tp.validate("").isValid());
232 assertThatThrownBy(() -> tp.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");
236 public void testToAuthorative_testFromAuthorative() {
237 // check with empty structure
238 JpaToscaProperty tp = new JpaToscaProperty();
239 ToscaProperty auth = tp.toAuthorative();
240 JpaToscaProperty tp2 = new JpaToscaProperty();
241 tp2.fromAuthorative(auth);
242 assertEquals(tp, tp2);
244 // populate and try again
245 PfConceptKey pparentKey = new PfConceptKey("tParentKey", VERSION_001);
246 PfReferenceKey pkey = new PfReferenceKey(pparentKey, "trigger0");
247 PfConceptKey ptypeKey = new PfConceptKey("TTypeKey", VERSION_001);
248 tp = new JpaToscaProperty(pkey, ptypeKey);
250 tp.setDescription(A_DESCRIPTION);
251 tp.setRequired(true);
252 tp.setDefaultValue(DEFAULT_KEY);
253 tp.setStatus(ToscaProperty.Status.SUPPORTED);
255 List<JpaToscaConstraint> constraints = new ArrayList<>();
256 JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
257 constraints.add(lsc);
258 tp.setConstraints(constraints);
260 PfConceptKey typeKey = new PfConceptKey("type", VERSION_001);
261 JpaToscaSchemaDefinition tes = new JpaToscaSchemaDefinition(typeKey);
262 tp.setEntrySchema(tes);
264 TreeMap<String, String> metadata = new TreeMap<>();
265 metadata.put("metaA", "dataA");
266 metadata.put("metaB", "dataB");
267 tp.setMetadata(metadata);
269 auth = tp.toAuthorative();
270 tp2 = new JpaToscaProperty();
271 tp2.fromAuthorative(auth);
273 // note: parent key info is not copied, so we manually copy it
274 tp2.getKey().setParentConceptKey(tp.getKey().getParentConceptKey());
276 assertEquals(tp.toString(), tp2.toString());
277 assertEquals(tp, tp2);
280 private JpaToscaProperty setUpJpaToscaProperty() {
281 PfConceptKey pparentKey = new PfConceptKey("tParentKey", VERSION_001);
282 PfReferenceKey pkey = new PfReferenceKey(pparentKey, "trigger0");
283 PfConceptKey ptypeKey = new PfConceptKey("TTypeKey", VERSION_001);
285 JpaToscaProperty tp = new JpaToscaProperty(pkey, ptypeKey);
286 tp.setDescription(A_DESCRIPTION);
287 tp.setRequired(false);
288 tp.setDefaultValue(DEFAULT_KEY);
289 tp.setStatus(ToscaProperty.Status.SUPPORTED);
291 JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
292 // Maps and Lists need to be modifiable
293 List<JpaToscaConstraint> constraints = new ArrayList<>(List.of(lsc));
294 tp.setConstraints(constraints);
296 PfConceptKey typeKey = new PfConceptKey("type", VERSION_001);
297 JpaToscaSchemaDefinition tes = new JpaToscaSchemaDefinition(typeKey);
298 tp.setEntrySchema(tes);
299 TreeMap<String, String> metadata = new TreeMap<>(Map.of("metaA", "dataA", "metaB", "dataB"));
300 tp.setMetadata(metadata);