2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 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.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.assertNotEquals;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertTrue;
32 import java.lang.reflect.Field;
33 import lombok.EqualsAndHashCode;
35 import lombok.NoArgsConstructor;
37 import org.junit.Test;
38 import org.onap.policy.models.base.PfKey.Compatibility;
39 import org.onap.policy.models.base.testconcepts.DummyPfKey;
41 public class PfKeyImplTest {
43 private static final String OTHER_IS_NULL = "^otherKey is marked .*on.*ull but is null$";
44 private static final String ID_IS_NULL = "^id is marked .*on.*ull but is null$";
45 private static final String VERSION123 = "1.2.3";
46 private static final String VERSION100 = "1.0.0";
47 private static final String VERSION001 = "0.0.1";
50 public void testConceptKey() {
51 assertThatIllegalArgumentException().isThrownBy(() -> new MyKey("some bad key id"))
52 .withMessage("parameter \"id\": value \"some bad key id\", " + "does not match regular expression \""
53 + PfKey.KEY_ID_REGEXP + "\"");
55 assertThatThrownBy(() -> new MyKey((MyKey) null))
56 .hasMessageMatching("^copyConcept is marked .*on.*ull but is null$");
58 MyKey someKey0 = new MyKey();
59 assertTrue(someKey0.isNullKey());
60 assertEquals(new MyKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION), someKey0);
62 MyKey someKey1 = new MyKey("name", VERSION001);
63 MyKey someKey2 = new MyKey(someKey1);
64 MyKey someKey3 = new MyKey(someKey1.getId());
65 assertEquals(someKey1, someKey2);
66 assertEquals(someKey1, someKey3);
67 assertFalse(someKey1.isNullKey());
68 assertFalse(someKey1.isNullVersion());
70 assertEquals(someKey2, someKey1.getKey());
71 assertEquals(1, someKey1.getKeys().size());
73 someKey0.setName("zero");
74 someKey0.setVersion("0.0.2");
76 someKey3.setVersion("0.0.2");
78 MyKey someKey4 = new MyKey(someKey1);
79 someKey4.setVersion("0.1.2");
81 MyKey someKey4a = new MyKey(someKey1);
82 someKey4a.setVersion("0.0.0");
84 MyKey someKey5 = new MyKey(someKey1);
85 someKey5.setVersion("1.2.2");
87 MyKey someKey6 = new MyKey(someKey1);
88 someKey6.setVersion("3.0.0");
90 assertEquals("name:0.1.2", someKey4.getId());
92 assertThatThrownBy(() -> someKey0.getCompatibility(null)).isInstanceOf(NullPointerException.class)
93 .hasMessageMatching("^otherKey is marked .*on.*ull but is null$");
95 assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(new DummyPfKey()));
96 assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(someKey1));
97 assertEquals(Compatibility.IDENTICAL, someKey2.getCompatibility(someKey1));
98 assertEquals(Compatibility.PATCH, someKey3.getCompatibility(someKey1));
99 assertEquals(Compatibility.MINOR, someKey4.getCompatibility(someKey1));
100 assertEquals(Compatibility.PATCH, someKey4a.getCompatibility(someKey1));
101 assertEquals(Compatibility.PATCH, someKey1.getCompatibility(someKey4a));
102 assertEquals(Compatibility.MAJOR, someKey5.getCompatibility(someKey1));
103 assertEquals(Compatibility.MAJOR, someKey6.getCompatibility(someKey1));
105 assertTrue(someKey1.isCompatible(someKey2));
106 assertTrue(someKey1.isCompatible(someKey3));
107 assertTrue(someKey1.isCompatible(someKey4));
108 assertFalse(someKey1.isCompatible(someKey0));
109 assertFalse(someKey1.isCompatible(someKey5));
110 assertFalse(someKey1.isCompatible(new DummyPfKey()));
112 assertEquals(PfValidationResult.ValidationResult.VALID,
113 someKey0.validate(new PfValidationResult()).getValidationResult());
114 assertEquals(PfValidationResult.ValidationResult.VALID,
115 someKey1.validate(new PfValidationResult()).getValidationResult());
116 assertEquals(PfValidationResult.ValidationResult.VALID,
117 someKey2.validate(new PfValidationResult()).getValidationResult());
118 assertEquals(PfValidationResult.ValidationResult.VALID,
119 someKey3.validate(new PfValidationResult()).getValidationResult());
120 assertEquals(PfValidationResult.ValidationResult.VALID,
121 someKey4.validate(new PfValidationResult()).getValidationResult());
122 assertEquals(PfValidationResult.ValidationResult.VALID,
123 someKey5.validate(new PfValidationResult()).getValidationResult());
124 assertEquals(PfValidationResult.ValidationResult.VALID,
125 someKey6.validate(new PfValidationResult()).getValidationResult());
128 assertNotNull(someKey0.toString());
130 MyKey someKey7 = new MyKey(someKey1);
131 assertEquals(244799191, someKey7.hashCode());
132 assertEquals(0, someKey7.compareTo(someKey1));
133 assertEquals(-12, someKey7.compareTo(someKey0));
135 assertThatThrownBy(() -> someKey0.compareTo(null)).isInstanceOf(NullPointerException.class)
136 .hasMessageMatching("^otherObj is marked .*on.*ull but is null$");
138 assertEquals(0, someKey0.compareTo(someKey0));
139 assertEquals(-36, someKey0.compareTo(new DummyPfKey()));
141 assertNotEquals(someKey0, null);
142 assertEquals(someKey0, (Object) someKey0);
143 assertNotEquals(someKey0, (Object) new DummyPfKey());
145 MyKey someKey8 = new MyKey();
146 someKey8.setVersion(VERSION001);
147 assertFalse(someKey8.isNullKey());
149 someKey8.setVersion("10");
150 assertEquals(0, someKey8.getMinorVersion());
152 someKey8.setVersion("10.11");
153 assertEquals(0, someKey8.getPatchVersion());
157 public void testNullArguments() {
158 assertThatThrownBy(() -> new MyKey((String) null)).hasMessageMatching(ID_IS_NULL);
160 assertThatThrownBy(() -> new MyKey((MyKey) null))
161 .hasMessageMatching("^copyConcept is marked .*on.*ull but is null$");
163 assertThatThrownBy(() -> new MyKey(null, null)).hasMessageMatching("name is marked .*on.*ull but is null$");
165 assertThatThrownBy(() -> new MyKey("name", null))
166 .hasMessageMatching("^version is marked .*on.*ull but is null$");
168 assertThatThrownBy(() -> new MyKey(null, VERSION001))
169 .hasMessageMatching("^name is marked .*on.*ull but is null$");
171 assertThatThrownBy(() -> new MyKey("AKey", VERSION001).isCompatible(null)).hasMessageMatching(OTHER_IS_NULL);
175 public void testValidation() throws Exception {
176 MyKey testKey = new MyKey("TheKey", VERSION001);
177 assertEquals("TheKey:0.0.1", testKey.getId());
179 Field nameField = testKey.getClass().getDeclaredField("name");
180 nameField.setAccessible(true);
181 nameField.set(testKey, "Key Name");
182 PfValidationResult validationResult = new PfValidationResult();
183 testKey.validate(validationResult);
184 nameField.set(testKey, "TheKey");
185 nameField.setAccessible(false);
186 assertEquals("name invalid-parameter name with value Key Name " + "does not match regular expression "
187 + PfKey.NAME_REGEXP, validationResult.getMessageList().get(0).getMessage());
189 Field versionField = testKey.getClass().getDeclaredField("version");
190 versionField.setAccessible(true);
191 versionField.set(testKey, "Key Version");
192 PfValidationResult validationResult2 = new PfValidationResult();
193 testKey.validate(validationResult2);
194 versionField.set(testKey, VERSION001);
195 versionField.setAccessible(false);
196 assertEquals("version invalid-parameter version with value Key Version " + "does not match regular expression "
197 + PfKey.VERSION_REGEXP, validationResult2.getMessageList().get(0).getMessage());
201 public void testkeynewerThan() {
202 MyKey key1 = new MyKey("Key1", VERSION123);
204 assertThatThrownBy(() -> key1.isNewerThan(null)).hasMessageMatching(OTHER_IS_NULL);
206 assertThatThrownBy(() -> key1.isNewerThan(new PfReferenceKey())).hasMessage(
207 "org.onap.policy.models.base.PfReferenceKey is not " + "an instance of " + PfKeyImpl.class.getName());
209 assertFalse(key1.isNewerThan(key1));
211 MyKey key1a = new MyKey("Key1a", VERSION123);
212 assertFalse(key1.isNewerThan(key1a));
214 MyKey key1b = new MyKey("Key0", VERSION123);
215 assertTrue(key1.isNewerThan(key1b));
217 key1a.setName("Key1");
218 assertFalse(key1.isNewerThan(key1a));
220 key1a.setVersion("0.2.3");
221 assertTrue(key1.isNewerThan(key1a));
222 key1a.setVersion("2.2.3");
223 assertFalse(key1.isNewerThan(key1a));
224 key1a.setVersion(VERSION123);
225 assertFalse(key1.isNewerThan(key1a));
227 key1a.setVersion("1.1.3");
228 assertTrue(key1.isNewerThan(key1a));
229 key1a.setVersion("1.3.3");
230 assertFalse(key1.isNewerThan(key1a));
231 key1a.setVersion(VERSION123);
232 assertFalse(key1.isNewerThan(key1a));
234 key1a.setVersion("1.2.2");
235 assertTrue(key1.isNewerThan(key1a));
236 key1a.setVersion("1.2.4");
237 assertFalse(key1.isNewerThan(key1a));
238 key1a.setVersion(VERSION123);
239 assertFalse(key1.isNewerThan(key1a));
241 key1.setVersion(VERSION100);
242 assertFalse(key1.isNewerThan(key1a));
243 key1a.setVersion(VERSION100);
244 assertFalse(key1.isNewerThan(key1a));
246 PfReferenceKey refKey = new PfReferenceKey();
248 assertThatThrownBy(() -> refKey.isNewerThan(null)).hasMessageMatching(OTHER_IS_NULL);
250 assertThatThrownBy(() -> refKey.isNewerThan(new MyKey()))
251 .hasMessage(MyKey.class.getName() + " is not an instance of " + PfReferenceKey.class.getName());
253 assertFalse(refKey.isNewerThan(refKey));
257 public void testmajorMinorPatch() {
258 MyKey key = new MyKey("Key", VERSION100);
259 assertEquals(1, key.getMajorVersion());
260 assertEquals(0, key.getMinorVersion());
261 assertEquals(0, key.getPatchVersion());
263 key = new MyKey("Key", "1.2.0");
264 assertEquals(1, key.getMajorVersion());
265 assertEquals(2, key.getMinorVersion());
266 assertEquals(0, key.getPatchVersion());
268 key = new MyKey("Key", VERSION123);
269 assertEquals(1, key.getMajorVersion());
270 assertEquals(2, key.getMinorVersion());
271 assertEquals(3, key.getPatchVersion());
276 @EqualsAndHashCode(callSuper = false)
278 private static class MyKey extends PfKeyImpl {
279 private static final long serialVersionUID = 1L;
282 private String version;
284 public MyKey(String name, String version) {
285 super(name, version);
288 public MyKey(String id) {
292 public MyKey(MyKey myKey) {