Changes for checkstyle 8.32
[policy/apex-pdp.git] / model / basic-model / src / test / java / org / onap / policy / apex / model / basicmodel / concepts / AxReferenceKeyTest.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.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
34
35 public class AxReferenceKeyTest {
36
37     @Test
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());
50
51         AxReferenceKey testReferenceKey = new AxReferenceKey();
52         testReferenceKey.setParentArtifactKey(new AxArtifactKey("PN", "0.0.1"));
53         assertEquals("PN:0.0.1", testReferenceKey.getParentArtifactKey().getId());
54
55         testReferenceKey.setParentReferenceKey(new AxReferenceKey("PN", "0.0.1", "LN"));
56         assertEquals("PN:0.0.1:NULL:LN", testReferenceKey.getParentReferenceKey().getId());
57
58         testReferenceKey.setParentKeyName("NPKN");
59         assertEquals("NPKN", testReferenceKey.getParentKeyName());
60
61         testReferenceKey.setParentKeyVersion("0.0.1");
62         assertEquals("0.0.1", testReferenceKey.getParentKeyVersion());
63
64         testReferenceKey.setParentLocalName("NPKLN");
65         assertEquals("NPKLN", testReferenceKey.getParentLocalName());
66
67         testReferenceKey.setLocalName("NLN");
68         assertEquals("NLN", testReferenceKey.getLocalName());
69
70         assertFalse(testReferenceKey.isCompatible(AxArtifactKey.getNullKey()));
71         assertFalse(testReferenceKey.isCompatible(AxReferenceKey.getNullKey()));
72         assertTrue(testReferenceKey.isCompatible(testReferenceKey));
73
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));
77
78         AxValidationResult result = new AxValidationResult();
79         result = testReferenceKey.validate(result);
80         assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult());
81
82         testReferenceKey.clean();
83
84         AxReferenceKey clonedReferenceKey = new AxReferenceKey(testReferenceKey);
85         assertEquals("AxReferenceKey:(parentKeyName=NPKN,parentKeyVersion=0.0.1,parentLocalName=NPKLN,localName=NLN)",
86             clonedReferenceKey.toString());
87
88         assertFalse(testReferenceKey.hashCode() == 0);
89
90         assertTrue(testReferenceKey.equals(testReferenceKey));
91         assertTrue(testReferenceKey.equals(clonedReferenceKey));
92         assertFalse(testReferenceKey.equals((Object) "Hello"));
93         assertFalse(testReferenceKey.equals(new AxReferenceKey("PKN", "0.0.2", "PLN", "LN")));
94         assertFalse(testReferenceKey.equals(new AxReferenceKey("NPKN", "0.0.2", "PLN", "LN")));
95         assertFalse(testReferenceKey.equals(new AxReferenceKey("NPKN", "0.0.1", "PLN", "LN")));
96         assertFalse(testReferenceKey.equals(new AxReferenceKey("NPKN", "0.0.1", "NPLN", "LN")));
97         assertTrue(testReferenceKey.equals(new AxReferenceKey("NPKN", "0.0.1", "NPKLN", "NLN")));
98
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")));
107
108         assertNotNull(testReferenceKey.getKeys());
109
110         try {
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());
115         }
116
117         try {
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());
122         }
123
124         try {
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());
130         }
131
132         AxReferenceKey targetRefKey = new AxReferenceKey();
133         assertEquals(testReferenceKey, testReferenceKey.copyTo(targetRefKey));
134     }
135
136     @Test
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());
141
142         try {
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);
150             assertEquals(
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");
156         }
157
158         try {
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);
166             assertEquals(
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");
172         }
173
174         try {
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);
182             assertEquals(
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");
188         }
189
190         try {
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);
198             assertEquals(
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");
204         }
205     }
206 }