bd119159435a30a0c1bb5680b96de5d76c9418c5
[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  *  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.PdpGroup;
41 import org.onap.policy.models.pdp.enums.PdpState;
42 import org.onap.policy.models.pdp.testconcepts.DummyJpaPdpSubgroupChild;
43
44 /**
45  * Test the {@link JpaPdpGroupSubGroup} class.
46  *
47  * @author Liam Fallon (liam.fallon@est.tech)
48  */
49 public class JpaPdpGroupTest {
50
51     private static final String NULL_ERROR = " is marked .*ull but is null";
52     private static final String NULL_KEY_ERROR = "key" + NULL_ERROR;
53     private static final String PDP_GROUP0 = "PDPGroup0";
54     private static final String VERSION = "1.0.0";
55
56     @Test
57     public void testJpaPdpGroup() {
58         assertThatThrownBy(() -> {
59             new JpaPdpGroup((JpaPdpGroup) null);
60         }).hasMessageMatching("copyConcept" + NULL_ERROR);
61
62         assertThatThrownBy(() -> {
63             new JpaPdpGroup((PfConceptKey) null);
64         }).hasMessageMatching(NULL_KEY_ERROR);
65
66         assertThatThrownBy(() -> {
67             new JpaPdpGroup((PdpGroup) null);
68         }).hasMessageMatching("authorativeConcept" + NULL_ERROR);
69
70         assertThatThrownBy(() -> {
71             new JpaPdpGroup((JpaPdpGroup) null);
72         }).hasMessageMatching("copyConcept" + NULL_ERROR);
73
74         assertThatThrownBy(() -> {
75             new JpaPdpGroup(null, null, null);
76         }).hasMessageMatching(NULL_KEY_ERROR);
77
78         assertThatThrownBy(() -> {
79             new JpaPdpGroup(new PfConceptKey(), null, null);
80         }).hasMessageMatching("pdpGroupState" + NULL_ERROR);
81
82         assertThatThrownBy(() -> {
83             new JpaPdpGroup(new PfConceptKey(), PdpState.PASSIVE, null);
84         }).hasMessageMatching("pdpSubGroups" + NULL_ERROR);
85
86         assertThatThrownBy(() -> {
87             new JpaPdpGroup(null, PdpState.PASSIVE, null);
88         }).hasMessageMatching(NULL_KEY_ERROR);
89
90         assertThatThrownBy(() -> {
91             new JpaPdpGroup(null, PdpState.PASSIVE, new ArrayList<>());
92         }).hasMessageMatching(NULL_KEY_ERROR);
93
94         assertThatThrownBy(() -> {
95             new JpaPdpGroup(null, null, new ArrayList<>());
96         }).hasMessageMatching(NULL_KEY_ERROR);
97
98         assertNotNull(new JpaPdpGroup((new PfConceptKey())));
99         assertNotNull(new JpaPdpGroup((new JpaPdpGroup())));
100
101         PdpGroup testPdpGroup = new PdpGroup();
102         testPdpGroup.setName(PDP_GROUP0);
103         testPdpGroup.setPdpSubgroups(new ArrayList<>());
104         JpaPdpGroup testJpaPdpGroup = new JpaPdpGroup();
105         testJpaPdpGroup.setKey(null);
106
107         testJpaPdpGroup.setKey(new PfConceptKey());
108
109         testPdpGroup.setVersion(VERSION);
110         testJpaPdpGroup.fromAuthorative(testPdpGroup);
111
112         assertEquals(PDP_GROUP0, testJpaPdpGroup.getKey().getName());
113         testJpaPdpGroup.setKey(PfConceptKey.getNullKey());
114         testJpaPdpGroup.fromAuthorative(testPdpGroup);
115
116         assertThatThrownBy(() -> {
117             testJpaPdpGroup.fromAuthorative(null);
118         }).hasMessageMatching("pdpGroup" + NULL_ERROR);
119
120         testJpaPdpGroup.setKey(new PfConceptKey(PDP_GROUP0, VERSION));
121         testJpaPdpGroup.fromAuthorative(testPdpGroup);
122
123         assertThatThrownBy(() -> new JpaPdpGroup((JpaPdpGroup) null)).isInstanceOf(NullPointerException.class);
124
125         assertEquals(PDP_GROUP0, testJpaPdpGroup.getKey().getName());
126         assertEquals(PDP_GROUP0, new JpaPdpGroup(testPdpGroup).getKey().getName());
127         assertEquals(PDP_GROUP0, ((PfConceptKey) new JpaPdpGroup(testPdpGroup).getKeys().get(0)).getName());
128
129         testJpaPdpGroup.clean();
130         assertEquals(PDP_GROUP0, testJpaPdpGroup.getKey().getName());
131
132         assertThatThrownBy(() -> {
133             testJpaPdpGroup.validate(null);
134         }).hasMessageMatching("resultIn" + NULL_ERROR);
135
136         assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
137         testJpaPdpGroup.setPdpGroupState(PdpState.PASSIVE);
138         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
139
140         testJpaPdpGroup.setKey(PfConceptKey.getNullKey());
141         assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
142         testJpaPdpGroup.setKey(new PfConceptKey("PdpGroup0", VERSION));
143         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
144
145         testJpaPdpGroup.setDescription("   ");
146         assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
147         testJpaPdpGroup.setDescription("  A Description ");
148         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
149         testJpaPdpGroup.setDescription(null);
150         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
151
152         testJpaPdpGroup.setProperties(new LinkedHashMap<>());
153         testJpaPdpGroup.getProperties().put(null, null);
154         assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
155         testJpaPdpGroup.getProperties().remove(null);
156         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
157
158         testJpaPdpGroup.setProperties(new LinkedHashMap<>());
159         testJpaPdpGroup.getProperties().put("NullKey", null);
160         assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
161         testJpaPdpGroup.getProperties().remove("NullKey");
162         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
163
164         List<JpaPdpSubGroup> jpaPdpSubgroups = testJpaPdpGroup.getPdpSubGroups();
165         assertNotNull(jpaPdpSubgroups);
166         testJpaPdpGroup.setPdpSubGroups(null);
167         assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
168         testJpaPdpGroup.setPdpSubGroups(new ArrayList<>());
169         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
170         testJpaPdpGroup.setPdpSubGroups(jpaPdpSubgroups);
171         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
172
173         JpaPdpGroup otherJpaPdpGroup = new JpaPdpGroup(testJpaPdpGroup);
174         assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
175         assertEquals(-1, testJpaPdpGroup.compareTo(null));
176         assertEquals(0, testJpaPdpGroup.compareTo(testJpaPdpGroup));
177         assertFalse(testJpaPdpGroup.compareTo(new DummyJpaPdpSubgroupChild()) == 0);
178
179         testJpaPdpGroup.getKey().setName("OtherName");
180         assertEquals(-1, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
181         testJpaPdpGroup.getKey().setName("PdpGroup0");
182         assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
183
184         JpaPdpSubGroup anotherPdpSubgroup =
185                 new JpaPdpSubGroup(new PfReferenceKey(testJpaPdpGroup.getKey(), "AnotherPdpSubgroup"));
186         testJpaPdpGroup.getPdpSubGroups().add(anotherPdpSubgroup);
187         assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
188         testJpaPdpGroup.getPdpSubGroups().remove(anotherPdpSubgroup);
189         assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
190
191         testJpaPdpGroup.setPdpGroupState(PdpState.ACTIVE);
192         assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
193         testJpaPdpGroup.setPdpGroupState(PdpState.PASSIVE);
194         assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
195
196         testJpaPdpGroup.setDescription("A Description");
197         assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
198         testJpaPdpGroup.setDescription(null);
199         assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
200
201         testJpaPdpGroup.getProperties().put("AnotherProperty", "Some String");
202         assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
203         testJpaPdpGroup.getProperties().remove("AnotherProperty");
204         assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
205
206         PdpGroup psg = testJpaPdpGroup.toAuthorative();
207         assertEquals(0, psg.getProperties().size());
208
209         testJpaPdpGroup.setProperties(new LinkedHashMap<>());
210         psg = testJpaPdpGroup.toAuthorative();
211         assertEquals(0, psg.getProperties().size());
212
213         testJpaPdpGroup.setProperties(null);
214         psg = testJpaPdpGroup.toAuthorative();
215         assertNull(psg.getProperties());
216         testJpaPdpGroup.setProperties(new LinkedHashMap<>());
217
218         testJpaPdpGroup.clean();
219         testJpaPdpGroup.getProperties().put(" PropKey ", " Prop Value ");
220         testJpaPdpGroup.clean();
221         assertEquals("PropKey", testJpaPdpGroup.getProperties().keySet().iterator().next());
222         assertEquals("Prop Value", testJpaPdpGroup.getProperties().get("PropKey"));
223         testJpaPdpGroup.setDescription(" A Description ");
224         testJpaPdpGroup.clean();
225         assertEquals("A Description", testJpaPdpGroup.getDescription());
226
227         assertEquals(1, testJpaPdpGroup.getKeys().size());
228         testJpaPdpGroup.getPdpSubGroups().add(anotherPdpSubgroup);
229         assertEquals(2, testJpaPdpGroup.getKeys().size());
230         testJpaPdpGroup.clean();
231         assertEquals(2, testJpaPdpGroup.getKeys().size());
232
233         assertEquals(testJpaPdpGroup, new JpaPdpGroup(testJpaPdpGroup));
234     }
235 }