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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.base;
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;
33 import java.lang.reflect.Field;
34 import lombok.EqualsAndHashCode;
36 import lombok.NoArgsConstructor;
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;
45 public class PfKeyImplTest {
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;
63 * Sets data in Keys for the tests.
66 public static void setUp() {
67 someKey = new MyKey();
69 someKey0 = new MyKey();
70 someKey1 = new MyKey("name", VERSION001);
71 someKey2 = new MyKey(someKey1);
72 someKey3 = new MyKey(someKey1.getId());
74 someKey0.setName("zero");
75 someKey0.setVersion("0.0.2");
76 someKey3.setVersion("0.0.2");
78 someKey4 = new MyKey(someKey1);
79 someKey4.setVersion("0.1.2");
81 someKey4a = new MyKey(someKey1);
82 someKey4a.setVersion("0.0.0");
84 someKey5 = new MyKey(someKey1);
85 someKey5.setVersion("1.2.2");
87 someKey6 = new MyKey(someKey1);
88 someKey6.setVersion("3.0.0");
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 + "\"");
97 assertThatThrownBy(() -> new MyKey((MyKey) null))
98 .hasMessageMatching("^copyConcept is marked .*on.*ull but is null$");
100 assertTrue(someKey.isNullKey());
101 assertEquals(new MyKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION), someKey);
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());
111 assertEquals(someKey22, someKey11.getKey());
112 assertEquals(1, someKey11.getKeys().size());
116 public void testCompatibilityConceptKey() {
117 assertEquals("name:0.1.2", someKey4.getId());
119 assertThatThrownBy(() -> someKey0.getCompatibility(null)).isInstanceOf(NullPointerException.class)
120 .hasMessageMatching("^otherKey is marked .*on.*ull but is null$");
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));
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()));
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());
152 public void testCleanConceptKey() {
154 assertNotNull(someKey0.toString());
156 MyKey someKey7 = new MyKey(someKey1);
157 assertEquals(244799191, someKey7.hashCode());
158 assertEquals(0, someKey7.compareTo(someKey1));
159 assertEquals(-12, someKey7.compareTo(someKey0));
161 assertThatThrownBy(() -> someKey0.compareTo(null)).isInstanceOf(NullPointerException.class)
162 .hasMessageMatching("^otherObj is marked .*on.*ull but is null$");
164 assertEquals(0, someKey0.compareTo(someKey0));
165 assertEquals(-36, someKey0.compareTo(new DummyPfKey()));
167 assertNotEquals(someKey0, null);
168 assertEquals(someKey0, (Object) someKey0);
169 assertNotEquals(someKey0, (Object) new DummyPfKey());
171 MyKey someKey8 = new MyKey();
172 someKey8.setVersion(VERSION001);
173 assertFalse(someKey8.isNullKey());
175 someKey8.setVersion("10");
176 assertEquals(0, someKey8.getMinorVersion());
178 someKey8.setVersion("10.11");
179 assertEquals(0, someKey8.getPatchVersion());
183 public void testNullArguments() {
184 assertThatThrownBy(() -> new MyKey((String) null)).hasMessageMatching(ID_IS_NULL);
186 assertThatThrownBy(() -> new MyKey((MyKey) null))
187 .hasMessageMatching("^copyConcept is marked .*on.*ull but is null$");
189 assertThatThrownBy(() -> new MyKey(null, null)).hasMessageMatching("name is marked .*on.*ull but is null$");
191 assertThatThrownBy(() -> new MyKey("name", null))
192 .hasMessageMatching("^version is marked .*on.*ull but is null$");
194 assertThatThrownBy(() -> new MyKey(null, VERSION001))
195 .hasMessageMatching("^name is marked .*on.*ull but is null$");
197 assertThatThrownBy(() -> new MyKey("AKey", VERSION001).isCompatible(null)).hasMessageMatching(OTHER_IS_NULL);
201 public void testValidation() throws Exception {
202 MyKey testKey = new MyKey("TheKey", VERSION001);
203 assertEquals("TheKey:0.0.1", testKey.getId());
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);
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);
225 public void testkeynewerThan() {
226 MyKey key1 = new MyKey("Key1", VERSION123);
228 assertThatThrownBy(() -> key1.isNewerThan(null)).hasMessageMatching(OTHER_IS_NULL);
230 assertThatThrownBy(() -> key1.isNewerThan(new PfReferenceKey())).hasMessage(
231 "org.onap.policy.models.base.PfReferenceKey is not " + "an instance of " + PfKeyImpl.class.getName());
233 assertFalse(key1.isNewerThan(key1));
235 MyKey key1a = new MyKey("Key1a", VERSION123);
236 assertFalse(key1.isNewerThan(key1a));
238 MyKey key1b = new MyKey("Key0", VERSION123);
239 assertTrue(key1.isNewerThan(key1b));
241 key1a.setName("Key1");
242 assertFalse(key1.isNewerThan(key1a));
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));
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));
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));
265 key1.setVersion(VERSION100);
266 assertFalse(key1.isNewerThan(key1a));
267 key1a.setVersion(VERSION100);
268 assertFalse(key1.isNewerThan(key1a));
270 PfReferenceKey refKey = new PfReferenceKey();
272 assertThatThrownBy(() -> refKey.isNewerThan(null)).hasMessageMatching(OTHER_IS_NULL);
274 assertThatThrownBy(() -> refKey.isNewerThan(new MyKey()))
275 .hasMessage(MyKey.class.getName() + " is not an instance of " + PfReferenceKey.class.getName());
277 assertFalse(refKey.isNewerThan(refKey));
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());
287 key = new MyKey("Key", "1.2.0");
288 assertEquals(1, key.getMajorVersion());
289 assertEquals(2, key.getMinorVersion());
290 assertEquals(0, key.getPatchVersion());
292 key = new MyKey("Key", VERSION123);
293 assertEquals(1, key.getMajorVersion());
294 assertEquals(2, key.getMinorVersion());
295 assertEquals(3, key.getPatchVersion());
300 @EqualsAndHashCode(callSuper = false)
302 public static class MyKey extends PfKeyImpl {
303 private static final long serialVersionUID = 1L;
305 @Pattern(regexp = NAME_REGEXP)
308 @Pattern(regexp = VERSION_REGEXP)
309 private String version;
311 public MyKey(String name, String version) {
312 super(name, version);
315 public MyKey(String id) {
319 public MyKey(MyKey myKey) {