From d7472e0c18469aaac0a851d705bfece2b974b40e Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Wed, 30 Jan 2019 14:14:15 +0100 Subject: [PATCH] (#29) Added end-2-end test for callable statements with outParameter --- demo/pom.xml | 1 + .../demo/controller/CityController.java | 50 ++++++++++++++++--- .../consol/citrus/demo/DemoApplicationIT.java | 14 ++++++ .../consol/citrus/demo/DemoJdbcServer.java | 10 +++- 4 files changed, 67 insertions(+), 8 deletions(-) diff --git a/demo/pom.xml b/demo/pom.xml index a1926b3..e8802b5 100644 --- a/demo/pom.xml +++ b/demo/pom.xml @@ -115,6 +115,7 @@ org.apache.maven.plugins maven-deploy-plugin + 2.8.2 true diff --git a/demo/src/main/java/com/consol/citrus/demo/controller/CityController.java b/demo/src/main/java/com/consol/citrus/demo/controller/CityController.java index 091add1..b117d93 100644 --- a/demo/src/main/java/com/consol/citrus/demo/controller/CityController.java +++ b/demo/src/main/java/com/consol/citrus/demo/controller/CityController.java @@ -4,12 +4,18 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.CallableStatementCreator; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.SqlParameter; import org.springframework.stereotype.Controller; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; +import java.sql.CallableStatement; +import java.sql.JDBCType; +import java.sql.Types; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -23,16 +29,22 @@ public class CityController { /** Logger */ private static Logger log = LoggerFactory.getLogger(CityController.class); + private final JdbcTemplate jdbcTemplate; + @Autowired - private JdbcTemplate jdbcTemplate; + public CityController(final JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } @GetMapping(path="/add") public @ResponseBody - String addNewProjects (@RequestParam String names) { - List splitUpNames = Arrays.asList(StringUtils.commaDelimitedListToStringArray(names)); + String addNewProjects (@RequestParam final String names) { + final List splitUpNames = Arrays.asList(StringUtils.commaDelimitedListToStringArray(names)); - for(Object name : splitUpNames) { - log.info(String.format("Inserting city %s", name)); + for(final Object name : splitUpNames) { + if(log.isDebugEnabled()){ + log.debug(String.format("Inserting city %s", name)); + } } if (splitUpNames.size() > 1) { @@ -46,7 +58,7 @@ String addNewProjects (@RequestParam String names) { } @GetMapping(path="/all") - public @ResponseBody Iterable getAllProjects(@RequestParam(required = false, defaultValue = "") String name) { + public @ResponseBody Iterable getAllProjects(@RequestParam(required = false, defaultValue = "") final String name) { if (StringUtils.hasText(name)) { return jdbcTemplate.query( "SELECT id, name FROM cities WHERE name = ?", new Object[]{ name }, @@ -59,4 +71,30 @@ String addNewProjects (@RequestParam String names) { ); } } + + @GetMapping(path="/findId") + public @ResponseBody Iterable findCityByName(@RequestParam(defaultValue = "") final String name) { + if (StringUtils.hasText(name)) { + return jdbcTemplate.call(createCallableStatement(name), createParameters()) + .values() + .stream() + .map(o -> (City) o ) + .collect(Collectors.toList()); + } + + return Collections.emptyList(); + } + + private List createParameters() { + return Collections.singletonList(new SqlParameter("name", Types.VARCHAR)); + } + + private CallableStatementCreator createCallableStatement(final String name) { + return con ->{ + final CallableStatement callableStatement = con.prepareCall("CALL findCityByName(?,?)"); + callableStatement.registerOutParameter(2, JDBCType.INTEGER); + callableStatement.setString("name", name); + return callableStatement; + }; + } } diff --git a/demo/src/test/java/com/consol/citrus/demo/DemoApplicationIT.java b/demo/src/test/java/com/consol/citrus/demo/DemoApplicationIT.java index 207b647..5209dfa 100644 --- a/demo/src/test/java/com/consol/citrus/demo/DemoApplicationIT.java +++ b/demo/src/test/java/com/consol/citrus/demo/DemoApplicationIT.java @@ -80,4 +80,18 @@ public void testCity() { .response(HttpStatus.OK) .payload(new ClassPathResource("cities.json"))); } + + @Test + @CitrusTest + public void testGetIdByCityName() { + http(action -> action.client(httpClient) + .send() + .get("/city/findId") + .queryParam("name", "Munich")); + + http(action -> action.client(httpClient) + .receive() + .response(HttpStatus.OK) + .payload("{ \"name\": \"Munich\", \"id\": 1 }")); + } } \ No newline at end of file diff --git a/demo/src/test/java/com/consol/citrus/demo/DemoJdbcServer.java b/demo/src/test/java/com/consol/citrus/demo/DemoJdbcServer.java index 0c86df7..a6af59d 100644 --- a/demo/src/test/java/com/consol/citrus/demo/DemoJdbcServer.java +++ b/demo/src/test/java/com/consol/citrus/demo/DemoJdbcServer.java @@ -13,8 +13,8 @@ */ public class DemoJdbcServer { - public static void main(String[] args) throws IOException, SQLException { - JdbcServer dbServer = new JdbcServer(args); + public static void main(final String[] args) throws IOException, SQLException { + final JdbcServer dbServer = new JdbcServer(args); dbServer.when() .statement() @@ -41,6 +41,12 @@ public static void main(String[] args) throws IOException, SQLException { .executeQuery(Pattern.compile("SELECT id, name FROM cities.*")) .thenReturn(new JsonDataSetProducer(new ClassPathResource("cities.json").getFile()).produce()); + dbServer.when() + .statement() + .execute(Pattern.compile("CALL findCityByName \\(Munich, .*")) + .thenReturn(new JsonDataSetProducer("[{ \"name\": \"Munich\", \"id\": 1 }]").produce()); + + dbServer.start(); } }