From f5262d89df0a6da748c7718c99ff906c35138ea1 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 20 May 2024 12:13:04 +0100 Subject: [PATCH] Add missing Testcontainers service connection tests Closes gh-35039 --- .../spring-boot-testcontainers/build.gradle | 14 ++++ ...rviceConnectionAutoConfigurationTests.java | 2 + ...ontainerConnectionDetailsFactoryTests.java | 66 ++++++++++++++++ ...ontainerConnectionDetailsFactoryTests.java | 75 +++++++++++++++++++ ...ontainerConnectionDetailsFactoryTests.java | 75 +++++++++++++++++++ ...ontainerConnectionDetailsFactoryTests.java | 70 +++++++++++++++++ ...ontainerConnectionDetailsFactoryTests.java | 72 ++++++++++++++++++ ...ontainerConnectionDetailsFactoryTests.java | 70 +++++++++++++++++ ...ontainerConnectionDetailsFactoryTests.java | 69 +++++++++++++++++ .../db/changelog/db.changelog-master.yaml | 20 +++++ 10 files changed, 533 insertions(+) create mode 100644 spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/cassandra/CassandraContainerConnectionDetailsFactoryTests.java create mode 100644 spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/couchbase/CouchbaseContainerConnectionDetailsFactoryTests.java create mode 100644 spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/elasticsearch/ElasticsearchContainerConnectionDetailsFactoryTests.java create mode 100644 spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/flyway/FlywayContainerConnectionDetailsFactoryTests.java create mode 100644 spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/jdbc/JdbcContainerConnectionDetailsFactoryTests.java create mode 100644 spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/liquibase/LiquibaseContainerConnectionDetailsFactoryTests.java create mode 100644 spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/redis/RedisContainerConnectionDetailsFactoryTests.java create mode 100644 spring-boot-project/spring-boot-testcontainers/src/test/resources/db/changelog/db.changelog-master.yaml diff --git a/spring-boot-project/spring-boot-testcontainers/build.gradle b/spring-boot-project/spring-boot-testcontainers/build.gradle index 9cfb970958..357deae606 100644 --- a/spring-boot-project/spring-boot-testcontainers/build.gradle +++ b/spring-boot-project/spring-boot-testcontainers/build.gradle @@ -36,20 +36,34 @@ dependencies { testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support")) testImplementation(project(":spring-boot-project:spring-boot-test")) testImplementation("ch.qos.logback:logback-classic") + testImplementation("co.elastic.clients:elasticsearch-java") { + exclude group: "commons-logging", module: "commons-logging" + } + testImplementation("com.couchbase.client:java-client") + testImplementation("com.datastax.oss:java-driver-core") testImplementation("org.assertj:assertj-core") testImplementation("org.awaitility:awaitility") + testImplementation("org.flywaydb:flyway-core") testImplementation("org.influxdb:influxdb-java") testImplementation("org.junit.jupiter:junit-jupiter") testImplementation("org.junit.platform:junit-platform-engine") testImplementation("org.junit.platform:junit-platform-launcher") + testImplementation("org.liquibase:liquibase-core") { + exclude(group: "javax.xml.bind", module: "jaxb-api") + } testImplementation("org.mockito:mockito-core") testImplementation("org.mockito:mockito-junit-jupiter") testImplementation("org.springframework:spring-core-test") + testImplementation("org.springframework:spring-jdbc") testImplementation("org.springframework:spring-r2dbc") testImplementation("org.springframework.amqp:spring-rabbit") + testImplementation("org.springframework.data:spring-data-redis") testImplementation("org.springframework.kafka:spring-kafka") testImplementation("org.testcontainers:junit-jupiter") testRuntimeOnly("com.oracle.database.r2dbc:oracle-r2dbc") + testRuntimeOnly("com.zaxxer:HikariCP") + testRuntimeOnly("io.lettuce:lettuce-core") + testRuntimeOnly("org.postgresql:postgresql") } diff --git a/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionAutoConfigurationTests.java b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionAutoConfigurationTests.java index 6e7fefc653..009092cebb 100644 --- a/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionAutoConfigurationTests.java @@ -30,6 +30,7 @@ import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails; import org.springframework.boot.testcontainers.beans.TestcontainerBeanDefinition; import org.springframework.boot.testcontainers.lifecycle.TestcontainersLifecycleApplicationContextInitializer; +import org.springframework.boot.testsupport.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.testcontainers.DisabledIfDockerUnavailable; import org.springframework.boot.testsupport.testcontainers.RedisContainer; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -81,6 +82,7 @@ class ServiceConnectionAutoConfigurationTests { } @Test + @ClassPathExclusions("lettuce-core-*.jar") void whenHasUserConfigurationDoesNotRegisterReplacement() { try (AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext()) { applicationContext.register(UserConfiguration.class, WithRedisAutoConfiguration.class, diff --git a/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/cassandra/CassandraContainerConnectionDetailsFactoryTests.java b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/cassandra/CassandraContainerConnectionDetailsFactoryTests.java new file mode 100644 index 0000000000..a2e7ecc550 --- /dev/null +++ b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/cassandra/CassandraContainerConnectionDetailsFactoryTests.java @@ -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.testcontainers.service.connection.cassandra; + +import com.datastax.oss.driver.api.core.CqlSession; +import org.junit.jupiter.api.Test; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration; +import org.springframework.boot.autoconfigure.cassandra.CassandraConnectionDetails; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.springframework.boot.testsupport.testcontainers.CassandraContainer; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link CassandraContainerConnectionDetailsFactory}. + * + * @author Andy Wilkinson + */ +@SpringJUnitConfig +@Testcontainers(disabledWithoutDocker = true) +class CassandraContainerConnectionDetailsFactoryTests { + + @Container + @ServiceConnection + static final CassandraContainer cassandra = new CassandraContainer(); + + @Autowired(required = false) + private CassandraConnectionDetails connectionDetails; + + @Autowired + private CqlSession cqlSession; + + @Test + void connectionCanBeMadeToCassandraContainer() { + assertThat(this.connectionDetails).isNotNull(); + assertThat(this.cqlSession.getMetadata().getNodes()).hasSize(1); + } + + @Configuration(proxyBeanMethods = false) + @ImportAutoConfiguration(CassandraAutoConfiguration.class) + static class TestConfiguration { + + } + +} diff --git a/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/couchbase/CouchbaseContainerConnectionDetailsFactoryTests.java b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/couchbase/CouchbaseContainerConnectionDetailsFactoryTests.java new file mode 100644 index 0000000000..debefc3980 --- /dev/null +++ b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/couchbase/CouchbaseContainerConnectionDetailsFactoryTests.java @@ -0,0 +1,75 @@ +/* + * 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.testcontainers.service.connection.couchbase; + +import java.time.Duration; + +import com.couchbase.client.java.Cluster; +import org.junit.jupiter.api.Test; +import org.testcontainers.couchbase.BucketDefinition; +import org.testcontainers.couchbase.CouchbaseContainer; +import org.testcontainers.couchbase.CouchbaseService; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration; +import org.springframework.boot.autoconfigure.couchbase.CouchbaseConnectionDetails; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.springframework.boot.testsupport.testcontainers.DockerImageNames; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link CouchbaseContainerConnectionDetailsFactory}. + * + * @author Andy Wilkinson + */ +@SpringJUnitConfig +@Testcontainers(disabledWithoutDocker = true) +class CouchbaseContainerConnectionDetailsFactoryTests { + + @Container + @ServiceConnection + static final CouchbaseContainer couchbase = new CouchbaseContainer(DockerImageNames.couchbase()) + .withEnabledServices(CouchbaseService.KV, CouchbaseService.INDEX, CouchbaseService.QUERY) + .withStartupAttempts(5) + .withStartupTimeout(Duration.ofMinutes(10)) + .withBucket(new BucketDefinition("cbbucket")); + + @Autowired(required = false) + private CouchbaseConnectionDetails connectionDetails; + + @Autowired + private Cluster cluster; + + @Test + void connectionCanBeMadeToCouchbaseContainer() { + assertThat(this.connectionDetails).isNotNull(); + assertThat(this.cluster.diagnostics().endpoints()).hasSize(1); + } + + @Configuration(proxyBeanMethods = false) + @ImportAutoConfiguration(CouchbaseAutoConfiguration.class) + static class TestConfiguration { + + } + +} diff --git a/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/elasticsearch/ElasticsearchContainerConnectionDetailsFactoryTests.java b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/elasticsearch/ElasticsearchContainerConnectionDetailsFactoryTests.java new file mode 100644 index 0000000000..64f693f46e --- /dev/null +++ b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/elasticsearch/ElasticsearchContainerConnectionDetailsFactoryTests.java @@ -0,0 +1,75 @@ +/* + * 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.testcontainers.service.connection.elasticsearch; + +import java.io.IOException; +import java.time.Duration; + +import co.elastic.clients.elasticsearch.ElasticsearchClient; +import org.junit.jupiter.api.Test; +import org.testcontainers.elasticsearch.ElasticsearchContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchClientAutoConfiguration; +import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchConnectionDetails; +import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.springframework.boot.testsupport.testcontainers.DockerImageNames; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link ElasticsearchContainerConnectionDetailsFactory}. + * + * @author Andy Wilkinson + */ +@SpringJUnitConfig +@Testcontainers(disabledWithoutDocker = true) +class ElasticsearchContainerConnectionDetailsFactoryTests { + + @Container + @ServiceConnection + static final ElasticsearchContainer elasticsearch = new ElasticsearchContainer(DockerImageNames.elasticsearch()) + .withEnv("ES_JAVA_OPTS", "-Xms32m -Xmx512m") + .withStartupAttempts(5) + .withStartupTimeout(Duration.ofMinutes(10)); + + @Autowired(required = false) + private ElasticsearchConnectionDetails connectionDetails; + + @Autowired + private ElasticsearchClient client; + + @Test + void connectionCanBeMadeToElasticsearchContainer() throws IOException { + assertThat(this.connectionDetails).isNotNull(); + assertThat(this.client.cluster().health().numberOfNodes()).isEqualTo(1); + } + + @Configuration(proxyBeanMethods = false) + @ImportAutoConfiguration({ ElasticsearchClientAutoConfiguration.class, + ElasticsearchRestClientAutoConfiguration.class }) + static class TestConfiguration { + + } + +} diff --git a/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/flyway/FlywayContainerConnectionDetailsFactoryTests.java b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/flyway/FlywayContainerConnectionDetailsFactoryTests.java new file mode 100644 index 0000000000..c6ce96899a --- /dev/null +++ b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/flyway/FlywayContainerConnectionDetailsFactoryTests.java @@ -0,0 +1,70 @@ +/* + * 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.testcontainers.service.connection.flyway; + +import org.flywaydb.core.Flyway; +import org.junit.jupiter.api.Test; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration; +import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.springframework.boot.testsupport.testcontainers.DockerImageNames; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatNoException; + +/** + * Tests for {@link FlywayContainerConnectionDetailsFactory}. + * + * @author Andy Wilkinson + */ +@SpringJUnitConfig +@Testcontainers(disabledWithoutDocker = true) +class FlywayContainerConnectionDetailsFactoryTests { + + @Container + @ServiceConnection + static final PostgreSQLContainer postgres = new PostgreSQLContainer<>(DockerImageNames.postgresql()); + + @Autowired(required = false) + private JdbcConnectionDetails connectionDetails; + + @Autowired + private Flyway flyway; + + @Test + void connectionCanBeMadeToJdbcContainer() { + assertThat(this.connectionDetails).isNotNull(); + JdbcTemplate jdbc = new JdbcTemplate(this.flyway.getConfiguration().getDataSource()); + assertThatNoException().isThrownBy(() -> jdbc.execute("SELECT * from public.flyway_schema_history")); + } + + @Configuration(proxyBeanMethods = false) + @ImportAutoConfiguration(FlywayAutoConfiguration.class) + static class TestConfiguration { + + } + +} diff --git a/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/jdbc/JdbcContainerConnectionDetailsFactoryTests.java b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/jdbc/JdbcContainerConnectionDetailsFactoryTests.java new file mode 100644 index 0000000000..feff309392 --- /dev/null +++ b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/jdbc/JdbcContainerConnectionDetailsFactoryTests.java @@ -0,0 +1,72 @@ +/* + * 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.testcontainers.service.connection.jdbc; + +import javax.sql.DataSource; + +import org.junit.jupiter.api.Test; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails; +import org.springframework.boot.jdbc.DatabaseDriver; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.springframework.boot.testsupport.testcontainers.DockerImageNames; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatNoException; + +/** + * Tests for {@link JdbcContainerConnectionDetailsFactory}. + * + * @author Andy Wilkinson + */ +@SpringJUnitConfig +@Testcontainers(disabledWithoutDocker = true) +class JdbcContainerConnectionDetailsFactoryTests { + + @Container + @ServiceConnection + static final PostgreSQLContainer postgres = new PostgreSQLContainer<>(DockerImageNames.postgresql()); + + @Autowired(required = false) + private JdbcConnectionDetails connectionDetails; + + @Autowired + private DataSource dataSource; + + @Test + void connectionCanBeMadeToJdbcContainer() { + assertThat(this.connectionDetails).isNotNull(); + JdbcTemplate jdbc = new JdbcTemplate(this.dataSource); + assertThatNoException().isThrownBy(() -> jdbc.execute(DatabaseDriver.POSTGRESQL.getValidationQuery())); + } + + @Configuration(proxyBeanMethods = false) + @ImportAutoConfiguration(DataSourceAutoConfiguration.class) + static class TestConfiguration { + + } + +} diff --git a/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/liquibase/LiquibaseContainerConnectionDetailsFactoryTests.java b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/liquibase/LiquibaseContainerConnectionDetailsFactoryTests.java new file mode 100644 index 0000000000..a37595667f --- /dev/null +++ b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/liquibase/LiquibaseContainerConnectionDetailsFactoryTests.java @@ -0,0 +1,70 @@ +/* + * 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.testcontainers.service.connection.liquibase; + +import liquibase.integration.spring.SpringLiquibase; +import org.junit.jupiter.api.Test; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails; +import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.springframework.boot.testsupport.testcontainers.DockerImageNames; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatNoException; + +/** + * Tests for {@link LiquibaseContainerConnectionDetailsFactory}. + * + * @author Andy Wilkinson + */ +@SpringJUnitConfig +@Testcontainers(disabledWithoutDocker = true) +class LiquibaseContainerConnectionDetailsFactoryTests { + + @Container + @ServiceConnection + static final PostgreSQLContainer postgres = new PostgreSQLContainer<>(DockerImageNames.postgresql()); + + @Autowired(required = false) + private JdbcConnectionDetails connectionDetails; + + @Autowired + private SpringLiquibase liquibase; + + @Test + void connectionCanBeMadeToJdbcContainer() { + assertThat(this.connectionDetails).isNotNull(); + JdbcTemplate jdbc = new JdbcTemplate(this.liquibase.getDataSource()); + assertThatNoException().isThrownBy(() -> jdbc.execute("SELECT * from example")); + } + + @Configuration(proxyBeanMethods = false) + @ImportAutoConfiguration(LiquibaseAutoConfiguration.class) + static class TestConfiguration { + + } + +} diff --git a/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/redis/RedisContainerConnectionDetailsFactoryTests.java b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/redis/RedisContainerConnectionDetailsFactoryTests.java new file mode 100644 index 0000000000..6bc97667ec --- /dev/null +++ b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/redis/RedisContainerConnectionDetailsFactoryTests.java @@ -0,0 +1,69 @@ +/* + * 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.testcontainers.service.connection.redis; + +import org.junit.jupiter.api.Test; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; +import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.springframework.boot.testsupport.testcontainers.RedisContainer; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnection; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link RedisContainerConnectionDetailsFactory}. + * + * @author Andy Wilkinson + */ +@SpringJUnitConfig +@Testcontainers(disabledWithoutDocker = true) +class RedisContainerConnectionDetailsFactoryTests { + + @Container + @ServiceConnection + static final RedisContainer redis = new RedisContainer(); + + @Autowired(required = false) + private RedisConnectionDetails connectionDetails; + + @Autowired + private RedisConnectionFactory connectionFactory; + + @Test + void connectionCanBeMadeToRedisContainer() { + assertThat(this.connectionDetails).isNotNull(); + try (RedisConnection connection = this.connectionFactory.getConnection()) { + assertThat(connection.commands().echo("Hello, World".getBytes())).isEqualTo("Hello, World".getBytes()); + } + } + + @Configuration(proxyBeanMethods = false) + @ImportAutoConfiguration(RedisAutoConfiguration.class) + static class TestConfiguration { + + } + +} diff --git a/spring-boot-project/spring-boot-testcontainers/src/test/resources/db/changelog/db.changelog-master.yaml b/spring-boot-project/spring-boot-testcontainers/src/test/resources/db/changelog/db.changelog-master.yaml new file mode 100644 index 0000000000..5d736c5541 --- /dev/null +++ b/spring-boot-project/spring-boot-testcontainers/src/test/resources/db/changelog/db.changelog-master.yaml @@ -0,0 +1,20 @@ +databaseChangeLog: + - changeSet: + id: 1 + author: wilkinsona + changes: + - createTable: + tableName: example + columns: + - column: + name: id + type: int + autoIncrement: true + constraints: + primaryKey: true + nullable: false + - column: + name: name + type: varchar(50) + constraints: + nullable: false