2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Modifications Copyright (C) 2019 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.assertNotNull;
28 import static org.junit.Assert.assertSame;
29 import static org.junit.Assert.assertTrue;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.TreeMap;
35 import org.junit.Test;
36 import org.onap.policy.models.base.PfConceptKey;
37 import org.onap.policy.models.base.PfReferenceKey;
38 import org.onap.policy.models.base.PfValidationResult;
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 JpaToscaEntrySchema tes = new JpaToscaEntrySchema(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 assertTrue(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 assertFalse(tp.compareTo(tp.getKey()) == 0);
116 PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", VERSION_001, "OtherProperty");
117 JpaToscaProperty otherDt = new JpaToscaProperty(otherDtKey);
119 assertFalse(tp.compareTo(otherDt) == 0);
120 otherDt.setKey(pkey);
121 assertFalse(tp.compareTo(otherDt) == 0);
122 otherDt.setType(ptypeKey);
123 assertFalse(tp.compareTo(otherDt) == 0);
124 otherDt.setDescription(A_DESCRIPTION);
125 assertFalse(tp.compareTo(otherDt) == 0);
126 otherDt.setRequired(false);
127 assertFalse(tp.compareTo(otherDt) == 0);
128 otherDt.setDefaultValue(DEFAULT_KEY);
129 assertFalse(tp.compareTo(otherDt) == 0);
130 otherDt.setStatus(ToscaProperty.Status.SUPPORTED);
131 assertFalse(tp.compareTo(otherDt) == 0);
132 assertFalse(tp.compareTo(otherDt) == 0);
133 otherDt.setConstraints(constraints);
134 assertFalse(tp.compareTo(otherDt) == 0);
135 otherDt.setEntrySchema(tes);
136 assertEquals(0, tp.compareTo(otherDt));
138 otherDt.setRequired(true);
139 assertFalse(tp.compareTo(otherDt) == 0);
140 otherDt.setRequired(false);
141 assertEquals(0, tp.compareTo(otherDt));
143 otherDt.setStatus(ToscaProperty.Status.UNSUPPORTED);
144 assertFalse(tp.compareTo(otherDt) == 0);
145 otherDt.setStatus(ToscaProperty.Status.SUPPORTED);
146 assertEquals(0, tp.compareTo(otherDt));
148 assertThatThrownBy(() -> new JpaToscaProperty((JpaToscaProperty) null))
149 .isInstanceOf(NullPointerException.class);
151 assertEquals(3, tp.getKeys().size());
152 assertEquals(2, new JpaToscaProperty().getKeys().size());
154 new JpaToscaProperty().clean();
156 assertEquals(tdtClone0, tp);
158 assertFalse(new JpaToscaProperty().validate(new PfValidationResult()).isValid());
159 assertTrue(tp.validate(new PfValidationResult()).isValid());
161 tp.setDescription(null);
162 assertTrue(tp.validate(new PfValidationResult()).isValid());
163 tp.setDescription("");
164 assertFalse(tp.validate(new PfValidationResult()).isValid());
165 tp.setDescription(A_DESCRIPTION);
166 assertTrue(tp.validate(new PfValidationResult()).isValid());
169 assertFalse(tp.validate(new PfValidationResult()).isValid());
171 assertTrue(tp.validate(new PfValidationResult()).isValid());
173 tp.setType(PfConceptKey.getNullKey());
174 assertFalse(tp.validate(new PfValidationResult()).isValid());
176 assertTrue(tp.validate(new PfValidationResult()).isValid());
178 tp.setDefaultValue(null);
179 assertTrue(tp.validate(new PfValidationResult()).isValid());
180 tp.setDefaultValue("");
181 assertFalse(tp.validate(new PfValidationResult()).isValid());
182 tp.setDefaultValue(DEFAULT_KEY);
183 assertTrue(tp.validate(new PfValidationResult()).isValid());
185 tp.getConstraints().add(null);
186 assertFalse(tp.validate(new PfValidationResult()).isValid());
187 tp.getConstraints().remove(null);
188 assertTrue(tp.validate(new PfValidationResult()).isValid());
190 tp.setMetadata(null);
191 assertTrue(tp.validate(new PfValidationResult()).isValid());
193 assertThatThrownBy(() -> tp.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null");
197 public void testToAuthorative_testFromAuthorative() {
198 // check with empty structure
199 JpaToscaProperty tp = new JpaToscaProperty();
200 ToscaProperty auth = tp.toAuthorative();
201 JpaToscaProperty tp2 = new JpaToscaProperty();
202 tp2.fromAuthorative(auth);
203 assertEquals(tp, tp2);
205 // populate and try again
206 PfConceptKey pparentKey = new PfConceptKey("tParentKey", VERSION_001);
207 PfReferenceKey pkey = new PfReferenceKey(pparentKey, "trigger0");
208 PfConceptKey ptypeKey = new PfConceptKey("TTypeKey", VERSION_001);
209 tp = new JpaToscaProperty(pkey, ptypeKey);
211 tp.setDescription(A_DESCRIPTION);
212 tp.setRequired(true);
213 tp.setDefaultValue(DEFAULT_KEY);
214 tp.setStatus(ToscaProperty.Status.SUPPORTED);
216 List<JpaToscaConstraint> constraints = new ArrayList<>();
217 JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
218 constraints.add(lsc);
219 tp.setConstraints(constraints);
221 PfConceptKey typeKey = new PfConceptKey("type", VERSION_001);
222 JpaToscaEntrySchema tes = new JpaToscaEntrySchema(typeKey);
223 tp.setEntrySchema(tes);
225 TreeMap<String, String> metadata = new TreeMap<>();
226 metadata.put("metaA", "dataA");
227 metadata.put("metaB", "dataB");
228 tp.setMetadata(metadata);
230 auth = tp.toAuthorative();
231 tp2 = new JpaToscaProperty();
232 tp2.fromAuthorative(auth);
234 // note: parent key info is not copied, so we manually copy it
235 tp2.getKey().setParentConceptKey(tp.getKey().getParentConceptKey());
237 assertEquals(tp.toString(), tp2.toString());
238 assertEquals(tp, tp2);