2afc94b539987c09493021ac98c0cdad3fd9a901
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfKeyImplTest.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.base;
23
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.assertNotNull;
30 import static org.junit.Assert.assertTrue;
31
32 import java.lang.reflect.Field;
33 import lombok.EqualsAndHashCode;
34 import lombok.Getter;
35 import lombok.NoArgsConstructor;
36 import lombok.Setter;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.onap.policy.common.parameters.ValidationResult;
40 import org.onap.policy.common.parameters.annotations.Pattern;
41 import org.onap.policy.models.base.PfKey.Compatibility;
42 import org.onap.policy.models.base.testconcepts.DummyPfKey;
43
44 public class PfKeyImplTest {
45
46     private static final String OTHER_IS_NULL = "^otherKey is marked .*on.*ull but is null$";
47     private static final String ID_IS_NULL = "^id is marked .*on.*ull but is null$";
48     private static final String VERSION123 = "1.2.3";
49     private static final String VERSION100 = "1.0.0";
50     private static final String VERSION001 = "0.0.1";
51     private static MyKey someKey;
52     private static MyKey someKey0;
53     private static MyKey someKey1;
54     private static MyKey someKey2;
55     private static MyKey someKey3;
56     private static MyKey someKey4;
57     private static MyKey someKey4a;
58     private static MyKey someKey5;
59     private static MyKey someKey6;
60
61     /**
62      * Sets data in Keys for the tests.
63      */
64     @BeforeClass
65     public static void setUp() {
66         someKey = new MyKey();
67
68         someKey0 = new MyKey();
69         someKey1 = new MyKey("name", VERSION001);
70         someKey2 = new MyKey(someKey1);
71         someKey3 = new MyKey(someKey1.getId());
72
73         someKey0.setName("zero");
74         someKey0.setVersion("0.0.2");
75         someKey3.setVersion("0.0.2");
76
77         someKey4 = new MyKey(someKey1);
78         someKey4.setVersion("0.1.2");
79
80         someKey4a = new MyKey(someKey1);
81         someKey4a.setVersion("0.0.0");
82
83         someKey5 = new MyKey(someKey1);
84         someKey5.setVersion("1.2.2");
85
86         someKey6 = new MyKey(someKey1);
87         someKey6.setVersion("3.0.0");
88     }
89
90     @Test
91     public void testConceptKey() {
92         assertThatIllegalArgumentException().isThrownBy(() -> new MyKey("some bad key id"))
93             .withMessage("parameter \"id\": value \"some bad key id\", " + "does not match regular expression \""
94                 + PfKey.KEY_ID_REGEXP + "\"");
95
96         assertThatThrownBy(() -> new MyKey((MyKey) null))
97             .hasMessageMatching("^copyConcept is marked .*on.*ull but is null$");
98
99         assertTrue(someKey.isNullKey());
100         assertEquals(new MyKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION), someKey);
101
102         MyKey someKey11 = new MyKey("name", VERSION001);
103         MyKey someKey22 = new MyKey(someKey11);
104         MyKey someKey33 = new MyKey(someKey11.getId());
105         assertEquals(someKey11, someKey22);
106         assertEquals(someKey11, someKey33);
107         assertFalse(someKey11.isNullKey());
108         assertFalse(someKey11.isNullVersion());
109
110         assertEquals(someKey22, someKey11.getKey());
111         assertEquals(1, someKey11.getKeys().size());
112     }
113
114     @Test
115     public void testCompatibilityConceptKey() {
116         assertEquals("name:0.1.2", someKey4.getId());
117
118         assertThatThrownBy(() -> someKey0.getCompatibility(null)).isInstanceOf(NullPointerException.class)
119             .hasMessageMatching("^otherKey is marked .*on.*ull but is null$");
120
121         assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(new DummyPfKey()));
122         assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(someKey1));
123         assertEquals(Compatibility.IDENTICAL, someKey2.getCompatibility(someKey1));
124         assertEquals(Compatibility.PATCH, someKey3.getCompatibility(someKey1));
125         assertEquals(Compatibility.MINOR, someKey4.getCompatibility(someKey1));
126         assertEquals(Compatibility.PATCH, someKey4a.getCompatibility(someKey1));
127         assertEquals(Compatibility.PATCH, someKey1.getCompatibility(someKey4a));
128         assertEquals(Compatibility.MAJOR, someKey5.getCompatibility(someKey1));
129         assertEquals(Compatibility.MAJOR, someKey6.getCompatibility(someKey1));
130
131         assertTrue(someKey1.isCompatible(someKey2));
132         assertTrue(someKey1.isCompatible(someKey3));
133         assertTrue(someKey1.isCompatible(someKey4));
134         assertFalse(someKey1.isCompatible(someKey0));
135         assertFalse(someKey1.isCompatible(someKey5));
136         assertFalse(someKey1.isCompatible(new DummyPfKey()));
137     }
138
139     @Test
140     public void testValidityConceptKey() {
141         assertTrue(someKey0.validate("").isValid());
142         assertTrue(someKey1.validate("").isValid());
143         assertTrue(someKey2.validate("").isValid());
144         assertTrue(someKey3.validate("").isValid());
145         assertTrue(someKey4.validate("").isValid());
146         assertTrue(someKey5.validate("").isValid());
147         assertTrue(someKey6.validate("").isValid());
148     }
149
150     @Test
151     public void testCleanConceptKey() {
152         someKey0.clean();
153         assertNotNull(someKey0.toString());
154
155         MyKey someKey7 = new MyKey(someKey1);
156         assertEquals(244799191, someKey7.hashCode());
157         assertEquals(0, someKey7.compareTo(someKey1));
158         assertEquals(-12, someKey7.compareTo(someKey0));
159
160         assertThatThrownBy(() -> someKey0.compareTo(null)).isInstanceOf(NullPointerException.class)
161             .hasMessageMatching("^otherObj is marked .*on.*ull but is null$");
162
163         assertEquals(0, someKey0.compareTo(someKey0));
164         assertEquals(-36, someKey0.compareTo(new DummyPfKey()));
165
166         MyKey someKey8 = new MyKey();
167         someKey8.setVersion(VERSION001);
168         assertFalse(someKey8.isNullKey());
169
170         someKey8.setVersion("10");
171         assertEquals(0, someKey8.getMinorVersion());
172
173         someKey8.setVersion("10.11");
174         assertEquals(0, someKey8.getPatchVersion());
175     }
176
177     @Test
178     public void testNullArguments() {
179         assertThatThrownBy(() -> new MyKey((String) null)).hasMessageMatching(ID_IS_NULL);
180
181         assertThatThrownBy(() -> new MyKey((MyKey) null))
182             .hasMessageMatching("^copyConcept is marked .*on.*ull but is null$");
183
184         assertThatThrownBy(() -> new MyKey(null, null)).hasMessageMatching("name is marked .*on.*ull but is null$");
185
186         assertThatThrownBy(() -> new MyKey("name", null))
187             .hasMessageMatching("^version is marked .*on.*ull but is null$");
188
189         assertThatThrownBy(() -> new MyKey(null, VERSION001))
190             .hasMessageMatching("^name is marked .*on.*ull but is null$");
191
192         assertThatThrownBy(() -> new MyKey("AKey", VERSION001).isCompatible(null)).hasMessageMatching(OTHER_IS_NULL);
193     }
194
195     @Test
196     public void testValidation() throws Exception {
197         MyKey testKey = new MyKey("TheKey", VERSION001);
198         assertEquals("TheKey:0.0.1", testKey.getId());
199
200         Field nameField = testKey.getClass().getDeclaredField("name");
201         nameField.setAccessible(true);
202         nameField.set(testKey, "Key Name");
203         ValidationResult validationResult = testKey.validate("");
204         nameField.set(testKey, "TheKey");
205         nameField.setAccessible(false);
206         assertThat(validationResult.getResult()).contains("\"name\"").doesNotContain("\"version\"")
207             .contains("does not match regular expression " + PfKey.NAME_REGEXP);
208
209         Field versionField = testKey.getClass().getDeclaredField("version");
210         versionField.setAccessible(true);
211         versionField.set(testKey, "Key Version");
212         ValidationResult validationResult2 = testKey.validate("");
213         versionField.set(testKey, VERSION001);
214         versionField.setAccessible(false);
215         assertThat(validationResult2.getResult()).doesNotContain("\"name\"").contains("\"version\"")
216             .contains("does not match regular expression " + PfKey.VERSION_REGEXP);
217     }
218
219     @Test
220     public void testkeynewerThan() {
221         MyKey key1 = new MyKey("Key1", VERSION123);
222
223         assertThatThrownBy(() -> key1.isNewerThan(null)).hasMessageMatching(OTHER_IS_NULL);
224
225         assertThatThrownBy(() -> key1.isNewerThan(new PfReferenceKey())).hasMessage(
226             "org.onap.policy.models.base.PfReferenceKey is not " + "an instance of " + PfKeyImpl.class.getName());
227
228         assertFalse(key1.isNewerThan(key1));
229
230         MyKey key1a = new MyKey("Key1a", VERSION123);
231         assertFalse(key1.isNewerThan(key1a));
232
233         MyKey key1b = new MyKey("Key0", VERSION123);
234         assertTrue(key1.isNewerThan(key1b));
235
236         key1a.setName("Key1");
237         assertFalse(key1.isNewerThan(key1a));
238
239         key1a.setVersion("0.2.3");
240         assertTrue(key1.isNewerThan(key1a));
241         key1a.setVersion("2.2.3");
242         assertFalse(key1.isNewerThan(key1a));
243         key1a.setVersion(VERSION123);
244         assertFalse(key1.isNewerThan(key1a));
245
246         key1a.setVersion("1.1.3");
247         assertTrue(key1.isNewerThan(key1a));
248         key1a.setVersion("1.3.3");
249         assertFalse(key1.isNewerThan(key1a));
250         key1a.setVersion(VERSION123);
251         assertFalse(key1.isNewerThan(key1a));
252
253         key1a.setVersion("1.2.2");
254         assertTrue(key1.isNewerThan(key1a));
255         key1a.setVersion("1.2.4");
256         assertFalse(key1.isNewerThan(key1a));
257         key1a.setVersion(VERSION123);
258         assertFalse(key1.isNewerThan(key1a));
259
260         key1.setVersion(VERSION100);
261         assertFalse(key1.isNewerThan(key1a));
262         key1a.setVersion(VERSION100);
263         assertFalse(key1.isNewerThan(key1a));
264
265         PfReferenceKey refKey = new PfReferenceKey();
266
267         assertThatThrownBy(() -> refKey.isNewerThan(null)).hasMessageMatching(OTHER_IS_NULL);
268
269         assertThatThrownBy(() -> refKey.isNewerThan(new MyKey()))
270             .hasMessage(MyKey.class.getName() + " is not an instance of " + PfReferenceKey.class.getName());
271
272         assertFalse(refKey.isNewerThan(refKey));
273     }
274
275     @Test
276     public void testmajorMinorPatch() {
277         MyKey key = new MyKey("Key", VERSION100);
278         assertEquals(1, key.getMajorVersion());
279         assertEquals(0, key.getMinorVersion());
280         assertEquals(0, key.getPatchVersion());
281
282         key = new MyKey("Key", "1.2.0");
283         assertEquals(1, key.getMajorVersion());
284         assertEquals(2, key.getMinorVersion());
285         assertEquals(0, key.getPatchVersion());
286
287         key = new MyKey("Key", VERSION123);
288         assertEquals(1, key.getMajorVersion());
289         assertEquals(2, key.getMinorVersion());
290         assertEquals(3, key.getPatchVersion());
291     }
292
293     @Getter
294     @Setter
295     @EqualsAndHashCode(callSuper = false)
296     @NoArgsConstructor
297     public static class MyKey extends PfKeyImpl {
298         private static final long serialVersionUID = 1L;
299
300         @Pattern(regexp = NAME_REGEXP)
301         private String name;
302
303         @Pattern(regexp = VERSION_REGEXP)
304         private String version;
305
306         public MyKey(String name, String version) {
307             super(name, version);
308         }
309
310         public MyKey(String id) {
311             super(id);
312         }
313
314         public MyKey(MyKey myKey) {
315             super(myKey);
316         }
317     }
318 }