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