-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from asarkar/feature/issue-1
Close #1: Support EmbeddedRedisConfigurer bean
- Loading branch information
Showing
10 changed files
with
148 additions
and
54 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
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
50 changes: 50 additions & 0 deletions
50
src/test/kotlin/com/asarkar/spring/test/redis/EmbeddedRedisConfigurerBeanTest.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,50 @@ | ||
package com.asarkar.spring.test.redis | ||
|
||
import io.lettuce.core.RedisClient | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.boot.test.context.TestConfiguration | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Import | ||
import redis.embedded.RedisServerBuilder | ||
|
||
@SpringBootTest | ||
@AutoConfigureEmbeddedRedis( | ||
port = 0, | ||
serverConfigurerClass = "com.asarkar.spring.test.redis.TestEmbeddedRedisConfigurerBean" | ||
) | ||
@Import(EmbeddedRedisConfigurerBeanTestConfiguration::class) | ||
class EmbeddedRedisConfigurerBeanTest { | ||
@Value("\${embedded-redis.port:-1}") | ||
private var port: Int = -1 | ||
|
||
@Autowired | ||
private lateinit var embeddedRedisConfigurer: TestEmbeddedRedisConfigurerBean | ||
|
||
@Test | ||
fun testEmbeddedRedisConfigurer() { | ||
val redisClient = RedisClient | ||
.create("redis://localhost:$port/") | ||
val syncCommands = redisClient.connect().sync() | ||
syncCommands.set("key", "value") | ||
assertThat(syncCommands.get("key")).isEqualTo("value") | ||
assertThat(embeddedRedisConfigurer.called).isTrue | ||
} | ||
} | ||
|
||
@TestConfiguration | ||
open class EmbeddedRedisConfigurerBeanTestConfiguration { | ||
@Bean | ||
open fun embeddedRedisConfigurer() = TestEmbeddedRedisConfigurerBean() | ||
} | ||
|
||
class TestEmbeddedRedisConfigurerBean : EmbeddedRedisConfigurer { | ||
var called: Boolean = false | ||
|
||
override fun configure(builder: RedisServerBuilder) { | ||
called = true | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/test/kotlin/com/asarkar/spring/test/redis/EmbeddedRedisConfigurerTest.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,69 @@ | ||
package com.asarkar.spring.test.redis | ||
|
||
import io.lettuce.core.RedisClient | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.AfterEach | ||
import org.junit.jupiter.api.Test | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import redis.embedded.RedisServerBuilder | ||
import java.nio.file.Files | ||
import java.nio.file.Paths | ||
import kotlin.random.Random | ||
|
||
private val charPool: List<Char> = ('a'..'z') + ('A'..'Z') + ('0'..'9') | ||
private val file = Paths.get(AutoConfigureWithRandomPortsTest::class.java.getResource("/").toURI()) | ||
.let { testPath -> | ||
generateSequence(testPath) { | ||
if (Files.isDirectory(it) && Files.exists(it.resolve("build.gradle.kts"))) { | ||
null | ||
} else { | ||
it.parent | ||
} | ||
} | ||
.take(10) // should be plenty | ||
.toList() | ||
.last() | ||
.resolve("build") | ||
.resolve( | ||
(1..6) | ||
.map { Random.nextInt(0, charPool.size) } | ||
.map(charPool::get) | ||
.joinToString("") | ||
) | ||
} | ||
|
||
@SpringBootTest | ||
@AutoConfigureEmbeddedRedis( | ||
port = 0, | ||
serverConfigurerClass = "com.asarkar.spring.test.redis.TestEmbeddedRedisConfigurer" | ||
) | ||
class EmbeddedRedisConfigurerTest { | ||
@Value("\${embedded-redis.port:-1}") | ||
private var port: Int = -1 | ||
|
||
@Test | ||
fun testEmbeddedRedisConfigurer() { | ||
val redisClient = RedisClient | ||
.create("redis://localhost:$port/") | ||
val syncCommands = redisClient.connect().sync() | ||
syncCommands.set("key", "value") | ||
assertThat(syncCommands.get("key")).isEqualTo("value") | ||
assertThat(Files.exists(file)).isTrue | ||
} | ||
|
||
@AfterEach | ||
fun afterEach() { | ||
Files.deleteIfExists(file) | ||
} | ||
} | ||
|
||
class TestEmbeddedRedisConfigurer : EmbeddedRedisConfigurer { | ||
private val log = LoggerFactory.getLogger(TestEmbeddedRedisConfigurer::class.java) | ||
|
||
override fun configure(builder: RedisServerBuilder) { | ||
Files.createFile(file) | ||
.also { log.info("Created file: {}", it.toAbsolutePath()) } | ||
} | ||
} |