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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.base;
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;
31 import org.junit.Test;
32 import org.onap.policy.models.base.PfKey.Compatibility;
33 import org.onap.policy.models.base.testconcepts.DummyPfConceptKeySub;
35 public class PfKeyUseTest {
37 private static final String OTHER_KEY_IS_NULL = "^otherKey is marked .*on.*ull but is null$";
40 public void testKeyUse() {
41 assertNotNull(new PfKeyUse());
42 assertNotNull(new PfKeyUse(new PfConceptKey()));
43 assertNotNull(new PfKeyUse(new PfReferenceKey()));
45 assertThatThrownBy(() -> new PfKeyUse((PfKeyUse) null))
46 .hasMessageMatching("^copyConcept is marked .*on.*ull but is null$");
48 PfConceptKey key = new PfConceptKey("Key", "0.0.1");
49 PfKeyUse keyUse = new PfKeyUse();
51 assertEquals(key, keyUse.getKey());
52 assertEquals("Key:0.0.1", keyUse.getId());
53 assertEquals(key, keyUse.getKeys().get(0));
54 assertFalse(keyUse.isNullKey());
56 assertEquals(Compatibility.IDENTICAL, keyUse.getCompatibility(key));
58 assertThatThrownBy(() -> key.getCompatibility(null)).hasMessageMatching(OTHER_KEY_IS_NULL);
60 assertTrue(keyUse.isCompatible(key));
63 assertNotNull(keyUse);
65 assertNotNull(keyUse.validate(""));
67 assertNotEquals(0, keyUse.hashCode());
69 PfKeyUse clonedKeyUse = new PfKeyUse(keyUse);
70 assertEquals("PfKeyUse(usedKey=PfConceptKey(name=Key, version=0.0.1))", clonedKeyUse.toString());
72 assertNotEquals(0, keyUse.hashCode());
74 assertEquals(keyUse, keyUse);
75 assertEquals(keyUse, clonedKeyUse);
76 assertNotEquals(keyUse, "Hello");
77 assertEquals(keyUse, new PfKeyUse(key));
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)));
86 public void testNullKey() {
87 PfKeyUse keyUseNull = new PfKeyUse(PfConceptKey.getNullKey());
88 PfKeyUse keyUse = new PfKeyUse();
89 assertEquals(false, keyUseNull.validate("").isValid());
91 assertThatThrownBy(() -> keyUse.setKey(null)).hasMessageMatching("^key is marked .*on.*ull but is null$");
93 assertThatThrownBy(() -> keyUse.getCompatibility(null)).hasMessageMatching(OTHER_KEY_IS_NULL);
95 assertThatThrownBy(() -> keyUse.isCompatible(null)).hasMessageMatching(OTHER_KEY_IS_NULL);
97 assertThatThrownBy(() -> keyUse.validate(null))
98 .hasMessageMatching("^fieldName is marked .*on.*ull but is null$");
100 PfKeyUse testKeyUse = new PfKeyUse(new DummyPfConceptKeySub(new PfConceptKey()));
101 assertEquals(testKeyUse, new PfKeyUse(testKeyUse));
103 assertThatThrownBy(() -> new PfKeyUse((PfKeyUse) null)).isInstanceOf(NullPointerException.class);
105 assertThatThrownBy(() -> keyUse.isNewerThan(null)).hasMessageMatching(OTHER_KEY_IS_NULL);
107 assertEquals(false, testKeyUse.isNewerThan(keyUse));
108 assertEquals(false, testKeyUse.isNewerThan(testKeyUse));
110 assertEquals(0, testKeyUse.getMajorVersion());
111 assertEquals(0, testKeyUse.getMinorVersion());
112 assertEquals(0, testKeyUse.getPatchVersion());