Fix some sonars in policy-models
[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-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
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.assertNotEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30
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;
39
40 /**
41  * Test the {@link JpaPdp} class.
42  *
43  * @author Liam Fallon (liam.fallon@est.tech)
44  */
45 public class JpaPdpTest {
46
47     private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
48     private static final String PDP1 = "ThePDP";
49
50     @Test
51     public void testJpaPdp() {
52         assertThatThrownBy(() -> {
53             new JpaPdp((JpaPdp) null);
54         }).hasMessageMatching("copyConcept is marked .*ull but is null");
55
56         assertThatThrownBy(() -> {
57             new JpaPdp((PfReferenceKey) null);
58         }).hasMessageMatching(NULL_KEY_ERROR);
59
60         assertThatThrownBy(() -> {
61             new JpaPdp(null, null, null);
62         }).hasMessageMatching(NULL_KEY_ERROR);
63
64         assertThatThrownBy(() -> {
65             new JpaPdp(new PfReferenceKey(), null, null);
66         }).hasMessageMatching("pdpState is marked .*ull but is null");
67
68         assertThatThrownBy(() -> {
69             new JpaPdp(new PfReferenceKey(), PdpState.ACTIVE, null);
70         }).hasMessageMatching("healthy is marked .*ull but is null");
71
72         assertThatThrownBy(() -> {
73             new JpaPdp(null, PdpState.ACTIVE, null);
74         }).hasMessageMatching(NULL_KEY_ERROR);
75
76         assertThatThrownBy(() -> {
77             new JpaPdp(null, PdpState.ACTIVE, PdpHealthStatus.UNKNOWN);
78         }).hasMessageMatching(NULL_KEY_ERROR);
79
80         assertThatThrownBy(() -> {
81             new JpaPdp(null, null, PdpHealthStatus.UNKNOWN);
82         }).hasMessageMatching(NULL_KEY_ERROR);
83
84         assertThatThrownBy(() -> {
85             new JpaPdp((Pdp) null);
86         }).hasMessageMatching("authorativeConcept is marked .*ull but is null");
87
88         assertNotNull(new JpaPdp((new PfReferenceKey())));
89
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);
98
99         assertThatThrownBy(() -> {
100             testJpaPdp.fromAuthorative(null);
101         }).hasMessageMatching("pdp is marked .*ull but is null");
102
103         assertThatThrownBy(() -> new JpaPdp((JpaPdp) null)).isInstanceOf(NullPointerException.class);
104
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());
108
109         testJpaPdp.clean();
110         assertEquals(PDP1, testJpaPdp.getKey().getLocalName());
111
112         testJpaPdp.setMessage("   A Message   ");
113         testJpaPdp.clean();
114         assertEquals("A Message", testJpaPdp.getMessage());
115
116         assertThatThrownBy(() -> {
117             testJpaPdp.validate(null);
118         }).hasMessageMatching("resultIn is marked .*ull but is null");
119
120         assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk());
121         assertTrue(testJpaPdp.validate(new PfValidationResult()).toString()
122                 .contains("INVALID:parent of key is a null key"));
123
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"));
130
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"));
137
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"));
144
145         testJpaPdp.setHealthy(PdpHealthStatus.HEALTHY);
146         assertTrue(testJpaPdp.validate(new PfValidationResult()).isOk());
147
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());
153
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());
160
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()));
166
167         testJpaPdp.getKey().setParentLocalName("ParentLocal1");
168         assertEquals(1, testJpaPdp.compareTo(otherJpaPdp));
169         testJpaPdp.getKey().setParentLocalName("ParentLocal");
170         assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
171
172         testJpaPdp.setPdpState(PdpState.PASSIVE);
173         assertEquals(-3, testJpaPdp.compareTo(otherJpaPdp));
174         testJpaPdp.setPdpState(PdpState.ACTIVE);
175         assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
176
177         testJpaPdp.setHealthy(PdpHealthStatus.NOT_HEALTHY);
178         assertEquals(1, testJpaPdp.compareTo(otherJpaPdp));
179         testJpaPdp.setHealthy(PdpHealthStatus.HEALTHY);
180         assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
181
182         testJpaPdp.setMessage("Invalid Message");
183         assertEquals(-13, testJpaPdp.compareTo(otherJpaPdp));
184         testJpaPdp.setMessage("Valid Message");
185         assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
186
187         assertEquals(testJpaPdp, new JpaPdp(testJpaPdp));
188     }
189 }