2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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.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.assertNull;
30 import static org.junit.Assert.assertTrue;
32 import java.util.ArrayList;
33 import java.util.LinkedHashMap;
34 import java.util.List;
35 import org.junit.Test;
36 import org.onap.policy.models.base.PfConceptKey;
37 import org.onap.policy.models.base.PfReferenceKey;
38 import org.onap.policy.models.base.PfSearchableKey;
39 import org.onap.policy.models.base.PfValidationResult;
40 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
41 import org.onap.policy.models.pdp.testconcepts.DummyJpaPdpSubgroupChild;
44 * Test the {@link JpaPdpSubGroupSubGroup} class.
46 * @author Liam Fallon (liam.fallon@est.tech)
48 public class JpaPdpSubGroupTest {
50 private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
51 private static final String PDP_A = "PDP-A";
54 public void testJpaPdpSubGroup() {
55 assertThatThrownBy(() -> {
56 new JpaPdpSubGroup((JpaPdpSubGroup) null);
57 }).hasMessageMatching("copyConcept is marked .*ull but is null");
59 assertThatThrownBy(() -> {
60 new JpaPdpSubGroup((PfReferenceKey) null);
61 }).hasMessageMatching(NULL_KEY_ERROR);
63 assertThatThrownBy(() -> {
64 new JpaPdpSubGroup((PdpSubGroup) null);
65 }).hasMessageMatching("authorativeConcept is marked .*ull but is null");
67 assertThatThrownBy(() -> {
68 new JpaPdpSubGroup(null, null, null, null);
69 }).hasMessageMatching(NULL_KEY_ERROR);
71 assertThatThrownBy(() -> {
72 new JpaPdpSubGroup(new PfReferenceKey(), null, null, null);
73 }).hasMessageMatching("supportedPolicyTypes is marked .*ull but is null");
75 assertThatThrownBy(() -> {
76 new JpaPdpSubGroup(new PfReferenceKey(), new ArrayList<>(), null, null);
77 }).hasMessageMatching("policies is marked .*ull but is null");
79 assertThatThrownBy(() -> {
80 new JpaPdpSubGroup(null, new ArrayList<>(), null, null);
81 }).hasMessageMatching(NULL_KEY_ERROR);
83 assertThatThrownBy(() -> {
84 new JpaPdpSubGroup(null, new ArrayList<>(), new ArrayList<>(), null);
85 }).hasMessageMatching(NULL_KEY_ERROR);
87 assertThatThrownBy(() -> {
88 new JpaPdpSubGroup(null, null, new ArrayList<>(), null);
89 }).hasMessageMatching(NULL_KEY_ERROR);
91 assertThatThrownBy(() -> {
92 new JpaPdpSubGroup(null, null, null, new ArrayList<>());
93 }).hasMessageMatching(NULL_KEY_ERROR);
95 assertThatThrownBy(() -> {
96 new JpaPdpSubGroup(new PfReferenceKey(), null, null, new ArrayList<>());
97 }).hasMessageMatching("supportedPolicyTypes is marked .*ull but is null");
99 assertThatThrownBy(() -> {
100 new JpaPdpSubGroup(new PfReferenceKey(), new ArrayList<>(), null, new ArrayList<>());
101 }).hasMessageMatching("policies is marked .*ull but is null");
103 assertThatThrownBy(() -> {
104 new JpaPdpSubGroup(null, new ArrayList<>(), null, new ArrayList<>());
105 }).hasMessageMatching(NULL_KEY_ERROR);
107 assertThatThrownBy(() -> {
108 new JpaPdpSubGroup(null, new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
109 }).hasMessageMatching(NULL_KEY_ERROR);
111 assertThatThrownBy(() -> {
112 new JpaPdpSubGroup(null, null, new ArrayList<>(), null);
113 }).hasMessageMatching(NULL_KEY_ERROR);
115 assertNotNull(new JpaPdpSubGroup((new PfReferenceKey())));
117 PdpSubGroup testPdpSubgroup = new PdpSubGroup();
118 testPdpSubgroup.setPdpType(PDP_A);
119 JpaPdpSubGroup testJpaPdpSubGroup = new JpaPdpSubGroup();
120 testJpaPdpSubGroup.setKey(null);
121 testJpaPdpSubGroup.fromAuthorative(testPdpSubgroup);
122 assertEquals(PDP_A, testJpaPdpSubGroup.getKey().getLocalName());
123 testJpaPdpSubGroup.setKey(PfReferenceKey.getNullKey());
124 testJpaPdpSubGroup.fromAuthorative(testPdpSubgroup);
126 assertThatThrownBy(() -> {
127 testJpaPdpSubGroup.fromAuthorative(null);
128 }).hasMessageMatching("pdpSubgroup is marked .*ull but is null");
130 assertThatThrownBy(() -> new JpaPdpSubGroup((JpaPdpSubGroup) null)).isInstanceOf(NullPointerException.class);
132 assertEquals(PDP_A, testJpaPdpSubGroup.getKey().getLocalName());
133 assertEquals(PDP_A, new JpaPdpSubGroup(testPdpSubgroup).getKey().getLocalName());
134 assertEquals(PDP_A, ((PfReferenceKey) new JpaPdpSubGroup(testPdpSubgroup).getKeys().get(0)).getLocalName());
136 testJpaPdpSubGroup.clean();
137 assertEquals(PDP_A, testJpaPdpSubGroup.getKey().getLocalName());
139 assertThatThrownBy(() -> {
140 testJpaPdpSubGroup.validate(null);
141 }).hasMessageMatching("resultIn is marked .*ull but is null");
143 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
144 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).toString()
145 .contains("INVALID:parent of key is a null key"));
147 testJpaPdpSubGroup.getKey().setParentConceptKey(new PfConceptKey("Parent:1.0.0"));
148 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
149 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).toString()
150 .contains("INVALID:parent of key is a null key"));
151 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).toString()
152 .contains("INVALID:a PDP subgroup must support at least one policy type"));
154 testJpaPdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
155 testJpaPdpSubGroup.getSupportedPolicyTypes().add(new PfSearchableKey("APolicyType:1.0.0"));
156 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
157 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).toString()
158 .contains("INVALID:a PDP subgroup must support at least one policy type"));
160 PfReferenceKey savedKey = testJpaPdpSubGroup.getKey();
161 testJpaPdpSubGroup.setKey(PfReferenceKey.getNullKey());
162 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
163 testJpaPdpSubGroup.setKey(savedKey);
164 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
166 testJpaPdpSubGroup.setProperties(new LinkedHashMap<>());
167 testJpaPdpSubGroup.getProperties().put(null, null);
168 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
169 testJpaPdpSubGroup.getProperties().remove(null);
170 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
172 testJpaPdpSubGroup.setProperties(new LinkedHashMap<>());
173 testJpaPdpSubGroup.getProperties().put("NullKey", null);
174 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
175 testJpaPdpSubGroup.getProperties().remove("NullKey");
176 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
178 testJpaPdpSubGroup.setDesiredInstanceCount(-1);
179 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
180 testJpaPdpSubGroup.setDesiredInstanceCount(0);
181 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
182 testJpaPdpSubGroup.setProperties(null);
184 testJpaPdpSubGroup.setCurrentInstanceCount(-1);
185 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
186 testJpaPdpSubGroup.setCurrentInstanceCount(0);
187 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
188 testJpaPdpSubGroup.setProperties(null);
189 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
191 List<PfSearchableKey> supportedPolicyTypes = testJpaPdpSubGroup.getSupportedPolicyTypes();
192 assertNotNull(supportedPolicyTypes);
193 testJpaPdpSubGroup.setSupportedPolicyTypes(null);
194 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
195 testJpaPdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
196 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
197 testJpaPdpSubGroup.setSupportedPolicyTypes(supportedPolicyTypes);
198 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
200 List<PfConceptKey> supportedPolicies = testJpaPdpSubGroup.getPolicies();
201 assertNotNull(supportedPolicies);
202 testJpaPdpSubGroup.setPolicies(null);
203 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
204 testJpaPdpSubGroup.setPolicies(new ArrayList<>());
205 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
206 testJpaPdpSubGroup.setPolicies(supportedPolicies);
207 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
209 List<JpaPdp> pdpInstances = testJpaPdpSubGroup.getPdpInstances();
210 assertNotNull(pdpInstances);
211 testJpaPdpSubGroup.setPdpInstances(null);
212 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
213 testJpaPdpSubGroup.setPdpInstances(new ArrayList<>());
214 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
215 testJpaPdpSubGroup.setPdpInstances(pdpInstances);
216 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
218 JpaPdpSubGroup otherJpaPdpSubGroup = new JpaPdpSubGroup(testJpaPdpSubGroup);
219 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
220 assertEquals(-1, testJpaPdpSubGroup.compareTo(null));
221 assertEquals(0, testJpaPdpSubGroup.compareTo(testJpaPdpSubGroup));
222 assertNotEquals(0, testJpaPdpSubGroup.compareTo(new DummyJpaPdpSubgroupChild()));
224 testJpaPdpSubGroup.getKey().setParentKeyName("Parent1");
225 assertEquals(1, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
226 testJpaPdpSubGroup.getKey().setParentKeyName("Parent");
227 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
229 testJpaPdpSubGroup.setCurrentInstanceCount(1);
230 assertEquals(1, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
231 testJpaPdpSubGroup.setCurrentInstanceCount(0);
232 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
234 testJpaPdpSubGroup.setDesiredInstanceCount(1);
235 assertEquals(1, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
236 testJpaPdpSubGroup.setDesiredInstanceCount(0);
237 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
239 PfSearchableKey anotherPolicyType = new PfSearchableKey("AnotherPolicyType.*", "1.0.0");
240 testJpaPdpSubGroup.getSupportedPolicyTypes().add(anotherPolicyType);
241 assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
242 testJpaPdpSubGroup.getSupportedPolicyTypes().remove(anotherPolicyType);
243 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
245 PfConceptKey anotherPolicy = new PfConceptKey("AnotherPolicy", "1.0.0");
246 testJpaPdpSubGroup.getPolicies().add(anotherPolicy);
247 assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
248 testJpaPdpSubGroup.getPolicies().remove(anotherPolicy);
249 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
251 JpaPdp anotherPdp = new JpaPdp(new PfReferenceKey(testJpaPdpSubGroup.getKey(), "AnotherPdp"));
252 testJpaPdpSubGroup.getPdpInstances().add(anotherPdp);
253 assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
254 testJpaPdpSubGroup.getPdpInstances().remove(anotherPdp);
255 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
257 testJpaPdpSubGroup.setProperties(new LinkedHashMap<>());
258 testJpaPdpSubGroup.getProperties().put("AnotherProperty", "Some String");
259 assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
260 testJpaPdpSubGroup.getProperties().remove("AnotherProperty");
261 testJpaPdpSubGroup.setProperties(null);
262 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
264 PdpSubGroup psg = testJpaPdpSubGroup.toAuthorative();
265 assertNull(psg.getProperties());
267 testJpaPdpSubGroup.setProperties(new LinkedHashMap<>());
268 psg = testJpaPdpSubGroup.toAuthorative();
269 assertEquals(0, psg.getProperties().size());
271 testJpaPdpSubGroup.getPolicies().add(new PfConceptKey("APolicy:1.0.0"));
272 testJpaPdpSubGroup.getPdpInstances().add(new JpaPdp());
274 testJpaPdpSubGroup.getProperties().put(" PropKey ", " Prop Value ");
275 testJpaPdpSubGroup.clean();
276 assertEquals("PropKey", testJpaPdpSubGroup.getProperties().keySet().iterator().next());
277 assertEquals("Prop Value", testJpaPdpSubGroup.getProperties().get("PropKey"));
279 assertEquals(4, testJpaPdpSubGroup.getKeys().size());
281 assertEquals(testJpaPdpSubGroup, new JpaPdpSubGroup(testJpaPdpSubGroup));