1 package org.onap.policy.models.base;
3 * ============LICENSE_START=======================================================
4 * Copyright (C) 2019 Nordix Foundation.
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 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
28 import org.junit.Test;
29 import org.onap.policy.models.base.testconcepts.DummyPfModel;
32 * Test of the PfModel clas.
34 * @author Liam Fallon (liam.fallon@est.tech)
36 public class PfModelTest {
39 public void testPfModel() {
40 assertNotNull(new DummyPfModel());
41 assertNotNull(new DummyPfModel(new PfConceptKey()));
42 assertNotNull(new DummyPfModel(new DummyPfModel()));
45 new DummyPfModel((PfConceptKey)null);
46 fail("test should throw an exception");
47 } catch (Exception exc) {
48 assertEquals("key is marked @NonNull but is null", exc.getMessage());
52 DummyPfModel nullModel = null;
53 new DummyPfModel(nullModel);
54 fail("test should throw an exception");
55 } catch (Exception exc) {
56 assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
59 DummyPfModel dpm = new DummyPfModel(new PfConceptKey("modelKey", "0.0.1"));
60 DummyPfModel dpmClone = new DummyPfModel(dpm);
61 assertEquals(dpm, dpmClone);
63 assertEquals(1, dpm.getKeys().size());
66 assertEquals(dpm, dpmClone);
70 fail("test should throw an exception");
71 } catch (Exception exc) {
72 assertEquals("target is marked @NonNull but is null", exc.getMessage());
75 assertEquals(0, dpm.compareTo(dpmClone));
76 assertEquals(-1, dpm.compareTo(null));
77 assertEquals(0, dpm.compareTo(dpm));
78 assertFalse(dpm.compareTo(dpm.getKey()) == 0);
82 public void testPfModelValidation() {
83 PfConceptKey dpmKey = new PfConceptKey("modelKey", "0.0.1");
84 DummyPfModel dpm = new DummyPfModel(dpmKey);
85 assertTrue(dpm.validate(new PfValidationResult()).isValid());
89 fail("test should throw an exception");
90 } catch (Exception exc) {
91 assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
94 dpm.setKey(PfConceptKey.getNullKey());
95 assertFalse(dpm.validate(new PfValidationResult()).isValid());
97 assertTrue(dpm.validate(new PfValidationResult()).isValid());
99 dpm.getKeyList().add(PfReferenceKey.getNullKey());
100 dpm.getKeyList().add(new PfKeyUse(PfReferenceKey.getNullKey()));
101 assertFalse(dpm.validate(new PfValidationResult()).isValid());
102 dpm.getKeyList().clear();
103 assertTrue(dpm.validate(new PfValidationResult()).isValid());
105 PfConceptKey goodCKey = new PfConceptKey("goodCKey", "0.0.1");
106 PfReferenceKey goodRKey = new PfReferenceKey(goodCKey, "goodLocalName");
108 dpm.getKeyList().add(goodCKey);
109 dpm.getKeyList().add(goodRKey);
110 assertTrue(dpm.validate(new PfValidationResult()).isValid());
112 PfConceptKey goodCKeyDup = new PfConceptKey(goodCKey);
113 dpm.getKeyList().add(goodCKeyDup);
114 assertFalse(dpm.validate(new PfValidationResult()).isValid());
115 dpm.getKeyList().remove(goodCKeyDup);
116 assertTrue(dpm.validate(new PfValidationResult()).isValid());
118 PfReferenceKey goodRKeyDup = new PfReferenceKey(goodRKey);
119 dpm.getKeyList().add(goodRKeyDup);
120 assertFalse(dpm.validate(new PfValidationResult()).isValid());
121 dpm.getKeyList().remove(goodRKeyDup);
122 assertTrue(dpm.validate(new PfValidationResult()).isValid());
124 PfKeyUse goodCKeyUse = new PfKeyUse(goodCKey);
125 dpm.getKeyList().add(goodCKeyUse);
126 assertTrue(dpm.validate(new PfValidationResult()).isValid());
128 PfKeyUse goodRKeyUse = new PfKeyUse(goodRKey);
129 dpm.getKeyList().add(goodRKeyUse);
130 assertTrue(dpm.validate(new PfValidationResult()).isValid());
132 PfConceptKey badCKey = new PfConceptKey("badCKey", "0.0.1");
133 PfKeyUse badCKeyUse = new PfKeyUse(badCKey);
134 dpm.getKeyList().add(badCKeyUse);
135 assertFalse(dpm.validate(new PfValidationResult()).isValid());
136 dpm.getKeyList().remove(badCKeyUse);
137 assertTrue(dpm.validate(new PfValidationResult()).isValid());
139 PfKeyUse badRKeyUse = new PfKeyUse(new PfReferenceKey(badCKey, "badLocalName"));
140 dpm.getKeyList().add(badRKeyUse);
141 assertFalse(dpm.validate(new PfValidationResult()).isValid());
142 dpm.getKeyList().remove(badRKeyUse);
143 assertTrue(dpm.validate(new PfValidationResult()).isValid());