Skip to content

Commit 911ffd9

Browse files
committed
🎨 Sonar
1 parent 1e884b0 commit 911ffd9

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

src/main/kotlin/io/github/tobi/laa/spring/boot/embedded/redis/highavailability/RedisHighAvailabilityContextCustomizer.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ internal class RedisHighAvailabilityContextCustomizer(
4545
private val customizer = config.customizer.map { c -> c.createInstance() }.toList()
4646
private val executableProvider = if (config.executeInDirectory.isNotEmpty()) {
4747
val osArch = detectOSandArchitecture()
48-
val resourcePath = newProvidedVersionsMap()[osArch]!!
48+
val resourcePath = newProvidedVersionsMap()[osArch]
49+
?: throw UnsupportedOperationException("Unsupported OS: $osArch")
4950
val executable = Path(config.executeInDirectory, resourcePath)
5051
ExecutableProvider {
5152
if (exists(executable)) {

src/test/kotlin/io/github/tobi/laa/spring/boot/embedded/redis/highavailability/RedisHighAvailabilityContextCustomizerTest.kt

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,22 @@ import io.mockk.impl.annotations.RelaxedMockK
1010
import io.mockk.junit5.MockKExtension
1111
import io.mockk.mockk
1212
import io.mockk.mockkConstructor
13+
import io.mockk.mockkStatic
1314
import org.assertj.core.api.Assertions.assertThat
1415
import org.assertj.core.api.Assertions.assertThatThrownBy
16+
import org.junit.jupiter.api.BeforeAll
1517
import org.junit.jupiter.api.BeforeEach
1618
import org.junit.jupiter.api.DisplayName
1719
import org.junit.jupiter.api.Test
1820
import org.junit.jupiter.api.extension.ExtendWith
1921
import org.springframework.context.annotation.AnnotationConfigApplicationContext
2022
import redis.embedded.Redis
23+
import redis.embedded.model.Architecture.aarch64
24+
import redis.embedded.model.OS.WINDOWS
25+
import redis.embedded.model.OsArchitecture
26+
import java.io.File
27+
28+
private const val CUSTOM_EXECUTION_DIR = "build/custom-execution-dir-high-availability"
2129

2230
@DisplayName("Tests for RedisHighAvailabilityContextCustomizer")
2331
@ExtendWith(MockKExtension::class)
@@ -30,6 +38,14 @@ internal class RedisHighAvailabilityContextCustomizerTest {
3038

3139
private lateinit var givenCustomizer: RedisHighAvailabilityContextCustomizer
3240

41+
companion object {
42+
@BeforeAll
43+
@JvmStatic
44+
fun beforeAll() {
45+
File(CUSTOM_EXECUTION_DIR).mkdirs()
46+
}
47+
}
48+
3349
@BeforeEach
3450
fun init() {
3551
givenCustomizer = RedisHighAvailabilityContextCustomizer(config, portProvider)
@@ -56,6 +72,24 @@ internal class RedisHighAvailabilityContextCustomizerTest {
5672
assertThat(comparedToDifferentClass).isFalse()
5773
}
5874

75+
@Test
76+
@DisplayName("RedisHighAvailabilityContextCustomizer should throw when unsupported OS was detected")
77+
fun unsupportedOS_shouldThrow() {
78+
val windowsArm64 = OsArchitecture(WINDOWS, aarch64)
79+
mockkStatic(OsArchitecture::detectOSandArchitecture) {
80+
every { OsArchitecture.detectOSandArchitecture() } returns windowsArm64
81+
assertThatThrownBy {
82+
AnnotationConfigApplicationContext().use {
83+
RedisHighAvailabilityContextCustomizerFactory()
84+
.createContextCustomizer(AnnotatedClass::class.java, mutableListOf())
85+
.customizeContext(it, mockk())
86+
it.refresh()
87+
it.start()
88+
}
89+
}.isInstanceOf(UnsupportedOperationException::class.java).hasMessage("Unsupported OS: $windowsArm64")
90+
}
91+
}
92+
5993
@Test
6094
@DisplayName("RedisHighAvailabilityContextCustomizer should throw NoSuchElementException when no nodes are available")
6195
fun noNodes_shouldThrow() {
@@ -64,8 +98,8 @@ internal class RedisHighAvailabilityContextCustomizerTest {
6498
assertThatThrownBy {
6599
AnnotationConfigApplicationContext().use {
66100
RedisHighAvailabilityContextCustomizerFactory()
67-
.createContextCustomizer(AnnotatedClass::class.java, mutableListOf())
68-
.customizeContext(it, mockk())
101+
.createContextCustomizer(AnnotatedClass::class.java, mutableListOf())
102+
.customizeContext(it, mockk())
69103
it.refresh()
70104
it.start()
71105
}
@@ -78,14 +112,14 @@ internal class RedisHighAvailabilityContextCustomizerTest {
78112
fun notEnoughNodes_shouldThrow() {
79113
mockkConstructor(NodeProvider::class) {
80114
every { anyConstructed<NodeProvider>().next() } returns Node(
81-
12345,
82-
"127.0.0.1"
115+
12345,
116+
"127.0.0.1"
83117
) andThenThrows NoSuchElementException()
84118
assertThatThrownBy {
85119
AnnotationConfigApplicationContext().use {
86120
RedisHighAvailabilityContextCustomizerFactory()
87-
.createContextCustomizer(AnnotatedClass::class.java, mutableListOf())
88-
.customizeContext(it, mockk())
121+
.createContextCustomizer(AnnotatedClass::class.java, mutableListOf())
122+
.customizeContext(it, mockk())
89123
it.refresh()
90124
it.start()
91125
}
@@ -135,8 +169,8 @@ internal class RedisHighAvailabilityContextCustomizerTest {
135169
var client: RedisClient?
136170
AnnotationConfigApplicationContext().use {
137171
RedisHighAvailabilityContextCustomizerFactory()
138-
.createContextCustomizer(AnnotatedClass::class.java, mutableListOf())
139-
.customizeContext(it, mockk())
172+
.createContextCustomizer(AnnotatedClass::class.java, mutableListOf())
173+
.customizeContext(it, mockk())
140174
it.refresh()
141175
it.start()
142176
server = RedisStore.server(it)
@@ -146,6 +180,6 @@ internal class RedisHighAvailabilityContextCustomizerTest {
146180
assertThatThrownBy { client!!.get("FOO") }.isInstanceOf(Exception::class.java)
147181
}
148182

149-
@EmbeddedRedisHighAvailability(ports = [0, 0, 0])
183+
@EmbeddedRedisHighAvailability(ports = [0, 0, 0], executeInDirectory = CUSTOM_EXECUTION_DIR)
150184
private class AnnotatedClass
151185
}

0 commit comments

Comments
 (0)