2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021, 2024 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.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertNotEquals;
28 import static org.junit.jupiter.api.Assertions.assertNotNull;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
31 import org.junit.jupiter.api.Test;
32 import org.onap.policy.models.base.testconcepts.DummyPfModel;
35 * Test of the PfModel clas.
37 * @author Liam Fallon (liam.fallon@est.tech)
41 private static final String VERSION001 = "0.0.1";
45 assertNotNull(new DummyPfModel());
46 assertNotNull(new DummyPfModel(new PfConceptKey()));
47 assertNotNull(new DummyPfModel(new DummyPfModel()));
49 assertThatThrownBy(() -> new DummyPfModel((PfConceptKey) null))
50 .hasMessageMatching("^key is marked .*on.*ull but is null$");
52 assertThatThrownBy(() -> new DummyPfModel((DummyPfModel) null))
53 .hasMessageMatching("^copyConcept is marked .*on.*ull but is null$");
55 DummyPfModel dpm = new DummyPfModel(new PfConceptKey("modelKey", VERSION001));
56 DummyPfModel dpmClone = new DummyPfModel(dpm);
57 assertEquals(dpm, dpmClone);
59 assertEquals(1, dpm.getKeys().size());
62 assertEquals(dpm, dpmClone);
64 assertThatThrownBy(() -> new DummyPfModel((DummyPfModel) null)).isInstanceOf(NullPointerException.class);
66 assertEquals(0, dpm.compareTo(dpmClone));
67 assertEquals(-1, dpm.compareTo(null));
68 assertEquals(0, dpm.compareTo(dpm));
69 assertNotEquals(0, dpm.compareTo(dpm.getKey()));
73 void testPfModelValidation() {
74 PfConceptKey dpmKey = new PfConceptKey("modelKey", VERSION001);
75 DummyPfModel dpm = new DummyPfModel(dpmKey);
76 assertTrue(dpm.validate("").isValid());
78 assertThatThrownBy(() -> dpm.validate(null)).hasMessageMatching("^fieldName is marked .*on.*ull but is null$");
80 dpm.setKey(PfConceptKey.getNullKey());
81 assertFalse(dpm.validate("").isValid());
83 assertTrue(dpm.validate("").isValid());
85 dpm.getKeyList().add(PfReferenceKey.getNullKey());
86 dpm.getKeyList().add(new PfKeyUse(PfReferenceKey.getNullKey()));
87 assertFalse(dpm.validate("").isValid());
88 dpm.getKeyList().clear();
89 assertTrue(dpm.validate("").isValid());
91 PfConceptKey goodCKey = new PfConceptKey("goodCKey", VERSION001);
92 PfReferenceKey goodRKey = new PfReferenceKey(goodCKey, "goodLocalName");
94 dpm.getKeyList().add(goodCKey);
95 dpm.getKeyList().add(goodRKey);
96 assertTrue(dpm.validate("").isValid());
100 void testPfReferenceValidation() {
101 PfConceptKey dpmKey = new PfConceptKey("modelKey", VERSION001);
102 DummyPfModel dpm = new DummyPfModel(dpmKey);
104 PfConceptKey goodCKey = new PfConceptKey("goodCKey", VERSION001);
105 PfReferenceKey goodRKey = new PfReferenceKey(goodCKey, "goodLocalName");
107 dpm.getKeyList().add(goodCKey);
108 dpm.getKeyList().add(goodRKey);
109 assertTrue(dpm.validate("").isValid());
111 PfConceptKey goodCKeyDup = new PfConceptKey(goodCKey);
112 dpm.getKeyList().add(goodCKeyDup);
113 assertFalse(dpm.validate("").isValid());
114 dpm.getKeyList().remove(goodCKeyDup);
115 assertTrue(dpm.validate("").isValid());
117 PfReferenceKey goodRKeyDup = new PfReferenceKey(goodRKey);
118 dpm.getKeyList().add(goodRKeyDup);
119 assertFalse(dpm.validate("").isValid());
120 dpm.getKeyList().remove(goodRKeyDup);
121 assertTrue(dpm.validate("").isValid());
123 PfKeyUse goodCKeyUse = new PfKeyUse(goodCKey);
124 dpm.getKeyList().add(goodCKeyUse);
125 assertTrue(dpm.validate("").isValid());
127 PfKeyUse goodRKeyUse = new PfKeyUse(goodRKey);
128 dpm.getKeyList().add(goodRKeyUse);
129 assertTrue(dpm.validate("").isValid());
131 PfConceptKey badCKey = new PfConceptKey("badCKey", VERSION001);
132 PfKeyUse badCKeyUse = new PfKeyUse(badCKey);
133 dpm.getKeyList().add(badCKeyUse);
134 assertFalse(dpm.validate("").isValid());
135 dpm.getKeyList().remove(badCKeyUse);
136 assertTrue(dpm.validate("").isValid());
138 PfKeyUse badRKeyUse = new PfKeyUse(new PfReferenceKey(badCKey, "badLocalName"));
139 dpm.getKeyList().add(badRKeyUse);
140 assertFalse(dpm.validate("").isValid());
141 dpm.getKeyList().remove(badRKeyUse);
142 assertTrue(dpm.validate("").isValid());