aboutsummaryrefslogtreecommitdiffstats
path: root/models-base/src/test/java/org/onap/policy/models/base/PfKeyTest.java
blob: 7e7a40998e394bafec3e05d609818b46ef1b5451 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.models.base;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.lang.reflect.Field;

import org.junit.Test;

import org.onap.policy.models.base.PfKey.Compatibility;
import org.onap.policy.models.base.testconcepts.DummyPfConcept;
import org.onap.policy.models.base.testconcepts.DummyPfKey;

public class PfKeyTest {

    @Test
    public void testConceptKey() {
        try {
            new PfConceptKey("some bad key id");
            fail("This test should throw an exception");
        } catch (IllegalArgumentException e) {
            assertEquals(
                    "parameter \"id\": value \"some bad key id\", "
                            + "does not match regular expression \"[A-Za-z0-9\\-_\\.]+:[0-9].[0-9].[0-9]\"",
                    e.getMessage());
        }

        try {
            new PfConceptKey((PfConceptKey) null);
            fail("This test should throw an exception");
        } catch (Exception e) {
            assertEquals("copyConcept is marked @NonNull but is null", e.getMessage());
        }

        PfConceptKey someKey0 = new PfConceptKey();
        assertEquals(PfConceptKey.getNullKey(), someKey0);

        PfConceptKey someKey1 = new PfConceptKey("name", "0.0.1");
        PfConceptKey someKey2 = new PfConceptKey(someKey1);
        PfConceptKey someKey3 = new PfConceptKey(someKey1.getId());
        assertEquals(someKey1, someKey2);
        assertEquals(someKey1, someKey3);
        assertFalse(someKey1.isNullVersion());

        assertEquals(someKey2, someKey1.getKey());
        assertEquals(1, someKey1.getKeys().size());

        someKey0.setName("zero");
        someKey0.setVersion("0.0.2");

        someKey3.setVersion("0.0.2");

        PfConceptKey someKey4 = new PfConceptKey(someKey1);
        someKey4.setVersion("0.1.2");

        PfConceptKey someKey4a = new PfConceptKey(someKey1);
        someKey4a.setVersion("0");

        PfConceptKey someKey5 = new PfConceptKey(someKey1);
        someKey5.setVersion("1.2.2");

        PfConceptKey someKey6 = new PfConceptKey(someKey1);
        someKey6.setVersion("3");

        assertEquals("name:0.1.2", someKey4.getId());

        PfConcept pfc = new DummyPfConcept();
        assertEquals(PfConceptKey.getNullKey().getId(), pfc.getId());

        assertTrue(PfConceptKey.getNullKey().matchesId(pfc.getId()));

        assertTrue(PfConceptKey.getNullKey().isNullKey());

        try {
            PfConceptKey.getNullKey().matchesId(null);
            fail("test should throw an exception");
        } catch (Exception exc) {
            assertEquals("id is marked @NonNull but is null", exc.getMessage());
        }

        assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(new DummyPfKey()));
        assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(someKey1));
        assertEquals(Compatibility.IDENTICAL, someKey2.getCompatibility(someKey1));
        assertEquals(Compatibility.PATCH, someKey3.getCompatibility(someKey1));
        assertEquals(Compatibility.MINOR, someKey4.getCompatibility(someKey1));
        assertEquals(Compatibility.PATCH, someKey4a.getCompatibility(someKey1));
        assertEquals(Compatibility.PATCH, someKey1.getCompatibility(someKey4a));
        assertEquals(Compatibility.MAJOR, someKey5.getCompatibility(someKey1));
        assertEquals(Compatibility.MAJOR, someKey6.getCompatibility(someKey1));

        assertTrue(someKey1.isCompatible(someKey2));
        assertTrue(someKey1.isCompatible(someKey3));
        assertTrue(someKey1.isCompatible(someKey4));
        assertFalse(someKey1.isCompatible(someKey0));
        assertFalse(someKey1.isCompatible(someKey5));
        assertFalse(someKey1.isCompatible(new DummyPfKey()));

        assertEquals(PfValidationResult.ValidationResult.VALID,
                someKey0.validate(new PfValidationResult()).getValidationResult());
        assertEquals(PfValidationResult.ValidationResult.VALID,
                someKey1.validate(new PfValidationResult()).getValidationResult());
        assertEquals(PfValidationResult.ValidationResult.VALID,
                someKey2.validate(new PfValidationResult()).getValidationResult());
        assertEquals(PfValidationResult.ValidationResult.VALID,
                someKey3.validate(new PfValidationResult()).getValidationResult());
        assertEquals(PfValidationResult.ValidationResult.VALID,
                someKey4.validate(new PfValidationResult()).getValidationResult());
        assertEquals(PfValidationResult.ValidationResult.VALID,
                someKey5.validate(new PfValidationResult()).getValidationResult());
        assertEquals(PfValidationResult.ValidationResult.VALID,
                someKey6.validate(new PfValidationResult()).getValidationResult());

        someKey0.clean();
        assertNotNull(someKey0.toString());

        PfConceptKey someKey7 = new PfConceptKey(someKey1);
        assertEquals(244799191, someKey7.hashCode());
        assertEquals(0, someKey7.compareTo(someKey1));
        assertEquals(-12, someKey7.compareTo(someKey0));

        try {
            someKey0.compareTo(null);
            fail("test should throw an exception here");
        } catch (NullPointerException e) {
            assertEquals("otherObj is marked @NonNull but is null", e.getMessage());
        }

        assertEquals(0, someKey0.compareTo(someKey0));
        assertEquals(266127751, someKey0.compareTo(new DummyPfKey()));

        assertFalse(someKey0.equals(null));
        assertTrue(someKey0.equals(someKey0));
        assertFalse(((PfKey) someKey0).equals(new DummyPfKey()));
    }

    @Test
    public void testNullArguments() {
        try {
            new PfConceptKey((String) null);
            fail("test should throw an exception here");
        } catch (Exception exc) {
            assertEquals("id is marked @NonNull but is null", exc.getMessage());
        }

        try {
            new PfConceptKey((PfConceptKey) null);
            fail("id is marked @NonNull but is null");
        } catch (Exception exc) {
            assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
        }

        try {
            new PfConceptKey(null, null);
            fail("id is marked @NonNull but is null");
        } catch (Exception exc) {
            assertEquals("name is marked @NonNull but is null", exc.getMessage());
        }

        try {
            new PfConceptKey("name", null);
            fail("id is marked @NonNull but is null");
        } catch (Exception exc) {
            assertEquals("version is marked @NonNull but is null", exc.getMessage());
        }

        try {
            new PfConceptKey(null, "0.0.1");
            fail("id is marked @NonNull but is null");
        } catch (Exception exc) {
            assertEquals("name is marked @NonNull but is null", exc.getMessage());
        }

        try {
            PfConceptKey key = new PfConceptKey("AKey", "0.0.1");
            key.isCompatible(null);
            fail("id is marked @NonNull but is null");
        } catch (Exception exc) {
            assertEquals("otherKey is marked @NonNull but is null", exc.getMessage());
        }
    }

    @Test
    public void testValidation() {
        PfConceptKey testKey = new PfConceptKey("TheKey", "0.0.1");
        assertEquals("TheKey:0.0.1", testKey.getId());

        try {
            Field nameField = testKey.getClass().getDeclaredField("name");
            nameField.setAccessible(true);
            nameField.set(testKey, "Key Name");
            PfValidationResult validationResult = new PfValidationResult();
            testKey.validate(validationResult);
            nameField.set(testKey, "TheKey");
            nameField.setAccessible(false);
            assertEquals(
                    "name invalid-parameter name with value Key Name "
                            + "does not match regular expression [A-Za-z0-9\\-_\\.]+",
                    validationResult.getMessageList().get(0).getMessage());
        } catch (Exception validationException) {
            fail("test should not throw an exception");
        }

        try {
            Field versionField = testKey.getClass().getDeclaredField("version");
            versionField.setAccessible(true);
            versionField.set(testKey, "Key Version");
            PfValidationResult validationResult = new PfValidationResult();
            testKey.validate(validationResult);
            versionField.set(testKey, "0.0.1");
            versionField.setAccessible(false);
            assertEquals(
                    "version invalid-parameter version with value Key Version "
                            + "does not match regular expression [0-9.]+",
                    validationResult.getMessageList().get(0).getMessage());
        } catch (Exception validationException) {
            fail("test should not throw an exception");
        }
    }

    @Test
    public void testkeynewerThan() {
        PfConceptKey key1 = new PfConceptKey("Key1", "1.2.3");

        try {
            key1.isNewerThan(null);
            fail("test should throw an exception");
        } catch (Exception exc) {
            assertEquals("otherKey is marked @NonNull but is null", exc.getMessage());
        }

        try {
            key1.isNewerThan(new PfReferenceKey());
            fail("test should throw an exception");
        } catch (Exception exc) {
            assertEquals("org.onap.policy.models.base.PfReferenceKey is not "
                    + "an instance of org.onap.policy.models.base.PfConceptKey", exc.getMessage());
        }

        assertFalse(key1.isNewerThan(key1));

        PfConceptKey key1a = new PfConceptKey("Key1a", "1.2.3");
        assertFalse(key1.isNewerThan(key1a));

        PfConceptKey key1b = new PfConceptKey("Key0", "1.2.3");
        assertTrue(key1.isNewerThan(key1b));

        key1a.setName("Key1");
        assertFalse(key1.isNewerThan(key1a));

        key1a.setVersion("0.2.3");
        assertTrue(key1.isNewerThan(key1a));
        key1a.setVersion("2.2.3");
        assertFalse(key1.isNewerThan(key1a));
        key1a.setVersion("1.2.3");
        assertFalse(key1.isNewerThan(key1a));

        key1a.setVersion("1.1.3");
        assertTrue(key1.isNewerThan(key1a));
        key1a.setVersion("1.3.3");
        assertFalse(key1.isNewerThan(key1a));
        key1a.setVersion("1.2.3");
        assertFalse(key1.isNewerThan(key1a));

        key1a.setVersion("1.2.2");
        assertTrue(key1.isNewerThan(key1a));
        key1a.setVersion("1.2.4");
        assertFalse(key1.isNewerThan(key1a));
        key1a.setVersion("1.2.3");
        assertFalse(key1.isNewerThan(key1a));

        key1.setVersion("1");
        assertFalse(key1.isNewerThan(key1a));
        key1a.setVersion("1");
        assertFalse(key1.isNewerThan(key1a));

        PfReferenceKey refKey = new PfReferenceKey();

        try {
            refKey.isNewerThan(null);
            fail("test should throw an exception");
        } catch (Exception exc) {
            assertEquals("otherKey is marked @NonNull but is null", exc.getMessage());
        }

        try {
            refKey.isNewerThan(new PfConceptKey());
            fail("test should throw an exception");
        } catch (Exception exc) {
            assertEquals("org.onap.policy.models.base.PfConceptKey is not "
                    + "an instance of org.onap.policy.models.base.PfReferenceKey", exc.getMessage());
        }

        assertFalse(refKey.isNewerThan(refKey));
    }

    @Test
    public void testmajorMinorPatch() {
        PfConceptKey key = new PfConceptKey("Key", "1");
        assertEquals(1, key.getMajorVersion());
        assertEquals(0, key.getMinorVersion());
        assertEquals(0, key.getPatchVersion());

        key = new PfConceptKey("Key", "1.2");
        assertEquals(1, key.getMajorVersion());
        assertEquals(2, key.getMinorVersion());
        assertEquals(0, key.getPatchVersion());

        key = new PfConceptKey("Key", "1.2.3");
        assertEquals(1, key.getMajorVersion());
        assertEquals(2, key.getMinorVersion());
        assertEquals(3, key.getPatchVersion());
    }
}