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