com.agiletestingframework.toolbox.managers.DatabaseAutomationManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of atf-toolbox Show documentation
Show all versions of atf-toolbox Show documentation
Automation Testing Framework Toolbox Provides simple automation.
The newest version!
package com.agiletestingframework.toolbox.managers;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.dbcp.BasicDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.zatarox.squiggle.SelectQuery;
public class DatabaseAutomationManager {
private static Logger log = LoggerFactory.getLogger(DatabaseAutomationManager.class);
private Map databaseSources;
public static final String PRIMARY_DATASOURCE_KEY = "Primary";
/**
* DatabaseAutomationManager Creates primary DataSource and adds it to the
* DataSource Collection
*/
public DatabaseAutomationManager() {
log.info("Initializing the DatabaseAutomationManager.");
databaseSources = new HashMap();
databaseSources.put(PRIMARY_DATASOURCE_KEY, createPrimaryDataSource());
}
/**
* AddDatabaseService Adds a Database Service to the collection with the key
* provided If the key already exists, the Database Service will be replaced
* within the collection
*
* @param key
* the Database Service in the collection
* @param dataSource
* instance of DatabaseService
*/
public void addDatabaseService(String key, BasicDataSource dataSource) {
if (databaseSources.containsKey(key)) {
log.info("Replaced database service for key :" + key);
databaseSources.put(key, dataSource);
} else {
log.info("Added database service with key: " + key);
databaseSources.put(key, dataSource);
}
}
/**
* RemoveDatabaseService
*
* @param key
* key to locate the Database Service to remove from the
* collection
*/
public void removeDatabaseService(String key) {
if (databaseSources.containsKey(key)) {
try {
databaseSources.remove(key);
log.info("Successfully removed database service : " + key);
} catch (Exception ex) {
log.info("Unable to remove database service: " + key, ex);
}
} else {
log.info("Unable to remove database service. No database service found with key : " + key);
}
}
/**
* getDatabaseSource
*
* @param key
* used to locate the database source
* @return the BasicDataSource located for the key provided
*/
public BasicDataSource getDatabaseSource(String key) {
if (databaseSources.containsKey(key)) {
return databaseSources.get(key);
} else {
log.warn("Unable to locate Database Service for key: " + key + " returning null.");
return null;
}
}
/**
* getPrimaryDatabaseSource
*
* @return the Primary BasicDataSource
*/
public BasicDataSource getPrimaryDatabaseSource() {
return getDatabaseSource(PRIMARY_DATASOURCE_KEY);
}
/**
* SelectStatementColumnBuilder
*
* @param columnsToReturn
* columns to return from a select statement
* @return list of columns to return
*/
public String selectStatementColumnBuilder(String[] columnsToReturn) {
if (columnsToReturn != null) {
StringBuilder columns = new StringBuilder();
for (String columnName : columnsToReturn) {
if (columns.toString() != "")
columns.append(", ");
columns.append(columnName);
}
return columns.toString();
}
return "*";
}
/**
* createPrimaryDataSource
*
* @return BasicDataSource based on the configuration
*/
private BasicDataSource createPrimaryDataSource() {
return createBasicDataSource(ConfigurationManager.getInstance().getDatabaseDriver(), ConfigurationManager.getInstance().getDatabaseUser(),
ConfigurationManager.getInstance().getDatabasePassword(), ConfigurationManager.getInstance().getDatabaseUrl());
}
/**
* createBasicDataSource
*
* @param dbDriver
* database driver
* @param dbUser
* database user
* @param dbPwd
* database password
* @param dbURL
* database url
* @return BasicDataSource based on parameter inputs
*/
public BasicDataSource createBasicDataSource(String dbDriver, String dbUser, String dbPwd, String dbURL) {
BasicDataSource d = new BasicDataSource();
d.setDriverClassName(dbDriver);
d.setUsername(dbUser);
d.setPassword(dbPwd);
d.setUrl(dbURL);
try {
Class.forName(ConfigurationManager.getInstance().getDatabaseDriver());
} catch (ClassNotFoundException e) {
log.error("Unable to initialize database driver.");
}
return d;
}
/**
* selectData Will use the PRIMARY datasource
*
* @param selectStmnt
* squiggle sql select query
* (https://github.com/gchauvet/squiggle-sql)
* @return ResultSet of the statement execution
*/
public List