2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.pdp.persistence.concepts;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
29 import org.junit.Test;
30 import org.onap.policy.models.base.PfConceptKey;
31 import org.onap.policy.models.base.PfReferenceKey;
32 import org.onap.policy.models.base.PfValidationResult;
33 import org.onap.policy.models.pdp.concepts.Pdp;
34 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
35 import org.onap.policy.models.pdp.enums.PdpState;
36 import org.onap.policy.models.pdp.persistence.concepts.JpaPdp;
37 import org.onap.policy.models.pdp.testconcepts.DummyJpaPdpChild;
40 * Test the {@link JpaPdp} class.
42 * @author Liam Fallon (liam.fallon@est.tech)
44 public class JpaPdpTest {
47 public void testJpaPdp() {
48 assertThatThrownBy(() -> {
49 new JpaPdp((JpaPdp) null);
50 }).hasMessage("copyConcept is marked @NonNull but is null");
52 assertThatThrownBy(() -> {
53 new JpaPdp((PfReferenceKey) null);
54 }).hasMessage("key is marked @NonNull but is null");
56 assertThatThrownBy(() -> {
57 new JpaPdp(null, null, null);
58 }).hasMessage("key is marked @NonNull but is null");
60 assertThatThrownBy(() -> {
61 new JpaPdp(new PfReferenceKey(), null, null);
62 }).hasMessage("pdpState is marked @NonNull but is null");
64 assertThatThrownBy(() -> {
65 new JpaPdp(new PfReferenceKey(), PdpState.ACTIVE, null);
66 }).hasMessage("healthy is marked @NonNull but is null");
68 assertThatThrownBy(() -> {
69 new JpaPdp(null, PdpState.ACTIVE, null);
70 }).hasMessage("key is marked @NonNull but is null");
72 assertThatThrownBy(() -> {
73 new JpaPdp(null, PdpState.ACTIVE, PdpHealthStatus.UNKNOWN);
74 }).hasMessage("key is marked @NonNull but is null");
76 assertThatThrownBy(() -> {
77 new JpaPdp(null, null, PdpHealthStatus.UNKNOWN);
78 }).hasMessage("key is marked @NonNull but is null");
80 assertThatThrownBy(() -> {
81 new JpaPdp((Pdp) null);
82 }).hasMessage("authorativeConcept is marked @NonNull but is null");
84 assertNotNull(new JpaPdp((new PfReferenceKey())));
86 Pdp testPdp = new Pdp();
87 testPdp.setInstanceId("ThePDP");
88 JpaPdp testJpaPdp = new JpaPdp();
89 testJpaPdp.setKey(null);
90 testJpaPdp.fromAuthorative(testPdp);
91 assertEquals("ThePDP", testJpaPdp.getKey().getLocalName());
92 testJpaPdp.setKey(PfReferenceKey.getNullKey());
93 testJpaPdp.fromAuthorative(testPdp);
95 assertThatThrownBy(() -> {
96 testJpaPdp.fromAuthorative(null);
97 }).hasMessage("pdp is marked @NonNull but is null");
99 assertThatThrownBy(() -> {
100 testJpaPdp.copyTo(null);
101 }).hasMessage("target is marked @NonNull but is null");
103 assertEquals("ThePDP", testJpaPdp.getKey().getLocalName());
104 assertEquals("ThePDP", new JpaPdp(testPdp).getKey().getLocalName());
105 assertEquals("ThePDP", ((PfReferenceKey) new JpaPdp(testPdp).getKeys().get(0)).getLocalName());
108 assertEquals("ThePDP", testJpaPdp.getKey().getLocalName());
110 testJpaPdp.setMessage(" A Message ");
112 assertEquals("A Message", testJpaPdp.getMessage());
114 assertThatThrownBy(() -> {
115 testJpaPdp.validate(null);
116 }).hasMessage("resultIn is marked @NonNull but is null");
118 assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk());
119 assertTrue(testJpaPdp.validate(new PfValidationResult()).toString()
120 .contains("INVALID:parent of key is a null key"));
122 testJpaPdp.getKey().setParentConceptKey(new PfConceptKey("Parent:1.0.0"));
123 assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk());
124 assertFalse(testJpaPdp.validate(new PfValidationResult()).toString()
125 .contains("INVALID:parent of key is a null key"));
126 assertTrue(testJpaPdp.validate(new PfValidationResult()).toString()
127 .contains("INVALID:local name of parent of key is null"));
129 testJpaPdp.getKey().setParentLocalName("ParentLocal");
130 assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk());
131 assertFalse(testJpaPdp.validate(new PfValidationResult()).toString()
132 .contains("INVALID:local name of parent of key is null"));
133 assertTrue(testJpaPdp.validate(new PfValidationResult()).toString()
134 .contains("INVALID:PDP state may not be null"));
136 testJpaPdp.setPdpState(PdpState.ACTIVE);
137 assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk());
138 assertFalse(testJpaPdp.validate(new PfValidationResult()).toString()
139 .contains("INVALID:PDP state may not be null"));
140 assertTrue(testJpaPdp.validate(new PfValidationResult()).toString()
141 .contains("INVALID:PDP health status may not be null"));
143 testJpaPdp.setHealthy(PdpHealthStatus.HEALTHY);
144 assertTrue(testJpaPdp.validate(new PfValidationResult()).isOk());
146 PfReferenceKey savedKey = testJpaPdp.getKey();
147 testJpaPdp.setKey(PfReferenceKey.getNullKey());
148 assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk());
149 testJpaPdp.setKey(savedKey);
150 assertTrue(testJpaPdp.validate(new PfValidationResult()).isOk());
152 testJpaPdp.setMessage("");
153 assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk());
154 testJpaPdp.setMessage("Valid Message");
155 assertTrue(testJpaPdp.validate(new PfValidationResult()).isOk());
157 JpaPdp otherJpaPdp = new JpaPdp(testJpaPdp);
158 assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
159 assertEquals(-1, testJpaPdp.compareTo(null));
160 assertEquals(0, testJpaPdp.compareTo(testJpaPdp));
161 assertFalse(testJpaPdp.compareTo(new DummyJpaPdpChild()) == 0);
163 testJpaPdp.getKey().setParentLocalName("ParentLocal1");
164 assertEquals(1, testJpaPdp.compareTo(otherJpaPdp));
165 testJpaPdp.getKey().setParentLocalName("ParentLocal");
166 assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
168 testJpaPdp.setPdpState(PdpState.PASSIVE);
169 assertEquals(-3, testJpaPdp.compareTo(otherJpaPdp));
170 testJpaPdp.setPdpState(PdpState.ACTIVE);
171 assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
173 testJpaPdp.setHealthy(PdpHealthStatus.NOT_HEALTHY);
174 assertEquals(1, testJpaPdp.compareTo(otherJpaPdp));
175 testJpaPdp.setHealthy(PdpHealthStatus.HEALTHY);
176 assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
178 testJpaPdp.setMessage("Invalid Message");
179 assertEquals(-13, testJpaPdp.compareTo(otherJpaPdp));
180 testJpaPdp.setMessage("Valid Message");
181 assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));