2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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;
32 import lombok.EqualsAndHashCode;
34 import lombok.NoArgsConstructor;
36 import org.junit.Test;
37 import org.onap.policy.models.base.PfKey.Compatibility;
38 import org.onap.policy.models.base.testconcepts.DummyPfKey;
40 public class PfKeyImplTest {
42 private static final String OTHER_IS_NULL = "otherKey is marked @NonNull but is null";
43 private static final String ID_IS_NULL = "id is marked @NonNull but is null";
44 private static final String VERSION123 = "1.2.3";
45 private static final String VERSION100 = "1.0.0";
46 private static final String VERSION001 = "0.0.1";
49 public void testConceptKey() {
50 assertThatIllegalArgumentException().isThrownBy(() -> new MyKey("some bad key id"))
51 .withMessage("parameter \"id\": value \"some bad key id\", "
52 + "does not match regular expression \"" + PfKey.KEY_ID_REGEXP + "\"");
54 assertThatThrownBy(() -> new MyKey((MyKey) null))
55 .hasMessage("copyConcept is marked @NonNull but is null");
57 MyKey someKey0 = new MyKey();
58 assertTrue(someKey0.isNullKey());
59 assertEquals(new MyKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION), someKey0);
61 MyKey someKey1 = new MyKey("name", VERSION001);
62 MyKey someKey2 = new MyKey(someKey1);
63 MyKey someKey3 = new MyKey(someKey1.getId());
64 assertEquals(someKey1, someKey2);
65 assertEquals(someKey1, someKey3);
66 assertFalse(someKey1.isNullKey());
67 assertFalse(someKey1.isNullVersion());
69 assertEquals(someKey2, someKey1.getKey());
70 assertEquals(1, someKey1.getKeys().size());
72 someKey0.setName("zero");
73 someKey0.setVersion("0.0.2");
75 someKey3.setVersion("0.0.2");
77 MyKey someKey4 = new MyKey(someKey1);
78 someKey4.setVersion("0.1.2");
80 MyKey someKey4a = new MyKey(someKey1);
81 someKey4a.setVersion("0.0.0");
83 MyKey someKey5 = new MyKey(someKey1);
84 someKey5.setVersion("1.2.2");
86 MyKey someKey6 = new MyKey(someKey1);
87 someKey6.setVersion("3.0.0");
89 assertEquals("name:0.1.2", someKey4.getId());
91 assertThatThrownBy(() -> someKey0.getCompatibility(null)).isInstanceOf(NullPointerException.class)
92 .hasMessage("otherKey is marked @NonNull but is null");
94 assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(new DummyPfKey()));
95 assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(someKey1));
96 assertEquals(Compatibility.IDENTICAL, someKey2.getCompatibility(someKey1));
97 assertEquals(Compatibility.PATCH, someKey3.getCompatibility(someKey1));
98 assertEquals(Compatibility.MINOR, someKey4.getCompatibility(someKey1));
99 assertEquals(Compatibility.PATCH, someKey4a.getCompatibility(someKey1));
100 assertEquals(Compatibility.PATCH, someKey1.getCompatibility(someKey4a));
101 assertEquals(Compatibility.MAJOR, someKey5.getCompatibility(someKey1));
102 assertEquals(Compatibility.MAJOR, someKey6.getCompatibility(someKey1));
104 assertTrue(someKey1.isCompatible(someKey2));
105 assertTrue(someKey1.isCompatible(someKey3));
106 assertTrue(someKey1.isCompatible(someKey4));
107 assertFalse(someKey1.isCompatible(someKey0));
108 assertFalse(someKey1.isCompatible(someKey5));
109 assertFalse(someKey1.isCompatible(new DummyPfKey()));
111 assertEquals(PfValidationResult.ValidationResult.VALID,
112 someKey0.validate(new PfValidationResult()).getValidationResult());
113 assertEquals(PfValidationResult.ValidationResult.VALID,
114 someKey1.validate(new PfValidationResult()).getValidationResult());
115 assertEquals(PfValidationResult.ValidationResult.VALID,
116 someKey2.validate(new PfValidationResult()).getValidationResult());
117 assertEquals(PfValidationResult.ValidationResult.VALID,
118 someKey3.validate(new PfValidationResult()).getValidationResult());
119 assertEquals(PfValidationResult.ValidationResult.VALID,
120 someKey4.validate(new PfValidationResult()).getValidationResult());
121 assertEquals(PfValidationResult.ValidationResult.VALID,
122 someKey5.validate(new PfValidationResult()).getValidationResult());
123 assertEquals(PfValidationResult.ValidationResult.VALID,
124 someKey6.validate(new PfValidationResult()).getValidationResult());
127 assertNotNull(someKey0.toString());
129 MyKey someKey7 = new MyKey(someKey1);
130 assertEquals(244799191, someKey7.hashCode());
131 assertEquals(0, someKey7.compareTo(someKey1));
132 assertEquals(-12, someKey7.compareTo(someKey0));
134 assertThatThrownBy(() -> someKey0.compareTo(null)).isInstanceOf(NullPointerException.class)
135 .hasMessage("otherObj is marked @NonNull but is null");
137 assertEquals(0, someKey0.compareTo(someKey0));
138 assertEquals(-36, someKey0.compareTo(new DummyPfKey()));
140 assertFalse(someKey0.equals(null));
141 assertTrue(someKey0.equals(someKey0));
142 assertFalse(someKey0.equals(new DummyPfKey()));
144 MyKey someKey8 = new MyKey();
145 someKey8.setVersion(VERSION001);
146 assertFalse(someKey8.isNullKey());
150 public void testNullArguments() {
151 assertThatThrownBy(() -> new MyKey((String) null)).hasMessage(ID_IS_NULL);
153 assertThatThrownBy(() -> new MyKey((MyKey) null))
154 .hasMessage("copyConcept is marked @NonNull but is null");
156 assertThatThrownBy(() -> new MyKey(null, null)).hasMessage("name is marked @NonNull but is null");
158 assertThatThrownBy(() -> new MyKey("name", null)).hasMessage("version is marked @NonNull but is null");
160 assertThatThrownBy(() -> new MyKey(null, VERSION001)).hasMessage("name is marked @NonNull but is null");
162 assertThatThrownBy(() -> new MyKey("AKey", VERSION001).isCompatible(null)).hasMessage(OTHER_IS_NULL);
166 public void testValidation() throws Exception {
167 MyKey testKey = new MyKey("TheKey", VERSION001);
168 assertEquals("TheKey:0.0.1", testKey.getId());
170 Field nameField = testKey.getClass().getDeclaredField("name");
171 nameField.setAccessible(true);
172 nameField.set(testKey, "Key Name");
173 PfValidationResult validationResult = new PfValidationResult();
174 testKey.validate(validationResult);
175 nameField.set(testKey, "TheKey");
176 nameField.setAccessible(false);
178 "name invalid-parameter name with value Key Name "
179 + "does not match regular expression " + PfKey.NAME_REGEXP,
180 validationResult.getMessageList().get(0).getMessage());
182 Field versionField = testKey.getClass().getDeclaredField("version");
183 versionField.setAccessible(true);
184 versionField.set(testKey, "Key Version");
185 PfValidationResult validationResult2 = new PfValidationResult();
186 testKey.validate(validationResult2);
187 versionField.set(testKey, VERSION001);
188 versionField.setAccessible(false);
190 "version invalid-parameter version with value Key Version "
191 + "does not match regular expression " + PfKey.VERSION_REGEXP,
192 validationResult2.getMessageList().get(0).getMessage());
196 public void testkeynewerThan() {
197 MyKey key1 = new MyKey("Key1", VERSION123);
199 assertThatThrownBy(() -> key1.isNewerThan(null)).hasMessage(OTHER_IS_NULL);
201 assertThatThrownBy(() -> key1.isNewerThan(new PfReferenceKey()))
202 .hasMessage("org.onap.policy.models.base.PfReferenceKey is not "
203 + "an instance of " + PfKeyImpl.class.getName());
205 assertFalse(key1.isNewerThan(key1));
207 MyKey key1a = new MyKey("Key1a", VERSION123);
208 assertFalse(key1.isNewerThan(key1a));
210 MyKey key1b = new MyKey("Key0", VERSION123);
211 assertTrue(key1.isNewerThan(key1b));
213 key1a.setName("Key1");
214 assertFalse(key1.isNewerThan(key1a));
216 key1a.setVersion("0.2.3");
217 assertTrue(key1.isNewerThan(key1a));
218 key1a.setVersion("2.2.3");
219 assertFalse(key1.isNewerThan(key1a));
220 key1a.setVersion(VERSION123);
221 assertFalse(key1.isNewerThan(key1a));
223 key1a.setVersion("1.1.3");
224 assertTrue(key1.isNewerThan(key1a));
225 key1a.setVersion("1.3.3");
226 assertFalse(key1.isNewerThan(key1a));
227 key1a.setVersion(VERSION123);
228 assertFalse(key1.isNewerThan(key1a));
230 key1a.setVersion("1.2.2");
231 assertTrue(key1.isNewerThan(key1a));
232 key1a.setVersion("1.2.4");
233 assertFalse(key1.isNewerThan(key1a));
234 key1a.setVersion(VERSION123);
235 assertFalse(key1.isNewerThan(key1a));
237 key1.setVersion(VERSION100);
238 assertFalse(key1.isNewerThan(key1a));
239 key1a.setVersion(VERSION100);
240 assertFalse(key1.isNewerThan(key1a));
242 PfReferenceKey refKey = new PfReferenceKey();
244 assertThatThrownBy(() -> refKey.isNewerThan(null)).hasMessage(OTHER_IS_NULL);
246 assertThatThrownBy(() -> refKey.isNewerThan(new MyKey()))
247 .hasMessage(MyKey.class.getName() + " is not an instance of " + PfReferenceKey.class.getName());
249 assertFalse(refKey.isNewerThan(refKey));
253 public void testmajorMinorPatch() {
254 MyKey key = new MyKey("Key", VERSION100);
255 assertEquals(1, key.getMajorVersion());
256 assertEquals(0, key.getMinorVersion());
257 assertEquals(0, key.getPatchVersion());
259 key = new MyKey("Key", "1.2.0");
260 assertEquals(1, key.getMajorVersion());
261 assertEquals(2, key.getMinorVersion());
262 assertEquals(0, key.getPatchVersion());
264 key = new MyKey("Key", VERSION123);
265 assertEquals(1, key.getMajorVersion());
266 assertEquals(2, key.getMinorVersion());
267 assertEquals(3, key.getPatchVersion());
272 @EqualsAndHashCode(callSuper = false)
274 private static class MyKey extends PfKeyImpl {
275 private static final long serialVersionUID = 1L;
278 private String version;
280 public MyKey(String name, String version) {
281 super(name, version);
284 public MyKey(String id) {
288 public MyKey(MyKey myKey) {