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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.base;
24 import static org.assertj.core.api.Assertions.assertThat;
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.assertNotEquals;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertTrue;
32 import java.lang.reflect.Field;
33 import org.junit.Test;
34 import org.onap.policy.common.parameters.ValidationResult;
36 public class PfReferenceKeyTest {
38 private static final String PARENT_LOCAL_NAME = "ParentLocalName";
39 private static final String NPKLN = "NPKLN";
40 private static final String LOCAL_NAME = "LocalName";
41 private static final String VERSION002 = "0.0.2";
42 private static final String VERSION001 = "0.0.1";
45 public void testPfReferenceKeyNotNull() {
46 assertNotNull(new PfReferenceKey());
47 assertNotNull(new PfReferenceKey(new PfConceptKey()));
48 assertNotNull(new PfReferenceKey(new PfConceptKey(), LOCAL_NAME));
49 assertNotNull(new PfReferenceKey(new PfReferenceKey()));
50 assertNotNull(new PfReferenceKey(new PfReferenceKey(), LOCAL_NAME));
51 assertNotNull(new PfReferenceKey(new PfConceptKey(), PARENT_LOCAL_NAME, LOCAL_NAME));
52 assertNotNull(new PfReferenceKey("ParentKeyName", VERSION001, LOCAL_NAME));
53 assertNotNull(new PfReferenceKey("ParentKeyName", VERSION001, PARENT_LOCAL_NAME, LOCAL_NAME));
54 assertNotNull(new PfReferenceKey("ParentKeyName:0.0.1:ParentLocalName:LocalName"));
55 assertEquals(PfReferenceKey.getNullKey().getKey(), PfReferenceKey.getNullKey());
56 assertEquals("NULL:0.0.0:NULL:NULL", PfReferenceKey.getNullKey().getId());
58 assertThatThrownBy(() -> new PfReferenceKey(new PfConceptKey(), null))
59 .hasMessage("parameter \"localName\" is null");
63 public void testPfReferenceKey() {
64 PfReferenceKey testReferenceKey = new PfReferenceKey();
65 testReferenceKey.setParentConceptKey(new PfConceptKey("PN", VERSION001));
66 assertEquals("PN:0.0.1", testReferenceKey.getParentConceptKey().getId());
68 assertEquals(0, testReferenceKey.getMajorVersion());
69 assertEquals(0, testReferenceKey.getMinorVersion());
70 assertEquals(1, testReferenceKey.getPatchVersion());
72 assertEquals(1, testReferenceKey.getKeys().size());
73 assertFalse(testReferenceKey.isNullKey());
75 testReferenceKey.setParentReferenceKey(new PfReferenceKey("PN", VERSION001, "LN"));
76 assertEquals("PN:0.0.1:NULL:LN", testReferenceKey.getParentReferenceKey().getId());
78 testReferenceKey.setParentKeyName("NPKN");
79 assertEquals("NPKN", testReferenceKey.getParentKeyName());
81 testReferenceKey.setParentKeyVersion(VERSION001);
82 assertEquals(VERSION001, testReferenceKey.getParentKeyVersion());
84 testReferenceKey.setParentLocalName(NPKLN);
85 assertEquals(NPKLN, testReferenceKey.getParentLocalName());
87 testReferenceKey.setLocalName("NLN");
88 assertEquals("NLN", testReferenceKey.getLocalName());
90 assertThatThrownBy(() -> testReferenceKey.isCompatible(null))
91 .hasMessageMatching("^otherKey is marked .*on.*ull but is null$");
93 assertFalse(testReferenceKey.isCompatible(PfConceptKey.getNullKey()));
94 assertFalse(testReferenceKey.isCompatible(PfReferenceKey.getNullKey()));
95 assertTrue(testReferenceKey.isCompatible(testReferenceKey));
97 assertEquals(PfKey.Compatibility.DIFFERENT, testReferenceKey.getCompatibility(PfConceptKey.getNullKey()));
98 assertEquals(PfKey.Compatibility.DIFFERENT, testReferenceKey.getCompatibility(PfReferenceKey.getNullKey()));
99 assertEquals(PfKey.Compatibility.IDENTICAL, testReferenceKey.getCompatibility(testReferenceKey));
101 assertTrue(testReferenceKey.validate("").isValid());
105 public void testMultiplePfReferenceKey() {
106 PfReferenceKey testReferenceKey = setTestReferenceKey();
107 testReferenceKey.clean();
109 PfReferenceKey clonedReferenceKey = new PfReferenceKey(testReferenceKey);
110 assertEquals("PfReferenceKey(parentKeyName=NPKN, parentKeyVersion=0.0.1, parentLocalName=NPKLN, localName=NLN)",
111 clonedReferenceKey.toString());
113 assertNotEquals(0, testReferenceKey.hashCode());
115 assertEquals(testReferenceKey, (Object) testReferenceKey);
116 assertEquals(testReferenceKey, clonedReferenceKey);
117 assertNotEquals(testReferenceKey, (Object) "Hello");
118 assertNotEquals(testReferenceKey, new PfReferenceKey("PKN", VERSION002, "PLN", "LN"));
119 assertNotEquals(testReferenceKey, new PfReferenceKey("NPKN", VERSION002, "PLN", "LN"));
120 assertNotEquals(testReferenceKey, new PfReferenceKey("NPKN", VERSION001, "PLN", "LN"));
121 assertNotEquals(testReferenceKey, new PfReferenceKey("NPKN", VERSION001, "NPLN", "LN"));
122 assertEquals(testReferenceKey, new PfReferenceKey("NPKN", VERSION001, NPKLN, "NLN"));
124 assertEquals(0, testReferenceKey.compareTo(testReferenceKey));
125 assertEquals(0, testReferenceKey.compareTo(clonedReferenceKey));
126 assertNotEquals(0, testReferenceKey.compareTo(new PfConceptKey()));
127 assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("PKN", VERSION002, "PLN", "LN")));
128 assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", VERSION002, "PLN", "LN")));
129 assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", VERSION001, "PLN", "LN")));
130 assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", VERSION001, "NPLN", "LN")));
131 assertEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", VERSION001, NPKLN, "NLN")));
133 assertNotEquals(testReferenceKey, null);
135 assertThatThrownBy(() -> new PfReferenceKey((PfReferenceKey) null)).isInstanceOf(NullPointerException.class);
137 assertEquals(testReferenceKey, new PfReferenceKey(testReferenceKey));
141 public void testValidation() throws Exception {
142 PfReferenceKey testReferenceKey = new PfReferenceKey();
143 testReferenceKey.setParentConceptKey(new PfConceptKey("PN", VERSION001));
144 assertEquals("PN:0.0.1", testReferenceKey.getParentConceptKey().getId());
146 Field parentNameField = testReferenceKey.getClass().getDeclaredField("parentKeyName");
147 parentNameField.setAccessible(true);
148 parentNameField.set(testReferenceKey, "Parent Name");
149 ValidationResult validationResult = testReferenceKey.validate("");
150 parentNameField.set(testReferenceKey, "ParentName");
151 parentNameField.setAccessible(false);
152 assertThat(validationResult.getResult()).contains("\"parentKeyName\"")
153 .contains("does not match regular expression " + PfKey.NAME_REGEXP);
155 Field parentVersionField = testReferenceKey.getClass().getDeclaredField("parentKeyVersion");
156 parentVersionField.setAccessible(true);
157 parentVersionField.set(testReferenceKey, "Parent Version");
158 ValidationResult validationResult2 = testReferenceKey.validate("");
159 parentVersionField.set(testReferenceKey, VERSION001);
160 parentVersionField.setAccessible(false);
161 assertThat(validationResult2.getResult()).contains("\"parentKeyVersion\"")
162 .contains("does not match regular expression " + PfKey.VERSION_REGEXP);
164 Field parentLocalNameField = testReferenceKey.getClass().getDeclaredField("parentLocalName");
165 parentLocalNameField.setAccessible(true);
166 parentLocalNameField.set(testReferenceKey, "Parent Local Name");
167 ValidationResult validationResult3 = testReferenceKey.validate("");
168 parentLocalNameField.set(testReferenceKey, PARENT_LOCAL_NAME);
169 parentLocalNameField.setAccessible(false);
170 assertThat(validationResult3.getResult()).contains("\"parentLocalName\"")
171 .contains("does not match regular expression [A-Za-z0-9\\-_\\.]+|^$");
173 Field localNameField = testReferenceKey.getClass().getDeclaredField("localName");
174 localNameField.setAccessible(true);
175 localNameField.set(testReferenceKey, "Local Name");
176 ValidationResult validationResult4 = testReferenceKey.validate("");
177 localNameField.set(testReferenceKey, LOCAL_NAME);
178 localNameField.setAccessible(false);
179 assertThat(validationResult4.getResult()).contains("\"localName\"")
180 .contains("does not match regular expression [A-Za-z0-9\\-_\\.]+|^$");
183 private PfReferenceKey setTestReferenceKey() {
184 PfReferenceKey testReferenceKey = new PfReferenceKey();
185 testReferenceKey.setParentConceptKey(new PfConceptKey("PN", VERSION001));
186 testReferenceKey.setParentReferenceKey(new PfReferenceKey("PN", VERSION001, "LN"));
187 testReferenceKey.setParentKeyName("NPKN");
188 testReferenceKey.setParentKeyVersion(VERSION001);
189 testReferenceKey.setParentLocalName(NPKLN);
190 testReferenceKey.setLocalName("NLN");
192 return testReferenceKey;