Add coverage for untested DBHelper class
[appc.git] / appc-dispatcher / appc-dispatcher-common / appc-data-access-lib / src / test / java / org / onap / appc / dao / util / helper / DBHelperTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2019 Ericsson
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  *
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.appc.dao.util.helper;
23
24 import org.junit.Test;
25 import org.mockito.Mockito;
26 import java.sql.Connection;
27 import java.sql.ResultSet;
28 import java.sql.SQLException;
29 import java.sql.Statement;
30
31 public class DBHelperTest {
32
33     @Test
34     public void testClose() throws SQLException {
35         ResultSet resultSet = Mockito.mock(ResultSet.class);
36         Statement statement = Mockito.mock(Statement.class);
37         Connection connection = Mockito.mock(Connection.class);
38         DBHelper.close(resultSet, statement, connection);
39         Mockito.verify(connection).close();
40     }
41
42     @Test
43     public void testCloseResultSet() throws SQLException {
44         ResultSet resultSet = Mockito.mock(ResultSet.class);
45         Mockito.doThrow(new SQLException()).when(resultSet).close();
46         DBHelper.closeResultSet(resultSet);
47         Mockito.verify(resultSet, Mockito.times(1)).close();
48     }
49
50     @Test
51     public void testCloseResultSetNull() throws SQLException {
52         ResultSet resultSet = Mockito.mock(ResultSet.class);
53         Mockito.doThrow(new SQLException()).when(resultSet).close();
54         DBHelper.closeResultSet(null);
55         Mockito.verify(resultSet, Mockito.times(0)).close();
56     }
57
58     @Test
59     public void testCloseStatement() throws SQLException {
60         Statement statement = Mockito.mock(Statement.class);
61         Mockito.doThrow(new SQLException()).when(statement).close();
62         DBHelper.closeStatement(statement);
63         Mockito.verify(statement, Mockito.times(1)).close();
64     }
65
66     @Test
67     public void testCloseStatementNull() throws SQLException {
68         Statement statement = Mockito.mock(Statement.class);
69         Mockito.doThrow(new SQLException()).when(statement).close();
70         DBHelper.closeStatement(null);
71         Mockito.verify(statement, Mockito.times(0)).close();
72     }
73
74     @Test
75     public void testCloseConnection() throws SQLException {
76         Connection connection = Mockito.mock(Connection.class);
77         Mockito.doThrow(new SQLException()).when(connection).close();
78         DBHelper.closeConnection(connection);
79         Mockito.verify(connection, Mockito.times(1)).close();
80     }
81
82     @Test
83     public void testCloseConnectionNull() throws SQLException {
84         Connection connection = Mockito.mock(Connection.class);
85         Mockito.doThrow(new SQLException()).when(connection).close();
86         DBHelper.closeConnection(null);
87         Mockito.verify(connection, Mockito.times(0)).close();
88     }
89 }