2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.pap.main.comm;
24 import static org.assertj.core.api.Assertions.assertThat;
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;
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 {
46 public static void setupClass() {
47 Registry.newRegistry();
51 void testIsDuplicateKeyException() {
56 assertThat(PdpStatusMessageHandler.isDuplicateKeyException(null, HibernateException.class)).isFalse();
59 assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
60 new Exception(), HibernateException.class))
63 // cause is also plain
64 assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
66 new Exception()), HibernateException.class))
70 assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
71 new SQLIntegrityConstraintViolationException(), HibernateException.class))
75 assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
77 new SQLIntegrityConstraintViolationException()), HibernateException.class))
80 // eclipselink exception, no internal exception
81 assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
82 new MyHibernateException(), HibernateException.class))
85 // eclipselink exception, cause is plain
86 assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
87 new MyHibernateException(
88 new Exception()), HibernateException.class))
91 // eclipselink exception, cause is dup
92 assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
93 new MyHibernateException(
94 new SQLIntegrityConstraintViolationException()), HibernateException.class))
97 // multiple cause both inside and outside the eclipselink exception
98 assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
101 new MyHibernateException(
103 new SQLIntegrityConstraintViolationException())))), HibernateException.class))
109 public static class MyHibernateException extends HibernateException {
111 private static final long serialVersionUID = 1L;
113 public MyHibernateException() {
117 public MyHibernateException(Exception exception) {