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.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
31 import org.junit.Test;
32 import org.onap.policy.models.base.PfConceptKey;
33 import org.onap.policy.models.base.PfReferenceKey;
34 import org.onap.policy.models.base.PfValidationResult;
35 import org.onap.policy.models.pdp.concepts.Pdp;
36 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
37 import org.onap.policy.models.pdp.enums.PdpState;
38 import org.onap.policy.models.pdp.testconcepts.DummyJpaPdpChild;
41 * Test the {@link JpaPdp} class.
43 * @author Liam Fallon (liam.fallon@est.tech)
45 public class JpaPdpTest {
47 private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
48 private static final String PDP1 = "ThePDP";
51 public void testJpaPdp() {
52 assertThatThrownBy(() -> {
53 new JpaPdp((JpaPdp) null);
54 }).hasMessageMatching("copyConcept is marked .*ull but is null");
56 assertThatThrownBy(() -> {
57 new JpaPdp((PfReferenceKey) null);
58 }).hasMessageMatching(NULL_KEY_ERROR);
60 assertThatThrownBy(() -> {
61 new JpaPdp(null, null, null);
62 }).hasMessageMatching(NULL_KEY_ERROR);
64 assertThatThrownBy(() -> {
65 new JpaPdp(new PfReferenceKey(), null, null);
66 }).hasMessageMatching("pdpState is marked .*ull but is null");
68 assertThatThrownBy(() -> {
69 new JpaPdp(new PfReferenceKey(), PdpState.ACTIVE, null);
70 }).hasMessageMatching("healthy is marked .*ull but is null");
72 assertThatThrownBy(() -> {
73 new JpaPdp(null, PdpState.ACTIVE, null);
74 }).hasMessageMatching(NULL_KEY_ERROR);
76 assertThatThrownBy(() -> {
77 new JpaPdp(null, PdpState.ACTIVE, PdpHealthStatus.UNKNOWN);
78 }).hasMessageMatching(NULL_KEY_ERROR);
80 assertThatThrownBy(() -> {
81 new JpaPdp(null, null, PdpHealthStatus.UNKNOWN);
82 }).hasMessageMatching(NULL_KEY_ERROR);
84 assertThatThrownBy(() -> {
85 new JpaPdp((Pdp) null);
86 }).hasMessageMatching("authorativeConcept is marked .*ull but is null");
88 assertNotNull(new JpaPdp((new PfReferenceKey())));
90 Pdp testPdp = new Pdp();
91 testPdp.setInstanceId(PDP1);
92 JpaPdp testJpaPdp = new JpaPdp();
93 testJpaPdp.setKey(null);
94 testJpaPdp.fromAuthorative(testPdp);
95 assertEquals(PDP1, testJpaPdp.getKey().getLocalName());
96 testJpaPdp.setKey(PfReferenceKey.getNullKey());
97 testJpaPdp.fromAuthorative(testPdp);
99 assertThatThrownBy(() -> {
100 testJpaPdp.fromAuthorative(null);
101 }).hasMessageMatching("pdp is marked .*ull but is null");
103 assertThatThrownBy(() -> new JpaPdp((JpaPdp) null)).isInstanceOf(NullPointerException.class);
105 assertEquals(PDP1, testJpaPdp.getKey().getLocalName());
106 assertEquals(PDP1, new JpaPdp(testPdp).getKey().getLocalName());
107 assertEquals(PDP1, ((PfReferenceKey) new JpaPdp(testPdp).getKeys().get(0)).getLocalName());
110 assertEquals(PDP1, testJpaPdp.getKey().getLocalName());
112 testJpaPdp.setMessage(" A Message ");
114 assertEquals("A Message", testJpaPdp.getMessage());
116 assertThatThrownBy(() -> {
117 testJpaPdp.validate(null);
118 }).hasMessageMatching("resultIn is marked .*ull but is null");
120 assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk());
121 assertTrue(testJpaPdp.validate(new PfValidationResult()).toString()
122 .contains("INVALID:parent of key is a null key"));
124 testJpaPdp.getKey().setParentConceptKey(new PfConceptKey("Parent:1.0.0"));
125 assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk());
126 assertFalse(testJpaPdp.validate(new PfValidationResult()).toString()
127 .contains("INVALID:parent of key is a null key"));
128 assertTrue(testJpaPdp.validate(new PfValidationResult()).toString()
129 .contains("INVALID:local name of parent of key is null"));
131 testJpaPdp.getKey().setParentLocalName("ParentLocal");
132 assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk());
133 assertFalse(testJpaPdp.validate(new PfValidationResult()).toString()
134 .contains("INVALID:local name of parent of key is null"));
135 assertTrue(testJpaPdp.validate(new PfValidationResult()).toString()
136 .contains("INVALID:PDP state may not be null"));
138 testJpaPdp.setPdpState(PdpState.ACTIVE);
139 assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk());
140 assertFalse(testJpaPdp.validate(new PfValidationResult()).toString()
141 .contains("INVALID:PDP state may not be null"));
142 assertTrue(testJpaPdp.validate(new PfValidationResult()).toString()
143 .contains("INVALID:PDP health status may not be null"));
145 testJpaPdp.setHealthy(PdpHealthStatus.HEALTHY);
146 assertTrue(testJpaPdp.validate(new PfValidationResult()).isOk());
148 PfReferenceKey savedKey = testJpaPdp.getKey();
149 testJpaPdp.setKey(PfReferenceKey.getNullKey());
150 assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk());
151 testJpaPdp.setKey(savedKey);
152 assertTrue(testJpaPdp.validate(new PfValidationResult()).isOk());
154 testJpaPdp.setMessage(null);
155 assertTrue(testJpaPdp.validate(new PfValidationResult()).isOk());
156 testJpaPdp.setMessage("");
157 assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk());
158 testJpaPdp.setMessage("Valid Message");
159 assertTrue(testJpaPdp.validate(new PfValidationResult()).isOk());
161 JpaPdp otherJpaPdp = new JpaPdp(testJpaPdp);
162 assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
163 assertEquals(-1, testJpaPdp.compareTo(null));
164 assertEquals(0, testJpaPdp.compareTo(testJpaPdp));
165 assertNotEquals(0, testJpaPdp.compareTo(new DummyJpaPdpChild()));
167 testJpaPdp.getKey().setParentLocalName("ParentLocal1");
168 assertEquals(1, testJpaPdp.compareTo(otherJpaPdp));
169 testJpaPdp.getKey().setParentLocalName("ParentLocal");
170 assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
172 testJpaPdp.setPdpState(PdpState.PASSIVE);
173 assertEquals(-3, testJpaPdp.compareTo(otherJpaPdp));
174 testJpaPdp.setPdpState(PdpState.ACTIVE);
175 assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
177 testJpaPdp.setHealthy(PdpHealthStatus.NOT_HEALTHY);
178 assertEquals(1, testJpaPdp.compareTo(otherJpaPdp));
179 testJpaPdp.setHealthy(PdpHealthStatus.HEALTHY);
180 assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
182 testJpaPdp.setMessage("Invalid Message");
183 assertEquals(-13, testJpaPdp.compareTo(otherJpaPdp));
184 testJpaPdp.setMessage("Valid Message");
185 assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
187 assertEquals(testJpaPdp, new JpaPdp(testJpaPdp));