|
| 1 | +// Copyright (c) Cosmo Tech. |
| 2 | +// Licensed under the MIT license. |
| 3 | +package com.cosmotech.api.tests |
| 4 | + |
| 5 | +import com.redis.om.spring.annotations.EnableRedisDocumentRepositories |
| 6 | +import com.redis.testcontainers.RedisServer |
| 7 | +import com.redis.testcontainers.RedisStackContainer |
| 8 | +import com.redis.testcontainers.junit.AbstractTestcontainersRedisTestBase |
| 9 | +import org.junit.jupiter.api.AfterAll |
| 10 | +import org.junit.jupiter.api.BeforeAll |
| 11 | +import org.springframework.test.context.DynamicPropertyRegistry |
| 12 | +import org.springframework.test.context.DynamicPropertySource |
| 13 | +import org.testcontainers.containers.PostgreSQLContainer |
| 14 | +import org.testcontainers.containers.PostgreSQLContainer.POSTGRESQL_PORT |
| 15 | +import org.testcontainers.containers.localstack.LocalStackContainer |
| 16 | +import org.testcontainers.utility.DockerImageName |
| 17 | +import org.testcontainers.utility.MountableFile |
| 18 | + |
| 19 | +@EnableRedisDocumentRepositories(basePackages = ["com.cosmotech"]) |
| 20 | +open class CsmTestBase : AbstractTestcontainersRedisTestBase() { |
| 21 | + |
| 22 | + companion object { |
| 23 | + private const val ADMIN_USER_CREDENTIALS = "adminusertest" |
| 24 | + private const val READER_USER_CREDENTIALS = "readusertest" |
| 25 | + private const val WRITER_USER_CREDENTIALS = "writeusertest" |
| 26 | + private const val DEFAULT_REDIS_PORT = 6379 |
| 27 | + private const val LOCALSTACK_FULL_IMAGE_NAME = "localstack/localstack:3.5.0" |
| 28 | + |
| 29 | + var postgres: PostgreSQLContainer<*> = |
| 30 | + PostgreSQLContainer("postgres:alpine3.19") |
| 31 | + .withCopyFileToContainer( |
| 32 | + MountableFile.forClasspathResource("init-db.sql"), "/docker-entrypoint-initdb.d/") |
| 33 | + |
| 34 | + var redisStackServer = RedisStackContainer(RedisStackContainer.DEFAULT_IMAGE_NAME) |
| 35 | + |
| 36 | + val localStackServer = |
| 37 | + LocalStackContainer(DockerImageName.parse(LOCALSTACK_FULL_IMAGE_NAME)) |
| 38 | + .withServices(LocalStackContainer.Service.S3) |
| 39 | + |
| 40 | + init { |
| 41 | + redisStackServer.start() |
| 42 | + postgres.start() |
| 43 | + localStackServer.start() |
| 44 | + localStackServer.execInContainer("awslocal", "s3", "mb", "s3://test-bucket") |
| 45 | + } |
| 46 | + |
| 47 | + @JvmStatic |
| 48 | + @DynamicPropertySource |
| 49 | + fun connectionProperties(registry: DynamicPropertyRegistry) { |
| 50 | + initPostgresConfiguration(registry) |
| 51 | + initRedisConfiguration(registry) |
| 52 | + initS3Configuration(registry) |
| 53 | + } |
| 54 | + |
| 55 | + private fun initRedisConfiguration(registry: DynamicPropertyRegistry) { |
| 56 | + val containerIp = |
| 57 | + redisStackServer.containerInfo.networkSettings.networks.entries |
| 58 | + .elementAt(0) |
| 59 | + .value |
| 60 | + .ipAddress |
| 61 | + |
| 62 | + registry.add("spring.data.redis.host") { containerIp } |
| 63 | + registry.add("spring.data.redis.port") { DEFAULT_REDIS_PORT } |
| 64 | + } |
| 65 | + |
| 66 | + private fun initS3Configuration(registry: DynamicPropertyRegistry) { |
| 67 | + registry.add("spring.cloud.aws.s3.endpoint") { localStackServer.endpoint } |
| 68 | + registry.add("spring.cloud.aws.credentials.access-key") { localStackServer.accessKey } |
| 69 | + registry.add("spring.cloud.aws.credentials.secret-key") { localStackServer.secretKey } |
| 70 | + registry.add("spring.cloud.aws.s3.region") { localStackServer.region } |
| 71 | + } |
| 72 | + |
| 73 | + private fun initPostgresConfiguration(registry: DynamicPropertyRegistry) { |
| 74 | + registry.add("csm.platform.internalResultServices.storage.host") { postgres.host } |
| 75 | + registry.add("csm.platform.internalResultServices.storage.port") { |
| 76 | + postgres.getMappedPort(POSTGRESQL_PORT) |
| 77 | + } |
| 78 | + registry.add("csm.platform.internalResultServices.storage.admin.username") { |
| 79 | + ADMIN_USER_CREDENTIALS |
| 80 | + } |
| 81 | + registry.add("csm.platform.internalResultServices.storage.admin.password") { |
| 82 | + ADMIN_USER_CREDENTIALS |
| 83 | + } |
| 84 | + registry.add("csm.platform.internalResultServices.storage.writer.username") { |
| 85 | + WRITER_USER_CREDENTIALS |
| 86 | + } |
| 87 | + registry.add("csm.platform.internalResultServices.storage.writer.password") { |
| 88 | + WRITER_USER_CREDENTIALS |
| 89 | + } |
| 90 | + registry.add("csm.platform.internalResultServices.storage.reader.username") { |
| 91 | + READER_USER_CREDENTIALS |
| 92 | + } |
| 93 | + registry.add("csm.platform.internalResultServices.storage.reader.password") { |
| 94 | + READER_USER_CREDENTIALS |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @BeforeAll |
| 100 | + fun beforeAll() { |
| 101 | + redisStackServer.start() |
| 102 | + postgres.start() |
| 103 | + } |
| 104 | + |
| 105 | + @AfterAll |
| 106 | + fun afterAll() { |
| 107 | + postgres.stop() |
| 108 | + } |
| 109 | + |
| 110 | + override fun redisServers(): MutableCollection<RedisServer> { |
| 111 | + return mutableListOf(redisStackServer) |
| 112 | + } |
| 113 | +} |
0 commit comments