From 8fdee1fb6c2ce27f245a921536739ef2d4f678a2 Mon Sep 17 00:00:00 2001 From: Joss Armstrong Date: Wed, 6 Feb 2019 14:58:20 +0000 Subject: Increase coverage in AppcDatabaseConnectionPool Add test cases and increase coverage to 94% Issue-ID: APPC-1394 Change-Id: I7ec2b0cb4fe5a22ebd8020eb7f5636075e1c5ac5 Signed-off-by: Joss Armstrong --- .../appc/dao/util/AppcDatabaseConnectionPool.java | 9 +++- .../dao/util/AppcDatabaseConnectionPoolTest.java | 54 +++++++++++++--------- 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/appc-dispatcher/appc-dispatcher-common/appc-data-access-lib/src/main/java/org/onap/appc/dao/util/AppcDatabaseConnectionPool.java b/appc-dispatcher/appc-dispatcher-common/appc-data-access-lib/src/main/java/org/onap/appc/dao/util/AppcDatabaseConnectionPool.java index ec8898113..1f4a75911 100644 --- a/appc-dispatcher/appc-dispatcher-common/appc-data-access-lib/src/main/java/org/onap/appc/dao/util/AppcDatabaseConnectionPool.java +++ b/appc-dispatcher/appc-dispatcher-common/appc-data-access-lib/src/main/java/org/onap/appc/dao/util/AppcDatabaseConnectionPool.java @@ -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. @@ -80,8 +82,7 @@ public class AppcDatabaseConnectionPool implements DBConnectionPoolService { String password = getConnectionProperty(configuration, PropertyPattern.PASSWORD); String jdbcDriver = getJDBCDriver(configuration); - dbConnectionPool = new DBConnectionPool(dbUrl, userName, password, jdbcDriver); - + dbConnectionPool = getDBConnectionPool(dbUrl, userName, password, jdbcDriver); // a simple health check Connection connection = null; try { @@ -141,4 +142,8 @@ public class AppcDatabaseConnectionPool implements DBConnectionPoolService { private String getJDBCDriver(Configuration configuration) { return configuration.getProperty(PropertyPattern.DRIVER.getPattern(), ""); } + + protected DBConnectionPool getDBConnectionPool(String dbUrl, String userName, String password, String jdbcDriver) { + return new DBConnectionPool(dbUrl, userName, password, jdbcDriver); + } } diff --git a/appc-dispatcher/appc-dispatcher-common/appc-data-access-lib/src/test/java/org/onap/appc/dao/util/AppcDatabaseConnectionPoolTest.java b/appc-dispatcher/appc-dispatcher-common/appc-data-access-lib/src/test/java/org/onap/appc/dao/util/AppcDatabaseConnectionPoolTest.java index bc3b9f2d4..ad1b4ea0f 100644 --- a/appc-dispatcher/appc-dispatcher-common/appc-data-access-lib/src/test/java/org/onap/appc/dao/util/AppcDatabaseConnectionPoolTest.java +++ b/appc-dispatcher/appc-dispatcher-common/appc-data-access-lib/src/test/java/org/onap/appc/dao/util/AppcDatabaseConnectionPoolTest.java @@ -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. @@ -23,43 +25,31 @@ package org.onap.appc.dao.util; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.when; +import java.sql.Connection; +import java.sql.SQLException; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Mock; import org.mockito.Mockito; import org.onap.appc.configuration.Configuration; import org.onap.appc.configuration.ConfigurationFactory; import org.onap.appc.dao.util.dbcp.DBConnectionPool; -import org.onap.appc.dao.util.exception.DBConnectionPoolException; -import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; -import java.sql.Connection; -import java.sql.SQLException; -import java.util.Map; - -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyObject; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.times; -import static org.powermock.api.mockito.PowerMockito.doReturn; -import static org.powermock.api.mockito.PowerMockito.mock; -import static org.powermock.api.mockito.PowerMockito.mockStatic; -import static org.powermock.api.mockito.PowerMockito.when; -import static org.powermock.api.support.membermodification.MemberMatcher.method; - @RunWith(PowerMockRunner.class) @PrepareForTest({ConfigurationFactory.class}) @PowerMockIgnore("javax.management.*") public class AppcDatabaseConnectionPoolTest { - private String dbName = "dbName"; private String dbUrl = "jdbc:h2:mem:~/test;MODE=MYSQL;DB_CLOSE_DELAY=-1"; private String username = "sa"; private String password = "sa"; @@ -124,7 +114,7 @@ public class AppcDatabaseConnectionPoolTest { @Test public void testGetConnection() throws SQLException { - final Connection connection = appcDatabaseConnectionPool.getConnection(); + appcDatabaseConnectionPool.getConnection(); Mockito.verify(dbConnectionPool, times(1)).getConnection(); } @@ -136,7 +126,27 @@ public class AppcDatabaseConnectionPoolTest { @Test public void testGetDataSourceStatus() { - Map dataSourceStatus = appcDatabaseConnectionPool.getDataSourceStatus(); + appcDatabaseConnectionPool.getDataSourceStatus(); Mockito.verify(dbConnectionPool, times(1)).getDataSourceStatus(); } + + @Test + public void testInit() throws SQLException { + Configuration mockConfiguration = Mockito.mock(Configuration.class); + when(ConfigurationFactory.getConfiguration()).thenReturn(mockConfiguration); + when(mockConfiguration.getProperty(Mockito.anyString(), Mockito.anyString())).thenReturn(""); + DBConnectionPool mockDbConnectionPool = Mockito.mock(DBConnectionPool.class); + Connection mockConnection = Mockito.mock(Connection.class); + when(mockDbConnectionPool.getConnection()).thenReturn(mockConnection); + when(appcDatabaseConnectionPool.getDBConnectionPool("", "", "", "")).thenReturn(mockDbConnectionPool); + appcDatabaseConnectionPool.init(); + Mockito.verify(mockConnection).close(); + } + + @Test + public void testSetDbName() { + AppcDatabaseConnectionPool pool = new AppcDatabaseConnectionPool(); + pool.setDbName("TEST"); + assertEquals("TEST", Whitebox.getInternalState(pool, "dbName")); + } } -- cgit 1.2.3-korg