68494309c367ce7e7fe781b607d7259b747ac82b
[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  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.base;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29
30 import org.junit.Test;
31 import org.onap.policy.models.base.PfKey.Compatibility;
32 import org.onap.policy.models.base.testconcepts.DummyPfConceptKeySub;
33
34 public class PfKeyUseTest {
35
36     @SuppressWarnings("unlikely-arg-type")
37     @Test
38     public void testKeyUse() {
39         assertNotNull(new PfKeyUse());
40         assertNotNull(new PfKeyUse(new PfConceptKey()));
41         assertNotNull(new PfKeyUse(new PfReferenceKey()));
42
43         try {
44             new PfKeyUse((PfKeyUse) null);
45             fail("test should throw an exception");
46         } catch (Exception exc) {
47             assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
48         }
49
50         PfConceptKey key = new PfConceptKey("Key", "0.0.1");
51         PfKeyUse keyUse = new PfKeyUse();
52         keyUse.setKey(key);
53         assertEquals(key, keyUse.getKey());
54         assertEquals("Key:0.0.1", keyUse.getId());
55         assertEquals(key, keyUse.getKeys().get(0));
56         assertFalse(keyUse.isNullKey());
57
58         assertEquals(Compatibility.IDENTICAL, keyUse.getCompatibility(key));
59
60         try {
61             key.getCompatibility(null);
62             fail("test should throw an exception");
63         } catch (Exception exc) {
64             assertEquals("otherKey is marked @NonNull but is null", exc.getMessage());
65         }
66
67         assertTrue(keyUse.isCompatible(key));
68
69         keyUse.clean();
70         assertNotNull(keyUse);
71
72         PfValidationResult result = new PfValidationResult();
73         result = keyUse.validate(result);
74         assertNotNull(result);
75
76         assertNotEquals(0, keyUse.hashCode());
77
78         PfKeyUse clonedKeyUse = new PfKeyUse(keyUse);
79         assertEquals("PfKeyUse(usedKey=PfConceptKey(name=Key, version=0.0.1))", clonedKeyUse.toString());
80
81         assertFalse(keyUse.hashCode() == 0);
82
83         assertTrue(keyUse.equals(keyUse));
84         assertTrue(keyUse.equals(clonedKeyUse));
85         assertFalse(keyUse.equals("Hello"));
86         assertTrue(keyUse.equals(new PfKeyUse(key)));
87
88         assertEquals(0, keyUse.compareTo(keyUse));
89         assertEquals(0, keyUse.compareTo(clonedKeyUse));
90         assertNotEquals(0, keyUse.compareTo(new PfConceptKey()));
91         assertEquals(0, keyUse.compareTo(new PfKeyUse(key)));
92
93         PfKeyUse keyUseNull = new PfKeyUse(PfConceptKey.getNullKey());
94         PfValidationResult resultNull = new PfValidationResult();
95         assertEquals(false, keyUseNull.validate(resultNull).isValid());
96
97         try {
98             keyUse.setKey(null);
99             fail("test should throw an exception");
100         } catch (Exception exc) {
101             assertEquals("key is marked @NonNull but is null", exc.getMessage());
102         }
103
104         try {
105             keyUse.getCompatibility(null);
106             fail("test should throw an exception");
107         } catch (Exception exc) {
108             assertEquals("otherKey is marked @NonNull but is null", exc.getMessage());
109         }
110
111         try {
112             keyUse.isCompatible(null);
113             fail("test should throw an exception");
114         } catch (Exception exc) {
115             assertEquals("otherKey is marked @NonNull but is null", exc.getMessage());
116         }
117
118         try {
119             keyUse.validate(null);
120             fail("test should throw an exception");
121         } catch (Exception exc) {
122             assertEquals("result is marked @NonNull but is null", exc.getMessage());
123         }
124
125         PfKeyUse testKeyUse = new PfKeyUse(new DummyPfConceptKeySub(new PfConceptKey()));
126         PfKeyUse targetKeyUse = new PfKeyUse(key);
127
128         try {
129             keyUse.copyTo(null);
130             fail("test should throw an exception");
131         } catch (Exception exc) {
132             assertEquals("target is marked @NonNull but is null", exc.getMessage());
133         }
134
135         try {
136             testKeyUse.copyTo(targetKeyUse);
137             keyUse.isCompatible(null);
138             fail("test should throw an exception");
139         } catch (Exception exc) {
140             assertEquals("error copying concept key: Some error message", exc.getMessage());
141         }
142
143         try {
144             keyUse.isNewerThan(null);
145             fail("test should throw an exception");
146         } catch (Exception exc) {
147             assertEquals("otherKey is marked @NonNull but is null", exc.getMessage());
148         }
149
150         assertEquals(false, testKeyUse.isNewerThan(keyUse));
151         assertEquals(false, testKeyUse.isNewerThan(testKeyUse));
152
153         assertEquals(0, testKeyUse.getMajorVersion());
154         assertEquals(0, testKeyUse.getMinorVersion());
155         assertEquals(0, testKeyUse.getPatchVersion());
156     }
157 }