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.pdp.persistence.concepts;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotEquals;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertNull;
31 import static org.junit.Assert.assertTrue;
33 import java.util.ArrayList;
34 import java.util.LinkedHashMap;
35 import java.util.List;
36 import org.junit.Test;
37 import org.onap.policy.models.base.PfConceptKey;
38 import org.onap.policy.models.base.PfReferenceKey;
39 import org.onap.policy.models.base.PfSearchableKey;
40 import org.onap.policy.models.base.Validated;
41 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
42 import org.onap.policy.models.pdp.testconcepts.DummyJpaPdpSubgroupChild;
45 * Test the {@link JpaPdpSubGroupSubGroup} class.
47 * @author Liam Fallon (liam.fallon@est.tech)
49 public class JpaPdpSubGroupTest {
51 private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
52 private static final String PDP_A = "PDP-A";
55 public void testJpaPdpSubGroupErrors() {
56 assertThatThrownBy(() -> {
57 new JpaPdpSubGroup((JpaPdpSubGroup) null);
58 }).hasMessageMatching("copyConcept is marked .*ull but is null");
60 assertThatThrownBy(() -> {
61 new JpaPdpSubGroup((PfReferenceKey) null);
62 }).hasMessageMatching(NULL_KEY_ERROR);
64 assertThatThrownBy(() -> {
65 new JpaPdpSubGroup((PdpSubGroup) null);
66 }).hasMessageMatching("authorativeConcept is marked .*ull but is null");
68 assertThatThrownBy(() -> {
69 new JpaPdpSubGroup(null, null, null, null);
70 }).hasMessageMatching(NULL_KEY_ERROR);
72 assertThatThrownBy(() -> {
73 new JpaPdpSubGroup(new PfReferenceKey(), null, null, null);
74 }).hasMessageMatching("supportedPolicyTypes is marked .*ull but is null");
76 assertThatThrownBy(() -> {
77 new JpaPdpSubGroup(new PfReferenceKey(), new ArrayList<>(), null, null);
78 }).hasMessageMatching("policies is marked .*ull but is null");
80 assertThatThrownBy(() -> {
81 new JpaPdpSubGroup(null, new ArrayList<>(), null, null);
82 }).hasMessageMatching(NULL_KEY_ERROR);
84 assertThatThrownBy(() -> {
85 new JpaPdpSubGroup(null, new ArrayList<>(), new ArrayList<>(), null);
86 }).hasMessageMatching(NULL_KEY_ERROR);
88 assertThatThrownBy(() -> {
89 new JpaPdpSubGroup(null, null, new ArrayList<>(), null);
90 }).hasMessageMatching(NULL_KEY_ERROR);
92 assertThatThrownBy(() -> {
93 new JpaPdpSubGroup(null, null, null, new ArrayList<>());
94 }).hasMessageMatching(NULL_KEY_ERROR);
96 assertThatThrownBy(() -> {
97 new JpaPdpSubGroup(new PfReferenceKey(), null, null, new ArrayList<>());
98 }).hasMessageMatching("supportedPolicyTypes is marked .*ull but is null");
100 assertThatThrownBy(() -> {
101 new JpaPdpSubGroup(new PfReferenceKey(), new ArrayList<>(), null, new ArrayList<>());
102 }).hasMessageMatching("policies is marked .*ull but is null");
104 assertThatThrownBy(() -> {
105 new JpaPdpSubGroup(null, new ArrayList<>(), null, new ArrayList<>());
106 }).hasMessageMatching(NULL_KEY_ERROR);
108 assertThatThrownBy(() -> {
109 new JpaPdpSubGroup(null, new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
110 }).hasMessageMatching(NULL_KEY_ERROR);
112 assertThatThrownBy(() -> {
113 new JpaPdpSubGroup(null, null, new ArrayList<>(), null);
114 }).hasMessageMatching(NULL_KEY_ERROR);
116 assertNotNull(new JpaPdpSubGroup((new PfReferenceKey())));
120 public void testJpaPdpSubGroup() {
121 PdpSubGroup testPdpSubgroup = new PdpSubGroup();
122 testPdpSubgroup.setPdpType(PDP_A);
123 JpaPdpSubGroup testJpaPdpSubGroup = new JpaPdpSubGroup();
124 testJpaPdpSubGroup.setKey(null);
125 testJpaPdpSubGroup.fromAuthorative(testPdpSubgroup);
126 assertEquals(PDP_A, testJpaPdpSubGroup.getKey().getLocalName());
127 testJpaPdpSubGroup.setKey(PfReferenceKey.getNullKey());
128 testJpaPdpSubGroup.fromAuthorative(testPdpSubgroup);
130 assertThatThrownBy(() -> {
131 testJpaPdpSubGroup.fromAuthorative(null);
132 }).hasMessageMatching("pdpSubgroup is marked .*ull but is null");
134 assertThatThrownBy(() -> new JpaPdpSubGroup((JpaPdpSubGroup) null)).isInstanceOf(NullPointerException.class);
136 assertEquals(PDP_A, testJpaPdpSubGroup.getKey().getLocalName());
137 assertEquals(PDP_A, new JpaPdpSubGroup(testPdpSubgroup).getKey().getLocalName());
138 assertEquals(PDP_A, ((PfReferenceKey) new JpaPdpSubGroup(testPdpSubgroup).getKeys().get(0)).getLocalName());
140 testJpaPdpSubGroup.clean();
141 assertEquals(PDP_A, testJpaPdpSubGroup.getKey().getLocalName());
143 assertThatThrownBy(() -> {
144 testJpaPdpSubGroup.validate(null);
145 }).hasMessageMatching("fieldName is marked .*ull but is null");
147 assertFalse(testJpaPdpSubGroup.validate("").isValid());
148 assertThat(testJpaPdpSubGroup.validate("").getResult())
149 .contains("parent of key").contains(Validated.IS_A_NULL_KEY);
151 testJpaPdpSubGroup.getKey().setParentConceptKey(new PfConceptKey("Parent:1.0.0"));
152 assertFalse(testJpaPdpSubGroup.validate("").isValid());
153 assertThat(testJpaPdpSubGroup.validate("").getResult())
154 .doesNotContain("parent of key")
155 .contains("supportedPolicyTypes").contains("is empty");
157 testJpaPdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
158 testJpaPdpSubGroup.getSupportedPolicyTypes().add(new PfSearchableKey("APolicyType:1.0.0"));
159 assertTrue(testJpaPdpSubGroup.validate("").isValid());
163 public void testJpaPdpSubGroupSavedKey() {
164 JpaPdpSubGroup testJpaPdpSubGroup = setUpJpaPdpSubGroup();
166 PfReferenceKey savedKey = testJpaPdpSubGroup.getKey();
167 testJpaPdpSubGroup.setKey(PfReferenceKey.getNullKey());
168 assertFalse(testJpaPdpSubGroup.validate("").isValid());
169 testJpaPdpSubGroup.setKey(savedKey);
170 assertTrue(testJpaPdpSubGroup.validate("").isValid());
172 testJpaPdpSubGroup.setProperties(new LinkedHashMap<>());
173 testJpaPdpSubGroup.getProperties().put(null, null);
174 assertFalse(testJpaPdpSubGroup.validate("").isValid());
175 testJpaPdpSubGroup.getProperties().remove(null);
176 assertTrue(testJpaPdpSubGroup.validate("").isValid());
178 testJpaPdpSubGroup.setProperties(new LinkedHashMap<>());
179 testJpaPdpSubGroup.getProperties().put("NullKey", null);
180 assertFalse(testJpaPdpSubGroup.validate("").isValid());
181 testJpaPdpSubGroup.getProperties().remove("NullKey");
182 assertTrue(testJpaPdpSubGroup.validate("").isValid());
184 testJpaPdpSubGroup.setDesiredInstanceCount(-1);
185 assertFalse(testJpaPdpSubGroup.validate("").isValid());
186 testJpaPdpSubGroup.setDesiredInstanceCount(0);
187 assertTrue(testJpaPdpSubGroup.validate("").isValid());
188 testJpaPdpSubGroup.setProperties(null);
190 testJpaPdpSubGroup.setCurrentInstanceCount(-1);
191 assertFalse(testJpaPdpSubGroup.validate("").isValid());
192 testJpaPdpSubGroup.setCurrentInstanceCount(0);
193 assertTrue(testJpaPdpSubGroup.validate("").isValid());
194 testJpaPdpSubGroup.setProperties(null);
195 assertTrue(testJpaPdpSubGroup.validate("").isValid());
199 public void testJpaPdpSubGroupPolicyTypes() {
200 JpaPdpSubGroup testJpaPdpSubGroup = setUpJpaPdpSubGroup();
202 List<PfSearchableKey> supportedPolicyTypes = testJpaPdpSubGroup.getSupportedPolicyTypes();
203 assertNotNull(supportedPolicyTypes);
204 testJpaPdpSubGroup.setSupportedPolicyTypes(null);
205 assertFalse(testJpaPdpSubGroup.validate("").isValid());
206 testJpaPdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
207 assertFalse(testJpaPdpSubGroup.validate("").isValid());
208 testJpaPdpSubGroup.setSupportedPolicyTypes(supportedPolicyTypes);
209 assertTrue(testJpaPdpSubGroup.validate("").isValid());
211 List<PfConceptKey> supportedPolicies = testJpaPdpSubGroup.getPolicies();
212 assertNotNull(supportedPolicies);
213 testJpaPdpSubGroup.setPolicies(null);
214 assertFalse(testJpaPdpSubGroup.validate("").isValid());
215 testJpaPdpSubGroup.setPolicies(new ArrayList<>());
216 assertTrue(testJpaPdpSubGroup.validate("").isValid());
217 testJpaPdpSubGroup.setPolicies(supportedPolicies);
218 assertTrue(testJpaPdpSubGroup.validate("").isValid());
220 List<JpaPdp> pdpInstances = testJpaPdpSubGroup.getPdpInstances();
221 assertNotNull(pdpInstances);
222 testJpaPdpSubGroup.setPdpInstances(null);
223 assertFalse(testJpaPdpSubGroup.validate("").isValid());
224 testJpaPdpSubGroup.setPdpInstances(new ArrayList<>());
225 assertTrue(testJpaPdpSubGroup.validate("").isValid());
226 testJpaPdpSubGroup.setPdpInstances(pdpInstances);
227 assertTrue(testJpaPdpSubGroup.validate("").isValid());
231 public void testJpaPdpSubGroupKeys() {
232 JpaPdpSubGroup testJpaPdpSubGroup = setUpJpaPdpSubGroup();
234 JpaPdpSubGroup otherJpaPdpSubGroup = new JpaPdpSubGroup(testJpaPdpSubGroup);
235 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
236 assertEquals(-1, testJpaPdpSubGroup.compareTo(null));
237 assertEquals(0, testJpaPdpSubGroup.compareTo(testJpaPdpSubGroup));
238 assertNotEquals(0, testJpaPdpSubGroup.compareTo(new DummyJpaPdpSubgroupChild()));
240 testJpaPdpSubGroup.getKey().setParentKeyName("Parent1");
241 assertEquals(1, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
242 testJpaPdpSubGroup.getKey().setParentKeyName("Parent");
243 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
245 testJpaPdpSubGroup.setCurrentInstanceCount(1);
246 assertEquals(1, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
247 testJpaPdpSubGroup.setCurrentInstanceCount(0);
248 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
250 testJpaPdpSubGroup.setDesiredInstanceCount(1);
251 assertEquals(1, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
252 testJpaPdpSubGroup.setDesiredInstanceCount(0);
253 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
255 PfSearchableKey anotherPolicyType = new PfSearchableKey("AnotherPolicyType.*", "1.0.0");
256 testJpaPdpSubGroup.getSupportedPolicyTypes().add(anotherPolicyType);
257 assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
258 testJpaPdpSubGroup.getSupportedPolicyTypes().remove(anotherPolicyType);
259 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
261 PfConceptKey anotherPolicy = new PfConceptKey("AnotherPolicy", "1.0.0");
262 testJpaPdpSubGroup.getPolicies().add(anotherPolicy);
263 assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
264 testJpaPdpSubGroup.getPolicies().remove(anotherPolicy);
265 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
267 JpaPdp anotherPdp = new JpaPdp(new PfReferenceKey(testJpaPdpSubGroup.getKey(), "AnotherPdp"));
268 testJpaPdpSubGroup.getPdpInstances().add(anotherPdp);
269 assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
270 testJpaPdpSubGroup.getPdpInstances().remove(anotherPdp);
271 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
273 testJpaPdpSubGroup.setProperties(new LinkedHashMap<>());
274 testJpaPdpSubGroup.getProperties().put("AnotherProperty", "Some String");
275 assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
276 testJpaPdpSubGroup.getProperties().remove("AnotherProperty");
277 testJpaPdpSubGroup.setProperties(null);
278 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
280 PdpSubGroup psg = testJpaPdpSubGroup.toAuthorative();
281 assertNull(psg.getProperties());
283 testJpaPdpSubGroup.setProperties(new LinkedHashMap<>());
284 psg = testJpaPdpSubGroup.toAuthorative();
285 assertEquals(0, psg.getProperties().size());
287 testJpaPdpSubGroup.getPolicies().add(new PfConceptKey("APolicy:1.0.0"));
288 testJpaPdpSubGroup.getPdpInstances().add(new JpaPdp());
290 testJpaPdpSubGroup.getProperties().put(" PropKey ", " Prop Value ");
291 testJpaPdpSubGroup.clean();
292 assertEquals("PropKey", testJpaPdpSubGroup.getProperties().keySet().iterator().next());
293 assertEquals("Prop Value", testJpaPdpSubGroup.getProperties().get("PropKey"));
295 assertEquals(4, testJpaPdpSubGroup.getKeys().size());
297 assertEquals(testJpaPdpSubGroup, new JpaPdpSubGroup(testJpaPdpSubGroup));
300 private JpaPdpSubGroup setUpJpaPdpSubGroup() {
301 PdpSubGroup testPdpSubgroup = new PdpSubGroup();
302 testPdpSubgroup.setPdpType(PDP_A);
303 JpaPdpSubGroup testJpaPdpSubGroup = new JpaPdpSubGroup();
304 testJpaPdpSubGroup.setKey(PfReferenceKey.getNullKey());
305 testJpaPdpSubGroup.fromAuthorative(testPdpSubgroup);
306 testJpaPdpSubGroup.getKey().setParentConceptKey(new PfConceptKey("Parent:1.0.0"));
307 testJpaPdpSubGroup.setProperties(new LinkedHashMap<>());
308 testJpaPdpSubGroup.setDesiredInstanceCount(0);
309 testJpaPdpSubGroup.setCurrentInstanceCount(0);
310 testJpaPdpSubGroup.setProperties(null);
311 testJpaPdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
312 testJpaPdpSubGroup.getSupportedPolicyTypes().add(new PfSearchableKey("APolicyType:1.0.0"));
313 testJpaPdpSubGroup.clean();
314 return testJpaPdpSubGroup;