Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / comm / PdpStatusMessageHandlerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2023 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.comm;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25
26 import java.io.Serial;
27 import java.sql.SQLIntegrityConstraintViolationException;
28 import org.hibernate.HibernateException;
29 import org.junit.jupiter.api.BeforeAll;
30 import org.junit.jupiter.api.Test;
31 import org.onap.policy.common.utils.services.Registry;
32 import org.onap.policy.pap.main.PolicyPapApplication;
33 import org.springframework.boot.test.context.SpringBootTest;
34 import org.springframework.test.annotation.DirtiesContext;
35 import org.springframework.test.context.ActiveProfiles;
36
37 @SpringBootTest(
38     classes = PolicyPapApplication.class,
39     webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
40     properties = {"db.initialize=false"})
41 @ActiveProfiles("test")
42 @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
43 class PdpStatusMessageHandlerTest {
44
45     @BeforeAll
46     public static void setupClass() {
47         Registry.newRegistry();
48     }
49
50     @Test
51     void testIsDuplicateKeyException() {
52
53         // @formatter:off
54
55         // null exception
56         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(null, HibernateException.class)).isFalse();
57
58         // plain exception
59         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
60                         new Exception(), HibernateException.class))
61             .isFalse();
62
63         // cause is also plain
64         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
65                         new Exception(
66                             new Exception()), HibernateException.class))
67             .isFalse();
68
69         // dup key
70         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
71                         new SQLIntegrityConstraintViolationException(), HibernateException.class))
72             .isTrue();
73
74         // cause is dup key
75         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
76                         new Exception(
77                             new SQLIntegrityConstraintViolationException()), HibernateException.class))
78             .isTrue();
79
80         // eclipselink exception, no internal exception
81         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
82                         new MyHibernateException(), HibernateException.class))
83             .isFalse();
84
85         // eclipselink exception, cause is plain
86         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
87                         new MyHibernateException(
88                             new Exception()), HibernateException.class))
89             .isFalse();
90
91         // eclipselink exception, cause is dup
92         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
93                         new MyHibernateException(
94                             new SQLIntegrityConstraintViolationException()), HibernateException.class))
95             .isTrue();
96
97         // multiple cause both inside and outside the eclipselink exception
98         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
99                         new Exception(
100                             new Exception(
101                                 new MyHibernateException(
102                                     new Exception(
103                                         new SQLIntegrityConstraintViolationException())))), HibernateException.class))
104             .isTrue();
105
106         // @formatter:on
107     }
108
109     public static class MyHibernateException extends HibernateException {
110         @Serial
111         private static final long serialVersionUID = 1L;
112
113         public MyHibernateException() {
114             super("");
115         }
116
117         public MyHibernateException(Exception exception) {
118             super(exception);
119         }
120     }
121 }