2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * ================================================================================
9 * Modifications (C) 2019 Ericsson
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.dao.util.dbcp;
27 import org.apache.commons.dbcp2.BasicDataSource;
28 import org.junit.After;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.rules.ExpectedException;
34 import org.mockito.Mockito;
35 import org.onap.appc.dao.util.exception.DBConnectionPoolException;
36 import org.powermock.reflect.Whitebox;
37 import java.sql.Connection;
38 import java.sql.SQLException;
41 public class DBConnectionPoolTest {
42 private final String connectURI = "jdbc:h2:mem:~/test;MODE=MYSQL;DB_CLOSE_DELAY=-1";
43 private final String username = "sa";
44 private final String password = "sa";
45 private final String driverClass = "org.h2.Driver";
47 private DBConnectionPool dbcp;
48 private DBConnectionPool dbcp2;
49 private Connection connection;
52 public ExpectedException expectedEx = ExpectedException.none();
55 public void setUp() throws Exception {
56 dbcp = new DBConnectionPool(connectURI, username, password, driverClass);
57 dbcp2 = new DBConnectionPool(connectURI, username, password, driverClass);
61 public void testGetConnection() {
63 connection = dbcp.getConnection();
64 } catch (DBConnectionPoolException e) {
65 Assert.fail(e.getMessage());
67 Assert.assertNotNull(connection);
71 public void testGetConnectionSQLExceptionFlow() throws SQLException {
72 DBConnectionPool dbcpSpy = Mockito.spy(new DBConnectionPool(connectURI, username, password, driverClass));
73 BasicDataSource mockDataSource = Mockito.mock(BasicDataSource.class);
74 Mockito.when(mockDataSource.getConnection()).thenThrow(new SQLException());
75 Whitebox.setInternalState(dbcpSpy, "dataSource", mockDataSource);
76 expectedEx.expect(SQLException.class);
77 connection = dbcpSpy.getConnection();
81 public void testGetConnectionDBConnectionPoolExceptionFlow() throws SQLException {
82 DBConnectionPool dbcpSpy = Mockito.spy(new DBConnectionPool(connectURI, username, password, driverClass));
83 BasicDataSource mockDataSource = Mockito.mock(BasicDataSource.class);
84 Mockito.when(mockDataSource.getConnection()).thenReturn(null);
85 Whitebox.setInternalState(dbcpSpy, "dataSource", mockDataSource);
86 expectedEx.expect(DBConnectionPoolException.class);
87 connection = dbcpSpy.getConnection();
91 public void testGetDataSourceStatus() {
92 Map<String, Integer> dataSourceStatus = dbcp.getDataSourceStatus();
93 Assert.assertNotNull(dataSourceStatus);
96 @Test(expected = DBConnectionPoolException.class)
97 public void testShutdown() throws DBConnectionPoolException {
99 connection = dbcp2.getConnection();
100 Assert.assertNull(connection);
104 public void testShutdownException() throws SQLException {
105 DBConnectionPool dbcpSpy =
106 Mockito.spy(new DBConnectionPool(connectURI, username, password, driverClass, 0, 0, 0, 0, 0));
107 BasicDataSource mockDataSource = Mockito.mock(BasicDataSource.class);
108 Mockito.doThrow(new SQLException()).when(mockDataSource).close();
109 Whitebox.setInternalState(dbcpSpy, "dataSource", mockDataSource);
111 Mockito.verify(mockDataSource).close();
115 public void clean() {
122 if (connection != null) {
125 } catch (SQLException e) {