3e5d738bddb83300791d32542906cf2262f5c9fe
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfKeyUseTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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 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 @NonNull 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                         .hasMessage("copyConcept is marked @NonNull 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)).hasMessage(OTHER_KEY_IS_NULL);
59
60         assertTrue(keyUse.isCompatible(key));
61
62         keyUse.clean();
63         assertNotNull(keyUse);
64
65         PfValidationResult result = new PfValidationResult();
66         result = keyUse.validate(result);
67         assertNotNull(result);
68
69         assertNotEquals(0, keyUse.hashCode());
70
71         PfKeyUse clonedKeyUse = new PfKeyUse(keyUse);
72         assertEquals("PfKeyUse(usedKey=PfConceptKey(name=Key, version=0.0.1))", clonedKeyUse.toString());
73
74         assertFalse(keyUse.hashCode() == 0);
75
76         assertTrue(keyUse.equals(keyUse));
77         assertTrue(keyUse.equals(clonedKeyUse));
78         assertFalse(keyUse.equals("Hello"));
79         assertTrue(keyUse.equals(new PfKeyUse(key)));
80
81         assertEquals(0, keyUse.compareTo(keyUse));
82         assertEquals(0, keyUse.compareTo(clonedKeyUse));
83         assertNotEquals(0, keyUse.compareTo(new PfConceptKey()));
84         assertEquals(0, keyUse.compareTo(new PfKeyUse(key)));
85
86         PfKeyUse keyUseNull = new PfKeyUse(PfConceptKey.getNullKey());
87         PfValidationResult resultNull = new PfValidationResult();
88         assertEquals(false, keyUseNull.validate(resultNull).isValid());
89
90         assertThatThrownBy(() -> keyUse.setKey(null)).hasMessage("key is marked @NonNull but is null");
91
92         assertThatThrownBy(() -> keyUse.getCompatibility(null)).hasMessage(OTHER_KEY_IS_NULL);
93
94         assertThatThrownBy(() -> keyUse.isCompatible(null)).hasMessage(OTHER_KEY_IS_NULL);
95
96         assertThatThrownBy(() -> keyUse.validate(null)).hasMessage("result is marked @NonNull but is null");
97
98         PfKeyUse testKeyUse = new PfKeyUse(new DummyPfConceptKeySub(new PfConceptKey()));
99         assertEquals(testKeyUse, new PfKeyUse(testKeyUse));
100
101         assertThatThrownBy(() -> new PfKeyUse((PfKeyUse) null)).isInstanceOf(NullPointerException.class);
102
103         assertThatThrownBy(() -> keyUse.isNewerThan(null)).hasMessage(OTHER_KEY_IS_NULL);
104
105         assertEquals(false, testKeyUse.isNewerThan(keyUse));
106         assertEquals(false, testKeyUse.isNewerThan(testKeyUse));
107
108         assertEquals(0, testKeyUse.getMajorVersion());
109         assertEquals(0, testKeyUse.getMinorVersion());
110         assertEquals(0, testKeyUse.getPatchVersion());
111     }
112 }