From 948354d309c5deb4f8059fba230d1f191d3bbc57 Mon Sep 17 00:00:00 2001 From: Kilemonn Date: Tue, 2 Jul 2024 16:23:10 +0900 Subject: [PATCH 1/5] Add test for Microsoft server + driver/dependency. --- build.gradle.kts | 10 ++- .../queue/sql/MicrosoftSqlMultiQueueTest.kt | 90 +++++++++++++++++++ 2 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 src/test/kotlin/au/kilemon/messagequeue/queue/sql/MicrosoftSqlMultiQueueTest.kt diff --git a/build.gradle.kts b/build.gradle.kts index f0447d0..64bde27 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -30,9 +30,6 @@ dependencies { implementation("org.springframework.boot:spring-boot-starter-data-redis:${springVersion}") // JPA dependency implementation("org.springframework.boot:spring-boot-starter-data-jpa:${springVersion}") - // No SQL drivers - // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mongodb - implementation("org.springframework.boot:spring-boot-starter-data-mongodb:${springVersion}") // https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-starter-webmvc-ui implementation("org.springdoc:springdoc-openapi-starter-webmvc-api:${springDocVersion}") @@ -51,6 +48,13 @@ dependencies { implementation("com.mysql:mysql-connector-j:8.4.0") // https://mvnrepository.com/artifact/org.postgresql/postgresql implementation("org.postgresql:postgresql:42.7.3") + // https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc + implementation("com.microsoft.sqlserver:mssql-jdbc:12.6.3.jre11") +// implementation("com.microsoft.sqlserver:mssql-jdbc:12.7.0") + + // No SQL drivers + // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mongodb + implementation("org.springframework.boot:spring-boot-starter-data-mongodb:${springVersion}") // JWT token // https://mvnrepository.com/artifact/com.auth0/java-jwt diff --git a/src/test/kotlin/au/kilemon/messagequeue/queue/sql/MicrosoftSqlMultiQueueTest.kt b/src/test/kotlin/au/kilemon/messagequeue/queue/sql/MicrosoftSqlMultiQueueTest.kt new file mode 100644 index 0000000..41d4017 --- /dev/null +++ b/src/test/kotlin/au/kilemon/messagequeue/queue/sql/MicrosoftSqlMultiQueueTest.kt @@ -0,0 +1,90 @@ +package au.kilemon.messagequeue.queue.sql + +import au.kilemon.messagequeue.configuration.QueueConfiguration +import au.kilemon.messagequeue.logging.LoggingConfiguration +import au.kilemon.messagequeue.queue.MultiQueueTest +import au.kilemon.messagequeue.queue.sql.MicrosoftSqlMultiQueueTest.Companion.MS_SQL_CONTAINER +import org.junit.jupiter.api.AfterAll +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.BeforeEach +import org.springframework.boot.test.util.TestPropertyValues +import org.springframework.context.ApplicationContextInitializer +import org.springframework.context.ConfigurableApplicationContext +import org.springframework.context.annotation.Import +import org.springframework.test.context.ContextConfiguration +import org.testcontainers.containers.GenericContainer +import org.testcontainers.utility.DockerImageName + +/** + * A test class for the [MS_SQL_CONTAINER] to ensure the [SqlMultiQueue] works as expected with this underlying data storage DB. + * + * @author github.com/Kilemonn + */ +@ContextConfiguration(initializers = [MicrosoftSqlMultiQueueTest.Initializer::class]) +@Import( *[QueueConfiguration::class, LoggingConfiguration::class, MultiQueueTest.MultiQueueTestConfiguration::class] ) +class MicrosoftSqlMultiQueueTest: SqlMultiQueueTest() +{ + companion object + { + lateinit var database: GenericContainer<*> + + private const val MS_SQL_CONTAINER = "mcr.microsoft.com/mssql/server:2022-CU13-ubuntu-22.04" + private const val MS_CONTAINER_PORT = 1433 + + /** + * Stop the container at the end of all the tests. + */ + @AfterAll + @JvmStatic + fun afterClass() + { + database.stop() + } + } + + /** + * The test initialiser for [MicrosoftSqlMultiQueueTest] to initialise the container and test properties. + * + * @author github.com/Kilemonn + */ + internal class Initializer : ApplicationContextInitializer + { + /** + * Force start the container, so we can place its host and dynamic ports into the system properties. + * + * Set the environment variables before any of the beans are initialised. + */ + override fun initialize(configurableApplicationContext: ConfigurableApplicationContext) + { + val password = "This154Strong" // container has password requirements + val username = "sa" + val envMap = HashMap() + envMap["MSSQL_SA_PASSWORD"] = password + envMap["ACCEPT_EULA"] = "Y" + + database = GenericContainer(DockerImageName.parse(MS_SQL_CONTAINER)) + .withExposedPorts(MS_CONTAINER_PORT).withReuse(false).withEnv(envMap) + database.start() + + // "jdbc:sqlserver://localhost:1433;databaseName=dbname;user=username;password=*****;" + val endpoint = "jdbc:sqlserver://${database.host}:${database.getMappedPort(MS_CONTAINER_PORT)};encrypt=false" + + TestPropertyValues.of( + "spring.datasource.url=$endpoint", + "spring.datasource.username=$username", + "spring.datasource.password=$password", + "hibernate.dialect=org.hibernate.dialect.SQLServerDialect" + ).applyTo(configurableApplicationContext.environment) + } + } + + /** + * Check the container is running before each test as it's required for the methods to access the [SqlMultiQueue]. + */ + @BeforeEach + fun beforeEach() + { + Assertions.assertTrue(database.isRunning) + multiQueue.clear() + } +} \ No newline at end of file From d8e304aed3af51b0893f1d6ea8856cf38e23c340 Mon Sep 17 00:00:00 2001 From: Kilemonn Date: Tue, 2 Jul 2024 17:01:19 +0900 Subject: [PATCH 2/5] Remove dialect context property in ms-sql test. --- .../messagequeue/queue/sql/MicrosoftSqlMultiQueueTest.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/kotlin/au/kilemon/messagequeue/queue/sql/MicrosoftSqlMultiQueueTest.kt b/src/test/kotlin/au/kilemon/messagequeue/queue/sql/MicrosoftSqlMultiQueueTest.kt index 41d4017..c54c7d8 100644 --- a/src/test/kotlin/au/kilemon/messagequeue/queue/sql/MicrosoftSqlMultiQueueTest.kt +++ b/src/test/kotlin/au/kilemon/messagequeue/queue/sql/MicrosoftSqlMultiQueueTest.kt @@ -72,8 +72,7 @@ class MicrosoftSqlMultiQueueTest: SqlMultiQueueTest() TestPropertyValues.of( "spring.datasource.url=$endpoint", "spring.datasource.username=$username", - "spring.datasource.password=$password", - "hibernate.dialect=org.hibernate.dialect.SQLServerDialect" + "spring.datasource.password=$password" ).applyTo(configurableApplicationContext.environment) } } From 25105044652549ddf4887a83a5df93e686ace84c Mon Sep 17 00:00:00 2001 From: Kilemonn Date: Tue, 2 Jul 2024 19:07:18 +0900 Subject: [PATCH 3/5] Add dialect explicitly (spring.jpa.properties.hibernate.dialect) to resolve test failing when all unit tests are run. --- build.gradle.kts | 1 - .../messagequeue/queue/sql/MicrosoftSqlMultiQueueTest.kt | 8 +++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 64bde27..a4d22e4 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -50,7 +50,6 @@ dependencies { implementation("org.postgresql:postgresql:42.7.3") // https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc implementation("com.microsoft.sqlserver:mssql-jdbc:12.6.3.jre11") -// implementation("com.microsoft.sqlserver:mssql-jdbc:12.7.0") // No SQL drivers // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mongodb diff --git a/src/test/kotlin/au/kilemon/messagequeue/queue/sql/MicrosoftSqlMultiQueueTest.kt b/src/test/kotlin/au/kilemon/messagequeue/queue/sql/MicrosoftSqlMultiQueueTest.kt index c54c7d8..4fbd495 100644 --- a/src/test/kotlin/au/kilemon/messagequeue/queue/sql/MicrosoftSqlMultiQueueTest.kt +++ b/src/test/kotlin/au/kilemon/messagequeue/queue/sql/MicrosoftSqlMultiQueueTest.kt @@ -66,13 +66,15 @@ class MicrosoftSqlMultiQueueTest: SqlMultiQueueTest() .withExposedPorts(MS_CONTAINER_PORT).withReuse(false).withEnv(envMap) database.start() - // "jdbc:sqlserver://localhost:1433;databaseName=dbname;user=username;password=*****;" - val endpoint = "jdbc:sqlserver://${database.host}:${database.getMappedPort(MS_CONTAINER_PORT)};encrypt=false" + // "jdbc:sqlserver://localhost:1433;databaseName=dbname;encrypt=false" + val endpoint = "jdbc:sqlserver://${database.host}:${database.getMappedPort(MS_CONTAINER_PORT)};encrypt=true;trustServerCertificate=true" TestPropertyValues.of( "spring.datasource.url=$endpoint", "spring.datasource.username=$username", - "spring.datasource.password=$password" + "spring.datasource.password=$password", + + "spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServerDialect" ).applyTo(configurableApplicationContext.environment) } } From 15716516d587ea1c3ec39ebfc39d5cde234fb606 Mon Sep 17 00:00:00 2001 From: Kilemonn Date: Tue, 2 Jul 2024 19:09:33 +0900 Subject: [PATCH 4/5] Bump to 0.3.3. --- build.gradle.kts | 2 +- .../kotlin/au/kilemon/messagequeue/MessageQueueApplication.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index a4d22e4..2bc2548 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,7 +14,7 @@ plugins { group = "au.kilemon" // Make sure version matches version defined in MessageQueueApplication -version = "0.3.2" +version = "0.3.3" java.sourceCompatibility = JavaVersion.VERSION_17 repositories { diff --git a/src/main/kotlin/au/kilemon/messagequeue/MessageQueueApplication.kt b/src/main/kotlin/au/kilemon/messagequeue/MessageQueueApplication.kt index 64bdbfb..8a0c595 100644 --- a/src/main/kotlin/au/kilemon/messagequeue/MessageQueueApplication.kt +++ b/src/main/kotlin/au/kilemon/messagequeue/MessageQueueApplication.kt @@ -17,7 +17,7 @@ open class MessageQueueApplication /** * Application version number, make sure this matches what is defined in `build.gradle.kts`. */ - const val VERSION: String = "0.3.2" + const val VERSION: String = "0.3.3" } } From 3bc7f5f7a4cd271b78021952f80e711d516ba2ee Mon Sep 17 00:00:00 2001 From: Kilemonn Date: Tue, 2 Jul 2024 19:35:29 +0900 Subject: [PATCH 5/5] Add comment explaining why the dialect is being set for MS SQL server only. --- .../messagequeue/queue/sql/MicrosoftSqlMultiQueueTest.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/kotlin/au/kilemon/messagequeue/queue/sql/MicrosoftSqlMultiQueueTest.kt b/src/test/kotlin/au/kilemon/messagequeue/queue/sql/MicrosoftSqlMultiQueueTest.kt index 4fbd495..e8d0095 100644 --- a/src/test/kotlin/au/kilemon/messagequeue/queue/sql/MicrosoftSqlMultiQueueTest.kt +++ b/src/test/kotlin/au/kilemon/messagequeue/queue/sql/MicrosoftSqlMultiQueueTest.kt @@ -74,6 +74,8 @@ class MicrosoftSqlMultiQueueTest: SqlMultiQueueTest() "spring.datasource.username=$username", "spring.datasource.password=$password", + // We have to explicitly set the dialect here since without it spring is unable to determine + // the dialect to use when we run all tests. "spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServerDialect" ).applyTo(configurableApplicationContext.environment) }