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.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.junit.Assert.fail;
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());
111 testReferenceKey.equals(null);
112 fail("test should throw an exception here");
113 } catch (Exception iae) {
114 assertEquals("comparison object may not be null", iae.getMessage());
118 testReferenceKey.copyTo(null);
119 fail("test should throw an exception here");
120 } catch (Exception iae) {
121 assertEquals("target may not be null", iae.getMessage());
125 testReferenceKey.copyTo(new AxArtifactKey("Key", "0.0.1"));
126 fail("test should throw an exception here");
127 } catch (Exception iae) {
128 assertEquals("org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey is not an instance of "
129 + "org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey", iae.getMessage());
132 AxReferenceKey targetRefKey = new AxReferenceKey();
133 assertEquals(testReferenceKey, testReferenceKey.copyTo(targetRefKey));
137 public void testValidation() {
138 AxReferenceKey testReferenceKey = new AxReferenceKey();
139 testReferenceKey.setParentArtifactKey(new AxArtifactKey("PN", "0.0.1"));
140 assertEquals("PN:0.0.1", testReferenceKey.getParentArtifactKey().getId());
143 Field parentNameField = testReferenceKey.getClass().getDeclaredField("parentKeyName");
144 parentNameField.setAccessible(true);
145 parentNameField.set(testReferenceKey, "Parent Name");
146 AxValidationResult validationResult = new AxValidationResult();
147 testReferenceKey.validate(validationResult);
148 parentNameField.set(testReferenceKey, "ParentName");
149 parentNameField.setAccessible(false);
151 "parentKeyName invalid-parameter parentKeyName with value Parent Name "
152 + "does not match regular expression [A-Za-z0-9\\-_\\.]+",
153 validationResult.getMessageList().get(0).getMessage());
154 } catch (Exception validationException) {
155 fail("test should not throw an exception");
159 Field parentVersionField = testReferenceKey.getClass().getDeclaredField("parentKeyVersion");
160 parentVersionField.setAccessible(true);
161 parentVersionField.set(testReferenceKey, "Parent Version");
162 AxValidationResult validationResult = new AxValidationResult();
163 testReferenceKey.validate(validationResult);
164 parentVersionField.set(testReferenceKey, "0.0.1");
165 parentVersionField.setAccessible(false);
167 "parentKeyVersion invalid-parameter parentKeyVersion with value Parent Version "
168 + "does not match regular expression [A-Za-z0-9.]+",
169 validationResult.getMessageList().get(0).getMessage());
170 } catch (Exception validationException) {
171 fail("test should not throw an exception");
175 Field parentLocalNameField = testReferenceKey.getClass().getDeclaredField("parentLocalName");
176 parentLocalNameField.setAccessible(true);
177 parentLocalNameField.set(testReferenceKey, "Parent Local Name");
178 AxValidationResult validationResult = new AxValidationResult();
179 testReferenceKey.validate(validationResult);
180 parentLocalNameField.set(testReferenceKey, "ParentLocalName");
181 parentLocalNameField.setAccessible(false);
183 "parentLocalName invalid-parameter parentLocalName with value "
184 + "Parent Local Name does not match regular expression [A-Za-z0-9\\-_\\.]+|^$",
185 validationResult.getMessageList().get(0).getMessage());
186 } catch (Exception validationException) {
187 fail("test should not throw an exception");
191 Field localNameField = testReferenceKey.getClass().getDeclaredField("localName");
192 localNameField.setAccessible(true);
193 localNameField.set(testReferenceKey, "Local Name");
194 AxValidationResult validationResult = new AxValidationResult();
195 testReferenceKey.validate(validationResult);
196 localNameField.set(testReferenceKey, "LocalName");
197 localNameField.setAccessible(false);
199 "localName invalid-parameter localName with value Local Name "
200 + "does not match regular expression [A-Za-z0-9\\-_\\.]+|^$",
201 validationResult.getMessageList().get(0).getMessage());
202 } catch (Exception validationException) {
203 fail("test should not throw an exception");