How to use performValidation method of com.consol.citrus.actions.ExecuteSQLQueryAction class

Best Citrus code snippet using com.consol.citrus.actions.ExecuteSQLQueryAction.performValidation

Source:ExecuteSQLQueryAction.java Github

copy

Full Screen

...83 } else {84 executeStatements(allResultRows, columnValuesMap, context);85 }86 // perform validation87 performValidation(columnValuesMap, allResultRows, context);88 // fill the request test context variables (extract tag)89 fillContextVariables(columnValuesMap, context);90 // legacy: save all columns as variables TODO: remove in major version upgrade91 for (Entry<String, List<String>> column : columnValuesMap.entrySet()) {92 List<String> columnValues = column.getValue();93 context.setVariable(column.getKey().toUpperCase(), columnValues.get(0) == null ? NULL_VALUE : columnValues.get(0));94 }95 } catch (DataAccessException e) {96 log.error("Failed to execute SQL statement", e);97 throw new CitrusRuntimeException(e);98 }99 }100 protected void executeStatements(List<Map<String, Object>> allResultRows, Map<String, List<String>> columnValuesMap, TestContext context) {101 for (String stmt : statements) {102 validateSqlStatement(stmt);103 final String toExecute;104 if (stmt.trim().endsWith(";")) {105 toExecute = context.replaceDynamicContentInString(stmt.trim().substring(0, stmt.trim().length()-1));106 } else {107 toExecute = context.replaceDynamicContentInString(stmt.trim());108 }109 if (log.isDebugEnabled()) {110 log.debug("Executing SQL query: " + toExecute);111 }112 List<Map<String, Object>> results = getJdbcTemplate().queryForList(toExecute);113 log.info("SQL query execution successful");114 allResultRows.addAll(results);115 fillColumnValuesMap(results, columnValuesMap);116 }117 }118 /**119 * Fills the (requested) test context variables with the db result values120 * @param columnValuesMap the map containing column names --> list of result values121 * @param context the test context the variables are stored to122 * @throws CitrusRuntimeException if requested column name was not found123 */124 private void fillContextVariables(Map<String, List<String>> columnValuesMap, TestContext context)125 throws CitrusRuntimeException {126 for (Entry<String, String> variableEntry : extractVariables.entrySet()) {127 String columnName = variableEntry.getKey();128 if (columnValuesMap.containsKey(columnName.toLowerCase())) {129 context.setVariable(variableEntry.getValue(), constructVariableValue(columnValuesMap.get(columnName.toLowerCase())));130 } else if (columnValuesMap.containsKey(columnName.toUpperCase())) {131 context.setVariable(variableEntry.getValue(), constructVariableValue(columnValuesMap.get(columnName.toUpperCase())));132 } else {133 throw new CitrusRuntimeException("Failed to create variables from database values! " +134 "Unable to find column '" + columnName + "' in database result set");135 }136 }137 }138 /**139 * Form a Map object which contains all columns of the result as keys140 * and a List of row values as values of the Map141 * @param results result map from last jdbc query execution142 * @param columnValuesMap map holding all result columns and corresponding values143 */144 private void fillColumnValuesMap(List<Map<String, Object>> results, Map<String, List<String>> columnValuesMap) {145 for (Map<String, Object> row : results) {146 for (Entry<String, Object> column : row.entrySet()) {147 String columnValue;148 String columnName = column.getKey();149 if (!columnValuesMap.containsKey(columnName)) {150 columnValuesMap.put(columnName, new ArrayList<String>());151 }152 if (column.getValue() instanceof byte[]) {153 columnValue = Base64.encodeBase64String((byte[]) column.getValue());154 } else {155 columnValue = column.getValue() == null ? null : column.getValue().toString();156 }157 columnValuesMap.get(columnName).add((columnValue));158 }159 }160 }161 /**162 * Gets the script validator implementation either autowired from application context163 * or if not set here a default implementation.164 */165 private SqlResultSetScriptValidator getScriptValidator() {166 if (validator != null) {167 return validator;168 } else {169 return new GroovySqlResultSetValidator();170 }171 }172 /**173 * Constructs a delimited string from multiple row values in result set in order to174 * set this expression as variable value.175 *176 * @param rowValues the list of values representing the allResultRows for a column in the result set.177 * @return the variable value as delimited string or single value.178 */179 private String constructVariableValue(List<String> rowValues) {180 if (CollectionUtils.isEmpty(rowValues)) {181 return "";182 } else if (rowValues.size() == 1) {183 return rowValues.get(0) == null ? NULL_VALUE : rowValues.get(0);184 } else {185 StringBuilder result = new StringBuilder();186 Iterator<String> it = rowValues.iterator();187 result.append(it.next());188 while (it.hasNext()) {189 String nextValue = it.next();190 result.append(";" + (nextValue == null ? NULL_VALUE : nextValue));191 }192 return result.toString();193 }194 }195 /**196 * Validates the database result set. At first script validation is done (if any was given).197 * Afterwards the control result set validation is performed.198 *199 * @param columnValuesMap map containing column names as keys and list of string as retrieved values from db200 * @param allResultRows list of all result rows retrieved from database201 * @return success flag202 * @throws UnknownElementException203 * @throws ValidationException204 */205 private void performValidation(final Map<String, List<String>> columnValuesMap,206 List<Map<String, Object>> allResultRows, TestContext context)207 throws UnknownElementException, ValidationException {208 // apply script validation if specified209 if (scriptValidationContext != null) {210 getScriptValidator().validateSqlResultSet(allResultRows, scriptValidationContext, context);211 }212 //now apply control set validation if specified213 if (CollectionUtils.isEmpty(controlResultSet)) {214 return;215 }216 performControlResultSetValidation(columnValuesMap, context);217 log.info("SQL query validation successful: All values OK");218 }219 private void performControlResultSetValidation(final Map<String, List<String>> columnValuesMap, TestContext context)...

Full Screen

Full Screen

performValidation

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;2import com.consol.citrus.db.driver.JdbcDriver;3import com.consol.citrus.db.driver.JdbcResultSet;4import com.consol.citrus.db.driver.JdbcResultSetMetaData;5import com.consol.citrus.db.driver.JdbcRow;6import com.consol.citrus.db.driver.JdbcRowBuilder;7import com.consol.citrus.db.driver.JdbcStatement;8import com.consol.citrus.db.driver.JdbcStatementBuilder;9import com.consol.citrus.db.driver.JdbcStatementResult;10import com.consol.citrus.db.driver.JdbcStatementResultBuilder;11import com.consol.citrus.db.driver.JdbcTable;12import com.consol.citrus.db.driver.JdbcTableBuilder;13import com.consol.citrus.db.driver.JdbcTableRow;14import com.consol.citrus.db.driver.JdbcTableRowBuilder;15import com.consol.citrus.db.driver.JdbcTableRows;16import com.consol.citrus.db.driver.JdbcTableRowsBuilder;17import com.consol.citrus.db.server.JdbcServer;18import com.consol.citrus.db.server.JdbcServerBuilder;19import com.consol.citrus.db.server.handler.QueryHandler;20import com.consol.citrus.db.server.handler.StatementHandler;21import com.consol.citrus.db.server.handler.TransactionHandler;22import com.consol.citrus.exceptions.ValidationException;23import com.consol.citrus.testng.CitrusParameters;24import com.consol.citrus.validation.builder.BuilderSupport;25import com.consol.citrus.validation.builder.StaticMessageContentBuilder;26import com.consol.citrus.validation.json.JsonTextMessageValidator;27import com.consol.citrus.variable.GlobalVariables;28import com.consol.citrus.variable.VariableUtils;29import org.springframework.core.io.ClassPathResource;30import org.springframework.http.HttpStatus;31import org.springframework.jdbc.core.JdbcTemplate;32import org.springframework.jdbc.datasource.DriverManagerDataSource;33import org.testng.annotations.*;34import java.io.IOException;35import java.sql.*;36import java.util.*;37import static org.testng.Assert.*;38public class TestClass extends TestNGCitrusTestDesigner {39 private DriverManagerDataSource dataSource;40 private JdbcTemplate jdbcTemplate;41 private JdbcServer jdbcServer;

Full Screen

Full Screen

performValidation

Using AI Code Generation

copy

Full Screen

1public void testSqlQuery() {2 variable("sqlQuery", "select * from table_name");3 variable("sqlResult", "select * from table_name");4 given()5 .database()6 .statement("select * from table_name")7 .queryResult()8 .row()9 .column("column_name", "column_value")10 .row()11 .column("column_name", "column_value")12 .row()13 .column("column_name", "column_value");14 when()15 .sql()16 .statement("${sqlQuery}")17 .execute();18 then()19 .validate("${sqlResult}", performValidation()20 .row()21 .column("column_name", "column_value")22 .row()23 .column("column_name", "column_value")24 .row()25 .column("column_name", "column_value"));26}27public void testSqlQuery() {28 variable("sqlQuery", "select * from table_name");29 variable("sqlResult", "select * from table_name");30 given()31 .database()32 .statement("select * from table_name")33 .queryResult()34 .row()35 .column("column_name", "column_value")36 .row()37 .column("column_name", "column_value")38 .row()39 .column("column_name", "column_value");40 when()41 .sql()42 .statement("${sqlQuery}")43 .execute();44 then()45 .validate("${sqlResult}", performValidation()46 .row()47 .column("column_name", "column_value")48 .row()49 .column("column_name", "column_value")50 .row()51 .column("column_name", "column_value"));52}53public void testSqlQuery() {54 variable("sqlQuery", "select * from table_name");55 variable("sqlResult", "select * from table_name");56 given()57 .database()58 .statement("select * from table_name")59 .queryResult()60 .row()61 .column("column_name", "column_value")62 .row()63 .column("column_name", "column_value")64 .row()65 .column("column_name",

Full Screen

Full Screen

performValidation

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.dsl.design.TestDesigner;2import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;3import com.consol.citrus.message.MessageType;4import org.springframework.beans.factory.annotation.Autowired;5import org.springframework.jdbc.core.JdbcTemplate;6import org.springframework.jdbc.datasource.DriverManagerDataSource;7import org.testng.annotations.Test;8import java.util.ArrayList;9import java.util.List;10public class ExecuteSQLQueryActionTest extends TestNGCitrusTestDesigner {11 private DriverManagerDataSource dataSource;12 public void configure() {13 JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);14 jdbcTemplate.update("insert into CITRUS_TEST (ID, NAME, DESCRIPTION) values (1, 'citrus', 'citrus rocks')");15 jdbcTemplate.update("insert into CITRUS_TEST (ID, NAME, DESCRIPTION) values (2, 'citrus', 'citrus rocks')");16 jdbcTemplate.update("insert into CITRUS_TEST (ID, NAME, DESCRIPTION) values (3, 'citrus', 'citrus rocks')");17 jdbcTemplate.update("insert into CITRUS_TEST (ID, NAME, DESCRIPTION) values (4, 'citrus', 'citrus rocks')");18 jdbcTemplate.update("insert into CITRUS_TEST (ID, NAME, DESCRIPTION) values (5, 'citrus', 'citrus rocks')");19 jdbcTemplate.update("insert into CITRUS_TEST (ID, NAME, DESCRIPTION) values (6, 'citrus', 'citrus rocks')");20 jdbcTemplate.update("insert into CITRUS_TEST (ID, NAME, DESCRIPTION) values (7, 'citrus', 'citrus rocks')");21 jdbcTemplate.update("insert into CITRUS_TEST (ID, NAME, DESCRIPTION) values (8, 'citrus', 'citrus rocks')");22 jdbcTemplate.update("insert into CITRUS_TEST (ID, NAME, DESCRIPTION) values (9, 'cit

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful