Remove version from PdpGroup
[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  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.pdp.persistence.concepts;
22
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30
31 import java.util.ArrayList;
32 import java.util.LinkedHashMap;
33 import java.util.List;
34
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.base.PfValidationResult;
39 import org.onap.policy.models.pdp.concepts.PdpGroup;
40 import org.onap.policy.models.pdp.enums.PdpState;
41 import org.onap.policy.models.pdp.testconcepts.DummyJpaPdpSubgroupChild;
42
43 /**
44  * Test the {@link JpaPdpGroupSubGroup} class.
45  *
46  * @author Liam Fallon (liam.fallon@est.tech)
47  */
48 public class JpaPdpGroupTest {
49
50     @Test
51     public void testJpaPdpGroup() {
52         assertThatThrownBy(() -> {
53             new JpaPdpGroup((JpaPdpGroup) null);
54         }).hasMessage("copyConcept is marked @NonNull but is null");
55
56         assertThatThrownBy(() -> {
57             new JpaPdpGroup((PfConceptKey) null);
58         }).hasMessage("key is marked @NonNull but is null");
59
60         assertThatThrownBy(() -> {
61             new JpaPdpGroup((PdpGroup) null);
62         }).hasMessage("authorativeConcept is marked @NonNull but is null");
63
64         assertThatThrownBy(() -> {
65             new JpaPdpGroup((JpaPdpGroup) null);
66         }).hasMessage("copyConcept is marked @NonNull but is null");
67
68         assertThatThrownBy(() -> {
69             new JpaPdpGroup(null, null, null);
70         }).hasMessage("key is marked @NonNull but is null");
71
72         assertThatThrownBy(() -> {
73             new JpaPdpGroup(new PfConceptKey(), null, null);
74         }).hasMessage("pdpGroupState is marked @NonNull but is null");
75
76         assertThatThrownBy(() -> {
77             new JpaPdpGroup(new PfConceptKey(), PdpState.PASSIVE, null);
78         }).hasMessage("pdpSubGroups is marked @NonNull but is null");
79
80         assertThatThrownBy(() -> {
81             new JpaPdpGroup(null, PdpState.PASSIVE, null);
82         }).hasMessage("key is marked @NonNull but is null");
83
84         assertThatThrownBy(() -> {
85             new JpaPdpGroup(null, PdpState.PASSIVE, new ArrayList<>());
86         }).hasMessage("key is marked @NonNull but is null");
87
88         assertThatThrownBy(() -> {
89             new JpaPdpGroup(null, null, new ArrayList<>());
90         }).hasMessage("key is marked @NonNull but is null");
91
92         assertNotNull(new JpaPdpGroup((new PfConceptKey())));
93         assertNotNull(new JpaPdpGroup((new JpaPdpGroup())));
94
95         PdpGroup testPdpGroup = new PdpGroup();
96         testPdpGroup.setName("PDPGroup0");
97         testPdpGroup.setPdpSubgroups(new ArrayList<>());
98         JpaPdpGroup testJpaPdpGroup = new JpaPdpGroup();
99         testJpaPdpGroup.setKey(null);
100
101         testJpaPdpGroup.setKey(new PfConceptKey());
102
103         testPdpGroup.setVersion("1.0.0");
104         testJpaPdpGroup.fromAuthorative(testPdpGroup);
105
106         assertEquals("PDPGroup0", testJpaPdpGroup.getKey().getName());
107         testJpaPdpGroup.setKey(PfConceptKey.getNullKey());
108         testJpaPdpGroup.fromAuthorative(testPdpGroup);
109
110         assertThatThrownBy(() -> {
111             testJpaPdpGroup.fromAuthorative(null);
112         }).hasMessage("pdpGroup is marked @NonNull but is null");
113
114         testJpaPdpGroup.setKey(new PfConceptKey("PDPGroup0", "1.0.0"));
115         testJpaPdpGroup.fromAuthorative(testPdpGroup);
116
117         assertThatThrownBy(() -> {
118             testJpaPdpGroup.copyTo(null);
119         }).hasMessage("target is marked @NonNull but is null");
120
121         assertEquals("PDPGroup0", testJpaPdpGroup.getKey().getName());
122         assertEquals("PDPGroup0", new JpaPdpGroup(testPdpGroup).getKey().getName());
123         assertEquals("PDPGroup0", ((PfConceptKey) new JpaPdpGroup(testPdpGroup).getKeys().get(0)).getName());
124
125         testJpaPdpGroup.clean();
126         assertEquals("PDPGroup0", testJpaPdpGroup.getKey().getName());
127
128         assertThatThrownBy(() -> {
129             testJpaPdpGroup.validate(null);
130         }).hasMessage("resultIn is marked @NonNull but is null");
131
132         assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
133         testJpaPdpGroup.setPdpGroupState(PdpState.PASSIVE);
134         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
135
136         testJpaPdpGroup.setKey(PfConceptKey.getNullKey());
137         assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
138         testJpaPdpGroup.setKey(new PfConceptKey("PdpGroup0", "1.0.0"));
139         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
140
141         testJpaPdpGroup.setDescription("   ");
142         assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
143         testJpaPdpGroup.setDescription("  A Description ");
144         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
145         testJpaPdpGroup.setDescription(null);
146         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
147
148         testJpaPdpGroup.setProperties(new LinkedHashMap<>());
149         testJpaPdpGroup.getProperties().put(null, null);
150         assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
151         testJpaPdpGroup.getProperties().remove(null);
152         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
153
154         testJpaPdpGroup.setProperties(new LinkedHashMap<>());
155         testJpaPdpGroup.getProperties().put("NullKey", null);
156         assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
157         testJpaPdpGroup.getProperties().remove("NullKey");
158         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
159
160         List<JpaPdpSubGroup> jpaPdpSubgroups = testJpaPdpGroup.getPdpSubGroups();
161         assertNotNull(jpaPdpSubgroups);
162         testJpaPdpGroup.setPdpSubGroups(null);
163         assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
164         testJpaPdpGroup.setPdpSubGroups(new ArrayList<>());
165         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
166         testJpaPdpGroup.setPdpSubGroups(jpaPdpSubgroups);
167         assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk());
168
169         JpaPdpGroup otherJpaPdpGroup = new JpaPdpGroup(testJpaPdpGroup);
170         assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
171         assertEquals(-1, testJpaPdpGroup.compareTo(null));
172         assertEquals(0, testJpaPdpGroup.compareTo(testJpaPdpGroup));
173         assertFalse(testJpaPdpGroup.compareTo(new DummyJpaPdpSubgroupChild()) == 0);
174
175         testJpaPdpGroup.getKey().setName("OtherName");
176         assertEquals(-1, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
177         testJpaPdpGroup.getKey().setName("PdpGroup0");
178         assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
179
180         JpaPdpSubGroup anotherPdpSubgroup =
181                 new JpaPdpSubGroup(new PfReferenceKey(testJpaPdpGroup.getKey(), "AnotherPdpSubgroup"));
182         testJpaPdpGroup.getPdpSubGroups().add(anotherPdpSubgroup);
183         assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
184         testJpaPdpGroup.getPdpSubGroups().remove(anotherPdpSubgroup);
185         assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
186
187         testJpaPdpGroup.setPdpGroupState(PdpState.ACTIVE);
188         assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
189         testJpaPdpGroup.setPdpGroupState(PdpState.PASSIVE);
190         assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
191
192         testJpaPdpGroup.setDescription("A Description");
193         assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
194         testJpaPdpGroup.setDescription(null);
195         assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
196
197         testJpaPdpGroup.getProperties().put("AnotherProperty", "Some String");
198         assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
199         testJpaPdpGroup.getProperties().remove("AnotherProperty");
200         assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup));
201
202         PdpGroup psg = testJpaPdpGroup.toAuthorative();
203         assertEquals(0, psg.getProperties().size());
204
205         testJpaPdpGroup.setProperties(new LinkedHashMap<>());
206         psg = testJpaPdpGroup.toAuthorative();
207         assertEquals(0, psg.getProperties().size());
208
209         testJpaPdpGroup.setProperties(null);
210         psg = testJpaPdpGroup.toAuthorative();
211         assertNull(psg.getProperties());
212         testJpaPdpGroup.setProperties(new LinkedHashMap<>());
213
214         testJpaPdpGroup.clean();
215         testJpaPdpGroup.getProperties().put(" PropKey ", " Prop Value ");
216         testJpaPdpGroup.clean();
217         assertEquals("PropKey", testJpaPdpGroup.getProperties().keySet().iterator().next());
218         assertEquals("Prop Value", testJpaPdpGroup.getProperties().get("PropKey"));
219         testJpaPdpGroup.setDescription(" A Description ");
220         testJpaPdpGroup.clean();
221         assertEquals("A Description", testJpaPdpGroup.getDescription());
222
223         assertEquals(1, testJpaPdpGroup.getKeys().size());
224         testJpaPdpGroup.getPdpSubGroups().add(anotherPdpSubgroup);
225         assertEquals(2, testJpaPdpGroup.getKeys().size());
226         testJpaPdpGroup.clean();
227         assertEquals(2, testJpaPdpGroup.getKeys().size());
228     }
229 }