Skip to content

Commit

Permalink
Merge pull request #32 from Kilemonn/add-support-for-ms-sql
Browse files Browse the repository at this point in the history
Add support for ms sql
  • Loading branch information
Kilemonn authored Jul 2, 2024
2 parents 4e8e768 + 3bc7f5f commit f7b78fd
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
11 changes: 7 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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}")
Expand All @@ -51,6 +48,12 @@ 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")

// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
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<ConfigurableApplicationContext>
{
/**
* 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<String, String>()
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;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",

// 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)
}
}

/**
* 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()
}
}

0 comments on commit f7b78fd

Please sign in to comment.