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;
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.PfValidationResult;
39 import org.onap.policy.models.pdp.concepts.PdpGroup;
40 import org.onap.policy.models.pdp.enums.PdpState;
41 import org.onap.policy.models.pdp.testconcepts.DummyJpaPdpSubgroupChild;
44 * Test the {@link JpaPdpGroupSubGroup} class.
46 * @author Liam Fallon (liam.fallon@est.tech)
48 public class JpaPdpGroupTest {
50 private static final String NULL_ERROR = " is marked .*ull but is null";
51 private static final String NULL_KEY_ERROR = "key" + NULL_ERROR;
52 private static final String PDP_GROUP0 = "PDPGroup0";
53 private static final String VERSION = "1.0.0";
56 public void testJpaPdpGroup() {
57 assertThatThrownBy(() -> {
58 new JpaPdpGroup((JpaPdpGroup) null);
59 }).hasMessageMatching("copyConcept" + NULL_ERROR);
61 assertThatThrownBy(() -> {
62 new JpaPdpGroup((PfConceptKey) null);
63 }).hasMessageMatching(NULL_KEY_ERROR);
65 assertThatThrownBy(() -> {
66 new JpaPdpGroup((PdpGroup) null);
67 }).hasMessageMatching("authorativeConcept" + NULL_ERROR);
69 assertThatThrownBy(() -> {
70 new JpaPdpGroup((JpaPdpGroup) null);
71 }).hasMessageMatching("copyConcept" + NULL_ERROR);
73 assertThatThrownBy(() -> {
74 new JpaPdpGroup(null, null, null);
75 }).hasMessageMatching(NULL_KEY_ERROR);
77 assertThatThrownBy(() -> {
78 new JpaPdpGroup(new PfConceptKey(), null, null);
79 }).hasMessageMatching("pdpGroupState" + NULL_ERROR);
81 assertThatThrownBy(() -> {
82 new JpaPdpGroup(new PfConceptKey(), PdpState.PASSIVE, null);
83 }).hasMessageMatching("pdpSubGroups" + NULL_ERROR);
85 assertThatThrownBy(() -> {
86 new JpaPdpGroup(null, PdpState.PASSIVE, null);
87 }).hasMessageMatching(NULL_KEY_ERROR);
89 assertThatThrownBy(() -> {
90 new JpaPdpGroup(null, PdpState.PASSIVE, new ArrayList<>());
91 }).hasMessageMatching(NULL_KEY_ERROR);
93 assertThatThrownBy(() -> {
94 new JpaPdpGroup(null, null, new ArrayList<>());
95 }).hasMessageMatching(NULL_KEY_ERROR);
97 assertNotNull(new JpaPdpGroup((new PfConceptKey())));
98 assertNotNull(new JpaPdpGroup((new JpaPdpGroup())));
100 PdpGroup testPdpGroup = new PdpGroup();
101 testPdpGroup.setName(PDP_GROUP0);
102 testPdpGroup.setPdpSubgroups(new ArrayList<>());
103 JpaPdpGroup testJpaPdpGroup = new JpaPdpGroup();
104 testJpaPdpGroup.setKey(null);
106 testJpaPdpGroup.setKey(new PfConceptKey());
108 testPdpGroup.setVersion(VERSION);
109 testJpaPdpGroup.fromAuthorative(testPdpGroup);
111 assertEquals(PDP_GROUP0, testJpaPdpGroup.getKey().getName());
112 testJpaPdpGroup.setKey(PfConceptKey.getNullKey());
113 testJpaPdpGroup.fromAuthorative(testPdpGroup);
115 assertThatThrownBy(() -> {
116 testJpaPdpGroup.fromAuthorative(null);
117 }).hasMessageMatching("pdpGroup" + NULL_ERROR);
119 testJpaPdpGroup.setKey(new PfConceptKey(PDP_GROUP0, VERSION));
120 testJpaPdpGroup.fromAuthorative(testPdpGroup);
122 assertThatThrownBy(() -> new JpaPdpGroup((JpaPdpGroup) null)).isInstanceOf(NullPointerException.class);
124 assertEquals(PDP_GROUP0, testJpaPdpGroup.getKey().getName());
125 assertEquals(PDP_GROUP0, new JpaPdpGroup(testPdpGroup).getKey().getName());
126 assertEquals(PDP_GROUP0, ((PfConceptKey) new JpaPdpGroup(testPdpGroup).getKeys().get(0)).getName());
128 testJpaPdpGroup.clean();
129 assertEquals(PDP_GROUP0, testJpaPdpGroup.getKey().getName());
131 assertThatThrownBy(() -> {
132 testJpaPdpGroup.validate(null);
133 }).hasMessageMatching("resultIn" + NULL_ERROR);
135 assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
136 testJpaPdpGroup.setPdpGroupState(PdpState.PASSIVE);
137 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
139 testJpaPdpGroup.setKey(PfConceptKey.getNullKey());
140 assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
141 testJpaPdpGroup.setKey(new PfConceptKey("PdpGroup0", VERSION));
142 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
144 testJpaPdpGroup.setDescription(" ");
145 assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
146 testJpaPdpGroup.setDescription(" A Description ");
147 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
148 testJpaPdpGroup.setDescription(null);
149 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
151 testJpaPdpGroup.setProperties(new LinkedHashMap<>());
152 testJpaPdpGroup.getProperties().put(null, null);
153 assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
154 testJpaPdpGroup.getProperties().remove(null);
155 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
157 testJpaPdpGroup.setProperties(new LinkedHashMap<>());
158 testJpaPdpGroup.getProperties().put("NullKey", null);
159 assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
160 testJpaPdpGroup.getProperties().remove("NullKey");
161 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
163 List<JpaPdpSubGroup> jpaPdpSubgroups = testJpaPdpGroup.getPdpSubGroups();
164 assertNotNull(jpaPdpSubgroups);
165 testJpaPdpGroup.setPdpSubGroups(null);
166 assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
167 testJpaPdpGroup.setPdpSubGroups(new ArrayList<>());
168 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
169 testJpaPdpGroup.setPdpSubGroups(jpaPdpSubgroups);
170 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
172 JpaPdpGroup otherJpaPdpGroup = new JpaPdpGroup(testJpaPdpGroup);
173 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
174 assertEquals(-1, testJpaPdpGroup.compareTo(null));
175 assertEquals(0, testJpaPdpGroup.compareTo(testJpaPdpGroup));
176 assertFalse(testJpaPdpGroup.compareTo(new DummyJpaPdpSubgroupChild()) == 0);
178 testJpaPdpGroup.getKey().setName("OtherName");
179 assertEquals(-1, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
180 testJpaPdpGroup.getKey().setName("PdpGroup0");
181 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
183 JpaPdpSubGroup anotherPdpSubgroup =
184 new JpaPdpSubGroup(new PfReferenceKey(testJpaPdpGroup.getKey(), "AnotherPdpSubgroup"));
185 testJpaPdpGroup.getPdpSubGroups().add(anotherPdpSubgroup);
186 assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
187 testJpaPdpGroup.getPdpSubGroups().remove(anotherPdpSubgroup);
188 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
190 testJpaPdpGroup.setPdpGroupState(PdpState.ACTIVE);
191 assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
192 testJpaPdpGroup.setPdpGroupState(PdpState.PASSIVE);
193 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
195 testJpaPdpGroup.setDescription("A Description");
196 assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
197 testJpaPdpGroup.setDescription(null);
198 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
200 testJpaPdpGroup.getProperties().put("AnotherProperty", "Some String");
201 assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
202 testJpaPdpGroup.getProperties().remove("AnotherProperty");
203 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
205 PdpGroup psg = testJpaPdpGroup.toAuthorative();
206 assertEquals(0, psg.getProperties().size());
208 testJpaPdpGroup.setProperties(new LinkedHashMap<>());
209 psg = testJpaPdpGroup.toAuthorative();
210 assertEquals(0, psg.getProperties().size());
212 testJpaPdpGroup.setProperties(null);
213 psg = testJpaPdpGroup.toAuthorative();
214 assertNull(psg.getProperties());
215 testJpaPdpGroup.setProperties(new LinkedHashMap<>());
217 testJpaPdpGroup.clean();
218 testJpaPdpGroup.getProperties().put(" PropKey ", " Prop Value ");
219 testJpaPdpGroup.clean();
220 assertEquals("PropKey", testJpaPdpGroup.getProperties().keySet().iterator().next());
221 assertEquals("Prop Value", testJpaPdpGroup.getProperties().get("PropKey"));
222 testJpaPdpGroup.setDescription(" A Description ");
223 testJpaPdpGroup.clean();
224 assertEquals("A Description", testJpaPdpGroup.getDescription());
226 assertEquals(1, testJpaPdpGroup.getKeys().size());
227 testJpaPdpGroup.getPdpSubGroups().add(anotherPdpSubgroup);
228 assertEquals(2, testJpaPdpGroup.getKeys().size());
229 testJpaPdpGroup.clean();
230 assertEquals(2, testJpaPdpGroup.getKeys().size());
232 assertEquals(testJpaPdpGroup, new JpaPdpGroup(testJpaPdpGroup));