2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
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.apex.model.basicmodel.concepts;
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;
35 public class AxReferenceKeyTest {
38 public void testAxReferenceKey() {
39 assertNotNull(new AxReferenceKey());
40 assertNotNull(new AxReferenceKey(new AxArtifactKey()));
41 assertNotNull(new AxReferenceKey(new AxArtifactKey(), "LocalName"));
42 assertNotNull(new AxReferenceKey(new AxReferenceKey()));
43 assertNotNull(new AxReferenceKey(new AxReferenceKey(), "LocalName"));
44 assertNotNull(new AxReferenceKey(new AxArtifactKey(), "ParentLocalName", "LocalName"));
45 assertNotNull(new AxReferenceKey("ParentKeyName", "0.0.1", "LocalName"));
46 assertNotNull(new AxReferenceKey("ParentKeyName", "0.0.1", "ParentLocalName", "LocalName"));
47 assertNotNull(new AxReferenceKey("ParentKeyName:0.0.1:ParentLocalName:LocalName"));
48 assertEquals(AxReferenceKey.getNullKey().getKey(), AxReferenceKey.getNullKey());
49 assertEquals("NULL:0.0.0:NULL:NULL", AxReferenceKey.getNullKey().getId());
51 AxReferenceKey testReferenceKey = new AxReferenceKey();
52 testReferenceKey.setParentArtifactKey(new AxArtifactKey("PN", "0.0.1"));
53 assertEquals("PN:0.0.1", testReferenceKey.getParentArtifactKey().getId());
55 testReferenceKey.setParentReferenceKey(new AxReferenceKey("PN", "0.0.1", "LN"));
56 assertEquals("PN:0.0.1:NULL:LN", testReferenceKey.getParentReferenceKey().getId());
58 testReferenceKey.setParentKeyName("NPKN");
59 assertEquals("NPKN", testReferenceKey.getParentKeyName());
61 testReferenceKey.setParentKeyVersion("0.0.1");
62 assertEquals("0.0.1", testReferenceKey.getParentKeyVersion());
64 testReferenceKey.setParentLocalName("NPKLN");
65 assertEquals("NPKLN", testReferenceKey.getParentLocalName());
67 testReferenceKey.setLocalName("NLN");
68 assertEquals("NLN", testReferenceKey.getLocalName());
70 assertFalse(testReferenceKey.isCompatible(AxArtifactKey.getNullKey()));
71 assertFalse(testReferenceKey.isCompatible(AxReferenceKey.getNullKey()));
72 assertTrue(testReferenceKey.isCompatible(testReferenceKey));
74 assertEquals(AxKey.Compatibility.DIFFERENT, testReferenceKey.getCompatibility(AxArtifactKey.getNullKey()));
75 assertEquals(AxKey.Compatibility.DIFFERENT, testReferenceKey.getCompatibility(AxReferenceKey.getNullKey()));
76 assertEquals(AxKey.Compatibility.IDENTICAL, testReferenceKey.getCompatibility(testReferenceKey));
78 AxValidationResult result = new AxValidationResult();
79 result = testReferenceKey.validate(result);
80 assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult());
82 testReferenceKey.clean();
84 AxReferenceKey clonedReferenceKey = new AxReferenceKey(testReferenceKey);
85 assertEquals("AxReferenceKey:(parentKeyName=NPKN,parentKeyVersion=0.0.1,parentLocalName=NPKLN,localName=NLN)",
86 clonedReferenceKey.toString());
88 assertNotEquals(0, testReferenceKey.hashCode());
90 assertEquals(testReferenceKey, testReferenceKey);
91 assertEquals(testReferenceKey, clonedReferenceKey);
92 assertNotEquals(testReferenceKey, (Object) "Hello");
93 assertNotEquals(testReferenceKey, new AxReferenceKey("PKN", "0.0.2", "PLN", "LN"));
94 assertNotEquals(testReferenceKey, new AxReferenceKey("NPKN", "0.0.2", "PLN", "LN"));
95 assertNotEquals(testReferenceKey, new AxReferenceKey("NPKN", "0.0.1", "PLN", "LN"));
96 assertNotEquals(testReferenceKey, new AxReferenceKey("NPKN", "0.0.1", "NPLN", "LN"));
97 assertEquals(testReferenceKey, new AxReferenceKey("NPKN", "0.0.1", "NPKLN", "NLN"));
99 assertEquals(0, testReferenceKey.compareTo(testReferenceKey));
100 assertEquals(0, testReferenceKey.compareTo(clonedReferenceKey));
101 assertNotEquals(0, testReferenceKey.compareTo(new AxArtifactKey()));
102 assertNotEquals(0, testReferenceKey.compareTo(new AxReferenceKey("PKN", "0.0.2", "PLN", "LN")));
103 assertNotEquals(0, testReferenceKey.compareTo(new AxReferenceKey("NPKN", "0.0.2", "PLN", "LN")));
104 assertNotEquals(0, testReferenceKey.compareTo(new AxReferenceKey("NPKN", "0.0.1", "PLN", "LN")));
105 assertNotEquals(0, testReferenceKey.compareTo(new AxReferenceKey("NPKN", "0.0.1", "NPLN", "LN")));
106 assertEquals(0, testReferenceKey.compareTo(new AxReferenceKey("NPKN", "0.0.1", "NPKLN", "NLN")));
108 assertNotNull(testReferenceKey.getKeys());
110 assertThatThrownBy(() -> testReferenceKey.equals(null))
111 .hasMessage("comparison object may not be null");
112 assertThatThrownBy(() -> testReferenceKey.copyTo(null))
113 .hasMessage("target may not be null");
114 assertThatThrownBy(() -> testReferenceKey.copyTo(new AxArtifactKey("Key", "0.0.1")))
115 .hasMessage("org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey is not an instance of "
116 + "org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey");
117 AxReferenceKey targetRefKey = new AxReferenceKey();
118 assertEquals(testReferenceKey, testReferenceKey.copyTo(targetRefKey));
122 public void testValidation() throws IllegalArgumentException, IllegalAccessException,
123 NoSuchFieldException, SecurityException {
124 AxReferenceKey testReferenceKey = new AxReferenceKey();
125 testReferenceKey.setParentArtifactKey(new AxArtifactKey("PN", "0.0.1"));
126 assertEquals("PN:0.0.1", testReferenceKey.getParentArtifactKey().getId());
128 Field parentNameField = testReferenceKey.getClass().getDeclaredField("parentKeyName");
129 parentNameField.setAccessible(true);
130 parentNameField.set(testReferenceKey, "Parent Name");
131 AxValidationResult validationResult = new AxValidationResult();
132 testReferenceKey.validate(validationResult);
133 parentNameField.set(testReferenceKey, "ParentName");
134 parentNameField.setAccessible(false);
136 "parentKeyName invalid-parameter parentKeyName with value Parent Name "
137 + "does not match regular expression [A-Za-z0-9\\-_\\.]+",
138 validationResult.getMessageList().get(0).getMessage());
140 Field parentVersionField = testReferenceKey.getClass().getDeclaredField("parentKeyVersion");
141 parentVersionField.setAccessible(true);
142 parentVersionField.set(testReferenceKey, "Parent Version");
143 AxValidationResult validationResultPV = new AxValidationResult();
144 testReferenceKey.validate(validationResultPV);
145 parentVersionField.set(testReferenceKey, "0.0.1");
146 parentVersionField.setAccessible(false);
148 "parentKeyVersion invalid-parameter parentKeyVersion with value Parent Version "
149 + "does not match regular expression [A-Za-z0-9.]+",
150 validationResultPV.getMessageList().get(0).getMessage());
152 Field parentLocalNameField = testReferenceKey.getClass().getDeclaredField("parentLocalName");
153 parentLocalNameField.setAccessible(true);
154 parentLocalNameField.set(testReferenceKey, "Parent Local Name");
155 AxValidationResult validationResultPL = new AxValidationResult();
156 testReferenceKey.validate(validationResultPL);
157 parentLocalNameField.set(testReferenceKey, "ParentLocalName");
158 parentLocalNameField.setAccessible(false);
160 "parentLocalName invalid-parameter parentLocalName with value "
161 + "Parent Local Name does not match regular expression [A-Za-z0-9\\-_\\.]+|^$",
162 validationResultPL.getMessageList().get(0).getMessage());
164 Field localNameField = testReferenceKey.getClass().getDeclaredField("localName");
165 localNameField.setAccessible(true);
166 localNameField.set(testReferenceKey, "Local Name");
167 AxValidationResult validationResultLN = new AxValidationResult();
168 testReferenceKey.validate(validationResultLN);
169 localNameField.set(testReferenceKey, "LocalName");
170 localNameField.setAccessible(false);
172 "localName invalid-parameter localName with value Local Name "
173 + "does not match regular expression [A-Za-z0-9\\-_\\.]+|^$",
174 validationResultLN.getMessageList().get(0).getMessage());