Test coverage for DBConnectionPool 68/77968/2
authorJoss Armstrong <joss.armstrong@ericsson.com>
Wed, 6 Feb 2019 16:53:34 +0000 (16:53 +0000)
committerPatrick Brady <patrick.brady@att.com>
Wed, 6 Feb 2019 21:51:03 +0000 (21:51 +0000)
Increased class coverage to 100%

Issue-ID: APPC-1394
Change-Id: I7c8adb081b7342a5a18d3a2ca0238625317c6b4d
Signed-off-by: Joss Armstrong <joss.armstrong@ericsson.com>
appc-dispatcher/appc-dispatcher-common/appc-data-access-lib/src/main/java/org/onap/appc/dao/util/dbcp/DBConnectionPool.java
appc-dispatcher/appc-dispatcher-common/appc-data-access-lib/src/test/java/org/onap/appc/dao/util/dbcp/DBConnectionPoolTest.java

index 161cc30..d201f39 100644 (file)
@@ -5,6 +5,8 @@
  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Copyright (C) 2017 Amdocs
+ * ================================================================================
+ * Modifications Copyright (C) 2019 Ericsson
  * =============================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -77,7 +79,6 @@ public class DBConnectionPool implements DBConnectionPoolService {
         if (dataSource == null) {
             throw new DBConnectionPoolException();
         }
-
         Connection connection = null;
         try {
             connection = dataSource.getConnection();
@@ -123,7 +124,7 @@ public class DBConnectionPool implements DBConnectionPoolService {
         return map;
     }
 
-    private BasicDataSource getBasicDataSource(String connectURI, String username, String password,
+    protected BasicDataSource getBasicDataSource(String connectURI, String username, String password,
                                                String driverClass, Integer initialSize, Integer maxtotal,
                                                Integer maxIdle, Integer maxWaitMillis, Integer minIdle) {
         BasicDataSource dataSource = new BasicDataSource();
index b9ba1da..d2ac778 100644 (file)
 
 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);
@@ -62,6 +69,26 @@ public class DBConnectionPoolTest {
         Assert.assertNotNull(connection);
     }
 
+    @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();
@@ -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) {