Add DAO module for Models
[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         PfReferenceKey testReferenceKey = new PfReferenceKey();
51         testReferenceKey.setParentConceptKey(new PfConceptKey("PN", "0.0.1"));
52         assertEquals("PN:0.0.1", testReferenceKey.getParentConceptKey().getId());
53
54         testReferenceKey.setParentReferenceKey(new PfReferenceKey("PN", "0.0.1", "LN"));
55         assertEquals("PN:0.0.1:NULL:LN", testReferenceKey.getParentReferenceKey().getId());
56
57         testReferenceKey.setParentKeyName("NPKN");
58         assertEquals("NPKN", testReferenceKey.getParentKeyName());
59
60         testReferenceKey.setParentKeyVersion("0.0.1");
61         assertEquals("0.0.1", testReferenceKey.getParentKeyVersion());
62
63         testReferenceKey.setParentLocalName("NPKLN");
64         assertEquals("NPKLN", testReferenceKey.getParentLocalName());
65
66         testReferenceKey.setLocalName("NLN");
67         assertEquals("NLN", testReferenceKey.getLocalName());
68
69         assertFalse(testReferenceKey.isCompatible(PfConceptKey.getNullKey()));
70         assertFalse(testReferenceKey.isCompatible(PfReferenceKey.getNullKey()));
71         assertTrue(testReferenceKey.isCompatible(testReferenceKey));
72
73         assertEquals(PfKey.Compatibility.DIFFERENT, testReferenceKey.getCompatibility(PfConceptKey.getNullKey()));
74         assertEquals(PfKey.Compatibility.DIFFERENT, testReferenceKey.getCompatibility(PfReferenceKey.getNullKey()));
75         assertEquals(PfKey.Compatibility.IDENTICAL, testReferenceKey.getCompatibility(testReferenceKey));
76
77         PfValidationResult result = new PfValidationResult();
78         result = testReferenceKey.validate(result);
79         assertEquals(PfValidationResult.ValidationResult.VALID, result.getValidationResult());
80
81         testReferenceKey.clean();
82
83         PfReferenceKey clonedReferenceKey = new PfReferenceKey(testReferenceKey);
84         assertEquals("PfReferenceKey(parentKeyName=NPKN, parentKeyVersion=0.0.1, parentLocalName=NPKLN, localName=NLN)",
85                 clonedReferenceKey.toString());
86
87         assertFalse(testReferenceKey.hashCode() == 0);
88
89         assertTrue(testReferenceKey.equals(testReferenceKey));
90         assertTrue(testReferenceKey.equals(clonedReferenceKey));
91         assertFalse(testReferenceKey.equals("Hello"));
92         assertFalse(testReferenceKey.equals(new PfReferenceKey("PKN", "0.0.2", "PLN", "LN")));
93         assertFalse(testReferenceKey.equals(new PfReferenceKey("NPKN", "0.0.2", "PLN", "LN")));
94         assertFalse(testReferenceKey.equals(new PfReferenceKey("NPKN", "0.0.1", "PLN", "LN")));
95         assertFalse(testReferenceKey.equals(new PfReferenceKey("NPKN", "0.0.1", "NPLN", "LN")));
96         assertTrue(testReferenceKey.equals(new PfReferenceKey("NPKN", "0.0.1", "NPKLN", "NLN")));
97
98         assertEquals(0, testReferenceKey.compareTo(testReferenceKey));
99         assertEquals(0, testReferenceKey.compareTo(clonedReferenceKey));
100         assertNotEquals(0, testReferenceKey.compareTo(new PfConceptKey()));
101         assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("PKN", "0.0.2", "PLN", "LN")));
102         assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", "0.0.2", "PLN", "LN")));
103         assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", "0.0.1", "PLN", "LN")));
104         assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", "0.0.1", "NPLN", "LN")));
105         assertEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", "0.0.1", "NPKLN", "NLN")));
106
107         assertFalse(testReferenceKey.equals(null));
108
109         try {
110             testReferenceKey.copyTo(null);
111             fail("test should throw an exception here");
112         } catch (Exception iae) {
113             assertEquals("target may not be null", iae.getMessage());
114         }
115
116         try {
117             testReferenceKey.copyTo(new PfConceptKey("Key", "0.0.1"));
118             fail("test should throw an exception here");
119         } catch (Exception iae) {
120             assertEquals("org.onap.policy.models.base.PfConceptKey"
121                     + " is not an instance of org.onap.policy.models.base.PfReferenceKey", iae.getMessage());
122         }
123
124         PfReferenceKey targetRefKey = new PfReferenceKey();
125         assertEquals(testReferenceKey, testReferenceKey.copyTo(targetRefKey));
126     }
127
128     @Test
129     public void testValidation() {
130         PfReferenceKey testReferenceKey = new PfReferenceKey();
131         testReferenceKey.setParentConceptKey(new PfConceptKey("PN", "0.0.1"));
132         assertEquals("PN:0.0.1", testReferenceKey.getParentConceptKey().getId());
133
134         try {
135             Field parentNameField = testReferenceKey.getClass().getDeclaredField("parentKeyName");
136             parentNameField.setAccessible(true);
137             parentNameField.set(testReferenceKey, "Parent Name");
138             PfValidationResult validationResult = new PfValidationResult();
139             testReferenceKey.validate(validationResult);
140             parentNameField.set(testReferenceKey, "ParentName");
141             parentNameField.setAccessible(false);
142             assertEquals(
143                     "parentKeyName invalid-parameter parentKeyName with value Parent Name "
144                             + "does not match regular expression [A-Za-z0-9\\-_\\.]+",
145                     validationResult.getMessageList().get(0).getMessage());
146         } catch (Exception validationException) {
147             fail("test should not throw an exception");
148         }
149
150         try {
151             Field parentVersionField = testReferenceKey.getClass().getDeclaredField("parentKeyVersion");
152             parentVersionField.setAccessible(true);
153             parentVersionField.set(testReferenceKey, "Parent Version");
154             PfValidationResult validationResult = new PfValidationResult();
155             testReferenceKey.validate(validationResult);
156             parentVersionField.set(testReferenceKey, "0.0.1");
157             parentVersionField.setAccessible(false);
158             assertEquals(
159                     "parentKeyVersion invalid-parameter parentKeyVersion with value Parent Version "
160                             + "does not match regular expression [A-Za-z0-9.]+",
161                     validationResult.getMessageList().get(0).getMessage());
162         } catch (Exception validationException) {
163             fail("test should not throw an exception");
164         }
165
166         try {
167             Field parentLocalNameField = testReferenceKey.getClass().getDeclaredField("parentLocalName");
168             parentLocalNameField.setAccessible(true);
169             parentLocalNameField.set(testReferenceKey, "Parent Local Name");
170             PfValidationResult validationResult = new PfValidationResult();
171             testReferenceKey.validate(validationResult);
172             parentLocalNameField.set(testReferenceKey, "ParentLocalName");
173             parentLocalNameField.setAccessible(false);
174             assertEquals(
175                     "parentLocalName invalid-parameter parentLocalName with value "
176                             + "Parent Local Name does not match regular expression [A-Za-z0-9\\-_\\.]+|^$",
177                     validationResult.getMessageList().get(0).getMessage());
178         } catch (Exception validationException) {
179             fail("test should not throw an exception");
180         }
181
182         try {
183             Field localNameField = testReferenceKey.getClass().getDeclaredField("localName");
184             localNameField.setAccessible(true);
185             localNameField.set(testReferenceKey, "Local Name");
186             PfValidationResult validationResult = new PfValidationResult();
187             testReferenceKey.validate(validationResult);
188             localNameField.set(testReferenceKey, "LocalName");
189             localNameField.setAccessible(false);
190             assertEquals(
191                     "localName invalid-parameter localName with value Local Name "
192                             + "does not match regular expression [A-Za-z0-9\\-_\\.]+|^$",
193                     validationResult.getMessageList().get(0).getMessage());
194         } catch (Exception validationException) {
195             fail("test should not throw an exception");
196         }
197     }
198 }