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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.pdp.persistence.concepts;
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;
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;
42 * Test the {@link JpaPdp} class.
44 * @author Liam Fallon (liam.fallon@est.tech)
46 public class JpaPdpTest {
48 private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
49 private static final String PDP1 = "ThePDP";
52 public void testJpaPdp() {
53 assertThatThrownBy(() -> {
54 new JpaPdp((JpaPdp) null);
55 }).hasMessageMatching("copyConcept is marked .*ull but is null");
57 assertThatThrownBy(() -> {
58 new JpaPdp((PfReferenceKey) null);
59 }).hasMessageMatching(NULL_KEY_ERROR);
61 assertThatThrownBy(() -> {
62 new JpaPdp(null, null, null);
63 }).hasMessageMatching(NULL_KEY_ERROR);
65 assertThatThrownBy(() -> {
66 new JpaPdp(new PfReferenceKey(), null, null);
67 }).hasMessageMatching("pdpState is marked .*ull but is null");
69 assertThatThrownBy(() -> {
70 new JpaPdp(new PfReferenceKey(), PdpState.ACTIVE, null);
71 }).hasMessageMatching("healthy is marked .*ull but is null");
73 assertThatThrownBy(() -> {
74 new JpaPdp(null, PdpState.ACTIVE, null);
75 }).hasMessageMatching(NULL_KEY_ERROR);
77 assertThatThrownBy(() -> {
78 new JpaPdp(null, PdpState.ACTIVE, PdpHealthStatus.UNKNOWN);
79 }).hasMessageMatching(NULL_KEY_ERROR);
81 assertThatThrownBy(() -> {
82 new JpaPdp(null, null, PdpHealthStatus.UNKNOWN);
83 }).hasMessageMatching(NULL_KEY_ERROR);
85 assertThatThrownBy(() -> {
86 new JpaPdp((Pdp) null);
87 }).hasMessageMatching("authorativeConcept is marked .*ull but is null");
89 assertNotNull(new JpaPdp((new PfReferenceKey())));
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);
103 assertThatThrownBy(() -> {
104 testJpaPdp.fromAuthorative(null);
105 }).hasMessageMatching("pdp is marked .*ull but is null");
107 assertThatThrownBy(() -> new JpaPdp((JpaPdp) null)).isInstanceOf(NullPointerException.class);
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());
114 assertEquals(PDP1, testJpaPdp.getKey().getLocalName());
116 testJpaPdp.setMessage(" A Message ");
118 assertEquals("A Message", testJpaPdp.getMessage());
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);
129 assertThatThrownBy(() -> {
130 testJpaPdp.validate(null);
131 }).hasMessageMatching("fieldName is marked .*ull but is null");
133 assertFalse(testJpaPdp.validate("").isValid());
134 assertThat(testJpaPdp.validate("").getResult())
135 .contains("parent").contains(Validated.IS_A_NULL_KEY);
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);
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);
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);
155 testJpaPdp.setHealthy(PdpHealthStatus.HEALTHY);
156 assertTrue(testJpaPdp.validate("").isValid());
160 public void testJpaPdpValidationSwapKey() {
161 JpaPdp testJpaPdp = setUpJpaPdp();
163 PfReferenceKey savedKey = testJpaPdp.getKey();
164 testJpaPdp.setKey(PfReferenceKey.getNullKey());
165 assertFalse(testJpaPdp.validate("").isValid());
166 testJpaPdp.setKey(savedKey);
167 assertTrue(testJpaPdp.validate("").isValid());
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());
178 public void testJpaPdpCompare() {
179 JpaPdp testJpaPdp = setUpJpaPdp();
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()));
187 testJpaPdp.getKey().setParentLocalName("ParentLocal1");
188 assertEquals(1, testJpaPdp.compareTo(otherJpaPdp));
189 testJpaPdp.getKey().setParentLocalName("ParentLocal");
190 assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
192 testJpaPdp.setPdpState(PdpState.PASSIVE);
193 assertEquals(-3, testJpaPdp.compareTo(otherJpaPdp));
194 testJpaPdp.setPdpState(PdpState.ACTIVE);
195 assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
197 testJpaPdp.setHealthy(PdpHealthStatus.NOT_HEALTHY);
198 assertEquals(1, testJpaPdp.compareTo(otherJpaPdp));
199 testJpaPdp.setHealthy(PdpHealthStatus.HEALTHY);
200 assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
202 testJpaPdp.setMessage("Invalid Message");
203 assertEquals(-13, testJpaPdp.compareTo(otherJpaPdp));
204 testJpaPdp.setMessage("Valid Message");
205 assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
207 assertEquals(testJpaPdp, new JpaPdp(testJpaPdp));
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");