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.base.PfValidationResult;
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 testPropertyPojo() {
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");
70 PfConceptKey pparentKey = new PfConceptKey("tParentKey", VERSION_001);
71 PfReferenceKey pkey = new PfReferenceKey(pparentKey, "trigger0");
72 PfConceptKey ptypeKey = new PfConceptKey("TTypeKey", VERSION_001);
73 JpaToscaProperty tp = new JpaToscaProperty(pkey, ptypeKey);
75 assertEquals(tp, new JpaToscaProperty(tp));
77 tp.setDescription(A_DESCRIPTION);
78 assertEquals(A_DESCRIPTION, tp.getDescription());
80 tp.setRequired(false);
81 assertFalse(tp.isRequired());
83 tp.setDefaultValue(DEFAULT_KEY);
85 tp.setStatus(ToscaProperty.Status.SUPPORTED);
87 List<JpaToscaConstraint> constraints = new ArrayList<>();
88 JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
90 tp.setConstraints(constraints);
91 assertEquals(constraints, tp.getConstraints());
93 PfConceptKey typeKey = new PfConceptKey("type", VERSION_001);
94 JpaToscaEntrySchema tes = new JpaToscaEntrySchema(typeKey);
95 tp.setEntrySchema(tes);
97 TreeMap<String, String> metadata = new TreeMap<>();
98 metadata.put("metaA", "dataA");
99 metadata.put("metaB", "dataB");
100 tp.setMetadata(metadata);
101 assertSame(metadata, tp.getMetadata());
103 JpaToscaProperty tdtClone0 = new JpaToscaProperty(tp);
104 assertEquals(tp, tdtClone0);
105 assertEquals(0, tp.compareTo(tdtClone0));
107 assertNotSame(tdtClone0.getMetadata(), tp.getMetadata());
109 JpaToscaProperty tdtClone1 = new JpaToscaProperty(tp);
110 assertEquals(tp, tdtClone1);
111 assertEquals(0, tp.compareTo(tdtClone1));
113 assertEquals(-1, tp.compareTo(null));
114 assertEquals(0, tp.compareTo(tp));
115 assertNotEquals(0, tp.compareTo(tp.getKey()));
117 PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", VERSION_001, "OtherProperty");
118 JpaToscaProperty otherDt = new JpaToscaProperty(otherDtKey);
120 assertNotEquals(0, tp.compareTo(otherDt));
121 otherDt.setKey(pkey);
122 assertNotEquals(0, tp.compareTo(otherDt));
123 otherDt.setType(ptypeKey);
124 assertNotEquals(0, tp.compareTo(otherDt));
125 otherDt.setDescription(A_DESCRIPTION);
126 assertNotEquals(0, tp.compareTo(otherDt));
127 otherDt.setRequired(false);
128 assertNotEquals(0, tp.compareTo(otherDt));
129 otherDt.setDefaultValue(DEFAULT_KEY);
130 assertNotEquals(0, tp.compareTo(otherDt));
131 otherDt.setStatus(ToscaProperty.Status.SUPPORTED);
132 assertNotEquals(0, tp.compareTo(otherDt));
133 assertNotEquals(0, tp.compareTo(otherDt));
134 otherDt.setConstraints(constraints);
135 assertNotEquals(0, tp.compareTo(otherDt));
136 otherDt.setEntrySchema(tes);
137 assertEquals(0, tp.compareTo(otherDt));
139 otherDt.setRequired(true);
140 assertNotEquals(0, tp.compareTo(otherDt));
141 otherDt.setRequired(false);
142 assertEquals(0, tp.compareTo(otherDt));
144 otherDt.setStatus(ToscaProperty.Status.UNSUPPORTED);
145 assertNotEquals(0, tp.compareTo(otherDt));
146 otherDt.setStatus(ToscaProperty.Status.SUPPORTED);
147 assertEquals(0, tp.compareTo(otherDt));
149 assertThatThrownBy(() -> new JpaToscaProperty((JpaToscaProperty) null))
150 .isInstanceOf(NullPointerException.class);
152 assertEquals(3, tp.getKeys().size());
153 assertEquals(2, new JpaToscaProperty().getKeys().size());
155 new JpaToscaProperty().clean();
157 assertEquals(tdtClone0, tp);
159 assertFalse(new JpaToscaProperty().validate(new PfValidationResult()).isValid());
160 assertTrue(tp.validate(new PfValidationResult()).isValid());
162 tp.setDescription(null);
163 assertTrue(tp.validate(new PfValidationResult()).isValid());
164 tp.setDescription("");
165 assertFalse(tp.validate(new PfValidationResult()).isValid());
166 tp.setDescription(A_DESCRIPTION);
167 assertTrue(tp.validate(new PfValidationResult()).isValid());
170 assertFalse(tp.validate(new PfValidationResult()).isValid());
172 assertTrue(tp.validate(new PfValidationResult()).isValid());
174 tp.setType(PfConceptKey.getNullKey());
175 assertFalse(tp.validate(new PfValidationResult()).isValid());
177 assertTrue(tp.validate(new PfValidationResult()).isValid());
179 tp.setDefaultValue(null);
180 assertTrue(tp.validate(new PfValidationResult()).isValid());
181 tp.setDefaultValue("");
182 assertFalse(tp.validate(new PfValidationResult()).isValid());
183 tp.setDefaultValue(DEFAULT_KEY);
184 assertTrue(tp.validate(new PfValidationResult()).isValid());
186 tp.getConstraints().add(null);
187 assertFalse(tp.validate(new PfValidationResult()).isValid());
188 tp.getConstraints().remove(null);
189 assertTrue(tp.validate(new PfValidationResult()).isValid());
191 tp.setMetadata(null);
192 assertTrue(tp.validate(new PfValidationResult()).isValid());
194 assertThatThrownBy(() -> tp.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null");
198 public void testToAuthorative_testFromAuthorative() {
199 // check with empty structure
200 JpaToscaProperty tp = new JpaToscaProperty();
201 ToscaProperty auth = tp.toAuthorative();
202 JpaToscaProperty tp2 = new JpaToscaProperty();
203 tp2.fromAuthorative(auth);
204 assertEquals(tp, tp2);
206 // populate and try again
207 PfConceptKey pparentKey = new PfConceptKey("tParentKey", VERSION_001);
208 PfReferenceKey pkey = new PfReferenceKey(pparentKey, "trigger0");
209 PfConceptKey ptypeKey = new PfConceptKey("TTypeKey", VERSION_001);
210 tp = new JpaToscaProperty(pkey, ptypeKey);
212 tp.setDescription(A_DESCRIPTION);
213 tp.setRequired(true);
214 tp.setDefaultValue(DEFAULT_KEY);
215 tp.setStatus(ToscaProperty.Status.SUPPORTED);
217 List<JpaToscaConstraint> constraints = new ArrayList<>();
218 JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
219 constraints.add(lsc);
220 tp.setConstraints(constraints);
222 PfConceptKey typeKey = new PfConceptKey("type", VERSION_001);
223 JpaToscaEntrySchema tes = new JpaToscaEntrySchema(typeKey);
224 tp.setEntrySchema(tes);
226 TreeMap<String, String> metadata = new TreeMap<>();
227 metadata.put("metaA", "dataA");
228 metadata.put("metaB", "dataB");
229 tp.setMetadata(metadata);
231 auth = tp.toAuthorative();
232 tp2 = new JpaToscaProperty();
233 tp2.fromAuthorative(auth);
235 // note: parent key info is not copied, so we manually copy it
236 tp2.getKey().setParentConceptKey(tp.getKey().getParentConceptKey());
238 assertEquals(tp.toString(), tp2.toString());
239 assertEquals(tp, tp2);