2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 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.assertTrue;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.LinkedHashMap;
34 import java.util.List;
36 import org.junit.Test;
37 import org.onap.policy.models.base.PfConceptKey;
38 import org.onap.policy.models.base.PfKey;
39 import org.onap.policy.models.base.PfReferenceKey;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
44 * DAO test for ToscaPolicyType.
46 * @author Liam Fallon (liam.fallon@est.tech)
48 public class JpaToscaPolicyTypeTest {
49 private static final String A_DESCRIPTION = "A Description";
50 private static final String VERSION_001 = "0.0.1";
53 public void testPolicyTypeNull() {
54 assertNotNull(new JpaToscaPolicyType());
55 assertNotNull(new JpaToscaPolicyType(new PfConceptKey()));
56 assertNotNull(new JpaToscaPolicyType(new JpaToscaPolicyType()));
58 assertThatThrownBy(() -> new JpaToscaPolicyType((PfConceptKey) null))
59 .hasMessageMatching("key is marked .*on.*ull but is null");
61 assertThatThrownBy(() -> new JpaToscaPolicyType((JpaToscaPolicyType) null))
62 .isInstanceOf(NullPointerException.class);
66 public void testPolicyTypePojo() {
67 PfConceptKey ptKey = new PfConceptKey("tdt", VERSION_001);
68 JpaToscaPolicyType tpt = new JpaToscaPolicyType(ptKey);
70 PfConceptKey derivedFromKey = new PfConceptKey("deriveFrom", VERSION_001);
71 tpt.setDerivedFrom(derivedFromKey);
73 Map<String, String> metadata = new HashMap<>();
74 metadata.put("key", "value");
75 tpt.setMetadata(metadata);
76 assertEquals(metadata, tpt.getMetadata());
78 tpt.setDescription(A_DESCRIPTION);
80 PfConceptKey propTypeKey = new PfConceptKey("propType", VERSION_001);
81 Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
82 JpaToscaProperty tp = new JpaToscaProperty(new PfReferenceKey(ptKey, "aProp"), propTypeKey);
83 properties.put(tp.getKey().getLocalName(), tp);
84 tpt.setProperties(properties);
85 assertEquals(properties, tpt.getProperties());
87 List<PfConceptKey> targets = new ArrayList<>();
88 PfConceptKey target = new PfConceptKey("target", VERSION_001);
90 tpt.setTargets(targets);
91 assertEquals(targets, tpt.getTargets());
93 List<JpaToscaTrigger> triggers = new ArrayList<>();
94 JpaToscaTrigger trigger = new JpaToscaTrigger(new PfReferenceKey(ptKey, "aTrigger"), "EventType", "Action");
95 triggers.add(trigger);
96 tpt.setTriggers(triggers);
97 assertEquals(triggers, tpt.getTriggers());
99 JpaToscaPolicyType tdtClone0 = new JpaToscaPolicyType(tpt);
100 assertEquals(tpt, tdtClone0);
101 assertEquals(0, tpt.compareTo(tdtClone0));
103 JpaToscaPolicyType tdtClone1 = new JpaToscaPolicyType(tpt);
104 assertEquals(tpt, tdtClone1);
105 assertEquals(0, tpt.compareTo(tdtClone1));
107 assertEquals(-1, tpt.compareTo(null));
108 assertEquals(0, tpt.compareTo(tpt));
109 assertNotEquals(0, tpt.compareTo(tpt.getKey()));
111 PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
112 JpaToscaPolicyType otherDt = new JpaToscaPolicyType(otherDtKey);
114 assertNotEquals(0, tpt.compareTo(otherDt));
115 otherDt.setKey(ptKey);
116 assertNotEquals(0, tpt.compareTo(otherDt));
117 otherDt.setDerivedFrom(derivedFromKey);
118 assertNotEquals(0, tpt.compareTo(otherDt));
119 otherDt.setMetadata(metadata);
120 assertNotEquals(0, tpt.compareTo(otherDt));
121 otherDt.setDescription(A_DESCRIPTION);
122 assertNotEquals(0, tpt.compareTo(otherDt));
123 otherDt.setProperties(properties);
124 assertNotEquals(0, tpt.compareTo(otherDt));
125 otherDt.setTargets(targets);
126 assertNotEquals(0, tpt.compareTo(otherDt));
127 otherDt.setTriggers(triggers);
128 assertEquals(0, tpt.compareTo(otherDt));
130 new JpaToscaPolicyType().clean();
132 assertEquals(tdtClone0, tpt);
136 public void testPolicyTypeValidation() {
137 JpaToscaPolicyType tpt = setUpJpaToscaPolicyType();
139 assertEquals(6, tpt.getKeys().size());
140 assertEquals(1, new JpaToscaPolicyType().getKeys().size());
142 assertFalse(new JpaToscaPolicyType().validate("").isValid());
143 assertTrue(tpt.validate("").isValid());
145 tpt.getProperties().put(null, null);
146 assertFalse(tpt.validate("").isValid());
147 tpt.getProperties().remove(null);
148 assertTrue(tpt.validate("").isValid());
150 tpt.getTargets().add(null);
151 assertFalse(tpt.validate("").isValid());
152 tpt.getTargets().remove(null);
153 assertTrue(tpt.validate("").isValid());
155 tpt.getTriggers().add(null);
156 assertFalse(tpt.validate("").isValid());
157 tpt.getTriggers().remove(null);
158 assertTrue(tpt.validate("").isValid());
162 public void testPolicyTypeValidation2() {
163 JpaToscaPolicyType tpt = setUpJpaToscaPolicyType();
165 tpt.getMetadata().put(null, null);
166 assertFalse(tpt.validate("").isValid());
167 tpt.getMetadata().remove(null);
168 assertTrue(tpt.validate("").isValid());
170 tpt.getMetadata().put("nullKey", null);
171 assertFalse(tpt.validate("").isValid());
172 tpt.getMetadata().remove("nullKey");
173 assertTrue(tpt.validate("").isValid());
175 tpt.setDescription("");
177 assertFalse(tpt.validate("").isValid());
178 tpt.setDescription(A_DESCRIPTION);
179 assertTrue(tpt.validate("").isValid());
181 tpt.setDerivedFrom(PfConceptKey.getNullKey());
182 assertFalse(tpt.validate("").isValid());
184 PfConceptKey derivedFromKey = new PfConceptKey("deriveFrom", VERSION_001);
185 tpt.setDerivedFrom(derivedFromKey);
186 assertTrue(tpt.validate("").isValid());
190 public void testPolicyTypeEntity() {
191 JpaToscaPolicyType tpt = setUpJpaToscaPolicyType();
193 assertThatThrownBy(() -> tpt.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");
195 assertThatThrownBy(() -> new JpaToscaEntityType<ToscaPolicy>((PfConceptKey) null))
196 .hasMessageMatching("key is marked .*on.*ull but is null");
198 assertThatThrownBy(() -> new JpaToscaEntityType<ToscaPolicy>((JpaToscaEntityType<ToscaPolicy>) null))
199 .isInstanceOf(NullPointerException.class);
201 JpaToscaEntityType<ToscaPolicy> tet = new JpaToscaEntityType<>(tpt.getKey());
202 assertEquals(-1, tet.compareTo(null));
203 assertEquals(0, tet.compareTo(tet));
204 assertNotEquals(0, tet.compareTo(tet.getKey()));
206 assertNotNull(new JpaToscaPolicyType(new ToscaPolicyType()));
208 assertNotNull(new JpaToscaEntityType<ToscaPolicyType>(new ToscaPolicyType()));
212 public void testGetReferencedDataTypes() {
213 JpaToscaPolicyType pt0 = new JpaToscaPolicyType(new PfConceptKey("pt0", "0.0.1"));
215 assertTrue(pt0.getReferencedDataTypes().isEmpty());
217 pt0.setProperties(new LinkedHashMap<>());
218 assertTrue(pt0.getReferencedDataTypes().isEmpty());
220 JpaToscaProperty prop0 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop0"));
221 prop0.setType(new PfConceptKey("string", PfKey.NULL_KEY_VERSION));
222 assertTrue(prop0.validate("").isValid());
224 pt0.getProperties().put(prop0.getKey().getLocalName(), prop0);
225 assertTrue(pt0.getReferencedDataTypes().isEmpty());
227 JpaToscaProperty prop1 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop1"));
228 prop1.setType(new PfConceptKey("the.property.Type0", "0.0.1"));
229 assertTrue(prop1.validate("").isValid());
231 pt0.getProperties().put(prop1.getKey().getLocalName(), prop1);
232 assertEquals(1, pt0.getReferencedDataTypes().size());
234 JpaToscaProperty prop2 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop2"));
235 prop2.setType(new PfConceptKey("the.property.Type0", "0.0.1"));
236 assertTrue(prop2.validate("").isValid());
238 pt0.getProperties().put(prop2.getKey().getLocalName(), prop2);
239 assertEquals(1, pt0.getReferencedDataTypes().size());
241 JpaToscaProperty prop3 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop4"));
242 prop3.setType(new PfConceptKey("the.property.Type1", "0.0.1"));
243 prop3.setEntrySchema(new JpaToscaSchemaDefinition());
244 prop3.getEntrySchema().setType(new PfConceptKey("the.property.Type3", "0.0.1"));
245 assertTrue(prop3.validate("").isValid());
247 pt0.getProperties().put(prop3.getKey().getLocalName(), prop3);
248 assertEquals(3, pt0.getReferencedDataTypes().size());
250 JpaToscaProperty prop4 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop4"));
251 prop4.setType(new PfConceptKey("the.property.Type1", "0.0.1"));
252 prop4.setEntrySchema(new JpaToscaSchemaDefinition());
253 prop4.getEntrySchema().setType(new PfConceptKey("the.property.Type2", "0.0.1"));
254 assertTrue(prop4.validate("").isValid());
256 pt0.getProperties().put(prop4.getKey().getLocalName(), prop4);
257 assertEquals(3, pt0.getReferencedDataTypes().size());
260 private JpaToscaPolicyType setUpJpaToscaPolicyType() {
261 PfConceptKey ptKey = new PfConceptKey("tdt", VERSION_001);
262 JpaToscaPolicyType tpt = new JpaToscaPolicyType(ptKey);
264 PfConceptKey derivedFromKey = new PfConceptKey("deriveFrom", VERSION_001);
265 tpt.setDerivedFrom(derivedFromKey);
266 // Maps and Lists need to be modifiable
267 Map<String, String> metadata = new HashMap<>(Map.of("key", "value"));
268 tpt.setMetadata(metadata);
270 tpt.setDescription(A_DESCRIPTION);
272 PfConceptKey propTypeKey = new PfConceptKey("propType", VERSION_001);
273 JpaToscaProperty tp = new JpaToscaProperty(new PfReferenceKey(ptKey, "aProp"), propTypeKey);
274 Map<String, JpaToscaProperty> properties = new LinkedHashMap<>(Map.of(tp.getKey().getLocalName(), tp));
275 tpt.setProperties(properties);
277 PfConceptKey target = new PfConceptKey("target", VERSION_001);
278 List<PfConceptKey> targets = new ArrayList<>(List.of(target));
279 tpt.setTargets(targets);
281 JpaToscaTrigger trigger = new JpaToscaTrigger(new PfReferenceKey(ptKey, "aTrigger"), "EventType", "Action");
282 List<JpaToscaTrigger> triggers = new ArrayList<>(List.of(trigger));
283 tpt.setTriggers(triggers);