cf7c41f6b8ee931a7ccb74b87b267217e5df3c10
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfModelTest.java
1 package org.onap.policy.models.base;
2 /*-
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
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 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;
27
28 import org.junit.Test;
29 import org.onap.policy.models.base.testconcepts.DummyPfModel;
30
31 /**
32  * Test of the PfModel clas.
33  *
34  * @author Liam Fallon (liam.fallon@est.tech)
35  */
36 public class PfModelTest {
37
38     @Test
39     public void testPfModel() {
40         assertNotNull(new DummyPfModel());
41         assertNotNull(new DummyPfModel(new PfConceptKey()));
42         assertNotNull(new DummyPfModel(new DummyPfModel()));
43
44         try {
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());
49         }
50
51         try {
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());
57         }
58
59         DummyPfModel dpm = new DummyPfModel(new PfConceptKey("modelKey", "0.0.1"));
60         DummyPfModel dpmClone = new DummyPfModel(dpm);
61         assertEquals(dpm, dpmClone);
62
63         assertEquals(1, dpm.getKeys().size());
64
65         dpmClone.clean();
66         assertEquals(dpm, dpmClone);
67
68         try {
69             dpm.copyTo(null);
70             fail("test should throw an exception");
71         } catch (Exception exc) {
72             assertEquals("target is marked @NonNull but is null", exc.getMessage());
73         }
74
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);
79     }
80
81     @Test
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());
86
87         try {
88             dpm.validate(null);
89             fail("test should throw an exception");
90         } catch (Exception exc) {
91             assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
92         }
93
94         dpm.setKey(PfConceptKey.getNullKey());
95         assertFalse(dpm.validate(new PfValidationResult()).isValid());
96         dpm.setKey(dpmKey);
97         assertTrue(dpm.validate(new PfValidationResult()).isValid());
98
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());
104
105         PfConceptKey goodCKey = new PfConceptKey("goodCKey", "0.0.1");
106         PfReferenceKey goodRKey = new PfReferenceKey(goodCKey, "goodLocalName");
107
108         dpm.getKeyList().add(goodCKey);
109         dpm.getKeyList().add(goodRKey);
110         assertTrue(dpm.validate(new PfValidationResult()).isValid());
111
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());
117
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());
123
124         PfKeyUse goodCKeyUse = new PfKeyUse(goodCKey);
125         dpm.getKeyList().add(goodCKeyUse);
126         assertTrue(dpm.validate(new PfValidationResult()).isValid());
127
128         PfKeyUse goodRKeyUse = new PfKeyUse(goodRKey);
129         dpm.getKeyList().add(goodRKeyUse);
130         assertTrue(dpm.validate(new PfValidationResult()).isValid());
131
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());
138
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());
144     }
145 }