-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
413 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.infobip</groupId> | ||
<artifactId>infobip-testcontainers-spring-boot-starter</artifactId> | ||
<version>4.2.1-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>infobip-mysql-testcontainers-spring-boot-starter</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>infobip-testcontainers-common</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<!-- LIBS --> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-jdbc</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.mysql</groupId> | ||
<artifactId>mysql-connector-j</artifactId> | ||
</dependency> | ||
|
||
|
||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>mysql</artifactId> | ||
<version>${testcontainers.version}</version> | ||
</dependency> | ||
|
||
<!--TEST--> | ||
<dependency> | ||
<groupId>com.infobip</groupId> | ||
<artifactId>infobip-testcontainers-test-common</artifactId> | ||
<scope>test</scope> | ||
<version>${project.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
45 changes: 45 additions & 0 deletions
45
...rter/src/main/java/com/infobip/testcontainers/spring/mysql/MySQLContainerInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.infobip.testcontainers.spring.mysql; | ||
|
||
import com.infobip.testcontainers.InitializerBase; | ||
import org.springframework.boot.test.util.TestPropertyValues; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.util.Optional; | ||
|
||
public class MySQLContainerInitializer extends InitializerBase<MySQLContainerWrapper> { | ||
|
||
@Override | ||
public void initialize(ConfigurableApplicationContext applicationContext) { | ||
var environment = applicationContext.getEnvironment(); | ||
var dataSourceUrlPropertyName = Optional.ofNullable( | ||
environment.getProperty("testcontainers.mysql.datasource.url.property.name")) | ||
.orElse("spring.datasource.url"); | ||
var dataSourceUrl = environment.getProperty(dataSourceUrlPropertyName); | ||
|
||
if (StringUtils.isEmpty(dataSourceUrl)) { | ||
throw new IllegalStateException("URL for test-container is null or empty."); | ||
} | ||
|
||
var database = dataSourceUrl.substring(dataSourceUrl.lastIndexOf("/") + 1); | ||
var wrapper = Optional.ofNullable(environment.getProperty("testcontainers.mysql.docker.image")) | ||
.map(imageName -> new MySQLContainerWrapper(database, imageName)) | ||
.orElseGet(() -> new MySQLContainerWrapper(database)); | ||
var container = handleReusable(wrapper); | ||
|
||
Optional.ofNullable(environment.getProperty("testcontainers.mysql.init-script")) | ||
.ifPresent(container::withInitScript); | ||
|
||
resolveStaticPort(dataSourceUrl, GENERIC_URL_WITH_PORT_GROUP_PATTERN) | ||
.ifPresent(staticPort -> bindPort(container, staticPort, MySQLContainerWrapper.MYSQL_PORT)); | ||
|
||
start(container); | ||
|
||
var url = replaceHostAndPortPlaceholders(dataSourceUrl, container, MySQLContainerWrapper.MYSQL_PORT); | ||
var values = TestPropertyValues.of(dataSourceUrlPropertyName + "=" + url); | ||
|
||
values.applyTo(applicationContext); | ||
|
||
registerContainerAsBean(applicationContext); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...-starter/src/main/java/com/infobip/testcontainers/spring/mysql/MySQLContainerWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.infobip.testcontainers.spring.mysql; | ||
|
||
public class MySQLContainerWrapper extends org.testcontainers.containers.MySQLContainer<MySQLContainerWrapper> { | ||
|
||
public MySQLContainerWrapper(String databaseName) { | ||
this(databaseName, IMAGE + ":" + DEFAULT_TAG); | ||
} | ||
|
||
public MySQLContainerWrapper(String databaseName, String dockerImageName) { | ||
super(dockerImageName); | ||
withDatabaseName(databaseName); | ||
} | ||
|
||
} |
2 changes: 2 additions & 0 deletions
2
...bip-mysql-testcontainers-spring-boot-starter/src/main/resources/META-INF/spring.factories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
org.springframework.context.ApplicationContextInitializer=com.infobip.testcontainers.spring.mysql.MySQLContainerInitializer | ||
org.springframework.context.ApplicationListener=com.infobip.testcontainers.spring.mysql.MySQLContainerInitializer |
45 changes: 45 additions & 0 deletions
45
.../src/test/java/com/infobip/testcontainers/spring/mysql/MySQLContainerInitializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.infobip.testcontainers.spring.mysql; | ||
|
||
import com.infobip.testcontainers.TestBase; | ||
import lombok.AllArgsConstructor; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.datasource.SimpleDriverDataSource; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
import java.sql.*; | ||
|
||
import static org.assertj.core.api.BDDAssertions.then; | ||
|
||
@AllArgsConstructor | ||
@ActiveProfiles("test") | ||
class MySQLContainerInitializerTest extends TestBase { | ||
|
||
private final DataSourceProperties properties; | ||
|
||
@Test | ||
void shouldCreateContainer() { | ||
// given | ||
JdbcTemplate jdbcTemplate = new JdbcTemplate(new SimpleDriverDataSource( | ||
getDriver(properties.getUrl()), | ||
properties.getUrl(), | ||
properties.getUsername(), | ||
properties.getPassword())); | ||
|
||
// when | ||
String actual = jdbcTemplate.queryForObject("SELECT database();", String.class); | ||
|
||
// then | ||
then(actual).isEqualTo("TestDatabase"); | ||
} | ||
|
||
private Driver getDriver(String jdbcUrl) { | ||
try { | ||
return DriverManager.getDriver(jdbcUrl); | ||
} catch (SQLException e) { | ||
throw new IllegalArgumentException(e); | ||
} | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
.../com/infobip/testcontainers/spring/mysql/MySQLContainerInitializerWithInitScriptTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.infobip.testcontainers.spring.mysql; | ||
|
||
import com.infobip.testcontainers.TestBase; | ||
import lombok.AllArgsConstructor; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.datasource.SimpleDriverDataSource; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
import java.sql.Driver; | ||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
|
||
import static org.assertj.core.api.BDDAssertions.then; | ||
|
||
@AllArgsConstructor | ||
@ActiveProfiles("init-script") | ||
class MySQLContainerInitializerWithInitScriptTest extends TestBase { | ||
|
||
private final DataSourceProperties properties; | ||
|
||
@Test | ||
void shouldCreateContainerWithInitScript() { | ||
// given | ||
JdbcTemplate jdbcTemplate = new JdbcTemplate(new SimpleDriverDataSource( | ||
getDriver(properties.getUrl()), | ||
properties.getUrl(), | ||
properties.getUsername(), | ||
properties.getPassword())); | ||
|
||
// when | ||
String actual = jdbcTemplate.queryForObject("SELECT name FROM test_table LIMIT 1", String.class); | ||
|
||
// then | ||
then(actual).isEqualTo("Test"); | ||
} | ||
|
||
private Driver getDriver(String jdbcUrl) { | ||
try { | ||
return DriverManager.getDriver(jdbcUrl); | ||
} catch (SQLException e) { | ||
throw new IllegalArgumentException(e); | ||
} | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
...va/com/infobip/testcontainers/spring/mysql/MySQLContainerInitializerWithReusableTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.infobip.testcontainers.spring.mysql; | ||
|
||
import com.infobip.testcontainers.ReusableTestBase; | ||
import com.infobip.testcontainers.spring.mysql.MySQLContainerWrapper; | ||
import lombok.AllArgsConstructor; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
import static org.assertj.core.api.BDDAssertions.then; | ||
|
||
@AllArgsConstructor | ||
@ActiveProfiles("reusable") | ||
class MySQLContainerInitializerWithReusableTest extends ReusableTestBase { | ||
|
||
private final MySQLContainerWrapper wrapper; | ||
private final int port = MySQLContainerWrapper.MYSQL_PORT; | ||
|
||
@Test | ||
void shouldReuseContainer() { | ||
// given | ||
var givenContainer = new MySQLContainerWrapper("TestDatabase"); | ||
givenContainer.withReuse(true); | ||
|
||
// when | ||
givenContainer.start(); | ||
|
||
// then | ||
then(givenContainer.getMappedPort(port)).isEqualTo(wrapper.getMappedPort(port)); | ||
} | ||
|
||
} |
Oops, something went wrong.