com.shaft.db.DatabaseActions Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of SHAFT_ENGINE Show documentation
Show all versions of SHAFT_ENGINE Show documentation
SHAFT is a unified test automation engine. Powered by best-in-class frameworks, SHAFT provides a
wizard-like syntax to drive your automation efficiently, maximize your ROI, and minimize your learning curve.
Stop reinventing the wheel. Upgrade now!
package com.shaft.db;
import com.shaft.driver.SHAFT;
import com.shaft.tools.io.ReportManager;
import com.shaft.tools.io.internal.FailureReporter;
import com.shaft.tools.io.internal.ReportManagerHelper;
import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetProvider;
import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;
@SuppressWarnings("unused")
public class DatabaseActions {
private DatabaseType dbType;
private String dbServerIP;
private String dbPort;
private String dbName;
private String username;
private String password;
private final ThreadLocal resultSetThreadLocal = new ThreadLocal<>();
private final ThreadLocal rowCountThreadLocal = new ThreadLocal<>();
private String customConnectionString = "";
/**
* This constructor is used for initializing the database variables that are
* needed to create new connections and perform queries
*
* @param databaseType database type that you want to connect with:
* DatabaseType.MY_SQL ,SQL_SERVER,POSTGRES_SQL.
* @param ip IP address that has database installation that we need to
* connect to (e.g. 72.55.136.25)
* @param port port of database installation on the server (e.g. 3306)
* @param name database name that you need to connect to
* @param username database username
* @param password password of database user
*/
public DatabaseActions(DatabaseType databaseType, String ip, String port, String name, String username,
String password) {
if (!ip.equals("") && !port.equals("") && !name.equals("") && !username.equals("") && !password.equals("")) {
this.dbType = databaseType;
this.dbServerIP = ip;
this.dbPort = port;
this.dbName = name;
this.username = username;
this.password = password;
} else {
failAction("Database Type: \"" + databaseType + "\", IP: \"" + ip + "\", Port: \"" + port + "\", Name: \""
+ name + "\", Username: \"" + username + "\", Password: \"" + password + "\"");
}
}
public static DatabaseActions getInstance(DatabaseType databaseType, String ip, String port, String name, String username,
String password) {
return new DatabaseActions(databaseType, ip, port, name, username, password);
}
/**
* This constructor is used for initializing the database variables that are
* needed to create new connections and perform queries
*
* @param customConnectionString custom database connection string ex: "jdbc:oracle:thin:@dbServerIP:dbPort:dbName"
*/
public DatabaseActions(String customConnectionString) {
if (!"".equals(customConnectionString)) {
this.customConnectionString = customConnectionString;
} else {
failAction("Custom Connection String: \"" + customConnectionString + "\"");
}
}
public static DatabaseActions getInstance(String customConnectionString) {
return new DatabaseActions(customConnectionString);
}
/**
* Returns a string representation of the provided resultSet object
*
* @param resultSet the object returned as a result of performing a certain
* database query
* @return a string value which represents the provided resultSet object
*/
public static String getResult(ResultSet resultSet) {
var resultSetString = getResultStringValue(resultSet, false);
passAction();
return resultSetString;
}
private static void failAction(String actionName, String testData, Exception... rootCauseException) {
String message = reportActionResult(actionName, testData, null, false, rootCauseException);
FailureReporter.fail(DatabaseActions.class, message, rootCauseException[0]);
}
/**
* Returns a string value which represents the data of the target row
*
* @param resultSet the object returned as a result of performing a certain
* database query
* @param columnName the name of the column holding the knownCellValue
* @param knownCellValue a value that the engine searches for under the
* specified columnName, when that value is found, the row
* that contains it is read and added to the returned
* string
* @return a string value which represents the data of the target row
*/
public static String getRow(ResultSet resultSet, String columnName, String knownCellValue) {
String reportMessage = "Column Name: \"" + columnName + "\" | Cell Content: \"" + knownCellValue + "\"";
var str = new StringBuilder();
var foundRow = false;
try {
resultSet.beforeFirst();
if (resultSet.last()) {
int columnsCount = resultSet.getMetaData().getColumnCount();
int lastRowID = resultSet.getRow();
int targetColumnID = resultSet.findColumn(columnName);
// read table data
for (var i = 1; i <= lastRowID; i++) {
resultSet.absolute(i);
if (String.valueOf(resultSet.getString(targetColumnID)).trim().equals(knownCellValue.trim())) {
for (var j = 1; j <= columnsCount; j++) {
str.append(resultSet.getString(j)).append("\t");
}
str.append("\n");
foundRow = true;
}
}
}
} catch (SQLException | NullPointerException rootCauseException) {
failAction(reportMessage, rootCauseException);
}
if (Boolean.TRUE.equals(foundRow)) {
passAction(reportMessage);
} else {
failAction(reportMessage);
}
return str.toString().trim();
}
public String getResult() {
return DatabaseActions.getResult(resultSetThreadLocal.get());
}
/**
* Returns a string value which represents the data of the target column
*
* @param resultSet the object returned as a result of performing a certain
* database query
* @param columnName the name of the target column that will be read
* @return a string value which represents the data of the target column
*/
public static String getColumn(ResultSet resultSet, String columnName) {
var str = new StringBuilder();
try {
resultSet.beforeFirst();
if (resultSet.last()) {
int lastRowID = resultSet.getRow();
int targetColumnID = resultSet.findColumn(columnName);
// read table data
for (var i = 1; i <= lastRowID; i++) {
resultSet.absolute(i);
str.append(resultSet.getString(targetColumnID)).append("\n");
}
}
} catch (SQLException | NullPointerException rootCauseException) {
failAction(rootCauseException);
}
passAction(columnName);
return str.toString().trim();
}
public String getRow(String columnName, String knownCellValue) {
return DatabaseActions.getRow(resultSetThreadLocal.get(), columnName, knownCellValue);
}
/**
* Returns the number of rows contained inside the provided resultSet
*
* @param resultSet the object returned as a result of performing a certain
* database query
* @return an integer value which represents the number of rows contained inside
* the provided resultSet
*/
public static int getRowCount(ResultSet resultSet) {
var rowCount = 0;
try {
resultSet.beforeFirst();
if (resultSet.last()) {
rowCount = resultSet.getRow();
resultSet.beforeFirst(); // reset pointer
}
} catch (SQLException rootCauseException) {
failAction(rootCauseException);
}
passAction();
return rowCount;
}
public String getColumn(String columnName) {
return DatabaseActions.getColumn(resultSetThreadLocal.get(), columnName);
}
public int getRowCount() {
return rowCountThreadLocal.get();
}
private static void passAction(String actionName, String testData, String queryResult) {
reportActionResult(actionName, testData, queryResult, true);
}
private static void passAction(String testData, String queryResult) {
String actionName = Thread.currentThread().getStackTrace()[2].getMethodName();
passAction(actionName, testData, queryResult);
}
private static void passAction(String testData) {
String actionName = Thread.currentThread().getStackTrace()[2].getMethodName();
passAction(actionName, testData, null);
}
private static void passAction() {
String actionName = Thread.currentThread().getStackTrace()[2].getMethodName();
passAction(actionName, null, null);
}
private void setRowCountForSelectStatement(ResultSet resultSet) {
var rowCount = 0;
try {
resultSet.beforeFirst();
if (resultSet.last()) {
rowCount = resultSet.getRow();
resultSet.beforeFirst(); // reset pointer
}
} catch (SQLException rootCauseException) {
failAction(rootCauseException);
}
rowCountThreadLocal.set(rowCount);
}
private static void failAction(String testData, Exception... rootCauseException) {
String actionName = Thread.currentThread().getStackTrace()[2].getMethodName();
failAction(actionName, testData, rootCauseException);
}
private static void failAction(Exception... rootCauseException) {
String actionName = Thread.currentThread().getStackTrace()[2].getMethodName();
failAction(actionName, null, rootCauseException);
}
private static String reportActionResult(String actionName, String testData, String queryResult,
Boolean passFailStatus, Exception... rootCauseException) {
actionName = actionName.substring(0, 1).toUpperCase() + actionName.substring(1);
String message;
if (Boolean.TRUE.equals(passFailStatus)) {
message = "Database Action \"" + actionName + "\" successfully performed.";
} else {
message = "Database Action \"" + actionName + "\" failed.";
}
List> attachments = new ArrayList<>();
if (testData != null && testData.length() >= 500) {
List