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