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;
34 import org.junit.Test;
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.base.PfReferenceKey;
37 import org.onap.policy.models.base.PfValidationResult;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
41 * DAO test for ToscaProperty.
43 * @author Liam Fallon (liam.fallon@est.tech)
45 public class JpaToscaPropertyTest {
47 private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
48 private static final String DEFAULT_KEY = "defaultKey";
49 private static final String A_DESCRIPTION = "A Description";
50 private static final String VERSION_001 = "0.0.1";
53 public void testPropertyPojo() {
54 assertNotNull(new JpaToscaProperty());
55 assertNotNull(new JpaToscaProperty(new PfReferenceKey()));
56 assertNotNull(new JpaToscaProperty(new PfReferenceKey(), new PfConceptKey()));
57 assertNotNull(new JpaToscaProperty(new JpaToscaProperty()));
59 assertThatThrownBy(() -> new JpaToscaProperty((PfReferenceKey) null)).hasMessageMatching(KEY_IS_NULL);
61 assertThatThrownBy(() -> new JpaToscaProperty(null, null)).hasMessageMatching(KEY_IS_NULL);
63 assertThatThrownBy(() -> new JpaToscaProperty(null, new PfConceptKey())).hasMessageMatching(KEY_IS_NULL);
65 assertThatThrownBy(() -> new JpaToscaProperty(new PfReferenceKey(), null))
66 .hasMessageMatching("type is marked .*on.*ull but is null");
68 PfConceptKey pparentKey = new PfConceptKey("tParentKey", VERSION_001);
69 PfReferenceKey pkey = new PfReferenceKey(pparentKey, "trigger0");
70 PfConceptKey ptypeKey = new PfConceptKey("TTypeKey", VERSION_001);
71 JpaToscaProperty tp = new JpaToscaProperty(pkey, ptypeKey);
73 assertEquals(tp, new JpaToscaProperty(tp));
75 tp.setDescription(A_DESCRIPTION);
76 assertEquals(A_DESCRIPTION, tp.getDescription());
78 tp.setRequired(false);
79 assertFalse(tp.isRequired());
81 tp.setDefaultValue(DEFAULT_KEY);
83 tp.setStatus(ToscaProperty.Status.SUPPORTED);
85 List<JpaToscaConstraint> constraints = new ArrayList<>();
86 JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
88 tp.setConstraints(constraints);
89 assertEquals(constraints, tp.getConstraints());
91 PfConceptKey typeKey = new PfConceptKey("type", VERSION_001);
92 JpaToscaEntrySchema tes = new JpaToscaEntrySchema(typeKey);
93 tp.setEntrySchema(tes);
95 TreeMap<String, String> metadata = new TreeMap<>();
96 metadata.put("metaA", "dataA");
97 metadata.put("metaB", "dataB");
98 tp.setMetadata(metadata);
99 assertSame(metadata, tp.getMetadata());
101 JpaToscaProperty tdtClone0 = new JpaToscaProperty(tp);
102 assertEquals(tp, tdtClone0);
103 assertEquals(0, tp.compareTo(tdtClone0));
105 assertTrue(tdtClone0.getMetadata() != tp.getMetadata());
107 JpaToscaProperty tdtClone1 = new JpaToscaProperty(tp);
108 assertEquals(tp, tdtClone1);
109 assertEquals(0, tp.compareTo(tdtClone1));
111 assertEquals(-1, tp.compareTo(null));
112 assertEquals(0, tp.compareTo(tp));
113 assertFalse(tp.compareTo(tp.getKey()) == 0);
115 PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", VERSION_001, "OtherProperty");
116 JpaToscaProperty otherDt = new JpaToscaProperty(otherDtKey);
118 assertFalse(tp.compareTo(otherDt) == 0);
119 otherDt.setKey(pkey);
120 assertFalse(tp.compareTo(otherDt) == 0);
121 otherDt.setType(ptypeKey);
122 assertFalse(tp.compareTo(otherDt) == 0);
123 otherDt.setDescription(A_DESCRIPTION);
124 assertFalse(tp.compareTo(otherDt) == 0);
125 otherDt.setRequired(false);
126 assertFalse(tp.compareTo(otherDt) == 0);
127 otherDt.setDefaultValue(DEFAULT_KEY);
128 assertFalse(tp.compareTo(otherDt) == 0);
129 otherDt.setStatus(ToscaProperty.Status.SUPPORTED);
130 assertFalse(tp.compareTo(otherDt) == 0);
131 assertFalse(tp.compareTo(otherDt) == 0);
132 otherDt.setConstraints(constraints);
133 assertFalse(tp.compareTo(otherDt) == 0);
134 otherDt.setEntrySchema(tes);
135 assertEquals(0, tp.compareTo(otherDt));
137 otherDt.setRequired(true);
138 assertFalse(tp.compareTo(otherDt) == 0);
139 otherDt.setRequired(false);
140 assertEquals(0, tp.compareTo(otherDt));
142 otherDt.setStatus(ToscaProperty.Status.UNSUPPORTED);
143 assertFalse(tp.compareTo(otherDt) == 0);
144 otherDt.setStatus(ToscaProperty.Status.SUPPORTED);
145 assertEquals(0, tp.compareTo(otherDt));
147 assertThatThrownBy(() -> new JpaToscaProperty((JpaToscaProperty) null))
148 .isInstanceOf(NullPointerException.class);
150 assertEquals(3, tp.getKeys().size());
151 assertEquals(2, new JpaToscaProperty().getKeys().size());
153 new JpaToscaProperty().clean();
155 assertEquals(tdtClone0, tp);
157 assertFalse(new JpaToscaProperty().validate(new PfValidationResult()).isValid());
158 assertTrue(tp.validate(new PfValidationResult()).isValid());
160 tp.setDescription(null);
161 assertTrue(tp.validate(new PfValidationResult()).isValid());
162 tp.setDescription("");
163 assertFalse(tp.validate(new PfValidationResult()).isValid());
164 tp.setDescription(A_DESCRIPTION);
165 assertTrue(tp.validate(new PfValidationResult()).isValid());
168 assertFalse(tp.validate(new PfValidationResult()).isValid());
170 assertTrue(tp.validate(new PfValidationResult()).isValid());
172 tp.setType(PfConceptKey.getNullKey());
173 assertFalse(tp.validate(new PfValidationResult()).isValid());
175 assertTrue(tp.validate(new PfValidationResult()).isValid());
177 tp.setDefaultValue(null);
178 assertTrue(tp.validate(new PfValidationResult()).isValid());
179 tp.setDefaultValue("");
180 assertFalse(tp.validate(new PfValidationResult()).isValid());
181 tp.setDefaultValue(DEFAULT_KEY);
182 assertTrue(tp.validate(new PfValidationResult()).isValid());
184 tp.getConstraints().add(null);
185 assertFalse(tp.validate(new PfValidationResult()).isValid());
186 tp.getConstraints().remove(null);
187 assertTrue(tp.validate(new PfValidationResult()).isValid());
189 tp.setMetadata(null);
190 assertTrue(tp.validate(new PfValidationResult()).isValid());
192 assertThatThrownBy(() -> tp.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null");
196 public void testToAuthorative_testFromAuthorative() {
197 // check with empty structure
198 JpaToscaProperty tp = new JpaToscaProperty();
199 ToscaProperty auth = tp.toAuthorative();
200 JpaToscaProperty tp2 = new JpaToscaProperty();
201 tp2.fromAuthorative(auth);
202 assertEquals(tp, tp2);
204 // populate and try again
205 PfConceptKey pparentKey = new PfConceptKey("tParentKey", VERSION_001);
206 PfReferenceKey pkey = new PfReferenceKey(pparentKey, "trigger0");
207 PfConceptKey ptypeKey = new PfConceptKey("TTypeKey", VERSION_001);
208 tp = new JpaToscaProperty(pkey, ptypeKey);
210 tp.setDescription(A_DESCRIPTION);
211 tp.setRequired(true);
212 tp.setDefaultValue(DEFAULT_KEY);
213 tp.setStatus(ToscaProperty.Status.SUPPORTED);
215 List<JpaToscaConstraint> constraints = new ArrayList<>();
216 JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
217 constraints.add(lsc);
218 tp.setConstraints(constraints);
220 PfConceptKey typeKey = new PfConceptKey("type", VERSION_001);
221 JpaToscaEntrySchema tes = new JpaToscaEntrySchema(typeKey);
222 tp.setEntrySchema(tes);
224 TreeMap<String, String> metadata = new TreeMap<>();
225 metadata.put("metaA", "dataA");
226 metadata.put("metaB", "dataB");
227 tp.setMetadata(metadata);
229 auth = tp.toAuthorative();
230 tp2 = new JpaToscaProperty();
231 tp2.fromAuthorative(auth);
233 // note: parent key info is not copied, so we manually copy it
234 tp2.getKey().setParentConceptKey(tp.getKey().getParentConceptKey());
236 assertEquals(tp.toString(), tp2.toString());
237 assertEquals(tp, tp2);