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