Repository: camel Updated Branches: refs/heads/camel-2.16.x 252bee8ed -> c1e0f30c4 refs/heads/master e205f90e5 -> 77644c086
When elsql supports functions Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/db1651df Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/db1651df Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/db1651df Branch: refs/heads/master Commit: db1651df62a3d9683b4a5204c808e534c7c07323 Parents: e205f90 Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Oct 7 18:19:06 2015 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Fri Oct 9 18:16:15 2015 +0200 ---------------------------------------------------------------------- components/camel-elsql/pom.xml | 2 +- .../component/elsql/ElsqlSqlMapSource.java | 6 ++ .../elsql/ElSqlProducerBodySimpleTest.java | 88 ++++++++++++++++++++ .../apache/camel/component/elsql/Project.java | 48 +++++++++++ .../src/test/resources/elsql/projects.elsql | 5 ++ 5 files changed, 148 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/db1651df/components/camel-elsql/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-elsql/pom.xml b/components/camel-elsql/pom.xml index 1fcf3b0..b883e1d 100644 --- a/components/camel-elsql/pom.xml +++ b/components/camel-elsql/pom.xml @@ -56,7 +56,7 @@ <dependency> <groupId>com.opengamma</groupId> <artifactId>elsql</artifactId> - <version>${elsql-version}</version> + <version>1.1.1-SNAPSHOT</version> </dependency> <!-- test dependencies --> http://git-wip-us.apache.org/repos/asf/camel/blob/db1651df/components/camel-elsql/src/main/java/org/apache/camel/component/elsql/ElsqlSqlMapSource.java ---------------------------------------------------------------------- diff --git a/components/camel-elsql/src/main/java/org/apache/camel/component/elsql/ElsqlSqlMapSource.java b/components/camel-elsql/src/main/java/org/apache/camel/component/elsql/ElsqlSqlMapSource.java index 725022e..eb489af 100644 --- a/components/camel-elsql/src/main/java/org/apache/camel/component/elsql/ElsqlSqlMapSource.java +++ b/components/camel-elsql/src/main/java/org/apache/camel/component/elsql/ElsqlSqlMapSource.java @@ -20,6 +20,7 @@ import java.util.Collections; import java.util.Map; import org.apache.camel.Exchange; +import org.apache.camel.language.simple.SimpleLanguage; import org.springframework.jdbc.core.namedparam.AbstractSqlParameterSource; /** @@ -48,6 +49,8 @@ public class ElsqlSqlMapSource extends AbstractSqlParameterSource { public boolean hasValue(String paramName) { if ("body".equals(paramName)) { return true; + } else if (paramName.startsWith("${") && paramName.endsWith("}")) { + return true; } else { return bodyMap.containsKey(paramName) || headersMap.containsKey(paramName); } @@ -58,6 +61,9 @@ public class ElsqlSqlMapSource extends AbstractSqlParameterSource { Object answer; if ("body".equals(paramName)) { answer = exchange.getIn().getBody(); + } else if (paramName.startsWith("${") && paramName.endsWith("}")) { + // its a simple language expression + answer = SimpleLanguage.expression(paramName).evaluate(exchange, Object.class); } else { answer = bodyMap.get(paramName); if (answer == null) { http://git-wip-us.apache.org/repos/asf/camel/blob/db1651df/components/camel-elsql/src/test/java/org/apache/camel/component/elsql/ElSqlProducerBodySimpleTest.java ---------------------------------------------------------------------- diff --git a/components/camel-elsql/src/test/java/org/apache/camel/component/elsql/ElSqlProducerBodySimpleTest.java b/components/camel-elsql/src/test/java/org/apache/camel/component/elsql/ElSqlProducerBodySimpleTest.java new file mode 100644 index 0000000..595017e --- /dev/null +++ b/components/camel-elsql/src/test/java/org/apache/camel/component/elsql/ElSqlProducerBodySimpleTest.java @@ -0,0 +1,88 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.elsql; + +import java.util.List; +import java.util.Map; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.impl.JndiRegistry; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.After; +import org.junit.Test; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; + +public class ElSqlProducerBodySimpleTest extends CamelTestSupport { + + private EmbeddedDatabase db; + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry jndi = super.createRegistry(); + + // this is the database we create with some initial data for our unit test + db = new EmbeddedDatabaseBuilder() + .setType(EmbeddedDatabaseType.DERBY).addScript("sql/createAndPopulateDatabase.sql").build(); + + jndi.bind("dataSource", db); + + return jndi; + } + + @Test + public void testSimpleBody() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + + Project pojo = new Project(); + pojo.setLicense("XXX"); + + template.sendBody("direct:simple", pojo); + + mock.assertIsSatisfied(); + + // the result is a List + List<?> received = assertIsInstanceOf(List.class, mock.getReceivedExchanges().get(0).getIn().getBody()); + + // and each row in the list is a Map + Map<?, ?> row = assertIsInstanceOf(Map.class, received.get(0)); + + // and we should be able the get the project from the map that should be Linux + assertEquals("Linux", row.get("PROJECT")); + } + + @After + public void tearDown() throws Exception { + super.tearDown(); + + db.shutdown(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + from("direct:simple") + .to("elsql:projectsByIdBody:elsql/projects.elsql?dataSource=dataSource") + .to("mock:result"); + } + }; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/db1651df/components/camel-elsql/src/test/java/org/apache/camel/component/elsql/Project.java ---------------------------------------------------------------------- diff --git a/components/camel-elsql/src/test/java/org/apache/camel/component/elsql/Project.java b/components/camel-elsql/src/test/java/org/apache/camel/component/elsql/Project.java new file mode 100644 index 0000000..9f6c19e --- /dev/null +++ b/components/camel-elsql/src/test/java/org/apache/camel/component/elsql/Project.java @@ -0,0 +1,48 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.elsql; + +public class Project { + + private String id; + private String license; + private String name; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getLicense() { + return license; + } + + public void setLicense(String license) { + this.license = license; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/db1651df/components/camel-elsql/src/test/resources/elsql/projects.elsql ---------------------------------------------------------------------- diff --git a/components/camel-elsql/src/test/resources/elsql/projects.elsql b/components/camel-elsql/src/test/resources/elsql/projects.elsql index ffc4192..74f16b9 100644 --- a/components/camel-elsql/src/test/resources/elsql/projects.elsql +++ b/components/camel-elsql/src/test/resources/elsql/projects.elsql @@ -3,6 +3,11 @@ FROM projects WHERE license = :body ORDER BY id +@NAME(projectsByIdBody) + SELECT * + FROM projects + WHERE license = :${body.license} + ORDER BY id @NAME(allProjects) SELECT * FROM projects