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.PdpGroup;
41 import org.onap.policy.models.pdp.enums.PdpState;
42 import org.onap.policy.models.pdp.testconcepts.DummyJpaPdpSubgroupChild;
45 * Test the {@link JpaPdpGroupSubGroup} class.
47 * @author Liam Fallon (liam.fallon@est.tech)
49 public class JpaPdpGroupTest {
51 private static final String NULL_ERROR = " is marked .*ull but is null";
52 private static final String NULL_KEY_ERROR = "key" + NULL_ERROR;
53 private static final String PDP_GROUP0 = "PDPGroup0";
54 private static final String VERSION = "1.0.0";
57 public void testJpaPdpGroup() {
58 assertThatThrownBy(() -> {
59 new JpaPdpGroup((JpaPdpGroup) null);
60 }).hasMessageMatching("copyConcept" + NULL_ERROR);
62 assertThatThrownBy(() -> {
63 new JpaPdpGroup((PfConceptKey) null);
64 }).hasMessageMatching(NULL_KEY_ERROR);
66 assertThatThrownBy(() -> {
67 new JpaPdpGroup((PdpGroup) null);
68 }).hasMessageMatching("authorativeConcept" + NULL_ERROR);
70 assertThatThrownBy(() -> {
71 new JpaPdpGroup((JpaPdpGroup) null);
72 }).hasMessageMatching("copyConcept" + NULL_ERROR);
74 assertThatThrownBy(() -> {
75 new JpaPdpGroup(null, null, null);
76 }).hasMessageMatching(NULL_KEY_ERROR);
78 assertThatThrownBy(() -> {
79 new JpaPdpGroup(new PfConceptKey(), null, null);
80 }).hasMessageMatching("pdpGroupState" + NULL_ERROR);
82 assertThatThrownBy(() -> {
83 new JpaPdpGroup(new PfConceptKey(), PdpState.PASSIVE, null);
84 }).hasMessageMatching("pdpSubGroups" + NULL_ERROR);
86 assertThatThrownBy(() -> {
87 new JpaPdpGroup(null, PdpState.PASSIVE, null);
88 }).hasMessageMatching(NULL_KEY_ERROR);
90 assertThatThrownBy(() -> {
91 new JpaPdpGroup(null, PdpState.PASSIVE, new ArrayList<>());
92 }).hasMessageMatching(NULL_KEY_ERROR);
94 assertThatThrownBy(() -> {
95 new JpaPdpGroup(null, null, new ArrayList<>());
96 }).hasMessageMatching(NULL_KEY_ERROR);
98 assertNotNull(new JpaPdpGroup((new PfConceptKey())));
99 assertNotNull(new JpaPdpGroup((new JpaPdpGroup())));
101 PdpGroup testPdpGroup = new PdpGroup();
102 testPdpGroup.setName(PDP_GROUP0);
103 testPdpGroup.setPdpSubgroups(new ArrayList<>());
104 JpaPdpGroup testJpaPdpGroup = new JpaPdpGroup();
105 testJpaPdpGroup.setKey(null);
107 testJpaPdpGroup.setKey(new PfConceptKey());
109 testPdpGroup.setVersion(VERSION);
110 testJpaPdpGroup.fromAuthorative(testPdpGroup);
112 assertEquals(PDP_GROUP0, testJpaPdpGroup.getKey().getName());
113 testJpaPdpGroup.setKey(PfConceptKey.getNullKey());
114 testJpaPdpGroup.fromAuthorative(testPdpGroup);
116 assertThatThrownBy(() -> {
117 testJpaPdpGroup.fromAuthorative(null);
118 }).hasMessageMatching("pdpGroup" + NULL_ERROR);
120 testJpaPdpGroup.setKey(new PfConceptKey(PDP_GROUP0, VERSION));
121 testJpaPdpGroup.fromAuthorative(testPdpGroup);
123 assertThatThrownBy(() -> new JpaPdpGroup((JpaPdpGroup) null)).isInstanceOf(NullPointerException.class);
125 assertEquals(PDP_GROUP0, testJpaPdpGroup.getKey().getName());
126 assertEquals(PDP_GROUP0, new JpaPdpGroup(testPdpGroup).getKey().getName());
127 assertEquals(PDP_GROUP0, ((PfConceptKey) new JpaPdpGroup(testPdpGroup).getKeys().get(0)).getName());
129 testJpaPdpGroup.clean();
130 assertEquals(PDP_GROUP0, testJpaPdpGroup.getKey().getName());
132 assertThatThrownBy(() -> {
133 testJpaPdpGroup.validate(null);
134 }).hasMessageMatching("resultIn" + NULL_ERROR);
136 assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
137 testJpaPdpGroup.setPdpGroupState(PdpState.PASSIVE);
138 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
140 testJpaPdpGroup.setKey(PfConceptKey.getNullKey());
141 assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
142 testJpaPdpGroup.setKey(new PfConceptKey("PdpGroup0", VERSION));
143 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
145 testJpaPdpGroup.setDescription(" ");
146 assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
147 testJpaPdpGroup.setDescription(" A Description ");
148 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
149 testJpaPdpGroup.setDescription(null);
150 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
152 testJpaPdpGroup.setProperties(new LinkedHashMap<>());
153 testJpaPdpGroup.getProperties().put(null, null);
154 assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
155 testJpaPdpGroup.getProperties().remove(null);
156 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
158 testJpaPdpGroup.setProperties(new LinkedHashMap<>());
159 testJpaPdpGroup.getProperties().put("NullKey", null);
160 assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
161 testJpaPdpGroup.getProperties().remove("NullKey");
162 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
164 List<JpaPdpSubGroup> jpaPdpSubgroups = testJpaPdpGroup.getPdpSubGroups();
165 assertNotNull(jpaPdpSubgroups);
166 testJpaPdpGroup.setPdpSubGroups(null);
167 assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
168 testJpaPdpGroup.setPdpSubGroups(new ArrayList<>());
169 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
170 testJpaPdpGroup.setPdpSubGroups(jpaPdpSubgroups);
171 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
173 JpaPdpGroup otherJpaPdpGroup = new JpaPdpGroup(testJpaPdpGroup);
174 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
175 assertEquals(-1, testJpaPdpGroup.compareTo(null));
176 assertEquals(0, testJpaPdpGroup.compareTo(testJpaPdpGroup));
177 assertFalse(testJpaPdpGroup.compareTo(new DummyJpaPdpSubgroupChild()) == 0);
179 testJpaPdpGroup.getKey().setName("OtherName");
180 assertEquals(-1, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
181 testJpaPdpGroup.getKey().setName("PdpGroup0");
182 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
184 JpaPdpSubGroup anotherPdpSubgroup =
185 new JpaPdpSubGroup(new PfReferenceKey(testJpaPdpGroup.getKey(), "AnotherPdpSubgroup"));
186 testJpaPdpGroup.getPdpSubGroups().add(anotherPdpSubgroup);
187 assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
188 testJpaPdpGroup.getPdpSubGroups().remove(anotherPdpSubgroup);
189 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
191 testJpaPdpGroup.setPdpGroupState(PdpState.ACTIVE);
192 assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
193 testJpaPdpGroup.setPdpGroupState(PdpState.PASSIVE);
194 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
196 testJpaPdpGroup.setDescription("A Description");
197 assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
198 testJpaPdpGroup.setDescription(null);
199 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
201 testJpaPdpGroup.getProperties().put("AnotherProperty", "Some String");
202 assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
203 testJpaPdpGroup.getProperties().remove("AnotherProperty");
204 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
206 PdpGroup psg = testJpaPdpGroup.toAuthorative();
207 assertEquals(0, psg.getProperties().size());
209 testJpaPdpGroup.setProperties(new LinkedHashMap<>());
210 psg = testJpaPdpGroup.toAuthorative();
211 assertEquals(0, psg.getProperties().size());
213 testJpaPdpGroup.setProperties(null);
214 psg = testJpaPdpGroup.toAuthorative();
215 assertNull(psg.getProperties());
216 testJpaPdpGroup.setProperties(new LinkedHashMap<>());
218 testJpaPdpGroup.clean();
219 testJpaPdpGroup.getProperties().put(" PropKey ", " Prop Value ");
220 testJpaPdpGroup.clean();
221 assertEquals("PropKey", testJpaPdpGroup.getProperties().keySet().iterator().next());
222 assertEquals("Prop Value", testJpaPdpGroup.getProperties().get("PropKey"));
223 testJpaPdpGroup.setDescription(" A Description ");
224 testJpaPdpGroup.clean();
225 assertEquals("A Description", testJpaPdpGroup.getDescription());
227 assertEquals(1, testJpaPdpGroup.getKeys().size());
228 testJpaPdpGroup.getPdpSubGroups().add(anotherPdpSubgroup);
229 assertEquals(2, testJpaPdpGroup.getKeys().size());
230 testJpaPdpGroup.clean();
231 assertEquals(2, testJpaPdpGroup.getKeys().size());
233 assertEquals(testJpaPdpGroup, new JpaPdpGroup(testJpaPdpGroup));