2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 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 testPfReferenceKey() {
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");
61 PfReferenceKey testReferenceKey = new PfReferenceKey();
62 testReferenceKey.setParentConceptKey(new PfConceptKey("PN", VERSION001));
63 assertEquals("PN:0.0.1", testReferenceKey.getParentConceptKey().getId());
65 assertEquals(0, testReferenceKey.getMajorVersion());
66 assertEquals(0, testReferenceKey.getMinorVersion());
67 assertEquals(1, testReferenceKey.getPatchVersion());
69 assertEquals(1, testReferenceKey.getKeys().size());
70 assertFalse(testReferenceKey.isNullKey());
72 testReferenceKey.setParentReferenceKey(new PfReferenceKey("PN", VERSION001, "LN"));
73 assertEquals("PN:0.0.1:NULL:LN", testReferenceKey.getParentReferenceKey().getId());
75 testReferenceKey.setParentKeyName("NPKN");
76 assertEquals("NPKN", testReferenceKey.getParentKeyName());
78 testReferenceKey.setParentKeyVersion(VERSION001);
79 assertEquals(VERSION001, testReferenceKey.getParentKeyVersion());
81 testReferenceKey.setParentLocalName(NPKLN);
82 assertEquals(NPKLN, testReferenceKey.getParentLocalName());
84 testReferenceKey.setLocalName("NLN");
85 assertEquals("NLN", testReferenceKey.getLocalName());
87 assertThatThrownBy(() -> testReferenceKey.isCompatible(null))
88 .hasMessageMatching("^otherKey is marked .*on.*ull but is null$");
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 assertTrue(testReferenceKey.validate("").isValid());
100 testReferenceKey.clean();
102 PfReferenceKey clonedReferenceKey = new PfReferenceKey(testReferenceKey);
103 assertEquals("PfReferenceKey(parentKeyName=NPKN, parentKeyVersion=0.0.1, parentLocalName=NPKLN, localName=NLN)",
104 clonedReferenceKey.toString());
106 assertNotEquals(0, testReferenceKey.hashCode());
108 assertEquals(testReferenceKey, (Object) testReferenceKey);
109 assertEquals(testReferenceKey, clonedReferenceKey);
110 assertNotEquals(testReferenceKey, (Object) "Hello");
111 assertNotEquals(testReferenceKey, new PfReferenceKey("PKN", VERSION002, "PLN", "LN"));
112 assertNotEquals(testReferenceKey, new PfReferenceKey("NPKN", VERSION002, "PLN", "LN"));
113 assertNotEquals(testReferenceKey, new PfReferenceKey("NPKN", VERSION001, "PLN", "LN"));
114 assertNotEquals(testReferenceKey, new PfReferenceKey("NPKN", VERSION001, "NPLN", "LN"));
115 assertEquals(testReferenceKey, new PfReferenceKey("NPKN", VERSION001, NPKLN, "NLN"));
117 assertEquals(0, testReferenceKey.compareTo(testReferenceKey));
118 assertEquals(0, testReferenceKey.compareTo(clonedReferenceKey));
119 assertNotEquals(0, testReferenceKey.compareTo(new PfConceptKey()));
120 assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("PKN", VERSION002, "PLN", "LN")));
121 assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", VERSION002, "PLN", "LN")));
122 assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", VERSION001, "PLN", "LN")));
123 assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", VERSION001, "NPLN", "LN")));
124 assertEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", VERSION001, NPKLN, "NLN")));
126 assertNotEquals(testReferenceKey, null);
128 assertThatThrownBy(() -> new PfReferenceKey((PfReferenceKey) null)).isInstanceOf(NullPointerException.class);
130 assertEquals(testReferenceKey, new PfReferenceKey(testReferenceKey));
134 public void testValidation() throws Exception {
135 PfReferenceKey testReferenceKey = new PfReferenceKey();
136 testReferenceKey.setParentConceptKey(new PfConceptKey("PN", VERSION001));
137 assertEquals("PN:0.0.1", testReferenceKey.getParentConceptKey().getId());
139 Field parentNameField = testReferenceKey.getClass().getDeclaredField("parentKeyName");
140 parentNameField.setAccessible(true);
141 parentNameField.set(testReferenceKey, "Parent Name");
142 ValidationResult validationResult = testReferenceKey.validate("");
143 parentNameField.set(testReferenceKey, "ParentName");
144 parentNameField.setAccessible(false);
145 assertThat(validationResult.getResult()).contains("\"parentKeyName\"")
146 .contains("does not match regular expression " + PfKey.NAME_REGEXP);
148 Field parentVersionField = testReferenceKey.getClass().getDeclaredField("parentKeyVersion");
149 parentVersionField.setAccessible(true);
150 parentVersionField.set(testReferenceKey, "Parent Version");
151 ValidationResult validationResult2 = testReferenceKey.validate("");
152 parentVersionField.set(testReferenceKey, VERSION001);
153 parentVersionField.setAccessible(false);
154 assertThat(validationResult2.getResult()).contains("\"parentKeyVersion\"")
155 .contains("does not match regular expression " + PfKey.VERSION_REGEXP);
157 Field parentLocalNameField = testReferenceKey.getClass().getDeclaredField("parentLocalName");
158 parentLocalNameField.setAccessible(true);
159 parentLocalNameField.set(testReferenceKey, "Parent Local Name");
160 ValidationResult validationResult3 = testReferenceKey.validate("");
161 parentLocalNameField.set(testReferenceKey, PARENT_LOCAL_NAME);
162 parentLocalNameField.setAccessible(false);
163 assertThat(validationResult3.getResult()).contains("\"parentLocalName\"")
164 .contains("does not match regular expression [A-Za-z0-9\\-_\\.]+|^$");
166 Field localNameField = testReferenceKey.getClass().getDeclaredField("localName");
167 localNameField.setAccessible(true);
168 localNameField.set(testReferenceKey, "Local Name");
169 ValidationResult validationResult4 = testReferenceKey.validate("");
170 localNameField.set(testReferenceKey, LOCAL_NAME);
171 localNameField.setAccessible(false);
172 assertThat(validationResult4.getResult()).contains("\"localName\"")
173 .contains("does not match regular expression [A-Za-z0-9\\-_\\.]+|^$");