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;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxKey.Compatibility;
35 public class AxKeyTest {
38 public void testArtifactKey() {
39 assertThatThrownBy(() -> new AxArtifactKey("some bad key id"))
40 .hasMessage("parameter \"id\": value \"some bad key id\", "
41 + "does not match regular expression \"[A-Za-z0-9\\-_\\.]+:[0-9].[0-9].[0-9]\"");
42 AxArtifactKey someKey0 = new AxArtifactKey();
43 assertEquals(AxArtifactKey.getNullKey(), someKey0);
45 AxArtifactKey someKey1 = new AxArtifactKey("name", "0.0.1");
46 AxArtifactKey someKey2 = new AxArtifactKey(someKey1);
47 AxArtifactKey someKey3 = new AxArtifactKey(someKey1.getId());
48 assertEquals(someKey1, someKey2);
49 assertEquals(someKey1, someKey3);
51 assertEquals(someKey2, someKey1.getKey());
52 assertEquals(1, someKey1.getKeys().size());
54 someKey0.setName("zero");
55 someKey0.setVersion("0.0.2");
57 someKey3.setVersion("0.0.2");
59 AxArtifactKey someKey4 = new AxArtifactKey(someKey1);
60 someKey4.setVersion("0.1.2");
62 AxArtifactKey someKey5 = new AxArtifactKey(someKey1);
63 someKey5.setVersion("1.2.2");
65 AxArtifactKey someKey6 = new AxArtifactKey(someKey1);
66 someKey6.setVersion("3");
68 assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(new AxReferenceKey()));
69 assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(someKey1));
70 assertEquals(Compatibility.IDENTICAL, someKey2.getCompatibility(someKey1));
71 assertEquals(Compatibility.PATCH, someKey3.getCompatibility(someKey1));
72 assertEquals(Compatibility.MINOR, someKey4.getCompatibility(someKey1));
73 assertEquals(Compatibility.MAJOR, someKey5.getCompatibility(someKey1));
74 assertEquals(Compatibility.MAJOR, someKey6.getCompatibility(someKey1));
76 assertTrue(someKey1.isCompatible(someKey2));
77 assertTrue(someKey1.isCompatible(someKey3));
78 assertTrue(someKey1.isCompatible(someKey4));
79 assertFalse(someKey1.isCompatible(someKey0));
80 assertFalse(someKey1.isCompatible(someKey5));
81 assertFalse(someKey1.isCompatible(new AxReferenceKey()));
83 assertEquals(AxValidationResult.ValidationResult.VALID,
84 someKey0.validate(new AxValidationResult()).getValidationResult());
85 assertEquals(AxValidationResult.ValidationResult.VALID,
86 someKey1.validate(new AxValidationResult()).getValidationResult());
87 assertEquals(AxValidationResult.ValidationResult.VALID,
88 someKey2.validate(new AxValidationResult()).getValidationResult());
89 assertEquals(AxValidationResult.ValidationResult.VALID,
90 someKey3.validate(new AxValidationResult()).getValidationResult());
91 assertEquals(AxValidationResult.ValidationResult.VALID,
92 someKey4.validate(new AxValidationResult()).getValidationResult());
93 assertEquals(AxValidationResult.ValidationResult.VALID,
94 someKey5.validate(new AxValidationResult()).getValidationResult());
95 assertEquals(AxValidationResult.ValidationResult.VALID,
96 someKey6.validate(new AxValidationResult()).getValidationResult());
99 assertNotNull(someKey0.toString());
101 AxArtifactKey someKey7 = new AxArtifactKey(someKey1);
102 assertEquals(150332875, someKey7.hashCode());
103 assertEquals(0, someKey7.compareTo(someKey1));
104 assertEquals(-12, someKey7.compareTo(someKey0));
106 assertThatThrownBy(() -> someKey0.compareTo(null))
107 .hasMessage("comparison object may not be null");
108 assertEquals(0, someKey0.compareTo(someKey0));
109 assertEquals(353602977, someKey0.compareTo(new AxReferenceKey()));
111 assertNotNull(someKey0);
112 assertEquals(someKey0, someKey0);
113 assertNotEquals(((AxKey) someKey0), new AxReferenceKey());
115 AxArtifactKey nullKey0 = AxArtifactKey.getNullKey();
116 assertTrue(nullKey0.isNullKey());
117 AxArtifactKey nullKey1 = new AxArtifactKey();
118 assertTrue(nullKey1.isNullKey());
119 AxArtifactKey nullKey2 = new AxArtifactKey(AxKey.NULL_KEY_NAME, AxKey.NULL_KEY_VERSION);
120 assertTrue(nullKey2.isNullKey());
121 AxArtifactKey notnullKey = new AxArtifactKey("Blah", AxKey.NULL_KEY_VERSION);
122 assertFalse(notnullKey.isNullKey());
128 public void testValidation() throws IllegalArgumentException, IllegalAccessException,
129 NoSuchFieldException, SecurityException {
130 AxArtifactKey testKey = new AxArtifactKey("TheKey", "0.0.1");
131 assertEquals("TheKey:0.0.1", testKey.getId());
133 Field nameField = testKey.getClass().getDeclaredField("name");
134 nameField.setAccessible(true);
135 nameField.set(testKey, "Key Name");
136 AxValidationResult validationResult = new AxValidationResult();
137 testKey.validate(validationResult);
138 nameField.set(testKey, "TheKey");
139 nameField.setAccessible(false);
141 "name invalid-parameter name with value Key Name "
142 + "does not match regular expression [A-Za-z0-9\\-_\\.]+",
143 validationResult.getMessageList().get(0).getMessage());
145 Field versionField = testKey.getClass().getDeclaredField("version");
146 versionField.setAccessible(true);
147 versionField.set(testKey, "Key Version");
148 AxValidationResult validationResultV = new AxValidationResult();
149 testKey.validate(validationResultV);
150 versionField.set(testKey, "0.0.1");
151 versionField.setAccessible(false);
153 "version invalid-parameter version with value Key Version "
154 + "does not match regular expression [A-Za-z0-9.]+",
155 validationResultV.getMessageList().get(0).getMessage());