2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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())));
91 Pdp testPdp = new Pdp();
92 testPdp.setInstanceId(PDP1);
93 JpaPdp testJpaPdp = new JpaPdp();
94 testJpaPdp.setKey(null);
95 testJpaPdp.fromAuthorative(testPdp);
96 assertEquals(PDP1, testJpaPdp.getKey().getLocalName());
97 testJpaPdp.setKey(PfReferenceKey.getNullKey());
98 testJpaPdp.fromAuthorative(testPdp);
100 assertThatThrownBy(() -> {
101 testJpaPdp.fromAuthorative(null);
102 }).hasMessageMatching("pdp is marked .*ull but is null");
104 assertThatThrownBy(() -> new JpaPdp((JpaPdp) null)).isInstanceOf(NullPointerException.class);
106 assertEquals(PDP1, testJpaPdp.getKey().getLocalName());
107 assertEquals(PDP1, new JpaPdp(testPdp).getKey().getLocalName());
108 assertEquals(PDP1, ((PfReferenceKey) new JpaPdp(testPdp).getKeys().get(0)).getLocalName());
111 assertEquals(PDP1, testJpaPdp.getKey().getLocalName());
113 testJpaPdp.setMessage(" A Message ");
115 assertEquals("A Message", testJpaPdp.getMessage());
117 assertThatThrownBy(() -> {
118 testJpaPdp.validate(null);
119 }).hasMessageMatching("fieldName is marked .*ull but is null");
121 assertFalse(testJpaPdp.validate("").isValid());
122 assertThat(testJpaPdp.validate("").getResult())
123 .contains("parent").contains(Validated.IS_A_NULL_KEY);
125 testJpaPdp.getKey().setParentConceptKey(new PfConceptKey("Parent:1.0.0"));
126 assertFalse(testJpaPdp.validate("").isValid());
127 assertThat(testJpaPdp.validate("").getResult())
128 .doesNotContain("\"parent\"")
129 .contains("local name").contains(Validated.IS_NULL);
131 testJpaPdp.getKey().setParentLocalName("ParentLocal");
132 assertFalse(testJpaPdp.validate("").isValid());
133 assertThat(testJpaPdp.validate("").getResult())
134 .doesNotContain("local name")
135 .contains("pdpState").contains(Validated.IS_NULL);
137 testJpaPdp.setPdpState(PdpState.ACTIVE);
138 assertFalse(testJpaPdp.validate("").isValid());
139 assertThat(testJpaPdp.validate("").getResult())
140 .doesNotContain("pdpState")
141 .contains("healthy").contains(Validated.IS_NULL);
143 testJpaPdp.setHealthy(PdpHealthStatus.HEALTHY);
144 assertTrue(testJpaPdp.validate("").isValid());
146 PfReferenceKey savedKey = testJpaPdp.getKey();
147 testJpaPdp.setKey(PfReferenceKey.getNullKey());
148 assertFalse(testJpaPdp.validate("").isValid());
149 testJpaPdp.setKey(savedKey);
150 assertTrue(testJpaPdp.validate("").isValid());
152 testJpaPdp.setMessage(null);
153 assertTrue(testJpaPdp.validate("").isValid());
154 testJpaPdp.setMessage("");
155 assertFalse(testJpaPdp.validate("").isValid());
156 testJpaPdp.setMessage("Valid Message");
157 assertTrue(testJpaPdp.validate("").isValid());
159 JpaPdp otherJpaPdp = new JpaPdp(testJpaPdp);
160 assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
161 assertEquals(-1, testJpaPdp.compareTo(null));
162 assertEquals(0, testJpaPdp.compareTo(testJpaPdp));
163 assertNotEquals(0, testJpaPdp.compareTo(new DummyJpaPdpChild()));
165 testJpaPdp.getKey().setParentLocalName("ParentLocal1");
166 assertEquals(1, testJpaPdp.compareTo(otherJpaPdp));
167 testJpaPdp.getKey().setParentLocalName("ParentLocal");
168 assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
170 testJpaPdp.setPdpState(PdpState.PASSIVE);
171 assertEquals(-3, testJpaPdp.compareTo(otherJpaPdp));
172 testJpaPdp.setPdpState(PdpState.ACTIVE);
173 assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
175 testJpaPdp.setHealthy(PdpHealthStatus.NOT_HEALTHY);
176 assertEquals(1, testJpaPdp.compareTo(otherJpaPdp));
177 testJpaPdp.setHealthy(PdpHealthStatus.HEALTHY);
178 assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
180 testJpaPdp.setMessage("Invalid Message");
181 assertEquals(-13, testJpaPdp.compareTo(otherJpaPdp));
182 testJpaPdp.setMessage("Valid Message");
183 assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
185 assertEquals(testJpaPdp, new JpaPdp(testJpaPdp));