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.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.pdp.concepts.PdpGroup;
39 import org.onap.policy.models.pdp.enums.PdpState;
40 import org.onap.policy.models.pdp.testconcepts.DummyJpaPdpSubgroupChild;
43 * Test the {@link JpaPdpGroupSubGroup} class.
45 * @author Liam Fallon (liam.fallon@est.tech)
47 public class JpaPdpGroupTest {
49 private static final String NULL_ERROR = " is marked .*ull but is null";
50 private static final String NULL_KEY_ERROR = "key" + NULL_ERROR;
51 private static final String PDP_GROUP0 = "PDPGroup0";
52 private static final String VERSION = "1.0.0";
55 public void testJpaPdpGroup() {
56 assertThatThrownBy(() -> {
57 new JpaPdpGroup((JpaPdpGroup) null);
58 }).hasMessageMatching("copyConcept" + NULL_ERROR);
60 assertThatThrownBy(() -> {
61 new JpaPdpGroup((PfConceptKey) null);
62 }).hasMessageMatching(NULL_KEY_ERROR);
64 assertThatThrownBy(() -> {
65 new JpaPdpGroup((PdpGroup) null);
66 }).hasMessageMatching("authorativeConcept" + NULL_ERROR);
68 assertThatThrownBy(() -> {
69 new JpaPdpGroup((JpaPdpGroup) null);
70 }).hasMessageMatching("copyConcept" + NULL_ERROR);
72 assertThatThrownBy(() -> {
73 new JpaPdpGroup(null, null, null);
74 }).hasMessageMatching(NULL_KEY_ERROR);
76 assertThatThrownBy(() -> {
77 new JpaPdpGroup(new PfConceptKey(), null, null);
78 }).hasMessageMatching("pdpGroupState" + NULL_ERROR);
80 assertThatThrownBy(() -> {
81 new JpaPdpGroup(new PfConceptKey(), PdpState.PASSIVE, null);
82 }).hasMessageMatching("pdpSubGroups" + NULL_ERROR);
84 assertThatThrownBy(() -> {
85 new JpaPdpGroup(null, PdpState.PASSIVE, null);
86 }).hasMessageMatching(NULL_KEY_ERROR);
88 assertThatThrownBy(() -> {
89 new JpaPdpGroup(null, PdpState.PASSIVE, new ArrayList<>());
90 }).hasMessageMatching(NULL_KEY_ERROR);
92 assertThatThrownBy(() -> {
93 new JpaPdpGroup(null, null, new ArrayList<>());
94 }).hasMessageMatching(NULL_KEY_ERROR);
96 assertNotNull(new JpaPdpGroup((new PfConceptKey())));
97 assertNotNull(new JpaPdpGroup((new JpaPdpGroup())));
101 public void testPdpGroupSet() {
102 PdpGroup testPdpGroup = new PdpGroup();
103 testPdpGroup.setName(PDP_GROUP0);
104 testPdpGroup.setPdpSubgroups(new ArrayList<>());
106 JpaPdpGroup testJpaPdpGroup = setUpSmallJpaPdpGroup();
108 assertEquals(PDP_GROUP0, testJpaPdpGroup.getKey().getName());
110 assertThatThrownBy(() -> {
111 testJpaPdpGroup.fromAuthorative(null);
112 }).hasMessageMatching("pdpGroup" + NULL_ERROR);
114 testJpaPdpGroup.setKey(new PfConceptKey(PDP_GROUP0, VERSION));
115 testJpaPdpGroup.fromAuthorative(testPdpGroup);
117 assertThatThrownBy(() -> new JpaPdpGroup((JpaPdpGroup) null)).isInstanceOf(NullPointerException.class);
119 assertEquals(PDP_GROUP0, testJpaPdpGroup.getKey().getName());
120 assertEquals(PDP_GROUP0, new JpaPdpGroup(testPdpGroup).getKey().getName());
121 assertEquals(PDP_GROUP0, ((PfConceptKey) new JpaPdpGroup(testPdpGroup).getKeys().get(0)).getName());
123 testJpaPdpGroup.clean();
124 assertEquals(PDP_GROUP0, testJpaPdpGroup.getKey().getName());
128 public void testPdpGroupValidation() {
129 JpaPdpGroup testJpaPdpGroup = setUpSmallJpaPdpGroup();
131 assertThatThrownBy(() -> {
132 testJpaPdpGroup.validate(null);
133 }).hasMessageMatching("fieldName" + NULL_ERROR);
135 assertFalse(testJpaPdpGroup.validate("").isValid());
136 testJpaPdpGroup.setPdpGroupState(PdpState.PASSIVE);
137 assertTrue(testJpaPdpGroup.validate("").isValid());
139 testJpaPdpGroup.setKey(PfConceptKey.getNullKey());
140 assertFalse(testJpaPdpGroup.validate("").isValid());
141 testJpaPdpGroup.setKey(new PfConceptKey("PdpGroup0", VERSION));
142 assertTrue(testJpaPdpGroup.validate("").isValid());
144 testJpaPdpGroup.setDescription(" ");
145 assertFalse(testJpaPdpGroup.validate("").isValid());
146 testJpaPdpGroup.setDescription(" A Description ");
147 assertTrue(testJpaPdpGroup.validate("").isValid());
148 testJpaPdpGroup.setDescription(null);
149 assertTrue(testJpaPdpGroup.validate("").isValid());
151 testJpaPdpGroup.setProperties(new LinkedHashMap<>());
152 testJpaPdpGroup.getProperties().put(null, null);
153 assertFalse(testJpaPdpGroup.validate("").isValid());
154 testJpaPdpGroup.getProperties().remove(null);
155 assertTrue(testJpaPdpGroup.validate("").isValid());
157 testJpaPdpGroup.setProperties(new LinkedHashMap<>());
158 testJpaPdpGroup.getProperties().put("NullKey", null);
159 assertFalse(testJpaPdpGroup.validate("").isValid());
160 testJpaPdpGroup.getProperties().remove("NullKey");
161 assertTrue(testJpaPdpGroup.validate("").isValid());
165 public void testPdpSubgroups() {
166 JpaPdpGroup testJpaPdpGroup = setUpJpaPdpGroup();
168 List<JpaPdpSubGroup> jpaPdpSubgroups = testJpaPdpGroup.getPdpSubGroups();
169 assertNotNull(jpaPdpSubgroups);
170 testJpaPdpGroup.setPdpSubGroups(null);
171 assertFalse(testJpaPdpGroup.validate("").isValid());
172 testJpaPdpGroup.setPdpSubGroups(new ArrayList<>());
173 assertTrue(testJpaPdpGroup.validate("").isValid());
174 testJpaPdpGroup.setPdpSubGroups(jpaPdpSubgroups);
175 assertTrue(testJpaPdpGroup.validate("").isValid());
177 JpaPdpGroup otherJpaPdpGroup = new JpaPdpGroup(testJpaPdpGroup);
178 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
179 assertEquals(-1, testJpaPdpGroup.compareTo(null));
180 assertEquals(0, testJpaPdpGroup.compareTo(testJpaPdpGroup));
181 assertNotEquals(0, testJpaPdpGroup.compareTo(new DummyJpaPdpSubgroupChild()));
183 testJpaPdpGroup.getKey().setName("OtherName");
184 assertEquals(-1, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
185 testJpaPdpGroup.getKey().setName("PdpGroup0");
186 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
188 JpaPdpSubGroup anotherPdpSubgroup =
189 new JpaPdpSubGroup(new PfReferenceKey(testJpaPdpGroup.getKey(), "AnotherPdpSubgroup"));
190 testJpaPdpGroup.getPdpSubGroups().add(anotherPdpSubgroup);
191 assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
192 testJpaPdpGroup.getPdpSubGroups().remove(anotherPdpSubgroup);
193 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
195 testJpaPdpGroup.setPdpGroupState(PdpState.ACTIVE);
196 assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
197 testJpaPdpGroup.setPdpGroupState(PdpState.PASSIVE);
198 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
200 testJpaPdpGroup.setDescription("A Description");
201 assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
202 testJpaPdpGroup.setDescription(null);
203 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
205 testJpaPdpGroup.getProperties().put("AnotherProperty", "Some String");
206 assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
207 testJpaPdpGroup.getProperties().remove("AnotherProperty");
208 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
210 PdpGroup psg = testJpaPdpGroup.toAuthorative();
211 assertEquals(0, psg.getProperties().size());
213 testJpaPdpGroup.setProperties(new LinkedHashMap<>());
214 psg = testJpaPdpGroup.toAuthorative();
215 assertEquals(0, psg.getProperties().size());
217 testJpaPdpGroup.setProperties(null);
218 psg = testJpaPdpGroup.toAuthorative();
219 assertNull(psg.getProperties());
220 testJpaPdpGroup.setProperties(new LinkedHashMap<>());
224 public void testPdpGroupsProperties() {
225 JpaPdpGroup testJpaPdpGroup = setUpJpaPdpGroup();
227 testJpaPdpGroup.getProperties().put(" PropKey ", " Prop Value ");
228 testJpaPdpGroup.clean();
229 assertEquals("PropKey", testJpaPdpGroup.getProperties().keySet().iterator().next());
230 assertEquals("Prop Value", testJpaPdpGroup.getProperties().get("PropKey"));
231 testJpaPdpGroup.setDescription(" A Description ");
232 testJpaPdpGroup.clean();
233 assertEquals("A Description", testJpaPdpGroup.getDescription());
235 JpaPdpSubGroup anotherPdpSubgroup =
236 new JpaPdpSubGroup(new PfReferenceKey(testJpaPdpGroup.getKey(), "AnotherPdpSubgroup"));
238 assertEquals(1, testJpaPdpGroup.getKeys().size());
239 testJpaPdpGroup.getPdpSubGroups().add(anotherPdpSubgroup);
240 assertEquals(2, testJpaPdpGroup.getKeys().size());
241 testJpaPdpGroup.clean();
242 assertEquals(2, testJpaPdpGroup.getKeys().size());
244 assertEquals(testJpaPdpGroup, new JpaPdpGroup(testJpaPdpGroup));
247 private JpaPdpGroup setUpSmallJpaPdpGroup() {
248 PdpGroup testPdpGroup = new PdpGroup();
249 testPdpGroup.setName(PDP_GROUP0);
250 testPdpGroup.setPdpSubgroups(new ArrayList<>());
251 testPdpGroup.setVersion(VERSION);
253 JpaPdpGroup testJpaPdpGroup = new JpaPdpGroup();
254 testJpaPdpGroup.setKey(new PfConceptKey(PDP_GROUP0, VERSION));
255 testJpaPdpGroup.fromAuthorative(testPdpGroup);
256 testJpaPdpGroup.clean();
258 return testJpaPdpGroup;
261 private JpaPdpGroup setUpJpaPdpGroup() {
262 JpaPdpGroup testJpaPdpGroup = setUpSmallJpaPdpGroup();
264 testJpaPdpGroup.setKey(new PfConceptKey("PdpGroup0", VERSION));
265 testJpaPdpGroup.setDescription(null);
266 testJpaPdpGroup.setPdpGroupState(PdpState.PASSIVE);
267 testJpaPdpGroup.setProperties(new LinkedHashMap<>());
268 testJpaPdpGroup.clean();
270 return testJpaPdpGroup;