From 1885b54390fc5bd9363db1a39719927ded65307e Mon Sep 17 00:00:00 2001 From: Sivasai Podugu Date: Wed, 7 Feb 2024 11:31:50 +0530 Subject: [PATCH] Feat[TE-15497]: Added SQL Server Queries Addon --- sql_server_queries/pom.xml | 110 ++++++++++++ .../android/SqlValidateQueriesonTables.java | 160 ++++++++++++++++++ .../SqlValidateQueriesonTablesWithUrl.java | 155 +++++++++++++++++ .../testsigma/addons/android/Sqlqueries.java | 68 ++++++++ .../addons/android/SqlqueriesValidate.java | 84 +++++++++ .../android/SqlqueriesValidateWithUrl.java | 79 +++++++++ .../addons/android/SqlqueriesWithUrl.java | 61 +++++++ .../testsigma/addons/android/Sqlselect.java | 88 ++++++++++ .../addons/android/SqlselectWithUrl.java | 83 +++++++++ .../addons/android/Sqlselectvalidate.java | 76 +++++++++ .../android/SqlselectvalidateWithUrl.java | 71 ++++++++ .../ios/SqlValidateQueriesonTables.java | 160 ++++++++++++++++++ .../SqlValidateQueriesonTablesWithUrl.java | 155 +++++++++++++++++ .../com/testsigma/addons/ios/Sqlqueries.java | 67 ++++++++ .../addons/ios/SqlqueriesValidate.java | 83 +++++++++ .../addons/ios/SqlqueriesValidateWithUrl.java | 78 +++++++++ .../addons/ios/SqlqueriesWithUrl.java | 62 +++++++ .../com/testsigma/addons/ios/Sqlselect.java | 88 ++++++++++ .../addons/ios/SqlselectWithUrl.java | 83 +++++++++ .../addons/ios/Sqlselectvalidate.java | 76 +++++++++ .../addons/ios/SqlselectvalidateWithUrl.java | 71 ++++++++ .../mobileweb/SqlValidateQueriesonTables.java | 160 ++++++++++++++++++ .../SqlValidateQueriesonTablesWithUrl.java | 155 +++++++++++++++++ .../addons/mobileweb/Sqlqueries.java | 67 ++++++++ .../addons/mobileweb/SqlqueriesValidate.java | 83 +++++++++ .../mobileweb/SqlqueriesValidateWithUrl.java | 78 +++++++++ .../addons/mobileweb/SqlqueriesWithUrl.java | 62 +++++++ .../testsigma/addons/mobileweb/Sqlselect.java | 88 ++++++++++ .../addons/mobileweb/SqlselectWithUrl.java | 83 +++++++++ .../addons/mobileweb/Sqlselectvalidate.java | 76 +++++++++ .../mobileweb/SqlselectvalidateWithUrl.java | 71 ++++++++ .../testsigma/addons/util/DatabaseUtil.java | 15 ++ .../web/SqlValidateQueriesonTables.java | 160 ++++++++++++++++++ .../SqlValidateQueriesonTablesWithUrl.java | 155 +++++++++++++++++ .../com/testsigma/addons/web/Sqlqueries.java | 77 +++++++++ .../addons/web/SqlqueriesValidate.java | 83 +++++++++ .../addons/web/SqlqueriesValidateWithUrl.java | 78 +++++++++ .../addons/web/SqlqueriesWithUrl.java | 72 ++++++++ .../com/testsigma/addons/web/Sqlselect.java | 88 ++++++++++ .../addons/web/SqlselectWithUrl.java | 83 +++++++++ .../addons/web/Sqlselectvalidate.java | 76 +++++++++ .../addons/web/SqlselectvalidateWithUrl.java | 71 ++++++++ .../addons/web/Sqlupdatequeries.java | 59 +++++++ .../addons/web/SqlupdatequeriesWithUrl.java | 54 ++++++ .../main/resources/testsigma-sdk.properties | 1 + 45 files changed, 3953 insertions(+) create mode 100644 sql_server_queries/pom.xml create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/android/SqlValidateQueriesonTables.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/android/SqlValidateQueriesonTablesWithUrl.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/android/Sqlqueries.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/android/SqlqueriesValidate.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/android/SqlqueriesValidateWithUrl.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/android/SqlqueriesWithUrl.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/android/Sqlselect.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/android/SqlselectWithUrl.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/android/Sqlselectvalidate.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/android/SqlselectvalidateWithUrl.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlValidateQueriesonTables.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlValidateQueriesonTablesWithUrl.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/ios/Sqlqueries.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlqueriesValidate.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlqueriesValidateWithUrl.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlqueriesWithUrl.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/ios/Sqlselect.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlselectWithUrl.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/ios/Sqlselectvalidate.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlselectvalidateWithUrl.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlValidateQueriesonTables.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlValidateQueriesonTablesWithUrl.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/Sqlqueries.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlqueriesValidate.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlqueriesValidateWithUrl.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlqueriesWithUrl.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/Sqlselect.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlselectWithUrl.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/Sqlselectvalidate.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlselectvalidateWithUrl.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/util/DatabaseUtil.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/web/SqlValidateQueriesonTables.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/web/SqlValidateQueriesonTablesWithUrl.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/web/Sqlqueries.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/web/SqlqueriesValidate.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/web/SqlqueriesValidateWithUrl.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/web/SqlqueriesWithUrl.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/web/Sqlselect.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/web/SqlselectWithUrl.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/web/Sqlselectvalidate.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/web/SqlselectvalidateWithUrl.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/web/Sqlupdatequeries.java create mode 100644 sql_server_queries/src/main/java/com/testsigma/addons/web/SqlupdatequeriesWithUrl.java create mode 100644 sql_server_queries/src/main/resources/testsigma-sdk.properties diff --git a/sql_server_queries/pom.xml b/sql_server_queries/pom.xml new file mode 100644 index 0000000..3eadb05 --- /dev/null +++ b/sql_server_queries/pom.xml @@ -0,0 +1,110 @@ + + + 4.0.0 + com.testsigma.addons + sql_server_queries + 1.1.0 + jar + + + UTF-8 + 11 + 11 + 1.2.5_cloud + 5.8.0-M1 + 1.0.0 + 3.2.1 + 1.18.20 + + + + + + com.testsigma + testsigma-java-sdk + ${testsigma.sdk.version} + + + org.projectlombok + lombok + ${lombok.version} + true + + + org.junit.jupiter + junit-jupiter-api + ${junit.jupiter.version} + test + + + org.testng + testng + 6.14.3 + + + + org.seleniumhq.selenium + selenium-java + 4.14.1 + + + + io.appium + java-client + 9.0.0 + + + com.fasterxml.jackson.core + jackson-annotations + 2.13.0 + + + + + org.apache.commons + commons-lang3 + 3.14.0 + + + + org.apache.ibatis + ibatis-core + 3.0 + + + + + sql_server_queries + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + + + org.apache.maven.plugins + maven-source-plugin + ${maven.source.plugin.version} + + + attach-sources + + jar + + + + + + + \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/android/SqlValidateQueriesonTables.java b/sql_server_queries/src/main/java/com/testsigma/addons/android/SqlValidateQueriesonTables.java new file mode 100644 index 0000000..2f39f62 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/android/SqlValidateQueriesonTables.java @@ -0,0 +1,160 @@ +package com.testsigma.addons.android; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import javax.sql.rowset.CachedRowSet; +import javax.sql.rowset.RowSetProvider; +import java.sql.*; +import java.util.Objects; + +@Data +@Action(actionText = "Validate SQL Query Query1 and compare with the Query Query2 from the Connection DB_Connection_URL for Database Database-Name using Username user-name and Password password", +description = "This action validate queries on the database connections", +applicationType = ApplicationType.ANDROID) +public class SqlValidateQueriesonTables extends WebAction { + + @TestData(reference = "Query1") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "Query2") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData testData3; + @TestData(reference = "user-name") + private com.testsigma.sdk.TestData userName; + @TestData(reference = "password") + private com.testsigma.sdk.TestData password; + @TestData(reference = "Database-Name") + private com.testsigma.sdk.TestData databaseName; + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = String.format("jdbc:sqlserver://%s;databaseName=%s;user=%s;password=%s;encrypt=false;",testData3.getValue().toString(), databaseName.getValue().toString(), userName.getValue().toString(), password.getValue().toString()); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + + Connection connection1 = databaseUtil.getConnection(url); + Connection connection2 = databaseUtil.getConnection(url); + + Statement stmt1 = connection1.createStatement(); + Statement stmt2 = connection2.createStatement(); + + String query1 = testData1.getValue().toString(); + String query2 = testData2.getValue().toString(); + + ResultSet resultSet1 = stmt1.executeQuery(query1); + ResultSet resultSet2 = stmt2.executeQuery(query2); + + boolean metadataComparisonSuccess = compareMetadata(resultSet1,resultSet2); + if(!metadataComparisonSuccess) { + return Result.FAILED; + } + boolean dataComparisonSuccess = compareQueryData(resultSet1, resultSet2); + if(!dataComparisonSuccess) { + return Result.FAILED; + } + logger.info(sb.toString()); + }catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + setSuccessMessage("The two queries are have similar data"); + logger.info("The two queries are have similar data"); + logger.info(sb.toString()); + return result; + } + + private boolean compareMetadata(ResultSet resultSet1, ResultSet resultSet2) throws SQLException { + + ResultSetMetaData rsmd1 = resultSet1.getMetaData(); + ResultSetMetaData rsmd2 = resultSet2.getMetaData(); + + int columnNo1 = resultSet1.getMetaData().getColumnCount(); + int columnNo2 = resultSet2.getMetaData().getColumnCount(); + + if(columnNo1 == columnNo2) + { + if(columnNo1 != 1) + { + for(int i=1;i<=columnNo1;i++) { + if (!rsmd1.getColumnName(i).equals(rsmd2.getColumnName(i))) { + sb.append("The ColumnNames does not match:
"); + sb.append("Column name from Query-1:" +"'" +rsmd1.getColumnName(i) + "'" + "and Column name from Query-2:" + "'" +rsmd2.getColumnName(i) +"'"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + return false; + } + } + } + return true; + } + sb.append("The Columns count does not match:
"); + sb.append("Columns from Query-1:" + "'").append(columnNo1).append("'").append("and Columns from Query-2:").append("'").append(columnNo2).append("'"); + setErrorMessage(sb.toString()); + return false; + } + private boolean compareQueryData(ResultSet resultSet1, ResultSet resultSet2) throws SQLException { + int columnNo1 = resultSet1.getMetaData().getColumnCount(); + + CachedRowSet cachedResultSet1 = RowSetProvider.newFactory().createCachedRowSet(); + CachedRowSet cachedResultSet2 = RowSetProvider.newFactory().createCachedRowSet(); + + cachedResultSet1.populate(resultSet1); + cachedResultSet2.populate(resultSet2); + + while (cachedResultSet1.next() && cachedResultSet2.next()) { + for (int i = 1; i <= columnNo1; i++) { + if (!Objects.equals(cachedResultSet1.getObject(i), cachedResultSet2.getObject(i))) { + sb.append("The values do not match for column: Column Position(Starting from 1) ").append(i).append(" Row Number: ").append(cachedResultSet1.getRow()); + sb.append("
"); + sb.append("Value from Query-1: '").append(cachedResultSet1.getObject(i)).append("' and Value from Query-2: '").append(cachedResultSet2.getObject(i)).append("'"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + return false; + } + } + } + + // Log the contents of both queries when values match + StringBuilder successLog = new StringBuilder("Values match for all columns. Contents of both queries:\n"); + + cachedResultSet1.beforeFirst(); + cachedResultSet2.beforeFirst(); + + // Log the contents of cachedResultSet1 + successLog.append("Query-1:\n"); + while (cachedResultSet1.next()) { + successLog.append(cachedResultSet1.getRow()).append(": "); + for (int i = 1; i <= columnNo1; i++) { + successLog.append(cachedResultSet1.getObject(i)).append("\n"); + } + successLog.append("\n"); + } + + // Log the contents of cachedResultSet2 + successLog.append("\nQuery-2:\n"); + while (cachedResultSet2.next()) { + successLog.append(cachedResultSet2.getRow()).append(": "); + for (int i = 1; i <= columnNo1; i++) { + successLog.append(cachedResultSet2.getObject(i)).append("\n"); + } + successLog.append("\n"); + } + + logger.info(successLog.toString()); + + return true; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/android/SqlValidateQueriesonTablesWithUrl.java b/sql_server_queries/src/main/java/com/testsigma/addons/android/SqlValidateQueriesonTablesWithUrl.java new file mode 100644 index 0000000..9fcff15 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/android/SqlValidateQueriesonTablesWithUrl.java @@ -0,0 +1,155 @@ +package com.testsigma.addons.android; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import javax.sql.rowset.CachedRowSet; +import javax.sql.rowset.RowSetProvider; +import java.sql.*; +import java.util.Objects; + +@Data +@Action(actionText = "Validate SQL Query Query1 and compare with the Query Query2 from the Connection DB_Connection_URL", +description = "This action validate queries on the database connections", +applicationType = ApplicationType.ANDROID) +public class SqlValidateQueriesonTablesWithUrl extends WebAction { + + @TestData(reference = "Query1") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "Query2") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData databaseUrl; + + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = databaseUrl.getValue().toString(); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + + Connection connection1 = databaseUtil.getConnection(url); + Connection connection2 = databaseUtil.getConnection(url); + + Statement stmt1 = connection1.createStatement(); + Statement stmt2 = connection2.createStatement(); + + String query1 = testData1.getValue().toString(); + String query2 = testData2.getValue().toString(); + + ResultSet resultSet1 = stmt1.executeQuery(query1); + ResultSet resultSet2 = stmt2.executeQuery(query2); + + boolean metadataComparisonSuccess = compareMetadata(resultSet1,resultSet2); + if(!metadataComparisonSuccess) { + return Result.FAILED; + } + boolean dataComparisonSuccess = compareQueryData(resultSet1, resultSet2); + if(!dataComparisonSuccess) { + return Result.FAILED; + } + logger.info(sb.toString()); + }catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + setSuccessMessage("The two queries are have similar data"); + logger.info("The two queries are have similar data"); + logger.info(sb.toString()); + return result; + } + + private boolean compareMetadata(ResultSet resultSet1, ResultSet resultSet2) throws SQLException { + + ResultSetMetaData rsmd1 = resultSet1.getMetaData(); + ResultSetMetaData rsmd2 = resultSet2.getMetaData(); + + int columnNo1 = resultSet1.getMetaData().getColumnCount(); + int columnNo2 = resultSet2.getMetaData().getColumnCount(); + + if(columnNo1 == columnNo2) + { + if(columnNo1 != 1) + { + for(int i=1;i<=columnNo1;i++) { + if (!rsmd1.getColumnName(i).equals(rsmd2.getColumnName(i))) { + sb.append("The ColumnNames does not match:
"); + sb.append("Column name from Query-1:" +"'" +rsmd1.getColumnName(i) + "'" + "and Column name from Query-2:" + "'" +rsmd2.getColumnName(i) +"'"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + return false; + } + } + } + return true; + } + sb.append("The Columns count does not match:
"); + sb.append("Columns from Query-1:" + "'").append(columnNo1).append("'").append("and Columns from Query-2:").append("'").append(columnNo2).append("'"); + setErrorMessage(sb.toString()); + return false; + } + private boolean compareQueryData(ResultSet resultSet1, ResultSet resultSet2) throws SQLException { + int columnNo1 = resultSet1.getMetaData().getColumnCount(); + + CachedRowSet cachedResultSet1 = RowSetProvider.newFactory().createCachedRowSet(); + CachedRowSet cachedResultSet2 = RowSetProvider.newFactory().createCachedRowSet(); + + cachedResultSet1.populate(resultSet1); + cachedResultSet2.populate(resultSet2); + + while (cachedResultSet1.next() && cachedResultSet2.next()) { + for (int i = 1; i <= columnNo1; i++) { + if (!Objects.equals(cachedResultSet1.getObject(i), cachedResultSet2.getObject(i))) { + sb.append("The values do not match for column: Column Position(Starting from 1) ").append(i).append(" Row Number: ").append(cachedResultSet1.getRow()); + sb.append("
"); + sb.append("Value from Query-1: '").append(cachedResultSet1.getObject(i)).append("' and Value from Query-2: '").append(cachedResultSet2.getObject(i)).append("'"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + return false; + } + } + } + + // Log the contents of both queries when values match + StringBuilder successLog = new StringBuilder("Values match for all columns. Contents of both queries:\n"); + + cachedResultSet1.beforeFirst(); + cachedResultSet2.beforeFirst(); + + // Log the contents of cachedResultSet1 + successLog.append("Query-1:\n"); + while (cachedResultSet1.next()) { + successLog.append(cachedResultSet1.getRow()).append(": "); + for (int i = 1; i <= columnNo1; i++) { + successLog.append(cachedResultSet1.getObject(i)).append("\n"); + } + successLog.append("\n"); + } + + // Log the contents of cachedResultSet2 + successLog.append("\nQuery-2:\n"); + while (cachedResultSet2.next()) { + successLog.append(cachedResultSet2.getRow()).append(": "); + for (int i = 1; i <= columnNo1; i++) { + successLog.append(cachedResultSet2.getObject(i)).append("\n"); + } + successLog.append("\n"); + } + + logger.info(successLog.toString()); + + return true; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/android/Sqlqueries.java b/sql_server_queries/src/main/java/com/testsigma/addons/android/Sqlqueries.java new file mode 100644 index 0000000..dd9eb54 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/android/Sqlqueries.java @@ -0,0 +1,68 @@ +package com.testsigma.addons.android; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Query on the Connection DB_Connection_URL for Database Database-Name using Username user-name and Password password", +description = "This action executes given query against the connection provided and prints the no. of affected/fetched rows.", +applicationType = ApplicationType.ANDROID) +public class Sqlqueries extends WebAction { + + @TestData(reference = "Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "user-name") + private com.testsigma.sdk.TestData userName; + @TestData(reference = "password") + private com.testsigma.sdk.TestData password; + @TestData(reference = "Database-Name") + private com.testsigma.sdk.TestData databaseName; + + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = String.format("jdbc:sqlserver://%s;databaseName=%s;user=%s;password=%s;encrypt=false;",testData2.getValue().toString(), databaseName.getValue().toString(), userName.getValue().toString(), password.getValue().toString()); + DatabaseUtil databaseUtil = new DatabaseUtil(); + int rowsUpdatedOrFetched = 0; + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + if(query.trim().toUpperCase().startsWith("SELECT")) { + ResultSet resultSet = stmt.executeQuery(query); + while (resultSet.next()){ + resultSet.getObject(1).toString(); + rowsUpdatedOrFetched ++; + } + setSuccessMessage("Successfully Executed Database Query, No. of Rows fetched from DB : " +rowsUpdatedOrFetched); + logger.info("Successfully Executed Database Query, No. of Rows fetched from DB : " +rowsUpdatedOrFetched); + }else { + rowsUpdatedOrFetched = stmt.executeUpdate(query); + setSuccessMessage("Successfully Executed Database Query, No. of rows affected in DB : " +rowsUpdatedOrFetched); + logger.info("Successfully Executed Database Query, No. of Rows fetched from DB : " +rowsUpdatedOrFetched); + } + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/android/SqlqueriesValidate.java b/sql_server_queries/src/main/java/com/testsigma/addons/android/SqlqueriesValidate.java new file mode 100644 index 0000000..5e2d839 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/android/SqlqueriesValidate.java @@ -0,0 +1,84 @@ +package com.testsigma.addons.android; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Query on the Connection DB_Connection_URL for Database Database-Name using Username user-name and Password password and verify affected rows count is Row-Count", +description = "This Action executes given SQL query and validates the affected rows.", +applicationType = ApplicationType.ANDROID) +public class SqlqueriesValidate extends WebAction { + + @TestData(reference = "Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "Row-Count") + private com.testsigma.sdk.TestData testData3; + StringBuffer sb = new StringBuffer(); + @TestData(reference = "user-name") + private com.testsigma.sdk.TestData userName; + @TestData(reference = "password") + private com.testsigma.sdk.TestData password; + @TestData(reference = "Database-Name") + private com.testsigma.sdk.TestData databaseName; + + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = String.format("jdbc:sqlserver://%s;databaseName=%s;user=%s;password=%s;encrypt=false;",testData2.getValue().toString(), databaseName.getValue().toString(), userName.getValue().toString(), password.getValue().toString()); + DatabaseUtil databaseUtil = new DatabaseUtil(); + int rowsUpdatedOrFetched = 0; + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + if(query.trim().toUpperCase().startsWith("SELECT")) { + ResultSet resultSet = stmt.executeQuery(query); + while (resultSet.next()){ + resultSet.getObject(1).toString(); + rowsUpdatedOrFetched ++; + } + sb.append("Successfully Executed Database Query and Rows fetched from DB : " +rowsUpdatedOrFetched + "
"); + }else { + rowsUpdatedOrFetched = stmt.executeUpdate(query); + sb.append("Successfully Executed Database Query, No. of rows affected in DB : " +rowsUpdatedOrFetched + "
"); + + } + if(rowsUpdatedOrFetched == Integer.parseInt(testData3.getValue().toString())) { + sb.append("Affected row count is matching with expected value." + "
"); + setSuccessMessage(sb.toString()); + logger.info(sb.toString()); + } + else { + result = Result.FAILED; + sb.append("The affected rows does not match with expected rows:" + "
"); + sb.append("Expected no. of affected rows:"+testData3.getValue().toString() + "
"); + sb.append("Actual affected rows from query execution:"+rowsUpdatedOrFetched + "
"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + sb.append("
"+errorMessage); + result = Result.FAILED; + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/android/SqlqueriesValidateWithUrl.java b/sql_server_queries/src/main/java/com/testsigma/addons/android/SqlqueriesValidateWithUrl.java new file mode 100644 index 0000000..23c0782 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/android/SqlqueriesValidateWithUrl.java @@ -0,0 +1,79 @@ +package com.testsigma.addons.android; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Query on the Connection DB_Connection_URL and verify affected rows count is Row-Count", +description = "This Action executes given SQL query and validates the affected rows.", +applicationType = ApplicationType.ANDROID) +public class SqlqueriesValidateWithUrl extends WebAction { + + @TestData(reference = "Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData databaseUrl; + @TestData(reference = "Row-Count") + private com.testsigma.sdk.TestData testData3; + StringBuffer sb = new StringBuffer(); + + + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = databaseUrl.getValue().toString(); + DatabaseUtil databaseUtil = new DatabaseUtil(); + int rowsUpdatedOrFetched = 0; + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + if(query.trim().toUpperCase().startsWith("SELECT")) { + ResultSet resultSet = stmt.executeQuery(query); + while (resultSet.next()){ + resultSet.getObject(1).toString(); + rowsUpdatedOrFetched ++; + } + sb.append("Successfully Executed Database Query and Rows fetched from DB : " +rowsUpdatedOrFetched + "
"); + }else { + rowsUpdatedOrFetched = stmt.executeUpdate(query); + sb.append("Successfully Executed Database Query, No. of rows affected in DB : " +rowsUpdatedOrFetched + "
"); + + } + if(rowsUpdatedOrFetched == Integer.parseInt(testData3.getValue().toString())) { + sb.append("Affected row count is matching with expected value." + "
"); + setSuccessMessage(sb.toString()); + logger.info(sb.toString()); + } + else { + result = Result.FAILED; + sb.append("The affected rows does not match with expected rows:" + "
"); + sb.append("Expected no. of affected rows:"+testData3.getValue().toString() + "
"); + sb.append("Actual affected rows from query execution:"+rowsUpdatedOrFetched + "
"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + sb.append("
"+errorMessage); + result = Result.FAILED; + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/android/SqlqueriesWithUrl.java b/sql_server_queries/src/main/java/com/testsigma/addons/android/SqlqueriesWithUrl.java new file mode 100644 index 0000000..98970c9 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/android/SqlqueriesWithUrl.java @@ -0,0 +1,61 @@ +package com.testsigma.addons.android; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Query on the Connection DB_Connection_URL", + description = "This action executes given query against the connection provided and prints the no. of affected/fetched rows.", + applicationType = ApplicationType.ANDROID) +public class SqlqueriesWithUrl extends WebAction { + + @TestData(reference = "Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData databaseUrl; + + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = databaseUrl.getValue().toString(); + DatabaseUtil databaseUtil = new DatabaseUtil(); + int rowsUpdatedOrFetched = 0; + try { + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + if (query.trim().toUpperCase().startsWith("SELECT")) { + ResultSet resultSet = stmt.executeQuery(query); + while (resultSet.next()) { + resultSet.getObject(1).toString(); + rowsUpdatedOrFetched++; + } + setSuccessMessage("Successfully Executed Database Query, No. of Rows fetched from DB : " + rowsUpdatedOrFetched); + logger.info("Successfully Executed Database Query, No. of Rows fetched from DB : " + rowsUpdatedOrFetched); + } else { + rowsUpdatedOrFetched = stmt.executeUpdate(query); + setSuccessMessage("Successfully Executed Database Query, No. of rows affected in DB : " + rowsUpdatedOrFetched); + logger.info("Successfully Executed Database Query, No. of Rows fetched from DB : " + rowsUpdatedOrFetched); + } + } catch (Exception e) { + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/android/Sqlselect.java b/sql_server_queries/src/main/java/com/testsigma/addons/android/Sqlselect.java new file mode 100644 index 0000000..0e59283 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/android/Sqlselect.java @@ -0,0 +1,88 @@ +package com.testsigma.addons.android; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.RunTimeData; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Select-Query on the connection DB_Connection_URL for Database Database-Name using Username user-name and Password password and store output into a variable-name", +description = "This Action executes a given Select Query and stores the result(First cell data) into a provided runtime variable.", +applicationType = ApplicationType.ANDROID) +public class Sqlselect extends WebAction { + + @TestData(reference = "Select-Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "variable-name", isRuntimeVariable = true) + private com.testsigma.sdk.TestData testData3; + @TestData(reference = "user-name") + private com.testsigma.sdk.TestData userName; + @TestData(reference = "password") + private com.testsigma.sdk.TestData password; + @TestData(reference = "Database-Name") + private com.testsigma.sdk.TestData databaseName; + @RunTimeData + private com.testsigma.sdk.RunTimeData runTimeData; + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = String.format("jdbc:sqlserver://%s;databaseName=%s;user=%s;password=%s;encrypt=false;",testData2.getValue().toString(), databaseName.getValue().toString(), userName.getValue().toString(), password.getValue().toString()); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + ResultSet resultSet = stmt.executeQuery(query); + StringBuilder resultStringBuilder = new StringBuilder(); + + while (resultSet.next()) { + ResultSetMetaData metaData = resultSet.getMetaData(); + int columnCount = metaData.getColumnCount(); + + if (columnCount > 0) { + // Assuming you want the first column's value + String resultData = resultSet.getObject(1).toString(); + + // Append the resultData to the StringBuilder + resultStringBuilder.append(resultData).append(", "); + + setSuccessMessage("Successfully Executed Select Query and Resultset is : " + resultData); + logger.info("Successfully Executed Select Query and Resultset is : " + resultData); + } + } + + if (resultStringBuilder.length() > 0) { + resultStringBuilder.setLength(resultStringBuilder.length() - 2); + String concatenatedResult = resultStringBuilder.toString(); + runTimeData = new com.testsigma.sdk.RunTimeData(); + runTimeData.setValue(concatenatedResult); + runTimeData.setKey(testData3.getValue().toString()); + + logger.info("Successfully retrieved result of the given query: " + concatenatedResult); + } + setSuccessMessage("Successfully Executed Select Query and stored in runtime variable: "+ testData3.getValue().toString()); + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/android/SqlselectWithUrl.java b/sql_server_queries/src/main/java/com/testsigma/addons/android/SqlselectWithUrl.java new file mode 100644 index 0000000..614e8bb --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/android/SqlselectWithUrl.java @@ -0,0 +1,83 @@ +package com.testsigma.addons.android; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.RunTimeData; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Select-Query on the connection DB_Connection_URL and store output into a variable-name", +description = "This Action executes a given Select Query and stores the result(First cell data) into a provided runtime variable.", +applicationType = ApplicationType.ANDROID) +public class SqlselectWithUrl extends WebAction { + + @TestData(reference = "Select-Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData databaseUrl; + @TestData(reference = "variable-name", isRuntimeVariable = true) + private com.testsigma.sdk.TestData testData3; + + @RunTimeData + private com.testsigma.sdk.RunTimeData runTimeData; + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = databaseUrl.getValue().toString(); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + ResultSet resultSet = stmt.executeQuery(query); + StringBuilder resultStringBuilder = new StringBuilder(); + + while (resultSet.next()) { + ResultSetMetaData metaData = resultSet.getMetaData(); + int columnCount = metaData.getColumnCount(); + + if (columnCount > 0) { + // Assuming you want the first column's value + String resultData = resultSet.getObject(1).toString(); + + // Append the resultData to the StringBuilder + resultStringBuilder.append(resultData).append(", "); + + setSuccessMessage("Successfully Executed Select Query and Resultset is : " + resultData); + logger.info("Successfully Executed Select Query and Resultset is : " + resultData); + } + } + + if (resultStringBuilder.length() > 0) { + resultStringBuilder.setLength(resultStringBuilder.length() - 2); + String concatenatedResult = resultStringBuilder.toString(); + runTimeData = new com.testsigma.sdk.RunTimeData(); + runTimeData.setValue(concatenatedResult); + runTimeData.setKey(testData3.getValue().toString()); + + logger.info("Successfully retrieved result of the given query: " + concatenatedResult); + } + setSuccessMessage("Successfully Executed Select Query and stored in runtime variable: "+ testData3.getValue().toString()); + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/android/Sqlselectvalidate.java b/sql_server_queries/src/main/java/com/testsigma/addons/android/Sqlselectvalidate.java new file mode 100644 index 0000000..8c875d1 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/android/Sqlselectvalidate.java @@ -0,0 +1,76 @@ +package com.testsigma.addons.android; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Select_Query on the connection DB_Connection_URL for Database Database-Name using Username user-name and Password password and verify output is Expected_Value", +description = "This Action executes a given Select Query and validates the result(First cell data) against the expected value.", +applicationType = ApplicationType.ANDROID) +public class Sqlselectvalidate extends WebAction { + + @TestData(reference = "Select_Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "Expected_Value") + private com.testsigma.sdk.TestData testData3; + @TestData(reference = "user-name") + private com.testsigma.sdk.TestData userName; + @TestData(reference = "password") + private com.testsigma.sdk.TestData password; + @TestData(reference = "Database-Name") + private com.testsigma.sdk.TestData databaseName; + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = String.format("jdbc:sqlserver://%s;databaseName=%s;user=%s;password=%s;encrypt=false;",testData2.getValue().toString(), databaseName.getValue().toString(), userName.getValue().toString(), password.getValue().toString()); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + ResultSet resultSet = stmt.executeQuery(query); + resultSet.next(); + String resultData = resultSet.getObject(1).toString(); + + if(testData3.getValue().toString().equals(resultData)) { + sb.append("
The output from the Select Query is matching with expected value."); + sb.append("
Expected value:"+testData3.getValue().toString()); + sb.append("
Actual output from query:"+resultData); + setSuccessMessage(sb.toString()); + logger.info(sb.toString()); + } + else { + result = Result.FAILED; + sb.append("The selected query value not match with expected rows:" + "
"); + sb.append("Expected value of select query:"+testData3.getValue().toString() + "
"); + sb.append("Actual value from query execution:"+resultData + "
"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + sb.append("
"+errorMessage); + result = Result.FAILED; + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/android/SqlselectvalidateWithUrl.java b/sql_server_queries/src/main/java/com/testsigma/addons/android/SqlselectvalidateWithUrl.java new file mode 100644 index 0000000..6d4b673 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/android/SqlselectvalidateWithUrl.java @@ -0,0 +1,71 @@ +package com.testsigma.addons.android; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Select_Query on the connection DB_Connection_URL and verify output is Expected_Value", +description = "This Action executes a given Select Query and validates the result(First cell data) against the expected value.", +applicationType = ApplicationType.ANDROID) +public class SqlselectvalidateWithUrl extends WebAction { + + @TestData(reference = "Select_Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData databaseUrl; + @TestData(reference = "Expected_Value") + private com.testsigma.sdk.TestData testData3; + + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = databaseUrl.getValue().toString(); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + ResultSet resultSet = stmt.executeQuery(query); + resultSet.next(); + String resultData = resultSet.getObject(1).toString(); + + if(testData3.getValue().toString().equals(resultData)) { + sb.append("
The output from the Select Query is matching with expected value."); + sb.append("
Expected value:"+testData3.getValue().toString()); + sb.append("
Actual output from query:"+resultData); + setSuccessMessage(sb.toString()); + logger.info(sb.toString()); + } + else { + result = Result.FAILED; + sb.append("The selected query value not match with expected rows:" + "
"); + sb.append("Expected value of select query:"+testData3.getValue().toString() + "
"); + sb.append("Actual value from query execution:"+resultData + "
"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + sb.append("
"+errorMessage); + result = Result.FAILED; + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlValidateQueriesonTables.java b/sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlValidateQueriesonTables.java new file mode 100644 index 0000000..1ee2168 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlValidateQueriesonTables.java @@ -0,0 +1,160 @@ +package com.testsigma.addons.ios; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import javax.sql.rowset.CachedRowSet; +import javax.sql.rowset.RowSetProvider; +import java.sql.*; +import java.util.Objects; + +@Data +@Action(actionText = "Validate SQL Query Query1 and compare with the Query Query2 from the Connection DB_Connection_URL for Database Database-Name using Username user-name and Password password", +description = "This action validate queries on the database connections", +applicationType = ApplicationType.IOS) +public class SqlValidateQueriesonTables extends WebAction { + + @TestData(reference = "Query1") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "Query2") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData testData3; + @TestData(reference = "user-name") + private com.testsigma.sdk.TestData userName; + @TestData(reference = "password") + private com.testsigma.sdk.TestData password; + @TestData(reference = "Database-Name") + private com.testsigma.sdk.TestData databaseName; + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = String.format("jdbc:sqlserver://%s;databaseName=%s;user=%s;password=%s;encrypt=false;",testData3.getValue().toString(), databaseName.getValue().toString(), userName.getValue().toString(), password.getValue().toString()); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + + Connection connection1 = databaseUtil.getConnection(url); + Connection connection2 = databaseUtil.getConnection(url); + + Statement stmt1 = connection1.createStatement(); + Statement stmt2 = connection2.createStatement(); + + String query1 = testData1.getValue().toString(); + String query2 = testData2.getValue().toString(); + + ResultSet resultSet1 = stmt1.executeQuery(query1); + ResultSet resultSet2 = stmt2.executeQuery(query2); + + boolean metadataComparisonSuccess = compareMetadata(resultSet1,resultSet2); + if(!metadataComparisonSuccess) { + return Result.FAILED; + } + boolean dataComparisonSuccess = compareQueryData(resultSet1, resultSet2); + if(!dataComparisonSuccess) { + return Result.FAILED; + } + logger.info(sb.toString()); + }catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + setSuccessMessage("The two queries are have similar data"); + logger.info("The two queries are have similar data"); + logger.info(sb.toString()); + return result; + } + + private boolean compareMetadata(ResultSet resultSet1, ResultSet resultSet2) throws SQLException { + + ResultSetMetaData rsmd1 = resultSet1.getMetaData(); + ResultSetMetaData rsmd2 = resultSet2.getMetaData(); + + int columnNo1 = resultSet1.getMetaData().getColumnCount(); + int columnNo2 = resultSet2.getMetaData().getColumnCount(); + + if(columnNo1 == columnNo2) + { + if(columnNo1 != 1) + { + for(int i=1;i<=columnNo1;i++) { + if (!rsmd1.getColumnName(i).equals(rsmd2.getColumnName(i))) { + sb.append("The ColumnNames does not match:
"); + sb.append("Column name from Query-1:" +"'" +rsmd1.getColumnName(i) + "'" + "and Column name from Query-2:" + "'" +rsmd2.getColumnName(i) +"'"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + return false; + } + } + } + return true; + } + sb.append("The Columns count does not match:
"); + sb.append("Columns from Query-1:" + "'").append(columnNo1).append("'").append("and Columns from Query-2:").append("'").append(columnNo2).append("'"); + setErrorMessage(sb.toString()); + return false; + } + private boolean compareQueryData(ResultSet resultSet1, ResultSet resultSet2) throws SQLException { + int columnNo1 = resultSet1.getMetaData().getColumnCount(); + + CachedRowSet cachedResultSet1 = RowSetProvider.newFactory().createCachedRowSet(); + CachedRowSet cachedResultSet2 = RowSetProvider.newFactory().createCachedRowSet(); + + cachedResultSet1.populate(resultSet1); + cachedResultSet2.populate(resultSet2); + + while (cachedResultSet1.next() && cachedResultSet2.next()) { + for (int i = 1; i <= columnNo1; i++) { + if (!Objects.equals(cachedResultSet1.getObject(i), cachedResultSet2.getObject(i))) { + sb.append("The values do not match for column: Column Position(Starting from 1) ").append(i).append(" Row Number: ").append(cachedResultSet1.getRow()); + sb.append("
"); + sb.append("Value from Query-1: '").append(cachedResultSet1.getObject(i)).append("' and Value from Query-2: '").append(cachedResultSet2.getObject(i)).append("'"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + return false; + } + } + } + + // Log the contents of both queries when values match + StringBuilder successLog = new StringBuilder("Values match for all columns. Contents of both queries:\n"); + + cachedResultSet1.beforeFirst(); + cachedResultSet2.beforeFirst(); + + // Log the contents of cachedResultSet1 + successLog.append("Query-1:\n"); + while (cachedResultSet1.next()) { + successLog.append(cachedResultSet1.getRow()).append(": "); + for (int i = 1; i <= columnNo1; i++) { + successLog.append(cachedResultSet1.getObject(i)).append("\n"); + } + successLog.append("\n"); + } + + // Log the contents of cachedResultSet2 + successLog.append("\nQuery-2:\n"); + while (cachedResultSet2.next()) { + successLog.append(cachedResultSet2.getRow()).append(": "); + for (int i = 1; i <= columnNo1; i++) { + successLog.append(cachedResultSet2.getObject(i)).append("\n"); + } + successLog.append("\n"); + } + + logger.info(successLog.toString()); + + return true; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlValidateQueriesonTablesWithUrl.java b/sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlValidateQueriesonTablesWithUrl.java new file mode 100644 index 0000000..95c62c9 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlValidateQueriesonTablesWithUrl.java @@ -0,0 +1,155 @@ +package com.testsigma.addons.ios; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import javax.sql.rowset.CachedRowSet; +import javax.sql.rowset.RowSetProvider; +import java.sql.*; +import java.util.Objects; + +@Data +@Action(actionText = "Validate SQL Query Query1 and compare with the Query Query2 from the Connection DB_Connection_URL", +description = "This action validate queries on the database connections", +applicationType = ApplicationType.IOS) +public class SqlValidateQueriesonTablesWithUrl extends WebAction { + + @TestData(reference = "Query1") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "Query2") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData databaseUrl; + + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = databaseUrl.getValue().toString(); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + + Connection connection1 = databaseUtil.getConnection(url); + Connection connection2 = databaseUtil.getConnection(url); + + Statement stmt1 = connection1.createStatement(); + Statement stmt2 = connection2.createStatement(); + + String query1 = testData1.getValue().toString(); + String query2 = testData2.getValue().toString(); + + ResultSet resultSet1 = stmt1.executeQuery(query1); + ResultSet resultSet2 = stmt2.executeQuery(query2); + + boolean metadataComparisonSuccess = compareMetadata(resultSet1,resultSet2); + if(!metadataComparisonSuccess) { + return Result.FAILED; + } + boolean dataComparisonSuccess = compareQueryData(resultSet1, resultSet2); + if(!dataComparisonSuccess) { + return Result.FAILED; + } + logger.info(sb.toString()); + }catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + setSuccessMessage("The two queries are have similar data"); + logger.info("The two queries are have similar data"); + logger.info(sb.toString()); + return result; + } + + private boolean compareMetadata(ResultSet resultSet1, ResultSet resultSet2) throws SQLException { + + ResultSetMetaData rsmd1 = resultSet1.getMetaData(); + ResultSetMetaData rsmd2 = resultSet2.getMetaData(); + + int columnNo1 = resultSet1.getMetaData().getColumnCount(); + int columnNo2 = resultSet2.getMetaData().getColumnCount(); + + if(columnNo1 == columnNo2) + { + if(columnNo1 != 1) + { + for(int i=1;i<=columnNo1;i++) { + if (!rsmd1.getColumnName(i).equals(rsmd2.getColumnName(i))) { + sb.append("The ColumnNames does not match:
"); + sb.append("Column name from Query-1:" +"'" +rsmd1.getColumnName(i) + "'" + "and Column name from Query-2:" + "'" +rsmd2.getColumnName(i) +"'"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + return false; + } + } + } + return true; + } + sb.append("The Columns count does not match:
"); + sb.append("Columns from Query-1:" + "'").append(columnNo1).append("'").append("and Columns from Query-2:").append("'").append(columnNo2).append("'"); + setErrorMessage(sb.toString()); + return false; + } + private boolean compareQueryData(ResultSet resultSet1, ResultSet resultSet2) throws SQLException { + int columnNo1 = resultSet1.getMetaData().getColumnCount(); + + CachedRowSet cachedResultSet1 = RowSetProvider.newFactory().createCachedRowSet(); + CachedRowSet cachedResultSet2 = RowSetProvider.newFactory().createCachedRowSet(); + + cachedResultSet1.populate(resultSet1); + cachedResultSet2.populate(resultSet2); + + while (cachedResultSet1.next() && cachedResultSet2.next()) { + for (int i = 1; i <= columnNo1; i++) { + if (!Objects.equals(cachedResultSet1.getObject(i), cachedResultSet2.getObject(i))) { + sb.append("The values do not match for column: Column Position(Starting from 1) ").append(i).append(" Row Number: ").append(cachedResultSet1.getRow()); + sb.append("
"); + sb.append("Value from Query-1: '").append(cachedResultSet1.getObject(i)).append("' and Value from Query-2: '").append(cachedResultSet2.getObject(i)).append("'"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + return false; + } + } + } + + // Log the contents of both queries when values match + StringBuilder successLog = new StringBuilder("Values match for all columns. Contents of both queries:\n"); + + cachedResultSet1.beforeFirst(); + cachedResultSet2.beforeFirst(); + + // Log the contents of cachedResultSet1 + successLog.append("Query-1:\n"); + while (cachedResultSet1.next()) { + successLog.append(cachedResultSet1.getRow()).append(": "); + for (int i = 1; i <= columnNo1; i++) { + successLog.append(cachedResultSet1.getObject(i)).append("\n"); + } + successLog.append("\n"); + } + + // Log the contents of cachedResultSet2 + successLog.append("\nQuery-2:\n"); + while (cachedResultSet2.next()) { + successLog.append(cachedResultSet2.getRow()).append(": "); + for (int i = 1; i <= columnNo1; i++) { + successLog.append(cachedResultSet2.getObject(i)).append("\n"); + } + successLog.append("\n"); + } + + logger.info(successLog.toString()); + + return true; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/ios/Sqlqueries.java b/sql_server_queries/src/main/java/com/testsigma/addons/ios/Sqlqueries.java new file mode 100644 index 0000000..6ccb2ed --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/ios/Sqlqueries.java @@ -0,0 +1,67 @@ +package com.testsigma.addons.ios; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Query on the Connection DB_Connection_URL for Database Database-Name using Username user-name and Password password", +description = "This action executes given query against the connection provided and prints the no. of affected/fetched rows.", +applicationType = ApplicationType.IOS) +public class Sqlqueries extends WebAction { + + @TestData(reference = "Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "user-name") + private com.testsigma.sdk.TestData userName; + @TestData(reference = "password") + private com.testsigma.sdk.TestData password; + @TestData(reference = "Database-Name") + private com.testsigma.sdk.TestData databaseName; + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = String.format("jdbc:sqlserver://%s;databaseName=%s;user=%s;password=%s;encrypt=false;",testData2.getValue().toString(), databaseName.getValue().toString(), userName.getValue().toString(), password.getValue().toString()); + DatabaseUtil databaseUtil = new DatabaseUtil(); + int rowsUpdatedOrFetched = 0; + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + if(query.trim().toUpperCase().startsWith("SELECT")) { + ResultSet resultSet = stmt.executeQuery(query); + while (resultSet.next()){ + resultSet.getObject(1).toString(); + rowsUpdatedOrFetched ++; + } + setSuccessMessage("Successfully Executed Database Query, No. of Rows fetched from DB : " +rowsUpdatedOrFetched); + logger.info("Successfully Executed Database Query, No. of Rows fetched from DB : " +rowsUpdatedOrFetched); + }else { + rowsUpdatedOrFetched = stmt.executeUpdate(query); + setSuccessMessage("Successfully Executed Database Query, No. of rows affected in DB : " +rowsUpdatedOrFetched); + logger.info("Successfully Executed Database Query, No. of Rows fetched from DB : " +rowsUpdatedOrFetched); + } + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlqueriesValidate.java b/sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlqueriesValidate.java new file mode 100644 index 0000000..63d314d --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlqueriesValidate.java @@ -0,0 +1,83 @@ +package com.testsigma.addons.ios; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Query on the Connection DB_Connection_URL for Database Database-Name using Username user-name and Password password and verify affected rows count is Row-Count", + description = "This Action executes given SQL query and validates the affected rows.", + applicationType = ApplicationType.IOS) +public class SqlqueriesValidate extends WebAction { + + @TestData(reference = "Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "Row-Count") + private com.testsigma.sdk.TestData testData3; + @TestData(reference = "user-name") + private com.testsigma.sdk.TestData userName; + @TestData(reference = "password") + private com.testsigma.sdk.TestData password; + @TestData(reference = "Database-Name") + private com.testsigma.sdk.TestData databaseName; + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = String.format("jdbc:sqlserver://%s;databaseName=%s;user=%s;password=%s;encrypt=false;",testData2.getValue().toString(), databaseName.getValue().toString(), userName.getValue().toString(), password.getValue().toString()); + DatabaseUtil databaseUtil = new DatabaseUtil(); + int rowsUpdatedOrFetched = 0; + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + if(query.trim().toUpperCase().startsWith("SELECT")) { + ResultSet resultSet = stmt.executeQuery(query); + while (resultSet.next()){ + resultSet.getObject(1).toString(); + rowsUpdatedOrFetched ++; + } + sb.append("Successfully Executed Database Query and Rows fetched from DB : " +rowsUpdatedOrFetched + "
"); + }else { + rowsUpdatedOrFetched = stmt.executeUpdate(query); + sb.append("Successfully Executed Database Query, No. of rows affected in DB : " +rowsUpdatedOrFetched + "
"); + + } + if(rowsUpdatedOrFetched == Integer.parseInt(testData3.getValue().toString())) { + sb.append("Affected row count is matching with expected value." + "
"); + setSuccessMessage(sb.toString()); + logger.info(sb.toString()); + } + else { + result = Result.FAILED; + sb.append("The affected rows does not match with expected rows:" + "
"); + sb.append("Expected no. of affected rows:"+testData3.getValue().toString() + "
"); + sb.append("Actual affected rows from query execution:"+rowsUpdatedOrFetched + "
"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + sb.append("
"+errorMessage); + result = Result.FAILED; + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlqueriesValidateWithUrl.java b/sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlqueriesValidateWithUrl.java new file mode 100644 index 0000000..fb574e3 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlqueriesValidateWithUrl.java @@ -0,0 +1,78 @@ +package com.testsigma.addons.ios; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Query on the Connection DB_Connection_URL and verify affected rows count is Row-Count", + description = "This Action executes given SQL query and validates the affected rows.", + applicationType = ApplicationType.IOS) +public class SqlqueriesValidateWithUrl extends WebAction { + + @TestData(reference = "Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData databaseUrl; + @TestData(reference = "Row-Count") + private com.testsigma.sdk.TestData testData3; + + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = databaseUrl.getValue().toString(); + DatabaseUtil databaseUtil = new DatabaseUtil(); + int rowsUpdatedOrFetched = 0; + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + if(query.trim().toUpperCase().startsWith("SELECT")) { + ResultSet resultSet = stmt.executeQuery(query); + while (resultSet.next()){ + resultSet.getObject(1).toString(); + rowsUpdatedOrFetched ++; + } + sb.append("Successfully Executed Database Query and Rows fetched from DB : " +rowsUpdatedOrFetched + "
"); + }else { + rowsUpdatedOrFetched = stmt.executeUpdate(query); + sb.append("Successfully Executed Database Query, No. of rows affected in DB : " +rowsUpdatedOrFetched + "
"); + + } + if(rowsUpdatedOrFetched == Integer.parseInt(testData3.getValue().toString())) { + sb.append("Affected row count is matching with expected value." + "
"); + setSuccessMessage(sb.toString()); + logger.info(sb.toString()); + } + else { + result = Result.FAILED; + sb.append("The affected rows does not match with expected rows:" + "
"); + sb.append("Expected no. of affected rows:"+testData3.getValue().toString() + "
"); + sb.append("Actual affected rows from query execution:"+rowsUpdatedOrFetched + "
"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + sb.append("
"+errorMessage); + result = Result.FAILED; + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlqueriesWithUrl.java b/sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlqueriesWithUrl.java new file mode 100644 index 0000000..d064650 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlqueriesWithUrl.java @@ -0,0 +1,62 @@ +package com.testsigma.addons.ios; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Query on the Connection DB_Connection_URL", +description = "This action executes given query against the connection provided and prints the no. of affected/fetched rows.", +applicationType = ApplicationType.IOS) +public class SqlqueriesWithUrl extends WebAction { + + @TestData(reference = "Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData databaseUrl; + + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = databaseUrl.getValue().toString(); + DatabaseUtil databaseUtil = new DatabaseUtil(); + int rowsUpdatedOrFetched = 0; + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + if(query.trim().toUpperCase().startsWith("SELECT")) { + ResultSet resultSet = stmt.executeQuery(query); + while (resultSet.next()){ + resultSet.getObject(1).toString(); + rowsUpdatedOrFetched ++; + } + setSuccessMessage("Successfully Executed Database Query, No. of Rows fetched from DB : " +rowsUpdatedOrFetched); + logger.info("Successfully Executed Database Query, No. of Rows fetched from DB : " +rowsUpdatedOrFetched); + }else { + rowsUpdatedOrFetched = stmt.executeUpdate(query); + setSuccessMessage("Successfully Executed Database Query, No. of rows affected in DB : " +rowsUpdatedOrFetched); + logger.info("Successfully Executed Database Query, No. of Rows fetched from DB : " +rowsUpdatedOrFetched); + } + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/ios/Sqlselect.java b/sql_server_queries/src/main/java/com/testsigma/addons/ios/Sqlselect.java new file mode 100644 index 0000000..eab7f6d --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/ios/Sqlselect.java @@ -0,0 +1,88 @@ +package com.testsigma.addons.ios; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.RunTimeData; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Select-Query on the connection DB_Connection_URL for Database Database-Name using Username user-name and Password password and store output into a variable-name", +description = "This Action executes a given Select Query and stores the result(First cell data) into a provided runtime variable.", +applicationType = ApplicationType.IOS) +public class Sqlselect extends WebAction { + + @TestData(reference = "Select-Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "variable-name", isRuntimeVariable = true) + private com.testsigma.sdk.TestData testData3; + @TestData(reference = "user-name") + private com.testsigma.sdk.TestData userName; + @TestData(reference = "password") + private com.testsigma.sdk.TestData password; + @TestData(reference = "Database-Name") + private com.testsigma.sdk.TestData databaseName; + @RunTimeData + private com.testsigma.sdk.RunTimeData runTimeData; + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = String.format("jdbc:sqlserver://%s;databaseName=%s;user=%s;password=%s;encrypt=false;",testData2.getValue().toString(), databaseName.getValue().toString(), userName.getValue().toString(), password.getValue().toString()); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + ResultSet resultSet = stmt.executeQuery(query); + StringBuilder resultStringBuilder = new StringBuilder(); + + while (resultSet.next()) { + ResultSetMetaData metaData = resultSet.getMetaData(); + int columnCount = metaData.getColumnCount(); + + if (columnCount > 0) { + // Assuming you want the first column's value + String resultData = resultSet.getObject(1).toString(); + + // Append the resultData to the StringBuilder + resultStringBuilder.append(resultData).append(", "); + + setSuccessMessage("Successfully Executed Select Query and Resultset is : " + resultData); + logger.info("Successfully Executed Select Query and Resultset is : " + resultData); + } + } + + if (resultStringBuilder.length() > 0) { + resultStringBuilder.setLength(resultStringBuilder.length() - 2); + String concatenatedResult = resultStringBuilder.toString(); + runTimeData = new com.testsigma.sdk.RunTimeData(); + runTimeData.setValue(concatenatedResult); + runTimeData.setKey(testData3.getValue().toString()); + + logger.info("Successfully retrieved result of the given query: " + concatenatedResult); + } + setSuccessMessage("Successfully Executed Select Query and stored in runtime variable: "+ testData3.getValue().toString()); + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlselectWithUrl.java b/sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlselectWithUrl.java new file mode 100644 index 0000000..c7ee1cd --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlselectWithUrl.java @@ -0,0 +1,83 @@ +package com.testsigma.addons.ios; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.RunTimeData; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Select-Query on the connection DB_Connection_URL and store output into a variable-name", +description = "This Action executes a given Select Query and stores the result(First cell data) into a provided runtime variable.", +applicationType = ApplicationType.IOS) +public class SqlselectWithUrl extends WebAction { + + @TestData(reference = "Select-Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData databaseUrl; + @TestData(reference = "variable-name", isRuntimeVariable = true) + private com.testsigma.sdk.TestData testData3; + + @RunTimeData + private com.testsigma.sdk.RunTimeData runTimeData; + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = databaseUrl.getValue().toString(); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + ResultSet resultSet = stmt.executeQuery(query); + StringBuilder resultStringBuilder = new StringBuilder(); + + while (resultSet.next()) { + ResultSetMetaData metaData = resultSet.getMetaData(); + int columnCount = metaData.getColumnCount(); + + if (columnCount > 0) { + // Assuming you want the first column's value + String resultData = resultSet.getObject(1).toString(); + + // Append the resultData to the StringBuilder + resultStringBuilder.append(resultData).append(", "); + + setSuccessMessage("Successfully Executed Select Query and Resultset is : " + resultData); + logger.info("Successfully Executed Select Query and Resultset is : " + resultData); + } + } + + if (resultStringBuilder.length() > 0) { + resultStringBuilder.setLength(resultStringBuilder.length() - 2); + String concatenatedResult = resultStringBuilder.toString(); + runTimeData = new com.testsigma.sdk.RunTimeData(); + runTimeData.setValue(concatenatedResult); + runTimeData.setKey(testData3.getValue().toString()); + + logger.info("Successfully retrieved result of the given query: " + concatenatedResult); + } + setSuccessMessage("Successfully Executed Select Query and stored in runtime variable: "+ testData3.getValue().toString()); + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/ios/Sqlselectvalidate.java b/sql_server_queries/src/main/java/com/testsigma/addons/ios/Sqlselectvalidate.java new file mode 100644 index 0000000..2ca7cec --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/ios/Sqlselectvalidate.java @@ -0,0 +1,76 @@ +package com.testsigma.addons.ios; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Select_Query on the connection DB_Connection_URL for Database Database-Name using Username user-name and Password password and verify output is Expected_Value", +description = "This Action executes a given Select Query and validates the result(First cell data) aginst the expected value.", +applicationType = ApplicationType.IOS) +public class Sqlselectvalidate extends WebAction { + + @TestData(reference = "Select_Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "Expected_Value") + private com.testsigma.sdk.TestData testData3; + @TestData(reference = "user-name") + private com.testsigma.sdk.TestData userName; + @TestData(reference = "password") + private com.testsigma.sdk.TestData password; + @TestData(reference = "Database-Name") + private com.testsigma.sdk.TestData databaseName; + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = String.format("jdbc:sqlserver://%s;databaseName=%s;user=%s;password=%s;encrypt=false;",testData2.getValue().toString(), databaseName.getValue().toString(), userName.getValue().toString(), password.getValue().toString()); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + ResultSet resultSet = stmt.executeQuery(query); + resultSet.next(); + String resultData = resultSet.getObject(1).toString(); + + if(testData3.getValue().toString().equals(resultData)) { + sb.append("
The output from the Select Query is matching with expected value."); + sb.append("
Expected value:"+testData3.getValue().toString()); + sb.append("
Actual output from query:"+resultData); + setSuccessMessage(sb.toString()); + logger.info(sb.toString()); + } + else { + result = Result.FAILED; + sb.append("The selected query value not match with expected rows:" + "
"); + sb.append("Expected value of select query:"+testData3.getValue().toString() + "
"); + sb.append("Actual value from query execution:"+resultData + "
"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + sb.append("
"+errorMessage); + result = Result.FAILED; + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlselectvalidateWithUrl.java b/sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlselectvalidateWithUrl.java new file mode 100644 index 0000000..08cca61 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/ios/SqlselectvalidateWithUrl.java @@ -0,0 +1,71 @@ +package com.testsigma.addons.ios; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Select_Query on the connection DB_Connection_URL and verify output is Expected_Value", +description = "This Action executes a given Select Query and validates the result(First cell data) against the expected value.", +applicationType = ApplicationType.IOS) +public class SqlselectvalidateWithUrl extends WebAction { + + @TestData(reference = "Select_Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData databaseUrl; + @TestData(reference = "Expected_Value") + private com.testsigma.sdk.TestData testData3; + + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = databaseUrl.getValue().toString(); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + ResultSet resultSet = stmt.executeQuery(query); + resultSet.next(); + String resultData = resultSet.getObject(1).toString(); + + if(testData3.getValue().toString().equals(resultData)) { + sb.append("
The output from the Select Query is matching with expected value."); + sb.append("
Expected value:"+testData3.getValue().toString()); + sb.append("
Actual output from query:"+resultData); + setSuccessMessage(sb.toString()); + logger.info(sb.toString()); + } + else { + result = Result.FAILED; + sb.append("The selected query value not match with expected rows:" + "
"); + sb.append("Expected value of select query:"+testData3.getValue().toString() + "
"); + sb.append("Actual value from query execution:"+resultData + "
"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + sb.append("
"+errorMessage); + result = Result.FAILED; + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlValidateQueriesonTables.java b/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlValidateQueriesonTables.java new file mode 100644 index 0000000..46c9739 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlValidateQueriesonTables.java @@ -0,0 +1,160 @@ +package com.testsigma.addons.mobileweb; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import javax.sql.rowset.CachedRowSet; +import javax.sql.rowset.RowSetProvider; +import java.sql.*; +import java.util.Objects; + +@Data +@Action(actionText = "Validate SQL Query Query1 and compare with the Query Query2 from the Connection DB_Connection_URL for Database Database-Name using Username user-name and Password password", +description = "This action validate queries on the database connections", +applicationType = ApplicationType.MOBILE_WEB) +public class SqlValidateQueriesonTables extends WebAction { + + @TestData(reference = "Query1") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "Query2") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData testData3; + @TestData(reference = "user-name") + private com.testsigma.sdk.TestData userName; + @TestData(reference = "password") + private com.testsigma.sdk.TestData password; + @TestData(reference = "Database-Name") + private com.testsigma.sdk.TestData databaseName; + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = String.format("jdbc:sqlserver://%s;databaseName=%s;user=%s;password=%s;encrypt=false;",testData3.getValue().toString(), databaseName.getValue().toString(), userName.getValue().toString(), password.getValue().toString()); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + + Connection connection1 = databaseUtil.getConnection(url); + Connection connection2 = databaseUtil.getConnection(url); + + Statement stmt1 = connection1.createStatement(); + Statement stmt2 = connection2.createStatement(); + + String query1 = testData1.getValue().toString(); + String query2 = testData2.getValue().toString(); + + ResultSet resultSet1 = stmt1.executeQuery(query1); + ResultSet resultSet2 = stmt2.executeQuery(query2); + + boolean metadataComparisonSuccess = compareMetadata(resultSet1,resultSet2); + if(!metadataComparisonSuccess) { + return Result.FAILED; + } + boolean dataComparisonSuccess = compareQueryData(resultSet1, resultSet2); + if(!dataComparisonSuccess) { + return Result.FAILED; + } + logger.info(sb.toString()); + }catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + setSuccessMessage("The two queries are have similar data"); + logger.info("The two queries are have similar data"); + logger.info(sb.toString()); + return result; + } + + private boolean compareMetadata(ResultSet resultSet1, ResultSet resultSet2) throws SQLException { + + ResultSetMetaData rsmd1 = resultSet1.getMetaData(); + ResultSetMetaData rsmd2 = resultSet2.getMetaData(); + + int columnNo1 = resultSet1.getMetaData().getColumnCount(); + int columnNo2 = resultSet2.getMetaData().getColumnCount(); + + if(columnNo1 == columnNo2) + { + if(columnNo1 != 1) + { + for(int i=1;i<=columnNo1;i++) { + if (!rsmd1.getColumnName(i).equals(rsmd2.getColumnName(i))) { + sb.append("The ColumnNames does not match:
"); + sb.append("Column name from Query-1:" +"'" +rsmd1.getColumnName(i) + "'" + "and Column name from Query-2:" + "'" +rsmd2.getColumnName(i) +"'"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + return false; + } + } + } + return true; + } + sb.append("The Columns count does not match:
"); + sb.append("Columns from Query-1:" + "'").append(columnNo1).append("'").append("and Columns from Query-2:").append("'").append(columnNo2).append("'"); + setErrorMessage(sb.toString()); + return false; + } + private boolean compareQueryData(ResultSet resultSet1, ResultSet resultSet2) throws SQLException { + int columnNo1 = resultSet1.getMetaData().getColumnCount(); + + CachedRowSet cachedResultSet1 = RowSetProvider.newFactory().createCachedRowSet(); + CachedRowSet cachedResultSet2 = RowSetProvider.newFactory().createCachedRowSet(); + + cachedResultSet1.populate(resultSet1); + cachedResultSet2.populate(resultSet2); + + while (cachedResultSet1.next() && cachedResultSet2.next()) { + for (int i = 1; i <= columnNo1; i++) { + if (!Objects.equals(cachedResultSet1.getObject(i), cachedResultSet2.getObject(i))) { + sb.append("The values do not match for column: Column Position(Starting from 1) ").append(i).append(" Row Number: ").append(cachedResultSet1.getRow()); + sb.append("
"); + sb.append("Value from Query-1: '").append(cachedResultSet1.getObject(i)).append("' and Value from Query-2: '").append(cachedResultSet2.getObject(i)).append("'"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + return false; + } + } + } + + // Log the contents of both queries when values match + StringBuilder successLog = new StringBuilder("Values match for all columns. Contents of both queries:\n"); + + cachedResultSet1.beforeFirst(); + cachedResultSet2.beforeFirst(); + + // Log the contents of cachedResultSet1 + successLog.append("Query-1:\n"); + while (cachedResultSet1.next()) { + successLog.append(cachedResultSet1.getRow()).append(": "); + for (int i = 1; i <= columnNo1; i++) { + successLog.append(cachedResultSet1.getObject(i)).append("\n"); + } + successLog.append("\n"); + } + + // Log the contents of cachedResultSet2 + successLog.append("\nQuery-2:\n"); + while (cachedResultSet2.next()) { + successLog.append(cachedResultSet2.getRow()).append(": "); + for (int i = 1; i <= columnNo1; i++) { + successLog.append(cachedResultSet2.getObject(i)).append("\n"); + } + successLog.append("\n"); + } + + logger.info(successLog.toString()); + + return true; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlValidateQueriesonTablesWithUrl.java b/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlValidateQueriesonTablesWithUrl.java new file mode 100644 index 0000000..3666e97 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlValidateQueriesonTablesWithUrl.java @@ -0,0 +1,155 @@ +package com.testsigma.addons.mobileweb; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import javax.sql.rowset.CachedRowSet; +import javax.sql.rowset.RowSetProvider; +import java.sql.*; +import java.util.Objects; + +@Data +@Action(actionText = "Validate SQL Query Query1 and compare with the Query Query2 from the Connection DB_Connection_URL", +description = "This action validate queries on the database connections", +applicationType = ApplicationType.MOBILE_WEB) +public class SqlValidateQueriesonTablesWithUrl extends WebAction { + + @TestData(reference = "Query1") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "Query2") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData databaseUrl; + + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = databaseUrl.getValue().toString(); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + + Connection connection1 = databaseUtil.getConnection(url); + Connection connection2 = databaseUtil.getConnection(url); + + Statement stmt1 = connection1.createStatement(); + Statement stmt2 = connection2.createStatement(); + + String query1 = testData1.getValue().toString(); + String query2 = testData2.getValue().toString(); + + ResultSet resultSet1 = stmt1.executeQuery(query1); + ResultSet resultSet2 = stmt2.executeQuery(query2); + + boolean metadataComparisonSuccess = compareMetadata(resultSet1,resultSet2); + if(!metadataComparisonSuccess) { + return Result.FAILED; + } + boolean dataComparisonSuccess = compareQueryData(resultSet1, resultSet2); + if(!dataComparisonSuccess) { + return Result.FAILED; + } + logger.info(sb.toString()); + }catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + setSuccessMessage("The two queries are have similar data"); + logger.info("The two queries are have similar data"); + logger.info(sb.toString()); + return result; + } + + private boolean compareMetadata(ResultSet resultSet1, ResultSet resultSet2) throws SQLException { + + ResultSetMetaData rsmd1 = resultSet1.getMetaData(); + ResultSetMetaData rsmd2 = resultSet2.getMetaData(); + + int columnNo1 = resultSet1.getMetaData().getColumnCount(); + int columnNo2 = resultSet2.getMetaData().getColumnCount(); + + if(columnNo1 == columnNo2) + { + if(columnNo1 != 1) + { + for(int i=1;i<=columnNo1;i++) { + if (!rsmd1.getColumnName(i).equals(rsmd2.getColumnName(i))) { + sb.append("The ColumnNames does not match:
"); + sb.append("Column name from Query-1:" +"'" +rsmd1.getColumnName(i) + "'" + "and Column name from Query-2:" + "'" +rsmd2.getColumnName(i) +"'"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + return false; + } + } + } + return true; + } + sb.append("The Columns count does not match:
"); + sb.append("Columns from Query-1:" + "'").append(columnNo1).append("'").append("and Columns from Query-2:").append("'").append(columnNo2).append("'"); + setErrorMessage(sb.toString()); + return false; + } + private boolean compareQueryData(ResultSet resultSet1, ResultSet resultSet2) throws SQLException { + int columnNo1 = resultSet1.getMetaData().getColumnCount(); + + CachedRowSet cachedResultSet1 = RowSetProvider.newFactory().createCachedRowSet(); + CachedRowSet cachedResultSet2 = RowSetProvider.newFactory().createCachedRowSet(); + + cachedResultSet1.populate(resultSet1); + cachedResultSet2.populate(resultSet2); + + while (cachedResultSet1.next() && cachedResultSet2.next()) { + for (int i = 1; i <= columnNo1; i++) { + if (!Objects.equals(cachedResultSet1.getObject(i), cachedResultSet2.getObject(i))) { + sb.append("The values do not match for column: Column Position(Starting from 1) ").append(i).append(" Row Number: ").append(cachedResultSet1.getRow()); + sb.append("
"); + sb.append("Value from Query-1: '").append(cachedResultSet1.getObject(i)).append("' and Value from Query-2: '").append(cachedResultSet2.getObject(i)).append("'"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + return false; + } + } + } + + // Log the contents of both queries when values match + StringBuilder successLog = new StringBuilder("Values match for all columns. Contents of both queries:\n"); + + cachedResultSet1.beforeFirst(); + cachedResultSet2.beforeFirst(); + + // Log the contents of cachedResultSet1 + successLog.append("Query-1:\n"); + while (cachedResultSet1.next()) { + successLog.append(cachedResultSet1.getRow()).append(": "); + for (int i = 1; i <= columnNo1; i++) { + successLog.append(cachedResultSet1.getObject(i)).append("\n"); + } + successLog.append("\n"); + } + + // Log the contents of cachedResultSet2 + successLog.append("\nQuery-2:\n"); + while (cachedResultSet2.next()) { + successLog.append(cachedResultSet2.getRow()).append(": "); + for (int i = 1; i <= columnNo1; i++) { + successLog.append(cachedResultSet2.getObject(i)).append("\n"); + } + successLog.append("\n"); + } + + logger.info(successLog.toString()); + + return true; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/Sqlqueries.java b/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/Sqlqueries.java new file mode 100644 index 0000000..151b587 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/Sqlqueries.java @@ -0,0 +1,67 @@ +package com.testsigma.addons.mobileweb; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Query on the Connection DB_Connection_URL for Database Database-Name using Username user-name and Password password", +description = "This action executes given query against the connection provided and prints the no. of affected/fetched rows.", +applicationType = ApplicationType.MOBILE_WEB) +public class Sqlqueries extends WebAction { + + @TestData(reference = "Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "user-name") + private com.testsigma.sdk.TestData userName; + @TestData(reference = "password") + private com.testsigma.sdk.TestData password; + @TestData(reference = "Database-Name") + private com.testsigma.sdk.TestData databaseName; + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = String.format("jdbc:sqlserver://%s;databaseName=%s;user=%s;password=%s;encrypt=false;",testData2.getValue().toString(), databaseName.getValue().toString(), userName.getValue().toString(), password.getValue().toString()); + DatabaseUtil databaseUtil = new DatabaseUtil(); + int rowsUpdatedOrFetched = 0; + try{ + Connection connection = databaseUtil.getConnection(testData2.getValue().toString()); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + if(query.trim().toUpperCase().startsWith("SELECT")) { + ResultSet resultSet = stmt.executeQuery(query); + while (resultSet.next()){ + resultSet.getObject(1).toString(); + rowsUpdatedOrFetched ++; + } + setSuccessMessage("Successfully Executed Database Query, No. of Rows fetched from DB : " +rowsUpdatedOrFetched); + logger.info("Successfully Executed Database Query, No. of Rows fetched from DB : " +rowsUpdatedOrFetched); + }else { + rowsUpdatedOrFetched = stmt.executeUpdate(query); + setSuccessMessage("Successfully Executed Database Query, No. of rows affected in DB : " +rowsUpdatedOrFetched); + logger.info("Successfully Executed Database Query, No. of Rows fetched from DB : " +rowsUpdatedOrFetched); + } + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlqueriesValidate.java b/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlqueriesValidate.java new file mode 100644 index 0000000..2fc3f1d --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlqueriesValidate.java @@ -0,0 +1,83 @@ +package com.testsigma.addons.mobileweb; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Query on the Connection DB_Connection_URL for Database Database-Name using Username user-name and Password password and verify affected rows count is Row-Count", + description = "This Action executes given SQL query and validates the affected rows.", + applicationType = ApplicationType.MOBILE_WEB) +public class SqlqueriesValidate extends WebAction { + + @TestData(reference = "Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "Row-Count") + private com.testsigma.sdk.TestData testData3; + @TestData(reference = "user-name") + private com.testsigma.sdk.TestData userName; + @TestData(reference = "password") + private com.testsigma.sdk.TestData password; + @TestData(reference = "Database-Name") + private com.testsigma.sdk.TestData databaseName; + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = String.format("jdbc:sqlserver://%s;databaseName=%s;user=%s;password=%s;encrypt=false;",testData2.getValue().toString(), databaseName.getValue().toString(), userName.getValue().toString(), password.getValue().toString()); + DatabaseUtil databaseUtil = new DatabaseUtil(); + int rowsUpdatedOrFetched = 0; + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + if(query.trim().toUpperCase().startsWith("SELECT")) { + ResultSet resultSet = stmt.executeQuery(query); + while (resultSet.next()){ + resultSet.getObject(1).toString(); + rowsUpdatedOrFetched ++; + } + sb.append("Successfully Executed Database Query and Rows fetched from DB : " +rowsUpdatedOrFetched + "
"); + }else { + rowsUpdatedOrFetched = stmt.executeUpdate(query); + sb.append("Successfully Executed Database Query, No. of rows affected in DB : " +rowsUpdatedOrFetched + "
"); + + } + if(rowsUpdatedOrFetched == Integer.parseInt(testData3.getValue().toString())) { + sb.append("Affected row count is matching with expected value." + "
"); + setSuccessMessage(sb.toString()); + logger.info(sb.toString()); + } + else { + result = Result.FAILED; + sb.append("The affected rows does not match with expected rows:" + "
"); + sb.append("Expected no. of affected rows:"+testData3.getValue().toString() + "
"); + sb.append("Actual affected rows from query execution:"+rowsUpdatedOrFetched + "
"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + sb.append("
"+errorMessage); + result = Result.FAILED; + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlqueriesValidateWithUrl.java b/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlqueriesValidateWithUrl.java new file mode 100644 index 0000000..a55627a --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlqueriesValidateWithUrl.java @@ -0,0 +1,78 @@ +package com.testsigma.addons.mobileweb; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Query on the Connection DB_Connection_URL and verify affected rows count is Row-Count", + description = "This Action executes given SQL query and validates the affected rows.", + applicationType = ApplicationType.MOBILE_WEB) +public class SqlqueriesValidateWithUrl extends WebAction { + + @TestData(reference = "Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData databaseUrl; + @TestData(reference = "Row-Count") + private com.testsigma.sdk.TestData testData3; + + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = databaseUrl.getValue().toString(); + DatabaseUtil databaseUtil = new DatabaseUtil(); + int rowsUpdatedOrFetched = 0; + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + if(query.trim().toUpperCase().startsWith("SELECT")) { + ResultSet resultSet = stmt.executeQuery(query); + while (resultSet.next()){ + resultSet.getObject(1).toString(); + rowsUpdatedOrFetched ++; + } + sb.append("Successfully Executed Database Query and Rows fetched from DB : " +rowsUpdatedOrFetched + "
"); + }else { + rowsUpdatedOrFetched = stmt.executeUpdate(query); + sb.append("Successfully Executed Database Query, No. of rows affected in DB : " +rowsUpdatedOrFetched + "
"); + + } + if(rowsUpdatedOrFetched == Integer.parseInt(testData3.getValue().toString())) { + sb.append("Affected row count is matching with expected value." + "
"); + setSuccessMessage(sb.toString()); + logger.info(sb.toString()); + } + else { + result = Result.FAILED; + sb.append("The affected rows does not match with expected rows:" + "
"); + sb.append("Expected no. of affected rows:"+testData3.getValue().toString() + "
"); + sb.append("Actual affected rows from query execution:"+rowsUpdatedOrFetched + "
"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + sb.append("
"+errorMessage); + result = Result.FAILED; + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlqueriesWithUrl.java b/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlqueriesWithUrl.java new file mode 100644 index 0000000..455068d --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlqueriesWithUrl.java @@ -0,0 +1,62 @@ +package com.testsigma.addons.mobileweb; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Query on the Connection DB_Connection_URL", +description = "This action executes given query against the connection provided and prints the no. of affected/fetched rows.", +applicationType = ApplicationType.MOBILE_WEB) +public class SqlqueriesWithUrl extends WebAction { + + @TestData(reference = "Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData databaseUrl; + + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = databaseUrl.getValue().toString(); + DatabaseUtil databaseUtil = new DatabaseUtil(); + int rowsUpdatedOrFetched = 0; + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + if(query.trim().toUpperCase().startsWith("SELECT")) { + ResultSet resultSet = stmt.executeQuery(query); + while (resultSet.next()){ + resultSet.getObject(1).toString(); + rowsUpdatedOrFetched ++; + } + setSuccessMessage("Successfully Executed Database Query, No. of Rows fetched from DB : " +rowsUpdatedOrFetched); + logger.info("Successfully Executed Database Query, No. of Rows fetched from DB : " +rowsUpdatedOrFetched); + }else { + rowsUpdatedOrFetched = stmt.executeUpdate(query); + setSuccessMessage("Successfully Executed Database Query, No. of rows affected in DB : " +rowsUpdatedOrFetched); + logger.info("Successfully Executed Database Query, No. of Rows fetched from DB : " +rowsUpdatedOrFetched); + } + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/Sqlselect.java b/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/Sqlselect.java new file mode 100644 index 0000000..7c7add6 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/Sqlselect.java @@ -0,0 +1,88 @@ +package com.testsigma.addons.mobileweb; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.RunTimeData; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Select-Query on the connection DB_Connection_URL for Database Database-Name using Username user-name and Password password and store output into a variable-name", +description = "This Action executes a given Select Query and stores the result(First cell data) into a provided runtime variable.", +applicationType = ApplicationType.MOBILE_WEB) +public class Sqlselect extends WebAction { + + @TestData(reference = "Select-Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "variable-name", isRuntimeVariable = true) + private com.testsigma.sdk.TestData testData3; + @TestData(reference = "user-name") + private com.testsigma.sdk.TestData userName; + @TestData(reference = "password") + private com.testsigma.sdk.TestData password; + @TestData(reference = "Database-Name") + private com.testsigma.sdk.TestData databaseName; + @RunTimeData + private com.testsigma.sdk.RunTimeData runTimeData; + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = String.format("jdbc:sqlserver://%s;databaseName=%s;user=%s;password=%s;encrypt=false;",testData2.getValue().toString(), databaseName.getValue().toString(), userName.getValue().toString(), password.getValue().toString()); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + ResultSet resultSet = stmt.executeQuery(query); + StringBuilder resultStringBuilder = new StringBuilder(); + + while (resultSet.next()) { + ResultSetMetaData metaData = resultSet.getMetaData(); + int columnCount = metaData.getColumnCount(); + + if (columnCount > 0) { + // Assuming you want the first column's value + String resultData = resultSet.getObject(1).toString(); + + // Append the resultData to the StringBuilder + resultStringBuilder.append(resultData).append(", "); + + setSuccessMessage("Successfully Executed Select Query and Resultset is : " + resultData); + logger.info("Successfully Executed Select Query and Resultset is : " + resultData); + } + } + + if (resultStringBuilder.length() > 0) { + resultStringBuilder.setLength(resultStringBuilder.length() - 2); + String concatenatedResult = resultStringBuilder.toString(); + runTimeData = new com.testsigma.sdk.RunTimeData(); + runTimeData.setValue(concatenatedResult); + runTimeData.setKey(testData3.getValue().toString()); + + logger.info("Successfully retrieved result of the given query: " + concatenatedResult); + } + setSuccessMessage("Successfully Executed Select Query and stored in runtime variable: "+ testData3.getValue().toString()); + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlselectWithUrl.java b/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlselectWithUrl.java new file mode 100644 index 0000000..376d2df --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlselectWithUrl.java @@ -0,0 +1,83 @@ +package com.testsigma.addons.mobileweb; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.RunTimeData; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Select-Query on the connection DB_Connection_URL and store output into a variable-name", +description = "This Action executes a given Select Query and stores the result(First cell data) into a provided runtime variable.", +applicationType = ApplicationType.MOBILE_WEB) +public class SqlselectWithUrl extends WebAction { + + @TestData(reference = "Select-Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData databaseUrl; + @TestData(reference = "variable-name", isRuntimeVariable = true) + private com.testsigma.sdk.TestData testData3; + + @RunTimeData + private com.testsigma.sdk.RunTimeData runTimeData; + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = databaseUrl.getValue().toString(); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + ResultSet resultSet = stmt.executeQuery(query); + StringBuilder resultStringBuilder = new StringBuilder(); + + while (resultSet.next()) { + ResultSetMetaData metaData = resultSet.getMetaData(); + int columnCount = metaData.getColumnCount(); + + if (columnCount > 0) { + // Assuming you want the first column's value + String resultData = resultSet.getObject(1).toString(); + + // Append the resultData to the StringBuilder + resultStringBuilder.append(resultData).append(", "); + + setSuccessMessage("Successfully Executed Select Query and Resultset is : " + resultData); + logger.info("Successfully Executed Select Query and Resultset is : " + resultData); + } + } + + if (resultStringBuilder.length() > 0) { + resultStringBuilder.setLength(resultStringBuilder.length() - 2); + String concatenatedResult = resultStringBuilder.toString(); + runTimeData = new com.testsigma.sdk.RunTimeData(); + runTimeData.setValue(concatenatedResult); + runTimeData.setKey(testData3.getValue().toString()); + + logger.info("Successfully retrieved result of the given query: " + concatenatedResult); + } + setSuccessMessage("Successfully Executed Select Query and stored in runtime variable: "+ testData3.getValue().toString()); + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/Sqlselectvalidate.java b/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/Sqlselectvalidate.java new file mode 100644 index 0000000..c0dfd17 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/Sqlselectvalidate.java @@ -0,0 +1,76 @@ +package com.testsigma.addons.mobileweb; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Select_Query on the connection DB_Connection_URL for Database Database-Name using Username user-name and Password password and verify output is Expected_Value", +description = "This Action executes a given Select Query and validates the result(First cell data) aginst the expected value.", +applicationType = ApplicationType.MOBILE_WEB) +public class Sqlselectvalidate extends WebAction { + + @TestData(reference = "Select_Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "Expected_Value") + private com.testsigma.sdk.TestData testData3; + @TestData(reference = "user-name") + private com.testsigma.sdk.TestData userName; + @TestData(reference = "password") + private com.testsigma.sdk.TestData password; + @TestData(reference = "Database-Name") + private com.testsigma.sdk.TestData databaseName; + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = String.format("jdbc:sqlserver://%s;databaseName=%s;user=%s;password=%s;encrypt=false;",testData2.getValue().toString(), databaseName.getValue().toString(), userName.getValue().toString(), password.getValue().toString()); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + ResultSet resultSet = stmt.executeQuery(query); + resultSet.next(); + String resultData = resultSet.getObject(1).toString(); + + if(testData3.getValue().toString().equals(resultData)) { + sb.append("
The output from the Select Query is matching with expected value."); + sb.append("
Expected value:"+testData3.getValue().toString()); + sb.append("
Actual output from query:"+resultData); + setSuccessMessage(sb.toString()); + logger.info(sb.toString()); + } + else { + result = Result.FAILED; + sb.append("The selected query value not match with expected rows:" + "
"); + sb.append("Expected value of select query:"+testData3.getValue().toString() + "
"); + sb.append("Actual value from query execution:"+resultData + "
"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + sb.append("
"+errorMessage); + result = Result.FAILED; + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlselectvalidateWithUrl.java b/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlselectvalidateWithUrl.java new file mode 100644 index 0000000..81b9a83 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/mobileweb/SqlselectvalidateWithUrl.java @@ -0,0 +1,71 @@ +package com.testsigma.addons.mobileweb; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Select_Query on the connection DB_Connection_URL and verify output is Expected_Value", +description = "This Action executes a given Select Query and validates the result(First cell data) against the expected value.", +applicationType = ApplicationType.MOBILE_WEB) +public class SqlselectvalidateWithUrl extends WebAction { + + @TestData(reference = "Select_Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData databaseUrl; + @TestData(reference = "Expected_Value") + private com.testsigma.sdk.TestData testData3; + + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = databaseUrl.getValue().toString(); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + ResultSet resultSet = stmt.executeQuery(query); + resultSet.next(); + String resultData = resultSet.getObject(1).toString(); + + if(testData3.getValue().toString().equals(resultData)) { + sb.append("
The output from the Select Query is matching with expected value."); + sb.append("
Expected value:"+testData3.getValue().toString()); + sb.append("
Actual output from query:"+resultData); + setSuccessMessage(sb.toString()); + logger.info(sb.toString()); + } + else { + result = Result.FAILED; + sb.append("The selected query value not match with expected rows:" + "
"); + sb.append("Expected value of select query:"+testData3.getValue().toString() + "
"); + sb.append("Actual value from query execution:"+resultData + "
"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + sb.append("
"+errorMessage); + result = Result.FAILED; + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/util/DatabaseUtil.java b/sql_server_queries/src/main/java/com/testsigma/addons/util/DatabaseUtil.java new file mode 100644 index 0000000..6e2c80d --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/util/DatabaseUtil.java @@ -0,0 +1,15 @@ +package com.testsigma.addons.util; + + +import java.sql.Connection; +import java.sql.DriverManager; + +public class DatabaseUtil { + private String dbClass = "com.mysql.jdbc.Driver"; + public Connection getConnection(String dbURL) throws Exception { + + Class.forName(dbClass).getDeclaredConstructor().newInstance(); + Connection con = DriverManager.getConnection(dbURL); + return con; + } +} diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlValidateQueriesonTables.java b/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlValidateQueriesonTables.java new file mode 100644 index 0000000..a3ff8bd --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlValidateQueriesonTables.java @@ -0,0 +1,160 @@ +package com.testsigma.addons.web; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import javax.sql.rowset.CachedRowSet; +import javax.sql.rowset.RowSetProvider; +import java.sql.*; +import java.util.Objects; + +@Data +@Action(actionText = "Validate SQL Query Query1 and compare with the Query Query2 from the Connection DB_Connection_URL for Database Database-Name using Username user-name and Password password", +description = "This action validate queries on the database connections", +applicationType = ApplicationType.WEB) +public class SqlValidateQueriesonTables extends WebAction { + + @TestData(reference = "Query1") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "Query2") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData testData3; + @TestData(reference = "user-name") + private com.testsigma.sdk.TestData userName; + @TestData(reference = "password") + private com.testsigma.sdk.TestData password; + @TestData(reference = "Database-Name") + private com.testsigma.sdk.TestData databaseName; + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = String.format("jdbc:sqlserver://%s;databaseName=%s;user=%s;password=%s;encrypt=false;",testData3.getValue().toString(), databaseName.getValue().toString(), userName.getValue().toString(), password.getValue().toString()); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + + Connection connection1 = databaseUtil.getConnection(url); + Connection connection2 = databaseUtil.getConnection(url); + + Statement stmt1 = connection1.createStatement(); + Statement stmt2 = connection2.createStatement(); + + String query1 = testData1.getValue().toString(); + String query2 = testData2.getValue().toString(); + + ResultSet resultSet1 = stmt1.executeQuery(query1); + ResultSet resultSet2 = stmt2.executeQuery(query2); + + boolean metadataComparisonSuccess = compareMetadata(resultSet1,resultSet2); + if(!metadataComparisonSuccess) { + return Result.FAILED; + } + boolean dataComparisonSuccess = compareQueryData(resultSet1, resultSet2); + if(!dataComparisonSuccess) { + return Result.FAILED; + } + logger.info(sb.toString()); + }catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + setSuccessMessage("The two queries are have similar data"); + logger.info("The two queries are have similar data"); + logger.info(sb.toString()); + return result; + } + + private boolean compareMetadata(ResultSet resultSet1, ResultSet resultSet2) throws SQLException { + + ResultSetMetaData rsmd1 = resultSet1.getMetaData(); + ResultSetMetaData rsmd2 = resultSet2.getMetaData(); + + int columnNo1 = resultSet1.getMetaData().getColumnCount(); + int columnNo2 = resultSet2.getMetaData().getColumnCount(); + + if(columnNo1 == columnNo2) + { + if(columnNo1 != 1) + { + for(int i=1;i<=columnNo1;i++) { + if (!rsmd1.getColumnName(i).equals(rsmd2.getColumnName(i))) { + sb.append("The ColumnNames does not match:
"); + sb.append("Column name from Query-1:" +"'" +rsmd1.getColumnName(i) + "'" + "and Column name from Query-2:" + "'" +rsmd2.getColumnName(i) +"'"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + return false; + } + } + } + return true; + } + sb.append("The Columns count does not match:
"); + sb.append("Columns from Query-1:" + "'").append(columnNo1).append("'").append("and Columns from Query-2:").append("'").append(columnNo2).append("'"); + setErrorMessage(sb.toString()); + return false; + } + private boolean compareQueryData(ResultSet resultSet1, ResultSet resultSet2) throws SQLException { + int columnNo1 = resultSet1.getMetaData().getColumnCount(); + + CachedRowSet cachedResultSet1 = RowSetProvider.newFactory().createCachedRowSet(); + CachedRowSet cachedResultSet2 = RowSetProvider.newFactory().createCachedRowSet(); + + cachedResultSet1.populate(resultSet1); + cachedResultSet2.populate(resultSet2); + + while (cachedResultSet1.next() && cachedResultSet2.next()) { + for (int i = 1; i <= columnNo1; i++) { + if (!Objects.equals(cachedResultSet1.getObject(i), cachedResultSet2.getObject(i))) { + sb.append("The values do not match for column: Column Position(Starting from 1) ").append(i).append(" Row Number: ").append(cachedResultSet1.getRow()); + sb.append("
"); + sb.append("Value from Query-1: '").append(cachedResultSet1.getObject(i)).append("' and Value from Query-2: '").append(cachedResultSet2.getObject(i)).append("'"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + return false; + } + } + } + + // Log the contents of both queries when values match + StringBuilder successLog = new StringBuilder("Values match for all columns. Contents of both queries:\n"); + + cachedResultSet1.beforeFirst(); + cachedResultSet2.beforeFirst(); + + // Log the contents of cachedResultSet1 + successLog.append("Query-1:\n"); + while (cachedResultSet1.next()) { + successLog.append(cachedResultSet1.getRow()).append(": "); + for (int i = 1; i <= columnNo1; i++) { + successLog.append(cachedResultSet1.getObject(i)).append("\n"); + } + successLog.append("\n"); + } + + // Log the contents of cachedResultSet2 + successLog.append("\nQuery-2:\n"); + while (cachedResultSet2.next()) { + successLog.append(cachedResultSet2.getRow()).append(": "); + for (int i = 1; i <= columnNo1; i++) { + successLog.append(cachedResultSet2.getObject(i)).append("\n"); + } + successLog.append("\n"); + } + + logger.info(successLog.toString()); + + return true; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlValidateQueriesonTablesWithUrl.java b/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlValidateQueriesonTablesWithUrl.java new file mode 100644 index 0000000..748edd0 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlValidateQueriesonTablesWithUrl.java @@ -0,0 +1,155 @@ +package com.testsigma.addons.web; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import javax.sql.rowset.CachedRowSet; +import javax.sql.rowset.RowSetProvider; +import java.sql.*; +import java.util.Objects; + +@Data +@Action(actionText = "Validate SQL Query Query1 and compare with the Query Query2 from the Connection DB_Connection_URL", +description = "This action validate queries on the database connections", +applicationType = ApplicationType.WEB) +public class SqlValidateQueriesonTablesWithUrl extends WebAction { + + @TestData(reference = "Query1") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "Query2") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData databaseUrl; + + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = databaseUrl.getValue().toString(); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + + Connection connection1 = databaseUtil.getConnection(url); + Connection connection2 = databaseUtil.getConnection(url); + + Statement stmt1 = connection1.createStatement(); + Statement stmt2 = connection2.createStatement(); + + String query1 = testData1.getValue().toString(); + String query2 = testData2.getValue().toString(); + + ResultSet resultSet1 = stmt1.executeQuery(query1); + ResultSet resultSet2 = stmt2.executeQuery(query2); + + boolean metadataComparisonSuccess = compareMetadata(resultSet1,resultSet2); + if(!metadataComparisonSuccess) { + return Result.FAILED; + } + boolean dataComparisonSuccess = compareQueryData(resultSet1, resultSet2); + if(!dataComparisonSuccess) { + return Result.FAILED; + } + logger.info(sb.toString()); + }catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + setSuccessMessage("The two queries are have similar data"); + logger.info("The two queries are have similar data"); + logger.info(sb.toString()); + return result; + } + + private boolean compareMetadata(ResultSet resultSet1, ResultSet resultSet2) throws SQLException { + + ResultSetMetaData rsmd1 = resultSet1.getMetaData(); + ResultSetMetaData rsmd2 = resultSet2.getMetaData(); + + int columnNo1 = resultSet1.getMetaData().getColumnCount(); + int columnNo2 = resultSet2.getMetaData().getColumnCount(); + + if(columnNo1 == columnNo2) + { + if(columnNo1 != 1) + { + for(int i=1;i<=columnNo1;i++) { + if (!rsmd1.getColumnName(i).equals(rsmd2.getColumnName(i))) { + sb.append("The ColumnNames does not match:
"); + sb.append("Column name from Query-1:" +"'" +rsmd1.getColumnName(i) + "'" + "and Column name from Query-2:" + "'" +rsmd2.getColumnName(i) +"'"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + return false; + } + } + } + return true; + } + sb.append("The Columns count does not match:
"); + sb.append("Columns from Query-1:" + "'").append(columnNo1).append("'").append("and Columns from Query-2:").append("'").append(columnNo2).append("'"); + setErrorMessage(sb.toString()); + return false; + } + private boolean compareQueryData(ResultSet resultSet1, ResultSet resultSet2) throws SQLException { + int columnNo1 = resultSet1.getMetaData().getColumnCount(); + + CachedRowSet cachedResultSet1 = RowSetProvider.newFactory().createCachedRowSet(); + CachedRowSet cachedResultSet2 = RowSetProvider.newFactory().createCachedRowSet(); + + cachedResultSet1.populate(resultSet1); + cachedResultSet2.populate(resultSet2); + + while (cachedResultSet1.next() && cachedResultSet2.next()) { + for (int i = 1; i <= columnNo1; i++) { + if (!Objects.equals(cachedResultSet1.getObject(i), cachedResultSet2.getObject(i))) { + sb.append("The values do not match for column: Column Position(Starting from 1) ").append(i).append(" Row Number: ").append(cachedResultSet1.getRow()); + sb.append("
"); + sb.append("Value from Query-1: '").append(cachedResultSet1.getObject(i)).append("' and Value from Query-2: '").append(cachedResultSet2.getObject(i)).append("'"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + return false; + } + } + } + + // Log the contents of both queries when values match + StringBuilder successLog = new StringBuilder("Values match for all columns. Contents of both queries:\n"); + + cachedResultSet1.beforeFirst(); + cachedResultSet2.beforeFirst(); + + // Log the contents of cachedResultSet1 + successLog.append("Query-1:\n"); + while (cachedResultSet1.next()) { + successLog.append(cachedResultSet1.getRow()).append(": "); + for (int i = 1; i <= columnNo1; i++) { + successLog.append(cachedResultSet1.getObject(i)).append("\n"); + } + successLog.append("\n"); + } + + // Log the contents of cachedResultSet2 + successLog.append("\nQuery-2:\n"); + while (cachedResultSet2.next()) { + successLog.append(cachedResultSet2.getRow()).append(": "); + for (int i = 1; i <= columnNo1; i++) { + successLog.append(cachedResultSet2.getObject(i)).append("\n"); + } + successLog.append("\n"); + } + + logger.info(successLog.toString()); + + return true; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/web/Sqlqueries.java b/sql_server_queries/src/main/java/com/testsigma/addons/web/Sqlqueries.java new file mode 100644 index 0000000..8a88c65 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/web/Sqlqueries.java @@ -0,0 +1,77 @@ +package com.testsigma.addons.web; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Query on the Connection DB_Connection_URL for Database Database-Name using Username user-name and Password password", +description = "This action executes given query against the connection provided and prints the no. of affected/fetched rows.", +applicationType = ApplicationType.WEB) +public class Sqlqueries extends WebAction { + + @TestData(reference = "Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "user-name") + private com.testsigma.sdk.TestData userName; + @TestData(reference = "password") + private com.testsigma.sdk.TestData password; + @TestData(reference = "Database-Name") + private com.testsigma.sdk.TestData databaseName; + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = String.format("jdbc:sqlserver://%s;databaseName=%s;user=%s;password=%s;encrypt=false;",testData2.getValue().toString(), databaseName.getValue().toString(), userName.getValue().toString(), password.getValue().toString()); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + ResultSet resultSet = stmt.executeQuery(query); + ResultSetMetaData rsmd = resultSet.getMetaData(); + int columnNo = resultSet.getMetaData().getColumnCount(); + sb.append("Successfully Executed Query and Resultset is : " + "
"); + for (int i = 1; i <= columnNo; i++) { + sb.append(rsmd.getColumnName(i)); + sb.append(", "); + } + sb.append("
"); + while (resultSet.next()) { + for (int j = 1; j <= columnNo; j++) { + if (j > 1) sb.append(", "); + String columnValue = resultSet.getString(j); + if (resultSet.wasNull()) { + sb.append(""); + } + sb.append(columnValue); + } + sb.append("
"); + } + setSuccessMessage(sb.toString()); + logger.info(sb.toString()); + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlqueriesValidate.java b/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlqueriesValidate.java new file mode 100644 index 0000000..b0bcc5c --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlqueriesValidate.java @@ -0,0 +1,83 @@ +package com.testsigma.addons.web; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Query on the Connection DB_Connection_URL for Database Database-Name using Username user-name and Password password and verify affected rows count is Row-Count", + description = "This Action executes given SQL query and validates the affected rows.", + applicationType = ApplicationType.WEB) +public class SqlqueriesValidate extends WebAction { + + @TestData(reference = "Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "Row-Count") + private com.testsigma.sdk.TestData testData3; + @TestData(reference = "user-name") + private com.testsigma.sdk.TestData userName; + @TestData(reference = "password") + private com.testsigma.sdk.TestData password; + @TestData(reference = "Database-Name") + private com.testsigma.sdk.TestData databaseName; + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = String.format("jdbc:sqlserver://%s;databaseName=%s;user=%s;password=%s;encrypt=false;",testData2.getValue().toString(), databaseName.getValue().toString(), userName.getValue().toString(), password.getValue().toString()); + DatabaseUtil databaseUtil = new DatabaseUtil(); + int rowsUpdatedOrFetched = 0; + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + if(query.trim().toUpperCase().startsWith("SELECT")) { + ResultSet resultSet = stmt.executeQuery(query); + while (resultSet.next()){ + resultSet.getObject(1).toString(); + rowsUpdatedOrFetched ++; + } + sb.append("Successfully Executed Database Query and Rows fetched from DB : " +rowsUpdatedOrFetched + "
"); + }else { + rowsUpdatedOrFetched = stmt.executeUpdate(query); + sb.append("Successfully Executed Database Query, No. of rows affected in DB : " +rowsUpdatedOrFetched + "
"); + + } + if(rowsUpdatedOrFetched == Integer.parseInt(testData3.getValue().toString())) { + sb.append("Affected row count is matching with expected value." + "
"); + setSuccessMessage(sb.toString()); + logger.info(sb.toString()); + } + else { + result = Result.FAILED; + sb.append("The affected rows does not match with expected rows:" + "
"); + sb.append("Expected no. of affected rows:"+testData3.getValue().toString() + "
"); + sb.append("Actual affected rows from query execution:"+rowsUpdatedOrFetched + "
"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + sb.append("
"+errorMessage); + result = Result.FAILED; + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlqueriesValidateWithUrl.java b/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlqueriesValidateWithUrl.java new file mode 100644 index 0000000..989c719 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlqueriesValidateWithUrl.java @@ -0,0 +1,78 @@ +package com.testsigma.addons.web; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Query on the Connection DB_Connection_URL and verify affected rows count is Row-Count", + description = "This Action executes given SQL query and validates the affected rows.", + applicationType = ApplicationType.WEB) +public class SqlqueriesValidateWithUrl extends WebAction { + + @TestData(reference = "Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData databaseUrl; + @TestData(reference = "Row-Count") + private com.testsigma.sdk.TestData testData3; + + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = databaseUrl.getValue().toString(); + DatabaseUtil databaseUtil = new DatabaseUtil(); + int rowsUpdatedOrFetched = 0; + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + if(query.trim().toUpperCase().startsWith("SELECT")) { + ResultSet resultSet = stmt.executeQuery(query); + while (resultSet.next()){ + resultSet.getObject(1).toString(); + rowsUpdatedOrFetched ++; + } + sb.append("Successfully Executed Database Query and Rows fetched from DB : " +rowsUpdatedOrFetched + "
"); + }else { + rowsUpdatedOrFetched = stmt.executeUpdate(query); + sb.append("Successfully Executed Database Query, No. of rows affected in DB : " +rowsUpdatedOrFetched + "
"); + + } + if(rowsUpdatedOrFetched == Integer.parseInt(testData3.getValue().toString())) { + sb.append("Affected row count is matching with expected value." + "
"); + setSuccessMessage(sb.toString()); + logger.info(sb.toString()); + } + else { + result = Result.FAILED; + sb.append("The affected rows does not match with expected rows:" + "
"); + sb.append("Expected no. of affected rows:"+testData3.getValue().toString() + "
"); + sb.append("Actual affected rows from query execution:"+rowsUpdatedOrFetched + "
"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + sb.append("
"+errorMessage); + result = Result.FAILED; + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlqueriesWithUrl.java b/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlqueriesWithUrl.java new file mode 100644 index 0000000..cb8051e --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlqueriesWithUrl.java @@ -0,0 +1,72 @@ +package com.testsigma.addons.web; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Query on the Connection DB_Connection_URL", +description = "This action executes given query against the connection provided and prints the no. of affected/fetched rows.", +applicationType = ApplicationType.WEB) +public class SqlqueriesWithUrl extends WebAction { + + @TestData(reference = "Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData databaseUrl; + + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = databaseUrl.getValue().toString(); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + ResultSet resultSet = stmt.executeQuery(query); + ResultSetMetaData rsmd = resultSet.getMetaData(); + int columnNo = resultSet.getMetaData().getColumnCount(); + sb.append("Successfully Executed Query and Resultset is : " + "
"); + for (int i = 1; i <= columnNo; i++) { + sb.append(rsmd.getColumnName(i)); + sb.append(", "); + } + sb.append("
"); + while (resultSet.next()) { + for (int j = 1; j <= columnNo; j++) { + if (j > 1) sb.append(", "); + String columnValue = resultSet.getString(j); + if (resultSet.wasNull()) { + sb.append(""); + } + sb.append(columnValue); + } + sb.append("
"); + } + setSuccessMessage(sb.toString()); + logger.info(sb.toString()); + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/web/Sqlselect.java b/sql_server_queries/src/main/java/com/testsigma/addons/web/Sqlselect.java new file mode 100644 index 0000000..96de1a2 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/web/Sqlselect.java @@ -0,0 +1,88 @@ +package com.testsigma.addons.web; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.RunTimeData; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Select-Query on the connection DB_Connection_URL for Database Database-Name using Username user-name and Password password and store output into a variable-name", +description = "This Action executes a given Select Query and stores the result(First cell data) into a provided runtime variable.", +applicationType = ApplicationType.WEB) +public class Sqlselect extends WebAction { + + @TestData(reference = "Select-Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "variable-name", isRuntimeVariable = true) + private com.testsigma.sdk.TestData testData3; + @TestData(reference = "user-name") + private com.testsigma.sdk.TestData userName; + @TestData(reference = "password") + private com.testsigma.sdk.TestData password; + @TestData(reference = "Database-Name") + private com.testsigma.sdk.TestData databaseName; + @RunTimeData + private com.testsigma.sdk.RunTimeData runTimeData; + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = String.format("jdbc:sqlserver://%s;databaseName=%s;user=%s;password=%s;encrypt=false;",testData2.getValue().toString(), databaseName.getValue().toString(), userName.getValue().toString(), password.getValue().toString()); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + ResultSet resultSet = stmt.executeQuery(query); + StringBuilder resultStringBuilder = new StringBuilder(); + + while (resultSet.next()) { + ResultSetMetaData metaData = resultSet.getMetaData(); + int columnCount = metaData.getColumnCount(); + + if (columnCount > 0) { + // Assuming you want the first column's value + String resultData = resultSet.getObject(1).toString(); + + // Append the resultData to the StringBuilder + resultStringBuilder.append(resultData).append(", "); + + setSuccessMessage("Successfully Executed Select Query and Resultset is : " + resultData); + logger.info("Successfully Executed Select Query and Resultset is : " + resultData); + } + } + + if (resultStringBuilder.length() > 0) { + resultStringBuilder.setLength(resultStringBuilder.length() - 2); + String concatenatedResult = resultStringBuilder.toString(); + runTimeData = new com.testsigma.sdk.RunTimeData(); + runTimeData.setValue(concatenatedResult); + runTimeData.setKey(testData3.getValue().toString()); + + logger.info("Successfully retrieved result of the given query: " + concatenatedResult); + } + setSuccessMessage("Successfully Executed Select Query and stored in runtime variable: "+ testData3.getValue().toString()); + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlselectWithUrl.java b/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlselectWithUrl.java new file mode 100644 index 0000000..18791b7 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlselectWithUrl.java @@ -0,0 +1,83 @@ +package com.testsigma.addons.web; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.RunTimeData; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Select-Query on the connection DB_Connection_URL and store output into a variable-name", +description = "This Action executes a given Select Query and stores the result(First cell data) into a provided runtime variable.", +applicationType = ApplicationType.WEB) +public class SqlselectWithUrl extends WebAction { + + @TestData(reference = "Select-Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData databaseUrl; + @TestData(reference = "variable-name", isRuntimeVariable = true) + private com.testsigma.sdk.TestData testData3; + + @RunTimeData + private com.testsigma.sdk.RunTimeData runTimeData; + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = databaseUrl.getValue().toString(); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + ResultSet resultSet = stmt.executeQuery(query); + StringBuilder resultStringBuilder = new StringBuilder(); + + while (resultSet.next()) { + ResultSetMetaData metaData = resultSet.getMetaData(); + int columnCount = metaData.getColumnCount(); + + if (columnCount > 0) { + // Assuming you want the first column's value + String resultData = resultSet.getObject(1).toString(); + + // Append the resultData to the StringBuilder + resultStringBuilder.append(resultData).append(", "); + + setSuccessMessage("Successfully Executed Select Query and Resultset is : " + resultData); + logger.info("Successfully Executed Select Query and Resultset is : " + resultData); + } + } + + if (resultStringBuilder.length() > 0) { + resultStringBuilder.setLength(resultStringBuilder.length() - 2); + String concatenatedResult = resultStringBuilder.toString(); + runTimeData = new com.testsigma.sdk.RunTimeData(); + runTimeData.setValue(concatenatedResult); + runTimeData.setKey(testData3.getValue().toString()); + + logger.info("Successfully retrieved result of the given query: " + concatenatedResult); + } + setSuccessMessage("Successfully Executed Select Query and stored in runtime variable: "+ testData3.getValue().toString()); + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/web/Sqlselectvalidate.java b/sql_server_queries/src/main/java/com/testsigma/addons/web/Sqlselectvalidate.java new file mode 100644 index 0000000..541ce4e --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/web/Sqlselectvalidate.java @@ -0,0 +1,76 @@ +package com.testsigma.addons.web; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Select_Query on the connection DB_Connection_URL for Database Database-Name using Username user-name and Password password and verify output is Expected_Value", +description = "This Action executes a given Select Query and validates the result(First cell data) aginst the expected value.", +applicationType = ApplicationType.WEB) +public class Sqlselectvalidate extends WebAction { + + @TestData(reference = "Select_Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "Expected_Value") + private com.testsigma.sdk.TestData testData3; + @TestData(reference = "user-name") + private com.testsigma.sdk.TestData userName; + @TestData(reference = "password") + private com.testsigma.sdk.TestData password; + @TestData(reference = "Database-Name") + private com.testsigma.sdk.TestData databaseName; + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = String.format("jdbc:sqlserver://%s;databaseName=%s;user=%s;password=%s;encrypt=false;",testData2.getValue().toString(), databaseName.getValue().toString(), userName.getValue().toString(), password.getValue().toString()); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + ResultSet resultSet = stmt.executeQuery(query); + resultSet.next(); + String resultData = resultSet.getObject(1).toString(); + + if(testData3.getValue().toString().equals(resultData)) { + sb.append("
The output from the Select Query is matching with expected value."); + sb.append("
Expected value:"+testData3.getValue().toString()); + sb.append("
Actual output from query:"+resultData); + setSuccessMessage(sb.toString()); + logger.info(sb.toString()); + } + else { + result = Result.FAILED; + sb.append("The selected query value not match with expected rows:" + "
"); + sb.append("Expected value of select query:"+testData3.getValue().toString() + "
"); + sb.append("Actual value from query execution:"+resultData + "
"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + sb.append("
"+errorMessage); + result = Result.FAILED; + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlselectvalidateWithUrl.java b/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlselectvalidateWithUrl.java new file mode 100644 index 0000000..e1f93d4 --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlselectvalidateWithUrl.java @@ -0,0 +1,71 @@ +package com.testsigma.addons.web; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Select_Query on the connection DB_Connection_URL and verify output is Expected_Value", +description = "This Action executes a given Select Query and validates the result(First cell data) against the expected value.", +applicationType = ApplicationType.WEB) +public class SqlselectvalidateWithUrl extends WebAction { + + @TestData(reference = "Select_Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData databaseUrl; + @TestData(reference = "Expected_Value") + private com.testsigma.sdk.TestData testData3; + + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = databaseUrl.getValue().toString(); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + ResultSet resultSet = stmt.executeQuery(query); + resultSet.next(); + String resultData = resultSet.getObject(1).toString(); + + if(testData3.getValue().toString().equals(resultData)) { + sb.append("
The output from the Select Query is matching with expected value."); + sb.append("
Expected value:"+testData3.getValue().toString()); + sb.append("
Actual output from query:"+resultData); + setSuccessMessage(sb.toString()); + logger.info(sb.toString()); + } + else { + result = Result.FAILED; + sb.append("The selected query value not match with expected rows:" + "
"); + sb.append("Expected value of select query:"+testData3.getValue().toString() + "
"); + sb.append("Actual value from query execution:"+resultData + "
"); + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + sb.append("
"+errorMessage); + result = Result.FAILED; + setErrorMessage(sb.toString()); + logger.warn(sb.toString()); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/web/Sqlupdatequeries.java b/sql_server_queries/src/main/java/com/testsigma/addons/web/Sqlupdatequeries.java new file mode 100644 index 0000000..48e338e --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/web/Sqlupdatequeries.java @@ -0,0 +1,59 @@ +package com.testsigma.addons.web; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Update Query on the Connection DB_Connection_URL for Database Database-Name using Username user-name and Password password", +description = "This action executes given update query against the connection provided and prints the no. of affected/fetched rows.", +applicationType = ApplicationType.WEB) +public class Sqlupdatequeries extends WebAction { + + @TestData(reference = "Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "user-name") + private com.testsigma.sdk.TestData userName; + @TestData(reference = "password") + private com.testsigma.sdk.TestData password; + @TestData(reference = "Database-Name") + private com.testsigma.sdk.TestData databaseName; + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = String.format("jdbc:sqlserver://%s;databaseName=%s;user=%s;password=%s;encrypt=false;",testData2.getValue().toString(), databaseName.getValue().toString(), userName.getValue().toString(), password.getValue().toString()); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + int resultdata = stmt.executeUpdate(query); + + sb.append("Successfully Executed Query and affected rows are : " + "
" +resultdata); + + setSuccessMessage(sb.toString()); + logger.info(sb.toString()); + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlupdatequeriesWithUrl.java b/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlupdatequeriesWithUrl.java new file mode 100644 index 0000000..dc77b4d --- /dev/null +++ b/sql_server_queries/src/main/java/com/testsigma/addons/web/SqlupdatequeriesWithUrl.java @@ -0,0 +1,54 @@ +package com.testsigma.addons.web; + +import com.testsigma.addons.util.DatabaseUtil; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +import java.sql.Connection; +import java.sql.Statement; + +@Data +@Action(actionText = "Execute SQL Update Query on the Connection DB_Connection_URL", +description = "This action executes given update query against the connection provided and prints the no. of affected/fetched rows.", +applicationType = ApplicationType.WEB) +public class SqlupdatequeriesWithUrl extends WebAction { + + @TestData(reference = "Query") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "DB_Connection_URL") + private com.testsigma.sdk.TestData databaseUrl; + + StringBuffer sb = new StringBuffer(); + + @Override + public Result execute() throws NoSuchElementException { + Result result = Result.SUCCESS; + logger.info("Initiating execution"); + String url = databaseUrl.getValue().toString(); + DatabaseUtil databaseUtil = new DatabaseUtil(); + try{ + Connection connection = databaseUtil.getConnection(url); + Statement stmt = connection.createStatement(); + String query = testData1.getValue().toString(); + int resultdata = stmt.executeUpdate(query); + + sb.append("Successfully Executed Query and affected rows are : " + "
" +resultdata); + + setSuccessMessage(sb.toString()); + logger.info(sb.toString()); + } + catch (Exception e){ + String errorMessage = ExceptionUtils.getStackTrace(e); + result = Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + return result; + } +} \ No newline at end of file diff --git a/sql_server_queries/src/main/resources/testsigma-sdk.properties b/sql_server_queries/src/main/resources/testsigma-sdk.properties new file mode 100644 index 0000000..8d3ef90 --- /dev/null +++ b/sql_server_queries/src/main/resources/testsigma-sdk.properties @@ -0,0 +1 @@ +testsigma-sdk.api.key=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJiODUzOWIxMi0wOWM1LTc0YTktNTZjMy0xMTQxZjFmYjY0M2UiLCJ1bmlxdWVJZCI6IjI2MjkiLCJleHRlcm5hbFRlbmFudElkIjoiMjgxNyJ9.7MXdsHPzg2noIx977KJnKYWdvgdzsBYM7wCfhmXQeDWdJtw0fVpfa1JvfKxJEOyqKkYQdgUq70LiMIqhRaHF9Q \ No newline at end of file