@Test public void testResultSetScriptValidationCombination() { String sql = "select ORDERTYPES, STATUS from orders where ID=5"; reset(jdbcTemplate); Map<String, Object> resultMap = new HashMap<String, Object>(); resultMap.put("ORDERTYPE", "small"); resultMap.put("STATUS", "in_progress"); expect(jdbcTemplate.queryForList(sql)).andReturn(Collections.singletonList(resultMap)); replay(jdbcTemplate); List<String> stmts = Collections.singletonList(sql); executeSQLQueryAction.setStatements(stmts); Map<String, List<String>> controlResultSet = new HashMap<String, List<String>>(); controlResultSet.put("ORDERTYPE", Collections.singletonList("small")); controlResultSet.put("STATUS", Collections.singletonList("in_progress")); executeSQLQueryAction.setControlResultSet(controlResultSet); ScriptValidationContext scriptValidationContext = new ScriptValidationContext( "assert rows.size() == 1\n" + "assert rows[0].ORDERTYPE == 'small'\n" + "assert rows[0] == [ORDERTYPE:'small', STATUS:'in_progress']", ScriptTypes.GROOVY); executeSQLQueryAction.setScriptValidationContext(scriptValidationContext); executeSQLQueryAction.execute(context); }
@Test public void testResultSetValidationError() { String sql = "select ORDERTYPE, STATUS from orders where ID=5"; reset(jdbcTemplate); Map<String, Object> resultMap = new HashMap<String, Object>(); resultMap.put("ORDERTYPE", "small"); resultMap.put("STATUS", "in_progress"); expect(jdbcTemplate.queryForList(sql)).andReturn(Collections.singletonList(resultMap)); replay(jdbcTemplate); List<String> stmts = Collections.singletonList(sql); executeSQLQueryAction.setStatements(stmts); Map<String, List<String>> controlResultSet = new HashMap<String, List<String>>(); controlResultSet.put( "ORDERTYPE", Collections.singletonList("xxl")); // this is supposed to cause an error controlResultSet.put("STATUS", Collections.singletonList("in_progress")); executeSQLQueryAction.setControlResultSet(controlResultSet); try { executeSQLQueryAction.execute(context); } catch (ValidationException e) { Assert.assertNull(context.getVariables().get("${ORDERTYPE}")); Assert.assertNull(context.getVariables().get("${STATUS}")); return; } Assert.fail("Expected test to fail with " + ValidationException.class + " but was successful"); }
@Test public void testResultSetValidationWithAliasNames() { String sql = "select ORDERTYPE AS TYPE, STATUS AS STATE from orders where ID=5"; reset(jdbcTemplate); Map<String, Object> resultMap = new HashMap<String, Object>(); resultMap.put("TYPE", "small"); resultMap.put("STATE", "in_progress"); expect(jdbcTemplate.queryForList(sql)).andReturn(Collections.singletonList(resultMap)); replay(jdbcTemplate); List<String> stmts = Collections.singletonList(sql); executeSQLQueryAction.setStatements(stmts); Map<String, List<String>> controlResultSet = new HashMap<String, List<String>>(); controlResultSet.put("TYPE", Collections.singletonList("small")); controlResultSet.put("STATE", Collections.singletonList("in_progress")); executeSQLQueryAction.setControlResultSet(controlResultSet); executeSQLQueryAction.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 public void testExtractMultipleRowValues() { String sql = "select distinct STATUS from orders"; reset(jdbcTemplate); List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>(); Map<String, Object> resultRow1 = new HashMap<String, Object>(); Map<String, Object> resultRow2 = new HashMap<String, Object>(); Map<String, Object> resultRow3 = new HashMap<String, Object>(); resultRow1.put("ORDERTYPE", "small"); resultRow1.put("STATUS", "started"); resultList.add(resultRow1); resultRow2.put("ORDERTYPE", null); resultRow2.put("STATUS", "in_progress"); resultList.add(resultRow2); resultRow3.put("ORDERTYPE", "big"); resultRow3.put("STATUS", "finished"); resultList.add(resultRow3); expect(jdbcTemplate.queryForList(sql)).andReturn(resultList); replay(jdbcTemplate); List<String> stmts = Collections.singletonList(sql); executeSQLQueryAction.setStatements(stmts); Map<String, String> extractVariables = new HashMap<String, String>(); extractVariables.put("STATUS", "orderStatus"); extractVariables.put("ORDERTYPE", "orderType"); executeSQLQueryAction.setExtractVariables(extractVariables); Map<String, List<String>> controlResultSet = new HashMap<String, List<String>>(); List<String> ordertypeValues = new ArrayList<String>(); ordertypeValues.add("small"); ordertypeValues.add(CitrusConstants.IGNORE_PLACEHOLDER); ordertypeValues.add("big"); controlResultSet.put("ORDERTYPE", ordertypeValues); List<String> statusValues = new ArrayList<String>(); statusValues.add("started"); statusValues.add("in_progress"); statusValues.add("finished"); controlResultSet.put("STATUS", statusValues); executeSQLQueryAction.setControlResultSet(controlResultSet); executeSQLQueryAction.execute(context); Assert.assertNotNull(context.getVariable("orderType")); 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 public void testNullValuesInMultipleRowsValidation() { String sql = "select ORDERTYPE, STATUS from orders where ID < 5"; reset(jdbcTemplate); List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>(); Map<String, Object> resultRow1 = new HashMap<String, Object>(); Map<String, Object> resultRow2 = new HashMap<String, Object>(); Map<String, Object> resultRow3 = new HashMap<String, Object>(); resultRow1.put("ORDERTYPE", "small"); resultRow1.put("STATUS", null); resultList.add(resultRow1); resultRow2.put("ORDERTYPE", "medium"); resultRow2.put("STATUS", "in_progress"); resultList.add(resultRow2); resultRow3.put("ORDERTYPE", null); resultRow3.put("STATUS", "finished"); resultList.add(resultRow3); expect(jdbcTemplate.queryForList(sql)).andReturn(resultList); replay(jdbcTemplate); List<String> stmts = Collections.singletonList(sql); executeSQLQueryAction.setStatements(stmts); Map<String, List<String>> controlResultSet = new HashMap<String, List<String>>(); List<String> ordertypeValues = new ArrayList<String>(); ordertypeValues.add("small"); ordertypeValues.add("medium"); ordertypeValues.add(""); // 1st possibility to validate null values controlResultSet.put("ORDERTYPE", ordertypeValues); List<String> statusValues = new ArrayList<String>(); statusValues.add("NULL"); // 2nd possibility to validate null values statusValues.add("in_progress"); statusValues.add("finished"); controlResultSet.put("STATUS", statusValues); executeSQLQueryAction.setControlResultSet(controlResultSet); executeSQLQueryAction.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 public void testMultipleStatementsValidationError() { String sql1 = "select ORDERTYPE, STATUS from orders where ID=5"; String sql2 = "select NAME, HEIGHT from customers where ID=1"; reset(jdbcTemplate); Map<String, Object> resultMap1 = new HashMap<String, Object>(); resultMap1.put("ORDERTYPE", "small"); resultMap1.put("STATUS", "in_progress"); expect(jdbcTemplate.queryForList(sql1)).andReturn(Collections.singletonList(resultMap1)); Map<String, Object> resultMap2 = new HashMap<String, Object>(); resultMap2.put("NAME", "Mickey Mouse"); resultMap2.put("HEIGHT", "0,3"); expect(jdbcTemplate.queryForList(sql2)).andReturn(Collections.singletonList(resultMap2)); replay(jdbcTemplate); List<String> stmts = new ArrayList<String>(); stmts.add(sql1); stmts.add(sql2); executeSQLQueryAction.setStatements(stmts); Map<String, List<String>> controlResultSet = new HashMap<String, List<String>>(); controlResultSet.put("ORDERTYPE", Collections.singletonList("small")); controlResultSet.put("STATUS", Collections.singletonList("in_progress")); controlResultSet.put( "NAME", Collections.singletonList("Donald Duck")); // this is supposed to cause an error controlResultSet.put("HEIGHT", Collections.singletonList("0,3")); executeSQLQueryAction.setControlResultSet(controlResultSet); try { executeSQLQueryAction.execute(context); } catch (ValidationException e) { Assert.assertNull(context.getVariables().get("${ORDERTYPE}")); Assert.assertNull(context.getVariables().get("${STATUS}")); Assert.assertNull(context.getVariables().get("${NAME}")); Assert.assertNull(context.getVariables().get("${HEIGHT}")); return; } Assert.fail("Expected test to fail with " + ValidationException.class + " but was successful"); }