Sonar Fixes policy/models, removing model-yaml
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfModelTest.java
1 /*-
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
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(new PfValidationResult()).isValid());
77
78         assertThatThrownBy(() -> dpm.validate(null)).hasMessageMatching("^resultIn is marked .*on.*ull but is null$");
79
80         dpm.setKey(PfConceptKey.getNullKey());
81         assertFalse(dpm.validate(new PfValidationResult()).isValid());
82         dpm.setKey(dpmKey);
83         assertTrue(dpm.validate(new PfValidationResult()).isValid());
84
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());
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(new PfValidationResult()).isValid());
97
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());
103
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());
109
110         PfKeyUse goodCKeyUse = new PfKeyUse(goodCKey);
111         dpm.getKeyList().add(goodCKeyUse);
112         assertTrue(dpm.validate(new PfValidationResult()).isValid());
113
114         PfKeyUse goodRKeyUse = new PfKeyUse(goodRKey);
115         dpm.getKeyList().add(goodRKeyUse);
116         assertTrue(dpm.validate(new PfValidationResult()).isValid());
117
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());
124
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());
130     }
131 }