Add Legacy Op Policy Persistence
[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         assertTrue(keyUse.isCompatible(key));
60
61         keyUse.clean();
62         assertNotNull(keyUse);
63
64         PfValidationResult result = new PfValidationResult();
65         result = keyUse.validate(result);
66         assertNotNull(result);
67
68         assertNotEquals(0, keyUse.hashCode());
69
70         PfKeyUse clonedKeyUse = new PfKeyUse(keyUse);
71         assertEquals("PfKeyUse(usedKey=PfConceptKey(name=Key, version=0.0.1))", clonedKeyUse.toString());
72
73         assertFalse(keyUse.hashCode() == 0);
74
75         assertTrue(keyUse.equals(keyUse));
76         assertTrue(keyUse.equals(clonedKeyUse));
77         assertFalse(keyUse.equals("Hello"));
78         assertTrue(keyUse.equals(new PfKeyUse(key)));
79
80         assertEquals(0, keyUse.compareTo(keyUse));
81         assertEquals(0, keyUse.compareTo(clonedKeyUse));
82         assertNotEquals(0, keyUse.compareTo(new PfConceptKey()));
83         assertEquals(0, keyUse.compareTo(new PfKeyUse(key)));
84
85         PfKeyUse keyUseNull = new PfKeyUse(PfConceptKey.getNullKey());
86         PfValidationResult resultNull = new PfValidationResult();
87         assertEquals(false, keyUseNull.validate(resultNull).isValid());
88
89         try {
90             keyUse.setKey(null);
91             fail("test should throw an exception");
92         } catch (Exception exc) {
93             assertEquals("key is marked @NonNull but is null", exc.getMessage());
94         }
95
96         try {
97             keyUse.getCompatibility(null);
98             fail("test should throw an exception");
99         } catch (Exception exc) {
100             assertEquals("otherKey is marked @NonNull but is null", exc.getMessage());
101         }
102
103         try {
104             keyUse.isCompatible(null);
105             fail("test should throw an exception");
106         } catch (Exception exc) {
107             assertEquals("otherKey is marked @NonNull but is null", exc.getMessage());
108         }
109
110         try {
111             keyUse.validate(null);
112             fail("test should throw an exception");
113         } catch (Exception exc) {
114             assertEquals("result is marked @NonNull but is null", exc.getMessage());
115         }
116
117         PfKeyUse testKeyUse = new PfKeyUse(new DummyPfConceptKeySub(new PfConceptKey()));
118         PfKeyUse targetKeyUse = new PfKeyUse(key);
119
120         try {
121             keyUse.copyTo(null);
122             fail("test should throw an exception");
123         } catch (Exception exc) {
124             assertEquals("target is marked @NonNull but is null", exc.getMessage());
125         }
126
127         try {
128             testKeyUse.copyTo(targetKeyUse);
129             keyUse.isCompatible(null);
130             fail("test should throw an exception");
131         } catch (Exception exc) {
132             assertEquals("error copying concept key: Some error message", exc.getMessage());
133         }
134
135         assertEquals(0, testKeyUse.getMajorVersion());
136         assertEquals(0, testKeyUse.getMinorVersion());
137         assertEquals(0, testKeyUse.getPatchVersion());
138     }
139 }