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