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