27726a9b43a99aa68a29d664b20efb30771e4137
[policy/apex-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.model.basicmodel.concepts;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotEquals;
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
32 import org.junit.Test;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
37
38 public class AxReferenceKeyTest {
39
40     @Test
41     public void testAxReferenceKey() {
42         assertNotNull(new AxReferenceKey());
43         assertNotNull(new AxReferenceKey(new AxArtifactKey()));
44         assertNotNull(new AxReferenceKey(new AxArtifactKey(), "LocalName"));
45         assertNotNull(new AxReferenceKey(new AxReferenceKey()));
46         assertNotNull(new AxReferenceKey(new AxReferenceKey(), "LocalName"));
47         assertNotNull(new AxReferenceKey(new AxArtifactKey(), "ParentLocalName", "LocalName"));
48         assertNotNull(new AxReferenceKey("ParentKeyName", "0.0.1", "LocalName"));
49         assertNotNull(new AxReferenceKey("ParentKeyName", "0.0.1", "ParentLocalName", "LocalName"));
50         assertNotNull(new AxReferenceKey("ParentKeyName:0.0.1:ParentLocalName:LocalName"));
51         assertEquals(AxReferenceKey.getNullKey().getKey(), AxReferenceKey.getNullKey());
52         assertEquals("NULL:0.0.0:NULL:NULL", AxReferenceKey.getNullKey().getId());
53
54         AxReferenceKey testReferenceKey = new AxReferenceKey();
55         testReferenceKey.setParentArtifactKey(new AxArtifactKey("PN", "0.0.1"));
56         assertEquals("PN:0.0.1", testReferenceKey.getParentArtifactKey().getId());
57
58         testReferenceKey.setParentReferenceKey(new AxReferenceKey("PN", "0.0.1", "LN"));
59         assertEquals("PN:0.0.1:NULL:LN", testReferenceKey.getParentReferenceKey().getId());
60
61         testReferenceKey.setParentKeyName("NPKN");
62         assertEquals("NPKN", testReferenceKey.getParentKeyName());
63
64         testReferenceKey.setParentKeyVersion("0.0.1");
65         assertEquals("0.0.1", testReferenceKey.getParentKeyVersion());
66
67         testReferenceKey.setParentLocalName("NPKLN");
68         assertEquals("NPKLN", testReferenceKey.getParentLocalName());
69
70         testReferenceKey.setLocalName("NLN");
71         assertEquals("NLN", testReferenceKey.getLocalName());
72
73         assertFalse(testReferenceKey.isCompatible(AxArtifactKey.getNullKey()));
74         assertFalse(testReferenceKey.isCompatible(AxReferenceKey.getNullKey()));
75         assertTrue(testReferenceKey.isCompatible(testReferenceKey));
76
77         assertEquals(AxKey.Compatibility.DIFFERENT, testReferenceKey.getCompatibility(AxArtifactKey.getNullKey()));
78         assertEquals(AxKey.Compatibility.DIFFERENT, testReferenceKey.getCompatibility(AxReferenceKey.getNullKey()));
79         assertEquals(AxKey.Compatibility.IDENTICAL, testReferenceKey.getCompatibility(testReferenceKey));
80
81         AxValidationResult result = new AxValidationResult();
82         result = testReferenceKey.validate(result);
83         assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult());
84
85         testReferenceKey.clean();
86
87         AxReferenceKey clonedReferenceKey = new AxReferenceKey(testReferenceKey);
88         assertEquals("AxReferenceKey:(parentKeyName=NPKN,parentKeyVersion=0.0.1,parentLocalName=NPKLN,localName=NLN)",
89             clonedReferenceKey.toString());
90
91         assertFalse(testReferenceKey.hashCode() == 0);
92
93         assertTrue(testReferenceKey.equals(testReferenceKey));
94         assertTrue(testReferenceKey.equals(clonedReferenceKey));
95         assertFalse(testReferenceKey.equals("Hello"));
96         assertFalse(testReferenceKey.equals(new AxReferenceKey("PKN", "0.0.2", "PLN", "LN")));
97         assertFalse(testReferenceKey.equals(new AxReferenceKey("NPKN", "0.0.2", "PLN", "LN")));
98         assertFalse(testReferenceKey.equals(new AxReferenceKey("NPKN", "0.0.1", "PLN", "LN")));
99         assertFalse(testReferenceKey.equals(new AxReferenceKey("NPKN", "0.0.1", "NPLN", "LN")));
100         assertTrue(testReferenceKey.equals(new AxReferenceKey("NPKN", "0.0.1", "NPKLN", "NLN")));
101
102         assertEquals(0, testReferenceKey.compareTo(testReferenceKey));
103         assertEquals(0, testReferenceKey.compareTo(clonedReferenceKey));
104         assertNotEquals(0, testReferenceKey.compareTo(new AxArtifactKey()));
105         assertNotEquals(0, testReferenceKey.compareTo(new AxReferenceKey("PKN", "0.0.2", "PLN", "LN")));
106         assertNotEquals(0, testReferenceKey.compareTo(new AxReferenceKey("NPKN", "0.0.2", "PLN", "LN")));
107         assertNotEquals(0, testReferenceKey.compareTo(new AxReferenceKey("NPKN", "0.0.1", "PLN", "LN")));
108         assertNotEquals(0, testReferenceKey.compareTo(new AxReferenceKey("NPKN", "0.0.1", "NPLN", "LN")));
109         assertEquals(0, testReferenceKey.compareTo(new AxReferenceKey("NPKN", "0.0.1", "NPKLN", "NLN")));
110
111         assertNotNull(testReferenceKey.getKeys());
112
113         try {
114             testReferenceKey.equals(null);
115             fail("test should throw an exception here");
116         } catch (Exception iae) {
117             assertEquals("comparison object may not be null", iae.getMessage());
118         }
119
120         try {
121             testReferenceKey.copyTo(null);
122             fail("test should throw an exception here");
123         } catch (Exception iae) {
124             assertEquals("target may not be null", iae.getMessage());
125         }
126
127         try {
128             testReferenceKey.copyTo(new AxArtifactKey("Key", "0.0.1"));
129             fail("test should throw an exception here");
130         } catch (Exception iae) {
131             assertEquals("org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey is not an instance of "
132                 + "org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey", iae.getMessage());
133         }
134
135         AxReferenceKey targetRefKey = new AxReferenceKey();
136         assertEquals(testReferenceKey, testReferenceKey.copyTo(targetRefKey));
137     }
138
139     @Test
140     public void testValidation() {
141         AxReferenceKey testReferenceKey = new AxReferenceKey();
142         testReferenceKey.setParentArtifactKey(new AxArtifactKey("PN", "0.0.1"));
143         assertEquals("PN:0.0.1", testReferenceKey.getParentArtifactKey().getId());
144
145         try {
146             Field parentNameField = testReferenceKey.getClass().getDeclaredField("parentKeyName");
147             parentNameField.setAccessible(true);
148             parentNameField.set(testReferenceKey, "Parent Name");
149             AxValidationResult validationResult = new AxValidationResult();
150             testReferenceKey.validate(validationResult);
151             parentNameField.set(testReferenceKey, "ParentName");
152             parentNameField.setAccessible(false);
153             assertEquals(
154                 "parentKeyName invalid-parameter parentKeyName with value Parent Name "
155                     + "does not match regular expression [A-Za-z0-9\\-_\\.]+",
156                 validationResult.getMessageList().get(0).getMessage());
157         } catch (Exception validationException) {
158             fail("test should not throw an exception");
159         }
160
161         try {
162             Field parentVersionField = testReferenceKey.getClass().getDeclaredField("parentKeyVersion");
163             parentVersionField.setAccessible(true);
164             parentVersionField.set(testReferenceKey, "Parent Version");
165             AxValidationResult validationResult = new AxValidationResult();
166             testReferenceKey.validate(validationResult);
167             parentVersionField.set(testReferenceKey, "0.0.1");
168             parentVersionField.setAccessible(false);
169             assertEquals(
170                 "parentKeyVersion invalid-parameter parentKeyVersion with value Parent Version "
171                     + "does not match regular expression [A-Za-z0-9.]+",
172                 validationResult.getMessageList().get(0).getMessage());
173         } catch (Exception validationException) {
174             fail("test should not throw an exception");
175         }
176
177         try {
178             Field parentLocalNameField = testReferenceKey.getClass().getDeclaredField("parentLocalName");
179             parentLocalNameField.setAccessible(true);
180             parentLocalNameField.set(testReferenceKey, "Parent Local Name");
181             AxValidationResult validationResult = new AxValidationResult();
182             testReferenceKey.validate(validationResult);
183             parentLocalNameField.set(testReferenceKey, "ParentLocalName");
184             parentLocalNameField.setAccessible(false);
185             assertEquals(
186                 "parentLocalName invalid-parameter parentLocalName with value "
187                     + "Parent Local Name does not match regular expression [A-Za-z0-9\\-_\\.]+|^$",
188                 validationResult.getMessageList().get(0).getMessage());
189         } catch (Exception validationException) {
190             fail("test should not throw an exception");
191         }
192
193         try {
194             Field localNameField = testReferenceKey.getClass().getDeclaredField("localName");
195             localNameField.setAccessible(true);
196             localNameField.set(testReferenceKey, "Local Name");
197             AxValidationResult validationResult = new AxValidationResult();
198             testReferenceKey.validate(validationResult);
199             localNameField.set(testReferenceKey, "LocalName");
200             localNameField.setAccessible(false);
201             assertEquals(
202                 "localName invalid-parameter localName with value Local Name "
203                     + "does not match regular expression [A-Za-z0-9\\-_\\.]+|^$",
204                 validationResult.getMessageList().get(0).getMessage());
205         } catch (Exception validationException) {
206             fail("test should not throw an exception");
207         }
208     }
209 }