From a9e89cccc344b5f1aecbe3936f6bb917cfe7b20f Mon Sep 17 00:00:00 2001 From: Rich Tabedzki Date: Thu, 8 Mar 2018 14:50:22 +0000 Subject: Fix sonar related items Changes made: * Optimized TerminatingCachedDataSource * Added additional unit tests Change-Id: I0a83c993898e2bee11b8d3c5df8f95b4b216e9b8 Issue-ID: CCSDK-151 Signed-off-by: Rich Tabedzki --- .../ccsdk/sli/core/dblib/DBResourceManager.java | 3 +- .../core/dblib/TerminatingCachedDataSource.java | 46 ----------- .../core/dblib/jdbc/JdbcDBCachedDataSource.java | 1 - .../ccsdk/sli/core/dblib/CachedDataSourceTest.java | 94 ++++++++++++++++++++++ .../sli/core/dblib/DBConfigExceptionTest.java | 19 +++++ .../dblib/DblibConfigurationExceptionTest.java | 24 ++++++ .../dblib/TerminatingCachedDataSourceTest.java | 94 ++++++++++++++++++++++ 7 files changed, 233 insertions(+), 48 deletions(-) create mode 100644 dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/CachedDataSourceTest.java create mode 100644 dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/DBConfigExceptionTest.java create mode 100644 dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/DblibConfigurationExceptionTest.java create mode 100644 dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/TerminatingCachedDataSourceTest.java (limited to 'dblib/provider') diff --git a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/DBResourceManager.java b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/DBResourceManager.java index 27c14d2a4..9acae340b 100755 --- a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/DBResourceManager.java +++ b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/DBResourceManager.java @@ -248,7 +248,8 @@ public class DBResourceManager implements DataSource, DataAccessor, DBResourceOb } catch(Exception exc) { LOGGER.warn("", exc); } - LOGGER.info("Thread DataSourceTester terminated {} for {}", this.getName(), ds.getDbConnectionName()); + if(ds != null) + LOGGER.info("Thread DataSourceTester terminated {} for {}", this.getName(), ds.getDbConnectionName()); } } diff --git a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/TerminatingCachedDataSource.java b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/TerminatingCachedDataSource.java index 92b31470e..e1afcc22e 100755 --- a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/TerminatingCachedDataSource.java +++ b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/TerminatingCachedDataSource.java @@ -38,54 +38,8 @@ public class TerminatingCachedDataSource extends CachedDataSource implements SQL return null; } - @Override - public long getInterval() { - return 1000; - } - - @Override - public long getInitialDelay() { - return 1000; - } - - @Override - public long getExpectedCompletionTime() { - return 50; - } - - @Override - public void setExpectedCompletionTime(long value) { - - } - - @Override - public void setInterval(long value) { - - } - - @Override - public void setInitialDelay(long value) { - - } - - @Override - public long getUnprocessedFailoverThreshold() { - return 3; - } - - @Override - public void setUnprocessedFailoverThreshold(long value) { - - } - - public int compareTo(CachedDataSource ods) - { - return 0; - } - @Override public Logger getParentLogger() throws SQLFeatureNotSupportedException { - // TODO Auto-generated method stub return null; } diff --git a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/jdbc/JdbcDBCachedDataSource.java b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/jdbc/JdbcDBCachedDataSource.java index a6582b9e7..80ca577cf 100755 --- a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/jdbc/JdbcDBCachedDataSource.java +++ b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/jdbc/JdbcDBCachedDataSource.java @@ -180,7 +180,6 @@ public class JdbcDBCachedDataSource extends CachedDataSource { public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException { - // TODO Auto-generated method stub return null; } diff --git a/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/CachedDataSourceTest.java b/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/CachedDataSourceTest.java new file mode 100644 index 000000000..6385c1b57 --- /dev/null +++ b/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/CachedDataSourceTest.java @@ -0,0 +1,94 @@ +package org.onap.ccsdk.sli.core.dblib; + +import static org.junit.Assert.*; + +import java.sql.SQLFeatureNotSupportedException; +import java.util.Properties; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.ccsdk.sli.core.dblib.config.BaseDBConfiguration; +import org.onap.ccsdk.sli.core.dblib.config.JDBCConfiguration; +import org.onap.ccsdk.sli.core.dblib.jdbc.JdbcDBCachedDataSource; +import org.slf4j.LoggerFactory; + +public class CachedDataSourceTest { + + private static final Properties props = new Properties(); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + props.setProperty("org.onap.ccsdk.sli.dbtype", "jdbc"); + props.setProperty("org.onap.ccsdk.sli.jdbc.hosts", "localhost"); + props.setProperty("org.onap.ccsdk.sli.jdbc.url", "jdbc:mysql://dbhost:3306/test"); + props.setProperty("org.onap.ccsdk.sli.jdbc.driver", "org.mariadb.jdbc.Driver"); + props.setProperty("org.onap.ccsdk.sli.jdbc.database", "test"); + props.setProperty("org.onap.ccsdk.sli.jdbc.user", "dbuser"); + props.setProperty("org.onap.ccsdk.sli.jdbc.password", "passw0rd"); + props.setProperty("org.onap.ccsdk.sli.jdbc.connection.name", "testdb01"); + props.setProperty("org.onap.ccsdk.sli.jdbc.connection.timeout", "50"); + props.setProperty("org.onap.ccsdk.sli.jdbc.request.timeout", "100"); + props.setProperty("org.onap.ccsdk.sli.jdbc.limit.init", "10"); + props.setProperty("org.onap.ccsdk.sli.jdbc.limit.min", "10"); + props.setProperty("org.onap.ccsdk.sli.jdbc.limit.max", "20"); + props.setProperty("org.onap.dblib.connection.recovery", "false"); + } + + @Test + public void testCachedDataSource() { + BaseDBConfiguration config = new JDBCConfiguration(props); + CachedDataSource ds = new JdbcDBCachedDataSource(config); + assertNotNull(ds); + } + + @Test + public void testConfigure() { + BaseDBConfiguration config = new JDBCConfiguration(props); + + CachedDataSource ds = new JdbcDBCachedDataSource(config); + assertNotNull(ds.configure(config)); + } + + @Test + public void testSetInitialDelay() { + BaseDBConfiguration config = new JDBCConfiguration(props); + CachedDataSource ds = new JdbcDBCachedDataSource(config); + ds.setInitialDelay(1000L); + assertTrue(ds.getInitialDelay() == 1000L); + } + + @Test + public void testSetInterval() { + BaseDBConfiguration config = new JDBCConfiguration(props); + CachedDataSource ds = new JdbcDBCachedDataSource(config); + ds.setInterval(1000L); + assertTrue(ds.getInterval() == 1000L); + } + + @Test + public void testSetExpectedCompletionTime() { + BaseDBConfiguration config = new JDBCConfiguration(props); + CachedDataSource ds = new JdbcDBCachedDataSource(config); + ds.setExpectedCompletionTime(100L); + assertTrue(ds.getExpectedCompletionTime() == 100L); + } + + @Test + public void testSetUnprocessedFailoverThreshold() { + BaseDBConfiguration config = new JDBCConfiguration(props); + CachedDataSource ds = new JdbcDBCachedDataSource(config); + ds.setUnprocessedFailoverThreshold(100L); + assertTrue(ds.getUnprocessedFailoverThreshold() == 100L); + } + + @Test + public void testGetParentLogger() { + BaseDBConfiguration config = new JDBCConfiguration(props); + CachedDataSource ds = new JdbcDBCachedDataSource(config); + try { + assertNull(ds.getParentLogger()); + } catch (SQLFeatureNotSupportedException e) { + LoggerFactory.getLogger(CachedDataSourceTest.class).warn("Test Failure", e); + } + } +} \ No newline at end of file diff --git a/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/DBConfigExceptionTest.java b/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/DBConfigExceptionTest.java new file mode 100644 index 000000000..2a5b65c7a --- /dev/null +++ b/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/DBConfigExceptionTest.java @@ -0,0 +1,19 @@ +package org.onap.ccsdk.sli.core.dblib; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; + +public class DBConfigExceptionTest { + + @Test + public void testDBConfigExceptionException() { + assertNotNull(new DBConfigException("JUnit Test")); + } + + @Test + public void testDBConfigExceptionString() { + assertNotNull(new DBConfigException(new Exception("JUnit Test"))); + } + +} diff --git a/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/DblibConfigurationExceptionTest.java b/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/DblibConfigurationExceptionTest.java new file mode 100644 index 000000000..7becd1ae4 --- /dev/null +++ b/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/DblibConfigurationExceptionTest.java @@ -0,0 +1,24 @@ +package org.onap.ccsdk.sli.core.dblib; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class DblibConfigurationExceptionTest { + + @Test + public void testDblibConfigurationException() { + assertNotNull(new DblibConfigurationException()); + } + + @Test + public void testDblibConfigurationExceptionString() { + assertNotNull(new DblibConfigurationException("JUnit Test")); + } + + @Test + public void testDblibConfigurationExceptionStringThrowable() { + assertNotNull(new DblibConfigurationException("JUnit Test", new Exception("JUnit Test"))); + } + +} diff --git a/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/TerminatingCachedDataSourceTest.java b/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/TerminatingCachedDataSourceTest.java new file mode 100644 index 000000000..160a3d4ae --- /dev/null +++ b/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/TerminatingCachedDataSourceTest.java @@ -0,0 +1,94 @@ +package org.onap.ccsdk.sli.core.dblib; + +import static org.junit.Assert.*; + +import java.sql.SQLFeatureNotSupportedException; +import java.util.Properties; + +import org.junit.Test; +import org.onap.ccsdk.sli.core.dblib.config.BaseDBConfiguration; +import org.onap.ccsdk.sli.core.dblib.config.JDBCConfiguration; +import org.slf4j.LoggerFactory; + +public class TerminatingCachedDataSourceTest { + + @Test + public void testTerminatingCachedDataSource() { + Properties props = new Properties(); + BaseDBConfiguration config = new JDBCConfiguration(props); + CachedDataSource ds = new TerminatingCachedDataSource(config); + assertNotNull(ds); + } + + @Test + public void testConfigure() { + Properties props = new Properties(); + props.setProperty("org.onap.ccsdk.sli.dbtype", "jdbc"); + props.setProperty("org.onap.ccsdk.sli.jdbc.hosts", "localhost"); + props.setProperty("org.onap.ccsdk.sli.jdbc.url", "jdbc:mysql://dbhost:3306/test"); + props.setProperty("org.onap.ccsdk.sli.jdbc.driver", "org.mariadb.jdbc.Driver"); + props.setProperty("org.onap.ccsdk.sli.jdbc.database", "test"); + props.setProperty("org.onap.ccsdk.sli.jdbc.user", "dbuser"); + props.setProperty("org.onap.ccsdk.sli.jdbc.password", "passw0rd"); + props.setProperty("org.onap.ccsdk.sli.jdbc.connection.name", "testdb01"); + props.setProperty("org.onap.ccsdk.sli.jdbc.connection.timeout", "50"); + props.setProperty("org.onap.ccsdk.sli.jdbc.request.timeout", "100"); + props.setProperty("org.onap.ccsdk.sli.jdbc.limit.init", "10"); + props.setProperty("org.onap.ccsdk.sli.jdbc.limit.min", "10"); + props.setProperty("org.onap.ccsdk.sli.jdbc.limit.max", "20"); + props.setProperty("org.onap.dblib.connection.recovery", "false"); + BaseDBConfiguration config = new JDBCConfiguration(props); + + CachedDataSource ds = new TerminatingCachedDataSource(config); + assertNull(ds.configure(config)); + } + + @Test + public void testSetInitialDelay() { + Properties props = new Properties(); + BaseDBConfiguration config = new JDBCConfiguration(props); + CachedDataSource ds = new TerminatingCachedDataSource(config); + ds.setInitialDelay(1000L); + assertTrue(ds.getInitialDelay() == 1000L); + } + + @Test + public void testSetInterval() { + Properties props = new Properties(); + BaseDBConfiguration config = new JDBCConfiguration(props); + CachedDataSource ds = new TerminatingCachedDataSource(config); + ds.setInterval(1000L); + assertTrue(ds.getInterval() == 1000L); + } + + @Test + public void testSetExpectedCompletionTime() { + Properties props = new Properties(); + BaseDBConfiguration config = new JDBCConfiguration(props); + CachedDataSource ds = new TerminatingCachedDataSource(config); + ds.setExpectedCompletionTime(100L); + assertTrue(ds.getExpectedCompletionTime() == 100L); + } + + @Test + public void testSetUnprocessedFailoverThreshold() { + Properties props = new Properties(); + BaseDBConfiguration config = new JDBCConfiguration(props); + CachedDataSource ds = new TerminatingCachedDataSource(config); + ds.setUnprocessedFailoverThreshold(100L); + assertTrue(ds.getUnprocessedFailoverThreshold() == 100L); + } + + @Test + public void testGetParentLogger() { + Properties props = new Properties(); + BaseDBConfiguration config = new JDBCConfiguration(props); + CachedDataSource ds = new TerminatingCachedDataSource(config); + ds.setInterval(100L); + try { + assertNull(ds.getParentLogger()); + } catch (SQLFeatureNotSupportedException e) { + LoggerFactory.getLogger(TerminatingCachedDataSourceTest.class).warn("Test Failure", e); + } + } +} \ No newline at end of file -- cgit 1.2.3-korg