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.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 "PolicyType version not specified, the version of the PolicyType for this policy must be specified in"
65 + " the type_version field");
67 assertThatThrownBy(() -> {
68 new JpaToscaPolicy((PfConceptKey) null);
69 }).hasMessageMatching(KEY_IS_NULL);
71 assertThatThrownBy(() -> {
72 new JpaToscaPolicy(null, null);
73 }).hasMessageMatching(KEY_IS_NULL);
75 assertThatThrownBy(() -> {
76 new JpaToscaPolicy(new PfConceptKey(), null);
77 }).hasMessageMatching("type is marked .*on.*ull but is null");
79 assertThatThrownBy(() -> {
80 new JpaToscaPolicy(null, new PfConceptKey());
81 }).hasMessageMatching(KEY_IS_NULL);
83 assertThatThrownBy(() -> new JpaToscaPolicy((JpaToscaPolicy) null)).isInstanceOf(NullPointerException.class);
85 PfConceptKey tpKey = new PfConceptKey("tdt", VERSION_001);
86 PfConceptKey ptKey = new PfConceptKey("policyType", VERSION_001);
87 JpaToscaPolicy tp = new JpaToscaPolicy(tpKey, ptKey);
89 Map<String, String> propertyMap = new HashMap<>();
90 propertyMap.put("Property", "\"Property Value\"");
91 tp.setProperties(propertyMap);
92 assertEquals(propertyMap, tp.getProperties());
94 List<PfConceptKey> targets = new ArrayList<>();
95 PfConceptKey target = new PfConceptKey("target", VERSION_001);
97 tp.setTargets(targets);
98 assertEquals(targets, tp.getTargets());
100 JpaToscaPolicy tdtClone0 = new JpaToscaPolicy(tp);
101 assertEquals(tp, tdtClone0);
102 assertEquals(0, tp.compareTo(tdtClone0));
104 JpaToscaPolicy tdtClone1 = new JpaToscaPolicy(tp);
105 assertEquals(tp, tdtClone1);
106 assertEquals(0, tp.compareTo(tdtClone1));
108 assertEquals(-1, tp.compareTo(null));
109 assertEquals(0, tp.compareTo(tp));
110 assertNotEquals(0, tp.compareTo(tp.getKey()));
112 PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
113 JpaToscaPolicy otherDt = new JpaToscaPolicy(otherDtKey);
115 assertNotEquals(0, tp.compareTo(otherDt));
116 otherDt.setKey(tpKey);
117 assertNotEquals(0, tp.compareTo(otherDt));
118 otherDt.setType(ptKey);
119 assertNotEquals(0, tp.compareTo(otherDt));
120 otherDt.setProperties(propertyMap);
121 assertNotEquals(0, tp.compareTo(otherDt));
122 otherDt.setTargets(targets);
123 assertEquals(0, tp.compareTo(otherDt));
125 assertEquals(3, tp.getKeys().size());
126 assertEquals(2, new JpaToscaPolicy().getKeys().size());
128 new JpaToscaPolicy().clean();
130 assertEquals(tdtClone0, tp);
132 assertFalse(new JpaToscaPolicy().validate("").isValid());
133 assertTrue(tp.validate("").isValid());
135 tp.getProperties().put(null, null);
136 assertFalse(tp.validate("").isValid());
137 tp.getProperties().remove(null);
138 assertTrue(tp.validate("").isValid());
140 tp.getProperties().put("Key", null);
141 assertFalse(tp.validate("").isValid());
142 tp.getProperties().remove("Key");
143 assertTrue(tp.validate("").isValid());
145 tp.getProperties().put(null, "Value");
146 assertFalse(tp.validate("").isValid());
147 tp.getProperties().remove(null);
148 assertTrue(tp.validate("").isValid());
150 tp.getTargets().add(null);
151 assertFalse(tp.validate("").isValid());
152 tp.getTargets().remove(null);
153 assertTrue(tp.validate("").isValid());
155 PfConceptKey tpTypeKey = tp.getKey();
156 assertNotNull(tpTypeKey);
158 assertFalse(tp.validate("").isValid());
159 tp.setType(PfConceptKey.getNullKey());
160 assertFalse(tp.validate("").isValid());
161 tp.setType(tpTypeKey);
162 assertTrue(tp.validate("").isValid());
164 assertThatThrownBy(() -> {
166 }).hasMessageMatching("fieldName is marked .*on.*ull but is null");
168 assertNotNull(tp.toAuthorative());
169 tp.getType().setVersion(PfKey.NULL_KEY_VERSION);
170 assertNotNull(tp.toAuthorative());
171 tp.setProperties(null);
172 assertNotNull(tp.toAuthorative());
174 assertThatThrownBy(() -> {
175 tp.fromAuthorative(null);
176 }).hasMessageMatching("toscaPolicy is marked .*on.*ull but is null");
178 ToscaPolicy pol1 = new ToscaPolicy();
179 pol1.setName("policy");
180 pol1.setVersion("1.2.3");
181 pol1.setType("poltype");
182 pol1.setTypeVersion("2.2.3");
183 tp.fromAuthorative(pol1);
184 assertEquals("2.2.3", tp.getType().getVersion());
188 public void testPolicyProperties() {
190 Map<String, Object> properties = new LinkedHashMap<>();
193 properties.put("byte", Byte.valueOf("2"));
194 properties.put("short", Short.valueOf("1234"));
195 properties.put("int", Integer.valueOf("12345678"));
196 properties.put("long", Long.valueOf("1234567890"));
197 properties.put("float", Float.valueOf("12345.678"));
198 properties.put("double", Double.valueOf("-12345.6789"));
199 properties.put("char", '%');
200 properties.put("string", "hello");
201 properties.put("boolean", false);
204 ToscaPolicy tp = new ToscaPolicy();
205 tp.setType("org.onap.Policy");
206 tp.setTypeVersion("1.2.3");
207 tp.setProperties(properties);
209 JpaToscaPolicy jtp = new JpaToscaPolicy(tp);
210 assertEquals(0, PfUtils.compareCollections(tp.getProperties().keySet(), jtp.getProperties().keySet()));
212 ToscaPolicy tpFromTo = jtp.toAuthorative();
215 assertEquals(2, tpFromTo.getProperties().get("byte"));
216 assertEquals(1234, tpFromTo.getProperties().get("short"));
217 assertEquals(12345678, tpFromTo.getProperties().get("int"));
218 assertEquals(1234567890, tpFromTo.getProperties().get("long"));
219 assertEquals(12345.678, tpFromTo.getProperties().get("float"));
220 assertEquals(-12345.6789, tpFromTo.getProperties().get("double"));
221 assertEquals("%", tpFromTo.getProperties().get("char"));
222 assertEquals("hello", tpFromTo.getProperties().get("string"));
223 assertEquals(false, tpFromTo.getProperties().get("boolean"));