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));
59 assertTrue(keyUse.isCompatible(key));
62 assertNotNull(keyUse);
64 PfValidationResult result = new PfValidationResult();
65 result = keyUse.validate(result);
66 assertNotNull(result);
68 assertNotEquals(0, keyUse.hashCode());
70 PfKeyUse clonedKeyUse = new PfKeyUse(keyUse);
71 assertEquals("PfKeyUse(usedKey=PfConceptKey(name=Key, version=0.0.1))", clonedKeyUse.toString());
73 assertFalse(keyUse.hashCode() == 0);
75 assertTrue(keyUse.equals(keyUse));
76 assertTrue(keyUse.equals(clonedKeyUse));
77 assertFalse(keyUse.equals("Hello"));
78 assertTrue(keyUse.equals(new PfKeyUse(key)));
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)));
85 PfKeyUse keyUseNull = new PfKeyUse(PfConceptKey.getNullKey());
86 PfValidationResult resultNull = new PfValidationResult();
87 assertEquals(false, keyUseNull.validate(resultNull).isValid());
91 fail("test should throw an exception");
92 } catch (Exception exc) {
93 assertEquals("key is marked @NonNull but is null", exc.getMessage());
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());
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());
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());
117 PfKeyUse testKeyUse = new PfKeyUse(new DummyPfConceptKeySub(new PfConceptKey()));
118 PfKeyUse targetKeyUse = new PfKeyUse(key);
122 fail("test should throw an exception");
123 } catch (Exception exc) {
124 assertEquals("target is marked @NonNull but is null", exc.getMessage());
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());