Fix Sonar Issues in models-pdp
[policy/models.git] / models-pdp / src / test / java / org / onap / policy / models / pdp / persistence / concepts / JpaPdpGroupTest.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.pdp.persistence.concepts;
23
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;
31
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;
41
42 /**
43  * Test the {@link JpaPdpGroupSubGroup} class.
44  *
45  * @author Liam Fallon (liam.fallon@est.tech)
46  */
47 public class JpaPdpGroupTest {
48
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";
53
54     @Test
55     public void testJpaPdpGroup() {
56         assertThatThrownBy(() -> {
57             new JpaPdpGroup((JpaPdpGroup) null);
58         }).hasMessageMatching("copyConcept" + NULL_ERROR);
59
60         assertThatThrownBy(() -> {
61             new JpaPdpGroup((PfConceptKey) null);
62         }).hasMessageMatching(NULL_KEY_ERROR);
63
64         assertThatThrownBy(() -> {
65             new JpaPdpGroup((PdpGroup) null);
66         }).hasMessageMatching("authorativeConcept" + NULL_ERROR);
67
68         assertThatThrownBy(() -> {
69             new JpaPdpGroup((JpaPdpGroup) null);
70         }).hasMessageMatching("copyConcept" + NULL_ERROR);
71
72         assertThatThrownBy(() -> {
73             new JpaPdpGroup(null, null, null);
74         }).hasMessageMatching(NULL_KEY_ERROR);
75
76         assertThatThrownBy(() -> {
77             new JpaPdpGroup(new PfConceptKey(), null, null);
78         }).hasMessageMatching("pdpGroupState" + NULL_ERROR);
79
80         assertThatThrownBy(() -> {
81             new JpaPdpGroup(new PfConceptKey(), PdpState.PASSIVE, null);
82         }).hasMessageMatching("pdpSubGroups" + NULL_ERROR);
83
84         assertThatThrownBy(() -> {
85             new JpaPdpGroup(null, PdpState.PASSIVE, null);
86         }).hasMessageMatching(NULL_KEY_ERROR);
87
88         assertThatThrownBy(() -> {
89             new JpaPdpGroup(null, PdpState.PASSIVE, new ArrayList<>());
90         }).hasMessageMatching(NULL_KEY_ERROR);
91
92         assertThatThrownBy(() -> {
93             new JpaPdpGroup(null, null, new ArrayList<>());
94         }).hasMessageMatching(NULL_KEY_ERROR);
95
96         assertNotNull(new JpaPdpGroup((new PfConceptKey())));
97         assertNotNull(new JpaPdpGroup((new JpaPdpGroup())));
98     }
99
100     @Test
101     public void testPdpGroupSet() {
102         PdpGroup testPdpGroup = new PdpGroup();
103         testPdpGroup.setName(PDP_GROUP0);
104         testPdpGroup.setPdpSubgroups(new ArrayList<>());
105
106         JpaPdpGroup testJpaPdpGroup = setUpSmallJpaPdpGroup();
107
108         assertEquals(PDP_GROUP0, testJpaPdpGroup.getKey().getName());
109
110         assertThatThrownBy(() -> {
111             testJpaPdpGroup.fromAuthorative(null);
112         }).hasMessageMatching("pdpGroup" + NULL_ERROR);
113
114         testJpaPdpGroup.setKey(new PfConceptKey(PDP_GROUP0, VERSION));
115         testJpaPdpGroup.fromAuthorative(testPdpGroup);
116
117         assertThatThrownBy(() -> new JpaPdpGroup((JpaPdpGroup) null)).isInstanceOf(NullPointerException.class);
118
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());
122
123         testJpaPdpGroup.clean();
124         assertEquals(PDP_GROUP0, testJpaPdpGroup.getKey().getName());
125     }
126
127     @Test
128     public void testPdpGroupValidation() {
129         JpaPdpGroup testJpaPdpGroup = setUpSmallJpaPdpGroup();
130
131         assertThatThrownBy(() -> {
132             testJpaPdpGroup.validate(null);
133         }).hasMessageMatching("fieldName" + NULL_ERROR);
134
135         assertFalse(testJpaPdpGroup.validate("").isValid());
136         testJpaPdpGroup.setPdpGroupState(PdpState.PASSIVE);
137         assertTrue(testJpaPdpGroup.validate("").isValid());
138
139         testJpaPdpGroup.setKey(PfConceptKey.getNullKey());
140         assertFalse(testJpaPdpGroup.validate("").isValid());
141         testJpaPdpGroup.setKey(new PfConceptKey("PdpGroup0", VERSION));
142         assertTrue(testJpaPdpGroup.validate("").isValid());
143
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());
150
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());
156
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());
162     }
163
164     @Test
165     public void testPdpSubgroups() {
166         JpaPdpGroup testJpaPdpGroup = setUpJpaPdpGroup();
167
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());
176
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()));
182
183         testJpaPdpGroup.getKey().setName("OtherName");
184         assertEquals(-1, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
185         testJpaPdpGroup.getKey().setName("PdpGroup0");
186         assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
187
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));
194
195         testJpaPdpGroup.setPdpGroupState(PdpState.ACTIVE);
196         assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
197         testJpaPdpGroup.setPdpGroupState(PdpState.PASSIVE);
198         assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
199
200         testJpaPdpGroup.setDescription("A Description");
201         assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
202         testJpaPdpGroup.setDescription(null);
203         assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
204
205         testJpaPdpGroup.getProperties().put("AnotherProperty", "Some String");
206         assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
207         testJpaPdpGroup.getProperties().remove("AnotherProperty");
208         assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
209
210         PdpGroup psg = testJpaPdpGroup.toAuthorative();
211         assertEquals(0, psg.getProperties().size());
212
213         testJpaPdpGroup.setProperties(new LinkedHashMap<>());
214         psg = testJpaPdpGroup.toAuthorative();
215         assertEquals(0, psg.getProperties().size());
216
217         testJpaPdpGroup.setProperties(null);
218         psg = testJpaPdpGroup.toAuthorative();
219         assertNull(psg.getProperties());
220         testJpaPdpGroup.setProperties(new LinkedHashMap<>());
221     }
222
223     @Test
224     public void testPdpGroupsProperties() {
225         JpaPdpGroup testJpaPdpGroup = setUpJpaPdpGroup();
226
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());
234
235         JpaPdpSubGroup anotherPdpSubgroup =
236                 new JpaPdpSubGroup(new PfReferenceKey(testJpaPdpGroup.getKey(), "AnotherPdpSubgroup"));
237
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());
243
244         assertEquals(testJpaPdpGroup, new JpaPdpGroup(testJpaPdpGroup));
245     }
246
247     private JpaPdpGroup setUpSmallJpaPdpGroup() {
248         PdpGroup testPdpGroup = new PdpGroup();
249         testPdpGroup.setName(PDP_GROUP0);
250         testPdpGroup.setPdpSubgroups(new ArrayList<>());
251         testPdpGroup.setVersion(VERSION);
252
253         JpaPdpGroup testJpaPdpGroup = new JpaPdpGroup();
254         testJpaPdpGroup.setKey(new PfConceptKey(PDP_GROUP0, VERSION));
255         testJpaPdpGroup.fromAuthorative(testPdpGroup);
256         testJpaPdpGroup.clean();
257
258         return testJpaPdpGroup;
259     }
260
261     private JpaPdpGroup setUpJpaPdpGroup() {
262         JpaPdpGroup testJpaPdpGroup = setUpSmallJpaPdpGroup();
263
264         testJpaPdpGroup.setKey(new PfConceptKey("PdpGroup0", VERSION));
265         testJpaPdpGroup.setDescription(null);
266         testJpaPdpGroup.setPdpGroupState(PdpState.PASSIVE);
267         testJpaPdpGroup.setProperties(new LinkedHashMap<>());
268         testJpaPdpGroup.clean();
269
270         return testJpaPdpGroup;
271     }
272 }