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.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.base.PfValidationResult;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
45 * DAO test for ToscaPolicyType.
47 * @author Liam Fallon (liam.fallon@est.tech)
49 public class JpaToscaPolicyTypeTest {
50 private static final String A_DESCRIPTION = "A Description";
51 private static final String VERSION_001 = "0.0.1";
54 public void testPolicyTypePojo() {
55 assertNotNull(new JpaToscaPolicyType());
56 assertNotNull(new JpaToscaPolicyType(new PfConceptKey()));
57 assertNotNull(new JpaToscaPolicyType(new JpaToscaPolicyType()));
59 assertThatThrownBy(() -> new JpaToscaPolicyType((PfConceptKey) null))
60 .hasMessageMatching("key is marked .*on.*ull but is null");
62 assertThatThrownBy(() -> new JpaToscaPolicyType((JpaToscaPolicyType) null))
63 .isInstanceOf(NullPointerException.class);
65 PfConceptKey ptKey = new PfConceptKey("tdt", VERSION_001);
66 JpaToscaPolicyType tpt = new JpaToscaPolicyType(ptKey);
68 PfConceptKey derivedFromKey = new PfConceptKey("deriveFrom", VERSION_001);
69 tpt.setDerivedFrom(derivedFromKey);
71 Map<String, String> metadata = new HashMap<>();
72 metadata.put("key", "value");
73 tpt.setMetadata(metadata);
74 assertEquals(metadata, tpt.getMetadata());
76 tpt.setDescription(A_DESCRIPTION);
78 PfConceptKey propTypeKey = new PfConceptKey("propType", VERSION_001);
79 Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
80 JpaToscaProperty tp = new JpaToscaProperty(new PfReferenceKey(ptKey, "aProp"), propTypeKey);
81 properties.put(tp.getKey().getLocalName(), tp);
82 tpt.setProperties(properties);
83 assertEquals(properties, tpt.getProperties());
85 List<PfConceptKey> targets = new ArrayList<>();
86 PfConceptKey target = new PfConceptKey("target", VERSION_001);
88 tpt.setTargets(targets);
89 assertEquals(targets, tpt.getTargets());
91 List<JpaToscaTrigger> triggers = new ArrayList<>();
92 JpaToscaTrigger trigger = new JpaToscaTrigger(new PfReferenceKey(ptKey, "aTrigger"), "EventType", "Action");
93 triggers.add(trigger);
94 tpt.setTriggers(triggers);
95 assertEquals(triggers, tpt.getTriggers());
97 JpaToscaPolicyType tdtClone0 = new JpaToscaPolicyType(tpt);
98 assertEquals(tpt, tdtClone0);
99 assertEquals(0, tpt.compareTo(tdtClone0));
101 JpaToscaPolicyType tdtClone1 = new JpaToscaPolicyType(tpt);
102 assertEquals(tpt, tdtClone1);
103 assertEquals(0, tpt.compareTo(tdtClone1));
105 assertEquals(-1, tpt.compareTo(null));
106 assertEquals(0, tpt.compareTo(tpt));
107 assertNotEquals(0, tpt.compareTo(tpt.getKey()));
109 PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
110 JpaToscaPolicyType otherDt = new JpaToscaPolicyType(otherDtKey);
112 assertNotEquals(0, tpt.compareTo(otherDt));
113 otherDt.setKey(ptKey);
114 assertNotEquals(0, tpt.compareTo(otherDt));
115 otherDt.setDerivedFrom(derivedFromKey);
116 assertNotEquals(0, tpt.compareTo(otherDt));
117 otherDt.setMetadata(metadata);
118 assertNotEquals(0, tpt.compareTo(otherDt));
119 otherDt.setDescription(A_DESCRIPTION);
120 assertNotEquals(0, tpt.compareTo(otherDt));
121 otherDt.setProperties(properties);
122 assertNotEquals(0, tpt.compareTo(otherDt));
123 otherDt.setTargets(targets);
124 assertNotEquals(0, tpt.compareTo(otherDt));
125 otherDt.setTriggers(triggers);
126 assertEquals(0, tpt.compareTo(otherDt));
128 assertEquals(6, tpt.getKeys().size());
129 assertEquals(1, new JpaToscaPolicyType().getKeys().size());
131 new JpaToscaPolicyType().clean();
133 assertEquals(tdtClone0, tpt);
135 assertFalse(new JpaToscaPolicyType().validate(new PfValidationResult()).isValid());
136 assertTrue(tpt.validate(new PfValidationResult()).isValid());
138 tpt.getProperties().put(null, null);
139 assertFalse(tpt.validate(new PfValidationResult()).isValid());
140 tpt.getProperties().remove(null);
141 assertTrue(tpt.validate(new PfValidationResult()).isValid());
143 tpt.getTargets().add(null);
144 assertFalse(tpt.validate(new PfValidationResult()).isValid());
145 tpt.getTargets().remove(null);
146 assertTrue(tpt.validate(new PfValidationResult()).isValid());
148 tpt.getTriggers().add(null);
149 assertFalse(tpt.validate(new PfValidationResult()).isValid());
150 tpt.getTriggers().remove(null);
151 assertTrue(tpt.validate(new PfValidationResult()).isValid());
153 tpt.getMetadata().put(null, null);
154 assertFalse(tpt.validate(new PfValidationResult()).isValid());
155 tpt.getMetadata().remove(null);
156 assertTrue(tpt.validate(new PfValidationResult()).isValid());
158 tpt.getMetadata().put("nullKey", null);
159 assertFalse(tpt.validate(new PfValidationResult()).isValid());
160 tpt.getMetadata().remove("nullKey");
161 assertTrue(tpt.validate(new PfValidationResult()).isValid());
163 tpt.setDescription("");
165 assertFalse(tpt.validate(new PfValidationResult()).isValid());
166 tpt.setDescription(A_DESCRIPTION);
167 assertTrue(tpt.validate(new PfValidationResult()).isValid());
169 tpt.setDerivedFrom(PfConceptKey.getNullKey());
170 assertFalse(tpt.validate(new PfValidationResult()).isValid());
171 tpt.setDerivedFrom(derivedFromKey);
172 assertTrue(tpt.validate(new PfValidationResult()).isValid());
174 assertThatThrownBy(() -> tpt.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null");
176 assertThatThrownBy(() -> new JpaToscaEntityType<ToscaPolicy>((PfConceptKey) null))
177 .hasMessageMatching("key is marked .*on.*ull but is null");
179 assertThatThrownBy(() -> new JpaToscaEntityType<ToscaPolicy>((JpaToscaEntityType<ToscaPolicy>) null))
180 .isInstanceOf(NullPointerException.class);
182 JpaToscaEntityType<ToscaPolicy> tet = new JpaToscaEntityType<>(tpt.getKey());
183 assertEquals(-1, tet.compareTo(null));
184 assertEquals(0, tet.compareTo(tet));
185 assertNotEquals(0, tet.compareTo(tet.getKey()));
187 assertNotNull(new JpaToscaPolicyType(new ToscaPolicyType()));
189 assertNotNull(new JpaToscaEntityType<ToscaPolicyType>(new ToscaPolicyType()));
193 public void testGetReferencedDataTypes() {
194 JpaToscaPolicyType pt0 = new JpaToscaPolicyType(new PfConceptKey("pt0", "0.0.1"));
196 assertTrue(pt0.getReferencedDataTypes().isEmpty());
198 pt0.setProperties(new LinkedHashMap<>());
199 assertTrue(pt0.getReferencedDataTypes().isEmpty());
201 JpaToscaProperty prop0 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop0"));
202 prop0.setType(new PfConceptKey("string", PfKey.NULL_KEY_VERSION));
203 assertTrue(prop0.validate(new PfValidationResult()).isValid());
205 pt0.getProperties().put(prop0.getKey().getLocalName(), prop0);
206 assertTrue(pt0.getReferencedDataTypes().isEmpty());
208 JpaToscaProperty prop1 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop1"));
209 prop1.setType(new PfConceptKey("the.property.Type0", "0.0.1"));
210 assertTrue(prop1.validate(new PfValidationResult()).isValid());
212 pt0.getProperties().put(prop1.getKey().getLocalName(), prop1);
213 assertEquals(1, pt0.getReferencedDataTypes().size());
215 JpaToscaProperty prop2 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop2"));
216 prop2.setType(new PfConceptKey("the.property.Type0", "0.0.1"));
217 assertTrue(prop2.validate(new PfValidationResult()).isValid());
219 pt0.getProperties().put(prop2.getKey().getLocalName(), prop2);
220 assertEquals(1, pt0.getReferencedDataTypes().size());
222 JpaToscaProperty prop3 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop4"));
223 prop3.setType(new PfConceptKey("the.property.Type1", "0.0.1"));
224 prop3.setEntrySchema(new JpaToscaEntrySchema());
225 prop3.getEntrySchema().setType(new PfConceptKey("the.property.Type3", "0.0.1"));
226 assertTrue(prop3.validate(new PfValidationResult()).isValid());
228 pt0.getProperties().put(prop3.getKey().getLocalName(), prop3);
229 assertEquals(3, pt0.getReferencedDataTypes().size());
231 JpaToscaProperty prop4 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop4"));
232 prop4.setType(new PfConceptKey("the.property.Type1", "0.0.1"));
233 prop4.setEntrySchema(new JpaToscaEntrySchema());
234 prop4.getEntrySchema().setType(new PfConceptKey("the.property.Type2", "0.0.1"));
235 assertTrue(prop4.validate(new PfValidationResult()).isValid());
237 pt0.getProperties().put(prop4.getKey().getLocalName(), prop4);
238 assertEquals(3, pt0.getReferencedDataTypes().size());