969b59c6dd9fcf2a17b4fe3f689c4482db2cec6c
[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-2020 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.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotEquals;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertNull;
31 import static org.junit.Assert.assertTrue;
32
33 import java.util.ArrayList;
34 import java.util.LinkedHashMap;
35 import java.util.List;
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.Validated;
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 .*ull 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         }).hasMessageMatching("copyConcept is marked .*ull but is null");
59
60         assertThatThrownBy(() -> {
61             new JpaPdpSubGroup((PfReferenceKey) null);
62         }).hasMessageMatching(NULL_KEY_ERROR);
63
64         assertThatThrownBy(() -> {
65             new JpaPdpSubGroup((PdpSubGroup) null);
66         }).hasMessageMatching("authorativeConcept is marked .*ull but is null");
67
68         assertThatThrownBy(() -> {
69             new JpaPdpSubGroup(null, null, null, null);
70         }).hasMessageMatching(NULL_KEY_ERROR);
71
72         assertThatThrownBy(() -> {
73             new JpaPdpSubGroup(new PfReferenceKey(), null, null, null);
74         }).hasMessageMatching("supportedPolicyTypes is marked .*ull but is null");
75
76         assertThatThrownBy(() -> {
77             new JpaPdpSubGroup(new PfReferenceKey(), new ArrayList<>(), null, null);
78         }).hasMessageMatching("policies is marked .*ull but is null");
79
80         assertThatThrownBy(() -> {
81             new JpaPdpSubGroup(null, new ArrayList<>(), null, null);
82         }).hasMessageMatching(NULL_KEY_ERROR);
83
84         assertThatThrownBy(() -> {
85             new JpaPdpSubGroup(null, new ArrayList<>(), new ArrayList<>(), null);
86         }).hasMessageMatching(NULL_KEY_ERROR);
87
88         assertThatThrownBy(() -> {
89             new JpaPdpSubGroup(null, null, new ArrayList<>(), null);
90         }).hasMessageMatching(NULL_KEY_ERROR);
91
92         assertThatThrownBy(() -> {
93             new JpaPdpSubGroup(null, null, null, new ArrayList<>());
94         }).hasMessageMatching(NULL_KEY_ERROR);
95
96         assertThatThrownBy(() -> {
97             new JpaPdpSubGroup(new PfReferenceKey(), null, null, new ArrayList<>());
98         }).hasMessageMatching("supportedPolicyTypes is marked .*ull but is null");
99
100         assertThatThrownBy(() -> {
101             new JpaPdpSubGroup(new PfReferenceKey(), new ArrayList<>(), null, new ArrayList<>());
102         }).hasMessageMatching("policies is marked .*ull but is null");
103
104         assertThatThrownBy(() -> {
105             new JpaPdpSubGroup(null, new ArrayList<>(), null, new ArrayList<>());
106         }).hasMessageMatching(NULL_KEY_ERROR);
107
108         assertThatThrownBy(() -> {
109             new JpaPdpSubGroup(null, new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
110         }).hasMessageMatching(NULL_KEY_ERROR);
111
112         assertThatThrownBy(() -> {
113             new JpaPdpSubGroup(null, null, new ArrayList<>(), null);
114         }).hasMessageMatching(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         }).hasMessageMatching("pdpSubgroup is marked .*ull 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         }).hasMessageMatching("fieldName is marked .*ull but is null");
143
144         assertFalse(testJpaPdpSubGroup.validate("").isValid());
145         assertThat(testJpaPdpSubGroup.validate("").getResult())
146                 .contains("parent of key").contains(Validated.IS_A_NULL_KEY);
147
148         testJpaPdpSubGroup.getKey().setParentConceptKey(new PfConceptKey("Parent:1.0.0"));
149         assertFalse(testJpaPdpSubGroup.validate("").isValid());
150         assertThat(testJpaPdpSubGroup.validate("").getResult())
151                 .doesNotContain("parent of key")
152                 .contains("supportedPolicyTypes").contains("is empty");
153
154         testJpaPdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
155         testJpaPdpSubGroup.getSupportedPolicyTypes().add(new PfSearchableKey("APolicyType:1.0.0"));
156         assertTrue(testJpaPdpSubGroup.validate("").isValid());
157
158         PfReferenceKey savedKey = testJpaPdpSubGroup.getKey();
159         testJpaPdpSubGroup.setKey(PfReferenceKey.getNullKey());
160         assertFalse(testJpaPdpSubGroup.validate("").isValid());
161         testJpaPdpSubGroup.setKey(savedKey);
162         assertTrue(testJpaPdpSubGroup.validate("").isValid());
163
164         testJpaPdpSubGroup.setProperties(new LinkedHashMap<>());
165         testJpaPdpSubGroup.getProperties().put(null, null);
166         assertFalse(testJpaPdpSubGroup.validate("").isValid());
167         testJpaPdpSubGroup.getProperties().remove(null);
168         assertTrue(testJpaPdpSubGroup.validate("").isValid());
169
170         testJpaPdpSubGroup.setProperties(new LinkedHashMap<>());
171         testJpaPdpSubGroup.getProperties().put("NullKey", null);
172         assertFalse(testJpaPdpSubGroup.validate("").isValid());
173         testJpaPdpSubGroup.getProperties().remove("NullKey");
174         assertTrue(testJpaPdpSubGroup.validate("").isValid());
175
176         testJpaPdpSubGroup.setDesiredInstanceCount(-1);
177         assertFalse(testJpaPdpSubGroup.validate("").isValid());
178         testJpaPdpSubGroup.setDesiredInstanceCount(0);
179         assertTrue(testJpaPdpSubGroup.validate("").isValid());
180         testJpaPdpSubGroup.setProperties(null);
181
182         testJpaPdpSubGroup.setCurrentInstanceCount(-1);
183         assertFalse(testJpaPdpSubGroup.validate("").isValid());
184         testJpaPdpSubGroup.setCurrentInstanceCount(0);
185         assertTrue(testJpaPdpSubGroup.validate("").isValid());
186         testJpaPdpSubGroup.setProperties(null);
187         assertTrue(testJpaPdpSubGroup.validate("").isValid());
188
189         List<PfSearchableKey> supportedPolicyTypes = testJpaPdpSubGroup.getSupportedPolicyTypes();
190         assertNotNull(supportedPolicyTypes);
191         testJpaPdpSubGroup.setSupportedPolicyTypes(null);
192         assertFalse(testJpaPdpSubGroup.validate("").isValid());
193         testJpaPdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
194         assertFalse(testJpaPdpSubGroup.validate("").isValid());
195         testJpaPdpSubGroup.setSupportedPolicyTypes(supportedPolicyTypes);
196         assertTrue(testJpaPdpSubGroup.validate("").isValid());
197
198         List<PfConceptKey> supportedPolicies = testJpaPdpSubGroup.getPolicies();
199         assertNotNull(supportedPolicies);
200         testJpaPdpSubGroup.setPolicies(null);
201         assertFalse(testJpaPdpSubGroup.validate("").isValid());
202         testJpaPdpSubGroup.setPolicies(new ArrayList<>());
203         assertTrue(testJpaPdpSubGroup.validate("").isValid());
204         testJpaPdpSubGroup.setPolicies(supportedPolicies);
205         assertTrue(testJpaPdpSubGroup.validate("").isValid());
206
207         List<JpaPdp> pdpInstances = testJpaPdpSubGroup.getPdpInstances();
208         assertNotNull(pdpInstances);
209         testJpaPdpSubGroup.setPdpInstances(null);
210         assertFalse(testJpaPdpSubGroup.validate("").isValid());
211         testJpaPdpSubGroup.setPdpInstances(new ArrayList<>());
212         assertTrue(testJpaPdpSubGroup.validate("").isValid());
213         testJpaPdpSubGroup.setPdpInstances(pdpInstances);
214         assertTrue(testJpaPdpSubGroup.validate("").isValid());
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         assertNotEquals(0, testJpaPdpSubGroup.compareTo(new DummyJpaPdpSubgroupChild()));
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         PfSearchableKey anotherPolicyType = new PfSearchableKey("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         assertEquals(testJpaPdpSubGroup, new JpaPdpSubGroup(testJpaPdpSubGroup));
280     }
281 }