Skip to content

Commit

Permalink
(#29) Added end-2-end test for callable statements with outParameter
Browse files Browse the repository at this point in the history
  • Loading branch information
svettwer committed Jan 30, 2019
1 parent d05243c commit d7472e0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 8 deletions.
1 change: 1 addition & 0 deletions demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<configuration>
<skip>true</skip>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Object> splitUpNames = Arrays.asList(StringUtils.commaDelimitedListToStringArray(names));
String addNewProjects (@RequestParam final String names) {
final List<Object> 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) {
Expand All @@ -46,7 +58,7 @@ String addNewProjects (@RequestParam String names) {
}

@GetMapping(path="/all")
public @ResponseBody Iterable<City> getAllProjects(@RequestParam(required = false, defaultValue = "") String name) {
public @ResponseBody Iterable<City> 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 },
Expand All @@ -59,4 +71,30 @@ String addNewProjects (@RequestParam String names) {
);
}
}

@GetMapping(path="/findId")
public @ResponseBody Iterable<City> 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<SqlParameter> 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;
};
}
}
14 changes: 14 additions & 0 deletions demo/src/test/java/com/consol/citrus/demo/DemoApplicationIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 }"));
}
}
10 changes: 8 additions & 2 deletions demo/src/test/java/com/consol/citrus/demo/DemoJdbcServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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();
}
}

0 comments on commit d7472e0

Please sign in to comment.