2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.pdp.persistence.concepts;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
31 import java.util.ArrayList;
32 import java.util.LinkedHashMap;
33 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 {
51 public void testJpaPdpGroup() {
52 assertThatThrownBy(() -> {
53 new JpaPdpGroup((JpaPdpGroup) null);
54 }).hasMessage("copyConcept is marked @NonNull but is null");
56 assertThatThrownBy(() -> {
57 new JpaPdpGroup((PfConceptKey) null);
58 }).hasMessage("key is marked @NonNull but is null");
60 assertThatThrownBy(() -> {
61 new JpaPdpGroup((PdpGroup) null);
62 }).hasMessage("authorativeConcept is marked @NonNull but is null");
64 assertThatThrownBy(() -> {
65 new JpaPdpGroup((JpaPdpGroup) null);
66 }).hasMessage("copyConcept is marked @NonNull but is null");
68 assertThatThrownBy(() -> {
69 new JpaPdpGroup(null, null, null);
70 }).hasMessage("key is marked @NonNull but is null");
72 assertThatThrownBy(() -> {
73 new JpaPdpGroup(new PfConceptKey(), null, null);
74 }).hasMessage("pdpGroupState is marked @NonNull but is null");
76 assertThatThrownBy(() -> {
77 new JpaPdpGroup(new PfConceptKey(), PdpState.PASSIVE, null);
78 }).hasMessage("pdpSubGroups is marked @NonNull but is null");
80 assertThatThrownBy(() -> {
81 new JpaPdpGroup(null, PdpState.PASSIVE, null);
82 }).hasMessage("key is marked @NonNull but is null");
84 assertThatThrownBy(() -> {
85 new JpaPdpGroup(null, PdpState.PASSIVE, new ArrayList<>());
86 }).hasMessage("key is marked @NonNull but is null");
88 assertThatThrownBy(() -> {
89 new JpaPdpGroup(null, null, new ArrayList<>());
90 }).hasMessage("key is marked @NonNull but is null");
92 assertNotNull(new JpaPdpGroup((new PfConceptKey())));
93 assertNotNull(new JpaPdpGroup((new JpaPdpGroup())));
95 PdpGroup testPdpGroup = new PdpGroup();
96 testPdpGroup.setName("PDPGroup0");
97 testPdpGroup.setPdpSubgroups(new ArrayList<>());
98 JpaPdpGroup testJpaPdpGroup = new JpaPdpGroup();
99 testJpaPdpGroup.setKey(null);
101 testJpaPdpGroup.setKey(new PfConceptKey());
103 testPdpGroup.setVersion("1.0.0");
104 testJpaPdpGroup.fromAuthorative(testPdpGroup);
106 assertEquals("PDPGroup0", testJpaPdpGroup.getKey().getName());
107 testJpaPdpGroup.setKey(PfConceptKey.getNullKey());
108 testJpaPdpGroup.fromAuthorative(testPdpGroup);
110 assertThatThrownBy(() -> {
111 testJpaPdpGroup.fromAuthorative(null);
112 }).hasMessage("pdpGroup is marked @NonNull but is null");
114 testJpaPdpGroup.setKey(new PfConceptKey("PDPGroup0", "1.0.0"));
115 testJpaPdpGroup.fromAuthorative(testPdpGroup);
117 assertThatThrownBy(() -> {
118 testJpaPdpGroup.copyTo(null);
119 }).hasMessage("target is marked @NonNull but is null");
121 assertEquals("PDPGroup0", testJpaPdpGroup.getKey().getName());
122 assertEquals("PDPGroup0", new JpaPdpGroup(testPdpGroup).getKey().getName());
123 assertEquals("PDPGroup0", ((PfConceptKey) new JpaPdpGroup(testPdpGroup).getKeys().get(0)).getName());
125 testJpaPdpGroup.clean();
126 assertEquals("PDPGroup0", testJpaPdpGroup.getKey().getName());
128 assertThatThrownBy(() -> {
129 testJpaPdpGroup.validate(null);
130 }).hasMessage("resultIn is marked @NonNull but is null");
132 assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
133 testJpaPdpGroup.setPdpGroupState(PdpState.PASSIVE);
134 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
136 testJpaPdpGroup.setKey(PfConceptKey.getNullKey());
137 assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
138 testJpaPdpGroup.setKey(new PfConceptKey("PdpGroup0", "1.0.0"));
139 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
141 testJpaPdpGroup.setDescription(" ");
142 assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
143 testJpaPdpGroup.setDescription(" A Description ");
144 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
145 testJpaPdpGroup.setDescription(null);
146 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
148 testJpaPdpGroup.setProperties(new LinkedHashMap<>());
149 testJpaPdpGroup.getProperties().put(null, null);
150 assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
151 testJpaPdpGroup.getProperties().remove(null);
152 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
154 testJpaPdpGroup.setProperties(new LinkedHashMap<>());
155 testJpaPdpGroup.getProperties().put("NullKey", null);
156 assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
157 testJpaPdpGroup.getProperties().remove("NullKey");
158 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
160 List<JpaPdpSubGroup> jpaPdpSubgroups = testJpaPdpGroup.getPdpSubGroups();
161 assertNotNull(jpaPdpSubgroups);
162 testJpaPdpGroup.setPdpSubGroups(null);
163 assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
164 testJpaPdpGroup.setPdpSubGroups(new ArrayList<>());
165 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
166 testJpaPdpGroup.setPdpSubGroups(jpaPdpSubgroups);
167 assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
169 JpaPdpGroup otherJpaPdpGroup = new JpaPdpGroup(testJpaPdpGroup);
170 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
171 assertEquals(-1, testJpaPdpGroup.compareTo(null));
172 assertEquals(0, testJpaPdpGroup.compareTo(testJpaPdpGroup));
173 assertFalse(testJpaPdpGroup.compareTo(new DummyJpaPdpSubgroupChild()) == 0);
175 testJpaPdpGroup.getKey().setName("OtherName");
176 assertEquals(-1, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
177 testJpaPdpGroup.getKey().setName("PdpGroup0");
178 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
180 JpaPdpSubGroup anotherPdpSubgroup =
181 new JpaPdpSubGroup(new PfReferenceKey(testJpaPdpGroup.getKey(), "AnotherPdpSubgroup"));
182 testJpaPdpGroup.getPdpSubGroups().add(anotherPdpSubgroup);
183 assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
184 testJpaPdpGroup.getPdpSubGroups().remove(anotherPdpSubgroup);
185 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
187 testJpaPdpGroup.setPdpGroupState(PdpState.ACTIVE);
188 assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
189 testJpaPdpGroup.setPdpGroupState(PdpState.PASSIVE);
190 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
192 testJpaPdpGroup.setDescription("A Description");
193 assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
194 testJpaPdpGroup.setDescription(null);
195 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
197 testJpaPdpGroup.getProperties().put("AnotherProperty", "Some String");
198 assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
199 testJpaPdpGroup.getProperties().remove("AnotherProperty");
200 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
202 PdpGroup psg = testJpaPdpGroup.toAuthorative();
203 assertEquals(0, psg.getProperties().size());
205 testJpaPdpGroup.setProperties(new LinkedHashMap<>());
206 psg = testJpaPdpGroup.toAuthorative();
207 assertEquals(0, psg.getProperties().size());
209 testJpaPdpGroup.setProperties(null);
210 psg = testJpaPdpGroup.toAuthorative();
211 assertNull(psg.getProperties());
212 testJpaPdpGroup.setProperties(new LinkedHashMap<>());
214 testJpaPdpGroup.clean();
215 testJpaPdpGroup.getProperties().put(" PropKey ", " Prop Value ");
216 testJpaPdpGroup.clean();
217 assertEquals("PropKey", testJpaPdpGroup.getProperties().keySet().iterator().next());
218 assertEquals("Prop Value", testJpaPdpGroup.getProperties().get("PropKey"));
219 testJpaPdpGroup.setDescription(" A Description ");
220 testJpaPdpGroup.clean();
221 assertEquals("A Description", testJpaPdpGroup.getDescription());
223 assertEquals(1, testJpaPdpGroup.getKeys().size());
224 testJpaPdpGroup.getPdpSubGroups().add(anotherPdpSubgroup);
225 assertEquals(2, testJpaPdpGroup.getKeys().size());
226 testJpaPdpGroup.clean();
227 assertEquals(2, testJpaPdpGroup.getKeys().size());