2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 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;
35 import java.util.TreeMap;
36 import org.junit.Test;
37 import org.onap.policy.models.base.PfConceptKey;
38 import org.onap.policy.models.base.PfReferenceKey;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
42 * DAO test for ToscaProperty.
44 * @author Liam Fallon (liam.fallon@est.tech)
46 public class JpaToscaPropertyTest {
48 private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
49 private static final String DEFAULT_KEY = "defaultKey";
50 private static final String A_DESCRIPTION = "A Description";
51 private static final String VERSION_001 = "0.0.1";
54 public void testPropertyPojo() {
55 assertNotNull(new JpaToscaProperty());
56 assertNotNull(new JpaToscaProperty(new PfReferenceKey()));
57 assertNotNull(new JpaToscaProperty(new PfReferenceKey(), new PfConceptKey()));
58 assertNotNull(new JpaToscaProperty(new JpaToscaProperty()));
60 assertThatThrownBy(() -> new JpaToscaProperty((PfReferenceKey) null)).hasMessageMatching(KEY_IS_NULL);
62 assertThatThrownBy(() -> new JpaToscaProperty(null, null)).hasMessageMatching(KEY_IS_NULL);
64 assertThatThrownBy(() -> new JpaToscaProperty(null, new PfConceptKey())).hasMessageMatching(KEY_IS_NULL);
66 assertThatThrownBy(() -> new JpaToscaProperty(new PfReferenceKey(), null))
67 .hasMessageMatching("type is marked .*on.*ull but is null");
69 PfConceptKey pparentKey = new PfConceptKey("tParentKey", VERSION_001);
70 PfReferenceKey pkey = new PfReferenceKey(pparentKey, "trigger0");
71 PfConceptKey ptypeKey = new PfConceptKey("TTypeKey", VERSION_001);
72 JpaToscaProperty tp = new JpaToscaProperty(pkey, ptypeKey);
74 assertEquals(tp, new JpaToscaProperty(tp));
76 tp.setDescription(A_DESCRIPTION);
77 assertEquals(A_DESCRIPTION, tp.getDescription());
79 tp.setRequired(false);
80 assertFalse(tp.isRequired());
82 tp.setDefaultValue(DEFAULT_KEY);
84 tp.setStatus(ToscaProperty.Status.SUPPORTED);
86 List<JpaToscaConstraint> constraints = new ArrayList<>();
87 JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
89 tp.setConstraints(constraints);
90 assertEquals(constraints, tp.getConstraints());
92 PfConceptKey typeKey = new PfConceptKey("type", VERSION_001);
93 JpaToscaSchemaDefinition tes = new JpaToscaSchemaDefinition(typeKey);
94 tp.setEntrySchema(tes);
96 TreeMap<String, String> metadata = new TreeMap<>();
97 metadata.put("metaA", "dataA");
98 metadata.put("metaB", "dataB");
99 tp.setMetadata(metadata);
100 assertSame(metadata, tp.getMetadata());
102 JpaToscaProperty tdtClone0 = new JpaToscaProperty(tp);
103 assertEquals(tp, tdtClone0);
104 assertEquals(0, tp.compareTo(tdtClone0));
106 assertNotSame(tdtClone0.getMetadata(), tp.getMetadata());
108 JpaToscaProperty tdtClone1 = new JpaToscaProperty(tp);
109 assertEquals(tp, tdtClone1);
110 assertEquals(0, tp.compareTo(tdtClone1));
112 assertEquals(-1, tp.compareTo(null));
113 assertEquals(0, tp.compareTo(tp));
114 assertNotEquals(0, tp.compareTo(tp.getKey()));
116 PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", VERSION_001, "OtherProperty");
117 JpaToscaProperty otherDt = new JpaToscaProperty(otherDtKey);
119 assertNotEquals(0, tp.compareTo(otherDt));
120 otherDt.setKey(pkey);
121 assertNotEquals(0, tp.compareTo(otherDt));
122 otherDt.setType(ptypeKey);
123 assertNotEquals(0, tp.compareTo(otherDt));
124 otherDt.setDescription(A_DESCRIPTION);
125 assertNotEquals(0, tp.compareTo(otherDt));
126 otherDt.setRequired(false);
127 assertNotEquals(0, tp.compareTo(otherDt));
128 otherDt.setDefaultValue(DEFAULT_KEY);
129 assertNotEquals(0, tp.compareTo(otherDt));
130 otherDt.setStatus(ToscaProperty.Status.SUPPORTED);
131 assertNotEquals(0, tp.compareTo(otherDt));
132 assertNotEquals(0, tp.compareTo(otherDt));
133 otherDt.setConstraints(constraints);
134 assertNotEquals(0, tp.compareTo(otherDt));
135 otherDt.setEntrySchema(tes);
136 assertNotEquals(0, tp.compareTo(otherDt));
137 otherDt.setMetadata(metadata);
138 assertEquals(0, tp.compareTo(otherDt));
140 otherDt.setRequired(true);
141 assertNotEquals(0, tp.compareTo(otherDt));
142 otherDt.setRequired(false);
143 assertEquals(0, tp.compareTo(otherDt));
145 otherDt.setStatus(ToscaProperty.Status.UNSUPPORTED);
146 assertNotEquals(0, tp.compareTo(otherDt));
147 otherDt.setStatus(ToscaProperty.Status.SUPPORTED);
148 assertEquals(0, tp.compareTo(otherDt));
150 assertThatThrownBy(() -> new JpaToscaProperty((JpaToscaProperty) null))
151 .isInstanceOf(NullPointerException.class);
153 assertEquals(3, tp.getKeys().size());
154 assertEquals(2, new JpaToscaProperty().getKeys().size());
156 new JpaToscaProperty().clean();
158 assertEquals(tdtClone0, tp);
160 assertFalse(new JpaToscaProperty().validate("").isValid());
161 assertTrue(tp.validate("").isValid());
163 tp.setDescription(null);
164 assertTrue(tp.validate("").isValid());
165 tp.setDescription("");
166 assertFalse(tp.validate("").isValid());
167 tp.setDescription(A_DESCRIPTION);
168 assertTrue(tp.validate("").isValid());
171 assertFalse(tp.validate("").isValid());
173 assertTrue(tp.validate("").isValid());
175 tp.setType(PfConceptKey.getNullKey());
176 assertFalse(tp.validate("").isValid());
178 assertTrue(tp.validate("").isValid());
180 tp.setDefaultValue(null);
181 assertTrue(tp.validate("").isValid());
182 tp.setDefaultValue("");
183 assertFalse(tp.validate("").isValid());
184 tp.setDefaultValue(DEFAULT_KEY);
185 assertTrue(tp.validate("").isValid());
187 tp.getConstraints().add(null);
188 assertFalse(tp.validate("").isValid());
189 tp.getConstraints().remove(null);
190 assertTrue(tp.validate("").isValid());
192 tp.setMetadata(null);
193 assertTrue(tp.validate("").isValid());
195 assertThatThrownBy(() -> tp.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");
199 public void testToAuthorative_testFromAuthorative() {
200 // check with empty structure
201 JpaToscaProperty tp = new JpaToscaProperty();
202 ToscaProperty auth = tp.toAuthorative();
203 JpaToscaProperty tp2 = new JpaToscaProperty();
204 tp2.fromAuthorative(auth);
205 assertEquals(tp, tp2);
207 // populate and try again
208 PfConceptKey pparentKey = new PfConceptKey("tParentKey", VERSION_001);
209 PfReferenceKey pkey = new PfReferenceKey(pparentKey, "trigger0");
210 PfConceptKey ptypeKey = new PfConceptKey("TTypeKey", VERSION_001);
211 tp = new JpaToscaProperty(pkey, ptypeKey);
213 tp.setDescription(A_DESCRIPTION);
214 tp.setRequired(true);
215 tp.setDefaultValue(DEFAULT_KEY);
216 tp.setStatus(ToscaProperty.Status.SUPPORTED);
218 List<JpaToscaConstraint> constraints = new ArrayList<>();
219 JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
220 constraints.add(lsc);
221 tp.setConstraints(constraints);
223 PfConceptKey typeKey = new PfConceptKey("type", VERSION_001);
224 JpaToscaSchemaDefinition tes = new JpaToscaSchemaDefinition(typeKey);
225 tp.setEntrySchema(tes);
227 TreeMap<String, String> metadata = new TreeMap<>();
228 metadata.put("metaA", "dataA");
229 metadata.put("metaB", "dataB");
230 tp.setMetadata(metadata);
232 auth = tp.toAuthorative();
233 tp2 = new JpaToscaProperty();
234 tp2.fromAuthorative(auth);
236 // note: parent key info is not copied, so we manually copy it
237 tp2.getKey().setParentConceptKey(tp.getKey().getParentConceptKey());
239 assertEquals(tp.toString(), tp2.toString());
240 assertEquals(tp, tp2);