Skip to content

Commit

Permalink
Sql - enable test with different databases #3053
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriOndrusek committed Sep 3, 2021
1 parent 985d7c6 commit fa40d87
Show file tree
Hide file tree
Showing 21 changed files with 663 additions and 83 deletions.
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 `SQL_JDBC_DB_KIND`. Example of usage:

`mvn clean test -f integration-tests/sql/ -DSQL_JDBC_DB_KIND=postgresql`

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

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

For more information about dev-service mode, see https://quarkus.io/guides/datasource#dev-services[documentation].

=== 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`.
125 changes: 120 additions & 5 deletions integration-tests/sql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-h2</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-direct</artifactId>
Expand Down Expand Up @@ -143,6 +139,125 @@
</plugins>
</build>
</profile>
<profile>
<id>h2</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>!SQL_JDBC_DB_KIND</name>
</property>
</activation>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-h2</artifactId>
</dependency>
</dependencies>
</profile>
<profile>
<id>postgresql</id>
<activation>
<property>
<name>SQL_JDBC_DB_KIND</name>
<value>postgresql</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>
</dependencies>
</profile>
<profile>
<id>mssql</id>
<activation>
<property>
<name>SQL_JDBC_DB_KIND</name>
<value>mssql</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-mssql</artifactId>
</dependency>
</dependencies>
</profile>
<profile>
<id>db2</id>
<activation>
<property>
<name>SQL_JDBC_DB_KIND</name>
<value>db2</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-db2</artifactId>
</dependency>
</dependencies>
</profile>
<profile>
<id>mysql</id>
<activation>
<property>
<name>SQL_JDBC_DB_KIND</name>
<value>mysql</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-mysql</artifactId>
</dependency>
</dependencies>
</profile>
<profile>
<id>mariadb</id>
<activation>
<property>
<name>SQL_JDBC_DB_KIND</name>
<value>mariadb</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-mariadb</artifactId>
</dependency>
</dependencies>
</profile>
<profile>
<id>debrby</id>
<activation>
<property>
<name>SQL_JDBC_DB_KIND</name>
<value>derby</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-derby</artifactId>
</dependency>
</dependencies>
</profile>
<profile>
<id>oracle</id>
<activation>
<property>
<name>SQL_JDBC_DB_KIND</name>
<value>oracle</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-oracle</artifactId>
</dependency>
</dependencies>
</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,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.quarkus.component.sql.it;

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

public class SqlHelper {

private static Set<String> BOOLEAN_AS_NUMBER = new HashSet<>() {
{
add("db2");
add("mssql");
add("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";
}
}

0 comments on commit fa40d87

Please sign in to comment.