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.assertNotEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
30 import java.lang.reflect.Field;
32 import org.junit.Test;
34 public class PfReferenceKeyTest {
37 public void testPfReferenceKey() {
38 assertNotNull(new PfReferenceKey());
39 assertNotNull(new PfReferenceKey(new PfConceptKey()));
40 assertNotNull(new PfReferenceKey(new PfConceptKey(), "LocalName"));
41 assertNotNull(new PfReferenceKey(new PfReferenceKey()));
42 assertNotNull(new PfReferenceKey(new PfReferenceKey(), "LocalName"));
43 assertNotNull(new PfReferenceKey(new PfConceptKey(), "ParentLocalName", "LocalName"));
44 assertNotNull(new PfReferenceKey("ParentKeyName", "0.0.1", "LocalName"));
45 assertNotNull(new PfReferenceKey("ParentKeyName", "0.0.1", "ParentLocalName", "LocalName"));
46 assertNotNull(new PfReferenceKey("ParentKeyName:0.0.1:ParentLocalName:LocalName"));
47 assertEquals(PfReferenceKey.getNullKey().getKey(), PfReferenceKey.getNullKey());
48 assertEquals("NULL:0.0.0:NULL:NULL", PfReferenceKey.getNullKey().getId());
51 new PfReferenceKey(new PfConceptKey(), null);
52 fail("test should throw an exception");
53 } catch (Exception exc) {
54 assertEquals("parameter \"localName\" is null", exc.getMessage());
57 PfReferenceKey testReferenceKey = new PfReferenceKey();
58 testReferenceKey.setParentConceptKey(new PfConceptKey("PN", "0.0.1"));
59 assertEquals("PN:0.0.1", testReferenceKey.getParentConceptKey().getId());
61 assertEquals(0, testReferenceKey.getMajorVersion());
62 assertEquals(0, testReferenceKey.getMinorVersion());
63 assertEquals(1, testReferenceKey.getPatchVersion());
65 assertEquals(1, testReferenceKey.getKeys().size());
66 assertFalse(testReferenceKey.isNullKey());
68 testReferenceKey.setParentReferenceKey(new PfReferenceKey("PN", "0.0.1", "LN"));
69 assertEquals("PN:0.0.1:NULL:LN", testReferenceKey.getParentReferenceKey().getId());
71 testReferenceKey.setParentKeyName("NPKN");
72 assertEquals("NPKN", testReferenceKey.getParentKeyName());
74 testReferenceKey.setParentKeyVersion("0.0.1");
75 assertEquals("0.0.1", testReferenceKey.getParentKeyVersion());
77 testReferenceKey.setParentLocalName("NPKLN");
78 assertEquals("NPKLN", testReferenceKey.getParentLocalName());
80 testReferenceKey.setLocalName("NLN");
81 assertEquals("NLN", testReferenceKey.getLocalName());
84 testReferenceKey.isCompatible(null);
85 fail("test should throw an exception here");
86 } catch (Exception exc) {
87 assertEquals("otherKey is marked @NonNull but is null", exc.getMessage());
90 assertFalse(testReferenceKey.isCompatible(PfConceptKey.getNullKey()));
91 assertFalse(testReferenceKey.isCompatible(PfReferenceKey.getNullKey()));
92 assertTrue(testReferenceKey.isCompatible(testReferenceKey));
94 assertEquals(PfKey.Compatibility.DIFFERENT, testReferenceKey.getCompatibility(PfConceptKey.getNullKey()));
95 assertEquals(PfKey.Compatibility.DIFFERENT, testReferenceKey.getCompatibility(PfReferenceKey.getNullKey()));
96 assertEquals(PfKey.Compatibility.IDENTICAL, testReferenceKey.getCompatibility(testReferenceKey));
98 PfValidationResult result = new PfValidationResult();
99 result = testReferenceKey.validate(result);
100 assertEquals(PfValidationResult.ValidationResult.VALID, result.getValidationResult());
102 testReferenceKey.clean();
104 PfReferenceKey clonedReferenceKey = new PfReferenceKey(testReferenceKey);
105 assertEquals("PfReferenceKey(parentKeyName=NPKN, parentKeyVersion=0.0.1, parentLocalName=NPKLN, localName=NLN)",
106 clonedReferenceKey.toString());
108 assertFalse(testReferenceKey.hashCode() == 0);
110 assertTrue(testReferenceKey.equals(testReferenceKey));
111 assertTrue(testReferenceKey.equals(clonedReferenceKey));
112 assertFalse(testReferenceKey.equals("Hello"));
113 assertFalse(testReferenceKey.equals(new PfReferenceKey("PKN", "0.0.2", "PLN", "LN")));
114 assertFalse(testReferenceKey.equals(new PfReferenceKey("NPKN", "0.0.2", "PLN", "LN")));
115 assertFalse(testReferenceKey.equals(new PfReferenceKey("NPKN", "0.0.1", "PLN", "LN")));
116 assertFalse(testReferenceKey.equals(new PfReferenceKey("NPKN", "0.0.1", "NPLN", "LN")));
117 assertTrue(testReferenceKey.equals(new PfReferenceKey("NPKN", "0.0.1", "NPKLN", "NLN")));
119 assertEquals(0, testReferenceKey.compareTo(testReferenceKey));
120 assertEquals(0, testReferenceKey.compareTo(clonedReferenceKey));
121 assertNotEquals(0, testReferenceKey.compareTo(new PfConceptKey()));
122 assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("PKN", "0.0.2", "PLN", "LN")));
123 assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", "0.0.2", "PLN", "LN")));
124 assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", "0.0.1", "PLN", "LN")));
125 assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", "0.0.1", "NPLN", "LN")));
126 assertEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", "0.0.1", "NPKLN", "NLN")));
128 assertFalse(testReferenceKey.equals(null));
131 testReferenceKey.copyTo(null);
132 fail("test should throw an exception here");
133 } catch (Exception iae) {
134 assertEquals("target may not be null", iae.getMessage());
138 testReferenceKey.copyTo(new PfConceptKey("Key", "0.0.1"));
139 fail("test should throw an exception here");
140 } catch (Exception iae) {
141 assertEquals("org.onap.policy.models.base.PfConceptKey"
142 + " is not an instance of org.onap.policy.models.base.PfReferenceKey", iae.getMessage());
145 PfReferenceKey targetRefKey = new PfReferenceKey();
146 assertEquals(testReferenceKey, testReferenceKey.copyTo(targetRefKey));
150 public void testValidation() {
151 PfReferenceKey testReferenceKey = new PfReferenceKey();
152 testReferenceKey.setParentConceptKey(new PfConceptKey("PN", "0.0.1"));
153 assertEquals("PN:0.0.1", testReferenceKey.getParentConceptKey().getId());
156 Field parentNameField = testReferenceKey.getClass().getDeclaredField("parentKeyName");
157 parentNameField.setAccessible(true);
158 parentNameField.set(testReferenceKey, "Parent Name");
159 PfValidationResult validationResult = new PfValidationResult();
160 testReferenceKey.validate(validationResult);
161 parentNameField.set(testReferenceKey, "ParentName");
162 parentNameField.setAccessible(false);
164 "parentKeyName invalid-parameter parentKeyName with value Parent Name "
165 + "does not match regular expression " + PfKey.NAME_REGEXP,
166 validationResult.getMessageList().get(0).getMessage());
167 } catch (Exception validationException) {
168 fail("test should not throw an exception");
172 Field parentVersionField = testReferenceKey.getClass().getDeclaredField("parentKeyVersion");
173 parentVersionField.setAccessible(true);
174 parentVersionField.set(testReferenceKey, "Parent Version");
175 PfValidationResult validationResult = new PfValidationResult();
176 testReferenceKey.validate(validationResult);
177 parentVersionField.set(testReferenceKey, "0.0.1");
178 parentVersionField.setAccessible(false);
180 "parentKeyVersion invalid-parameter parentKeyVersion with value Parent Version "
181 + "does not match regular expression " + PfKey.VERSION_REGEXP,
182 validationResult.getMessageList().get(0).getMessage());
183 } catch (Exception validationException) {
184 fail("test should not throw an exception");
188 Field parentLocalNameField = testReferenceKey.getClass().getDeclaredField("parentLocalName");
189 parentLocalNameField.setAccessible(true);
190 parentLocalNameField.set(testReferenceKey, "Parent Local Name");
191 PfValidationResult validationResult = new PfValidationResult();
192 testReferenceKey.validate(validationResult);
193 parentLocalNameField.set(testReferenceKey, "ParentLocalName");
194 parentLocalNameField.setAccessible(false);
196 "parentLocalName invalid-parameter parentLocalName with value "
197 + "Parent Local Name does not match regular expression [A-Za-z0-9\\-_\\.]+|^$",
198 validationResult.getMessageList().get(0).getMessage());
199 } catch (Exception validationException) {
200 fail("test should not throw an exception");
204 Field localNameField = testReferenceKey.getClass().getDeclaredField("localName");
205 localNameField.setAccessible(true);
206 localNameField.set(testReferenceKey, "Local Name");
207 PfValidationResult validationResult = new PfValidationResult();
208 testReferenceKey.validate(validationResult);
209 localNameField.set(testReferenceKey, "LocalName");
210 localNameField.setAccessible(false);
212 "localName invalid-parameter localName with value Local Name "
213 + "does not match regular expression [A-Za-z0-9\\-_\\.]+|^$",
214 validationResult.getMessageList().get(0).getMessage());
215 } catch (Exception validationException) {
216 fail("test should not throw an exception");