Created
November 25, 2019 07:39
-
-
Save tyagiakhilesh/5b42a90a80d7283098e4bccb18f6b039 to your computer and use it in GitHub Desktop.
Loading data in mysql container managed by testconatiners.
This file contains hidden or 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
import my.personal.package.LiquibaseRunner; | |
import org.apache.commons.dbcp.BasicDataSource; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.testcontainers.containers.Container.ExecResult; | |
import org.testcontainers.containers.MySQLContainer; | |
import org.testcontainers.containers.output.Slf4jLogConsumer; | |
import org.testcontainers.junit.jupiter.Container; | |
import org.testcontainers.junit.jupiter.Testcontainers; | |
import org.testcontainers.utility.MountableFile; | |
import java.io.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.nio.file.FileVisitResult; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.nio.file.SimpleFileVisitor; | |
import java.nio.file.attribute.BasicFileAttributes; | |
import java.util.Optional; | |
@Testcontainers | |
public class LoadDataInMySQLInTestContainers { | |
private static final Logger logger = LoggerFactory.getLogger(TestSetup.class); | |
private static final Logger loggerMySql = LoggerFactory.getLogger("- MySQL - "); | |
@Container | |
protected static MySQLContainer MY_SQL_CONTAINER = new MySQLContainer(); | |
protected static void setUpBeforeAll() throws IOException, InterruptedException { | |
final Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(loggerMySql); | |
MY_SQL_CONTAINER.followOutput(logConsumer); | |
final File script = File.createTempFile("script", ".sql"); | |
script.deleteOnExit(); | |
try (FileWriter fw = new FileWriter(script, true); | |
BufferedWriter bw = new BufferedWriter(fw); | |
PrintWriter out = new PrintWriter(bw)) { | |
Files.walkFileTree(Paths.get("src/test/resources/data"), new SimpleFileVisitor<Path>() { | |
@Override | |
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) | |
throws IOException { | |
BufferedReader in = new BufferedReader(new FileReader(file.toFile())); | |
String content; | |
while ((content = in.readLine()) != null) { | |
out.write(content); | |
out.write(String.format("%n")); | |
} | |
in.close(); | |
return FileVisitResult.CONTINUE; | |
} | |
}); | |
} | |
final BasicDataSource dataSource = new BasicDataSource(); | |
dataSource.setUrl(MY_SQL_CONTAINER.getJdbcUrl()); | |
dataSource.setUsername(MY_SQL_CONTAINER.getUsername()); | |
dataSource.setPassword(MY_SQL_CONTAINER.getPassword()); | |
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); | |
final LiquibaseRunner runner = new LiquibaseRunner(dataSource); | |
runner.runChangeLog("changelog.xml", Optional.empty()); | |
runner.runChangeLog("db/changelogs/changelog-master.xml", Optional.empty()); | |
MY_SQL_CONTAINER.copyFileToContainer(MountableFile.forHostPath(script.getPath()), "/tmp/data.sql"); | |
final String command = String.format("mysql -u%s -p%s -D%s < /tmp/data.sql", | |
MY_SQL_CONTAINER.getUsername(), MY_SQL_CONTAINER.getPassword(), MY_SQL_CONTAINER.getDatabaseName()); | |
final ExecResult result = MY_SQL_CONTAINER.execInContainer("bash", "-c", command); | |
logger.info("Result is: {}", result.getExitCode()); | |
if (result.getExitCode() != 0) { | |
logger.error("StdErr is: {}", result.getStderr()); | |
logger.error("StdOut is: {}", result.getStdout()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment