2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 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.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
31 import java.lang.reflect.Field;
32 import org.junit.Test;
34 public class PfReferenceKeyTest {
36 private static final String PARENT_LOCAL_NAME = "ParentLocalName";
37 private static final String NPKLN = "NPKLN";
38 private static final String LOCAL_NAME = "LocalName";
39 private static final String VERSION002 = "0.0.2";
40 private static final String VERSION001 = "0.0.1";
43 public void testPfReferenceKey() {
44 assertNotNull(new PfReferenceKey());
45 assertNotNull(new PfReferenceKey(new PfConceptKey()));
46 assertNotNull(new PfReferenceKey(new PfConceptKey(), LOCAL_NAME));
47 assertNotNull(new PfReferenceKey(new PfReferenceKey()));
48 assertNotNull(new PfReferenceKey(new PfReferenceKey(), LOCAL_NAME));
49 assertNotNull(new PfReferenceKey(new PfConceptKey(), PARENT_LOCAL_NAME, LOCAL_NAME));
50 assertNotNull(new PfReferenceKey("ParentKeyName", VERSION001, LOCAL_NAME));
51 assertNotNull(new PfReferenceKey("ParentKeyName", VERSION001, PARENT_LOCAL_NAME, LOCAL_NAME));
52 assertNotNull(new PfReferenceKey("ParentKeyName:0.0.1:ParentLocalName:LocalName"));
53 assertEquals(PfReferenceKey.getNullKey().getKey(), PfReferenceKey.getNullKey());
54 assertEquals("NULL:0.0.0:NULL:NULL", PfReferenceKey.getNullKey().getId());
56 assertThatThrownBy(() -> new PfReferenceKey(new PfConceptKey(), null))
57 .hasMessage("parameter \"localName\" is null");
59 PfReferenceKey testReferenceKey = new PfReferenceKey();
60 testReferenceKey.setParentConceptKey(new PfConceptKey("PN", VERSION001));
61 assertEquals("PN:0.0.1", testReferenceKey.getParentConceptKey().getId());
63 assertEquals(0, testReferenceKey.getMajorVersion());
64 assertEquals(0, testReferenceKey.getMinorVersion());
65 assertEquals(1, testReferenceKey.getPatchVersion());
67 assertEquals(1, testReferenceKey.getKeys().size());
68 assertFalse(testReferenceKey.isNullKey());
70 testReferenceKey.setParentReferenceKey(new PfReferenceKey("PN", VERSION001, "LN"));
71 assertEquals("PN:0.0.1:NULL:LN", testReferenceKey.getParentReferenceKey().getId());
73 testReferenceKey.setParentKeyName("NPKN");
74 assertEquals("NPKN", testReferenceKey.getParentKeyName());
76 testReferenceKey.setParentKeyVersion(VERSION001);
77 assertEquals(VERSION001, testReferenceKey.getParentKeyVersion());
79 testReferenceKey.setParentLocalName(NPKLN);
80 assertEquals(NPKLN, testReferenceKey.getParentLocalName());
82 testReferenceKey.setLocalName("NLN");
83 assertEquals("NLN", testReferenceKey.getLocalName());
85 assertThatThrownBy(() -> testReferenceKey.isCompatible(null))
86 .hasMessage("otherKey is marked @NonNull but is null");
88 assertFalse(testReferenceKey.isCompatible(PfConceptKey.getNullKey()));
89 assertFalse(testReferenceKey.isCompatible(PfReferenceKey.getNullKey()));
90 assertTrue(testReferenceKey.isCompatible(testReferenceKey));
92 assertEquals(PfKey.Compatibility.DIFFERENT, testReferenceKey.getCompatibility(PfConceptKey.getNullKey()));
93 assertEquals(PfKey.Compatibility.DIFFERENT, testReferenceKey.getCompatibility(PfReferenceKey.getNullKey()));
94 assertEquals(PfKey.Compatibility.IDENTICAL, testReferenceKey.getCompatibility(testReferenceKey));
96 PfValidationResult result = new PfValidationResult();
97 result = testReferenceKey.validate(result);
98 assertEquals(PfValidationResult.ValidationResult.VALID, result.getValidationResult());
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 assertFalse(testReferenceKey.hashCode() == 0);
108 assertTrue(testReferenceKey.equals(testReferenceKey));
109 assertTrue(testReferenceKey.equals(clonedReferenceKey));
110 assertFalse(testReferenceKey.equals("Hello"));
111 assertFalse(testReferenceKey.equals(new PfReferenceKey("PKN", VERSION002, "PLN", "LN")));
112 assertFalse(testReferenceKey.equals(new PfReferenceKey("NPKN", VERSION002, "PLN", "LN")));
113 assertFalse(testReferenceKey.equals(new PfReferenceKey("NPKN", VERSION001, "PLN", "LN")));
114 assertFalse(testReferenceKey.equals(new PfReferenceKey("NPKN", VERSION001, "NPLN", "LN")));
115 assertTrue(testReferenceKey.equals(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 assertFalse(testReferenceKey.equals(null));
128 assertThatThrownBy(() -> testReferenceKey.copyTo(null)).hasMessage("target may not be null");
130 assertThatThrownBy(() -> testReferenceKey.copyTo(new PfConceptKey("Key", VERSION001)))
131 .hasMessage("org.onap.policy.models.base.PfConceptKey"
132 + " is not an instance of org.onap.policy.models.base.PfReferenceKey");
134 PfReferenceKey targetRefKey = new PfReferenceKey();
135 assertEquals(testReferenceKey, testReferenceKey.copyTo(targetRefKey));
139 public void testValidation() throws Exception {
140 PfReferenceKey testReferenceKey = new PfReferenceKey();
141 testReferenceKey.setParentConceptKey(new PfConceptKey("PN", VERSION001));
142 assertEquals("PN:0.0.1", testReferenceKey.getParentConceptKey().getId());
144 Field parentNameField = testReferenceKey.getClass().getDeclaredField("parentKeyName");
145 parentNameField.setAccessible(true);
146 parentNameField.set(testReferenceKey, "Parent Name");
147 PfValidationResult validationResult = new PfValidationResult();
148 testReferenceKey.validate(validationResult);
149 parentNameField.set(testReferenceKey, "ParentName");
150 parentNameField.setAccessible(false);
152 "parentKeyName invalid-parameter parentKeyName with value Parent Name "
153 + "does not match regular expression " + PfKey.NAME_REGEXP,
154 validationResult.getMessageList().get(0).getMessage());
156 Field parentVersionField = testReferenceKey.getClass().getDeclaredField("parentKeyVersion");
157 parentVersionField.setAccessible(true);
158 parentVersionField.set(testReferenceKey, "Parent Version");
159 PfValidationResult validationResult2 = new PfValidationResult();
160 testReferenceKey.validate(validationResult2);
161 parentVersionField.set(testReferenceKey, VERSION001);
162 parentVersionField.setAccessible(false);
164 "parentKeyVersion invalid-parameter parentKeyVersion with value Parent Version "
165 + "does not match regular expression " + PfKey.VERSION_REGEXP,
166 validationResult2.getMessageList().get(0).getMessage());
168 Field parentLocalNameField = testReferenceKey.getClass().getDeclaredField("parentLocalName");
169 parentLocalNameField.setAccessible(true);
170 parentLocalNameField.set(testReferenceKey, "Parent Local Name");
171 PfValidationResult validationResult3 = new PfValidationResult();
172 testReferenceKey.validate(validationResult3);
173 parentLocalNameField.set(testReferenceKey, PARENT_LOCAL_NAME);
174 parentLocalNameField.setAccessible(false);
176 "parentLocalName invalid-parameter parentLocalName with value "
177 + "Parent Local Name does not match regular expression [A-Za-z0-9\\-_\\.]+|^$",
178 validationResult3.getMessageList().get(0).getMessage());
180 Field localNameField = testReferenceKey.getClass().getDeclaredField("localName");
181 localNameField.setAccessible(true);
182 localNameField.set(testReferenceKey, "Local Name");
183 PfValidationResult validationResult4 = new PfValidationResult();
184 testReferenceKey.validate(validationResult4);
185 localNameField.set(testReferenceKey, LOCAL_NAME);
186 localNameField.setAccessible(false);
188 "localName invalid-parameter localName with value Local Name "
189 + "does not match regular expression [A-Za-z0-9\\-_\\.]+|^$",
190 validationResult4.getMessageList().get(0).getMessage());