2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 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
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.assertThatIllegalArgumentException;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
31 import java.lang.reflect.Field;
33 import lombok.EqualsAndHashCode;
35 import lombok.NoArgsConstructor;
38 import org.junit.Test;
39 import org.onap.policy.models.base.PfKey.Compatibility;
40 import org.onap.policy.models.base.testconcepts.DummyPfKey;
42 public class PfKeyImplTest {
44 private static final String OTHER_IS_NULL = "^otherKey is marked .*on.*ull but is null$";
45 private static final String ID_IS_NULL = "^id is marked .*on.*ull but is null$";
46 private static final String VERSION123 = "1.2.3";
47 private static final String VERSION100 = "1.0.0";
48 private static final String VERSION001 = "0.0.1";
51 public void testConceptKey() {
52 assertThatIllegalArgumentException().isThrownBy(() -> new MyKey("some bad key id"))
53 .withMessage("parameter \"id\": value \"some bad key id\", " + "does not match regular expression \""
54 + PfKey.KEY_ID_REGEXP + "\"");
56 assertThatThrownBy(() -> new MyKey((MyKey) null))
57 .hasMessageMatching("^copyConcept is marked .*on.*ull but is null$");
59 MyKey someKey0 = new MyKey();
60 assertTrue(someKey0.isNullKey());
61 assertEquals(new MyKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION), someKey0);
63 MyKey someKey1 = new MyKey("name", VERSION001);
64 MyKey someKey2 = new MyKey(someKey1);
65 MyKey someKey3 = new MyKey(someKey1.getId());
66 assertEquals(someKey1, someKey2);
67 assertEquals(someKey1, someKey3);
68 assertFalse(someKey1.isNullKey());
69 assertFalse(someKey1.isNullVersion());
71 assertEquals(someKey2, someKey1.getKey());
72 assertEquals(1, someKey1.getKeys().size());
74 someKey0.setName("zero");
75 someKey0.setVersion("0.0.2");
77 someKey3.setVersion("0.0.2");
79 MyKey someKey4 = new MyKey(someKey1);
80 someKey4.setVersion("0.1.2");
82 MyKey someKey4a = new MyKey(someKey1);
83 someKey4a.setVersion("0.0.0");
85 MyKey someKey5 = new MyKey(someKey1);
86 someKey5.setVersion("1.2.2");
88 MyKey someKey6 = new MyKey(someKey1);
89 someKey6.setVersion("3.0.0");
91 assertEquals("name:0.1.2", someKey4.getId());
93 assertThatThrownBy(() -> someKey0.getCompatibility(null)).isInstanceOf(NullPointerException.class)
94 .hasMessageMatching("^otherKey is marked .*on.*ull but is null$");
96 assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(new DummyPfKey()));
97 assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(someKey1));
98 assertEquals(Compatibility.IDENTICAL, someKey2.getCompatibility(someKey1));
99 assertEquals(Compatibility.PATCH, someKey3.getCompatibility(someKey1));
100 assertEquals(Compatibility.MINOR, someKey4.getCompatibility(someKey1));
101 assertEquals(Compatibility.PATCH, someKey4a.getCompatibility(someKey1));
102 assertEquals(Compatibility.PATCH, someKey1.getCompatibility(someKey4a));
103 assertEquals(Compatibility.MAJOR, someKey5.getCompatibility(someKey1));
104 assertEquals(Compatibility.MAJOR, someKey6.getCompatibility(someKey1));
106 assertTrue(someKey1.isCompatible(someKey2));
107 assertTrue(someKey1.isCompatible(someKey3));
108 assertTrue(someKey1.isCompatible(someKey4));
109 assertFalse(someKey1.isCompatible(someKey0));
110 assertFalse(someKey1.isCompatible(someKey5));
111 assertFalse(someKey1.isCompatible(new DummyPfKey()));
113 assertEquals(PfValidationResult.ValidationResult.VALID,
114 someKey0.validate(new PfValidationResult()).getValidationResult());
115 assertEquals(PfValidationResult.ValidationResult.VALID,
116 someKey1.validate(new PfValidationResult()).getValidationResult());
117 assertEquals(PfValidationResult.ValidationResult.VALID,
118 someKey2.validate(new PfValidationResult()).getValidationResult());
119 assertEquals(PfValidationResult.ValidationResult.VALID,
120 someKey3.validate(new PfValidationResult()).getValidationResult());
121 assertEquals(PfValidationResult.ValidationResult.VALID,
122 someKey4.validate(new PfValidationResult()).getValidationResult());
123 assertEquals(PfValidationResult.ValidationResult.VALID,
124 someKey5.validate(new PfValidationResult()).getValidationResult());
125 assertEquals(PfValidationResult.ValidationResult.VALID,
126 someKey6.validate(new PfValidationResult()).getValidationResult());
129 assertNotNull(someKey0.toString());
131 MyKey someKey7 = new MyKey(someKey1);
132 assertEquals(244799191, someKey7.hashCode());
133 assertEquals(0, someKey7.compareTo(someKey1));
134 assertEquals(-12, someKey7.compareTo(someKey0));
136 assertThatThrownBy(() -> someKey0.compareTo(null)).isInstanceOf(NullPointerException.class)
137 .hasMessageMatching("^otherObj is marked .*on.*ull but is null$");
139 assertEquals(0, someKey0.compareTo(someKey0));
140 assertEquals(-36, someKey0.compareTo(new DummyPfKey()));
142 assertFalse(someKey0.equals(null));
143 assertTrue(someKey0.equals(someKey0));
144 assertFalse(someKey0.equals(new DummyPfKey()));
146 MyKey someKey8 = new MyKey();
147 someKey8.setVersion(VERSION001);
148 assertFalse(someKey8.isNullKey());
152 public void testNullArguments() {
153 assertThatThrownBy(() -> new MyKey((String) null)).hasMessageMatching(ID_IS_NULL);
155 assertThatThrownBy(() -> new MyKey((MyKey) null))
156 .hasMessageMatching("^copyConcept is marked .*on.*ull but is null$");
158 assertThatThrownBy(() -> new MyKey(null, null)).hasMessageMatching("name is marked .*on.*ull but is null$");
160 assertThatThrownBy(() -> new MyKey("name", null))
161 .hasMessageMatching("^version is marked .*on.*ull but is null$");
163 assertThatThrownBy(() -> new MyKey(null, VERSION001))
164 .hasMessageMatching("^name is marked .*on.*ull but is null$");
166 assertThatThrownBy(() -> new MyKey("AKey", VERSION001).isCompatible(null)).hasMessageMatching(OTHER_IS_NULL);
170 public void testValidation() throws Exception {
171 MyKey testKey = new MyKey("TheKey", VERSION001);
172 assertEquals("TheKey:0.0.1", testKey.getId());
174 Field nameField = testKey.getClass().getDeclaredField("name");
175 nameField.setAccessible(true);
176 nameField.set(testKey, "Key Name");
177 PfValidationResult validationResult = new PfValidationResult();
178 testKey.validate(validationResult);
179 nameField.set(testKey, "TheKey");
180 nameField.setAccessible(false);
181 assertEquals("name invalid-parameter name with value Key Name " + "does not match regular expression "
182 + PfKey.NAME_REGEXP, validationResult.getMessageList().get(0).getMessage());
184 Field versionField = testKey.getClass().getDeclaredField("version");
185 versionField.setAccessible(true);
186 versionField.set(testKey, "Key Version");
187 PfValidationResult validationResult2 = new PfValidationResult();
188 testKey.validate(validationResult2);
189 versionField.set(testKey, VERSION001);
190 versionField.setAccessible(false);
191 assertEquals("version invalid-parameter version with value Key Version " + "does not match regular expression "
192 + PfKey.VERSION_REGEXP, validationResult2.getMessageList().get(0).getMessage());
196 public void testkeynewerThan() {
197 MyKey key1 = new MyKey("Key1", VERSION123);
199 assertThatThrownBy(() -> key1.isNewerThan(null)).hasMessageMatching(OTHER_IS_NULL);
201 assertThatThrownBy(() -> key1.isNewerThan(new PfReferenceKey())).hasMessage(
202 "org.onap.policy.models.base.PfReferenceKey is not " + "an instance of " + PfKeyImpl.class.getName());
204 assertFalse(key1.isNewerThan(key1));
206 MyKey key1a = new MyKey("Key1a", VERSION123);
207 assertFalse(key1.isNewerThan(key1a));
209 MyKey key1b = new MyKey("Key0", VERSION123);
210 assertTrue(key1.isNewerThan(key1b));
212 key1a.setName("Key1");
213 assertFalse(key1.isNewerThan(key1a));
215 key1a.setVersion("0.2.3");
216 assertTrue(key1.isNewerThan(key1a));
217 key1a.setVersion("2.2.3");
218 assertFalse(key1.isNewerThan(key1a));
219 key1a.setVersion(VERSION123);
220 assertFalse(key1.isNewerThan(key1a));
222 key1a.setVersion("1.1.3");
223 assertTrue(key1.isNewerThan(key1a));
224 key1a.setVersion("1.3.3");
225 assertFalse(key1.isNewerThan(key1a));
226 key1a.setVersion(VERSION123);
227 assertFalse(key1.isNewerThan(key1a));
229 key1a.setVersion("1.2.2");
230 assertTrue(key1.isNewerThan(key1a));
231 key1a.setVersion("1.2.4");
232 assertFalse(key1.isNewerThan(key1a));
233 key1a.setVersion(VERSION123);
234 assertFalse(key1.isNewerThan(key1a));
236 key1.setVersion(VERSION100);
237 assertFalse(key1.isNewerThan(key1a));
238 key1a.setVersion(VERSION100);
239 assertFalse(key1.isNewerThan(key1a));
241 PfReferenceKey refKey = new PfReferenceKey();
243 assertThatThrownBy(() -> refKey.isNewerThan(null)).hasMessageMatching(OTHER_IS_NULL);
245 assertThatThrownBy(() -> refKey.isNewerThan(new MyKey()))
246 .hasMessage(MyKey.class.getName() + " is not an instance of " + PfReferenceKey.class.getName());
248 assertFalse(refKey.isNewerThan(refKey));
252 public void testmajorMinorPatch() {
253 MyKey key = new MyKey("Key", VERSION100);
254 assertEquals(1, key.getMajorVersion());
255 assertEquals(0, key.getMinorVersion());
256 assertEquals(0, key.getPatchVersion());
258 key = new MyKey("Key", "1.2.0");
259 assertEquals(1, key.getMajorVersion());
260 assertEquals(2, key.getMinorVersion());
261 assertEquals(0, key.getPatchVersion());
263 key = new MyKey("Key", VERSION123);
264 assertEquals(1, key.getMajorVersion());
265 assertEquals(2, key.getMinorVersion());
266 assertEquals(3, key.getPatchVersion());
271 @EqualsAndHashCode(callSuper = false)
273 private static class MyKey extends PfKeyImpl {
274 private static final long serialVersionUID = 1L;
277 private String version;
279 public MyKey(String name, String version) {
280 super(name, version);
283 public MyKey(String id) {
287 public MyKey(MyKey myKey) {