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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.base;
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;
30 import org.junit.Test;
31 import org.onap.policy.models.base.PfKey.Compatibility;
32 import org.onap.policy.models.base.testconcepts.DummyPfConceptKeySub;
34 public class PfKeyUseTest {
36 @SuppressWarnings("unlikely-arg-type")
38 public void testKeyUse() {
39 assertNotNull(new PfKeyUse());
40 assertNotNull(new PfKeyUse(new PfConceptKey()));
41 assertNotNull(new PfKeyUse(new PfReferenceKey()));
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());
50 PfConceptKey key = new PfConceptKey("Key", "0.0.1");
51 PfKeyUse keyUse = new PfKeyUse();
53 assertEquals(key, keyUse.getKey());
54 assertEquals("Key:0.0.1", keyUse.getId());
55 assertEquals(key, keyUse.getKeys().get(0));
56 assertFalse(keyUse.isNullKey());
58 assertEquals(Compatibility.IDENTICAL, keyUse.getCompatibility(key));
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());
67 assertTrue(keyUse.isCompatible(key));
70 assertNotNull(keyUse);
72 PfValidationResult result = new PfValidationResult();
73 result = keyUse.validate(result);
74 assertNotNull(result);
76 assertNotEquals(0, keyUse.hashCode());
78 PfKeyUse clonedKeyUse = new PfKeyUse(keyUse);
79 assertEquals("PfKeyUse(usedKey=PfConceptKey(name=Key, version=0.0.1))", clonedKeyUse.toString());
81 assertFalse(keyUse.hashCode() == 0);
83 assertTrue(keyUse.equals(keyUse));
84 assertTrue(keyUse.equals(clonedKeyUse));
85 assertFalse(keyUse.equals("Hello"));
86 assertTrue(keyUse.equals(new PfKeyUse(key)));
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)));
93 PfKeyUse keyUseNull = new PfKeyUse(PfConceptKey.getNullKey());
94 PfValidationResult resultNull = new PfValidationResult();
95 assertEquals(false, keyUseNull.validate(resultNull).isValid());
99 fail("test should throw an exception");
100 } catch (Exception exc) {
101 assertEquals("key is marked @NonNull but is null", exc.getMessage());
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());
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());
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());
125 PfKeyUse testKeyUse = new PfKeyUse(new DummyPfConceptKeySub(new PfConceptKey()));
126 PfKeyUse targetKeyUse = new PfKeyUse(key);
130 fail("test should throw an exception");
131 } catch (Exception exc) {
132 assertEquals("target is marked @NonNull but is null", exc.getMessage());
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());
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());
150 assertEquals(false, testKeyUse.isNewerThan(keyUse));
151 assertEquals(false, testKeyUse.isNewerThan(testKeyUse));
153 assertEquals(0, testKeyUse.getMajorVersion());
154 assertEquals(0, testKeyUse.getMinorVersion());
155 assertEquals(0, testKeyUse.getPatchVersion());