Fix sonar issues on policy-models
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfModelTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021 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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.base;
23
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;
30
31 import org.junit.Test;
32 import org.onap.policy.models.base.testconcepts.DummyPfModel;
33
34 /**
35  * Test of the PfModel clas.
36  *
37  * @author Liam Fallon (liam.fallon@est.tech)
38  */
39 public class PfModelTest {
40
41     private static final String VERSION001 = "0.0.1";
42
43     @Test
44     public void testPfModel() {
45         assertNotNull(new DummyPfModel());
46         assertNotNull(new DummyPfModel(new PfConceptKey()));
47         assertNotNull(new DummyPfModel(new DummyPfModel()));
48
49         assertThatThrownBy(() -> new DummyPfModel((PfConceptKey) null))
50             .hasMessageMatching("^key is marked .*on.*ull but is null$");
51
52         assertThatThrownBy(() -> new DummyPfModel((DummyPfModel) null))
53             .hasMessageMatching("^copyConcept is marked .*on.*ull but is null$");
54
55         DummyPfModel dpm = new DummyPfModel(new PfConceptKey("modelKey", VERSION001));
56         DummyPfModel dpmClone = new DummyPfModel(dpm);
57         assertEquals(dpm, dpmClone);
58
59         assertEquals(1, dpm.getKeys().size());
60
61         dpmClone.clean();
62         assertEquals(dpm, dpmClone);
63
64         assertThatThrownBy(() -> new DummyPfModel((DummyPfModel) null)).isInstanceOf(NullPointerException.class);
65
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()));
70     }
71
72     @Test
73     public void testPfModelValidation() {
74         PfConceptKey dpmKey = new PfConceptKey("modelKey", VERSION001);
75         DummyPfModel dpm = new DummyPfModel(dpmKey);
76         assertTrue(dpm.validate("").isValid());
77
78         assertThatThrownBy(() -> dpm.validate(null)).hasMessageMatching("^fieldName is marked .*on.*ull but is null$");
79
80         dpm.setKey(PfConceptKey.getNullKey());
81         assertFalse(dpm.validate("").isValid());
82         dpm.setKey(dpmKey);
83         assertTrue(dpm.validate("").isValid());
84
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());
90
91         PfConceptKey goodCKey = new PfConceptKey("goodCKey", VERSION001);
92         PfReferenceKey goodRKey = new PfReferenceKey(goodCKey, "goodLocalName");
93
94         dpm.getKeyList().add(goodCKey);
95         dpm.getKeyList().add(goodRKey);
96         assertTrue(dpm.validate("").isValid());
97     }
98
99     @Test
100     public void testPfReferenceValidation() {
101         PfConceptKey dpmKey = new PfConceptKey("modelKey", VERSION001);
102         DummyPfModel dpm = new DummyPfModel(dpmKey);
103
104         PfConceptKey goodCKey = new PfConceptKey("goodCKey", VERSION001);
105         PfReferenceKey goodRKey = new PfReferenceKey(goodCKey, "goodLocalName");
106
107         dpm.getKeyList().add(goodCKey);
108         dpm.getKeyList().add(goodRKey);
109         assertTrue(dpm.validate("").isValid());
110
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());
116
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());
122
123         PfKeyUse goodCKeyUse = new PfKeyUse(goodCKey);
124         dpm.getKeyList().add(goodCKeyUse);
125         assertTrue(dpm.validate("").isValid());
126
127         PfKeyUse goodRKeyUse = new PfKeyUse(goodRKey);
128         dpm.getKeyList().add(goodRKeyUse);
129         assertTrue(dpm.validate("").isValid());
130
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());
137
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());
143     }
144 }