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.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 @NonNull but is null";
51 private static final String PDP_A = "PDP-A";
54 public void testJpaPdpSubGroup() {
55 assertThatThrownBy(() -> {
56 new JpaPdpSubGroup((JpaPdpSubGroup) null);
57 }).hasMessage("copyConcept is marked @NonNull but is null");
59 assertThatThrownBy(() -> {
60 new JpaPdpSubGroup((PfReferenceKey) null);
61 }).hasMessage(NULL_KEY_ERROR);
63 assertThatThrownBy(() -> {
64 new JpaPdpSubGroup((PdpSubGroup) null);
65 }).hasMessage("authorativeConcept is marked @NonNull but is null");
67 assertThatThrownBy(() -> {
68 new JpaPdpSubGroup(null, null, null, null);
69 }).hasMessage(NULL_KEY_ERROR);
71 assertThatThrownBy(() -> {
72 new JpaPdpSubGroup(new PfReferenceKey(), null, null, null);
73 }).hasMessage("supportedPolicyTypes is marked @NonNull but is null");
75 assertThatThrownBy(() -> {
76 new JpaPdpSubGroup(new PfReferenceKey(), new ArrayList<>(), null, null);
77 }).hasMessage("policies is marked @NonNull but is null");
79 assertThatThrownBy(() -> {
80 new JpaPdpSubGroup(null, new ArrayList<>(), null, null);
81 }).hasMessage(NULL_KEY_ERROR);
83 assertThatThrownBy(() -> {
84 new JpaPdpSubGroup(null, new ArrayList<>(), new ArrayList<>(), null);
85 }).hasMessage(NULL_KEY_ERROR);
87 assertThatThrownBy(() -> {
88 new JpaPdpSubGroup(null, null, new ArrayList<>(), null);
89 }).hasMessage(NULL_KEY_ERROR);
91 assertThatThrownBy(() -> {
92 new JpaPdpSubGroup(null, null, null, new ArrayList<>());
93 }).hasMessage(NULL_KEY_ERROR);
95 assertThatThrownBy(() -> {
96 new JpaPdpSubGroup(new PfReferenceKey(), null, null, new ArrayList<>());
97 }).hasMessage("supportedPolicyTypes is marked @NonNull but is null");
99 assertThatThrownBy(() -> {
100 new JpaPdpSubGroup(new PfReferenceKey(), new ArrayList<>(), null, new ArrayList<>());
101 }).hasMessage("policies is marked @NonNull but is null");
103 assertThatThrownBy(() -> {
104 new JpaPdpSubGroup(null, new ArrayList<>(), null, new ArrayList<>());
105 }).hasMessage(NULL_KEY_ERROR);
107 assertThatThrownBy(() -> {
108 new JpaPdpSubGroup(null, new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
109 }).hasMessage(NULL_KEY_ERROR);
111 assertThatThrownBy(() -> {
112 new JpaPdpSubGroup(null, null, new ArrayList<>(), null);
113 }).hasMessage(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 }).hasMessage("pdpSubgroup is marked @NonNull but is null");
130 assertThatThrownBy(() -> {
131 testJpaPdpSubGroup.copyTo(null);
132 }).hasMessage("target is marked @NonNull but is null");
134 assertEquals(PDP_A, testJpaPdpSubGroup.getKey().getLocalName());
135 assertEquals(PDP_A, new JpaPdpSubGroup(testPdpSubgroup).getKey().getLocalName());
136 assertEquals(PDP_A, ((PfReferenceKey) new JpaPdpSubGroup(testPdpSubgroup).getKeys().get(0)).getLocalName());
138 testJpaPdpSubGroup.clean();
139 assertEquals(PDP_A, testJpaPdpSubGroup.getKey().getLocalName());
141 assertThatThrownBy(() -> {
142 testJpaPdpSubGroup.validate(null);
143 }).hasMessage("resultIn is marked @NonNull but is null");
145 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
146 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).toString()
147 .contains("INVALID:parent of key is a null key"));
149 testJpaPdpSubGroup.getKey().setParentConceptKey(new PfConceptKey("Parent:1.0.0"));
150 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
151 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).toString()
152 .contains("INVALID:parent of key is a null key"));
153 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).toString()
154 .contains("INVALID:a PDP subgroup must support at least one policy type"));
156 testJpaPdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
157 testJpaPdpSubGroup.getSupportedPolicyTypes().add(new PfConceptKey("APolicyType:1.0.0"));
158 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
159 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).toString()
160 .contains("INVALID:a PDP subgroup must support at least one policy type"));
162 PfReferenceKey savedKey = testJpaPdpSubGroup.getKey();
163 testJpaPdpSubGroup.setKey(PfReferenceKey.getNullKey());
164 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
165 testJpaPdpSubGroup.setKey(savedKey);
166 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
168 testJpaPdpSubGroup.setProperties(new LinkedHashMap<>());
169 testJpaPdpSubGroup.getProperties().put(null, null);
170 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
171 testJpaPdpSubGroup.getProperties().remove(null);
172 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
174 testJpaPdpSubGroup.setProperties(new LinkedHashMap<>());
175 testJpaPdpSubGroup.getProperties().put("NullKey", null);
176 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
177 testJpaPdpSubGroup.getProperties().remove("NullKey");
178 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
180 testJpaPdpSubGroup.setDesiredInstanceCount(-1);
181 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
182 testJpaPdpSubGroup.setDesiredInstanceCount(0);
183 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
184 testJpaPdpSubGroup.setProperties(null);
186 testJpaPdpSubGroup.setCurrentInstanceCount(-1);
187 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
188 testJpaPdpSubGroup.setCurrentInstanceCount(0);
189 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
190 testJpaPdpSubGroup.setProperties(null);
191 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
193 List<PfConceptKey> supportedPolicyTypes = testJpaPdpSubGroup.getSupportedPolicyTypes();
194 assertNotNull(supportedPolicyTypes);
195 testJpaPdpSubGroup.setSupportedPolicyTypes(null);
196 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
197 testJpaPdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
198 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
199 testJpaPdpSubGroup.setSupportedPolicyTypes(supportedPolicyTypes);
200 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
202 List<PfConceptKey> supportedPolicies = testJpaPdpSubGroup.getPolicies();
203 assertNotNull(supportedPolicies);
204 testJpaPdpSubGroup.setPolicies(null);
205 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
206 testJpaPdpSubGroup.setPolicies(new ArrayList<>());
207 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
208 testJpaPdpSubGroup.setPolicies(supportedPolicies);
209 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
211 List<JpaPdp> pdpInstances = testJpaPdpSubGroup.getPdpInstances();
212 assertNotNull(pdpInstances);
213 testJpaPdpSubGroup.setPdpInstances(null);
214 assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
215 testJpaPdpSubGroup.setPdpInstances(new ArrayList<>());
216 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
217 testJpaPdpSubGroup.setPdpInstances(pdpInstances);
218 assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
220 JpaPdpSubGroup otherJpaPdpSubGroup = new JpaPdpSubGroup(testJpaPdpSubGroup);
221 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
222 assertEquals(-1, testJpaPdpSubGroup.compareTo(null));
223 assertEquals(0, testJpaPdpSubGroup.compareTo(testJpaPdpSubGroup));
224 assertFalse(testJpaPdpSubGroup.compareTo(new DummyJpaPdpSubgroupChild()) == 0);
226 testJpaPdpSubGroup.getKey().setParentKeyName("Parent1");
227 assertEquals(1, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
228 testJpaPdpSubGroup.getKey().setParentKeyName("Parent");
229 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
231 testJpaPdpSubGroup.setCurrentInstanceCount(1);
232 assertEquals(1, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
233 testJpaPdpSubGroup.setCurrentInstanceCount(0);
234 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
236 testJpaPdpSubGroup.setDesiredInstanceCount(1);
237 assertEquals(1, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
238 testJpaPdpSubGroup.setDesiredInstanceCount(0);
239 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
241 PfConceptKey anotherPolicyType = new PfConceptKey("AnotherPolicyType", "1.0.0");
242 testJpaPdpSubGroup.getSupportedPolicyTypes().add(anotherPolicyType);
243 assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
244 testJpaPdpSubGroup.getSupportedPolicyTypes().remove(anotherPolicyType);
245 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
247 PfConceptKey anotherPolicy = new PfConceptKey("AnotherPolicy", "1.0.0");
248 testJpaPdpSubGroup.getPolicies().add(anotherPolicy);
249 assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
250 testJpaPdpSubGroup.getPolicies().remove(anotherPolicy);
251 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
253 JpaPdp anotherPdp = new JpaPdp(new PfReferenceKey(testJpaPdpSubGroup.getKey(), "AnotherPdp"));
254 testJpaPdpSubGroup.getPdpInstances().add(anotherPdp);
255 assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
256 testJpaPdpSubGroup.getPdpInstances().remove(anotherPdp);
257 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
259 testJpaPdpSubGroup.setProperties(new LinkedHashMap<>());
260 testJpaPdpSubGroup.getProperties().put("AnotherProperty", "Some String");
261 assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
262 testJpaPdpSubGroup.getProperties().remove("AnotherProperty");
263 testJpaPdpSubGroup.setProperties(null);
264 assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
266 PdpSubGroup psg = testJpaPdpSubGroup.toAuthorative();
267 assertNull(psg.getProperties());
269 testJpaPdpSubGroup.setProperties(new LinkedHashMap<>());
270 psg = testJpaPdpSubGroup.toAuthorative();
271 assertEquals(0, psg.getProperties().size());
273 testJpaPdpSubGroup.getPolicies().add(new PfConceptKey("APolicy:1.0.0"));
274 testJpaPdpSubGroup.getPdpInstances().add(new JpaPdp());
276 testJpaPdpSubGroup.getProperties().put(" PropKey ", " Prop Value ");
277 testJpaPdpSubGroup.clean();
278 assertEquals("PropKey", testJpaPdpSubGroup.getProperties().keySet().iterator().next());
279 assertEquals("Prop Value", testJpaPdpSubGroup.getProperties().get("PropKey"));
281 assertEquals(4, testJpaPdpSubGroup.getKeys().size());