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.PfUtils;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
43 * DAO test for ToscaPolicy.
45 * @author Liam Fallon (liam.fallon@est.tech)
47 public class JpaToscaPolicyTest {
49 private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
50 private static final String VERSION_001 = "0.0.1";
53 public void testPolicyPojo() {
54 assertNotNull(new JpaToscaPolicy());
55 assertNotNull(new JpaToscaPolicy(new PfConceptKey()));
56 assertNotNull(new JpaToscaPolicy(new PfConceptKey(), new PfConceptKey()));
57 assertNotNull(new JpaToscaPolicy(new JpaToscaPolicy()));
59 final ToscaPolicy pol = new ToscaPolicy();
61 assertThatThrownBy(() -> {
62 new JpaToscaPolicy(pol);
64 "Version not specified, the version of this TOSCA entity must be specified in the type_version field");
66 assertThatThrownBy(() -> {
67 new JpaToscaPolicy((PfConceptKey) null);
68 }).hasMessageMatching(KEY_IS_NULL);
70 assertThatThrownBy(() -> {
71 new JpaToscaPolicy(null, null);
72 }).hasMessageMatching(KEY_IS_NULL);
74 assertThatThrownBy(() -> {
75 new JpaToscaPolicy(new PfConceptKey(), null);
76 }).hasMessageMatching("type is marked .*on.*ull but is null");
78 assertThatThrownBy(() -> {
79 new JpaToscaPolicy(null, new PfConceptKey());
80 }).hasMessageMatching(KEY_IS_NULL);
82 assertThatThrownBy(() -> new JpaToscaPolicy((JpaToscaPolicy) null)).isInstanceOf(NullPointerException.class);
86 public void testJpaToscaPolicy() {
87 PfConceptKey tpKey = new PfConceptKey("tdt", VERSION_001);
88 PfConceptKey ptKey = new PfConceptKey("policyType", VERSION_001);
89 JpaToscaPolicy tp = new JpaToscaPolicy(tpKey, ptKey);
91 Map<String, String> propertyMap = new HashMap<>();
92 propertyMap.put("Property", "\"Property Value\"");
93 tp.setProperties(propertyMap);
94 assertEquals(propertyMap, tp.getProperties());
96 List<PfConceptKey> targets = new ArrayList<>();
97 PfConceptKey target = new PfConceptKey("target", VERSION_001);
99 tp.setTargets(targets);
100 assertEquals(targets, tp.getTargets());
102 JpaToscaPolicy tdtClone0 = new JpaToscaPolicy(tp);
103 assertEquals(tp, tdtClone0);
104 assertEquals(0, tp.compareTo(tdtClone0));
106 JpaToscaPolicy tdtClone1 = new JpaToscaPolicy(tp);
107 assertEquals(tp, tdtClone1);
108 assertEquals(0, tp.compareTo(tdtClone1));
110 assertEquals(-1, tp.compareTo(null));
111 assertEquals(0, tp.compareTo(tp));
112 assertNotEquals(0, tp.compareTo(tp.getKey()));
114 PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
115 JpaToscaPolicy otherDt = new JpaToscaPolicy(otherDtKey);
117 assertNotEquals(0, tp.compareTo(otherDt));
118 otherDt.setKey(tpKey);
119 assertNotEquals(0, tp.compareTo(otherDt));
120 otherDt.setType(ptKey);
121 assertNotEquals(0, tp.compareTo(otherDt));
122 otherDt.setProperties(propertyMap);
123 assertNotEquals(0, tp.compareTo(otherDt));
124 otherDt.setTargets(targets);
125 assertEquals(0, tp.compareTo(otherDt));
127 assertEquals(3, tp.getKeys().size());
128 assertEquals(2, new JpaToscaPolicy().getKeys().size());
130 new JpaToscaPolicy().clean();
132 assertEquals(tdtClone0, tp);
136 public void testJpaToscaPolicyValidation() {
137 JpaToscaPolicy tp = setUpJpaToscaPolicy();
138 assertFalse(new JpaToscaPolicy().validate("").isValid());
139 assertTrue(tp.validate("").isValid());
141 tp.getProperties().put(null, null);
142 assertFalse(tp.validate("").isValid());
143 tp.getProperties().remove(null);
144 assertTrue(tp.validate("").isValid());
146 tp.getProperties().put("Key", null);
147 assertFalse(tp.validate("").isValid());
148 tp.getProperties().remove("Key");
149 assertTrue(tp.validate("").isValid());
151 tp.getProperties().put(null, "Value");
152 assertFalse(tp.validate("").isValid());
153 tp.getProperties().remove(null);
154 assertTrue(tp.validate("").isValid());
156 tp.getTargets().add(null);
157 assertFalse(tp.validate("").isValid());
158 tp.getTargets().remove(null);
159 assertTrue(tp.validate("").isValid());
163 public void testJpaToscaPolicyAuthorative() {
164 JpaToscaPolicy tp = setUpJpaToscaPolicy();
165 PfConceptKey tpTypeKey = tp.getKey();
166 assertNotNull(tpTypeKey);
168 assertFalse(tp.validate("").isValid());
169 tp.setType(PfConceptKey.getNullKey());
170 assertFalse(tp.validate("").isValid());
171 tp.setType(tpTypeKey);
172 assertTrue(tp.validate("").isValid());
174 assertThatThrownBy(() -> {
176 }).hasMessageMatching("fieldName is marked .*on.*ull but is null");
178 assertNotNull(tp.toAuthorative());
179 tp.getType().setVersion(PfKey.NULL_KEY_VERSION);
180 assertNotNull(tp.toAuthorative());
181 tp.setProperties(null);
182 assertNotNull(tp.toAuthorative());
184 assertThatThrownBy(() -> {
185 tp.fromAuthorative(null);
186 }).hasMessageMatching("toscaPolicy is marked .*on.*ull but is null");
188 ToscaPolicy pol1 = new ToscaPolicy();
189 pol1.setName("policy");
190 pol1.setVersion("1.2.3");
191 pol1.setType("poltype");
192 pol1.setTypeVersion("2.2.3");
193 tp.fromAuthorative(pol1);
194 assertEquals("2.2.3", tp.getType().getVersion());
198 public void testPolicyProperties() {
200 Map<String, Object> properties = new LinkedHashMap<>();
203 properties.put("byte", Byte.valueOf("2"));
204 properties.put("short", Short.valueOf("1234"));
205 properties.put("int", Integer.valueOf("12345678"));
206 properties.put("long", Long.valueOf("1234567890"));
207 properties.put("float", Float.valueOf("12345.678"));
208 properties.put("double", Double.valueOf("-12345.6789"));
209 properties.put("char", '%');
210 properties.put("string", "hello");
211 properties.put("boolean", false);
214 ToscaPolicy tp = new ToscaPolicy();
215 tp.setType("org.onap.Policy");
216 tp.setTypeVersion("1.2.3");
217 tp.setProperties(properties);
219 JpaToscaPolicy jtp = new JpaToscaPolicy(tp);
220 assertEquals(0, PfUtils.compareCollections(tp.getProperties().keySet(), jtp.getProperties().keySet()));
222 ToscaPolicy tpFromTo = jtp.toAuthorative();
225 assertEquals(2, tpFromTo.getProperties().get("byte"));
226 assertEquals(1234, tpFromTo.getProperties().get("short"));
227 assertEquals(12345678, tpFromTo.getProperties().get("int"));
228 assertEquals(1234567890, tpFromTo.getProperties().get("long"));
229 assertEquals(12345.678, tpFromTo.getProperties().get("float"));
230 assertEquals(-12345.6789, tpFromTo.getProperties().get("double"));
231 assertEquals("%", tpFromTo.getProperties().get("char"));
232 assertEquals("hello", tpFromTo.getProperties().get("string"));
233 assertEquals(false, tpFromTo.getProperties().get("boolean"));
237 private JpaToscaPolicy setUpJpaToscaPolicy() {
238 PfConceptKey tpKey = new PfConceptKey("tdt", VERSION_001);
239 PfConceptKey ptKey = new PfConceptKey("policyType", VERSION_001);
240 JpaToscaPolicy tp = new JpaToscaPolicy(tpKey, ptKey);
242 // Maps and Lists need to be modifiable
243 Map<String, String> propertyMap = new HashMap<>(Map.of("Property", "\"Property Value\""));
244 tp.setProperties(propertyMap);
246 PfConceptKey target = new PfConceptKey("target", VERSION_001);
247 List<PfConceptKey> targets = new ArrayList<>(List.of(target));
248 tp.setTargets(targets);