Consistent returns on Service Template gets
[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 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.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29
30 import org.junit.Test;
31 import org.onap.policy.models.base.testconcepts.DummyPfModel;
32
33 /**
34  * Test of the PfModel clas.
35  *
36  * @author Liam Fallon (liam.fallon@est.tech)
37  */
38 public class PfModelTest {
39
40     private static final String VERSION001 = "0.0.1";
41
42     @Test
43     public void testPfModel() {
44         assertNotNull(new DummyPfModel());
45         assertNotNull(new DummyPfModel(new PfConceptKey()));
46         assertNotNull(new DummyPfModel(new DummyPfModel()));
47
48         assertThatThrownBy(() -> new DummyPfModel((PfConceptKey) null))
49             .hasMessageMatching("^key is marked .*on.*ull but is null$");
50
51         assertThatThrownBy(() -> new DummyPfModel((DummyPfModel) null))
52             .hasMessageMatching("^copyConcept is marked .*on.*ull but is null$");
53
54         DummyPfModel dpm = new DummyPfModel(new PfConceptKey("modelKey", VERSION001));
55         DummyPfModel dpmClone = new DummyPfModel(dpm);
56         assertEquals(dpm, dpmClone);
57
58         assertEquals(1, dpm.getKeys().size());
59
60         dpmClone.clean();
61         assertEquals(dpm, dpmClone);
62
63         assertThatThrownBy(() -> new DummyPfModel((DummyPfModel) null)).isInstanceOf(NullPointerException.class);
64
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);
69     }
70
71     @Test
72     public void testPfModelValidation() {
73         PfConceptKey dpmKey = new PfConceptKey("modelKey", VERSION001);
74         DummyPfModel dpm = new DummyPfModel(dpmKey);
75         assertTrue(dpm.validate(new PfValidationResult()).isValid());
76
77         assertThatThrownBy(() -> dpm.validate(null)).hasMessageMatching("^resultIn is marked .*on.*ull but is null$");
78
79         dpm.setKey(PfConceptKey.getNullKey());
80         assertFalse(dpm.validate(new PfValidationResult()).isValid());
81         dpm.setKey(dpmKey);
82         assertTrue(dpm.validate(new PfValidationResult()).isValid());
83
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());
89
90         PfConceptKey goodCKey = new PfConceptKey("goodCKey", VERSION001);
91         PfReferenceKey goodRKey = new PfReferenceKey(goodCKey, "goodLocalName");
92
93         dpm.getKeyList().add(goodCKey);
94         dpm.getKeyList().add(goodRKey);
95         assertTrue(dpm.validate(new PfValidationResult()).isValid());
96
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());
102
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());
108
109         PfKeyUse goodCKeyUse = new PfKeyUse(goodCKey);
110         dpm.getKeyList().add(goodCKeyUse);
111         assertTrue(dpm.validate(new PfValidationResult()).isValid());
112
113         PfKeyUse goodRKeyUse = new PfKeyUse(goodRKey);
114         dpm.getKeyList().add(goodRKeyUse);
115         assertTrue(dpm.validate(new PfValidationResult()).isValid());
116
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());
123
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());
129     }
130 }