From fed0d3a80a99b445fabad8ed95378a5aebdabd7b Mon Sep 17 00:00:00 2001 From: Timon Borter Date: Fri, 15 Sep 2023 14:48:59 +0200 Subject: [PATCH] fix(#984): allow with sql keyword for query action the `WITH` clause (also known as Common Table Expressions (CTEs)) is part of the SQL standard SQL:1999. according to the [list of SQL reserved words](https://en.wikipedia.org/wiki/List_of_SQL_reserved_words) all modern databases support it (as they should). --- .../actions/ExecuteSQLQueryAction.java | 25 +- .../actions/ExecuteSQLQueryActionTest.java | 232 ++++++++++-------- .../actions/test-sql-query-statements.sql | 4 +- 3 files changed, 145 insertions(+), 116 deletions(-) diff --git a/connectors/citrus-sql/src/main/java/org/citrusframework/actions/ExecuteSQLQueryAction.java b/connectors/citrus-sql/src/main/java/org/citrusframework/actions/ExecuteSQLQueryAction.java index 8e8182e6fb..e1c64148a1 100644 --- a/connectors/citrus-sql/src/main/java/org/citrusframework/actions/ExecuteSQLQueryAction.java +++ b/connectors/citrus-sql/src/main/java/org/citrusframework/actions/ExecuteSQLQueryAction.java @@ -143,14 +143,18 @@ public void doExecute(TestContext context) { * @param context */ protected void executeStatements(List statements, List> allResultRows, Map> columnValuesMap, TestContext context) { - for (String stmt : statements) { - validateSqlStatement(stmt); - final String toExecute; + if (getJdbcTemplate() == null) { + throw new CitrusRuntimeException("No JdbcTemplate configured for query execution!"); + } - if (stmt.trim().endsWith(";")) { - toExecute = context.replaceDynamicContentInString(stmt.trim().substring(0, stmt.trim().length()-1)); + for (String statement : statements) { + validateSqlStatement(statement); + + final String toExecute; + if (statement.trim().endsWith(";")) { + toExecute = context.replaceDynamicContentInString(statement.trim().substring(0, statement.trim().length() - 1)); } else { - toExecute = context.replaceDynamicContentInString(stmt.trim()); + toExecute = context.replaceDynamicContentInString(statement.trim()); } if (log.isDebugEnabled()) { @@ -333,11 +337,12 @@ private void performControlResultSetValidation(final Map> c /** * Does some simple validation on the SQL statement. - * @param stmt The statement which is to be validated. + * @param statement The statement which is to be validated. */ - protected void validateSqlStatement(String stmt) { - if (!stmt.toLowerCase().startsWith("select")) { - throw new CitrusRuntimeException("Missing keyword SELECT in statement: " + stmt); + protected void validateSqlStatement(String statement) { + String trimmedStatement = statement.toLowerCase().trim(); + if (!(trimmedStatement.startsWith("select") || trimmedStatement.startsWith("with"))) { + throw new CitrusRuntimeException("Missing SELECT or WITH keyword in statement: " + trimmedStatement); } } diff --git a/connectors/citrus-sql/src/test/java/org/citrusframework/actions/ExecuteSQLQueryActionTest.java b/connectors/citrus-sql/src/test/java/org/citrusframework/actions/ExecuteSQLQueryActionTest.java index e4d27da58d..fbc34288b3 100644 --- a/connectors/citrus-sql/src/test/java/org/citrusframework/actions/ExecuteSQLQueryActionTest.java +++ b/connectors/citrus-sql/src/test/java/org/citrusframework/actions/ExecuteSQLQueryActionTest.java @@ -50,8 +50,9 @@ */ public class ExecuteSQLQueryActionTest extends UnitTestSupport { - private static final String DB_STMT_1 = "select ORDERTYPE, STATUS from orders where ID=5"; - private static final String DB_STMT_2 = "select NAME, HEIGHT from customers where ID=1"; + private static final String DB_STMT_1 = "select ORDERTYPE, STATUS from orders where ID = 5"; + private static final String DB_STMT_2 = "select NAME, HEIGHT from customers where ID = 1"; + private static final String DB_STMT_3 = "WITH RECURSIVE relations AS (SELECT id, framework_name FROM framework f INNER JOIN relations r ON f.relation_id = r.id) SELECT * FROM relations"; private ExecuteSQLQueryAction.Builder executeSQLQueryAction; @@ -77,14 +78,14 @@ public void testSQLStatement() { String sql = DB_STMT_1; reset(jdbcTemplate); - Map resultMap = new HashMap(); + Map resultMap = new HashMap<>(); resultMap.put("ORDERTYPE", "small"); resultMap.put("STATUS", "in_progress"); when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List stmts = Collections.singletonList(sql); - executeSQLQueryAction.statements(stmts); + List statements = Collections.singletonList(sql); + executeSQLQueryAction.statements(statements); executeSQLQueryAction.build().execute(context); Assert.assertNotNull(context.getVariable("${ORDERTYPE}")); @@ -98,14 +99,14 @@ public void testSQLStatementWithTransaction() { String sql = DB_STMT_1; reset(jdbcTemplate, transactionManager); - Map resultMap = new HashMap(); + Map resultMap = new HashMap<>(); resultMap.put("ORDERTYPE", "small"); resultMap.put("STATUS", "in_progress"); when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List stmts = Collections.singletonList(sql); - executeSQLQueryAction.statements(stmts); + List statements = Collections.singletonList(sql); + executeSQLQueryAction.statements(statements); executeSQLQueryAction.transactionManager(transactionManager); executeSQLQueryAction.build().execute(context); @@ -120,14 +121,14 @@ public void testSQLStatementLowerCaseColumnNames() { String sql = DB_STMT_1; reset(jdbcTemplate); - Map resultMap = new HashMap(); + Map resultMap = new HashMap<>(); resultMap.put("ordertype", "small"); resultMap.put("status", "in_progress"); when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List stmts = Collections.singletonList(sql); - executeSQLQueryAction.statements(stmts); + List statements = Collections.singletonList(sql); + executeSQLQueryAction.statements(statements); executeSQLQueryAction.build().execute(context); Assert.assertNotNull(context.getVariable("${ORDERTYPE}")); @@ -140,25 +141,33 @@ public void testSQLStatementLowerCaseColumnNames() { public void testSQLMultipleStatements() { String sql1 = DB_STMT_1; String sql2 = DB_STMT_2; + String sql3 = DB_STMT_3; reset(jdbcTemplate); - Map resultMap1 = new HashMap(); + Map resultMap1 = new HashMap<>(); resultMap1.put("ORDERTYPE", "small"); resultMap1.put("STATUS", "in_progress"); when(jdbcTemplate.queryForList(sql1)).thenReturn(Collections.singletonList(resultMap1)); - Map resultMap2 = new HashMap(); + Map resultMap2 = new HashMap<>(); resultMap2.put("NAME", "Mickey Mouse"); resultMap2.put("HEIGHT", "0,3"); when(jdbcTemplate.queryForList(sql2)).thenReturn(Collections.singletonList(resultMap2)); - List stmts = new ArrayList(); - stmts.add(sql1); - stmts.add(sql2); + Map resultMap3 = new HashMap<>(); + resultMap2.put("ID", "1234"); + resultMap2.put("FRAMEWORK_NAME", "citrusframework/citrus"); - executeSQLQueryAction.statements(stmts); + when(jdbcTemplate.queryForList(sql3)).thenReturn(Collections.singletonList(resultMap3)); + + List statements = new ArrayList<>(); + statements.add(sql1); + statements.add(sql2); + statements.add(sql3); + + executeSQLQueryAction.statements(statements); executeSQLQueryAction.build().execute(context); Assert.assertNotNull(context.getVariable("${ORDERTYPE}")); @@ -169,6 +178,10 @@ public void testSQLMultipleStatements() { Assert.assertEquals(context.getVariable("${NAME}"), "Mickey Mouse"); Assert.assertNotNull(context.getVariable("${HEIGHT}")); Assert.assertEquals(context.getVariable("${HEIGHT}"), "0,3"); + Assert.assertNotNull(context.getVariable("${ID}")); + Assert.assertEquals(context.getVariable("${ID}"), "1234"); + Assert.assertNotNull(context.getVariable("${FRAMEWORK_NAME}")); + Assert.assertEquals(context.getVariable("${FRAMEWORK_NAME}"), "citrusframework/citrus"); } @Test @@ -177,13 +190,13 @@ public void testSQLResource() { String sql2 = "SELECT NAME, HEIGHT FROM customers WHERE ID=1"; reset(jdbcTemplate); - Map resultMap1 = new HashMap(); + Map resultMap1 = new HashMap<>(); resultMap1.put("ORDERTYPE", "small"); resultMap1.put("STATUS", "in_progress"); when(jdbcTemplate.queryForList(sql1)).thenReturn(Collections.singletonList(resultMap1)); - Map resultMap2 = new HashMap(); + Map resultMap2 = new HashMap<>(); resultMap2.put("NAME", "Mickey Mouse"); resultMap2.put("HEIGHT", "0,3"); @@ -207,14 +220,14 @@ public void testNullValue() { String sql = DB_STMT_1; reset(jdbcTemplate); - Map resultMap = new HashMap(); + Map resultMap = new HashMap<>(); resultMap.put("ORDERTYPE", "small"); resultMap.put("STATUS", null); when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List stmts = Collections.singletonList(sql); - executeSQLQueryAction.statements(stmts); + List statements = Collections.singletonList(sql); + executeSQLQueryAction.statements(statements); executeSQLQueryAction.build().execute(context); Assert.assertNotNull(context.getVariable("${ORDERTYPE}")); @@ -223,21 +236,21 @@ public void testNullValue() { Assert.assertEquals(context.getVariable("${STATUS}"), "NULL"); } - @Test + @Test public void testVariableSupport() { - context.setVariable("orderId", "5"); + context.setVariable("orderId", "5"); - String sql = "select ORDERTYPE, STATUS from orders where ID=${orderId}"; + String sql = "select ORDERTYPE, STATUS from orders where ID = ${orderId}"; reset(jdbcTemplate); - Map resultMap = new HashMap(); + Map resultMap = new HashMap<>(); resultMap.put("ORDERTYPE", "small"); resultMap.put("STATUS", "in_progress"); when(jdbcTemplate.queryForList(DB_STMT_1)).thenReturn(Collections.singletonList(resultMap)); - List stmts = Collections.singletonList(sql); - executeSQLQueryAction.statements(stmts); + List statements = Collections.singletonList(sql); + executeSQLQueryAction.statements(statements); executeSQLQueryAction.build().execute(context); Assert.assertNotNull(context.getVariable("${ORDERTYPE}")); @@ -251,14 +264,14 @@ public void testExtractToVariables() { String sql = DB_STMT_1; reset(jdbcTemplate); - Map resultMap = new HashMap(); + Map resultMap = new HashMap<>(); resultMap.put("ORDERTYPE", "small"); resultMap.put("STATUS", "in_progress"); when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List stmts = Collections.singletonList(sql); - executeSQLQueryAction.statements(stmts); + List statements = Collections.singletonList(sql); + executeSQLQueryAction.statements(statements); executeSQLQueryAction.extract("STATUS", "orderStatus"); executeSQLQueryAction.build().execute(context); @@ -275,14 +288,14 @@ public void testExtractToVariablesLowerCaseColumnNames() { String sql = DB_STMT_1; reset(jdbcTemplate); - Map resultMap = new HashMap(); + Map resultMap = new HashMap<>(); resultMap.put("ordertype", "small"); resultMap.put("status", "in_progress"); when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List stmts = Collections.singletonList(sql); - executeSQLQueryAction.statements(stmts); + List statements = Collections.singletonList(sql); + executeSQLQueryAction.statements(statements); executeSQLQueryAction.extract("ordertype", "orderType"); executeSQLQueryAction.extract("STATUS", "orderStatus"); executeSQLQueryAction.build().execute(context); @@ -302,14 +315,14 @@ public void testExtractToVariablesUnknownColumnMapping() { String sql = DB_STMT_1; reset(jdbcTemplate); - Map resultMap = new HashMap(); + Map resultMap = new HashMap<>(); resultMap.put("ORDERTYPE", "small"); resultMap.put("STATUS", "in_progress"); when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List stmts = Collections.singletonList(sql); - executeSQLQueryAction.statements(stmts); + List statements = Collections.singletonList(sql); + executeSQLQueryAction.statements(statements); executeSQLQueryAction.extract("UNKNOWN_COLUMN", "orderStatus"); executeSQLQueryAction.build().execute(context); } @@ -319,14 +332,14 @@ public void testResultSetValidation() { String sql = DB_STMT_1; reset(jdbcTemplate); - Map resultMap = new HashMap(); + Map resultMap = new HashMap<>(); resultMap.put("ORDERTYPE", "small"); resultMap.put("STATUS", "in_progress"); when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List stmts = Collections.singletonList(sql); - executeSQLQueryAction.statements(stmts); + List statements = Collections.singletonList(sql); + executeSQLQueryAction.statements(statements); executeSQLQueryAction.validate("ORDERTYPE", "small"); executeSQLQueryAction.validate("STATUS", "in_progress"); executeSQLQueryAction.build().execute(context); @@ -342,14 +355,14 @@ public void testResultSetValidationLowerCase() { String sql = DB_STMT_1; reset(jdbcTemplate); - Map resultMap = new HashMap(); + Map resultMap = new HashMap<>(); resultMap.put("ordertype", "small"); resultMap.put("status", "in_progress"); when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List stmts = Collections.singletonList(sql); - executeSQLQueryAction.statements(stmts); + List statements = Collections.singletonList(sql); + executeSQLQueryAction.statements(statements); executeSQLQueryAction.validate("ORDERTYPE", "small"); executeSQLQueryAction.validate("STATUS", "in_progress"); executeSQLQueryAction.build().execute(context); @@ -365,14 +378,14 @@ public void testResultSetValidationWithAliasNames() { String sql = "select ORDERTYPE AS TYPE, STATUS AS STATE from orders where ID=5"; reset(jdbcTemplate); - Map resultMap = new HashMap(); + Map resultMap = new HashMap<>(); resultMap.put("TYPE", "small"); resultMap.put("STATE", "in_progress"); when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List stmts = Collections.singletonList(sql); - executeSQLQueryAction.statements(stmts); + List statements = Collections.singletonList(sql); + executeSQLQueryAction.statements(statements); executeSQLQueryAction.validate("TYPE", "small"); executeSQLQueryAction.validate("STATE", "in_progress"); executeSQLQueryAction.build().execute(context); @@ -388,14 +401,14 @@ public void testResultSetValidationError() { String sql = DB_STMT_1; reset(jdbcTemplate); - Map resultMap = new HashMap(); + Map resultMap = new HashMap<>(); resultMap.put("ORDERTYPE", "small"); resultMap.put("STATUS", "in_progress"); when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List stmts = Collections.singletonList(sql); - executeSQLQueryAction.statements(stmts); + List statements = Collections.singletonList(sql); + executeSQLQueryAction.statements(statements); executeSQLQueryAction.validate("ORDERTYPE", "xxl"); //this is supposed to cause an error executeSQLQueryAction.validate("STATUS", "in_progress"); @@ -416,10 +429,10 @@ public void testResultSetMultipleRowsValidation() { String sql = "select ORDERTYPE, STATUS from orders where ID < 5"; reset(jdbcTemplate); - List> resultList = new ArrayList>(); - Map resultRow1 = new HashMap(); - Map resultRow2 = new HashMap(); - Map resultRow3 = new HashMap(); + List> resultList = new ArrayList<>(); + Map resultRow1 = new HashMap<>(); + Map resultRow2 = new HashMap<>(); + Map resultRow3 = new HashMap<>(); resultRow1.put("ORDERTYPE", "small"); resultRow1.put("STATUS", "started"); @@ -433,8 +446,8 @@ public void testResultSetMultipleRowsValidation() { when(jdbcTemplate.queryForList(sql)).thenReturn(resultList); - List stmts = Collections.singletonList(sql); - executeSQLQueryAction.statements(stmts); + List statements = Collections.singletonList(sql); + executeSQLQueryAction.statements(statements); executeSQLQueryAction.validate("ORDERTYPE", "small", "medium", "big"); executeSQLQueryAction.validate("STATUS", "started", "in_progress", "finished"); executeSQLQueryAction.build().execute(context); @@ -450,10 +463,10 @@ public void testNullValuesInMultipleRowsValidation() { String sql = "select ORDERTYPE, STATUS from orders where ID < 5"; reset(jdbcTemplate); - List> resultList = new ArrayList>(); - Map resultRow1 = new HashMap(); - Map resultRow2 = new HashMap(); - Map resultRow3 = new HashMap(); + List> resultList = new ArrayList<>(); + Map resultRow1 = new HashMap<>(); + Map resultRow2 = new HashMap<>(); + Map resultRow3 = new HashMap<>(); resultRow1.put("ORDERTYPE", "small"); resultRow1.put("STATUS", null); @@ -467,8 +480,8 @@ public void testNullValuesInMultipleRowsValidation() { when(jdbcTemplate.queryForList(sql)).thenReturn(resultList); - List stmts = Collections.singletonList(sql); - executeSQLQueryAction.statements(stmts); + List statements = Collections.singletonList(sql); + executeSQLQueryAction.statements(statements); executeSQLQueryAction.validate("ORDERTYPE", "small", "medium", ""); // 1st possibility to validate null values executeSQLQueryAction.validate("STATUS", "NULL", "in_progress", "finished"); // 2nd possibility to validate null values executeSQLQueryAction.build().execute(context); @@ -484,10 +497,10 @@ public void testIgnoreInMultipleRowsValidation() { String sql = "select ORDERTYPE, STATUS from orders where ID < 5"; reset(jdbcTemplate); - List> resultList = new ArrayList>(); - Map resultRow1 = new HashMap(); - Map resultRow2 = new HashMap(); - Map resultRow3 = new HashMap(); + List> resultList = new ArrayList<>(); + Map resultRow1 = new HashMap<>(); + Map resultRow2 = new HashMap<>(); + Map resultRow3 = new HashMap<>(); resultRow1.put("ORDERTYPE", "small"); resultRow1.put("STATUS", "started"); @@ -501,8 +514,8 @@ public void testIgnoreInMultipleRowsValidation() { when(jdbcTemplate.queryForList(sql)).thenReturn(resultList); - List stmts = Collections.singletonList(sql); - executeSQLQueryAction.statements(stmts); + List statements = Collections.singletonList(sql); + executeSQLQueryAction.statements(statements); executeSQLQueryAction.validate("ORDERTYPE", "small", CitrusSettings.IGNORE_PLACEHOLDER, "big"); executeSQLQueryAction.validate("STATUS", CitrusSettings.IGNORE_PLACEHOLDER, "in_progress", "finished"); executeSQLQueryAction.build().execute(context); @@ -518,10 +531,10 @@ public void testExtractMultipleRowValues() { String sql = "select distinct STATUS from orders"; reset(jdbcTemplate); - List> resultList = new ArrayList>(); - Map resultRow1 = new HashMap(); - Map resultRow2 = new HashMap(); - Map resultRow3 = new HashMap(); + List> resultList = new ArrayList<>(); + Map resultRow1 = new HashMap<>(); + Map resultRow2 = new HashMap<>(); + Map resultRow3 = new HashMap<>(); resultRow1.put("ORDERTYPE", "small"); resultRow1.put("STATUS", "started"); @@ -535,8 +548,8 @@ public void testExtractMultipleRowValues() { when(jdbcTemplate.queryForList(sql)).thenReturn(resultList); - List stmts = Collections.singletonList(sql); - executeSQLQueryAction.statements(stmts); + List statements = Collections.singletonList(sql); + executeSQLQueryAction.statements(statements); executeSQLQueryAction.extract("STATUS", "orderStatus"); executeSQLQueryAction.extract("ORDERTYPE", "orderType"); executeSQLQueryAction.validate("ORDERTYPE", "small", CitrusSettings.IGNORE_PLACEHOLDER, "big"); @@ -559,23 +572,23 @@ public void testMultipleStatementsValidationError() { String sql2 = DB_STMT_2; reset(jdbcTemplate); - Map resultMap1 = new HashMap(); + Map resultMap1 = new HashMap<>(); resultMap1.put("ORDERTYPE", "small"); resultMap1.put("STATUS", "in_progress"); when(jdbcTemplate.queryForList(sql1)).thenReturn(Collections.singletonList(resultMap1)); - Map resultMap2 = new HashMap(); + Map resultMap2 = new HashMap<>(); resultMap2.put("NAME", "Mickey Mouse"); resultMap2.put("HEIGHT", "0,3"); when(jdbcTemplate.queryForList(sql2)).thenReturn(Collections.singletonList(resultMap2)); - List stmts = new ArrayList(); - stmts.add(sql1); - stmts.add(sql2); + List statements = new ArrayList(); + statements.add(sql1); + statements.add(sql2); - executeSQLQueryAction.statements(stmts); + executeSQLQueryAction.statements(statements); executeSQLQueryAction.validate("ORDERTYPE", "small"); executeSQLQueryAction.validate("STATUS", "in_progress"); executeSQLQueryAction.validate("NAME", "Donald Duck"); //this is supposed to cause an error @@ -600,13 +613,13 @@ public void testSQLStatementsWithFileResource() { String sql2 = "select NAME, HEIGHT\nfrom customers\nwhere ID=1"; reset(jdbcTemplate); - Map resultMap1 = new HashMap(); + Map resultMap1 = new HashMap<>(); resultMap1.put("ORDERTYPE", "small"); resultMap1.put("STATUS", "in_progress"); when(jdbcTemplate.queryForList(sql1)).thenReturn(Collections.singletonList(resultMap1)); - Map resultMap2 = new HashMap(); + Map resultMap2 = new HashMap<>(); resultMap2.put("NAME", "Mickey Mouse"); resultMap2.put("HEIGHT", "0,3"); @@ -630,14 +643,14 @@ public void testResultSetScriptValidation() { String sql = "select ORDERTYPES, STATUS from orders where ID=5"; reset(jdbcTemplate); - Map resultMap = new HashMap(); + Map resultMap = new HashMap<>(); resultMap.put("ORDERTYPE", "small"); resultMap.put("STATUS", "in_progress"); when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List stmts = Collections.singletonList(sql); - executeSQLQueryAction.statements(stmts); + List statements = Collections.singletonList(sql); + executeSQLQueryAction.statements(statements); String validationScript = "assert rows.size() == 1\n" + "assert rows[0].ORDERTYPE == 'small'\n" + @@ -654,18 +667,18 @@ public void testResultSetScriptValidation() { } @Test - public void testResultSetScriptValidationMultipleStmts() { + public void testResultSetScriptValidationMultiplestatements() { String sql1 = "select ORDERTYPES, STATUS from orders where ID=5"; String sql2 = "select ERRORTYPES from types"; reset(jdbcTemplate); - Map resultMap = new HashMap(); + Map resultMap = new HashMap<>(); resultMap.put("ORDERTYPE", "small"); resultMap.put("STATUS", "in_progress"); - List> results = new ArrayList>(); + List> results = new ArrayList<>(); for (int i = 1; i < 4; i++) { - Map columnMap = new HashMap(); + Map columnMap = new HashMap<>(); columnMap.put("ID", String.valueOf(i)); columnMap.put("NAME", "error" + i); @@ -675,10 +688,10 @@ public void testResultSetScriptValidationMultipleStmts() { when(jdbcTemplate.queryForList(sql1)).thenReturn(Collections.singletonList(resultMap)); when(jdbcTemplate.queryForList(sql2)).thenReturn(results); - List stmts = new ArrayList(); - stmts.add(sql1); - stmts.add(sql2); - executeSQLQueryAction.statements(stmts); + List statements = new ArrayList(); + statements.add(sql1); + statements.add(sql2); + executeSQLQueryAction.statements(statements); String validationScript = "assert rows.size() == 4\n" + "assert rows[0].ORDERTYPE == 'small'\n" + @@ -696,14 +709,14 @@ public void testResultSetScriptValidationWrongValue() { String sql = "select ORDERTYPES, STATUS from orders where ID=5"; reset(jdbcTemplate); - Map resultMap = new HashMap(); + Map resultMap = new HashMap<>(); resultMap.put("ORDERTYPE", "small"); resultMap.put("STATUS", "in_progress"); when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List stmts = Collections.singletonList(sql); - executeSQLQueryAction.statements(stmts); + List statements = Collections.singletonList(sql); + executeSQLQueryAction.statements(statements); String validationScript = "assert rows.size() == 1\n" + "assert rows[0] == [ORDERTYPE:'big', STATUS:'in_progress']"; @@ -727,14 +740,14 @@ public void testResultSetScriptValidationCombination() { String sql = "select ORDERTYPES, STATUS from orders where ID=5"; reset(jdbcTemplate); - Map resultMap = new HashMap(); + Map resultMap = new HashMap<>(); resultMap.put("ORDERTYPE", "small"); resultMap.put("STATUS", "in_progress"); when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List stmts = Collections.singletonList(sql); - executeSQLQueryAction.statements(stmts); + List statements = Collections.singletonList(sql); + executeSQLQueryAction.statements(statements); executeSQLQueryAction.validate("ORDERTYPE", "small"); executeSQLQueryAction.validate("STATUS", "in_progress"); @@ -752,14 +765,14 @@ public void testResultSetValidationWithVariableAndFunction() { String sql = DB_STMT_1; reset(jdbcTemplate); - Map resultMap = new HashMap(); + Map resultMap = new HashMap<>(); resultMap.put("ORDERTYPE", "testVariableValue"); resultMap.put("STATUS", "in_progress"); when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List stmts = Collections.singletonList(sql); - executeSQLQueryAction.statements(stmts); + List statements = Collections.singletonList(sql); + executeSQLQueryAction.statements(statements); executeSQLQueryAction.validate("ORDERTYPE", "${testVariable}"); executeSQLQueryAction.validate("STATUS", "citrus:concat('in_', ${progressVar})"); @@ -774,14 +787,14 @@ public void testBinaryBlobColumnValues() { String sql = "select ORDERTYPE, BINARY_DATA from orders where ID=5"; reset(jdbcTemplate); - Map resultMap = new HashMap(); + Map resultMap = new HashMap<>(); resultMap.put("ORDERTYPE", "small"); resultMap.put("BINARY_DATA", "some_binary_data".getBytes()); when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List stmts = Collections.singletonList(sql); - executeSQLQueryAction.statements(stmts); + List statements = Collections.singletonList(sql); + executeSQLQueryAction.statements(statements); executeSQLQueryAction.extract("BINARY_DATA", "binaryData"); executeSQLQueryAction.build().execute(context); @@ -789,4 +802,15 @@ public void testBinaryBlobColumnValues() { Assert.assertEquals(context.getVariable("${binaryData}"), Base64.encodeBase64String("some_binary_data".getBytes())); Assert.assertEquals(new String(Base64.decodeBase64(context.getVariable("${binaryData}"))), "some_binary_data"); } + + @Test + public void testNoJdbcTemplateConfigured() { + // Special ExecuteSQLQueryAction without a JdbcTemplate + executeSQLQueryAction = new ExecuteSQLQueryAction.Builder().jdbcTemplate(null); + executeSQLQueryAction.statements(Collections.singletonList("statement")); + + CitrusRuntimeException exception = Assert.expectThrows(CitrusRuntimeException.class, () -> executeSQLQueryAction.build().execute(context)); + + Assert.assertEquals(exception.getMessage(), "No JdbcTemplate configured for query execution!"); + } } diff --git a/connectors/citrus-sql/src/test/resources/org/citrusframework/actions/test-sql-query-statements.sql b/connectors/citrus-sql/src/test/resources/org/citrusframework/actions/test-sql-query-statements.sql index 3aaeae79bd..2a593f8bf2 100644 --- a/connectors/citrus-sql/src/test/resources/org/citrusframework/actions/test-sql-query-statements.sql +++ b/connectors/citrus-sql/src/test/resources/org/citrusframework/actions/test-sql-query-statements.sql @@ -1,8 +1,8 @@ --Test SQL statements --Query in one line: -select ORDERTYPE, STATUS from orders where ID=5; +select ORDERTYPE, STATUS from orders where ID = 5; --Query in multiple lines: select NAME, HEIGHT from customers -where ID=1; \ No newline at end of file +where ID=1;