Changes for Checkstyle 8.32
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfReferenceKeyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
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.models.base;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30
31 import java.lang.reflect.Field;
32 import org.junit.Test;
33
34 public class PfReferenceKeyTest {
35
36     private static final String PARENT_LOCAL_NAME = "ParentLocalName";
37     private static final String NPKLN = "NPKLN";
38     private static final String LOCAL_NAME = "LocalName";
39     private static final String VERSION002 = "0.0.2";
40     private static final String VERSION001 = "0.0.1";
41
42     @Test
43     public void testPfReferenceKey() {
44         assertNotNull(new PfReferenceKey());
45         assertNotNull(new PfReferenceKey(new PfConceptKey()));
46         assertNotNull(new PfReferenceKey(new PfConceptKey(), LOCAL_NAME));
47         assertNotNull(new PfReferenceKey(new PfReferenceKey()));
48         assertNotNull(new PfReferenceKey(new PfReferenceKey(), LOCAL_NAME));
49         assertNotNull(new PfReferenceKey(new PfConceptKey(), PARENT_LOCAL_NAME, LOCAL_NAME));
50         assertNotNull(new PfReferenceKey("ParentKeyName", VERSION001, LOCAL_NAME));
51         assertNotNull(new PfReferenceKey("ParentKeyName", VERSION001, PARENT_LOCAL_NAME, LOCAL_NAME));
52         assertNotNull(new PfReferenceKey("ParentKeyName:0.0.1:ParentLocalName:LocalName"));
53         assertEquals(PfReferenceKey.getNullKey().getKey(), PfReferenceKey.getNullKey());
54         assertEquals("NULL:0.0.0:NULL:NULL", PfReferenceKey.getNullKey().getId());
55
56         assertThatThrownBy(() -> new PfReferenceKey(new PfConceptKey(), null))
57             .hasMessage("parameter \"localName\" is null");
58
59         PfReferenceKey testReferenceKey = new PfReferenceKey();
60         testReferenceKey.setParentConceptKey(new PfConceptKey("PN", VERSION001));
61         assertEquals("PN:0.0.1", testReferenceKey.getParentConceptKey().getId());
62
63         assertEquals(0, testReferenceKey.getMajorVersion());
64         assertEquals(0, testReferenceKey.getMinorVersion());
65         assertEquals(1, testReferenceKey.getPatchVersion());
66
67         assertEquals(1, testReferenceKey.getKeys().size());
68         assertFalse(testReferenceKey.isNullKey());
69
70         testReferenceKey.setParentReferenceKey(new PfReferenceKey("PN", VERSION001, "LN"));
71         assertEquals("PN:0.0.1:NULL:LN", testReferenceKey.getParentReferenceKey().getId());
72
73         testReferenceKey.setParentKeyName("NPKN");
74         assertEquals("NPKN", testReferenceKey.getParentKeyName());
75
76         testReferenceKey.setParentKeyVersion(VERSION001);
77         assertEquals(VERSION001, testReferenceKey.getParentKeyVersion());
78
79         testReferenceKey.setParentLocalName(NPKLN);
80         assertEquals(NPKLN, testReferenceKey.getParentLocalName());
81
82         testReferenceKey.setLocalName("NLN");
83         assertEquals("NLN", testReferenceKey.getLocalName());
84
85         assertThatThrownBy(() -> testReferenceKey.isCompatible(null))
86             .hasMessageMatching("^otherKey is marked .*on.*ull but is null$");
87
88         assertFalse(testReferenceKey.isCompatible(PfConceptKey.getNullKey()));
89         assertFalse(testReferenceKey.isCompatible(PfReferenceKey.getNullKey()));
90         assertTrue(testReferenceKey.isCompatible(testReferenceKey));
91
92         assertEquals(PfKey.Compatibility.DIFFERENT, testReferenceKey.getCompatibility(PfConceptKey.getNullKey()));
93         assertEquals(PfKey.Compatibility.DIFFERENT, testReferenceKey.getCompatibility(PfReferenceKey.getNullKey()));
94         assertEquals(PfKey.Compatibility.IDENTICAL, testReferenceKey.getCompatibility(testReferenceKey));
95
96         PfValidationResult result = new PfValidationResult();
97         result = testReferenceKey.validate(result);
98         assertEquals(PfValidationResult.ValidationResult.VALID, result.getValidationResult());
99
100         testReferenceKey.clean();
101
102         PfReferenceKey clonedReferenceKey = new PfReferenceKey(testReferenceKey);
103         assertEquals("PfReferenceKey(parentKeyName=NPKN, parentKeyVersion=0.0.1, parentLocalName=NPKLN, localName=NLN)",
104             clonedReferenceKey.toString());
105
106         assertFalse(testReferenceKey.hashCode() == 0);
107
108         assertTrue(testReferenceKey.equals(testReferenceKey));
109         assertTrue(testReferenceKey.equals(clonedReferenceKey));
110         assertFalse(testReferenceKey.equals("Hello"));
111         assertFalse(testReferenceKey.equals(new PfReferenceKey("PKN", VERSION002, "PLN", "LN")));
112         assertFalse(testReferenceKey.equals(new PfReferenceKey("NPKN", VERSION002, "PLN", "LN")));
113         assertFalse(testReferenceKey.equals(new PfReferenceKey("NPKN", VERSION001, "PLN", "LN")));
114         assertFalse(testReferenceKey.equals(new PfReferenceKey("NPKN", VERSION001, "NPLN", "LN")));
115         assertTrue(testReferenceKey.equals(new PfReferenceKey("NPKN", VERSION001, NPKLN, "NLN")));
116
117         assertEquals(0, testReferenceKey.compareTo(testReferenceKey));
118         assertEquals(0, testReferenceKey.compareTo(clonedReferenceKey));
119         assertNotEquals(0, testReferenceKey.compareTo(new PfConceptKey()));
120         assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("PKN", VERSION002, "PLN", "LN")));
121         assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", VERSION002, "PLN", "LN")));
122         assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", VERSION001, "PLN", "LN")));
123         assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", VERSION001, "NPLN", "LN")));
124         assertEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", VERSION001, NPKLN, "NLN")));
125
126         assertFalse(testReferenceKey.equals(null));
127
128         assertThatThrownBy(() -> new PfReferenceKey((PfReferenceKey) null)).isInstanceOf(NullPointerException.class);
129
130         assertEquals(testReferenceKey, new PfReferenceKey(testReferenceKey));
131     }
132
133     @Test
134     public void testValidation() throws Exception {
135         PfReferenceKey testReferenceKey = new PfReferenceKey();
136         testReferenceKey.setParentConceptKey(new PfConceptKey("PN", VERSION001));
137         assertEquals("PN:0.0.1", testReferenceKey.getParentConceptKey().getId());
138
139         Field parentNameField = testReferenceKey.getClass().getDeclaredField("parentKeyName");
140         parentNameField.setAccessible(true);
141         parentNameField.set(testReferenceKey, "Parent Name");
142         PfValidationResult validationResult = new PfValidationResult();
143         testReferenceKey.validate(validationResult);
144         parentNameField.set(testReferenceKey, "ParentName");
145         parentNameField.setAccessible(false);
146         assertEquals(
147             "parentKeyName invalid-parameter parentKeyName with value Parent Name "
148                 + "does not match regular expression " + PfKey.NAME_REGEXP,
149             validationResult.getMessageList().get(0).getMessage());
150
151         Field parentVersionField = testReferenceKey.getClass().getDeclaredField("parentKeyVersion");
152         parentVersionField.setAccessible(true);
153         parentVersionField.set(testReferenceKey, "Parent Version");
154         PfValidationResult validationResult2 = new PfValidationResult();
155         testReferenceKey.validate(validationResult2);
156         parentVersionField.set(testReferenceKey, VERSION001);
157         parentVersionField.setAccessible(false);
158         assertEquals(
159             "parentKeyVersion invalid-parameter parentKeyVersion with value Parent Version "
160                 + "does not match regular expression " + PfKey.VERSION_REGEXP,
161             validationResult2.getMessageList().get(0).getMessage());
162
163         Field parentLocalNameField = testReferenceKey.getClass().getDeclaredField("parentLocalName");
164         parentLocalNameField.setAccessible(true);
165         parentLocalNameField.set(testReferenceKey, "Parent Local Name");
166         PfValidationResult validationResult3 = new PfValidationResult();
167         testReferenceKey.validate(validationResult3);
168         parentLocalNameField.set(testReferenceKey, PARENT_LOCAL_NAME);
169         parentLocalNameField.setAccessible(false);
170         assertEquals(
171             "parentLocalName invalid-parameter parentLocalName with value "
172                 + "Parent Local Name does not match regular expression [A-Za-z0-9\\-_\\.]+|^$",
173             validationResult3.getMessageList().get(0).getMessage());
174
175         Field localNameField = testReferenceKey.getClass().getDeclaredField("localName");
176         localNameField.setAccessible(true);
177         localNameField.set(testReferenceKey, "Local Name");
178         PfValidationResult validationResult4 = new PfValidationResult();
179         testReferenceKey.validate(validationResult4);
180         localNameField.set(testReferenceKey, LOCAL_NAME);
181         localNameField.setAccessible(false);
182         assertEquals(
183             "localName invalid-parameter localName with value Local Name "
184                 + "does not match regular expression [A-Za-z0-9\\-_\\.]+|^$",
185             validationResult4.getMessageList().get(0).getMessage());
186     }
187 }