5129bf5f5eae4b61eea6c0b56b88e69092d51738
[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  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.main.comm;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24
25 import java.sql.SQLIntegrityConstraintViolationException;
26 import org.eclipse.persistence.exceptions.EclipseLinkException;
27 import org.junit.Test;
28
29 public class PdpStatusMessageHandlerTest {
30
31     @Test
32     public void testIsDuplicateKeyException() {
33
34         // @formatter:off
35
36         // null exception
37         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(null)).isFalse();
38
39         // plain exception
40         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
41                         new Exception()))
42             .isFalse();
43
44         // cause is also plain
45         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
46                         new Exception(
47                             new Exception())))
48             .isFalse();
49
50         // dup key
51         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
52                         new SQLIntegrityConstraintViolationException()))
53             .isTrue();
54
55         // cause is dup key
56         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
57                         new Exception(
58                             new SQLIntegrityConstraintViolationException())))
59             .isTrue();
60
61         // eclipselink exception, no internal exception
62         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
63                         new MyEclipseLinkException()))
64             .isFalse();
65
66         // eclipselink exception, cause is plain
67         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
68                         new MyEclipseLinkException(
69                             new Exception())))
70             .isFalse();
71
72         // eclipselink exception, cause is dup
73         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
74                         new MyEclipseLinkException(
75                             new SQLIntegrityConstraintViolationException())))
76             .isTrue();
77
78         // multiple cause both inside and outside of the eclipselink exception
79         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
80                         new Exception(
81                             new Exception(
82                                 new MyEclipseLinkException(
83                                     new Exception(
84                                         new SQLIntegrityConstraintViolationException()))))))
85             .isTrue();
86
87         // @formatter:on
88     }
89
90     public static class MyEclipseLinkException extends EclipseLinkException {
91         private static final long serialVersionUID = 1L;
92
93         public MyEclipseLinkException() {
94             // do nothing
95         }
96
97         public MyEclipseLinkException(Exception exception) {
98             setInternalException(exception);
99         }
100     }
101 }