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