generated from ministryofjustice/hmpps-template-kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
4b5550a
commit b86d0ca
Showing
9 changed files
with
165 additions
and
28 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
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
Empty file.
17 changes: 17 additions & 0 deletions
17
src/test/kotlin/uk/gov/justice/digital/hmpps/healthandmedicationapi/config/FixedClock.kt
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,17 @@ | ||
package uk.gov.justice.digital.hmpps.healthandmedicationapi.config | ||
|
||
import java.time.Clock | ||
import java.time.Instant | ||
import java.time.ZoneId | ||
import java.time.temporal.TemporalAmount | ||
|
||
class FixedClock(var instant: Instant, private var zone: ZoneId) : Clock() { | ||
|
||
fun elapse(amountToAdd: TemporalAmount) { | ||
instant += amountToAdd | ||
} | ||
|
||
override fun instant(): Instant = this.instant | ||
override fun withZone(zone: ZoneId): Clock = this.apply { this.zone = zone } | ||
override fun getZone(): ZoneId = this.zone | ||
} |
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
33 changes: 33 additions & 0 deletions
33
src/test/kotlin/uk/gov/justice/digital/hmpps/healthandmedicationapi/integration/TestBase.kt
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,33 @@ | ||
package uk.gov.justice.digital.hmpps.healthandmedicationapi.integration | ||
|
||
import org.springframework.test.context.ActiveProfiles | ||
import org.springframework.test.context.DynamicPropertyRegistry | ||
import org.springframework.test.context.DynamicPropertySource | ||
import uk.gov.justice.digital.hmpps.healthandmedicationapi.config.FixedClock | ||
import uk.gov.justice.digital.hmpps.healthandmedicationapi.integration.testcontainers.PostgresContainer | ||
import java.time.Instant | ||
import java.time.ZoneId | ||
|
||
@ActiveProfiles("test") | ||
abstract class TestBase { | ||
|
||
companion object { | ||
val clock: FixedClock = FixedClock( | ||
Instant.parse("2024-06-14T09:10:11.123+01:00"), | ||
ZoneId.of("Europe/London"), | ||
) | ||
|
||
private val pgContainer = PostgresContainer.instance | ||
|
||
@JvmStatic | ||
@DynamicPropertySource | ||
fun properties(registry: DynamicPropertyRegistry) { | ||
pgContainer?.run { | ||
registry.add("spring.datasource.url", pgContainer::getJdbcUrl) | ||
registry.add("spring.flyway.url", pgContainer::getJdbcUrl) | ||
registry.add("spring.flyway.user", pgContainer::getUsername) | ||
registry.add("spring.flyway.password", pgContainer::getPassword) | ||
} | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...tice/digital/hmpps/healthandmedicationapi/integration/testcontainers/PostgresContainer.kt
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,40 @@ | ||
package uk.gov.justice.digital.hmpps.healthandmedicationapi.integration.testcontainers | ||
|
||
import org.slf4j.LoggerFactory | ||
import org.testcontainers.containers.PostgreSQLContainer | ||
import org.testcontainers.containers.wait.strategy.Wait | ||
import java.io.IOException | ||
import java.net.ServerSocket | ||
|
||
object PostgresContainer { | ||
val instance: PostgreSQLContainer<Nothing>? by lazy { startPostgresqlContainer() } | ||
|
||
private fun startPostgresqlContainer(): PostgreSQLContainer<Nothing>? { | ||
if (isPostgresRunning()) { | ||
log.warn("Using existing Postgres database") | ||
return null | ||
} | ||
log.info("Creating a Postgres database") | ||
return PostgreSQLContainer<Nothing>("postgres").apply { | ||
withEnv("HOSTNAME_EXTERNAL", "localhost") | ||
withEnv("PORT_EXTERNAL", "5432") | ||
withDatabaseName("health-and-medication-data") | ||
withUsername("health-and-medication-data") | ||
withPassword("health-and-medication-data") | ||
setWaitStrategy(Wait.forListeningPort()) | ||
withReuse(true) | ||
|
||
start() | ||
} | ||
} | ||
|
||
private fun isPostgresRunning(): Boolean = | ||
try { | ||
val serverSocket = ServerSocket(5432) | ||
serverSocket.localPort == 0 | ||
} catch (e: IOException) { | ||
true | ||
} | ||
|
||
private val log = LoggerFactory.getLogger(this::class.java) | ||
} |