Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions spring-boot-project/spring-boot-docker-compose/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ dependencies {
dockerTestImplementation("org.junit.jupiter:junit-jupiter")
dockerTestImplementation("org.testcontainers:testcontainers")

dockerTestRuntimeOnly("com.clickhouse:clickhouse-jdbc")
dockerTestRuntimeOnly("com.clickhouse:clickhouse-r2dbc")
dockerTestRuntimeOnly("com.microsoft.sqlserver:mssql-jdbc")
dockerTestRuntimeOnly("com.oracle.database.r2dbc:oracle-r2dbc")
dockerTestRuntimeOnly("io.r2dbc:r2dbc-mssql")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.boot.docker.compose.service.connection.clickhouse;

import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails;
import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.boot.testsupport.container.TestImage;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Integration tests for {@link ClickHouseJdbcDockerComposeConnectionDetailsFactory}.
*
* @author Dmytro Nosan
*/
class ClickHouseJdbcDockerComposeConnectionDetailsFactoryIntegrationTests {

@DockerComposeTest(composeFile = "clickhouse-defaults-compose.yaml", image = TestImage.CLICKHOUSE)
void runCreatesJdbcConnectionDetailsWithDefaultValues(JdbcConnectionDetails connectionDetails) {
assertThat(connectionDetails.getUsername()).isEqualTo("default");
assertThat(connectionDetails.getPassword()).isEqualTo("");
assertThat(connectionDetails.getJdbcUrl()).startsWith("jdbc:clickhouse://").endsWith("/default");
checkDatabaseAccess(connectionDetails);
}

@DockerComposeTest(composeFile = "clickhouse-compose.yaml", image = TestImage.CLICKHOUSE)
void runCreatesJdbcConnectionDetails(JdbcConnectionDetails connectionDetails) {
assertThat(connectionDetails.getUsername()).isEqualTo("myuser");
assertThat(connectionDetails.getPassword()).isEqualTo("secret");
assertThat(connectionDetails.getJdbcUrl()).startsWith("jdbc:clickhouse://").endsWith("/mydatabase");
checkDatabaseAccess(connectionDetails);
}

private void checkDatabaseAccess(JdbcConnectionDetails connectionDetails) {
DataSource dataSource = DataSourceBuilder.create()
.type(SimpleDriverDataSource.class)
.url(connectionDetails.getJdbcUrl())
.username(connectionDetails.getUsername())
.password(connectionDetails.getPassword())
.build();
JdbcTemplate template = new JdbcTemplate(dataSource);
String sql = DatabaseDriver.CLICKHOUSE.getValidationQuery();
assertThat(template.queryForObject(sql, Integer.class)).isEqualTo(1);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.boot.docker.compose.service.connection.clickhouse;

import java.time.Duration;

import io.r2dbc.spi.ConnectionFactories;
import io.r2dbc.spi.ConnectionFactory;
import io.r2dbc.spi.ConnectionFactoryOptions;
import reactor.core.publisher.Mono;

import org.springframework.boot.autoconfigure.r2dbc.R2dbcConnectionDetails;
import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.boot.testsupport.container.TestImage;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Integration tests for {@link ClickHouseR2dbcDockerComposeConnectionDetailsFactory}.
*
* @author Dmytro Nosan
*/
class ClickHouseR2dbcDockerComposeConnectionDetailsFactoryIntegrationTests {

@DockerComposeTest(composeFile = "clickhouse-defaults-compose.yaml", image = TestImage.CLICKHOUSE)
void runCreatesR2dbcConnectionDetailsWithDefaultValues(R2dbcConnectionDetails connectionDetails) {
ConnectionFactoryOptions options = connectionDetails.getConnectionFactoryOptions();
assertCommonOptions(options);
assertThat(options.getRequiredValue(ConnectionFactoryOptions.DATABASE)).isEqualTo("default");
assertThat(options.getRequiredValue(ConnectionFactoryOptions.USER)).isEqualTo("default");
assertThat(options.hasOption(ConnectionFactoryOptions.PASSWORD)).isFalse();
checkDatabaseAccess(connectionDetails);
}

@DockerComposeTest(composeFile = "clickhouse-compose.yaml", image = TestImage.CLICKHOUSE)
void runCreatesR2dbcConnectionDetails(R2dbcConnectionDetails connectionDetails) {
ConnectionFactoryOptions options = connectionDetails.getConnectionFactoryOptions();
assertCommonOptions(options);
assertThat(options.getRequiredValue(ConnectionFactoryOptions.DATABASE)).isEqualTo("mydatabase");
assertThat(options.getRequiredValue(ConnectionFactoryOptions.USER)).isEqualTo("myuser");
assertThat(options.getRequiredValue(ConnectionFactoryOptions.PASSWORD)).isEqualTo("secret");
checkDatabaseAccess(connectionDetails);
}

private void assertCommonOptions(ConnectionFactoryOptions options) {
assertThat(options.getRequiredValue(ConnectionFactoryOptions.HOST)).isNotNull();
assertThat(options.getRequiredValue(ConnectionFactoryOptions.PORT)).isNotNull();
assertThat(options.getRequiredValue(ConnectionFactoryOptions.DRIVER)).isEqualTo("clickhouse");
}

private void checkDatabaseAccess(R2dbcConnectionDetails connectionDetails) {
ConnectionFactoryOptions connectionFactoryOptions = connectionDetails.getConnectionFactoryOptions();
ConnectionFactory connectionFactory = ConnectionFactories.get(connectionFactoryOptions);
String sql = DatabaseDriver.CLICKHOUSE.getValidationQuery();
Integer result = Mono.from(connectionFactory.create())
.flatMapMany((connection) -> connection.createStatement(sql).execute())
.flatMap((r) -> r.map((row, rowMetadata) -> row.get(0, Integer.class)))
.blockFirst(Duration.ofSeconds(30));
assertThat(result).isEqualTo(1);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
database:
image: '{imageName}'
ports:
- '8123'
environment:
- 'CLICKHOUSE_USER=myuser'
- 'CLICKHOUSE_PASSWORD=secret'
- 'CLICKHOUSE_DB=mydatabase'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
database:
image: '{imageName}'
ports:
- '8123'
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.boot.docker.compose.service.connection.clickhouse;

import java.util.Map;

/**
* ClickHouse environment details.
*
* @author Dmytro Nosan
*/
class ClickHouseEnvironment {

private final String username;

private final String password;

private final String database;

ClickHouseEnvironment(Map<String, String> env) {
this.username = env.getOrDefault("CLICKHOUSE_USER", "default");
this.password = env.getOrDefault("CLICKHOUSE_PASSWORD", "");
this.database = env.getOrDefault("CLICKHOUSE_DB", "default");
}

String getUsername() {
return this.username;
}

String getPassword() {
return this.password;
}

String getDatabase() {
return this.database;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.boot.docker.compose.service.connection.clickhouse;

import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails;
import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource;
import org.springframework.boot.docker.compose.service.connection.jdbc.JdbcUrlBuilder;

/**
* {@link DockerComposeConnectionDetailsFactory} to create {@link JdbcConnectionDetails}
* for a {@code ClickHouse} service.
*
* @author Dmytro Nosan
*/
class ClickHouseJdbcDockerComposeConnectionDetailsFactory
extends DockerComposeConnectionDetailsFactory<JdbcConnectionDetails> {

protected ClickHouseJdbcDockerComposeConnectionDetailsFactory() {
super("clickhouse/clickhouse-server");
}

@Override
protected JdbcConnectionDetails getDockerComposeConnectionDetails(DockerComposeConnectionSource source) {
return new ClickHouseJdbcDockerComposeConnectionDetails(source.getRunningService());
}

/**
* {@link JdbcConnectionDetails} backed by a {@code ClickHouse}
* {@link RunningService}.
*/
static class ClickHouseJdbcDockerComposeConnectionDetails extends DockerComposeConnectionDetails
implements JdbcConnectionDetails {

private static final JdbcUrlBuilder jdbcUrlBuilder = new JdbcUrlBuilder("clickhouse", 8123);

private final ClickHouseEnvironment environment;

private final String jdbcUrl;

ClickHouseJdbcDockerComposeConnectionDetails(RunningService service) {
super(service);
this.environment = new ClickHouseEnvironment(service.env());
this.jdbcUrl = jdbcUrlBuilder.build(service, this.environment.getDatabase());
}

@Override
public String getUsername() {
return this.environment.getUsername();
}

@Override
public String getPassword() {
return this.environment.getPassword();
}

@Override
public String getJdbcUrl() {
return this.jdbcUrl;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.boot.docker.compose.service.connection.clickhouse;

import io.r2dbc.spi.ConnectionFactoryOptions;

import org.springframework.boot.autoconfigure.r2dbc.R2dbcConnectionDetails;
import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource;
import org.springframework.boot.docker.compose.service.connection.r2dbc.ConnectionFactoryOptionsBuilder;

/**
* {@link DockerComposeConnectionDetailsFactory} to create {@link R2dbcConnectionDetails}
* for a {@code ClickHouse} service.
*
* @author Dmytro Nosan
*/
class ClickHouseR2dbcDockerComposeConnectionDetailsFactory
extends DockerComposeConnectionDetailsFactory<R2dbcConnectionDetails> {

ClickHouseR2dbcDockerComposeConnectionDetailsFactory() {
super("clickhouse/clickhouse-server", "io.r2dbc.spi.ConnectionFactoryOptions");
}

@Override
protected R2dbcConnectionDetails getDockerComposeConnectionDetails(DockerComposeConnectionSource source) {
return new ClickHouseR2dbcDockerComposeConnectionDetails(source.getRunningService());
}

/**
* {@link R2dbcConnectionDetails} backed by a {@code ClickHouse}
* {@link RunningService}.
*/
static class ClickHouseR2dbcDockerComposeConnectionDetails extends DockerComposeConnectionDetails
implements R2dbcConnectionDetails {

private static final ConnectionFactoryOptionsBuilder connectionFactoryOptionsBuilder = new ConnectionFactoryOptionsBuilder(
"clickhouse", 8123);

private final ConnectionFactoryOptions connectionFactoryOptions;

ClickHouseR2dbcDockerComposeConnectionDetails(RunningService service) {
super(service);
ClickHouseEnvironment environment = new ClickHouseEnvironment(service.env());
this.connectionFactoryOptions = connectionFactoryOptionsBuilder.build(service, environment.getDatabase(),
environment.getUsername(), environment.getPassword());
}

@Override
public ConnectionFactoryOptions getConnectionFactoryOptions() {
return this.connectionFactoryOptions;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed 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
*
* https://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.
*/

/**
* Auto-configuration for Docker Compose ClickHouse service connections.
*/
package org.springframework.boot.docker.compose.service.connection.clickhouse;
Loading