Merge "Fix more sonar issues in models: yaml to dao"
[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 @NonNull 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         }).hasMessage("copyConcept is marked @NonNull but is null");
54
55         assertThatThrownBy(() -> {
56             new JpaPdp((PfReferenceKey) null);
57         }).hasMessage(NULL_KEY_ERROR);
58
59         assertThatThrownBy(() -> {
60             new JpaPdp(null, null, null);
61         }).hasMessage(NULL_KEY_ERROR);
62
63         assertThatThrownBy(() -> {
64             new JpaPdp(new PfReferenceKey(), null, null);
65         }).hasMessage("pdpState is marked @NonNull but is null");
66
67         assertThatThrownBy(() -> {
68             new JpaPdp(new PfReferenceKey(), PdpState.ACTIVE, null);
69         }).hasMessage("healthy is marked @NonNull but is null");
70
71         assertThatThrownBy(() -> {
72             new JpaPdp(null, PdpState.ACTIVE, null);
73         }).hasMessage(NULL_KEY_ERROR);
74
75         assertThatThrownBy(() -> {
76             new JpaPdp(null, PdpState.ACTIVE, PdpHealthStatus.UNKNOWN);
77         }).hasMessage(NULL_KEY_ERROR);
78
79         assertThatThrownBy(() -> {
80             new JpaPdp(null, null, PdpHealthStatus.UNKNOWN);
81         }).hasMessage(NULL_KEY_ERROR);
82
83         assertThatThrownBy(() -> {
84             new JpaPdp((Pdp) null);
85         }).hasMessage("authorativeConcept is marked @NonNull 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         }).hasMessage("pdp is marked @NonNull but is null");
101
102         assertThatThrownBy(() -> {
103             testJpaPdp.copyTo(null);
104         }).hasMessage("target is marked @NonNull but is null");
105
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());
109
110         testJpaPdp.clean();
111         assertEquals(PDP1, testJpaPdp.getKey().getLocalName());
112
113         testJpaPdp.setMessage("   A Message   ");
114         testJpaPdp.clean();
115         assertEquals("A Message", testJpaPdp.getMessage());
116
117         assertThatThrownBy(() -> {
118             testJpaPdp.validate(null);
119         }).hasMessage("resultIn is marked @NonNull but is null");
120
121         assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk());
122         assertTrue(testJpaPdp.validate(new PfValidationResult()).toString()
123                 .contains("INVALID:parent of key is a null key"));
124
125         testJpaPdp.getKey().setParentConceptKey(new PfConceptKey("Parent:1.0.0"));
126         assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk());
127         assertFalse(testJpaPdp.validate(new PfValidationResult()).toString()
128                 .contains("INVALID:parent of key is a null key"));
129         assertTrue(testJpaPdp.validate(new PfValidationResult()).toString()
130                 .contains("INVALID:local name of parent of key is null"));
131
132         testJpaPdp.getKey().setParentLocalName("ParentLocal");
133         assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk());
134         assertFalse(testJpaPdp.validate(new PfValidationResult()).toString()
135                 .contains("INVALID:local name of parent of key is null"));
136         assertTrue(testJpaPdp.validate(new PfValidationResult()).toString()
137                 .contains("INVALID:PDP state may not be null"));
138
139         testJpaPdp.setPdpState(PdpState.ACTIVE);
140         assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk());
141         assertFalse(testJpaPdp.validate(new PfValidationResult()).toString()
142                 .contains("INVALID:PDP state may not be null"));
143         assertTrue(testJpaPdp.validate(new PfValidationResult()).toString()
144                 .contains("INVALID:PDP health status may not be null"));
145
146         testJpaPdp.setHealthy(PdpHealthStatus.HEALTHY);
147         assertTrue(testJpaPdp.validate(new PfValidationResult()).isOk());
148
149         PfReferenceKey savedKey = testJpaPdp.getKey();
150         testJpaPdp.setKey(PfReferenceKey.getNullKey());
151         assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk());
152         testJpaPdp.setKey(savedKey);
153         assertTrue(testJpaPdp.validate(new PfValidationResult()).isOk());
154
155         testJpaPdp.setMessage(null);
156         assertTrue(testJpaPdp.validate(new PfValidationResult()).isOk());
157         testJpaPdp.setMessage("");
158         assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk());
159         testJpaPdp.setMessage("Valid Message");
160         assertTrue(testJpaPdp.validate(new PfValidationResult()).isOk());
161
162         JpaPdp otherJpaPdp = new JpaPdp(testJpaPdp);
163         assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
164         assertEquals(-1, testJpaPdp.compareTo(null));
165         assertEquals(0, testJpaPdp.compareTo(testJpaPdp));
166         assertFalse(testJpaPdp.compareTo(new DummyJpaPdpChild()) == 0);
167
168         testJpaPdp.getKey().setParentLocalName("ParentLocal1");
169         assertEquals(1, testJpaPdp.compareTo(otherJpaPdp));
170         testJpaPdp.getKey().setParentLocalName("ParentLocal");
171         assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
172
173         testJpaPdp.setPdpState(PdpState.PASSIVE);
174         assertEquals(-3, testJpaPdp.compareTo(otherJpaPdp));
175         testJpaPdp.setPdpState(PdpState.ACTIVE);
176         assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
177
178         testJpaPdp.setHealthy(PdpHealthStatus.NOT_HEALTHY);
179         assertEquals(1, testJpaPdp.compareTo(otherJpaPdp));
180         testJpaPdp.setHealthy(PdpHealthStatus.HEALTHY);
181         assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
182
183         testJpaPdp.setMessage("Invalid Message");
184         assertEquals(-13, testJpaPdp.compareTo(otherJpaPdp));
185         testJpaPdp.setMessage("Valid Message");
186         assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
187     }
188 }