Add unit test for PDP groups
[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  * ================================================================================
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.pdp.persistence.concepts;
22
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;
28
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;
38
39 /**
40  * Test the {@link JpaPdp} class.
41  *
42  * @author Liam Fallon (liam.fallon@est.tech)
43  */
44 public class JpaPdpTest {
45
46     @Test
47     public void testJpaPdp() {
48         assertThatThrownBy(() -> {
49             new JpaPdp((JpaPdp) null);
50         }).hasMessage("copyConcept is marked @NonNull but is null");
51
52         assertThatThrownBy(() -> {
53             new JpaPdp((PfReferenceKey) null);
54         }).hasMessage("key is marked @NonNull but is null");
55
56         assertThatThrownBy(() -> {
57             new JpaPdp(null, null, null);
58         }).hasMessage("key is marked @NonNull but is null");
59
60         assertThatThrownBy(() -> {
61             new JpaPdp(new PfReferenceKey(), null, null);
62         }).hasMessage("pdpState is marked @NonNull but is null");
63
64         assertThatThrownBy(() -> {
65             new JpaPdp(new PfReferenceKey(), PdpState.ACTIVE, null);
66         }).hasMessage("healthy is marked @NonNull but is null");
67
68         assertThatThrownBy(() -> {
69             new JpaPdp(null, PdpState.ACTIVE, null);
70         }).hasMessage("key is marked @NonNull but is null");
71
72         assertThatThrownBy(() -> {
73             new JpaPdp(null, PdpState.ACTIVE, PdpHealthStatus.UNKNOWN);
74         }).hasMessage("key is marked @NonNull but is null");
75
76         assertThatThrownBy(() -> {
77             new JpaPdp(null, null, PdpHealthStatus.UNKNOWN);
78         }).hasMessage("key is marked @NonNull but is null");
79
80         assertThatThrownBy(() -> {
81             new JpaPdp((Pdp) null);
82         }).hasMessage("authorativeConcept is marked @NonNull but is null");
83
84         assertNotNull(new JpaPdp((new PfReferenceKey())));
85
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);
94
95         assertThatThrownBy(() -> {
96             testJpaPdp.fromAuthorative(null);
97         }).hasMessage("pdp is marked @NonNull but is null");
98
99         assertThatThrownBy(() -> {
100             testJpaPdp.copyTo(null);
101         }).hasMessage("target is marked @NonNull but is null");
102
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());
106
107         testJpaPdp.clean();
108         assertEquals("ThePDP", testJpaPdp.getKey().getLocalName());
109
110         testJpaPdp.setMessage("   A Message   ");
111         testJpaPdp.clean();
112         assertEquals("A Message", testJpaPdp.getMessage());
113
114         assertThatThrownBy(() -> {
115             testJpaPdp.validate(null);
116         }).hasMessage("resultIn is marked @NonNull but is null");
117
118         assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk());
119         assertTrue(testJpaPdp.validate(new PfValidationResult()).toString()
120                 .contains("INVALID:parent of key is a null key"));
121
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"));
128
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"));
135
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"));
142
143         testJpaPdp.setHealthy(PdpHealthStatus.HEALTHY);
144         assertTrue(testJpaPdp.validate(new PfValidationResult()).isOk());
145
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());
151
152         testJpaPdp.setMessage("");
153         assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk());
154         testJpaPdp.setMessage("Valid Message");
155         assertTrue(testJpaPdp.validate(new PfValidationResult()).isOk());
156
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);
162
163         testJpaPdp.getKey().setParentLocalName("ParentLocal1");
164         assertEquals(1, testJpaPdp.compareTo(otherJpaPdp));
165         testJpaPdp.getKey().setParentLocalName("ParentLocal");
166         assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
167
168         testJpaPdp.setPdpState(PdpState.PASSIVE);
169         assertEquals(-3, testJpaPdp.compareTo(otherJpaPdp));
170         testJpaPdp.setPdpState(PdpState.ACTIVE);
171         assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
172
173         testJpaPdp.setHealthy(PdpHealthStatus.NOT_HEALTHY);
174         assertEquals(1, testJpaPdp.compareTo(otherJpaPdp));
175         testJpaPdp.setHealthy(PdpHealthStatus.HEALTHY);
176         assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
177
178         testJpaPdp.setMessage("Invalid Message");
179         assertEquals(-13, testJpaPdp.compareTo(otherJpaPdp));
180         testJpaPdp.setMessage("Valid Message");
181         assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
182     }
183 }