Add unit test for PDP groups
[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  * ================================================================================
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.pdp.persistence.concepts;
22
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;
30
31 import java.util.ArrayList;
32 import java.util.LinkedHashMap;
33 import java.util.List;
34
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     @Test
51     public void testJpaPdpGroup() {
52         assertThatThrownBy(() -> {
53             new JpaPdpGroup((JpaPdpGroup) null);
54         }).hasMessage("copyConcept is marked @NonNull but is null");
55
56         assertThatThrownBy(() -> {
57             new JpaPdpGroup((PfConceptKey) null);
58         }).hasMessage("key is marked @NonNull but is null");
59
60         assertThatThrownBy(() -> {
61             new JpaPdpGroup((PdpGroup) null);
62         }).hasMessage("authorativeConcept is marked @NonNull but is null");
63
64         assertThatThrownBy(() -> {
65             new JpaPdpGroup((JpaPdpGroup) null);
66         }).hasMessage("copyConcept is marked @NonNull but is null");
67
68         assertThatThrownBy(() -> {
69             new JpaPdpGroup(null, null, null);
70         }).hasMessage("key is marked @NonNull but is null");
71
72         assertThatThrownBy(() -> {
73             new JpaPdpGroup(new PfConceptKey(), null, null);
74         }).hasMessage("pdpGroupState is marked @NonNull but is null");
75
76         assertThatThrownBy(() -> {
77             new JpaPdpGroup(new PfConceptKey(), PdpState.PASSIVE, null);
78         }).hasMessage("pdpSubGroups is marked @NonNull but is null");
79
80         assertThatThrownBy(() -> {
81             new JpaPdpGroup(null, PdpState.PASSIVE, null);
82         }).hasMessage("key is marked @NonNull but is null");
83
84         assertThatThrownBy(() -> {
85             new JpaPdpGroup(null, PdpState.PASSIVE, new ArrayList<>());
86         }).hasMessage("key is marked @NonNull but is null");
87
88         assertThatThrownBy(() -> {
89             new JpaPdpGroup(null, null, new ArrayList<>());
90         }).hasMessage("key is marked @NonNull but is null");
91
92         assertNotNull(new JpaPdpGroup((new PfConceptKey())));
93         assertNotNull(new JpaPdpGroup((new JpaPdpGroup())));
94
95         PdpGroup testPdpGroup = new PdpGroup();
96         testPdpGroup.setName("PDPGroup0");
97         testPdpGroup.setPdpSubgroups(new ArrayList<>());
98         JpaPdpGroup testJpaPdpGroup = new JpaPdpGroup();
99         testJpaPdpGroup.setKey(null);
100
101         assertThatThrownBy(() -> {
102             testJpaPdpGroup.fromAuthorative(testPdpGroup);
103         }).hasMessage("version is marked @NonNull but is null");
104
105         testJpaPdpGroup.setKey(new PfConceptKey());
106
107         assertThatThrownBy(() -> {
108             testJpaPdpGroup.fromAuthorative(testPdpGroup);
109         }).hasMessage("version is marked @NonNull but is null");
110
111         testPdpGroup.setVersion("1.0.0");
112         testJpaPdpGroup.fromAuthorative(testPdpGroup);
113
114         assertEquals("PDPGroup0", testJpaPdpGroup.getKey().getName());
115         testJpaPdpGroup.setKey(PfConceptKey.getNullKey());
116         testJpaPdpGroup.fromAuthorative(testPdpGroup);
117
118         assertThatThrownBy(() -> {
119             testJpaPdpGroup.fromAuthorative(null);
120         }).hasMessage("pdpGroup is marked @NonNull but is null");
121
122         testJpaPdpGroup.setKey(new PfConceptKey("PDPGroup0", "1.0.0"));
123         testJpaPdpGroup.fromAuthorative(testPdpGroup);
124
125         assertThatThrownBy(() -> {
126             testJpaPdpGroup.copyTo(null);
127         }).hasMessage("target is marked @NonNull but is null");
128
129         assertEquals("PDPGroup0", testJpaPdpGroup.getKey().getName());
130         assertEquals("PDPGroup0", new JpaPdpGroup(testPdpGroup).getKey().getName());
131         assertEquals("PDPGroup0", ((PfConceptKey) new JpaPdpGroup(testPdpGroup).getKeys().get(0)).getName());
132
133         testJpaPdpGroup.clean();
134         assertEquals("PDPGroup0", testJpaPdpGroup.getKey().getName());
135
136         assertThatThrownBy(() -> {
137             testJpaPdpGroup.validate(null);
138         }).hasMessage("resultIn is marked @NonNull but is null");
139
140         assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
141         testJpaPdpGroup.setPdpGroupState(PdpState.PASSIVE);
142         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
143
144         testJpaPdpGroup.setKey(PfConceptKey.getNullKey());
145         assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
146         testJpaPdpGroup.setKey(new PfConceptKey("PdpGroup0", "1.0.0"));
147         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
148
149         testJpaPdpGroup.setDescription("   ");
150         assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
151         testJpaPdpGroup.setDescription("  A Description ");
152         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
153         testJpaPdpGroup.setDescription(null);
154         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
155
156         testJpaPdpGroup.setProperties(new LinkedHashMap<>());
157         testJpaPdpGroup.getProperties().put(null, null);
158         assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
159         testJpaPdpGroup.getProperties().remove(null);
160         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
161
162         testJpaPdpGroup.setProperties(new LinkedHashMap<>());
163         testJpaPdpGroup.getProperties().put("NullKey", null);
164         assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
165         testJpaPdpGroup.getProperties().remove("NullKey");
166         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
167
168         List<JpaPdpSubGroup> jpaPdpSubgroups = testJpaPdpGroup.getPdpSubGroups();
169         assertNotNull(jpaPdpSubgroups);
170         testJpaPdpGroup.setPdpSubGroups(null);
171         assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
172         testJpaPdpGroup.setPdpSubGroups(new ArrayList<>());
173         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
174         testJpaPdpGroup.setPdpSubGroups(jpaPdpSubgroups);
175         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
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         assertFalse(testJpaPdpGroup.compareTo(new DummyJpaPdpSubgroupChild()) == 0);
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         testJpaPdpGroup.clean();
223         testJpaPdpGroup.getProperties().put(" PropKey ", " Prop Value ");
224         testJpaPdpGroup.clean();
225         assertEquals("PropKey", testJpaPdpGroup.getProperties().keySet().iterator().next());
226         assertEquals("Prop Value", testJpaPdpGroup.getProperties().get("PropKey"));
227         testJpaPdpGroup.setDescription(" A Description ");
228         testJpaPdpGroup.clean();
229         assertEquals("A Description", testJpaPdpGroup.getDescription());
230
231         assertEquals(1, testJpaPdpGroup.getKeys().size());
232         testJpaPdpGroup.getPdpSubGroups().add(anotherPdpSubgroup);
233         assertEquals(2, testJpaPdpGroup.getKeys().size());
234         testJpaPdpGroup.clean();
235         assertEquals(2, testJpaPdpGroup.getKeys().size());
236     }
237 }