2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 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.testconcepts.DummyPfModel;
35 * Test of the PfModel clas.
37 * @author Liam Fallon (liam.fallon@est.tech)
39 public class PfModelTest {
41 private static final String VERSION001 = "0.0.1";
44 public void testPfModel() {
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 public void testPfModelValidation() {
74 PfConceptKey dpmKey = new PfConceptKey("modelKey", VERSION001);
75 DummyPfModel dpm = new DummyPfModel(dpmKey);
76 assertTrue(dpm.validate(new PfValidationResult()).isValid());
78 assertThatThrownBy(() -> dpm.validate(null)).hasMessageMatching("^resultIn is marked .*on.*ull but is null$");
80 dpm.setKey(PfConceptKey.getNullKey());
81 assertFalse(dpm.validate(new PfValidationResult()).isValid());
83 assertTrue(dpm.validate(new PfValidationResult()).isValid());
85 dpm.getKeyList().add(PfReferenceKey.getNullKey());
86 dpm.getKeyList().add(new PfKeyUse(PfReferenceKey.getNullKey()));
87 assertFalse(dpm.validate(new PfValidationResult()).isValid());
88 dpm.getKeyList().clear();
89 assertTrue(dpm.validate(new PfValidationResult()).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(new PfValidationResult()).isValid());
98 PfConceptKey goodCKeyDup = new PfConceptKey(goodCKey);
99 dpm.getKeyList().add(goodCKeyDup);
100 assertFalse(dpm.validate(new PfValidationResult()).isValid());
101 dpm.getKeyList().remove(goodCKeyDup);
102 assertTrue(dpm.validate(new PfValidationResult()).isValid());
104 PfReferenceKey goodRKeyDup = new PfReferenceKey(goodRKey);
105 dpm.getKeyList().add(goodRKeyDup);
106 assertFalse(dpm.validate(new PfValidationResult()).isValid());
107 dpm.getKeyList().remove(goodRKeyDup);
108 assertTrue(dpm.validate(new PfValidationResult()).isValid());
110 PfKeyUse goodCKeyUse = new PfKeyUse(goodCKey);
111 dpm.getKeyList().add(goodCKeyUse);
112 assertTrue(dpm.validate(new PfValidationResult()).isValid());
114 PfKeyUse goodRKeyUse = new PfKeyUse(goodRKey);
115 dpm.getKeyList().add(goodRKeyUse);
116 assertTrue(dpm.validate(new PfValidationResult()).isValid());
118 PfConceptKey badCKey = new PfConceptKey("badCKey", VERSION001);
119 PfKeyUse badCKeyUse = new PfKeyUse(badCKey);
120 dpm.getKeyList().add(badCKeyUse);
121 assertFalse(dpm.validate(new PfValidationResult()).isValid());
122 dpm.getKeyList().remove(badCKeyUse);
123 assertTrue(dpm.validate(new PfValidationResult()).isValid());
125 PfKeyUse badRKeyUse = new PfKeyUse(new PfReferenceKey(badCKey, "badLocalName"));
126 dpm.getKeyList().add(badRKeyUse);
127 assertFalse(dpm.validate(new PfValidationResult()).isValid());
128 dpm.getKeyList().remove(badRKeyUse);
129 assertTrue(dpm.validate(new PfValidationResult()).isValid());