Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sql - enable test with different databases #3053 #3066

Merged
merged 1 commit into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 45 additions & 0 deletions integration-tests/sql/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
== SQL integration tests

=== Default database type

When the tests are executed without any special configuration, dev-service `H2` database is used (more details will follow).

=== Dev-service databases

As is described in the https://quarkus.io/guides/datasource#dev-services[documentation], several database types could be started in dev-service mode.
Running the tests against a database in dev-service mode could be achieved by addition of build property `cq.sqlJdbcKind`. Example of usage:

`mvn clean test -f integration-tests/sql/ -cq.sqlJdbcKind=postgresql`

Following databases could be started in the dev-service mode:

- Postgresql (container) - add `-Dcq.sqlJdbcKind=postgresql`
- MySQL (container) - add `-Dcq.sqlJdbcKind=mysql`
- MariaDB (container) - add `-Dcq.sqlJdbcKind=mariadb`
- H2 (in-process) used by default
- Apache Derby (in-process) - add `-Dcq.sqlJdbcKind=derby`
- DB2 (container) (requires license acceptance) - add `-Dcq.sqlJdbcKind=db2`
- MSSQL (container) (requires license acceptance) - add `-Dcq.sqlJdbcKind=mssql`

For more information about dev-service mode, see https://quarkus.io/guides/datasource#dev-services[documentation].
JiriOndrusek marked this conversation as resolved.
Show resolved Hide resolved

=== External databases

To execute the tests against external database, configure database type by providing a build property in the same way as with dev-service mode (see previous chapter).
Provide the rest of database's connection information by setting environment variables

```
export SQL_JDBC_URL=#jdbc_url
export SQL_JDBC_USERNAME=#username
export SQL_JDBC_PASSWORD=#password
```

or for windows:

```
$Env:SQL_JDBC_URL = "#jdbc_url"
$Env:SQL_JDBC_USERNAME="#username"
$Env:SQL_JDBC_PASSWORD="#password"
```

Oracle database could be used as external db. In that case use parameter `-DSQL_JDBC_DB_KIND=oracle`.
8 changes: 5 additions & 3 deletions integration-tests/sql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@

<artifactId>camel-quarkus-integration-test-sql</artifactId>
<name>Camel Quarkus :: Integration Tests :: SQL</name>
<description>Integration tests for Camel Quarkus SQL extension</description>

<properties>
<cq.sqlJdbcKind>h2</cq.sqlJdbcKind>
</properties>

<dependencies>
<dependency>
Expand All @@ -44,7 +47,7 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-h2</artifactId>
<artifactId>quarkus-jdbc-${cq.sqlJdbcKind}</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
Expand Down Expand Up @@ -144,5 +147,4 @@
</build>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.quarkus.component.sql.it;

import java.util.Collections;
import java.util.HashMap;

import io.smallrye.config.ConfigSourceContext;
import io.smallrye.config.ConfigSourceFactory;
import io.smallrye.config.common.MapBackedConfigSource;
import org.eclipse.microprofile.config.spi.ConfigSource;

public class SqlConfigSourceFactory implements ConfigSourceFactory {

private final static MapBackedConfigSource source;

static {
String jdbcUrl = System.getenv("SQL_JDBC_URL");

//external db
if (jdbcUrl != null) {
source = new MapBackedConfigSource("env_database", new HashMap() {
{
put("quarkus.datasource.jdbc.url", jdbcUrl);
put("quarkus.datasource.username", System.getenv("SQL_JDBC_USERNAME"));
put("quarkus.datasource.password", System.getenv("SQL_JDBC_PASSWORD"));
}
}) {
};
} else {
source = new MapBackedConfigSource("env_database", new HashMap()) {
};
}
}

@Override
public Iterable<ConfigSource> getConfigSources(ConfigSourceContext configSourceContext) {
return Collections.singletonList(source);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
*/
package org.apache.camel.quarkus.component.sql.it;

import java.io.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
Expand All @@ -25,30 +28,44 @@
import javax.inject.Inject;

import io.agroal.api.AgroalDataSource;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ApplicationScoped
public class SqlDbInitializer {

private static final Logger LOGGER = LoggerFactory.getLogger(SqlDbInitializer.class);

@Inject
AgroalDataSource dataSource;

@ConfigProperty(name = "quarkus.datasource.db-kind")
String dbKind;

public void initDb() throws SQLException, IOException {

try (Connection conn = dataSource.getConnection()) {
try (Statement statement = conn.createStatement()) {
try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("sql/initDb.sql");
try (InputStream is = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("sql/" + dbKind + "/initDb.sql");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr)) {

reader.lines().filter(s -> s != null && !"".equals(s) && !s.startsWith("--")).forEach(s -> {
try {
statement.execute(s);
} catch (SQLException e) {
throw new RuntimeException(e);
if (!s.toUpperCase().startsWith("DROP TABLE")) {
throw new RuntimeException(e);
} else {
LOGGER.debug(String.format("Command '%s' failed.", s)); //use debug logging
}
}
});
}
}
}
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.quarkus.component.sql.it;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class SqlHelper {

private static Set<String> BOOLEAN_AS_NUMBER = new HashSet<>(Arrays.asList("db2", "mssql", "oracle"));

static String convertBooleanToSqlDialect(String dbKind, boolean value) {
return convertBooleanToSqlResult(dbKind, value).toString();
}

static Object convertBooleanToSqlResult(String dbKind, boolean value) {

if (value) {
return BOOLEAN_AS_NUMBER.contains(dbKind) ? 1 : true;
}
return BOOLEAN_AS_NUMBER.contains(dbKind) ? 0 : false;
}

static String getSelectProjectsScriptName(String dbKind) {
return BOOLEAN_AS_NUMBER.contains(dbKind) ? "selectProjectsAsNumber.sql" : "selectProjectsAsBoolean.sql";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,18 @@
import org.apache.camel.CamelContext;
import org.apache.camel.CamelExecutionException;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.sql.SqlConstants;
import org.apache.camel.quarkus.component.sql.it.model.Camel;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.springframework.util.LinkedCaseInsensitiveMap;

@Path("/sql")
@ApplicationScoped
public class SqlResource {

@ConfigProperty(name = "quarkus.datasource.db-kind")
String dbKind;

@Inject
AgroalDataSource dataSource;

Expand All @@ -64,7 +69,7 @@ public String getCamel(@PathParam("species") String species) throws Exception {
Map<String, Object> params = new HashMap<>();
params.put("species", species);

return producerTemplate.requestBodyAndHeaders("sql:classpath:sql/get-camels.sql",
return producerTemplate.requestBodyAndHeaders("sql:classpath:sql/common/get-camels.sql",
null, params,
String.class);
}
Expand Down Expand Up @@ -110,6 +115,7 @@ public String getCamelSelectListWithType(@PathParam("species") String species) t
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
@SuppressWarnings("unchecked")
public Response insert(@QueryParam("table") String table, Map<String, Object> values) throws Exception {
LinkedHashMap linkedHashMap = new LinkedHashMap(values);

Expand Down Expand Up @@ -168,33 +174,18 @@ public List consumerResults(@PathParam("resultId") String resultId) throws Excep
return list;
}

@GET
@Path("/route/{routeId}/{operation}")
@Produces(MediaType.TEXT_PLAIN)
public String route(@PathParam("routeId") String routeId, @PathParam("operation") String operation)
throws Exception {
//is start enough
switch (operation) {
case "stop":
camelContext.getRouteController().stopRoute(routeId);
break;
case "start":
camelContext.getRouteController().startRoute(routeId);
break;
case "status":
return camelContext.getRouteController().getRouteStatus(routeId).name();

}

return null;
}

@Path("/toDirect/{directId}")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Object toDirect(@PathParam("directId") String directId, @QueryParam("body") String body, Map<String, Object> headers)
throws Exception {
String sql = (String) headers.get(SqlConstants.SQL_QUERY);
if (sql != null) {
headers.put(SqlConstants.SQL_QUERY,
sql.replaceAll("BOOLEAN_FALSE", SqlHelper.convertBooleanToSqlDialect(dbKind, false)));
}

try {
return producerTemplate.requestBodyAndHeaders("direct:" + directId, body, headers, Object.class);
} catch (CamelExecutionException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.camel.quarkus.component.sql.it;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.sql.SQLException;
import java.util.HashMap;
Expand All @@ -37,11 +38,15 @@
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.processor.aggregate.jdbc.JdbcAggregationRepository;
import org.apache.camel.processor.idempotent.jdbc.JdbcMessageIdRepository;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.springframework.transaction.jta.JtaTransactionManager;

@ApplicationScoped
public class SqlRoutes extends RouteBuilder {

@ConfigProperty(name = "quarkus.datasource.db-kind")
String dbKind;

@Inject
@Named("results")
Map<String, List> results;
Expand All @@ -63,18 +68,25 @@ public void configure() throws IOException, SQLException {
//db has to be initialized before routes are started
sqlDbInitializer.initDb();

from("sql:select * from projects where processed = false order by id?initialDelay=0&delay=50&consumer.onConsume=update projects set processed = true where id = :#id")
.id("consumerRoute").autoStartup(false)
.process(e -> results.get("consumerRoute").add(e.getMessage().getBody(Map.class)));

from("sql:classpath:sql/selectProjects.sql?initialDelay=0&delay=50&consumer.onConsume=update projects set processed = true")
.id("consumerClasspathRoute").autoStartup(false)
.process(e -> results.get("consumerClasspathRoute").add(e.getMessage().getBody(Map.class)));

Path tmpFile = createTmpFileFrom("sql/selectProjects.sql");
from("sql:file:" + tmpFile
+ "?initialDelay=0&delay=50&consumer.onConsume=update projects set processed = true")
.id("consumerFileRoute").autoStartup(false)
String representationOfTrue = SqlHelper.convertBooleanToSqlDialect(dbKind, true);
String representationOfFalse = SqlHelper.convertBooleanToSqlDialect(dbKind, false);
String selectProjectsScriptName = SqlHelper.getSelectProjectsScriptName(dbKind);

from(String.format("sql:select * from projectsViaSql where processed = %s"
+ " order by id?initialDelay=0&delay=50&consumer.onConsume=update projectsViaSql set processed = %s"
+ " where id = :#id", representationOfFalse, representationOfTrue))
.process(e -> results.get("consumerRoute").add(e.getMessage().getBody(Map.class)));

from(String.format("sql:classpath:sql/common/%s?initialDelay=0&delay=50&" +
"consumer.onConsume=update projectsViaClasspath set processed = %s", selectProjectsScriptName,
representationOfTrue))
.process(e -> results.get("consumerClasspathRoute").add(e.getMessage().getBody(Map.class)));

//File `sql/common/selectProjectsAs*.sql` is copied and modified to create tmp file for another test case
// (to have different file for the sql request from file and from classpath)
Path tmpFile = createTmpFileFrom("sql/common/" + selectProjectsScriptName);
from(String.format("sql:file:%s?initialDelay=0&delay=50&" +
"consumer.onConsume=update projectsViaFile set processed = %s", tmpFile, representationOfTrue))
.process(e -> results.get("consumerFileRoute").add(e.getMessage().getBody(Map.class)));

from("direct:transacted")
Expand Down Expand Up @@ -114,7 +126,9 @@ private Path createTmpFileFrom(String file) throws IOException {
while ((c = is.read()) >= 0) {
baos.write(c);
}
fos.write(baos.toByteArray());
String content = new String(baos.toByteArray(), StandardCharsets.UTF_8);
content = content.replaceAll("projectsViaClasspath", "projectsViaFile");
fos.write(content.getBytes(StandardCharsets.UTF_8));
}
return tmpFile.toPath();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.apache.camel.quarkus.component.sql.it.SqlConfigSourceFactory
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
## limitations under the License.
## ---------------------------------------------------------------------------

quarkus.datasource.db-kind=${cq.sqlJdbcKind:h2}

#
# Camel Quarkus SQL
#
quarkus.native.resources.includes=sql/*.sql

quarkus.native.resources.includes=sql/${SQL_JDBC_DB_KIND:h2}/*.sql,sql/common/*.sql