2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 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;
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.PfValidationResult;
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 testJpaPdpSubGroup() {
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())));
118 PdpSubGroup testPdpSubgroup = new PdpSubGroup();
119 testPdpSubgroup.setPdpType(PDP_A);
120 JpaPdpSubGroup testJpaPdpSubGroup = new JpaPdpSubGroup();
121 testJpaPdpSubGroup.setKey(null);
122 testJpaPdpSubGroup.fromAuthorative(testPdpSubgroup);
123 assertEquals(PDP_A, testJpaPdpSubGroup.getKey().getLocalName());
124 testJpaPdpSubGroup.setKey(PfReferenceKey.getNullKey());
125 testJpaPdpSubGroup.fromAuthorative(testPdpSubgroup);
127 assertThatThrownBy(() -> {
128 testJpaPdpSubGroup.fromAuthorative(null);
129 }).hasMessageMatching("pdpSubgroup is marked .*ull but is null");
131 assertThatThrownBy(() -> new JpaPdpSubGroup((JpaPdpSubGroup) null)).isInstanceOf(NullPointerException.class);
133 assertEquals(PDP_A, testJpaPdpSubGroup.getKey().getLocalName());
134 assertEquals(PDP_A, new JpaPdpSubGroup(testPdpSubgroup).getKey().getLocalName());
135 assertEquals(PDP_A, ((PfReferenceKey) new JpaPdpSubGroup(testPdpSubgroup).getKeys().get(0)).getLocalName());
137 testJpaPdpSubGroup.clean();
138 assertEquals(PDP_A, testJpaPdpSubGroup.getKey().getLocalName());
140 assertThatThrownBy(() -> {
141 testJpaPdpSubGroup.validate(null);
142 }).hasMessageMatching("resultIn is marked .*ull but is null");
144 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
145 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).toString()
146 .contains("INVALID:parent of key is a null key"));
148 testJpaPdpSubGroup.getKey().setParentConceptKey(new PfConceptKey("Parent:1.0.0"));
149 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
150 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).toString()
151 .contains("INVALID:parent of key is a null key"));
152 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).toString()
153 .contains("INVALID:a PDP subgroup must support at least one policy type"));
155 testJpaPdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
156 testJpaPdpSubGroup.getSupportedPolicyTypes().add(new PfSearchableKey("APolicyType:1.0.0"));
157 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
158 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).toString()
159 .contains("INVALID:a PDP subgroup must support at least one policy type"));
161 PfReferenceKey savedKey = testJpaPdpSubGroup.getKey();
162 testJpaPdpSubGroup.setKey(PfReferenceKey.getNullKey());
163 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
164 testJpaPdpSubGroup.setKey(savedKey);
165 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
167 testJpaPdpSubGroup.setProperties(new LinkedHashMap<>());
168 testJpaPdpSubGroup.getProperties().put(null, null);
169 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
170 testJpaPdpSubGroup.getProperties().remove(null);
171 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
173 testJpaPdpSubGroup.setProperties(new LinkedHashMap<>());
174 testJpaPdpSubGroup.getProperties().put("NullKey", null);
175 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
176 testJpaPdpSubGroup.getProperties().remove("NullKey");
177 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
179 testJpaPdpSubGroup.setDesiredInstanceCount(-1);
180 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
181 testJpaPdpSubGroup.setDesiredInstanceCount(0);
182 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
183 testJpaPdpSubGroup.setProperties(null);
185 testJpaPdpSubGroup.setCurrentInstanceCount(-1);
186 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
187 testJpaPdpSubGroup.setCurrentInstanceCount(0);
188 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
189 testJpaPdpSubGroup.setProperties(null);
190 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
192 List<PfSearchableKey> supportedPolicyTypes = testJpaPdpSubGroup.getSupportedPolicyTypes();
193 assertNotNull(supportedPolicyTypes);
194 testJpaPdpSubGroup.setSupportedPolicyTypes(null);
195 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
196 testJpaPdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
197 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
198 testJpaPdpSubGroup.setSupportedPolicyTypes(supportedPolicyTypes);
199 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
201 List<PfConceptKey> supportedPolicies = testJpaPdpSubGroup.getPolicies();
202 assertNotNull(supportedPolicies);
203 testJpaPdpSubGroup.setPolicies(null);
204 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
205 testJpaPdpSubGroup.setPolicies(new ArrayList<>());
206 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
207 testJpaPdpSubGroup.setPolicies(supportedPolicies);
208 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
210 List<JpaPdp> pdpInstances = testJpaPdpSubGroup.getPdpInstances();
211 assertNotNull(pdpInstances);
212 testJpaPdpSubGroup.setPdpInstances(null);
213 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
214 testJpaPdpSubGroup.setPdpInstances(new ArrayList<>());
215 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
216 testJpaPdpSubGroup.setPdpInstances(pdpInstances);
217 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
219 JpaPdpSubGroup otherJpaPdpSubGroup = new JpaPdpSubGroup(testJpaPdpSubGroup);
220 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
221 assertEquals(-1, testJpaPdpSubGroup.compareTo(null));
222 assertEquals(0, testJpaPdpSubGroup.compareTo(testJpaPdpSubGroup));
223 assertFalse(testJpaPdpSubGroup.compareTo(new DummyJpaPdpSubgroupChild()) == 0);
225 testJpaPdpSubGroup.getKey().setParentKeyName("Parent1");
226 assertEquals(1, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
227 testJpaPdpSubGroup.getKey().setParentKeyName("Parent");
228 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
230 testJpaPdpSubGroup.setCurrentInstanceCount(1);
231 assertEquals(1, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
232 testJpaPdpSubGroup.setCurrentInstanceCount(0);
233 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
235 testJpaPdpSubGroup.setDesiredInstanceCount(1);
236 assertEquals(1, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
237 testJpaPdpSubGroup.setDesiredInstanceCount(0);
238 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
240 PfSearchableKey anotherPolicyType = new PfSearchableKey("AnotherPolicyType.*", "1.0.0");
241 testJpaPdpSubGroup.getSupportedPolicyTypes().add(anotherPolicyType);
242 assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
243 testJpaPdpSubGroup.getSupportedPolicyTypes().remove(anotherPolicyType);
244 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
246 PfConceptKey anotherPolicy = new PfConceptKey("AnotherPolicy", "1.0.0");
247 testJpaPdpSubGroup.getPolicies().add(anotherPolicy);
248 assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
249 testJpaPdpSubGroup.getPolicies().remove(anotherPolicy);
250 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
252 JpaPdp anotherPdp = new JpaPdp(new PfReferenceKey(testJpaPdpSubGroup.getKey(), "AnotherPdp"));
253 testJpaPdpSubGroup.getPdpInstances().add(anotherPdp);
254 assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
255 testJpaPdpSubGroup.getPdpInstances().remove(anotherPdp);
256 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
258 testJpaPdpSubGroup.setProperties(new LinkedHashMap<>());
259 testJpaPdpSubGroup.getProperties().put("AnotherProperty", "Some String");
260 assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
261 testJpaPdpSubGroup.getProperties().remove("AnotherProperty");
262 testJpaPdpSubGroup.setProperties(null);
263 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
265 PdpSubGroup psg = testJpaPdpSubGroup.toAuthorative();
266 assertNull(psg.getProperties());
268 testJpaPdpSubGroup.setProperties(new LinkedHashMap<>());
269 psg = testJpaPdpSubGroup.toAuthorative();
270 assertEquals(0, psg.getProperties().size());
272 testJpaPdpSubGroup.getPolicies().add(new PfConceptKey("APolicy:1.0.0"));
273 testJpaPdpSubGroup.getPdpInstances().add(new JpaPdp());
275 testJpaPdpSubGroup.getProperties().put(" PropKey ", " Prop Value ");
276 testJpaPdpSubGroup.clean();
277 assertEquals("PropKey", testJpaPdpSubGroup.getProperties().keySet().iterator().next());
278 assertEquals("Prop Value", testJpaPdpSubGroup.getProperties().get("PropKey"));
280 assertEquals(4, testJpaPdpSubGroup.getKeys().size());
282 assertEquals(testJpaPdpSubGroup, new JpaPdpSubGroup(testJpaPdpSubGroup));