Changes for Checkstyle 8.32
[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 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
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.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;
42
43 /**
44  * Test the {@link JpaPdpGroupSubGroup} class.
45  *
46  * @author Liam Fallon (liam.fallon@est.tech)
47  */
48 public class JpaPdpGroupTest {
49
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";
54
55     @Test
56     public void testJpaPdpGroup() {
57         assertThatThrownBy(() -> {
58             new JpaPdpGroup((JpaPdpGroup) null);
59         }).hasMessageMatching("copyConcept" + NULL_ERROR);
60
61         assertThatThrownBy(() -> {
62             new JpaPdpGroup((PfConceptKey) null);
63         }).hasMessageMatching(NULL_KEY_ERROR);
64
65         assertThatThrownBy(() -> {
66             new JpaPdpGroup((PdpGroup) null);
67         }).hasMessageMatching("authorativeConcept" + NULL_ERROR);
68
69         assertThatThrownBy(() -> {
70             new JpaPdpGroup((JpaPdpGroup) null);
71         }).hasMessageMatching("copyConcept" + NULL_ERROR);
72
73         assertThatThrownBy(() -> {
74             new JpaPdpGroup(null, null, null);
75         }).hasMessageMatching(NULL_KEY_ERROR);
76
77         assertThatThrownBy(() -> {
78             new JpaPdpGroup(new PfConceptKey(), null, null);
79         }).hasMessageMatching("pdpGroupState" + NULL_ERROR);
80
81         assertThatThrownBy(() -> {
82             new JpaPdpGroup(new PfConceptKey(), PdpState.PASSIVE, null);
83         }).hasMessageMatching("pdpSubGroups" + NULL_ERROR);
84
85         assertThatThrownBy(() -> {
86             new JpaPdpGroup(null, PdpState.PASSIVE, null);
87         }).hasMessageMatching(NULL_KEY_ERROR);
88
89         assertThatThrownBy(() -> {
90             new JpaPdpGroup(null, PdpState.PASSIVE, new ArrayList<>());
91         }).hasMessageMatching(NULL_KEY_ERROR);
92
93         assertThatThrownBy(() -> {
94             new JpaPdpGroup(null, null, new ArrayList<>());
95         }).hasMessageMatching(NULL_KEY_ERROR);
96
97         assertNotNull(new JpaPdpGroup((new PfConceptKey())));
98         assertNotNull(new JpaPdpGroup((new JpaPdpGroup())));
99
100         PdpGroup testPdpGroup = new PdpGroup();
101         testPdpGroup.setName(PDP_GROUP0);
102         testPdpGroup.setPdpSubgroups(new ArrayList<>());
103         JpaPdpGroup testJpaPdpGroup = new JpaPdpGroup();
104         testJpaPdpGroup.setKey(null);
105
106         testJpaPdpGroup.setKey(new PfConceptKey());
107
108         testPdpGroup.setVersion(VERSION);
109         testJpaPdpGroup.fromAuthorative(testPdpGroup);
110
111         assertEquals(PDP_GROUP0, testJpaPdpGroup.getKey().getName());
112         testJpaPdpGroup.setKey(PfConceptKey.getNullKey());
113         testJpaPdpGroup.fromAuthorative(testPdpGroup);
114
115         assertThatThrownBy(() -> {
116             testJpaPdpGroup.fromAuthorative(null);
117         }).hasMessageMatching("pdpGroup" + NULL_ERROR);
118
119         testJpaPdpGroup.setKey(new PfConceptKey(PDP_GROUP0, VERSION));
120         testJpaPdpGroup.fromAuthorative(testPdpGroup);
121
122         assertThatThrownBy(() -> new JpaPdpGroup((JpaPdpGroup) null)).isInstanceOf(NullPointerException.class);
123
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());
127
128         testJpaPdpGroup.clean();
129         assertEquals(PDP_GROUP0, testJpaPdpGroup.getKey().getName());
130
131         assertThatThrownBy(() -> {
132             testJpaPdpGroup.validate(null);
133         }).hasMessageMatching("resultIn" + NULL_ERROR);
134
135         assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
136         testJpaPdpGroup.setPdpGroupState(PdpState.PASSIVE);
137         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
138
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());
143
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());
150
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());
156
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());
162
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());
171
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);
177
178         testJpaPdpGroup.getKey().setName("OtherName");
179         assertEquals(-1, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
180         testJpaPdpGroup.getKey().setName("PdpGroup0");
181         assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
182
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));
189
190         testJpaPdpGroup.setPdpGroupState(PdpState.ACTIVE);
191         assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
192         testJpaPdpGroup.setPdpGroupState(PdpState.PASSIVE);
193         assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
194
195         testJpaPdpGroup.setDescription("A Description");
196         assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
197         testJpaPdpGroup.setDescription(null);
198         assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
199
200         testJpaPdpGroup.getProperties().put("AnotherProperty", "Some String");
201         assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
202         testJpaPdpGroup.getProperties().remove("AnotherProperty");
203         assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
204
205         PdpGroup psg = testJpaPdpGroup.toAuthorative();
206         assertEquals(0, psg.getProperties().size());
207
208         testJpaPdpGroup.setProperties(new LinkedHashMap<>());
209         psg = testJpaPdpGroup.toAuthorative();
210         assertEquals(0, psg.getProperties().size());
211
212         testJpaPdpGroup.setProperties(null);
213         psg = testJpaPdpGroup.toAuthorative();
214         assertNull(psg.getProperties());
215         testJpaPdpGroup.setProperties(new LinkedHashMap<>());
216
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());
225
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());
231
232         assertEquals(testJpaPdpGroup, new JpaPdpGroup(testJpaPdpGroup));
233     }
234 }