How to validate and compare the database records using RestAssured Automation Cucumber-Feature file by using parameterization. I want the tableName, columns and condition to be passed through the feature file and parameterize it. Is there a better way to do it, please suggest.
I want the SQL query parameters to be passed through the Scenario file. In the feature file, I am passing tablename=apidb_connection_user through the Cucumber step. column= "userId" and condition= "userid = '798874745' "
Feature File:
Scenario: Validate Login
And API response matches with the database apidb_connection_user table
| userId | userid = '798874745' |
APICommonSteps.java
@And("API response matches with the database <tablename> table")
public void verifyDatabaseDetails(String tableName, DataTable data) throws SQLException {
apiUtil.verifyDatabaseDetails(tableName, new HashMap<>(data.asMap()));
}
APIUtil.java:
public void verifyDatabaseDetails(String tableName, Map<String,String> data) throws SQLException {
Connection connection = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
Statement statement = connection.createStatement();
String sqlQuery = "Select" + column + " from " + tableName +"where" + condition;
ResultSet resultSet = statement.executeQuery(sqlQuery);
String databaseJson = String.valueOf(convertResultSetToJson(resultSet));
String apiResponseJson = response.getBody().asString();
softAssert.assertEquals(databaseJson, apiResponseJson, String.valueOf(true));
resultSet.close();
statement.close();
connection.close();
}
private static JsonArray convertResultSetToJson(ResultSet resultSet) throws SQLException {
JsonArray jsonArray = new JsonArray();
while (resultSet.next()) {
int totalColumns = resultSet.getMetaData().getColumnCount();
JsonObject jsonObject = new JsonObject();
for (int i = 1; i <= totalColumns; i++) {
String columnName = resultSet.getMetaData().getColumnName(i);
String columnValue = resultSet.getString(i);
jsonObject.addProperty(columnName, columnValue);
}
jsonArray.add(jsonObject);
}
return jsonArray;
}