|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.docker.compose.service.connection.clickhouse; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | + |
| 21 | +import io.r2dbc.spi.ConnectionFactories; |
| 22 | +import io.r2dbc.spi.ConnectionFactory; |
| 23 | +import io.r2dbc.spi.ConnectionFactoryOptions; |
| 24 | +import reactor.core.publisher.Mono; |
| 25 | + |
| 26 | +import org.springframework.boot.autoconfigure.r2dbc.R2dbcConnectionDetails; |
| 27 | +import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest; |
| 28 | +import org.springframework.boot.jdbc.DatabaseDriver; |
| 29 | +import org.springframework.boot.testsupport.container.TestImage; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | + |
| 33 | +/** |
| 34 | + * Integration tests for {@link ClickHouseR2dbcDockerComposeConnectionDetailsFactory}. |
| 35 | + * |
| 36 | + * @author Stephane Nicoll |
| 37 | + */ |
| 38 | +class ClickHouseR2dbcDockerComposeConnectionDetailsFactoryIntegrationTests { |
| 39 | + |
| 40 | + @DockerComposeTest(composeFile = "clickhouse-compose.yaml", image = TestImage.CLICKHOUSE) |
| 41 | + void runCreatesConnectionDetails(R2dbcConnectionDetails connectionDetails) { |
| 42 | + assertConnectionDetails(connectionDetails); |
| 43 | + checkDatabaseAccess(connectionDetails); |
| 44 | + } |
| 45 | + |
| 46 | + @DockerComposeTest(composeFile = "clickhouse-bitnami-compose.yaml", image = TestImage.BITNAMI_CLICKHOUSE) |
| 47 | + void runWithBitnamiImageCreatesConnectionDetails(R2dbcConnectionDetails connectionDetails) { |
| 48 | + assertConnectionDetails(connectionDetails); |
| 49 | + // See https://github.com/bitnami/containers/issues/73550 |
| 50 | + // checkDatabaseAccess(connectionDetails); |
| 51 | + } |
| 52 | + |
| 53 | + private void assertConnectionDetails(R2dbcConnectionDetails connectionDetails) { |
| 54 | + ConnectionFactoryOptions connectionFactoryOptions = connectionDetails.getConnectionFactoryOptions(); |
| 55 | + assertThat(connectionFactoryOptions.toString()).contains("database=mydatabase", "driver=clickhouse", |
| 56 | + "password=REDACTED", "user=myuser"); |
| 57 | + assertThat(connectionFactoryOptions.getRequiredValue(ConnectionFactoryOptions.PASSWORD)).isEqualTo("secret"); |
| 58 | + } |
| 59 | + |
| 60 | + private void checkDatabaseAccess(R2dbcConnectionDetails connectionDetails) { |
| 61 | + ConnectionFactoryOptions connectionFactoryOptions = connectionDetails.getConnectionFactoryOptions(); |
| 62 | + ConnectionFactory connectionFactory = ConnectionFactories.get(connectionFactoryOptions); |
| 63 | + String sql = DatabaseDriver.CLICKHOUSE.getValidationQuery(); |
| 64 | + Integer result = Mono.from(connectionFactory.create()) |
| 65 | + .flatMapMany((connection) -> connection.createStatement(sql).execute()) |
| 66 | + .flatMap((r) -> r.map((row, rowMetadata) -> row.get(0, Integer.class))) |
| 67 | + .blockFirst(Duration.ofSeconds(30)); |
| 68 | + assertThat(result).isEqualTo(1); |
| 69 | + } |
| 70 | + |
| 71 | +} |
0 commit comments