Skip to content

Commit b100fe2

Browse files
committed
Add unit tests for EventStoreDB configuration and subscription filters
1 parent 201afa8 commit b100fe2

File tree

8 files changed

+785
-41
lines changed

8 files changed

+785
-41
lines changed

.github/workflows/detekt.yml

Lines changed: 0 additions & 18 deletions
This file was deleted.

.github/workflows/maven_release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
cache: gradle
2929

3030
- name: Validate Gradle wrapper
31-
uses: gradle/wrapper-validation-action@v2
31+
uses: gradle/actions/wrapper-validation@v4
3232

3333
- name: Print Version
3434
run: ./gradlew printVersion
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package io.github.abaddon.kcqrs.eventstoredb.config
2+
3+
import io.kurrent.dbclient.Position
4+
import org.junit.jupiter.api.Assertions.*
5+
import org.junit.jupiter.api.Test
6+
import org.junit.jupiter.api.assertThrows
7+
8+
internal class SubscriptionFilterConfigTest {
9+
10+
@Test
11+
fun `Given event type prefix filter when created then filter type and value are set correctly`() {
12+
// Given
13+
val filterType = SubscriptionFilterConfig.SUBSCRIPTION_FILTER_EVENT_TYPE_PREFIX
14+
val filterValue = "Counter"
15+
16+
// When
17+
val config = SubscriptionFilterConfig(filterType, filterValue)
18+
19+
// Then
20+
assertNotNull(config)
21+
}
22+
23+
@Test
24+
fun `Given event type regex filter when subscribeToAllOptions called then returns valid options`() {
25+
// Given
26+
val filterType = SubscriptionFilterConfig.SUBSCRIPTION_FILTER_EVENT_TYPE_REGEX
27+
val filterValue = "^Counter.*Event$"
28+
val config = SubscriptionFilterConfig(filterType, filterValue)
29+
val position = Position(0, 0)
30+
31+
// When
32+
val options = config.subscribeToAllOptions(position)
33+
34+
// Then
35+
assertNotNull(options)
36+
}
37+
38+
@Test
39+
fun `Given stream name prefix filter when subscribeToAllOptions called then returns valid options`() {
40+
// Given
41+
val filterType = SubscriptionFilterConfig.SUBSCRIPTION_FILTER_STREAM_NAME_PREFIX
42+
val filterValue = "account"
43+
val config = SubscriptionFilterConfig(filterType, filterValue)
44+
val position = Position(0, 0)
45+
46+
// When
47+
val options = config.subscribeToAllOptions(position)
48+
49+
// Then
50+
assertNotNull(options)
51+
}
52+
53+
@Test
54+
fun `Given stream name regex filter when subscribeToAllOptions called then returns valid options`() {
55+
// Given
56+
val filterType = SubscriptionFilterConfig.SUBSCRIPTION_FILTER_STREAM_NAME_REGEX
57+
val filterValue = "^(account|transaction)"
58+
val config = SubscriptionFilterConfig(filterType, filterValue)
59+
val position = Position(0, 0)
60+
61+
// When
62+
val options = config.subscribeToAllOptions(position)
63+
64+
// Then
65+
assertNotNull(options)
66+
}
67+
68+
@Test
69+
fun `Given different positions when subscribeToAllOptions called then position is set correctly`() {
70+
// Given
71+
val config = SubscriptionFilterConfig(
72+
SubscriptionFilterConfig.SUBSCRIPTION_FILTER_EVENT_TYPE_PREFIX,
73+
"Test"
74+
)
75+
val startPosition = Position(0, 0)
76+
val endPosition = Position(100, 50)
77+
78+
// When
79+
val optionsFromStart = config.subscribeToAllOptions(startPosition)
80+
val optionsFromEnd = config.subscribeToAllOptions(endPosition)
81+
82+
// Then
83+
assertNotNull(optionsFromStart)
84+
assertNotNull(optionsFromEnd)
85+
}
86+
87+
@Test
88+
fun `Given invalid filter type when subscribeToAllOptions called then throws IllegalStateException`() {
89+
// Given
90+
val filterType = "INVALID_FILTER_TYPE"
91+
val filterValue = "test"
92+
val config = SubscriptionFilterConfig(filterType, filterValue)
93+
val position = Position(0, 0)
94+
95+
// When/Then
96+
val exception = assertThrows<IllegalStateException> {
97+
config.subscribeToAllOptions(position)
98+
}
99+
100+
assertTrue(exception.message?.contains("No filter type") == true)
101+
}
102+
103+
@Test
104+
fun `Given event type prefix constant when accessed then has correct value`() {
105+
// Then
106+
assertEquals("eventTypePrefix", SubscriptionFilterConfig.SUBSCRIPTION_FILTER_EVENT_TYPE_PREFIX)
107+
}
108+
109+
@Test
110+
fun `Given event type regex constant when accessed then has correct value`() {
111+
// Then
112+
assertEquals("eventTypeRegEx", SubscriptionFilterConfig.SUBSCRIPTION_FILTER_EVENT_TYPE_REGEX)
113+
}
114+
115+
@Test
116+
fun `Given stream name prefix constant when accessed then has correct value`() {
117+
// Then
118+
assertEquals("streamNamePrefix", SubscriptionFilterConfig.SUBSCRIPTION_FILTER_STREAM_NAME_PREFIX)
119+
}
120+
121+
@Test
122+
fun `Given stream name regex constant when accessed then has correct value`() {
123+
// Then
124+
assertEquals("streamNameRegEx", SubscriptionFilterConfig.SUBSCRIPTION_FILTER_STREAM_NAME_REGEX)
125+
}
126+
127+
@Test
128+
fun `Given complex regex pattern when created then pattern is preserved`() {
129+
// Given
130+
val filterType = SubscriptionFilterConfig.SUBSCRIPTION_FILTER_STREAM_NAME_REGEX
131+
val complexPattern = "^(account|transaction|customer)\\.(create|update|delete)$"
132+
val config = SubscriptionFilterConfig(filterType, complexPattern)
133+
val position = Position(0, 0)
134+
135+
// When
136+
val options = config.subscribeToAllOptions(position)
137+
138+
// Then
139+
assertNotNull(options)
140+
}
141+
142+
@Test
143+
fun `Given multiple configs with different filter types when created then each is independent`() {
144+
// Given
145+
val config1 = SubscriptionFilterConfig(
146+
SubscriptionFilterConfig.SUBSCRIPTION_FILTER_EVENT_TYPE_PREFIX,
147+
"Account"
148+
)
149+
val config2 = SubscriptionFilterConfig(
150+
SubscriptionFilterConfig.SUBSCRIPTION_FILTER_STREAM_NAME_REGEX,
151+
"^account"
152+
)
153+
val position = Position(0, 0)
154+
155+
// When
156+
val options1 = config1.subscribeToAllOptions(position)
157+
val options2 = config2.subscribeToAllOptions(position)
158+
159+
// Then
160+
assertNotNull(options1)
161+
assertNotNull(options2)
162+
}
163+
164+
@Test
165+
fun `Given empty filter value when created then config is created`() {
166+
// Given
167+
val filterType = SubscriptionFilterConfig.SUBSCRIPTION_FILTER_EVENT_TYPE_PREFIX
168+
val filterValue = ""
169+
val config = SubscriptionFilterConfig(filterType, filterValue)
170+
val position = Position(0, 0)
171+
172+
// When
173+
val options = config.subscribeToAllOptions(position)
174+
175+
// Then
176+
assertNotNull(options)
177+
}
178+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package io.github.abaddon.kcqrs.eventstoredb.eventstore
2+
3+
import io.github.abaddon.kcqrs.eventstoredb.config.EventStoreDBConfig
4+
import org.junit.jupiter.api.Assertions.*
5+
import org.junit.jupiter.api.Test
6+
7+
internal class EventStoreDBRepositoryConfigTest {
8+
9+
@Test
10+
fun `Given valid config when created then all properties are set correctly`() {
11+
// Given
12+
val connectionString = "kurrentdb://127.0.0.1:2113?tls=false&tlsVerifyCert=false"
13+
val eventStoreDBConfig = EventStoreDBConfig(connectionString)
14+
val streamName = "TestStream"
15+
val maxReadPageSize = 1000L
16+
val maxWritePageSize = 500
17+
18+
// When
19+
val config = EventStoreDBRepositoryConfig(
20+
eventStoreDB = eventStoreDBConfig,
21+
streamName = streamName,
22+
maxReadPageSize = maxReadPageSize,
23+
maxWritePageSize = maxWritePageSize
24+
)
25+
26+
// Then
27+
assertEquals(streamName, config.streamName)
28+
assertEquals(maxReadPageSize, config.maxReadPageSize)
29+
assertEquals(maxWritePageSize, config.maxWritePageSize)
30+
}
31+
32+
@Test
33+
fun `Given config when eventStoreDBClientSettings called then returns valid KurrentDBClientSettings`() {
34+
// Given
35+
val connectionString = "kurrentdb://localhost:2113?tls=true&tlsVerifyCert=true"
36+
val eventStoreDBConfig = EventStoreDBConfig(connectionString)
37+
val config = EventStoreDBRepositoryConfig(
38+
eventStoreDB = eventStoreDBConfig,
39+
streamName = "TestStream",
40+
maxReadPageSize = 500L,
41+
maxWritePageSize = 100
42+
)
43+
44+
// When
45+
val clientSettings = config.eventStoreDBClientSettings()
46+
47+
// Then
48+
assertNotNull(clientSettings)
49+
assertTrue(clientSettings.isTls)
50+
assertTrue(clientSettings.isTlsVerifyCert)
51+
assertEquals(1, clientSettings.hosts.size)
52+
assertEquals("localhost", clientSettings.hosts[0].host)
53+
assertEquals(2113, clientSettings.hosts[0].port)
54+
}
55+
56+
@Test
57+
fun `Given different stream names when created then each config has unique stream name`() {
58+
// Given
59+
val connectionString = "kurrentdb://127.0.0.1:2113?tls=false"
60+
val eventStoreDBConfig = EventStoreDBConfig(connectionString)
61+
62+
// When
63+
val config1 = EventStoreDBRepositoryConfig(eventStoreDBConfig, "Stream1", 100L, 50)
64+
val config2 = EventStoreDBRepositoryConfig(eventStoreDBConfig, "Stream2", 100L, 50)
65+
66+
// Then
67+
assertNotEquals(config1.streamName, config2.streamName)
68+
assertEquals("Stream1", config1.streamName)
69+
assertEquals("Stream2", config2.streamName)
70+
}
71+
72+
@Test
73+
fun `Given config with different page sizes when created then page sizes are set correctly`() {
74+
// Given
75+
val connectionString = "kurrentdb://127.0.0.1:2113?tls=false"
76+
val eventStoreDBConfig = EventStoreDBConfig(connectionString)
77+
78+
// When
79+
val config1 = EventStoreDBRepositoryConfig(eventStoreDBConfig, "Stream", 100L, 50)
80+
val config2 = EventStoreDBRepositoryConfig(eventStoreDBConfig, "Stream", 2000L, 1000)
81+
82+
// Then
83+
assertEquals(100L, config1.maxReadPageSize)
84+
assertEquals(50, config1.maxWritePageSize)
85+
assertEquals(2000L, config2.maxReadPageSize)
86+
assertEquals(1000, config2.maxWritePageSize)
87+
}
88+
89+
@Test
90+
fun `Given config when copy is created then both configs are independent`() {
91+
// Given
92+
val connectionString = "kurrentdb://127.0.0.1:2113?tls=false"
93+
val eventStoreDBConfig = EventStoreDBConfig(connectionString)
94+
val originalConfig = EventStoreDBRepositoryConfig(eventStoreDBConfig, "OriginalStream", 500L, 250)
95+
96+
// When
97+
val copiedConfig = originalConfig.copy(streamName = "CopiedStream")
98+
99+
// Then
100+
assertNotEquals(originalConfig.streamName, copiedConfig.streamName)
101+
assertEquals("OriginalStream", originalConfig.streamName)
102+
assertEquals("CopiedStream", copiedConfig.streamName)
103+
assertEquals(originalConfig.maxReadPageSize, copiedConfig.maxReadPageSize)
104+
assertEquals(originalConfig.maxWritePageSize, copiedConfig.maxWritePageSize)
105+
}
106+
}

0 commit comments

Comments
 (0)