|
| 1 | +package org.jenkinsci.plugins.database.mysql; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.instanceOf; |
| 5 | +import static org.hamcrest.Matchers.is; |
| 6 | + |
| 7 | +import hudson.util.Secret; |
| 8 | +import java.io.IOException; |
| 9 | +import org.jenkinsci.plugins.database.GlobalDatabaseConfiguration; |
| 10 | +import org.jenkinsci.plugins.database.mysql.MySQLDatabase; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.jvnet.hudson.test.JenkinsRule; |
| 13 | +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; |
| 14 | +import org.testcontainers.containers.MySQLContainer; |
| 15 | +import org.testcontainers.junit.jupiter.Container; |
| 16 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 17 | + |
| 18 | +@WithJenkins |
| 19 | +@Testcontainers |
| 20 | +public class MySQLDatabaseTest { |
| 21 | + |
| 22 | + public static final String TEST_IMAGE = "mysql:8.2.0"; |
| 23 | + |
| 24 | + @Container |
| 25 | + private static final MySQLContainer<?> mysql = new MySQLContainer<>(TEST_IMAGE); |
| 26 | + |
| 27 | + public void setConfiguration() throws IOException { |
| 28 | + MySQLDatabase database = new MySQLDatabase( |
| 29 | + mysql.getHost() + ":" + mysql.getMappedPort(3306), |
| 30 | + mysql.getDatabaseName(), |
| 31 | + mysql.getUsername(), |
| 32 | + Secret.fromString(mysql.getPassword()), |
| 33 | + null); |
| 34 | + database.setValidationQuery("SELECT 1"); |
| 35 | + GlobalDatabaseConfiguration.get().setDatabase(database); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void shouldSetConfiguration(JenkinsRule j) throws IOException { |
| 40 | + setConfiguration(); |
| 41 | + assertThat(GlobalDatabaseConfiguration.get().getDatabase(), instanceOf(MySQLDatabase.class)); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void shouldConstructDatabase(JenkinsRule j) throws IOException { |
| 46 | + MySQLDatabase database = new MySQLDatabase( |
| 47 | + mysql.getHost() + ":" + mysql.getMappedPort(3306), |
| 48 | + mysql.getDatabaseName(), |
| 49 | + mysql.getUsername(), |
| 50 | + Secret.fromString(mysql.getPassword()), |
| 51 | + null); |
| 52 | + assertThat(database.getDescriptor().getDisplayName(), is("MySQL")); |
| 53 | + assertThat( |
| 54 | + database.getJdbcUrl(), |
| 55 | + is("jdbc:mysql://" + mysql.getHost() + ":" + mysql.getMappedPort(3306) + "/" |
| 56 | + + mysql.getDatabaseName() + "")); |
| 57 | + assertThat(database.getDriverClass(), is(com.mysql.cj.jdbc.Driver.class)); |
| 58 | + } |
| 59 | +} |
0 commit comments