2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 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;
33 import org.junit.Test;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
39 public class AxReferenceKeyTest {
42 public void testAxReferenceKey() {
43 assertNotNull(new AxReferenceKey());
44 assertNotNull(new AxReferenceKey(new AxArtifactKey()));
45 assertNotNull(new AxReferenceKey(new AxArtifactKey(), "LocalName"));
46 assertNotNull(new AxReferenceKey(new AxReferenceKey()));
47 assertNotNull(new AxReferenceKey(new AxReferenceKey(), "LocalName"));
48 assertNotNull(new AxReferenceKey(new AxArtifactKey(), "ParentLocalName", "LocalName"));
49 assertNotNull(new AxReferenceKey("ParentKeyName", "0.0.1", "LocalName"));
50 assertNotNull(new AxReferenceKey("ParentKeyName", "0.0.1", "ParentLocalName", "LocalName"));
51 assertNotNull(new AxReferenceKey("ParentKeyName:0.0.1:ParentLocalName:LocalName"));
52 assertEquals(AxReferenceKey.getNullKey().getKey(), AxReferenceKey.getNullKey());
53 assertEquals("NULL:0.0.0:NULL:NULL", AxReferenceKey.getNullKey().getId());
55 AxReferenceKey testReferenceKey = new AxReferenceKey();
56 testReferenceKey.setParentArtifactKey(new AxArtifactKey("PN", "0.0.1"));
57 assertEquals("PN:0.0.1", testReferenceKey.getParentArtifactKey().getId());
59 testReferenceKey.setParentReferenceKey(new AxReferenceKey("PN", "0.0.1", "LN"));
60 assertEquals("PN:0.0.1:NULL:LN", testReferenceKey.getParentReferenceKey().getId());
62 testReferenceKey.setParentKeyName("NPKN");
63 assertEquals("NPKN", testReferenceKey.getParentKeyName());
65 testReferenceKey.setParentKeyVersion("0.0.1");
66 assertEquals("0.0.1", testReferenceKey.getParentKeyVersion());
68 testReferenceKey.setParentLocalName("NPKLN");
69 assertEquals("NPKLN", testReferenceKey.getParentLocalName());
71 testReferenceKey.setLocalName("NLN");
72 assertEquals("NLN", testReferenceKey.getLocalName());
74 assertFalse(testReferenceKey.isCompatible(AxArtifactKey.getNullKey()));
75 assertFalse(testReferenceKey.isCompatible(AxReferenceKey.getNullKey()));
76 assertTrue(testReferenceKey.isCompatible(testReferenceKey));
78 assertEquals(AxKey.Compatibility.DIFFERENT, testReferenceKey.getCompatibility(AxArtifactKey.getNullKey()));
79 assertEquals(AxKey.Compatibility.DIFFERENT, testReferenceKey.getCompatibility(AxReferenceKey.getNullKey()));
80 assertEquals(AxKey.Compatibility.IDENTICAL, testReferenceKey.getCompatibility(testReferenceKey));
82 AxValidationResult result = new AxValidationResult();
83 result = testReferenceKey.validate(result);
84 assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult());
86 testReferenceKey.clean();
88 AxReferenceKey clonedReferenceKey = new AxReferenceKey(testReferenceKey);
89 assertEquals("AxReferenceKey:(parentKeyName=NPKN,parentKeyVersion=0.0.1,parentLocalName=NPKLN,localName=NLN)",
90 clonedReferenceKey.toString());
92 assertFalse(testReferenceKey.hashCode() == 0);
94 assertTrue(testReferenceKey.equals(testReferenceKey));
95 assertTrue(testReferenceKey.equals(clonedReferenceKey));
96 assertFalse(testReferenceKey.equals((Object)"Hello"));
97 assertFalse(testReferenceKey.equals(new AxReferenceKey("PKN", "0.0.2", "PLN", "LN")));
98 assertFalse(testReferenceKey.equals(new AxReferenceKey("NPKN", "0.0.2", "PLN", "LN")));
99 assertFalse(testReferenceKey.equals(new AxReferenceKey("NPKN", "0.0.1", "PLN", "LN")));
100 assertFalse(testReferenceKey.equals(new AxReferenceKey("NPKN", "0.0.1", "NPLN", "LN")));
101 assertTrue(testReferenceKey.equals(new AxReferenceKey("NPKN", "0.0.1", "NPKLN", "NLN")));
103 assertEquals(0, testReferenceKey.compareTo(testReferenceKey));
104 assertEquals(0, testReferenceKey.compareTo(clonedReferenceKey));
105 assertNotEquals(0, testReferenceKey.compareTo(new AxArtifactKey()));
106 assertNotEquals(0, testReferenceKey.compareTo(new AxReferenceKey("PKN", "0.0.2", "PLN", "LN")));
107 assertNotEquals(0, testReferenceKey.compareTo(new AxReferenceKey("NPKN", "0.0.2", "PLN", "LN")));
108 assertNotEquals(0, testReferenceKey.compareTo(new AxReferenceKey("NPKN", "0.0.1", "PLN", "LN")));
109 assertNotEquals(0, testReferenceKey.compareTo(new AxReferenceKey("NPKN", "0.0.1", "NPLN", "LN")));
110 assertEquals(0, testReferenceKey.compareTo(new AxReferenceKey("NPKN", "0.0.1", "NPKLN", "NLN")));
112 assertNotNull(testReferenceKey.getKeys());
115 testReferenceKey.equals(null);
116 fail("test should throw an exception here");
117 } catch (Exception iae) {
118 assertEquals("comparison object may not be null", iae.getMessage());
122 testReferenceKey.copyTo(null);
123 fail("test should throw an exception here");
124 } catch (Exception iae) {
125 assertEquals("target may not be null", iae.getMessage());
129 testReferenceKey.copyTo(new AxArtifactKey("Key", "0.0.1"));
130 fail("test should throw an exception here");
131 } catch (Exception iae) {
132 assertEquals("org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey is not an instance of "
133 + "org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey", iae.getMessage());
136 AxReferenceKey targetRefKey = new AxReferenceKey();
137 assertEquals(testReferenceKey, testReferenceKey.copyTo(targetRefKey));
141 public void testValidation() {
142 AxReferenceKey testReferenceKey = new AxReferenceKey();
143 testReferenceKey.setParentArtifactKey(new AxArtifactKey("PN", "0.0.1"));
144 assertEquals("PN:0.0.1", testReferenceKey.getParentArtifactKey().getId());
147 Field parentNameField = testReferenceKey.getClass().getDeclaredField("parentKeyName");
148 parentNameField.setAccessible(true);
149 parentNameField.set(testReferenceKey, "Parent Name");
150 AxValidationResult validationResult = new AxValidationResult();
151 testReferenceKey.validate(validationResult);
152 parentNameField.set(testReferenceKey, "ParentName");
153 parentNameField.setAccessible(false);
155 "parentKeyName invalid-parameter parentKeyName with value Parent Name "
156 + "does not match regular expression [A-Za-z0-9\\-_\\.]+",
157 validationResult.getMessageList().get(0).getMessage());
158 } catch (Exception validationException) {
159 fail("test should not throw an exception");
163 Field parentVersionField = testReferenceKey.getClass().getDeclaredField("parentKeyVersion");
164 parentVersionField.setAccessible(true);
165 parentVersionField.set(testReferenceKey, "Parent Version");
166 AxValidationResult validationResult = new AxValidationResult();
167 testReferenceKey.validate(validationResult);
168 parentVersionField.set(testReferenceKey, "0.0.1");
169 parentVersionField.setAccessible(false);
171 "parentKeyVersion invalid-parameter parentKeyVersion with value Parent Version "
172 + "does not match regular expression [A-Za-z0-9.]+",
173 validationResult.getMessageList().get(0).getMessage());
174 } catch (Exception validationException) {
175 fail("test should not throw an exception");
179 Field parentLocalNameField = testReferenceKey.getClass().getDeclaredField("parentLocalName");
180 parentLocalNameField.setAccessible(true);
181 parentLocalNameField.set(testReferenceKey, "Parent Local Name");
182 AxValidationResult validationResult = new AxValidationResult();
183 testReferenceKey.validate(validationResult);
184 parentLocalNameField.set(testReferenceKey, "ParentLocalName");
185 parentLocalNameField.setAccessible(false);
187 "parentLocalName invalid-parameter parentLocalName with value "
188 + "Parent Local Name does not match regular expression [A-Za-z0-9\\-_\\.]+|^$",
189 validationResult.getMessageList().get(0).getMessage());
190 } catch (Exception validationException) {
191 fail("test should not throw an exception");
195 Field localNameField = testReferenceKey.getClass().getDeclaredField("localName");
196 localNameField.setAccessible(true);
197 localNameField.set(testReferenceKey, "Local Name");
198 AxValidationResult validationResult = new AxValidationResult();
199 testReferenceKey.validate(validationResult);
200 localNameField.set(testReferenceKey, "LocalName");
201 localNameField.setAccessible(false);
203 "localName invalid-parameter localName with value Local Name "
204 + "does not match regular expression [A-Za-z0-9\\-_\\.]+|^$",
205 validationResult.getMessageList().get(0).getMessage());
206 } catch (Exception validationException) {
207 fail("test should not throw an exception");