b5b29d30b8f09bc1c8d43b63c9e57537122130ae
[policy/models.git] / models-pdp / src / test / java / org / onap / policy / models / pdp / persistence / concepts / JpaPdpTest.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.pdp.persistence.concepts;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotEquals;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertTrue;
31
32 import org.junit.Test;
33 import org.onap.policy.models.base.PfConceptKey;
34 import org.onap.policy.models.base.PfReferenceKey;
35 import org.onap.policy.models.base.Validated;
36 import org.onap.policy.models.pdp.concepts.Pdp;
37 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
38 import org.onap.policy.models.pdp.enums.PdpState;
39 import org.onap.policy.models.pdp.testconcepts.DummyJpaPdpChild;
40
41 /**
42  * Test the {@link JpaPdp} class.
43  *
44  * @author Liam Fallon (liam.fallon@est.tech)
45  */
46 public class JpaPdpTest {
47
48     private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
49     private static final String PDP1 = "ThePDP";
50
51     @Test
52     public void testJpaPdp() {
53         assertThatThrownBy(() -> {
54             new JpaPdp((JpaPdp) null);
55         }).hasMessageMatching("copyConcept is marked .*ull but is null");
56
57         assertThatThrownBy(() -> {
58             new JpaPdp((PfReferenceKey) null);
59         }).hasMessageMatching(NULL_KEY_ERROR);
60
61         assertThatThrownBy(() -> {
62             new JpaPdp(null, null, null);
63         }).hasMessageMatching(NULL_KEY_ERROR);
64
65         assertThatThrownBy(() -> {
66             new JpaPdp(new PfReferenceKey(), null, null);
67         }).hasMessageMatching("pdpState is marked .*ull but is null");
68
69         assertThatThrownBy(() -> {
70             new JpaPdp(new PfReferenceKey(), PdpState.ACTIVE, null);
71         }).hasMessageMatching("healthy is marked .*ull but is null");
72
73         assertThatThrownBy(() -> {
74             new JpaPdp(null, PdpState.ACTIVE, null);
75         }).hasMessageMatching(NULL_KEY_ERROR);
76
77         assertThatThrownBy(() -> {
78             new JpaPdp(null, PdpState.ACTIVE, PdpHealthStatus.UNKNOWN);
79         }).hasMessageMatching(NULL_KEY_ERROR);
80
81         assertThatThrownBy(() -> {
82             new JpaPdp(null, null, PdpHealthStatus.UNKNOWN);
83         }).hasMessageMatching(NULL_KEY_ERROR);
84
85         assertThatThrownBy(() -> {
86             new JpaPdp((Pdp) null);
87         }).hasMessageMatching("authorativeConcept is marked .*ull but is null");
88
89         assertNotNull(new JpaPdp((new PfReferenceKey())));
90     }
91
92     @Test
93     public void testJpaPdpInstace() {
94         Pdp testPdp = new Pdp();
95         testPdp.setInstanceId(PDP1);
96         JpaPdp testJpaPdp = new JpaPdp();
97         testJpaPdp.setKey(null);
98         testJpaPdp.fromAuthorative(testPdp);
99         assertEquals(PDP1, testJpaPdp.getKey().getLocalName());
100         testJpaPdp.setKey(PfReferenceKey.getNullKey());
101         testJpaPdp.fromAuthorative(testPdp);
102
103         assertThatThrownBy(() -> {
104             testJpaPdp.fromAuthorative(null);
105         }).hasMessageMatching("pdp is marked .*ull but is null");
106
107         assertThatThrownBy(() -> new JpaPdp((JpaPdp) null)).isInstanceOf(NullPointerException.class);
108
109         assertEquals(PDP1, testJpaPdp.getKey().getLocalName());
110         assertEquals(PDP1, new JpaPdp(testPdp).getKey().getLocalName());
111         assertEquals(PDP1, ((PfReferenceKey) new JpaPdp(testPdp).getKeys().get(0)).getLocalName());
112
113         testJpaPdp.clean();
114         assertEquals(PDP1, testJpaPdp.getKey().getLocalName());
115
116         testJpaPdp.setMessage("   A Message   ");
117         testJpaPdp.clean();
118         assertEquals("A Message", testJpaPdp.getMessage());
119     }
120
121     @Test
122     public void testJpaPdpValidation() {
123         Pdp testPdp = new Pdp();
124         testPdp.setInstanceId(PDP1);
125         JpaPdp testJpaPdp = new JpaPdp();
126         testJpaPdp.setKey(PfReferenceKey.getNullKey());
127         testJpaPdp.fromAuthorative(testPdp);
128
129         assertThatThrownBy(() -> {
130             testJpaPdp.validate(null);
131         }).hasMessageMatching("fieldName is marked .*ull but is null");
132
133         assertFalse(testJpaPdp.validate("").isValid());
134         assertThat(testJpaPdp.validate("").getResult())
135                 .contains("parent").contains(Validated.IS_A_NULL_KEY);
136
137         testJpaPdp.getKey().setParentConceptKey(new PfConceptKey("Parent:1.0.0"));
138         assertFalse(testJpaPdp.validate("").isValid());
139         assertThat(testJpaPdp.validate("").getResult())
140             .doesNotContain("\"parent\"")
141             .contains("local name").contains(Validated.IS_NULL);
142
143         testJpaPdp.getKey().setParentLocalName("ParentLocal");
144         assertFalse(testJpaPdp.validate("").isValid());
145         assertThat(testJpaPdp.validate("").getResult())
146             .doesNotContain("local name")
147             .contains("pdpState").contains(Validated.IS_NULL);
148
149         testJpaPdp.setPdpState(PdpState.ACTIVE);
150         assertFalse(testJpaPdp.validate("").isValid());
151         assertThat(testJpaPdp.validate("").getResult())
152             .doesNotContain("pdpState")
153             .contains("healthy").contains(Validated.IS_NULL);
154
155         testJpaPdp.setHealthy(PdpHealthStatus.HEALTHY);
156         assertTrue(testJpaPdp.validate("").isValid());
157     }
158
159     @Test
160     public void testJpaPdpValidationSwapKey() {
161         JpaPdp testJpaPdp = setUpJpaPdp();
162
163         PfReferenceKey savedKey = testJpaPdp.getKey();
164         testJpaPdp.setKey(PfReferenceKey.getNullKey());
165         assertFalse(testJpaPdp.validate("").isValid());
166         testJpaPdp.setKey(savedKey);
167         assertTrue(testJpaPdp.validate("").isValid());
168
169         testJpaPdp.setMessage(null);
170         assertTrue(testJpaPdp.validate("").isValid());
171         testJpaPdp.setMessage("");
172         assertFalse(testJpaPdp.validate("").isValid());
173         testJpaPdp.setMessage("Valid Message");
174         assertTrue(testJpaPdp.validate("").isValid());
175     }
176
177     @Test
178     public void testJpaPdpCompare() {
179         JpaPdp testJpaPdp = setUpJpaPdp();
180
181         JpaPdp otherJpaPdp = new JpaPdp(testJpaPdp);
182         assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
183         assertEquals(-1, testJpaPdp.compareTo(null));
184         assertEquals(0, testJpaPdp.compareTo(testJpaPdp));
185         assertNotEquals(0, testJpaPdp.compareTo(new DummyJpaPdpChild()));
186
187         testJpaPdp.getKey().setParentLocalName("ParentLocal1");
188         assertEquals(1, testJpaPdp.compareTo(otherJpaPdp));
189         testJpaPdp.getKey().setParentLocalName("ParentLocal");
190         assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
191
192         testJpaPdp.setPdpState(PdpState.PASSIVE);
193         assertEquals(-3, testJpaPdp.compareTo(otherJpaPdp));
194         testJpaPdp.setPdpState(PdpState.ACTIVE);
195         assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
196
197         testJpaPdp.setHealthy(PdpHealthStatus.NOT_HEALTHY);
198         assertEquals(1, testJpaPdp.compareTo(otherJpaPdp));
199         testJpaPdp.setHealthy(PdpHealthStatus.HEALTHY);
200         assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
201
202         testJpaPdp.setMessage("Invalid Message");
203         assertEquals(-13, testJpaPdp.compareTo(otherJpaPdp));
204         testJpaPdp.setMessage("Valid Message");
205         assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
206
207         assertEquals(testJpaPdp, new JpaPdp(testJpaPdp));
208     }
209
210     private JpaPdp setUpJpaPdp() {
211         Pdp testPdp = new Pdp();
212         testPdp.setInstanceId(PDP1);
213         JpaPdp testJpaPdp = new JpaPdp();
214         testJpaPdp.setKey(PfReferenceKey.getNullKey());
215         testJpaPdp.fromAuthorative(testPdp);
216         testJpaPdp.getKey().setParentConceptKey(new PfConceptKey("Parent:1.0.0"));
217         testJpaPdp.getKey().setParentLocalName("ParentLocal");
218         testJpaPdp.setPdpState(PdpState.ACTIVE);
219         testJpaPdp.setHealthy(PdpHealthStatus.HEALTHY);
220         testJpaPdp.setMessage("Valid Message");
221         return testJpaPdp;
222     }
223 }