ff12fc5e84367b00de32d03349e191073f58f1df
[policy/apex-pdp.git] / model / basic-model / src / test / java / org / onap / policy / apex / model / basicmodel / concepts / AxKeyTest.java
1 /*
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.basicmodel.concepts;
23
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;
30
31 import java.lang.reflect.Field;
32 import org.junit.Test;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxKey.Compatibility;
34
35 public class AxKeyTest {
36
37     @Test
38     public void testArtifactKey() {
39         try {
40             new AxArtifactKey("some bad key id");
41             fail("This test should throw an exception");
42         } catch (IllegalArgumentException e) {
43             assertEquals("parameter \"id\": value \"some bad key id\", "
44                             + "does not match regular expression \"[A-Za-z0-9\\-_\\.]+:[0-9].[0-9].[0-9]\"",
45                             e.getMessage());
46         }
47
48         AxArtifactKey someKey0 = new AxArtifactKey();
49         assertEquals(AxArtifactKey.getNullKey(), someKey0);
50
51         AxArtifactKey someKey1 = new AxArtifactKey("name", "0.0.1");
52         AxArtifactKey someKey2 = new AxArtifactKey(someKey1);
53         AxArtifactKey someKey3 = new AxArtifactKey(someKey1.getId());
54         assertEquals(someKey1, someKey2);
55         assertEquals(someKey1, someKey3);
56
57         assertEquals(someKey2, someKey1.getKey());
58         assertEquals(1, someKey1.getKeys().size());
59
60         someKey0.setName("zero");
61         someKey0.setVersion("0.0.2");
62
63         someKey3.setVersion("0.0.2");
64
65         AxArtifactKey someKey4 = new AxArtifactKey(someKey1);
66         someKey4.setVersion("0.1.2");
67
68         AxArtifactKey someKey5 = new AxArtifactKey(someKey1);
69         someKey5.setVersion("1.2.2");
70
71         AxArtifactKey someKey6 = new AxArtifactKey(someKey1);
72         someKey6.setVersion("3");
73
74         assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(new AxReferenceKey()));
75         assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(someKey1));
76         assertEquals(Compatibility.IDENTICAL, someKey2.getCompatibility(someKey1));
77         assertEquals(Compatibility.PATCH, someKey3.getCompatibility(someKey1));
78         assertEquals(Compatibility.MINOR, someKey4.getCompatibility(someKey1));
79         assertEquals(Compatibility.MAJOR, someKey5.getCompatibility(someKey1));
80         assertEquals(Compatibility.MAJOR, someKey6.getCompatibility(someKey1));
81
82         assertTrue(someKey1.isCompatible(someKey2));
83         assertTrue(someKey1.isCompatible(someKey3));
84         assertTrue(someKey1.isCompatible(someKey4));
85         assertFalse(someKey1.isCompatible(someKey0));
86         assertFalse(someKey1.isCompatible(someKey5));
87         assertFalse(someKey1.isCompatible(new AxReferenceKey()));
88
89         assertEquals(AxValidationResult.ValidationResult.VALID,
90                         someKey0.validate(new AxValidationResult()).getValidationResult());
91         assertEquals(AxValidationResult.ValidationResult.VALID,
92                         someKey1.validate(new AxValidationResult()).getValidationResult());
93         assertEquals(AxValidationResult.ValidationResult.VALID,
94                         someKey2.validate(new AxValidationResult()).getValidationResult());
95         assertEquals(AxValidationResult.ValidationResult.VALID,
96                         someKey3.validate(new AxValidationResult()).getValidationResult());
97         assertEquals(AxValidationResult.ValidationResult.VALID,
98                         someKey4.validate(new AxValidationResult()).getValidationResult());
99         assertEquals(AxValidationResult.ValidationResult.VALID,
100                         someKey5.validate(new AxValidationResult()).getValidationResult());
101         assertEquals(AxValidationResult.ValidationResult.VALID,
102                         someKey6.validate(new AxValidationResult()).getValidationResult());
103
104         someKey0.clean();
105         assertNotNull(someKey0.toString());
106
107         AxArtifactKey someKey7 = new AxArtifactKey(someKey1);
108         assertEquals(150332875, someKey7.hashCode());
109         assertEquals(0, someKey7.compareTo(someKey1));
110         assertEquals(-12, someKey7.compareTo(someKey0));
111
112         try {
113             someKey0.compareTo(null);
114         } catch (IllegalArgumentException e) {
115             assertEquals("comparison object may not be null", e.getMessage());
116         }
117
118         assertEquals(0, someKey0.compareTo(someKey0));
119         assertEquals(353602977, someKey0.compareTo(new AxReferenceKey()));
120
121         assertNotNull(someKey0);
122         assertEquals(someKey0, someKey0);
123         assertNotEquals(((AxKey) someKey0), new AxReferenceKey());
124
125         AxArtifactKey nullKey0 = AxArtifactKey.getNullKey();
126         assertTrue(nullKey0.isNullKey());
127         AxArtifactKey nullKey1 = new AxArtifactKey();
128         assertTrue(nullKey1.isNullKey());
129         AxArtifactKey nullKey2 = new AxArtifactKey(AxKey.NULL_KEY_NAME, AxKey.NULL_KEY_VERSION);
130         assertTrue(nullKey2.isNullKey());
131         AxArtifactKey notnullKey = new AxArtifactKey("Blah", AxKey.NULL_KEY_VERSION);
132         assertFalse(notnullKey.isNullKey());
133
134     }
135
136
137     @Test
138     public void testValidation() {
139         AxArtifactKey testKey = new AxArtifactKey("TheKey", "0.0.1");
140         assertEquals("TheKey:0.0.1", testKey.getId());
141
142         try {
143             Field nameField = testKey.getClass().getDeclaredField("name");
144             nameField.setAccessible(true);
145             nameField.set(testKey, "Key Name");
146             AxValidationResult validationResult = new AxValidationResult();
147             testKey.validate(validationResult);
148             nameField.set(testKey, "TheKey");
149             nameField.setAccessible(false);
150             assertEquals(
151                 "name invalid-parameter name with value Key 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");
156         }
157
158         try {
159             Field versionField = testKey.getClass().getDeclaredField("version");
160             versionField.setAccessible(true);
161             versionField.set(testKey, "Key Version");
162             AxValidationResult validationResult = new AxValidationResult();
163             testKey.validate(validationResult);
164             versionField.set(testKey, "0.0.1");
165             versionField.setAccessible(false);
166             assertEquals(
167                 "version invalid-parameter version with value Key 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");
172         }
173     }
174 }