diff options
author | Joss Armstrong <joss.armstrong@ericsson.com> | 2019-02-06 16:53:34 +0000 |
---|---|---|
committer | Patrick Brady <patrick.brady@att.com> | 2019-02-06 21:51:03 +0000 |
commit | 24ccf46cd06239bcb2173abf0dd43c689a4c8697 (patch) | |
tree | be4b07a3d7c5830400a47f9accde55d01830af48 /appc-dispatcher/appc-dispatcher-common/appc-data-access-lib/src/test | |
parent | 8fdee1fb6c2ce27f245a921536739ef2d4f678a2 (diff) |
Test coverage for DBConnectionPool
Increased class coverage to 100%
Issue-ID: APPC-1394
Change-Id: I7c8adb081b7342a5a18d3a2ca0238625317c6b4d
Signed-off-by: Joss Armstrong <joss.armstrong@ericsson.com>
Diffstat (limited to 'appc-dispatcher/appc-dispatcher-common/appc-data-access-lib/src/test')
-rw-r--r-- | appc-dispatcher/appc-dispatcher-common/appc-data-access-lib/src/test/java/org/onap/appc/dao/util/dbcp/DBConnectionPoolTest.java | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/appc-dispatcher/appc-dispatcher-common/appc-data-access-lib/src/test/java/org/onap/appc/dao/util/dbcp/DBConnectionPoolTest.java b/appc-dispatcher/appc-dispatcher-common/appc-data-access-lib/src/test/java/org/onap/appc/dao/util/dbcp/DBConnectionPoolTest.java index b9ba1daa7..d2ac778a4 100644 --- a/appc-dispatcher/appc-dispatcher-common/appc-data-access-lib/src/test/java/org/onap/appc/dao/util/dbcp/DBConnectionPoolTest.java +++ b/appc-dispatcher/appc-dispatcher-common/appc-data-access-lib/src/test/java/org/onap/appc/dao/util/dbcp/DBConnectionPoolTest.java @@ -25,12 +25,16 @@ package org.onap.appc.dao.util.dbcp; +import org.apache.commons.dbcp2.BasicDataSource; import org.junit.After; import org.junit.Assert; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.Mockito; import org.onap.appc.dao.util.exception.DBConnectionPoolException; - +import org.powermock.reflect.Whitebox; import java.sql.Connection; import java.sql.SQLException; import java.util.Map; @@ -46,6 +50,9 @@ public class DBConnectionPoolTest { private DBConnectionPool dbcp2; private Connection connection; + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + @Before public void setUp() throws Exception { dbcp = new DBConnectionPool(connectURI, username, password, driverClass); @@ -63,6 +70,26 @@ public class DBConnectionPoolTest { } @Test + public void testGetConnectionSQLExceptionFlow() throws SQLException { + DBConnectionPool dbcpSpy = Mockito.spy(new DBConnectionPool(connectURI, username, password, driverClass)); + BasicDataSource mockDataSource = Mockito.mock(BasicDataSource.class); + Mockito.when(mockDataSource.getConnection()).thenThrow(new SQLException()); + Whitebox.setInternalState(dbcpSpy, "dataSource", mockDataSource); + expectedEx.expect(SQLException.class); + connection = dbcpSpy.getConnection(); + } + + @Test + public void testGetConnectionDBConnectionPoolExceptionFlow() throws SQLException { + DBConnectionPool dbcpSpy = Mockito.spy(new DBConnectionPool(connectURI, username, password, driverClass)); + BasicDataSource mockDataSource = Mockito.mock(BasicDataSource.class); + Mockito.when(mockDataSource.getConnection()).thenReturn(null); + Whitebox.setInternalState(dbcpSpy, "dataSource", mockDataSource); + expectedEx.expect(DBConnectionPoolException.class); + connection = dbcpSpy.getConnection(); + } + + @Test public void testGetDataSourceStatus() { Map<String, Integer> dataSourceStatus = dbcp.getDataSourceStatus(); Assert.assertNotNull(dataSourceStatus); @@ -75,6 +102,17 @@ public class DBConnectionPoolTest { Assert.assertNull(connection); } + @Test + public void testShutdownException() throws SQLException { + DBConnectionPool dbcpSpy = Mockito.spy(new DBConnectionPool(connectURI, username, password, driverClass, + 0, 0, 0, 0, 0)); + BasicDataSource mockDataSource = Mockito.mock(BasicDataSource.class); + Mockito.doThrow(new SQLException()).when(mockDataSource).close(); + Whitebox.setInternalState(dbcpSpy, "dataSource", mockDataSource); + dbcpSpy.shutdown(); + Mockito.verify(mockDataSource).close(); + } + @After public void clean() { if (dbcp != null) { |