2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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())));
99 PdpGroup testPdpGroup = new PdpGroup();
100 testPdpGroup.setName(PDP_GROUP0);
101 testPdpGroup.setPdpSubgroups(new ArrayList<>());
102 JpaPdpGroup testJpaPdpGroup = new JpaPdpGroup();
103 testJpaPdpGroup.setKey(null);
105 testJpaPdpGroup.setKey(new PfConceptKey());
107 testPdpGroup.setVersion(VERSION);
108 testJpaPdpGroup.fromAuthorative(testPdpGroup);
110 assertEquals(PDP_GROUP0, testJpaPdpGroup.getKey().getName());
111 testJpaPdpGroup.setKey(PfConceptKey.getNullKey());
112 testJpaPdpGroup.fromAuthorative(testPdpGroup);
114 assertThatThrownBy(() -> {
115 testJpaPdpGroup.fromAuthorative(null);
116 }).hasMessageMatching("pdpGroup" + NULL_ERROR);
118 testJpaPdpGroup.setKey(new PfConceptKey(PDP_GROUP0, VERSION));
119 testJpaPdpGroup.fromAuthorative(testPdpGroup);
121 assertThatThrownBy(() -> new JpaPdpGroup((JpaPdpGroup) null)).isInstanceOf(NullPointerException.class);
123 assertEquals(PDP_GROUP0, testJpaPdpGroup.getKey().getName());
124 assertEquals(PDP_GROUP0, new JpaPdpGroup(testPdpGroup).getKey().getName());
125 assertEquals(PDP_GROUP0, ((PfConceptKey) new JpaPdpGroup(testPdpGroup).getKeys().get(0)).getName());
127 testJpaPdpGroup.clean();
128 assertEquals(PDP_GROUP0, testJpaPdpGroup.getKey().getName());
130 assertThatThrownBy(() -> {
131 testJpaPdpGroup.validate(null);
132 }).hasMessageMatching("fieldName" + NULL_ERROR);
134 assertFalse(testJpaPdpGroup.validate("").isValid());
135 testJpaPdpGroup.setPdpGroupState(PdpState.PASSIVE);
136 assertTrue(testJpaPdpGroup.validate("").isValid());
138 testJpaPdpGroup.setKey(PfConceptKey.getNullKey());
139 assertFalse(testJpaPdpGroup.validate("").isValid());
140 testJpaPdpGroup.setKey(new PfConceptKey("PdpGroup0", VERSION));
141 assertTrue(testJpaPdpGroup.validate("").isValid());
143 testJpaPdpGroup.setDescription(" ");
144 assertFalse(testJpaPdpGroup.validate("").isValid());
145 testJpaPdpGroup.setDescription(" A Description ");
146 assertTrue(testJpaPdpGroup.validate("").isValid());
147 testJpaPdpGroup.setDescription(null);
148 assertTrue(testJpaPdpGroup.validate("").isValid());
150 testJpaPdpGroup.setProperties(new LinkedHashMap<>());
151 testJpaPdpGroup.getProperties().put(null, null);
152 assertFalse(testJpaPdpGroup.validate("").isValid());
153 testJpaPdpGroup.getProperties().remove(null);
154 assertTrue(testJpaPdpGroup.validate("").isValid());
156 testJpaPdpGroup.setProperties(new LinkedHashMap<>());
157 testJpaPdpGroup.getProperties().put("NullKey", null);
158 assertFalse(testJpaPdpGroup.validate("").isValid());
159 testJpaPdpGroup.getProperties().remove("NullKey");
160 assertTrue(testJpaPdpGroup.validate("").isValid());
162 List<JpaPdpSubGroup> jpaPdpSubgroups = testJpaPdpGroup.getPdpSubGroups();
163 assertNotNull(jpaPdpSubgroups);
164 testJpaPdpGroup.setPdpSubGroups(null);
165 assertFalse(testJpaPdpGroup.validate("").isValid());
166 testJpaPdpGroup.setPdpSubGroups(new ArrayList<>());
167 assertTrue(testJpaPdpGroup.validate("").isValid());
168 testJpaPdpGroup.setPdpSubGroups(jpaPdpSubgroups);
169 assertTrue(testJpaPdpGroup.validate("").isValid());
171 JpaPdpGroup otherJpaPdpGroup = new JpaPdpGroup(testJpaPdpGroup);
172 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
173 assertEquals(-1, testJpaPdpGroup.compareTo(null));
174 assertEquals(0, testJpaPdpGroup.compareTo(testJpaPdpGroup));
175 assertNotEquals(0, testJpaPdpGroup.compareTo(new DummyJpaPdpSubgroupChild()));
177 testJpaPdpGroup.getKey().setName("OtherName");
178 assertEquals(-1, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
179 testJpaPdpGroup.getKey().setName("PdpGroup0");
180 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
182 JpaPdpSubGroup anotherPdpSubgroup =
183 new JpaPdpSubGroup(new PfReferenceKey(testJpaPdpGroup.getKey(), "AnotherPdpSubgroup"));
184 testJpaPdpGroup.getPdpSubGroups().add(anotherPdpSubgroup);
185 assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
186 testJpaPdpGroup.getPdpSubGroups().remove(anotherPdpSubgroup);
187 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
189 testJpaPdpGroup.setPdpGroupState(PdpState.ACTIVE);
190 assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
191 testJpaPdpGroup.setPdpGroupState(PdpState.PASSIVE);
192 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
194 testJpaPdpGroup.setDescription("A Description");
195 assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
196 testJpaPdpGroup.setDescription(null);
197 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
199 testJpaPdpGroup.getProperties().put("AnotherProperty", "Some String");
200 assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
201 testJpaPdpGroup.getProperties().remove("AnotherProperty");
202 assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
204 PdpGroup psg = testJpaPdpGroup.toAuthorative();
205 assertEquals(0, psg.getProperties().size());
207 testJpaPdpGroup.setProperties(new LinkedHashMap<>());
208 psg = testJpaPdpGroup.toAuthorative();
209 assertEquals(0, psg.getProperties().size());
211 testJpaPdpGroup.setProperties(null);
212 psg = testJpaPdpGroup.toAuthorative();
213 assertNull(psg.getProperties());
214 testJpaPdpGroup.setProperties(new LinkedHashMap<>());
216 testJpaPdpGroup.clean();
217 testJpaPdpGroup.getProperties().put(" PropKey ", " Prop Value ");
218 testJpaPdpGroup.clean();
219 assertEquals("PropKey", testJpaPdpGroup.getProperties().keySet().iterator().next());
220 assertEquals("Prop Value", testJpaPdpGroup.getProperties().get("PropKey"));
221 testJpaPdpGroup.setDescription(" A Description ");
222 testJpaPdpGroup.clean();
223 assertEquals("A Description", testJpaPdpGroup.getDescription());
225 assertEquals(1, testJpaPdpGroup.getKeys().size());
226 testJpaPdpGroup.getPdpSubGroups().add(anotherPdpSubgroup);
227 assertEquals(2, testJpaPdpGroup.getKeys().size());
228 testJpaPdpGroup.clean();
229 assertEquals(2, testJpaPdpGroup.getKeys().size());
231 assertEquals(testJpaPdpGroup, new JpaPdpGroup(testJpaPdpGroup));