2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 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.assertNotNull;
28 import static org.junit.Assert.assertTrue;
30 import org.junit.Test;
31 import org.onap.policy.models.base.testconcepts.DummyPfModel;
34 * Test of the PfModel clas.
36 * @author Liam Fallon (liam.fallon@est.tech)
38 public class PfModelTest {
40 private static final String VERSION001 = "0.0.1";
43 public void testPfModel() {
44 assertNotNull(new DummyPfModel());
45 assertNotNull(new DummyPfModel(new PfConceptKey()));
46 assertNotNull(new DummyPfModel(new DummyPfModel()));
48 assertThatThrownBy(() -> new DummyPfModel((PfConceptKey) null))
49 .hasMessage("key is marked @NonNull but is null");
51 assertThatThrownBy(() -> new DummyPfModel((DummyPfModel) null))
52 .hasMessage("copyConcept is marked @NonNull but is null");
54 DummyPfModel dpm = new DummyPfModel(new PfConceptKey("modelKey", VERSION001));
55 DummyPfModel dpmClone = new DummyPfModel(dpm);
56 assertEquals(dpm, dpmClone);
58 assertEquals(1, dpm.getKeys().size());
61 assertEquals(dpm, dpmClone);
63 assertThatThrownBy(() -> dpm.copyTo(null)).hasMessage("target is marked @NonNull but is null");
65 assertEquals(0, dpm.compareTo(dpmClone));
66 assertEquals(-1, dpm.compareTo(null));
67 assertEquals(0, dpm.compareTo(dpm));
68 assertFalse(dpm.compareTo(dpm.getKey()) == 0);
72 public void testPfModelValidation() {
73 PfConceptKey dpmKey = new PfConceptKey("modelKey", VERSION001);
74 DummyPfModel dpm = new DummyPfModel(dpmKey);
75 assertTrue(dpm.validate(new PfValidationResult()).isValid());
77 assertThatThrownBy(() -> dpm.validate(null)).hasMessage("resultIn is marked @NonNull but is null");
79 dpm.setKey(PfConceptKey.getNullKey());
80 assertFalse(dpm.validate(new PfValidationResult()).isValid());
82 assertTrue(dpm.validate(new PfValidationResult()).isValid());
84 dpm.getKeyList().add(PfReferenceKey.getNullKey());
85 dpm.getKeyList().add(new PfKeyUse(PfReferenceKey.getNullKey()));
86 assertFalse(dpm.validate(new PfValidationResult()).isValid());
87 dpm.getKeyList().clear();
88 assertTrue(dpm.validate(new PfValidationResult()).isValid());
90 PfConceptKey goodCKey = new PfConceptKey("goodCKey", VERSION001);
91 PfReferenceKey goodRKey = new PfReferenceKey(goodCKey, "goodLocalName");
93 dpm.getKeyList().add(goodCKey);
94 dpm.getKeyList().add(goodRKey);
95 assertTrue(dpm.validate(new PfValidationResult()).isValid());
97 PfConceptKey goodCKeyDup = new PfConceptKey(goodCKey);
98 dpm.getKeyList().add(goodCKeyDup);
99 assertFalse(dpm.validate(new PfValidationResult()).isValid());
100 dpm.getKeyList().remove(goodCKeyDup);
101 assertTrue(dpm.validate(new PfValidationResult()).isValid());
103 PfReferenceKey goodRKeyDup = new PfReferenceKey(goodRKey);
104 dpm.getKeyList().add(goodRKeyDup);
105 assertFalse(dpm.validate(new PfValidationResult()).isValid());
106 dpm.getKeyList().remove(goodRKeyDup);
107 assertTrue(dpm.validate(new PfValidationResult()).isValid());
109 PfKeyUse goodCKeyUse = new PfKeyUse(goodCKey);
110 dpm.getKeyList().add(goodCKeyUse);
111 assertTrue(dpm.validate(new PfValidationResult()).isValid());
113 PfKeyUse goodRKeyUse = new PfKeyUse(goodRKey);
114 dpm.getKeyList().add(goodRKeyUse);
115 assertTrue(dpm.validate(new PfValidationResult()).isValid());
117 PfConceptKey badCKey = new PfConceptKey("badCKey", VERSION001);
118 PfKeyUse badCKeyUse = new PfKeyUse(badCKey);
119 dpm.getKeyList().add(badCKeyUse);
120 assertFalse(dpm.validate(new PfValidationResult()).isValid());
121 dpm.getKeyList().remove(badCKeyUse);
122 assertTrue(dpm.validate(new PfValidationResult()).isValid());
124 PfKeyUse badRKeyUse = new PfKeyUse(new PfReferenceKey(badCKey, "badLocalName"));
125 dpm.getKeyList().add(badRKeyUse);
126 assertFalse(dpm.validate(new PfValidationResult()).isValid());
127 dpm.getKeyList().remove(badRKeyUse);
128 assertTrue(dpm.validate(new PfValidationResult()).isValid());