f1d1810408dfd2bd6831a69ff4ca89fe2edcb515
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfReferenceKeyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
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.models.base;
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
34 public class PfReferenceKeyTest {
35
36     @Test
37     public void testPfReferenceKey() {
38         assertNotNull(new PfReferenceKey());
39         assertNotNull(new PfReferenceKey(new PfConceptKey()));
40         assertNotNull(new PfReferenceKey(new PfConceptKey(), "LocalName"));
41         assertNotNull(new PfReferenceKey(new PfReferenceKey()));
42         assertNotNull(new PfReferenceKey(new PfReferenceKey(), "LocalName"));
43         assertNotNull(new PfReferenceKey(new PfConceptKey(), "ParentLocalName", "LocalName"));
44         assertNotNull(new PfReferenceKey("ParentKeyName", "0.0.1", "LocalName"));
45         assertNotNull(new PfReferenceKey("ParentKeyName", "0.0.1", "ParentLocalName", "LocalName"));
46         assertNotNull(new PfReferenceKey("ParentKeyName:0.0.1:ParentLocalName:LocalName"));
47         assertEquals(PfReferenceKey.getNullKey().getKey(), PfReferenceKey.getNullKey());
48         assertEquals("NULL:0.0.0:NULL:NULL", PfReferenceKey.getNullKey().getId());
49
50         try {
51             new PfReferenceKey(new PfConceptKey(), null);
52             fail("test should throw an exception");
53         } catch (Exception exc) {
54             assertEquals("parameter \"localName\" is null", exc.getMessage());
55         }
56
57         PfReferenceKey testReferenceKey = new PfReferenceKey();
58         testReferenceKey.setParentConceptKey(new PfConceptKey("PN", "0.0.1"));
59         assertEquals("PN:0.0.1", testReferenceKey.getParentConceptKey().getId());
60
61         assertEquals(0, testReferenceKey.getMajorVersion());
62         assertEquals(0, testReferenceKey.getMinorVersion());
63         assertEquals(1, testReferenceKey.getPatchVersion());
64
65         assertEquals(1, testReferenceKey.getKeys().size());
66         assertFalse(testReferenceKey.isNullKey());
67
68         testReferenceKey.setParentReferenceKey(new PfReferenceKey("PN", "0.0.1", "LN"));
69         assertEquals("PN:0.0.1:NULL:LN", testReferenceKey.getParentReferenceKey().getId());
70
71         testReferenceKey.setParentKeyName("NPKN");
72         assertEquals("NPKN", testReferenceKey.getParentKeyName());
73
74         testReferenceKey.setParentKeyVersion("0.0.1");
75         assertEquals("0.0.1", testReferenceKey.getParentKeyVersion());
76
77         testReferenceKey.setParentLocalName("NPKLN");
78         assertEquals("NPKLN", testReferenceKey.getParentLocalName());
79
80         testReferenceKey.setLocalName("NLN");
81         assertEquals("NLN", testReferenceKey.getLocalName());
82
83         try {
84             testReferenceKey.isCompatible(null);
85             fail("test should throw an exception here");
86         } catch (Exception exc) {
87             assertEquals("otherKey is marked @NonNull but is null", exc.getMessage());
88         }
89
90         assertFalse(testReferenceKey.isCompatible(PfConceptKey.getNullKey()));
91         assertFalse(testReferenceKey.isCompatible(PfReferenceKey.getNullKey()));
92         assertTrue(testReferenceKey.isCompatible(testReferenceKey));
93
94         assertEquals(PfKey.Compatibility.DIFFERENT, testReferenceKey.getCompatibility(PfConceptKey.getNullKey()));
95         assertEquals(PfKey.Compatibility.DIFFERENT, testReferenceKey.getCompatibility(PfReferenceKey.getNullKey()));
96         assertEquals(PfKey.Compatibility.IDENTICAL, testReferenceKey.getCompatibility(testReferenceKey));
97
98         PfValidationResult result = new PfValidationResult();
99         result = testReferenceKey.validate(result);
100         assertEquals(PfValidationResult.ValidationResult.VALID, result.getValidationResult());
101
102         testReferenceKey.clean();
103
104         PfReferenceKey clonedReferenceKey = new PfReferenceKey(testReferenceKey);
105         assertEquals("PfReferenceKey(parentKeyName=NPKN, parentKeyVersion=0.0.1, parentLocalName=NPKLN, localName=NLN)",
106                 clonedReferenceKey.toString());
107
108         assertFalse(testReferenceKey.hashCode() == 0);
109
110         assertTrue(testReferenceKey.equals(testReferenceKey));
111         assertTrue(testReferenceKey.equals(clonedReferenceKey));
112         assertFalse(testReferenceKey.equals("Hello"));
113         assertFalse(testReferenceKey.equals(new PfReferenceKey("PKN", "0.0.2", "PLN", "LN")));
114         assertFalse(testReferenceKey.equals(new PfReferenceKey("NPKN", "0.0.2", "PLN", "LN")));
115         assertFalse(testReferenceKey.equals(new PfReferenceKey("NPKN", "0.0.1", "PLN", "LN")));
116         assertFalse(testReferenceKey.equals(new PfReferenceKey("NPKN", "0.0.1", "NPLN", "LN")));
117         assertTrue(testReferenceKey.equals(new PfReferenceKey("NPKN", "0.0.1", "NPKLN", "NLN")));
118
119         assertEquals(0, testReferenceKey.compareTo(testReferenceKey));
120         assertEquals(0, testReferenceKey.compareTo(clonedReferenceKey));
121         assertNotEquals(0, testReferenceKey.compareTo(new PfConceptKey()));
122         assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("PKN", "0.0.2", "PLN", "LN")));
123         assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", "0.0.2", "PLN", "LN")));
124         assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", "0.0.1", "PLN", "LN")));
125         assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", "0.0.1", "NPLN", "LN")));
126         assertEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", "0.0.1", "NPKLN", "NLN")));
127
128         assertFalse(testReferenceKey.equals(null));
129
130         try {
131             testReferenceKey.copyTo(null);
132             fail("test should throw an exception here");
133         } catch (Exception iae) {
134             assertEquals("target may not be null", iae.getMessage());
135         }
136
137         try {
138             testReferenceKey.copyTo(new PfConceptKey("Key", "0.0.1"));
139             fail("test should throw an exception here");
140         } catch (Exception iae) {
141             assertEquals("org.onap.policy.models.base.PfConceptKey"
142                     + " is not an instance of org.onap.policy.models.base.PfReferenceKey", iae.getMessage());
143         }
144
145         PfReferenceKey targetRefKey = new PfReferenceKey();
146         assertEquals(testReferenceKey, testReferenceKey.copyTo(targetRefKey));
147     }
148
149     @Test
150     public void testValidation() {
151         PfReferenceKey testReferenceKey = new PfReferenceKey();
152         testReferenceKey.setParentConceptKey(new PfConceptKey("PN", "0.0.1"));
153         assertEquals("PN:0.0.1", testReferenceKey.getParentConceptKey().getId());
154
155         try {
156             Field parentNameField = testReferenceKey.getClass().getDeclaredField("parentKeyName");
157             parentNameField.setAccessible(true);
158             parentNameField.set(testReferenceKey, "Parent Name");
159             PfValidationResult validationResult = new PfValidationResult();
160             testReferenceKey.validate(validationResult);
161             parentNameField.set(testReferenceKey, "ParentName");
162             parentNameField.setAccessible(false);
163             assertEquals(
164                     "parentKeyName invalid-parameter parentKeyName with value Parent Name "
165                             + "does not match regular expression " + PfKey.NAME_REGEXP,
166                     validationResult.getMessageList().get(0).getMessage());
167         } catch (Exception validationException) {
168             fail("test should not throw an exception");
169         }
170
171         try {
172             Field parentVersionField = testReferenceKey.getClass().getDeclaredField("parentKeyVersion");
173             parentVersionField.setAccessible(true);
174             parentVersionField.set(testReferenceKey, "Parent Version");
175             PfValidationResult validationResult = new PfValidationResult();
176             testReferenceKey.validate(validationResult);
177             parentVersionField.set(testReferenceKey, "0.0.1");
178             parentVersionField.setAccessible(false);
179             assertEquals(
180                     "parentKeyVersion invalid-parameter parentKeyVersion with value Parent Version "
181                             + "does not match regular expression " + PfKey.VERSION_REGEXP,
182                     validationResult.getMessageList().get(0).getMessage());
183         } catch (Exception validationException) {
184             fail("test should not throw an exception");
185         }
186
187         try {
188             Field parentLocalNameField = testReferenceKey.getClass().getDeclaredField("parentLocalName");
189             parentLocalNameField.setAccessible(true);
190             parentLocalNameField.set(testReferenceKey, "Parent Local Name");
191             PfValidationResult validationResult = new PfValidationResult();
192             testReferenceKey.validate(validationResult);
193             parentLocalNameField.set(testReferenceKey, "ParentLocalName");
194             parentLocalNameField.setAccessible(false);
195             assertEquals(
196                     "parentLocalName invalid-parameter parentLocalName with value "
197                             + "Parent Local Name does not match regular expression [A-Za-z0-9\\-_\\.]+|^$",
198                     validationResult.getMessageList().get(0).getMessage());
199         } catch (Exception validationException) {
200             fail("test should not throw an exception");
201         }
202
203         try {
204             Field localNameField = testReferenceKey.getClass().getDeclaredField("localName");
205             localNameField.setAccessible(true);
206             localNameField.set(testReferenceKey, "Local Name");
207             PfValidationResult validationResult = new PfValidationResult();
208             testReferenceKey.validate(validationResult);
209             localNameField.set(testReferenceKey, "LocalName");
210             localNameField.setAccessible(false);
211             assertEquals(
212                     "localName invalid-parameter localName with value Local Name "
213                             + "does not match regular expression [A-Za-z0-9\\-_\\.]+|^$",
214                     validationResult.getMessageList().get(0).getMessage());
215         } catch (Exception validationException) {
216             fail("test should not throw an exception");
217         }
218     }
219 }