diff --git a/connectors/citrus-sql/src/main/java/org/citrusframework/actions/ExecutePLSQLAction.java b/connectors/citrus-sql/src/main/java/org/citrusframework/actions/ExecutePLSQLAction.java index e0e651c164..ac52e11874 100644 --- a/connectors/citrus-sql/src/main/java/org/citrusframework/actions/ExecutePLSQLAction.java +++ b/connectors/citrus-sql/src/main/java/org/citrusframework/actions/ExecutePLSQLAction.java @@ -16,13 +16,6 @@ package org.citrusframework.actions; -import java.io.IOException; -import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.List; -import java.util.StringTokenizer; -import javax.sql.DataSource; - import org.citrusframework.context.TestContext; import org.citrusframework.exceptions.CitrusRuntimeException; import org.citrusframework.util.FileUtils; @@ -32,6 +25,13 @@ import org.springframework.transaction.support.TransactionTemplate; import org.springframework.util.StringUtils; +import javax.sql.DataSource; +import java.io.IOException; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; + /** * Class executes PLSQL statements either declared inline as PLSQL statements or given by an * external file resource. @@ -103,9 +103,13 @@ public String decorate(String line) { * @param context */ protected void executeStatements(List statements, TestContext context) { - for (String stmt : statements) { + if (getJdbcTemplate() == null) { + throw new CitrusRuntimeException("No JdbcTemplate configured for sql execution!"); + } + + for (String statement : statements) { try { - final String toExecute = context.replaceDynamicContentInString(stmt.trim()); + final String toExecute = context.replaceDynamicContentInString(statement.trim()); if (logger.isDebugEnabled()) { logger.debug("Executing PLSQL statement: " + toExecute); diff --git a/connectors/citrus-sql/src/main/java/org/citrusframework/actions/ExecuteSQLAction.java b/connectors/citrus-sql/src/main/java/org/citrusframework/actions/ExecuteSQLAction.java index dfb56162fd..170d1e0d6e 100644 --- a/connectors/citrus-sql/src/main/java/org/citrusframework/actions/ExecuteSQLAction.java +++ b/connectors/citrus-sql/src/main/java/org/citrusframework/actions/ExecuteSQLAction.java @@ -16,13 +16,13 @@ package org.citrusframework.actions; -import java.util.List; -import javax.sql.DataSource; - import org.citrusframework.context.TestContext; import org.citrusframework.exceptions.CitrusRuntimeException; import org.springframework.transaction.support.TransactionTemplate; +import javax.sql.DataSource; +import java.util.List; + /** * Test action execute SQL statements. Use this action when executing * database altering statements like UPDATE, INSERT, ALTER, DELETE. Statements are either @@ -80,14 +80,18 @@ public void doExecute(TestContext context) { * @param context */ protected void executeStatements(List statements, TestContext context) { - for (String stmt : statements) { + if (getJdbcTemplate() == null) { + throw new CitrusRuntimeException("No JdbcTemplate configured for sql execution!"); + } + + for (String statement : statements) { try { final String toExecute; - if (stmt.trim().endsWith(";")) { - toExecute = context.replaceDynamicContentInString(stmt.trim().substring(0, stmt.trim().length()-1)); + 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 (logger.isDebugEnabled()) { 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 9c71dad35b..e8810679fe 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 @@ -16,17 +16,6 @@ package org.citrusframework.actions; -import java.io.IOException; -import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import javax.sql.DataSource; - import org.apache.commons.codec.binary.Base64; import org.citrusframework.CitrusSettings; import org.citrusframework.context.TestContext; @@ -45,6 +34,17 @@ import org.springframework.transaction.support.TransactionTemplate; import org.springframework.util.CollectionUtils; +import javax.sql.DataSource; +import java.io.IOException; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + /** * Action executes SQL queries and offers result set validation. * @@ -123,12 +123,6 @@ public void doExecute(TestContext context) { // fill the request test context variables (extract tag) fillContextVariables(columnValuesMap, context); - - // legacy: save all columns as variables TODO: remove in major version upgrade - for (Entry> column : columnValuesMap.entrySet()) { - List columnValues = column.getValue(); - context.setVariable(column.getKey().toUpperCase(), columnValues.get(0) == null ? NULL_VALUE : columnValues.get(0)); - } } catch (DataAccessException e) { logger.error("Failed to execute SQL statement", e); throw new CitrusRuntimeException(e); diff --git a/connectors/citrus-sql/src/test/java/org/citrusframework/actions/ExecutePLSQLActionTest.java b/connectors/citrus-sql/src/test/java/org/citrusframework/actions/ExecutePLSQLActionTest.java index 34baf721bb..a674105e21 100644 --- a/connectors/citrus-sql/src/test/java/org/citrusframework/actions/ExecutePLSQLActionTest.java +++ b/connectors/citrus-sql/src/test/java/org/citrusframework/actions/ExecutePLSQLActionTest.java @@ -16,14 +16,20 @@ package org.citrusframework.actions; +import org.citrusframework.exceptions.CitrusRuntimeException; import org.citrusframework.testng.AbstractTestNGUnitTest; import org.mockito.Mockito; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.transaction.PlatformTransactionManager; +import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -import static org.mockito.Mockito.*; +import java.util.Collections; + +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; /** @@ -42,93 +48,93 @@ public void setUp() { .jdbcTemplate(jdbcTemplate); } - @Test - public void testPLSQLExecutionWithInlineScript() { - String stmt = "DECLARE " + + @Test + public void testPLSQLExecutionWithInlineScript() { + String stmt = "DECLARE " + "Zahl1 number(2);" + "Text varchar(20) := 'Hello World!';" + - "BEGIN" + + "BEGIN" + "EXECUTE IMMEDIATE \"" + - "select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"" + - "END;/"; + "select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"" + + "END;/"; - executePLSQLActionBuilder.sqlScript(stmt); + executePLSQLActionBuilder.sqlScript(stmt); - String controlStatement = "DECLARE " + + String controlStatement = "DECLARE " + "Zahl1 number(2);" + "Text varchar(20) := 'Hello World!';" + - "BEGIN" + + "BEGIN" + "EXECUTE IMMEDIATE \"" + - "select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"" + - "END;"; + "select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"" + + "END;"; - reset(jdbcTemplate); - executePLSQLActionBuilder.build().execute(context); - verify(jdbcTemplate).execute(controlStatement); - } + reset(jdbcTemplate); + executePLSQLActionBuilder.build().execute(context); + verify(jdbcTemplate).execute(controlStatement); + } - @Test - public void testPLSQLExecutionWithTransaction() { - String stmt = "DECLARE " + + @Test + public void testPLSQLExecutionWithTransaction() { + String stmt = "DECLARE " + "Zahl1 number(2);" + "Text varchar(20) := 'Hello World!';" + - "BEGIN" + + "BEGIN" + "EXECUTE IMMEDIATE \"" + - "select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"" + - "END;/"; + "select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"" + + "END;/"; - executePLSQLActionBuilder.transactionManager(transactionManager); - executePLSQLActionBuilder.sqlScript(stmt); + executePLSQLActionBuilder.transactionManager(transactionManager); + executePLSQLActionBuilder.sqlScript(stmt); - String controlStatement = "DECLARE " + + String controlStatement = "DECLARE " + "Zahl1 number(2);" + "Text varchar(20) := 'Hello World!';" + - "BEGIN" + + "BEGIN" + "EXECUTE IMMEDIATE \"" + - "select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"" + - "END;"; + "select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"" + + "END;"; - reset(jdbcTemplate, transactionManager); - executePLSQLActionBuilder.build().execute(context); - verify(jdbcTemplate).execute(controlStatement); - } + reset(jdbcTemplate, transactionManager); + executePLSQLActionBuilder.build().execute(context); + verify(jdbcTemplate).execute(controlStatement); + } - @Test + @Test public void testPLSQLExecutionWithInlineScriptNoEndingCharacter() { String stmt = "DECLARE " + "Zahl1 number(2);" + "Text varchar(20) := 'Hello World!';" + - "BEGIN" + + "BEGIN" + "EXECUTE IMMEDIATE \"" + - "select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"" + - "END;"; + "select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"" + + "END;"; executePLSQLActionBuilder.sqlScript(stmt); String controlStatement = "DECLARE " + "Zahl1 number(2);" + "Text varchar(20) := 'Hello World!';" + - "BEGIN" + + "BEGIN" + "EXECUTE IMMEDIATE \"" + - "select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"" + - "END;"; + "select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"" + + "END;"; reset(jdbcTemplate); executePLSQLActionBuilder.build().execute(context); verify(jdbcTemplate).execute(controlStatement); } - @Test + @Test public void testPLSQLExecutionWithFileResource() { executePLSQLActionBuilder.sqlResource("classpath:org/citrusframework/actions/test-plsql.sql"); String controlStatement = "DECLARE\n" + " Zahl1 number(2);\n" + " Text varchar(20) := 'Hello World!';\n" + - "BEGIN\n" + + "BEGIN\n" + " EXECUTE IMMEDIATE \"\n" + - " select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"\n" + - "END;"; + " select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"\n" + + "END;"; reset(jdbcTemplate); @@ -137,37 +143,37 @@ public void testPLSQLExecutionWithFileResource() { verify(jdbcTemplate).execute(controlStatement); } - @Test + @Test public void testPLSQLExecutionWithInlineScriptVariableSupport() { - context.setVariable("myText", "Hello World!"); - context.setVariable("tableName", "Greetings"); + context.setVariable("myText", "Hello World!"); + context.setVariable("tableName", "Greetings"); String stmt = "DECLARE " + "Zahl1 number(2);" + "Text varchar(20) := '${myText}';" + - "BEGIN" + + "BEGIN" + "EXECUTE IMMEDIATE \"" + - "select number_of_greetings into Zahl1 from ${tableName} where text='${myText}';\"" + - "END;/"; + "select number_of_greetings into Zahl1 from ${tableName} where text='${myText}';\"" + + "END;/"; executePLSQLActionBuilder.sqlScript(stmt); String controlStatement = "DECLARE " + "Zahl1 number(2);" + "Text varchar(20) := 'Hello World!';" + - "BEGIN" + + "BEGIN" + "EXECUTE IMMEDIATE \"" + - "select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"" + - "END;"; + "select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"" + + "END;"; reset(jdbcTemplate); executePLSQLActionBuilder.build().execute(context); verify(jdbcTemplate).execute(controlStatement); } - @Test + @Test public void testPLSQLExecutionWithFileResourceVariableSupport() { - context.setVariable("myText", "Hello World!"); + context.setVariable("myText", "Hello World!"); context.setVariable("tableName", "Greetings"); executePLSQLActionBuilder.sqlResource("classpath:org/citrusframework/actions/test-plsql-with-variables.sql"); @@ -175,64 +181,75 @@ public void testPLSQLExecutionWithFileResourceVariableSupport() { String controlStatement = "DECLARE\n" + " Zahl1 number(2);\n" + " Text varchar(20) := 'Hello World!';\n" + - "BEGIN\n" + + "BEGIN\n" + " EXECUTE IMMEDIATE \"\n" + - " select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"\n" + - "END;"; + " select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"\n" + + "END;"; reset(jdbcTemplate); executePLSQLActionBuilder.build().execute(context); verify(jdbcTemplate).execute(controlStatement); } - @Test + @Test public void testPLSQLExecutionWithMultipleInlineStatements() { String stmt = "DECLARE " + "Zahl1 number(2);" + "Text varchar(20) := 'Hello World!';" + - "BEGIN" + + "BEGIN" + "EXECUTE IMMEDIATE \"" + - "select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"" + - "END;" + - "/" + - "DECLARE " + + "select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"" + + "END;" + + "/" + + "DECLARE " + "Zahl1 number(2);" + "Text varchar(20) := 'Hello World!';" + - "BEGIN" + + "BEGIN" + "EXECUTE IMMEDIATE \"" + - "select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"" + - "END;" + - "/"; + "select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"" + + "END;" + + "/"; executePLSQLActionBuilder.sqlScript(stmt); String controlStatement = "DECLARE " + "Zahl1 number(2);" + "Text varchar(20) := 'Hello World!';" + - "BEGIN" + + "BEGIN" + "EXECUTE IMMEDIATE \"" + - "select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"" + - "END;"; + "select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"" + + "END;"; reset(jdbcTemplate); executePLSQLActionBuilder.build().execute(context); verify(jdbcTemplate, times(2)).execute(controlStatement); } - @Test + @Test public void testPLSQLExecutionWithFileResourceMultipleStmts() { executePLSQLActionBuilder.sqlResource("classpath:org/citrusframework/actions/test-plsql-multiple-stmts.sql"); String controlStatement = "DECLARE\n" + " Zahl1 number(2);\n" + " Text varchar(20) := 'Hello World!';\n" + - "BEGIN\n" + + "BEGIN\n" + " EXECUTE IMMEDIATE \"\n" + - " select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"\n" + - "END;"; + " select number_of_greetings into Zahl1 from Greetings where text='Hello World!';\"\n" + + "END;"; reset(jdbcTemplate); executePLSQLActionBuilder.build().execute(context); verify(jdbcTemplate, times(2)).execute(controlStatement); } + + @Test + public void testNoJdbcTemplateConfigured() { + // Special ExecuteSQLQueryAction without a JdbcTemplate + executePLSQLActionBuilder = new ExecutePLSQLAction.Builder().jdbcTemplate(null); + executePLSQLActionBuilder.statements(Collections.singletonList("statement")); + + CitrusRuntimeException exception = Assert.expectThrows(CitrusRuntimeException.class, () -> executePLSQLActionBuilder.build().execute(context)); + + Assert.assertEquals(exception.getMessage(), "No JdbcTemplate configured for sql execution!"); + } } diff --git a/connectors/citrus-sql/src/test/java/org/citrusframework/actions/ExecuteSQLActionTest.java b/connectors/citrus-sql/src/test/java/org/citrusframework/actions/ExecuteSQLActionTest.java index a44c811544..9c7d99b512 100644 --- a/connectors/citrus-sql/src/test/java/org/citrusframework/actions/ExecuteSQLActionTest.java +++ b/connectors/citrus-sql/src/test/java/org/citrusframework/actions/ExecuteSQLActionTest.java @@ -16,18 +16,20 @@ package org.citrusframework.actions; -import java.util.ArrayList; -import java.util.List; - import org.citrusframework.exceptions.CitrusRuntimeException; import org.citrusframework.testng.AbstractTestNGUnitTest; import org.mockito.Mockito; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.transaction.PlatformTransactionManager; +import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.reset; import static org.mockito.Mockito.verify; @@ -52,40 +54,40 @@ public void setUp() { .jdbcTemplate(jdbcTemplate); } - @Test - public void testSQLExecutionWithInlineStatements() { - List stmts = new ArrayList<>(); - stmts.add(DB_STMT_1); - stmts.add(DB_STMT_2); + @Test + public void testSQLExecutionWithInlineStatements() { + List stmts = new ArrayList<>(); + stmts.add(DB_STMT_1); + stmts.add(DB_STMT_2); - executeSQLActionBuilder.statements(stmts); + executeSQLActionBuilder.statements(stmts); - reset(jdbcTemplate); + reset(jdbcTemplate); - executeSQLActionBuilder.build().execute(context); + executeSQLActionBuilder.build().execute(context); - verify(jdbcTemplate).execute(DB_STMT_1); - verify(jdbcTemplate).execute(DB_STMT_2); - } + verify(jdbcTemplate).execute(DB_STMT_1); + verify(jdbcTemplate).execute(DB_STMT_2); + } - @Test - public void testSQLExecutionWithTransactions() { - List stmts = new ArrayList<>(); - stmts.add(DB_STMT_1); - stmts.add(DB_STMT_2); + @Test + public void testSQLExecutionWithTransactions() { + List stmts = new ArrayList<>(); + stmts.add(DB_STMT_1); + stmts.add(DB_STMT_2); - executeSQLActionBuilder.statements(stmts); - executeSQLActionBuilder.transactionManager(transactionManager); + executeSQLActionBuilder.statements(stmts); + executeSQLActionBuilder.transactionManager(transactionManager); - reset(jdbcTemplate, transactionManager); + reset(jdbcTemplate, transactionManager); executeSQLActionBuilder.build().execute(context); - verify(jdbcTemplate).execute(DB_STMT_1); - verify(jdbcTemplate).execute(DB_STMT_2); - } + verify(jdbcTemplate).execute(DB_STMT_1); + verify(jdbcTemplate).execute(DB_STMT_2); + } - @Test + @Test public void testSQLExecutionWithFileResource() { executeSQLActionBuilder.sqlResource("classpath:org/citrusframework/actions/test-sql-statements.sql"); @@ -97,12 +99,12 @@ public void testSQLExecutionWithFileResource() { verify(jdbcTemplate).execute(DB_STMT_2); } - @Test + @Test public void testSQLExecutionWithInlineScriptVariableSupport() { - context.setVariable("resolvedStatus", "resolved"); - context.setVariable("version", "1"); + context.setVariable("resolvedStatus", "resolved"); + context.setVariable("version", "1"); - List stmts = new ArrayList<>(); + List stmts = new ArrayList<>(); stmts.add("DELETE * FROM ERRORS WHERE STATUS='${resolvedStatus}'"); stmts.add("DELETE * FROM CONFIGURATION WHERE VERSION=${version}"); @@ -116,9 +118,9 @@ public void testSQLExecutionWithInlineScriptVariableSupport() { verify(jdbcTemplate).execute(DB_STMT_2); } - @Test + @Test public void testSQLExecutionWithFileResourceVariableSupport() { - context.setVariable("resolvedStatus", "resolved"); + context.setVariable("resolvedStatus", "resolved"); context.setVariable("version", "1"); executeSQLActionBuilder.sqlResource("classpath:org/citrusframework/actions/test-sql-with-variables.sql"); @@ -132,7 +134,6 @@ public void testSQLExecutionWithFileResourceVariableSupport() { } @Test - @SuppressWarnings("serial") public void testSQLExecutionIgnoreErrors() { List stmts = new ArrayList<>(); stmts.add(DB_STMT_1); @@ -143,14 +144,14 @@ public void testSQLExecutionIgnoreErrors() { reset(jdbcTemplate); - doThrow(new DataAccessException("Something went wrong!") {}).when(jdbcTemplate).execute(DB_STMT_2); + doThrow(new DataAccessException("Something went wrong!") { + }).when(jdbcTemplate).execute(DB_STMT_2); executeSQLActionBuilder.build().execute(context); verify(jdbcTemplate).execute(DB_STMT_1); } @Test(expectedExceptions = CitrusRuntimeException.class) - @SuppressWarnings("serial") public void testSQLExecutionErrorForwarding() { List stmts = new ArrayList<>(); stmts.add(DB_STMT_1); @@ -161,9 +162,21 @@ public void testSQLExecutionErrorForwarding() { reset(jdbcTemplate); - doThrow(new DataAccessException("Something went wrong!") {}).when(jdbcTemplate).execute(DB_STMT_2); + doThrow(new DataAccessException("Something went wrong!") { + }).when(jdbcTemplate).execute(DB_STMT_2); executeSQLActionBuilder.build().execute(context); verify(jdbcTemplate).execute(DB_STMT_1); } + + @Test + public void testNoJdbcTemplateConfigured() { + // Special ExecuteSQLQueryAction without a JdbcTemplate + executeSQLActionBuilder = new ExecuteSQLAction.Builder().jdbcTemplate(null); + executeSQLActionBuilder.statements(Collections.singletonList("statement")); + + CitrusRuntimeException exception = Assert.expectThrows(CitrusRuntimeException.class, () -> executeSQLActionBuilder.build().execute(context)); + + Assert.assertEquals(exception.getMessage(), "No JdbcTemplate configured for sql execution!"); + } } 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 fbc34288b3..9f9c8d3f56 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 @@ -16,12 +16,7 @@ package org.citrusframework.actions; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - +import org.apache.commons.codec.binary.Base64; import org.citrusframework.CitrusSettings; import org.citrusframework.UnitTestSupport; import org.citrusframework.context.TestContextFactory; @@ -30,17 +25,26 @@ import org.citrusframework.script.ScriptTypes; import org.citrusframework.validation.script.ScriptValidationContext; import org.citrusframework.validation.script.sql.SqlResultSetScriptValidator; -import org.apache.commons.codec.binary.Base64; import org.mockito.Mockito; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.TransactionStatus; +import org.springframework.transaction.support.TransactionTemplate; import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.reset; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -69,52 +73,57 @@ protected TestContextFactory createTestContextFactory() { @BeforeMethod public void setUp() { - executeSQLQueryAction = new ExecuteSQLQueryAction.Builder() - .jdbcTemplate(jdbcTemplate); + executeSQLQueryAction = new ExecuteSQLQueryAction.Builder() + .jdbcTemplate(jdbcTemplate); } - @Test - public void testSQLStatement() { - String sql = DB_STMT_1; - reset(jdbcTemplate); + @Test + public void testSQLStatement() { + String sql = DB_STMT_1; + reset(jdbcTemplate); - Map resultMap = new HashMap<>(); - resultMap.put("ORDERTYPE", "small"); - resultMap.put("STATUS", "in_progress"); + Map resultMap = new HashMap<>(); + resultMap.put("ORDERTYPE", "small"); + resultMap.put("STATUS", "in_progress"); - when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); + when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List statements = Collections.singletonList(sql); - executeSQLQueryAction.statements(statements); - executeSQLQueryAction.build().execute(context); + executeSQLQueryAction.statements(Collections.singletonList(sql)); + executeSQLQueryAction.extract("ORDERTYPE", "orderType"); + executeSQLQueryAction.extract("STATUS", "status"); + executeSQLQueryAction.build().execute(context); - Assert.assertNotNull(context.getVariable("${ORDERTYPE}")); - Assert.assertEquals(context.getVariable("${ORDERTYPE}"), "small"); - Assert.assertNotNull(context.getVariable("${STATUS}")); - Assert.assertEquals(context.getVariable("${STATUS}"), "in_progress"); - } + Assert.assertNotNull(context.getVariable("${orderType}")); + Assert.assertEquals(context.getVariable("${orderType}"), "small"); + Assert.assertNotNull(context.getVariable("${status}")); + Assert.assertEquals(context.getVariable("${status}"), "in_progress"); + } - @Test - public void testSQLStatementWithTransaction() { - String sql = DB_STMT_1; - reset(jdbcTemplate, transactionManager); + @Test + public void testSQLStatementWithTransaction() { + reset(jdbcTemplate, transactionManager); + TransactionStatus transactionStatusMock = mock(TransactionStatus.class); + doReturn(transactionStatusMock).when(transactionManager).getTransaction(any(TransactionTemplate.class)); - Map resultMap = new HashMap<>(); - resultMap.put("ORDERTYPE", "small"); - resultMap.put("STATUS", "in_progress"); + Map resultMap = new HashMap<>(); + resultMap.put("ORDERTYPE", "small"); + resultMap.put("STATUS", "in_progress"); - when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); + when(jdbcTemplate.queryForList(DB_STMT_1)).thenReturn(Collections.singletonList(resultMap)); - List statements = Collections.singletonList(sql); - executeSQLQueryAction.statements(statements); - executeSQLQueryAction.transactionManager(transactionManager); + executeSQLQueryAction.statements(Collections.singletonList(DB_STMT_1)); + executeSQLQueryAction.transactionManager(transactionManager); + executeSQLQueryAction.extract("ORDERTYPE", "orderType"); + executeSQLQueryAction.extract("STATUS", "status"); executeSQLQueryAction.build().execute(context); - Assert.assertNotNull(context.getVariable("${ORDERTYPE}")); - Assert.assertEquals(context.getVariable("${ORDERTYPE}"), "small"); - Assert.assertNotNull(context.getVariable("${STATUS}")); - Assert.assertEquals(context.getVariable("${STATUS}"), "in_progress"); - } + verify(transactionManager).commit(transactionStatusMock); + + Assert.assertNotNull(context.getVariable("${orderType}")); + Assert.assertEquals(context.getVariable("${orderType}"), "small"); + Assert.assertNotNull(context.getVariable("${status}")); + Assert.assertEquals(context.getVariable("${status}"), "in_progress"); + } @Test public void testSQLStatementLowerCaseColumnNames() { @@ -127,17 +136,18 @@ public void testSQLStatementLowerCaseColumnNames() { when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List statements = Collections.singletonList(sql); - executeSQLQueryAction.statements(statements); + executeSQLQueryAction.statements(Collections.singletonList(sql)); + executeSQLQueryAction.extract("ORDERTYPE", "orderType"); + executeSQLQueryAction.extract("STATUS", "status"); executeSQLQueryAction.build().execute(context); - Assert.assertNotNull(context.getVariable("${ORDERTYPE}")); - Assert.assertEquals(context.getVariable("${ORDERTYPE}"), "small"); - Assert.assertNotNull(context.getVariable("${STATUS}")); - Assert.assertEquals(context.getVariable("${STATUS}"), "in_progress"); + Assert.assertNotNull(context.getVariable("${orderType}")); + Assert.assertEquals(context.getVariable("${orderType}"), "small"); + Assert.assertNotNull(context.getVariable("${status}")); + Assert.assertEquals(context.getVariable("${status}"), "in_progress"); } - @Test + @Test public void testSQLMultipleStatements() { String sql1 = DB_STMT_1; String sql2 = DB_STMT_2; @@ -168,54 +178,29 @@ public void testSQLMultipleStatements() { statements.add(sql3); executeSQLQueryAction.statements(statements); + executeSQLQueryAction.extract("ORDERTYPE", "orderType"); + executeSQLQueryAction.extract("STATUS", "status"); + executeSQLQueryAction.extract("NAME", "name"); + executeSQLQueryAction.extract("HEIGHT", "height"); + executeSQLQueryAction.extract("ID", "id"); + executeSQLQueryAction.extract("FRAMEWORK_NAME", "frameworkName"); executeSQLQueryAction.build().execute(context); - Assert.assertNotNull(context.getVariable("${ORDERTYPE}")); - Assert.assertEquals(context.getVariable("${ORDERTYPE}"), "small"); - Assert.assertNotNull(context.getVariable("${STATUS}")); - Assert.assertEquals(context.getVariable("${STATUS}"), "in_progress"); - Assert.assertNotNull(context.getVariable("${NAME}")); - 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 - public void testSQLResource() { - String sql1 = "SELECT ORDERTYPE, STATUS FROM orders WHERE ID=5"; - String sql2 = "SELECT NAME, HEIGHT FROM customers WHERE ID=1"; - reset(jdbcTemplate); - - 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<>(); - resultMap2.put("NAME", "Mickey Mouse"); - resultMap2.put("HEIGHT", "0,3"); - - when(jdbcTemplate.queryForList(sql2)).thenReturn(Collections.singletonList(resultMap2)); - - executeSQLQueryAction.sqlResource("classpath:org/citrusframework/actions/test-query.sql"); - executeSQLQueryAction.build().execute(context); - - Assert.assertNotNull(context.getVariable("${ORDERTYPE}")); - Assert.assertEquals(context.getVariable("${ORDERTYPE}"), "small"); - Assert.assertNotNull(context.getVariable("${STATUS}")); - Assert.assertEquals(context.getVariable("${STATUS}"), "in_progress"); - Assert.assertNotNull(context.getVariable("${NAME}")); - Assert.assertEquals(context.getVariable("${NAME}"), "Mickey Mouse"); - Assert.assertNotNull(context.getVariable("${HEIGHT}")); - Assert.assertEquals(context.getVariable("${HEIGHT}"), "0,3"); + Assert.assertNotNull(context.getVariable("${orderType}")); + Assert.assertEquals(context.getVariable("${orderType}"), "small"); + Assert.assertNotNull(context.getVariable("${status}")); + Assert.assertEquals(context.getVariable("${status}"), "in_progress"); + Assert.assertNotNull(context.getVariable("${name}")); + 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("${frameworkName}")); + Assert.assertEquals(context.getVariable("${frameworkName}"), "citrusframework/citrus"); } - @Test + @Test public void testNullValue() { String sql = DB_STMT_1; reset(jdbcTemplate); @@ -226,14 +211,8 @@ public void testNullValue() { when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List statements = Collections.singletonList(sql); - executeSQLQueryAction.statements(statements); + executeSQLQueryAction.statements(Collections.singletonList(sql)); executeSQLQueryAction.build().execute(context); - - Assert.assertNotNull(context.getVariable("${ORDERTYPE}")); - Assert.assertEquals(context.getVariable("${ORDERTYPE}"), "small"); - Assert.assertNotNull(context.getVariable("${STATUS}")); - Assert.assertEquals(context.getVariable("${STATUS}"), "NULL"); } @Test @@ -249,38 +228,8 @@ public void testVariableSupport() { when(jdbcTemplate.queryForList(DB_STMT_1)).thenReturn(Collections.singletonList(resultMap)); - List statements = Collections.singletonList(sql); - executeSQLQueryAction.statements(statements); - executeSQLQueryAction.build().execute(context); - - Assert.assertNotNull(context.getVariable("${ORDERTYPE}")); - Assert.assertEquals(context.getVariable("${ORDERTYPE}"), "small"); - Assert.assertNotNull(context.getVariable("${STATUS}")); - Assert.assertEquals(context.getVariable("${STATUS}"), "in_progress"); - } - - @Test - public void testExtractToVariables() { - String sql = DB_STMT_1; - reset(jdbcTemplate); - - Map resultMap = new HashMap<>(); - resultMap.put("ORDERTYPE", "small"); - resultMap.put("STATUS", "in_progress"); - - when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - - List statements = Collections.singletonList(sql); - executeSQLQueryAction.statements(statements); - executeSQLQueryAction.extract("STATUS", "orderStatus"); + executeSQLQueryAction.statements(Collections.singletonList(sql)); executeSQLQueryAction.build().execute(context); - - Assert.assertNotNull(context.getVariable("${orderStatus}")); - Assert.assertEquals(context.getVariable("${orderStatus}"), "in_progress"); - Assert.assertNotNull(context.getVariable("${ORDERTYPE}")); - Assert.assertEquals(context.getVariable("${ORDERTYPE}"), "small"); - Assert.assertNotNull(context.getVariable("${STATUS}")); - Assert.assertEquals(context.getVariable("${STATUS}"), "in_progress"); } @Test @@ -294,23 +243,18 @@ public void testExtractToVariablesLowerCaseColumnNames() { when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List statements = Collections.singletonList(sql); - executeSQLQueryAction.statements(statements); + executeSQLQueryAction.statements(Collections.singletonList(sql)); executeSQLQueryAction.extract("ordertype", "orderType"); executeSQLQueryAction.extract("STATUS", "orderStatus"); executeSQLQueryAction.build().execute(context); Assert.assertNotNull(context.getVariable("${orderStatus}")); Assert.assertEquals(context.getVariable("${orderStatus}"), "in_progress"); - Assert.assertNotNull(context.getVariable("${ORDERTYPE}")); - Assert.assertEquals(context.getVariable("${ORDERTYPE}"), "small"); Assert.assertNotNull(context.getVariable("${orderType}")); Assert.assertEquals(context.getVariable("${orderType}"), "small"); - Assert.assertNotNull(context.getVariable("${STATUS}")); - Assert.assertEquals(context.getVariable("${STATUS}"), "in_progress"); } - @Test(expectedExceptions = {CitrusRuntimeException.class}) + @Test(expectedExceptions = {CitrusRuntimeException.class}) public void testExtractToVariablesUnknownColumnMapping() { String sql = DB_STMT_1; reset(jdbcTemplate); @@ -321,13 +265,12 @@ public void testExtractToVariablesUnknownColumnMapping() { when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List statements = Collections.singletonList(sql); - executeSQLQueryAction.statements(statements); + executeSQLQueryAction.statements(Collections.singletonList(sql)); executeSQLQueryAction.extract("UNKNOWN_COLUMN", "orderStatus"); executeSQLQueryAction.build().execute(context); } - @Test + @Test public void testResultSetValidation() { String sql = DB_STMT_1; reset(jdbcTemplate); @@ -338,16 +281,10 @@ public void testResultSetValidation() { when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List statements = Collections.singletonList(sql); - executeSQLQueryAction.statements(statements); + executeSQLQueryAction.statements(Collections.singletonList(sql)); executeSQLQueryAction.validate("ORDERTYPE", "small"); executeSQLQueryAction.validate("STATUS", "in_progress"); executeSQLQueryAction.build().execute(context); - - Assert.assertNotNull(context.getVariable("${ORDERTYPE}")); - Assert.assertEquals(context.getVariable("${ORDERTYPE}"), "small"); - Assert.assertNotNull(context.getVariable("${STATUS}")); - Assert.assertEquals(context.getVariable("${STATUS}"), "in_progress"); } @Test @@ -361,19 +298,13 @@ public void testResultSetValidationLowerCase() { when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List statements = Collections.singletonList(sql); - executeSQLQueryAction.statements(statements); + executeSQLQueryAction.statements(Collections.singletonList(sql)); executeSQLQueryAction.validate("ORDERTYPE", "small"); executeSQLQueryAction.validate("STATUS", "in_progress"); executeSQLQueryAction.build().execute(context); - - Assert.assertNotNull(context.getVariable("${ORDERTYPE}")); - Assert.assertEquals(context.getVariable("${ORDERTYPE}"), "small"); - Assert.assertNotNull(context.getVariable("${STATUS}")); - Assert.assertEquals(context.getVariable("${STATUS}"), "in_progress"); } - @Test + @Test public void testResultSetValidationWithAliasNames() { String sql = "select ORDERTYPE AS TYPE, STATUS AS STATE from orders where ID=5"; reset(jdbcTemplate); @@ -384,19 +315,13 @@ public void testResultSetValidationWithAliasNames() { when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List statements = Collections.singletonList(sql); - executeSQLQueryAction.statements(statements); + executeSQLQueryAction.statements(Collections.singletonList(sql)); executeSQLQueryAction.validate("TYPE", "small"); executeSQLQueryAction.validate("STATE", "in_progress"); executeSQLQueryAction.build().execute(context); - - Assert.assertNotNull(context.getVariable("${TYPE}")); - Assert.assertEquals(context.getVariable("${TYPE}"), "small"); - Assert.assertNotNull(context.getVariable("${STATE}")); - Assert.assertEquals(context.getVariable("${STATE}"), "in_progress"); } - @Test + @Test public void testResultSetValidationError() { String sql = DB_STMT_1; reset(jdbcTemplate); @@ -407,8 +332,7 @@ public void testResultSetValidationError() { when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List statements = Collections.singletonList(sql); - executeSQLQueryAction.statements(statements); + executeSQLQueryAction.statements(Collections.singletonList(sql)); executeSQLQueryAction.validate("ORDERTYPE", "xxl"); //this is supposed to cause an error executeSQLQueryAction.validate("STATUS", "in_progress"); @@ -446,16 +370,10 @@ public void testResultSetMultipleRowsValidation() { when(jdbcTemplate.queryForList(sql)).thenReturn(resultList); - List statements = Collections.singletonList(sql); - executeSQLQueryAction.statements(statements); + executeSQLQueryAction.statements(Collections.singletonList(sql)); executeSQLQueryAction.validate("ORDERTYPE", "small", "medium", "big"); executeSQLQueryAction.validate("STATUS", "started", "in_progress", "finished"); executeSQLQueryAction.build().execute(context); - - Assert.assertNotNull(context.getVariable("ORDERTYPE")); - Assert.assertEquals(context.getVariable("ORDERTYPE"), "small"); - Assert.assertNotNull(context.getVariable("STATUS")); - Assert.assertEquals(context.getVariable("STATUS"), "started"); } @Test @@ -480,16 +398,10 @@ public void testNullValuesInMultipleRowsValidation() { when(jdbcTemplate.queryForList(sql)).thenReturn(resultList); - List statements = Collections.singletonList(sql); - executeSQLQueryAction.statements(statements); + executeSQLQueryAction.statements(Collections.singletonList(sql)); 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); - - Assert.assertNotNull(context.getVariable("ORDERTYPE")); - Assert.assertEquals(context.getVariable("ORDERTYPE"), "small"); - Assert.assertNotNull(context.getVariable("STATUS")); - Assert.assertEquals(context.getVariable("STATUS"), "NULL"); } @Test @@ -514,16 +426,10 @@ public void testIgnoreInMultipleRowsValidation() { when(jdbcTemplate.queryForList(sql)).thenReturn(resultList); - List statements = Collections.singletonList(sql); - executeSQLQueryAction.statements(statements); + executeSQLQueryAction.statements(Collections.singletonList(sql)); executeSQLQueryAction.validate("ORDERTYPE", "small", CitrusSettings.IGNORE_PLACEHOLDER, "big"); executeSQLQueryAction.validate("STATUS", CitrusSettings.IGNORE_PLACEHOLDER, "in_progress", "finished"); executeSQLQueryAction.build().execute(context); - - Assert.assertNotNull(context.getVariable("ORDERTYPE")); - Assert.assertEquals(context.getVariable("ORDERTYPE"), "small"); - Assert.assertNotNull(context.getVariable("STATUS")); - Assert.assertEquals(context.getVariable("STATUS"), "started"); } @Test @@ -548,8 +454,7 @@ public void testExtractMultipleRowValues() { when(jdbcTemplate.queryForList(sql)).thenReturn(resultList); - List statements = Collections.singletonList(sql); - executeSQLQueryAction.statements(statements); + executeSQLQueryAction.statements(Collections.singletonList(sql)); executeSQLQueryAction.extract("STATUS", "orderStatus"); executeSQLQueryAction.extract("ORDERTYPE", "orderType"); executeSQLQueryAction.validate("ORDERTYPE", "small", CitrusSettings.IGNORE_PLACEHOLDER, "big"); @@ -560,10 +465,6 @@ public void testExtractMultipleRowValues() { Assert.assertEquals(context.getVariable("orderType"), "small;NULL;big"); Assert.assertNotNull(context.getVariable("orderStatus")); Assert.assertEquals(context.getVariable("orderStatus"), "started;in_progress;finished"); - Assert.assertNotNull(context.getVariable("ORDERTYPE")); - Assert.assertEquals(context.getVariable("ORDERTYPE"), "small"); - Assert.assertNotNull(context.getVariable("STATUS")); - Assert.assertEquals(context.getVariable("STATUS"), "started"); } @Test @@ -604,6 +505,7 @@ public void testMultipleStatementsValidationError() { return; } + Assert.fail("Expected test to fail with " + ValidationException.class + " but was successful"); } @@ -626,16 +528,20 @@ public void testSQLStatementsWithFileResource() { when(jdbcTemplate.queryForList(sql2)).thenReturn(Collections.singletonList(resultMap2)); executeSQLQueryAction.sqlResource("classpath:org/citrusframework/actions/test-sql-query-statements.sql"); + executeSQLQueryAction.extract("ORDERTYPE", "orderType"); + executeSQLQueryAction.extract("STATUS", "status"); + executeSQLQueryAction.extract("NAME", "name"); + executeSQLQueryAction.extract("HEIGHT", "height"); executeSQLQueryAction.build().execute(context); - Assert.assertNotNull(context.getVariable("${ORDERTYPE}")); - Assert.assertEquals(context.getVariable("${ORDERTYPE}"), "small"); - Assert.assertNotNull(context.getVariable("${STATUS}")); - Assert.assertEquals(context.getVariable("${STATUS}"), "in_progress"); - Assert.assertNotNull(context.getVariable("${NAME}")); - Assert.assertEquals(context.getVariable("${NAME}"), "Mickey Mouse"); - Assert.assertNotNull(context.getVariable("${HEIGHT}")); - Assert.assertEquals(context.getVariable("${HEIGHT}"), "0,3"); + Assert.assertNotNull(context.getVariable("${orderType}")); + Assert.assertEquals(context.getVariable("${orderType}"), "small"); + Assert.assertNotNull(context.getVariable("${status}")); + Assert.assertEquals(context.getVariable("${status}"), "in_progress"); + Assert.assertNotNull(context.getVariable("${name}")); + Assert.assertEquals(context.getVariable("${name}"), "Mickey Mouse"); + Assert.assertNotNull(context.getVariable("${height}")); + Assert.assertEquals(context.getVariable("${height}"), "0,3"); } @Test @@ -649,8 +555,7 @@ public void testResultSetScriptValidation() { when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List statements = Collections.singletonList(sql); - executeSQLQueryAction.statements(statements); + executeSQLQueryAction.statements(Collections.singletonList(sql)); String validationScript = "assert rows.size() == 1\n" + "assert rows[0].ORDERTYPE == 'small'\n" + @@ -658,16 +563,11 @@ public void testResultSetScriptValidation() { executeSQLQueryAction.validateScript(validationScript, ScriptTypes.GROOVY); executeSQLQueryAction.build().execute(context); - Assert.assertNotNull(context.getVariable("${ORDERTYPE}")); - Assert.assertEquals(context.getVariable("${ORDERTYPE}"), "small"); - Assert.assertNotNull(context.getVariable("${STATUS}")); - Assert.assertEquals(context.getVariable("${STATUS}"), "in_progress"); - verify(resultSetScriptValidator).validateSqlResultSet(any(List.class), any(ScriptValidationContext.class), eq(context)); } @Test - public void testResultSetScriptValidationMultiplestatements() { + public void testResultSetScriptValidationMultipleStatements() { String sql1 = "select ORDERTYPES, STATUS from orders where ID=5"; String sql2 = "select ERRORTYPES from types"; reset(jdbcTemplate); @@ -715,8 +615,7 @@ public void testResultSetScriptValidationWrongValue() { when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List statements = Collections.singletonList(sql); - executeSQLQueryAction.statements(statements); + executeSQLQueryAction.statements(Collections.singletonList(sql)); String validationScript = "assert rows.size() == 1\n" + "assert rows[0] == [ORDERTYPE:'big', STATUS:'in_progress']"; @@ -746,8 +645,7 @@ public void testResultSetScriptValidationCombination() { when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List statements = Collections.singletonList(sql); - executeSQLQueryAction.statements(statements); + executeSQLQueryAction.statements(Collections.singletonList(sql)); executeSQLQueryAction.validate("ORDERTYPE", "small"); executeSQLQueryAction.validate("STATUS", "in_progress"); @@ -771,8 +669,7 @@ public void testResultSetValidationWithVariableAndFunction() { when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List statements = Collections.singletonList(sql); - executeSQLQueryAction.statements(statements); + executeSQLQueryAction.statements(Collections.singletonList(sql)); executeSQLQueryAction.validate("ORDERTYPE", "${testVariable}"); executeSQLQueryAction.validate("STATUS", "citrus:concat('in_', ${progressVar})"); @@ -793,8 +690,7 @@ public void testBinaryBlobColumnValues() { when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap)); - List statements = Collections.singletonList(sql); - executeSQLQueryAction.statements(statements); + executeSQLQueryAction.statements(Collections.singletonList(sql)); executeSQLQueryAction.extract("BINARY_DATA", "binaryData"); executeSQLQueryAction.build().execute(context); @@ -806,7 +702,7 @@ public void testBinaryBlobColumnValues() { @Test public void testNoJdbcTemplateConfigured() { // Special ExecuteSQLQueryAction without a JdbcTemplate - executeSQLQueryAction = new ExecuteSQLQueryAction.Builder().jdbcTemplate(null); + executeSQLQueryAction = new ExecuteSQLQueryAction.Builder().jdbcTemplate(null); executeSQLQueryAction.statements(Collections.singletonList("statement")); CitrusRuntimeException exception = Assert.expectThrows(CitrusRuntimeException.class, () -> executeSQLQueryAction.build().execute(context)); diff --git a/connectors/citrus-sql/src/test/java/org/citrusframework/actions/dsl/ExecuteSQLQueryTestActionBuilderTest.java b/connectors/citrus-sql/src/test/java/org/citrusframework/actions/dsl/ExecuteSQLQueryTestActionBuilderTest.java index 8ea7715067..f915a03726 100644 --- a/connectors/citrus-sql/src/test/java/org/citrusframework/actions/dsl/ExecuteSQLQueryTestActionBuilderTest.java +++ b/connectors/citrus-sql/src/test/java/org/citrusframework/actions/dsl/ExecuteSQLQueryTestActionBuilderTest.java @@ -16,26 +16,23 @@ package org.citrusframework.actions.dsl; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Set; - import org.citrusframework.DefaultTestCaseRunner; import org.citrusframework.TestCase; import org.citrusframework.UnitTestSupport; import org.citrusframework.actions.ExecuteSQLQueryAction; -import org.citrusframework.validation.script.sql.SqlResultSetScriptValidator; import org.mockito.Mockito; import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.Resource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.transaction.PlatformTransactionManager; import org.testng.Assert; import org.testng.annotations.Test; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; + import static org.citrusframework.actions.ExecuteSQLQueryAction.Builder.query; import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.reset; @@ -49,19 +46,16 @@ public class ExecuteSQLQueryTestActionBuilderTest extends UnitTestSupport { private final JdbcTemplate jdbcTemplate = Mockito.mock(JdbcTemplate.class); private final PlatformTransactionManager transactionManager = Mockito.mock(PlatformTransactionManager.class); - private final Resource resource = Mockito.mock(Resource.class); - - private final SqlResultSetScriptValidator validator = Mockito.mock(SqlResultSetScriptValidator.class); @Test - public void testExecuteSQLQueryWithResource() throws IOException { + public void testExecuteSQLQueryWithResource() { List> results = new ArrayList<>(); - results.add(Collections.singletonMap("NAME", "Leonard")); + results.add(Collections.singletonMap("NAME", "Leonard")); reset(jdbcTemplate); when(jdbcTemplate.queryForList(anyString())).thenReturn(results) - .thenReturn(Collections.singletonList(Collections.singletonMap("CNT_EPISODES", "100000"))); + .thenReturn(Collections.singletonList(Collections.singletonMap("CNT_EPISODES", "100000"))); DefaultTestCaseRunner builder = new DefaultTestCaseRunner(context); builder.variable("episodeId", "citrus:randomNumber(5)"); @@ -69,28 +63,28 @@ public void testExecuteSQLQueryWithResource() throws IOException { .sqlResource(new ClassPathResource("org/citrusframework/actions/dsl/query-script.sql")) .validate("NAME", "Leonard") .validate("CNT_EPISODES", "100000") - .extract("NAME", "actorName")); + .extract("NAME", "actorName") + .extract("CNT_EPISODES", "episodesCount")); - Assert.assertNotNull(context.getVariable("NAME")); Assert.assertNotNull(context.getVariable("actorName")); - Assert.assertNotNull(context.getVariable("CNT_EPISODES")); - Assert.assertEquals(context.getVariable("NAME"), "Leonard"); + Assert.assertNotNull(context.getVariable("episodesCount")); Assert.assertEquals(context.getVariable("actorName"), "Leonard"); - Assert.assertEquals(context.getVariable("CNT_EPISODES"), "100000"); + Assert.assertEquals(context.getVariable("episodesCount"), "100000"); TestCase test = builder.getTestCase(); Assert.assertEquals(test.getActionCount(), 1); Assert.assertEquals(test.getActions().get(0).getClass(), ExecuteSQLQueryAction.class); - ExecuteSQLQueryAction action = (ExecuteSQLQueryAction)test.getActions().get(0); + ExecuteSQLQueryAction action = (ExecuteSQLQueryAction) test.getActions().get(0); Assert.assertEquals(action.getName(), "sql-query"); Assert.assertEquals(action.getControlResultSet().size(), 2); Set>> rows = action.getControlResultSet().entrySet(); Assert.assertEquals(getRow("NAME", rows).toString(), "NAME=[Leonard]"); Assert.assertEquals(getRow("CNT_EPISODES", rows).toString(), "CNT_EPISODES=[100000]"); - Assert.assertEquals(action.getExtractVariables().size(), 1); - Assert.assertEquals(action.getExtractVariables().entrySet().iterator().next().toString(), "NAME=actorName"); + Assert.assertEquals(action.getExtractVariables().size(), 2); + Assert.assertEquals(action.getExtractVariables().get("NAME"), "actorName"); + Assert.assertEquals(action.getExtractVariables().get("CNT_EPISODES"), "episodesCount"); Assert.assertNull(action.getScriptValidationContext()); Assert.assertEquals(action.getJdbcTemplate(), jdbcTemplate); Assert.assertEquals(action.getStatements().size(), 2); @@ -101,40 +95,40 @@ public void testExecuteSQLQueryWithResource() throws IOException { @Test public void testExecuteSQLQueryWithStatements() { List> results = new ArrayList<>(); - results.add(Collections.singletonMap("NAME", "Penny")); - results.add(Collections.singletonMap("NAME", "Sheldon")); + results.add(Collections.singletonMap("NAME", "Penny")); + results.add(Collections.singletonMap("NAME", "Sheldon")); reset(jdbcTemplate); when(jdbcTemplate.queryForList("SELECT NAME FROM ACTORS")).thenReturn(results); - when(jdbcTemplate.queryForList("SELECT COUNT(*) as CNT_EPISODES FROM EPISODES")).thenReturn(Collections.singletonList(Collections.singletonMap("CNT_EPISODES", "9999"))); + when(jdbcTemplate.queryForList("SELECT COUNT(*) as CNT_EPISODES FROM EPISODES")).thenReturn(Collections.singletonList(Collections.singletonMap("CNT_EPISODES", "9999"))); DefaultTestCaseRunner builder = new DefaultTestCaseRunner(context); builder.$(query().jdbcTemplate(jdbcTemplate) - .statement("SELECT NAME FROM ACTORS") - .statement("SELECT COUNT(*) as CNT_EPISODES FROM EPISODES") - .validate("NAME", "Penny", "Sheldon") - .validate("CNT_EPISODES", "9999") - .extract("CNT_EPISODES", "cntEpisodes")); - - Assert.assertNotNull(context.getVariable("NAME")); - Assert.assertNotNull(context.getVariable("CNT_EPISODES")); - Assert.assertNotNull(context.getVariable("cntEpisodes")); - Assert.assertEquals(context.getVariable("NAME"), "Penny"); - Assert.assertEquals(context.getVariable("CNT_EPISODES"), "9999"); - Assert.assertEquals(context.getVariable("cntEpisodes"), "9999"); + .statement("SELECT NAME FROM ACTORS") + .statement("SELECT COUNT(*) as CNT_EPISODES FROM EPISODES") + .validate("NAME", "Penny", "Sheldon") + .validate("CNT_EPISODES", "9999") + .extract("NAME", "actorName") + .extract("CNT_EPISODES", "episodesCount")); + + Assert.assertNotNull(context.getVariable("actorName")); + Assert.assertNotNull(context.getVariable("episodesCount")); + Assert.assertEquals(context.getVariable("actorName"), "Penny;Sheldon"); + Assert.assertEquals(context.getVariable("episodesCount"), "9999"); TestCase test = builder.getTestCase(); Assert.assertEquals(test.getActionCount(), 1); Assert.assertEquals(test.getActions().get(0).getClass(), ExecuteSQLQueryAction.class); - ExecuteSQLQueryAction action = (ExecuteSQLQueryAction)test.getActions().get(0); + ExecuteSQLQueryAction action = (ExecuteSQLQueryAction) test.getActions().get(0); Assert.assertEquals(action.getName(), "sql-query"); Assert.assertEquals(action.getControlResultSet().size(), 2); Set>> rows = action.getControlResultSet().entrySet(); Assert.assertEquals(getRow("NAME", rows).toString(), "NAME=[Penny, Sheldon]"); Assert.assertEquals(getRow("CNT_EPISODES", rows).toString(), "CNT_EPISODES=[9999]"); - Assert.assertEquals(action.getExtractVariables().size(), 1); - Assert.assertEquals(action.getExtractVariables().entrySet().iterator().next().toString(), "CNT_EPISODES=cntEpisodes"); + Assert.assertEquals(action.getExtractVariables().size(), 2); + Assert.assertEquals(action.getExtractVariables().get("NAME"), "actorName"); + Assert.assertEquals(action.getExtractVariables().get("CNT_EPISODES"), "episodesCount"); Assert.assertEquals(action.getStatements().size(), 2); Assert.assertEquals(action.getStatements().toString(), "[SELECT NAME FROM ACTORS, SELECT COUNT(*) as CNT_EPISODES FROM EPISODES]"); Assert.assertNull(action.getScriptValidationContext()); @@ -146,43 +140,43 @@ public void testExecuteSQLQueryWithStatements() { @Test public void testExecuteSQLQueryWithTransaction() { List> results = new ArrayList<>(); - results.add(Collections.singletonMap("NAME", "Penny")); - results.add(Collections.singletonMap("NAME", "Sheldon")); + results.add(Collections.singletonMap("NAME", "Penny")); + results.add(Collections.singletonMap("NAME", "Sheldon")); reset(jdbcTemplate, transactionManager); when(jdbcTemplate.queryForList("SELECT NAME FROM ACTORS")).thenReturn(results); - when(jdbcTemplate.queryForList("SELECT COUNT(*) as CNT_EPISODES FROM EPISODES")).thenReturn(Collections.singletonList(Collections.singletonMap("CNT_EPISODES", "9999"))); + when(jdbcTemplate.queryForList("SELECT COUNT(*) as CNT_EPISODES FROM EPISODES")).thenReturn(Collections.singletonList(Collections.singletonMap("CNT_EPISODES", "9999"))); DefaultTestCaseRunner builder = new DefaultTestCaseRunner(context); builder.$(query().jdbcTemplate(jdbcTemplate) - .transactionManager(transactionManager) - .transactionTimeout(5000) - .transactionIsolationLevel("ISOLATION_READ_COMMITTED") - .statement("SELECT NAME FROM ACTORS") - .statement("SELECT COUNT(*) as CNT_EPISODES FROM EPISODES") - .validate("NAME", "Penny", "Sheldon") - .validate("CNT_EPISODES", "9999") - .extract("CNT_EPISODES", "cntEpisodes")); - - Assert.assertNotNull(context.getVariable("NAME")); - Assert.assertNotNull(context.getVariable("CNT_EPISODES")); - Assert.assertNotNull(context.getVariable("cntEpisodes")); - Assert.assertEquals(context.getVariable("NAME"), "Penny"); - Assert.assertEquals(context.getVariable("CNT_EPISODES"), "9999"); - Assert.assertEquals(context.getVariable("cntEpisodes"), "9999"); + .transactionManager(transactionManager) + .transactionTimeout(5000) + .transactionIsolationLevel("ISOLATION_READ_COMMITTED") + .statement("SELECT NAME FROM ACTORS") + .statement("SELECT COUNT(*) as CNT_EPISODES FROM EPISODES") + .validate("NAME", "Penny", "Sheldon") + .validate("CNT_EPISODES", "9999") + .extract("NAME", "actorName") + .extract("CNT_EPISODES", "episodesCount")); + + Assert.assertNotNull(context.getVariable("actorName")); + Assert.assertNotNull(context.getVariable("episodesCount")); + Assert.assertEquals(context.getVariable("actorName"), "Penny;Sheldon"); + Assert.assertEquals(context.getVariable("episodesCount"), "9999"); TestCase test = builder.getTestCase(); Assert.assertEquals(test.getActionCount(), 1); Assert.assertEquals(test.getActions().get(0).getClass(), ExecuteSQLQueryAction.class); - ExecuteSQLQueryAction action = (ExecuteSQLQueryAction)test.getActions().get(0); + ExecuteSQLQueryAction action = (ExecuteSQLQueryAction) test.getActions().get(0); Assert.assertEquals(action.getName(), "sql-query"); Assert.assertEquals(action.getControlResultSet().size(), 2); Set>> rows = action.getControlResultSet().entrySet(); Assert.assertEquals(getRow("NAME", rows).toString(), "NAME=[Penny, Sheldon]"); Assert.assertEquals(getRow("CNT_EPISODES", rows).toString(), "CNT_EPISODES=[9999]"); - Assert.assertEquals(action.getExtractVariables().size(), 1); - Assert.assertEquals(action.getExtractVariables().entrySet().iterator().next().toString(), "CNT_EPISODES=cntEpisodes"); + Assert.assertEquals(action.getExtractVariables().size(), 2); + Assert.assertEquals(action.getExtractVariables().get("NAME"), "actorName"); + Assert.assertEquals(action.getExtractVariables().get("CNT_EPISODES"), "episodesCount"); Assert.assertEquals(action.getStatements().size(), 2); Assert.assertEquals(action.getStatements().toString(), "[SELECT NAME FROM ACTORS, SELECT COUNT(*) as CNT_EPISODES FROM EPISODES]"); Assert.assertNull(action.getScriptValidationContext()); @@ -195,6 +189,7 @@ public void testExecuteSQLQueryWithTransaction() { /** * Gets row from result set with given column name. + * * @param columnName * @param rows * @return