Allow semantic versioning in all templates in models
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfKeyImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021,2023 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.base;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertFalse;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertTrue;
31
32 import java.lang.reflect.Field;
33 import lombok.EqualsAndHashCode;
34 import lombok.Getter;
35 import lombok.NoArgsConstructor;
36 import lombok.Setter;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.onap.policy.common.parameters.ValidationResult;
40 import org.onap.policy.common.parameters.annotations.Pattern;
41 import org.onap.policy.models.base.PfKey.Compatibility;
42 import org.onap.policy.models.base.testconcepts.DummyPfKey;
43
44 public class PfKeyImplTest {
45
46     private static final String OTHER_IS_NULL = "^otherKey is marked .*on.*ull but is null$";
47     private static final String ID_IS_NULL = "^id is marked .*on.*ull but is null$";
48     private static final String VERSION123 = "1.2.3";
49     private static final String VERSION100 = "1.0.0";
50     private static final String VERSION001 = "0.0.1";
51     private static final String NAME = "name";
52     private static MyKey someKey;
53     private static MyKey someKey0;
54     private static MyKey someKey1;
55     private static MyKey someKey2;
56     private static MyKey someKey3;
57     private static MyKey someKey4;
58     private static MyKey someKey4a;
59     private static MyKey someKey5;
60     private static MyKey someKey6;
61
62     private static final MyKey buildKey1 = new MyKey(NAME, "0.0.3+1");
63     private static final MyKey buildKey2 = new MyKey(NAME, "0.1.0-1");
64     private static final MyKey buildKey3 = new MyKey(NAME, "3.0.0-SNAPSHOT");
65     private static final MyKey buildKey4 = new MyKey(NAME, "1.0.0-rc.1");
66
67     /**
68      * Sets data in Keys for the tests.
69      */
70     @BeforeClass
71     public static void setUp() {
72         someKey = new MyKey();
73
74         someKey0 = new MyKey();
75         someKey1 = new MyKey(NAME, VERSION001);
76         someKey2 = new MyKey(someKey1);
77         someKey3 = new MyKey(someKey1.getId());
78
79         someKey0.setName("zero");
80         someKey0.setVersion("0.0.2");
81         someKey3.setVersion("0.0.2");
82
83         someKey4 = new MyKey(someKey1);
84         someKey4.setVersion("0.1.2");
85
86         someKey4a = new MyKey(someKey1);
87         someKey4a.setVersion("0.0.0");
88
89         someKey5 = new MyKey(someKey1);
90         someKey5.setVersion("1.2.2");
91
92         someKey6 = new MyKey(someKey1);
93         someKey6.setVersion("3.0.0");
94     }
95
96     @Test
97     public void testConceptKey() {
98         assertThatIllegalArgumentException().isThrownBy(() -> new MyKey("some bad key id"))
99             .withMessage("parameter \"id\": value \"some bad key id\", " + "does not match regular expression \""
100                 + PfKey.KEY_ID_REGEXP + "\"");
101
102         assertThatThrownBy(() -> new MyKey((MyKey) null))
103             .hasMessageMatching("^copyConcept is marked .*on.*ull but is null$");
104
105         assertTrue(someKey.isNullKey());
106         assertEquals(new MyKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION), someKey);
107
108         MyKey someKey11 = new MyKey(NAME, VERSION001);
109         MyKey someKey22 = new MyKey(someKey11);
110         MyKey someKey33 = new MyKey(someKey11.getId());
111         assertEquals(someKey11, someKey22);
112         assertEquals(someKey11, someKey33);
113         assertFalse(someKey11.isNullKey());
114         assertFalse(someKey11.isNullVersion());
115
116         assertEquals(someKey22, someKey11.getKey());
117         assertEquals(1, someKey11.getKeys().size());
118     }
119
120     @Test
121     public void testCompatibilityConceptKey() {
122         assertEquals("name:0.1.2", someKey4.getId());
123
124         assertThatThrownBy(() -> someKey0.getCompatibility(null)).isInstanceOf(NullPointerException.class)
125             .hasMessageMatching("^otherKey is marked .*on.*ull but is null$");
126
127         assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(new DummyPfKey()));
128         assertEquals(Compatibility.DIFFERENT, buildKey1.getCompatibility(new DummyPfKey()));
129         assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(someKey1));
130         assertEquals(Compatibility.IDENTICAL, someKey2.getCompatibility(someKey1));
131         assertEquals(Compatibility.IDENTICAL, buildKey1.getCompatibility(new MyKey(buildKey1)));
132         assertEquals(Compatibility.PATCH, someKey3.getCompatibility(someKey1));
133         assertEquals(Compatibility.PATCH, buildKey1.getCompatibility(someKey1));
134         assertEquals(Compatibility.MINOR, someKey4.getCompatibility(someKey1));
135         assertEquals(Compatibility.MINOR, buildKey2.getCompatibility(buildKey1));
136         assertEquals(Compatibility.PATCH, someKey4a.getCompatibility(someKey1));
137         assertEquals(Compatibility.PATCH, someKey1.getCompatibility(someKey4a));
138         assertEquals(Compatibility.MAJOR, someKey5.getCompatibility(someKey1));
139         assertEquals(Compatibility.MAJOR, someKey6.getCompatibility(someKey1));
140         assertEquals(Compatibility.MAJOR, buildKey3.getCompatibility(someKey1));
141
142         assertTrue(someKey1.isCompatible(someKey2));
143         assertTrue(someKey1.isCompatible(someKey3));
144         assertTrue(someKey1.isCompatible(someKey4));
145         assertFalse(someKey1.isCompatible(someKey0));
146         assertFalse(someKey1.isCompatible(someKey5));
147         assertFalse(someKey1.isCompatible(buildKey3));
148         assertFalse(someKey1.isCompatible(buildKey4));
149         assertFalse(someKey1.isCompatible(new DummyPfKey()));
150     }
151
152     @Test
153     public void testValidityConceptKey() {
154         assertTrue(someKey0.validate("").isValid());
155         assertTrue(someKey1.validate("").isValid());
156         assertTrue(someKey2.validate("").isValid());
157         assertTrue(someKey3.validate("").isValid());
158         assertTrue(someKey4.validate("").isValid());
159         assertTrue(someKey5.validate("").isValid());
160         assertTrue(someKey6.validate("").isValid());
161         assertTrue(buildKey1.validate("").isValid());
162         assertTrue(buildKey2.validate("").isValid());
163         assertTrue(buildKey3.validate("").isValid());
164         assertTrue(buildKey4.validate("").isValid());
165     }
166
167     @Test
168     public void testCleanConceptKey() {
169         someKey0.clean();
170         assertNotNull(someKey0.toString());
171
172         MyKey someKey7 = new MyKey(someKey1);
173         assertEquals(244799191, someKey7.hashCode());
174         assertEquals(0, someKey7.compareTo(someKey1));
175         assertEquals(-12, someKey7.compareTo(someKey0));
176
177         assertThatThrownBy(() -> someKey0.compareTo(null)).isInstanceOf(NullPointerException.class)
178             .hasMessageMatching("^otherObj is marked .*on.*ull but is null$");
179
180         assertEquals(0, someKey0.compareTo(someKey0));
181         assertEquals(-36, someKey0.compareTo(new DummyPfKey()));
182
183         MyKey someKey8 = new MyKey();
184         someKey8.setVersion(VERSION001);
185         assertFalse(someKey8.isNullKey());
186
187         someKey8.setVersion("10");
188         assertEquals(0, someKey8.getMinorVersion());
189
190         someKey8.setVersion("10.11");
191         assertEquals(0, someKey8.getPatchVersion());
192     }
193
194     @Test
195     public void testNullArguments() {
196         assertThatThrownBy(() -> new MyKey((String) null)).hasMessageMatching(ID_IS_NULL);
197
198         assertThatThrownBy(() -> new MyKey((MyKey) null))
199             .hasMessageMatching("^copyConcept is marked .*on.*ull but is null$");
200
201         assertThatThrownBy(() -> new MyKey(null, null)).hasMessageMatching("name is marked .*on.*ull but is null$");
202
203         assertThatThrownBy(() -> new MyKey(NAME, null))
204             .hasMessageMatching("^version is marked .*on.*ull but is null$");
205
206         assertThatThrownBy(() -> new MyKey(null, VERSION001))
207             .hasMessageMatching("^name is marked .*on.*ull but is null$");
208
209         assertThatThrownBy(() -> new MyKey("AKey", VERSION001).isCompatible(null)).hasMessageMatching(OTHER_IS_NULL);
210     }
211
212     @Test
213     public void testValidation() throws Exception {
214         MyKey testKey = new MyKey("TheKey", VERSION001);
215         assertEquals("TheKey:0.0.1", testKey.getId());
216
217         Field nameField = testKey.getClass().getDeclaredField(NAME);
218         nameField.setAccessible(true);
219         nameField.set(testKey, "Key Name");
220         ValidationResult validationResult = testKey.validate("");
221         nameField.set(testKey, "TheKey");
222         nameField.setAccessible(false);
223         assertThat(validationResult.getResult()).contains("\"name\"").doesNotContain("\"version\"")
224             .contains("does not match regular expression " + PfKey.NAME_REGEXP);
225
226         Field versionField = testKey.getClass().getDeclaredField("version");
227         versionField.setAccessible(true);
228         versionField.set(testKey, "Key Version");
229         ValidationResult validationResult2 = testKey.validate("");
230         versionField.set(testKey, VERSION001);
231         versionField.setAccessible(false);
232         assertThat(validationResult2.getResult()).doesNotContain("\"name\"").contains("\"version\"")
233             .contains("does not match regular expression " + PfKey.VERSION_REGEXP);
234     }
235
236     @Test
237     public void testkeynewerThan() {
238         MyKey key1 = new MyKey("Key1", VERSION123);
239
240         assertThatThrownBy(() -> key1.isNewerThan(null)).hasMessageMatching(OTHER_IS_NULL);
241
242         assertThatThrownBy(() -> key1.isNewerThan(new PfReferenceKey())).hasMessage(
243             "org.onap.policy.models.base.PfReferenceKey is not " + "an instance of " + PfKeyImpl.class.getName());
244
245         assertFalse(key1.isNewerThan(key1));
246
247         MyKey key1a = new MyKey("Key1a", VERSION123);
248         assertFalse(key1.isNewerThan(key1a));
249
250         MyKey key1b = new MyKey("Key0", VERSION123);
251         assertTrue(key1.isNewerThan(key1b));
252
253         key1a.setName("Key1");
254         assertFalse(key1.isNewerThan(key1a));
255
256         key1a.setVersion("0.2.3");
257         assertTrue(key1.isNewerThan(key1a));
258         key1a.setVersion("2.2.3");
259         assertFalse(key1.isNewerThan(key1a));
260         key1a.setVersion(VERSION123);
261         assertFalse(key1.isNewerThan(key1a));
262
263         key1a.setVersion("1.1.3");
264         assertTrue(key1.isNewerThan(key1a));
265         key1a.setVersion("1.3.3");
266         assertFalse(key1.isNewerThan(key1a));
267         key1a.setVersion(VERSION123);
268         assertFalse(key1.isNewerThan(key1a));
269
270         key1a.setVersion("1.2.2");
271         assertTrue(key1.isNewerThan(key1a));
272         key1a.setVersion("1.2.4");
273         assertFalse(key1.isNewerThan(key1a));
274         key1a.setVersion(VERSION123);
275         assertFalse(key1.isNewerThan(key1a));
276
277         key1.setVersion(VERSION100);
278         assertFalse(key1.isNewerThan(key1a));
279         key1a.setVersion(VERSION100);
280         assertFalse(key1.isNewerThan(key1a));
281
282         PfReferenceKey refKey = new PfReferenceKey();
283
284         assertThatThrownBy(() -> refKey.isNewerThan(null)).hasMessageMatching(OTHER_IS_NULL);
285
286         assertThatThrownBy(() -> refKey.isNewerThan(new MyKey()))
287             .hasMessage(MyKey.class.getName() + " is not an instance of " + PfReferenceKey.class.getName());
288
289         assertFalse(refKey.isNewerThan(refKey));
290     }
291
292     @Test
293     public void testmajorMinorPatch() {
294         MyKey key = new MyKey("Key", VERSION100);
295         assertEquals(1, key.getMajorVersion());
296         assertEquals(0, key.getMinorVersion());
297         assertEquals(0, key.getPatchVersion());
298
299         key = new MyKey("Key", "1.2.0");
300         assertEquals(1, key.getMajorVersion());
301         assertEquals(2, key.getMinorVersion());
302         assertEquals(0, key.getPatchVersion());
303
304         key = new MyKey("Key", VERSION123);
305         assertEquals(1, key.getMajorVersion());
306         assertEquals(2, key.getMinorVersion());
307         assertEquals(3, key.getPatchVersion());
308     }
309
310     @Getter
311     @Setter
312     @EqualsAndHashCode(callSuper = false)
313     @NoArgsConstructor
314     public static class MyKey extends PfKeyImpl {
315         private static final long serialVersionUID = 1L;
316
317         @Pattern(regexp = NAME_REGEXP)
318         private String name;
319
320         @Pattern(regexp = VERSION_REGEXP)
321         private String version;
322
323         public MyKey(String name, String version) {
324             super(name, version);
325         }
326
327         public MyKey(String id) {
328             super(id);
329         }
330
331         public MyKey(MyKey myKey) {
332             super(myKey);
333         }
334     }
335 }