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;
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 .*on.*ull but is null$";
43 private static final String ID_IS_NULL = "^id is marked .*on.*ull 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\", " + "does not match regular expression \""
52 + PfKey.KEY_ID_REGEXP + "\"");
54 assertThatThrownBy(() -> new MyKey((MyKey) null))
55 .hasMessageMatching("^copyConcept is marked .*on.*ull 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 .hasMessageMatching("^otherKey is marked .*on.*ull 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 .hasMessageMatching("^otherObj is marked .*on.*ull 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)).hasMessageMatching(ID_IS_NULL);
153 assertThatThrownBy(() -> new MyKey((MyKey) null))
154 .hasMessageMatching("^copyConcept is marked .*on.*ull but is null$");
156 assertThatThrownBy(() -> new MyKey(null, null)).hasMessageMatching("name is marked .*on.*ull but is null$");
158 assertThatThrownBy(() -> new MyKey("name", null))
159 .hasMessageMatching("^version is marked .*on.*ull but is null$");
161 assertThatThrownBy(() -> new MyKey(null, VERSION001))
162 .hasMessageMatching("^name is marked .*on.*ull but is null$");
164 assertThatThrownBy(() -> new MyKey("AKey", VERSION001).isCompatible(null)).hasMessageMatching(OTHER_IS_NULL);
168 public void testValidation() throws Exception {
169 MyKey testKey = new MyKey("TheKey", VERSION001);
170 assertEquals("TheKey:0.0.1", testKey.getId());
172 Field nameField = testKey.getClass().getDeclaredField("name");
173 nameField.setAccessible(true);
174 nameField.set(testKey, "Key Name");
175 PfValidationResult validationResult = new PfValidationResult();
176 testKey.validate(validationResult);
177 nameField.set(testKey, "TheKey");
178 nameField.setAccessible(false);
179 assertEquals("name invalid-parameter name with value Key Name " + "does not match regular expression "
180 + PfKey.NAME_REGEXP, 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);
189 assertEquals("version invalid-parameter version with value Key Version " + "does not match regular expression "
190 + PfKey.VERSION_REGEXP, validationResult2.getMessageList().get(0).getMessage());
194 public void testkeynewerThan() {
195 MyKey key1 = new MyKey("Key1", VERSION123);
197 assertThatThrownBy(() -> key1.isNewerThan(null)).hasMessageMatching(OTHER_IS_NULL);
199 assertThatThrownBy(() -> key1.isNewerThan(new PfReferenceKey())).hasMessage(
200 "org.onap.policy.models.base.PfReferenceKey is not " + "an instance of " + PfKeyImpl.class.getName());
202 assertFalse(key1.isNewerThan(key1));
204 MyKey key1a = new MyKey("Key1a", VERSION123);
205 assertFalse(key1.isNewerThan(key1a));
207 MyKey key1b = new MyKey("Key0", VERSION123);
208 assertTrue(key1.isNewerThan(key1b));
210 key1a.setName("Key1");
211 assertFalse(key1.isNewerThan(key1a));
213 key1a.setVersion("0.2.3");
214 assertTrue(key1.isNewerThan(key1a));
215 key1a.setVersion("2.2.3");
216 assertFalse(key1.isNewerThan(key1a));
217 key1a.setVersion(VERSION123);
218 assertFalse(key1.isNewerThan(key1a));
220 key1a.setVersion("1.1.3");
221 assertTrue(key1.isNewerThan(key1a));
222 key1a.setVersion("1.3.3");
223 assertFalse(key1.isNewerThan(key1a));
224 key1a.setVersion(VERSION123);
225 assertFalse(key1.isNewerThan(key1a));
227 key1a.setVersion("1.2.2");
228 assertTrue(key1.isNewerThan(key1a));
229 key1a.setVersion("1.2.4");
230 assertFalse(key1.isNewerThan(key1a));
231 key1a.setVersion(VERSION123);
232 assertFalse(key1.isNewerThan(key1a));
234 key1.setVersion(VERSION100);
235 assertFalse(key1.isNewerThan(key1a));
236 key1a.setVersion(VERSION100);
237 assertFalse(key1.isNewerThan(key1a));
239 PfReferenceKey refKey = new PfReferenceKey();
241 assertThatThrownBy(() -> refKey.isNewerThan(null)).hasMessageMatching(OTHER_IS_NULL);
243 assertThatThrownBy(() -> refKey.isNewerThan(new MyKey()))
244 .hasMessage(MyKey.class.getName() + " is not an instance of " + PfReferenceKey.class.getName());
246 assertFalse(refKey.isNewerThan(refKey));
250 public void testmajorMinorPatch() {
251 MyKey key = new MyKey("Key", VERSION100);
252 assertEquals(1, key.getMajorVersion());
253 assertEquals(0, key.getMinorVersion());
254 assertEquals(0, key.getPatchVersion());
256 key = new MyKey("Key", "1.2.0");
257 assertEquals(1, key.getMajorVersion());
258 assertEquals(2, key.getMinorVersion());
259 assertEquals(0, key.getPatchVersion());
261 key = new MyKey("Key", VERSION123);
262 assertEquals(1, key.getMajorVersion());
263 assertEquals(2, key.getMinorVersion());
264 assertEquals(3, key.getPatchVersion());
269 @EqualsAndHashCode(callSuper = false)
271 private static class MyKey extends PfKeyImpl {
272 private static final long serialVersionUID = 1L;
275 private String version;
277 public MyKey(String name, String version) {
278 super(name, version);
281 public MyKey(String id) {
285 public MyKey(MyKey myKey) {