Fix more sonar issues in models: yaml to dao
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfKeyTest.java
1 /*-
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
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.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;
30
31 import java.lang.reflect.Field;
32 import org.junit.Test;
33 import org.onap.policy.models.base.PfKey.Compatibility;
34 import org.onap.policy.models.base.testconcepts.DummyPfConcept;
35 import org.onap.policy.models.base.testconcepts.DummyPfKey;
36
37 public class PfKeyTest {
38
39     private static final String OTHER_IS_NULL = "otherKey is marked @NonNull but is null";
40     private static final String ID_IS_NULL = "id is marked @NonNull but is null";
41     private static final String VERSION123 = "1.2.3";
42     private static final String VERSION100 = "1.0.0";
43     private static final String VERSION001 = "0.0.1";
44
45     @Test
46     public void testConceptKey() {
47         assertThatIllegalArgumentException().isThrownBy(() -> new PfConceptKey("some bad key id"))
48                         .withMessage("parameter \"id\": value \"some bad key id\", "
49                                         + "does not match regular expression \"" + PfKey.KEY_ID_REGEXP + "\"");
50
51         assertThatThrownBy(() -> new PfConceptKey((PfConceptKey) null))
52                         .hasMessage("copyConcept is marked @NonNull but is null");
53
54         PfConceptKey someKey0 = new PfConceptKey();
55         assertEquals(PfConceptKey.getNullKey(), someKey0);
56
57         PfConceptKey someKey1 = new PfConceptKey("name", VERSION001);
58         PfConceptKey someKey2 = new PfConceptKey(someKey1);
59         PfConceptKey someKey3 = new PfConceptKey(someKey1.getId());
60         assertEquals(someKey1, someKey2);
61         assertEquals(someKey1, someKey3);
62         assertFalse(someKey1.isNullVersion());
63
64         assertEquals(someKey2, someKey1.getKey());
65         assertEquals(1, someKey1.getKeys().size());
66
67         someKey0.setName("zero");
68         someKey0.setVersion("0.0.2");
69
70         someKey3.setVersion("0.0.2");
71
72         PfConceptKey someKey4 = new PfConceptKey(someKey1);
73         someKey4.setVersion("0.1.2");
74
75         PfConceptKey someKey4a = new PfConceptKey(someKey1);
76         someKey4a.setVersion("0.0.0");
77
78         PfConceptKey someKey5 = new PfConceptKey(someKey1);
79         someKey5.setVersion("1.2.2");
80
81         PfConceptKey someKey6 = new PfConceptKey(someKey1);
82         someKey6.setVersion("3.0.0");
83
84         assertEquals("name:0.1.2", someKey4.getId());
85
86         PfConcept pfc = new DummyPfConcept();
87         assertEquals(PfConceptKey.getNullKey().getId(), pfc.getId());
88
89         assertTrue(PfConceptKey.getNullKey().matchesId(pfc.getId()));
90
91         assertTrue(PfConceptKey.getNullKey().isNullKey());
92
93         assertThatThrownBy(() -> PfConceptKey.getNullKey().matchesId(null)).hasMessage(ID_IS_NULL);
94
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));
104
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()));
111
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());
126
127         someKey0.clean();
128         assertNotNull(someKey0.toString());
129
130         PfConceptKey someKey7 = new PfConceptKey(someKey1);
131         assertEquals(244799191, someKey7.hashCode());
132         assertEquals(0, someKey7.compareTo(someKey1));
133         assertEquals(-12, someKey7.compareTo(someKey0));
134
135         assertThatThrownBy(() -> someKey0.compareTo(null)).isInstanceOf(NullPointerException.class)
136                         .hasMessage("otherObj is marked @NonNull but is null");
137
138         assertEquals(0, someKey0.compareTo(someKey0));
139         assertEquals(266127751, someKey0.compareTo(new DummyPfKey()));
140
141         assertFalse(someKey0.equals(null));
142         assertTrue(someKey0.equals(someKey0));
143         assertFalse(someKey0.equals(new DummyPfKey()));
144     }
145
146     @Test
147     public void testNullArguments() {
148         assertThatThrownBy(() -> new PfConceptKey((String) null)).hasMessage(ID_IS_NULL);
149
150         assertThatThrownBy(() -> new PfConceptKey((PfConceptKey) null))
151                         .hasMessage("copyConcept is marked @NonNull but is null");
152
153         assertThatThrownBy(() -> new PfConceptKey(null, null)).hasMessage("name is marked @NonNull but is null");
154
155         assertThatThrownBy(() -> new PfConceptKey("name", null)).hasMessage("version is marked @NonNull but is null");
156
157         assertThatThrownBy(() -> new PfConceptKey(null, VERSION001)).hasMessage("name is marked @NonNull but is null");
158
159         assertThatThrownBy(() -> new PfConceptKey("AKey", VERSION001).isCompatible(null)).hasMessage(OTHER_IS_NULL);
160     }
161
162     @Test
163     public void testValidation() throws Exception {
164         PfConceptKey testKey = new PfConceptKey("TheKey", VERSION001);
165         assertEquals("TheKey:0.0.1", testKey.getId());
166
167         Field nameField = testKey.getClass().getDeclaredField("name");
168         nameField.setAccessible(true);
169         nameField.set(testKey, "Key Name");
170         PfValidationResult validationResult = new PfValidationResult();
171         testKey.validate(validationResult);
172         nameField.set(testKey, "TheKey");
173         nameField.setAccessible(false);
174         assertEquals(
175                 "name invalid-parameter name with value Key Name "
176                         + "does not match regular expression " + PfKey.NAME_REGEXP,
177                 validationResult.getMessageList().get(0).getMessage());
178
179         Field versionField = testKey.getClass().getDeclaredField("version");
180         versionField.setAccessible(true);
181         versionField.set(testKey, "Key Version");
182         PfValidationResult validationResult2 = new PfValidationResult();
183         testKey.validate(validationResult2);
184         versionField.set(testKey, VERSION001);
185         versionField.setAccessible(false);
186         assertEquals(
187                 "version invalid-parameter version with value Key Version "
188                         + "does not match regular expression " + PfKey.VERSION_REGEXP,
189                 validationResult2.getMessageList().get(0).getMessage());
190     }
191
192     @Test
193     public void testkeynewerThan() {
194         PfConceptKey key1 = new PfConceptKey("Key1", VERSION123);
195
196         assertThatThrownBy(() -> key1.isNewerThan(null)).hasMessage(OTHER_IS_NULL);
197
198         assertThatThrownBy(() -> key1.isNewerThan(new PfReferenceKey()))
199                         .hasMessage("org.onap.policy.models.base.PfReferenceKey is not "
200                                         + "an instance of org.onap.policy.models.base.PfConceptKey");
201
202         assertFalse(key1.isNewerThan(key1));
203
204         PfConceptKey key1a = new PfConceptKey("Key1a", VERSION123);
205         assertFalse(key1.isNewerThan(key1a));
206
207         PfConceptKey key1b = new PfConceptKey("Key0", VERSION123);
208         assertTrue(key1.isNewerThan(key1b));
209
210         key1a.setName("Key1");
211         assertFalse(key1.isNewerThan(key1a));
212
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));
219
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));
226
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));
233
234         key1.setVersion(VERSION100);
235         assertFalse(key1.isNewerThan(key1a));
236         key1a.setVersion(VERSION100);
237         assertFalse(key1.isNewerThan(key1a));
238
239         PfReferenceKey refKey = new PfReferenceKey();
240
241         assertThatThrownBy(() -> refKey.isNewerThan(null)).hasMessage(OTHER_IS_NULL);
242
243         assertThatThrownBy(() -> refKey.isNewerThan(new PfConceptKey()))
244                         .hasMessage("org.onap.policy.models.base.PfConceptKey is not "
245                                         + "an instance of org.onap.policy.models.base.PfReferenceKey");
246
247         assertFalse(refKey.isNewerThan(refKey));
248     }
249
250     @Test
251     public void testmajorMinorPatch() {
252         PfConceptKey key = new PfConceptKey("Key", VERSION100);
253         assertEquals(1, key.getMajorVersion());
254         assertEquals(0, key.getMinorVersion());
255         assertEquals(0, key.getPatchVersion());
256
257         key = new PfConceptKey("Key", "1.2.0");
258         assertEquals(1, key.getMajorVersion());
259         assertEquals(2, key.getMinorVersion());
260         assertEquals(0, key.getPatchVersion());
261
262         key = new PfConceptKey("Key", VERSION123);
263         assertEquals(1, key.getMajorVersion());
264         assertEquals(2, key.getMinorVersion());
265         assertEquals(3, key.getPatchVersion());
266     }
267 }