Skip to content

Commit

Permalink
Merge 2fbd4f6 into f152443
Browse files Browse the repository at this point in the history
  • Loading branch information
rjalic committed Jul 31, 2023
2 parents f152443 + 2fbd4f6 commit 7daf7bd
Show file tree
Hide file tree
Showing 31 changed files with 267 additions and 48 deletions.
50 changes: 40 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ Usual use cases include:
* [MSSQL](#MSSQL)
* [Tests](#MSSQLTests)
* [Local development](#MSSQLLocalDevelopment)
* [Docker image](#MSSQLDockerImage)
* [Docker image version](#MSSQLDockerImage)
* [Initialization script](#MSSQLInitScript)
* [PostgreSQL](#PostgreSQL)
* [Tests](#PostgreSQLTests)
* [Local development](#PostgreSQLLocalDevelopment)
* [Docker image](#PostgreSQLDockerImage)
* [Docker image version](#PostgreSQLDockerImage)
* [Initialization script](#PostgreSQLInitScript)
* [Redis](#Redis)
* [Tests](#RedisTests)
* [Local development](#RedisDevelopment)
* [Docker image](#RedisDockerImage)
* [Docker image version](#RedisDockerImage)
* [Kafka](#Kafka)
* [Automatic topic creation](#KafkaTopic)
* [Tests](#KafkaTests)
Expand All @@ -34,14 +36,15 @@ Usual use cases include:
* [RabbitMQ](#RabbitMq)
* [Tests](#RabbitMqTests)
* [Local development](#RabbitMqLocalDevelopment)
* [Docker image](#RabbitMqDockerImage)
* [Docker image version](#RabbitMqDockerImage)
* [ClickHouse](#ClickHouse)
* [Tests](#ClickHouseTests)
* [Local development](#ClickHouseLocalDevelopment)
* [Docker image version](#ClickHouseDockerImageVersion)
* [Docker image version](#ClickHouseDockerImage)
* [Initialization script](#ClickHouseInitScript)

<a name="Changelog"></a>
## Changelog
## Changelog

For changes check the [changelog](CHANGELOG.md).

Expand Down Expand Up @@ -147,6 +150,15 @@ To change the docker image used simply add the following property (e.g. in yaml)
testcontainers.mssql.docker.image: mcr.microsoft.com/mssql/server:2017-CU12
```

<a name="MSSQLInitScript"></a>
#### Initialization script

To add an SQL script with which the container will be initialized with add the following property (e.g. in yaml):

```yaml
testcontainers.mssql.init-script: db/init-script.sql
```

<a name="PostgreSQL"></a>
### PostgreSQL

Expand Down Expand Up @@ -220,7 +232,16 @@ spring:
To change the docker image used simply add the following property (e.g. in yaml):

```yaml
testcontainers.postgresql.docker.image: postgres:10
testcontainers.postgresql.docker.image: postgres:15
```

<a name="PostgreSQLInitScript"></a>
#### Initialization script

To add an SQL script with which the container will be initialized with add the following property (e.g. in yaml):

```yaml
testcontainers.postgresql.init-script: db/init-script.sql
```

<a name="Redis"></a>
Expand Down Expand Up @@ -501,7 +522,6 @@ Add the following profile:
Before starting the application locally, activate development profile:

![profile.png](profile.png)
<a name="ClickHouseDockerImageVersion"></a>

and update your local configuration (e.g. application-development.yaml):

Expand All @@ -512,7 +532,7 @@ spring:
```
In case your datasource configuration is different from default one you can provide custom configuration property path
```yaml
testcontainers.clickhouse.custom-path: "spring.datasource.clickhouse"
testcontainers.clickhouse.custom-path: "spring.datasource.clickhouse"
```
in this case your configuration would look like this

Expand All @@ -523,12 +543,22 @@ spring:
jdbc-url: <host>:<port>
```

<a name="ClickHouseDockerImage"></a>
#### Docker image version

To change the docker image used simply add the following property (e.g. in yaml):

```yaml
testcontainers.clickhouse.docker.image: rabbitmq:latest
testcontainers.clickhouse.docker.image: clickhouse:latest
```

<a name="ClickHouseInitScript"></a>
#### Initialization script

To add an SQL script with which the container will be initialized with add the following property (e.g. in yaml):

```yaml
testcontainers.clickhouse.init-script: db/init-script.sql
```

## <a name="Contributing"></a> Contributing
Expand Down
7 changes: 4 additions & 3 deletions infobip-clickhouse-testcontainers-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<artifactId>infobip-clickhouse-testcontainers-spring-boot-starter</artifactId>

<properties>
<clickhouse-jdbc.version>0.2.4</clickhouse-jdbc.version>
<clickhouse-jdbc.version>0.3.2</clickhouse-jdbc.version>
</properties>

<dependencies>
Expand All @@ -31,15 +31,16 @@
<artifactId>clickhouse</artifactId>
<version>${testcontainers.version}</version>
</dependency>

<dependency>
<groupId>ru.yandex.clickhouse</groupId>
<groupId>com.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>${clickhouse-jdbc.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ public void initialize(ConfigurableApplicationContext applicationContext) {
.map(ClickhouseContainerWrapper::new)
.orElseGet(ClickhouseContainerWrapper::new);

Optional.ofNullable(environment.getProperty("testcontainers.clickhouse.init-script")).ifPresent(container::withInitScript);

resolveStaticPort(jdbcUrlValue, GENERIC_URL_WITH_PORT_GROUP_PATTERN)
.ifPresent(staticPort -> bindPort(container, staticPort, ClickhouseContainerWrapper.HTTP_PORT));

start(container);

String url = replaceHostAndPortPlaceholders(jdbcUrlValue, container, ClickhouseContainerWrapper.HTTP_PORT);

TestPropertyValues values = TestPropertyValues.of(
String.format("%s=%s", jdbcUrlPropertyPath, url));
TestPropertyValues values = TestPropertyValues.of(String.format("%s=%s", jdbcUrlPropertyPath, url));
values.applyTo(applicationContext);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public class ClickhouseContainerWrapper extends ClickHouseContainer {
public ClickhouseContainerWrapper() {
}

public ClickhouseContainerWrapper(String confluentPlatformVersion) {
super(confluentPlatformVersion);
public ClickhouseContainerWrapper(String image) {
super(image);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
@SpringBootTest(classes = Main.class)
class ClickhouseContainerInitializerTest {

DataSource dataSource;
private DataSource dataSource;

@Test
void shouldCreateContainer() {
void shouldCreateContainer() {
//given
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

Expand All @@ -30,4 +30,5 @@ void shouldCreateContainer() {
//then
then(result).isEqualTo("1");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.infobip.testcontainers.spring.clickhouse;

import lombok.AllArgsConstructor;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestConstructor;

import javax.sql.DataSource;

import static org.assertj.core.api.BDDAssertions.then;

@AllArgsConstructor
@ActiveProfiles("init-script")
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
@SpringBootTest(classes = Main.class)
class ClickhouseContainerInitializerWithInitScriptTest {

private DataSource dataSource;

@Test
void shouldCreateContainer() {
//given
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

//when
String result = jdbcTemplate.queryForObject("SELECT name FROM test_table WHERE id = 1", String.class);

//then
then(result).isEqualTo("Test");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
@SpringBootTest(classes = Main.class)
class ClickhouseContainerInitializerWithStaticPortTest {

Environment environment;
DataSource dataSource;
private Environment environment;
private DataSource dataSource;

@Test
void shouldCreateContainerWithStaticPort() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public class Main {
public static void main(String[] args) {
new SpringApplicationBuilder(Main.class).run(args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
spring:
datasource:
clickhouse:
jdbc-url: jdbc:clickhouse://<host>:<port>

testcontainers:
clickhouse:
init-script: db/init-script.sql
custom-path: "spring.datasource.clickhouse"
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
spring:
datasource:
clickhouse:
jdbc-url: jdbc:clickhouse://<host>:5001
jdbc-url: jdbc:clickhouse://<host>:5001

testcontainers:
clickhouse:
custom-path: "spring.datasource.clickhouse"
clickhouse:
custom-path: "spring.datasource.clickhouse"
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
spring:
datasource:
clickhouse:
jdbc-url: jdbc:clickhouse://<host>:<port>
jdbc-url: jdbc:clickhouse://<host>:<port>

testcontainers:
clickhouse:
custom-path: "spring.datasource.clickhouse"
clickhouse:
custom-path: "spring.datasource.clickhouse"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE test_table
(
id Int32,
name String
) ENGINE = MergeTree() ORDER BY id;

INSERT INTO test_table (id, name)
VALUES (1, 'Test');
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@ class DatabaseCreator {
}

private String getDatabaseUrlPattern(String url) {

if (url.startsWith("jdbc:jtds")) {
return "(?<jdbcBaseUrl>.*)/(?<databaseName>[^;]*);?(?<otherParameters>.*)";
}

return "(?<jdbcBaseUrl>.*);database=(?<databaseName>[^;]*)(?<otherParameters>;.*)?";
}

void createDatabaseIfItDoesntExist() {
void createDatabaseIfItDoesNotExist() {
if(!databaseExists()) {
template.execute(createDatabaseQuery);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public void initialize(ConfigurableApplicationContext applicationContext) {
.map(MSSQLServerContainerWrapper::new)
.orElseGet(MSSQLServerContainerWrapper::new);

Optional<MSSQLServerContainerWrapper> initScript = Optional.ofNullable(
environment.getProperty("testcontainers.mssql.init-script")).map(container::withInitScript);

resolveStaticPort(urlPropertyNameToValue.values(), GENERIC_URL_WITH_PORT_GROUP_PATTERN)
.ifPresent(staticPort -> bindPort(container, staticPort, MS_SQL_SERVER_PORT));

Expand All @@ -42,9 +45,11 @@ public void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertyValues values = TestPropertyValues.of(testPropertyValues);

values.applyTo(applicationContext);
String url = replacedNameToValue.getOrDefault("spring.datasource.url", replacedNameToValue.get("spring.flyway.url"));
DatabaseCreator creator = new DatabaseCreator(url, container.getUsername(), container.getPassword());
creator.createDatabaseIfItDoesntExist();
if (initScript.isEmpty()) {
String url = replacedNameToValue.getOrDefault("spring.datasource.url", replacedNameToValue.get("spring.flyway.url"));
DatabaseCreator creator = new DatabaseCreator(url, container.getUsername(), container.getPassword());
creator.createDatabaseIfItDoesNotExist();
}
}

private List<String> getUrlPropertyNames(Environment environment) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.infobip.testcontainers.spring.mssql;

import lombok.AllArgsConstructor;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestConstructor;

import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;

import static org.assertj.core.api.BDDAssertions.then;

@AllArgsConstructor
@ActiveProfiles("init-script")
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
@SpringBootTest(classes = Main.class)
class MSSQLServerContainerInitializerWithInitScriptTest {

private final DataSourceProperties properties;

@Test
void shouldCreateDatabase() {
// given
JdbcTemplate jdbcTemplate = new JdbcTemplate(new SimpleDriverDataSource(
getDriver(properties.getUrl()),
properties.getUrl(),
properties.getUsername(),
properties.getPassword()));

// when
String actual = jdbcTemplate.queryForObject("SELECT TOP 1 name FROM test_table", String.class);

// then
then(actual).isEqualTo("Test");
}

private Driver getDriver(String jdbcUrl) {
try {
return DriverManager.getDriver(jdbcUrl);
} catch (SQLException e) {
throw new IllegalArgumentException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class MSSQLServerContainerInitializerWithJtdsDriverTest {

@Test
void shouldCreateDatabase() {

// given
JdbcTemplate jdbcTemplate = new JdbcTemplate(new SimpleDriverDataSource(
getDriver(properties.getUrl()),
Expand All @@ -45,4 +44,5 @@ private Driver getDriver(String jdbcUrl) {
throw new IllegalArgumentException(e);
}
}

}
Loading

0 comments on commit 7daf7bd

Please sign in to comment.