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