2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.base;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
29 import java.lang.reflect.Field;
31 import org.junit.Test;
33 import org.onap.policy.models.base.PfKey.Compatibility;
34 import org.onap.policy.models.base.testpojos.DummyPfConcept;
35 import org.onap.policy.models.base.testpojos.DummyPfKey;
37 public class PfKeyTest {
40 public void testConceptKey() {
42 new PfConceptKey("some bad key id");
43 fail("This test should throw an exception");
44 } catch (IllegalArgumentException e) {
45 assertEquals("parameter \"id\": value \"some bad key id\", "
46 + "does not match regular expression \"[A-Za-z0-9\\-_\\.]+:[0-9].[0-9].[0-9]\"",
50 PfConceptKey someKey0 = new PfConceptKey();
51 assertEquals(PfConceptKey.getNullKey(), someKey0);
53 PfConceptKey someKey1 = new PfConceptKey("name", "0.0.1");
54 PfConceptKey someKey2 = new PfConceptKey(someKey1);
55 PfConceptKey someKey3 = new PfConceptKey(someKey1.getId());
56 assertEquals(someKey1, someKey2);
57 assertEquals(someKey1, someKey3);
59 assertEquals(someKey2, someKey1.getKey());
60 assertEquals(1, someKey1.getKeys().size());
62 someKey0.setName("zero");
63 someKey0.setVersion("0.0.2");
65 someKey3.setVersion("0.0.2");
67 PfConceptKey someKey4 = new PfConceptKey(someKey1);
68 someKey4.setVersion("0.1.2");
70 PfConceptKey someKey5 = new PfConceptKey(someKey1);
71 someKey5.setVersion("1.2.2");
73 PfConceptKey someKey6 = new PfConceptKey(someKey1);
74 someKey6.setVersion("3");
76 assertEquals("name:0.1.2", someKey4.getId());
78 PfConcept pfc = new DummyPfConcept();
79 assertEquals(PfConceptKey.getNullKey().getId(), pfc.getId());
80 assertTrue(PfConceptKey.getNullKey().matchesId(pfc.getId()));
82 assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(new DummyPfKey()));
83 assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(someKey1));
84 assertEquals(Compatibility.IDENTICAL, someKey2.getCompatibility(someKey1));
85 assertEquals(Compatibility.PATCH, someKey3.getCompatibility(someKey1));
86 assertEquals(Compatibility.MINOR, someKey4.getCompatibility(someKey1));
87 assertEquals(Compatibility.MAJOR, someKey5.getCompatibility(someKey1));
88 assertEquals(Compatibility.MAJOR, someKey6.getCompatibility(someKey1));
90 assertTrue(someKey1.isCompatible(someKey2));
91 assertTrue(someKey1.isCompatible(someKey3));
92 assertTrue(someKey1.isCompatible(someKey4));
93 assertFalse(someKey1.isCompatible(someKey0));
94 assertFalse(someKey1.isCompatible(someKey5));
95 assertFalse(someKey1.isCompatible(new DummyPfKey()));
97 assertEquals(PfValidationResult.ValidationResult.VALID,
98 someKey0.validate(new PfValidationResult()).getValidationResult());
99 assertEquals(PfValidationResult.ValidationResult.VALID,
100 someKey1.validate(new PfValidationResult()).getValidationResult());
101 assertEquals(PfValidationResult.ValidationResult.VALID,
102 someKey2.validate(new PfValidationResult()).getValidationResult());
103 assertEquals(PfValidationResult.ValidationResult.VALID,
104 someKey3.validate(new PfValidationResult()).getValidationResult());
105 assertEquals(PfValidationResult.ValidationResult.VALID,
106 someKey4.validate(new PfValidationResult()).getValidationResult());
107 assertEquals(PfValidationResult.ValidationResult.VALID,
108 someKey5.validate(new PfValidationResult()).getValidationResult());
109 assertEquals(PfValidationResult.ValidationResult.VALID,
110 someKey6.validate(new PfValidationResult()).getValidationResult());
113 assertNotNull(someKey0.toString());
115 PfConceptKey someKey7 = new PfConceptKey(someKey1);
116 assertEquals(244799191, someKey7.hashCode());
117 assertEquals(0, someKey7.compareTo(someKey1));
118 assertEquals(-12, someKey7.compareTo(someKey0));
121 someKey0.compareTo(null);
122 } catch (IllegalArgumentException e) {
123 assertEquals("comparison object may not be null", e.getMessage());
126 assertEquals(0, someKey0.compareTo(someKey0));
127 assertEquals(266127751, someKey0.compareTo(new DummyPfKey()));
129 assertFalse(someKey0.equals(null));
130 assertTrue(someKey0.equals(someKey0));
131 assertFalse(((PfKey) someKey0).equals(new DummyPfKey()));
136 public void testValidation() {
137 PfConceptKey testKey = new PfConceptKey("TheKey", "0.0.1");
138 assertEquals("TheKey:0.0.1", testKey.getId());
141 Field nameField = testKey.getClass().getDeclaredField("name");
142 nameField.setAccessible(true);
143 nameField.set(testKey, "Key Name");
144 PfValidationResult validationResult = new PfValidationResult();
145 testKey.validate(validationResult);
146 nameField.set(testKey, "TheKey");
147 nameField.setAccessible(false);
149 "name invalid-parameter name with value Key Name "
150 + "does not match regular expression [A-Za-z0-9\\-_\\.]+",
151 validationResult.getMessageList().get(0).getMessage());
152 } catch (Exception validationException) {
153 fail("test should not throw an exception");
157 Field versionField = testKey.getClass().getDeclaredField("version");
158 versionField.setAccessible(true);
159 versionField.set(testKey, "Key Version");
160 PfValidationResult validationResult = new PfValidationResult();
161 testKey.validate(validationResult);
162 versionField.set(testKey, "0.0.1");
163 versionField.setAccessible(false);
165 "version invalid-parameter version with value Key Version "
166 + "does not match regular expression [A-Za-z0-9.]+",
167 validationResult.getMessageList().get(0).getMessage());
168 } catch (Exception validationException) {
169 fail("test should not throw an exception");