Add unit test for PDP groups
[policy/models.git] / models-pdp / src / test / java / org / onap / policy / models / pdp / persistence / concepts / JpaPdpSubGroupTest.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.PdpSubGroup;
40 import org.onap.policy.models.pdp.testconcepts.DummyJpaPdpSubgroupChild;
41
42 /**
43  * Test the {@link JpaPdpSubGroupSubGroup} class.
44  *
45  * @author Liam Fallon (liam.fallon@est.tech)
46  */
47 public class JpaPdpSubGroupTest {
48
49     @Test
50     public void testJpaPdpSubGroup() {
51         assertThatThrownBy(() -> {
52             new JpaPdpSubGroup((JpaPdpSubGroup) null);
53         }).hasMessage("copyConcept is marked @NonNull but is null");
54
55         assertThatThrownBy(() -> {
56             new JpaPdpSubGroup((PfReferenceKey) null);
57         }).hasMessage("key is marked @NonNull but is null");
58
59         assertThatThrownBy(() -> {
60             new JpaPdpSubGroup((PdpSubGroup) null);
61         }).hasMessage("authorativeConcept is marked @NonNull but is null");
62
63         assertThatThrownBy(() -> {
64             new JpaPdpSubGroup(null, null, null, null);
65         }).hasMessage("key is marked @NonNull but is null");
66
67         assertThatThrownBy(() -> {
68             new JpaPdpSubGroup(new PfReferenceKey(), null, null, null);
69         }).hasMessage("supportedPolicyTypes is marked @NonNull but is null");
70
71         assertThatThrownBy(() -> {
72             new JpaPdpSubGroup(new PfReferenceKey(), new ArrayList<>(), null, null);
73         }).hasMessage("policies is marked @NonNull but is null");
74
75         assertThatThrownBy(() -> {
76             new JpaPdpSubGroup(null, new ArrayList<>(), null, null);
77         }).hasMessage("key is marked @NonNull but is null");
78
79         assertThatThrownBy(() -> {
80             new JpaPdpSubGroup(null, new ArrayList<>(), new ArrayList<>(), null);
81         }).hasMessage("key is marked @NonNull but is null");
82
83         assertThatThrownBy(() -> {
84             new JpaPdpSubGroup(null, null, new ArrayList<>(), null);
85         }).hasMessage("key is marked @NonNull but is null");
86
87         assertThatThrownBy(() -> {
88             new JpaPdpSubGroup(null, null, null, new ArrayList<>());
89         }).hasMessage("key is marked @NonNull but is null");
90
91         assertThatThrownBy(() -> {
92             new JpaPdpSubGroup(new PfReferenceKey(), null, null, new ArrayList<>());
93         }).hasMessage("supportedPolicyTypes is marked @NonNull but is null");
94
95         assertThatThrownBy(() -> {
96             new JpaPdpSubGroup(new PfReferenceKey(), new ArrayList<>(), null, new ArrayList<>());
97         }).hasMessage("policies is marked @NonNull but is null");
98
99         assertThatThrownBy(() -> {
100             new JpaPdpSubGroup(null, new ArrayList<>(), null, new ArrayList<>());
101         }).hasMessage("key is marked @NonNull but is null");
102
103         assertThatThrownBy(() -> {
104             new JpaPdpSubGroup(null, new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
105         }).hasMessage("key is marked @NonNull but is null");
106
107         assertThatThrownBy(() -> {
108             new JpaPdpSubGroup(null, null, new ArrayList<>(), null);
109         }).hasMessage("key is marked @NonNull but is null");
110
111         assertNotNull(new JpaPdpSubGroup((new PfReferenceKey())));
112
113         PdpSubGroup testPdpSubgroup = new PdpSubGroup();
114         testPdpSubgroup.setPdpType("PDP-A");
115         JpaPdpSubGroup testJpaPdpSubGroup = new JpaPdpSubGroup();
116         testJpaPdpSubGroup.setKey(null);
117         testJpaPdpSubGroup.fromAuthorative(testPdpSubgroup);
118         assertEquals("PDP-A", testJpaPdpSubGroup.getKey().getLocalName());
119         testJpaPdpSubGroup.setKey(PfReferenceKey.getNullKey());
120         testJpaPdpSubGroup.fromAuthorative(testPdpSubgroup);
121
122         assertThatThrownBy(() -> {
123             testJpaPdpSubGroup.fromAuthorative(null);
124         }).hasMessage("pdpSubgroup is marked @NonNull but is null");
125
126         assertThatThrownBy(() -> {
127             testJpaPdpSubGroup.copyTo(null);
128         }).hasMessage("target is marked @NonNull but is null");
129
130         assertEquals("PDP-A", testJpaPdpSubGroup.getKey().getLocalName());
131         assertEquals("PDP-A", new JpaPdpSubGroup(testPdpSubgroup).getKey().getLocalName());
132         assertEquals("PDP-A", ((PfReferenceKey) new JpaPdpSubGroup(testPdpSubgroup).getKeys().get(0)).getLocalName());
133
134         testJpaPdpSubGroup.clean();
135         assertEquals("PDP-A", testJpaPdpSubGroup.getKey().getLocalName());
136
137         assertThatThrownBy(() -> {
138             testJpaPdpSubGroup.validate(null);
139         }).hasMessage("resultIn is marked @NonNull but is null");
140
141         assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
142         assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).toString()
143                 .contains("INVALID:parent of key is a null key"));
144
145         testJpaPdpSubGroup.getKey().setParentConceptKey(new PfConceptKey("Parent:1.0.0"));
146         assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
147         assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).toString()
148                 .contains("INVALID:parent of key is a null key"));
149         assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).toString()
150                 .contains("INVALID:a PDP subgroup must support at least one policy type"));
151
152         testJpaPdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
153         testJpaPdpSubGroup.getSupportedPolicyTypes().add(new PfConceptKey("APolicyType:1.0.0"));
154         assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
155         assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).toString()
156                 .contains("INVALID:a PDP subgroup must support at least one policy type"));
157
158         PfReferenceKey savedKey = testJpaPdpSubGroup.getKey();
159         testJpaPdpSubGroup.setKey(PfReferenceKey.getNullKey());
160         assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
161         testJpaPdpSubGroup.setKey(savedKey);
162         assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
163
164         testJpaPdpSubGroup.setProperties(new LinkedHashMap<>());
165         testJpaPdpSubGroup.getProperties().put(null, null);
166         assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
167         testJpaPdpSubGroup.getProperties().remove(null);
168         assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
169
170         testJpaPdpSubGroup.setProperties(new LinkedHashMap<>());
171         testJpaPdpSubGroup.getProperties().put("NullKey", null);
172         assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
173         testJpaPdpSubGroup.getProperties().remove("NullKey");
174         assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
175
176         testJpaPdpSubGroup.setDesiredInstanceCount(-1);
177         assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
178         testJpaPdpSubGroup.setDesiredInstanceCount(0);
179         assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
180         testJpaPdpSubGroup.setProperties(null);
181
182         testJpaPdpSubGroup.setCurrentInstanceCount(-1);
183         assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
184         testJpaPdpSubGroup.setCurrentInstanceCount(0);
185         assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
186         testJpaPdpSubGroup.setProperties(null);
187         assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
188
189         List<PfConceptKey> supportedPolicyTypes = testJpaPdpSubGroup.getSupportedPolicyTypes();
190         assertNotNull(supportedPolicyTypes);
191         testJpaPdpSubGroup.setSupportedPolicyTypes(null);
192         assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
193         testJpaPdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
194         assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
195         testJpaPdpSubGroup.setSupportedPolicyTypes(supportedPolicyTypes);
196         assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
197
198         List<PfConceptKey> supportedPolicies = testJpaPdpSubGroup.getPolicies();
199         assertNotNull(supportedPolicies);
200         testJpaPdpSubGroup.setPolicies(null);
201         assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
202         testJpaPdpSubGroup.setPolicies(new ArrayList<>());
203         assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
204         testJpaPdpSubGroup.setPolicies(supportedPolicies);
205         assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
206
207         List<JpaPdp> pdpInstances = testJpaPdpSubGroup.getPdpInstances();
208         assertNotNull(pdpInstances);
209         testJpaPdpSubGroup.setPdpInstances(null);
210         assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
211         testJpaPdpSubGroup.setPdpInstances(new ArrayList<>());
212         assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
213         testJpaPdpSubGroup.setPdpInstances(pdpInstances);
214         assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk());
215
216         JpaPdpSubGroup otherJpaPdpSubGroup = new JpaPdpSubGroup(testJpaPdpSubGroup);
217         assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
218         assertEquals(-1, testJpaPdpSubGroup.compareTo(null));
219         assertEquals(0, testJpaPdpSubGroup.compareTo(testJpaPdpSubGroup));
220         assertFalse(testJpaPdpSubGroup.compareTo(new DummyJpaPdpSubgroupChild()) == 0);
221
222         testJpaPdpSubGroup.getKey().setParentKeyName("Parent1");
223         assertEquals(1, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
224         testJpaPdpSubGroup.getKey().setParentKeyName("Parent");
225         assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
226
227         testJpaPdpSubGroup.setCurrentInstanceCount(1);
228         assertEquals(1, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
229         testJpaPdpSubGroup.setCurrentInstanceCount(0);
230         assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
231
232         testJpaPdpSubGroup.setDesiredInstanceCount(1);
233         assertEquals(1, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
234         testJpaPdpSubGroup.setDesiredInstanceCount(0);
235         assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
236
237         PfConceptKey anotherPolicyType = new PfConceptKey("AnotherPolicyType", "1.0.0");
238         testJpaPdpSubGroup.getSupportedPolicyTypes().add(anotherPolicyType);
239         assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
240         testJpaPdpSubGroup.getSupportedPolicyTypes().remove(anotherPolicyType);
241         assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
242
243         PfConceptKey anotherPolicy = new PfConceptKey("AnotherPolicy", "1.0.0");
244         testJpaPdpSubGroup.getPolicies().add(anotherPolicy);
245         assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
246         testJpaPdpSubGroup.getPolicies().remove(anotherPolicy);
247         assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
248
249         JpaPdp anotherPdp = new JpaPdp(new PfReferenceKey(testJpaPdpSubGroup.getKey(), "AnotherPdp"));
250         testJpaPdpSubGroup.getPdpInstances().add(anotherPdp);
251         assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
252         testJpaPdpSubGroup.getPdpInstances().remove(anotherPdp);
253         assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
254
255         testJpaPdpSubGroup.setProperties(new LinkedHashMap<>());
256         testJpaPdpSubGroup.getProperties().put("AnotherProperty", "Some String");
257         assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
258         testJpaPdpSubGroup.getProperties().remove("AnotherProperty");
259         testJpaPdpSubGroup.setProperties(null);
260         assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup));
261
262         PdpSubGroup psg = testJpaPdpSubGroup.toAuthorative();
263         assertNull(psg.getProperties());
264
265         testJpaPdpSubGroup.setProperties(new LinkedHashMap<>());
266         psg = testJpaPdpSubGroup.toAuthorative();
267         assertEquals(0, psg.getProperties().size());
268
269         testJpaPdpSubGroup.getPolicies().add(new PfConceptKey("APolicy:1.0.0"));
270         testJpaPdpSubGroup.getPdpInstances().add(new JpaPdp());
271
272         testJpaPdpSubGroup.getProperties().put(" PropKey ", " Prop Value ");
273         testJpaPdpSubGroup.clean();
274         assertEquals("PropKey", testJpaPdpSubGroup.getProperties().keySet().iterator().next());
275         assertEquals("Prop Value", testJpaPdpSubGroup.getProperties().get("PropKey"));
276
277         assertEquals(4, testJpaPdpSubGroup.getKeys().size());
278     }
279 }