1b8534433f53c0e54c9c4f902b32c07bbd149112
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfKeyUseTest.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.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 org.junit.Test;
32 import org.onap.policy.models.base.PfKey.Compatibility;
33 import org.onap.policy.models.base.testconcepts.DummyPfConceptKeySub;
34
35 public class PfKeyUseTest {
36
37     private static final String OTHER_KEY_IS_NULL = "^otherKey is marked .*on.*ull but is null$";
38
39     @Test
40     public void testKeyUse() {
41         assertNotNull(new PfKeyUse());
42         assertNotNull(new PfKeyUse(new PfConceptKey()));
43         assertNotNull(new PfKeyUse(new PfReferenceKey()));
44
45         assertThatThrownBy(() -> new PfKeyUse((PfKeyUse) null))
46             .hasMessageMatching("^copyConcept is marked .*on.*ull but is null$");
47
48         PfConceptKey key = new PfConceptKey("Key", "0.0.1");
49         PfKeyUse keyUse = new PfKeyUse();
50         keyUse.setKey(key);
51         assertEquals(key, keyUse.getKey());
52         assertEquals("Key:0.0.1", keyUse.getId());
53         assertEquals(key, keyUse.getKeys().get(0));
54         assertFalse(keyUse.isNullKey());
55
56         assertEquals(Compatibility.IDENTICAL, keyUse.getCompatibility(key));
57
58         assertThatThrownBy(() -> key.getCompatibility(null)).hasMessageMatching(OTHER_KEY_IS_NULL);
59
60         assertTrue(keyUse.isCompatible(key));
61
62         keyUse.clean();
63         assertNotNull(keyUse);
64
65         assertNotNull(keyUse.validate(""));
66
67         assertNotEquals(0, keyUse.hashCode());
68
69         PfKeyUse clonedKeyUse = new PfKeyUse(keyUse);
70         assertEquals("PfKeyUse(usedKey=PfConceptKey(name=Key, version=0.0.1))", clonedKeyUse.toString());
71
72         assertNotEquals(0, keyUse.hashCode());
73
74         assertEquals(keyUse, keyUse);
75         assertEquals(keyUse, clonedKeyUse);
76         assertNotEquals(keyUse, "Hello");
77         assertEquals(keyUse, new PfKeyUse(key));
78
79         assertEquals(0, keyUse.compareTo(keyUse));
80         assertEquals(0, keyUse.compareTo(clonedKeyUse));
81         assertNotEquals(0, keyUse.compareTo(new PfConceptKey()));
82         assertEquals(0, keyUse.compareTo(new PfKeyUse(key)));
83     }
84
85     @Test
86     public void testNullKey() {
87         PfKeyUse keyUseNull = new PfKeyUse(PfConceptKey.getNullKey());
88         PfKeyUse keyUse = new PfKeyUse();
89         assertEquals(false, keyUseNull.validate("").isValid());
90
91         assertThatThrownBy(() -> keyUse.setKey(null)).hasMessageMatching("^key is marked .*on.*ull but is null$");
92
93         assertThatThrownBy(() -> keyUse.getCompatibility(null)).hasMessageMatching(OTHER_KEY_IS_NULL);
94
95         assertThatThrownBy(() -> keyUse.isCompatible(null)).hasMessageMatching(OTHER_KEY_IS_NULL);
96
97         assertThatThrownBy(() -> keyUse.validate(null))
98                         .hasMessageMatching("^fieldName is marked .*on.*ull but is null$");
99
100         PfKeyUse testKeyUse = new PfKeyUse(new DummyPfConceptKeySub(new PfConceptKey()));
101         assertEquals(testKeyUse, new PfKeyUse(testKeyUse));
102
103         assertThatThrownBy(() -> new PfKeyUse((PfKeyUse) null)).isInstanceOf(NullPointerException.class);
104
105         assertThatThrownBy(() -> keyUse.isNewerThan(null)).hasMessageMatching(OTHER_KEY_IS_NULL);
106
107         assertEquals(false, testKeyUse.isNewerThan(keyUse));
108         assertEquals(false, testKeyUse.isNewerThan(testKeyUse));
109
110         assertEquals(0, testKeyUse.getMajorVersion());
111         assertEquals(0, testKeyUse.getMinorVersion());
112         assertEquals(0, testKeyUse.getPatchVersion());
113     }
114 }