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