Skip to content

Commit

Permalink
Merge pull request #35 from citrusframework/enhancement/29/mplement-r…
Browse files Browse the repository at this point in the history
…egisterOutParameter-in-JdbcCallableStatement

Enhancement/29/mplement register out parameter in jdbc callable statement
  • Loading branch information
svettwer authored Feb 15, 2019
2 parents b8d580d + d11066c commit 80fbdf8
Show file tree
Hide file tree
Showing 27 changed files with 3,688 additions and 647 deletions.
6 changes: 2 additions & 4 deletions agent/pom.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.consol.citrus</groupId>
<artifactId>citrus-db</artifactId>
<version>0.1.5-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
6 changes: 3 additions & 3 deletions demo/pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.consol.citrus</groupId>
<artifactId>citrus-db-demo</artifactId>
<version>0.1.5-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>

<name>citrus-db-demo</name>

Expand Down Expand Up @@ -116,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,13 +4,22 @@
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.LinkedCaseInsensitiveMap;
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.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
Expand All @@ -23,16 +32,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 +61,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 +74,30 @@ String addNewProjects (@RequestParam String names) {
);
}
}

@GetMapping(path="/findId")
public @ResponseBody Iterable<City> findCityByName(@RequestParam(defaultValue = "") final String name) {
if (StringUtils.hasText(name)) {
final Map<String, Object> call = jdbcTemplate.call(createCallableStatement(name), createParameters());
final ArrayList<LinkedCaseInsensitiveMap> resultSet = (ArrayList<LinkedCaseInsensitiveMap>) call.get("#result-set-1");
return resultSet.stream().map(linkedCaseInsensitiveMap ->
new City(Long.valueOf((Integer) linkedCaseInsensitiveMap.get("id")), (String) linkedCaseInsensitiveMap.get("name"))
).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();
}
}
11 changes: 5 additions & 6 deletions docs/pom.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.consol.citrus</groupId>
<artifactId>citrus-db</artifactId>
<version>0.1.5-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -38,10 +37,10 @@
<sourceDocumentName>index.adoc</sourceDocumentName>
<attributes>
<icons>font</icons>
<pagenums/>
<pagenums />
<version>${project.version}</version>
<plugindir>${project.basedir}/plugin</plugindir>
<idprefix/>
<idprefix />
<idseparator>-</idseparator>
<allow-uri-read>true</allow-uri-read>
</attributes>
Expand Down Expand Up @@ -103,7 +102,7 @@
<backend>pdf</backend>
<sourceHighlighter>rouge</sourceHighlighter>
<attributes>
<toc/>
<toc />
</attributes>
</configuration>
</execution>
Expand Down
14 changes: 10 additions & 4 deletions driver/pom.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.consol.citrus</groupId>
<artifactId>citrus-db</artifactId>
<version>0.1.5-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -35,6 +33,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down Expand Up @@ -83,6 +85,10 @@
<pattern>org.slf4j</pattern>
<shadedPattern>org.shaded.slf4j</shadedPattern>
</relocation>
<relocation>
<pattern>commons-beanutils</pattern>
<shadedPattern>org.shaded.commons-beanutils</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
Expand Down
Loading

0 comments on commit 80fbdf8

Please sign in to comment.