1
1
package org.learning.by.example.petstore.command.consumer
2
2
3
3
import org.assertj.core.api.Assertions.assertThat
4
+ import org.awaitility.Awaitility.await
4
5
import org.junit.jupiter.api.BeforeEach
5
6
import org.junit.jupiter.api.MethodOrderer
6
7
import org.junit.jupiter.api.Order
7
8
import org.junit.jupiter.api.Test
8
9
import org.junit.jupiter.api.TestMethodOrder
10
+ import org.learning.by.example.petstore.command.Command
9
11
import org.learning.by.example.petstore.command.consumer.CommandsConsumerConfig.Constants.CONSUMER_CONFIG_BOOSTRAP_SERVER
10
12
import org.learning.by.example.petstore.command.test.CustomKafkaContainer
11
13
import org.springframework.beans.factory.annotation.Autowired
12
14
import org.springframework.boot.test.context.SpringBootTest
15
+ import org.springframework.test.annotation.DirtiesContext
13
16
import org.springframework.test.context.ActiveProfiles
14
17
import org.springframework.test.context.DynamicPropertyRegistry
15
18
import org.springframework.test.context.DynamicPropertySource
16
19
import org.testcontainers.junit.jupiter.Container
17
20
import org.testcontainers.junit.jupiter.Testcontainers
21
+ import reactor.core.publisher.Mono
18
22
import reactor.test.StepVerifier
19
23
import java.time.Duration
20
24
import java.time.Instant
21
25
import java.util.UUID
26
+ import java.util.concurrent.TimeUnit
22
27
23
28
@SpringBootTest
24
29
@Testcontainers
25
30
@ActiveProfiles(" consumer" )
26
31
@TestMethodOrder(MethodOrderer .OrderAnnotation ::class )
32
+ @DirtiesContext
27
33
internal class CommandsConsumerImplTest (
28
34
@Autowired val commandsConsumerImpl : CommandsConsumerImpl ,
29
35
@Autowired val commandsConsumerConfig : CommandsConsumerConfig
@@ -79,34 +85,44 @@ internal class CommandsConsumerImplTest(
79
85
assertThat(KAFKA_CONTAINER .sendMessage(commandsConsumerConfig.topic, FIRST_COMMAND )).isTrue()
80
86
assertThat(KAFKA_CONTAINER .sendMessage(commandsConsumerConfig.topic, SECOND_COMMAND )).isTrue()
81
87
82
- StepVerifier .create(commandsConsumerImpl.receiveCommands())
83
- .expectSubscription()
84
- .thenRequest(Long .MAX_VALUE )
85
- .consumeNextWith {
86
- assertThat(it.commandName).isEqualTo(" example command 1" )
87
- assertThat(it.id).isEqualTo(UUID .fromString(" 4cb5294b-1034-4bc4-9b3d-899adb782d89" ))
88
- assertThat(it.timestamp).isEqualTo(Instant .parse(" 2020-06-28T08:53:35.168283Z" ))
89
- assertThat(it.get<String >(" attribute1" )).isEqualTo(" value1" )
90
- assertThat(it.get<Int >(" attribute2" )).isEqualTo(123 )
91
- }
92
- .consumeNextWith {
93
- assertThat(it.commandName).isEqualTo(" example command 2" )
94
- assertThat(it.id).isEqualTo(UUID .fromString(" 4cb5294b-1034-4bc4-9b3d-542adb232a21" ))
95
- assertThat(it.timestamp).isEqualTo(Instant .parse(" 2020-06-28T11:22:13.456732Z" ))
96
- assertThat(it.get<Boolean >(" attribute1" )).isEqualTo(false )
97
- assertThat(it.get<Double >(" attribute2" )).isEqualTo(125.5 )
98
- }
99
- .expectNextCount(0L )
100
- .thenCancel()
101
- .verify(Duration .ofSeconds(5L ))
88
+ val commands = arrayListOf<Command >()
89
+
90
+ val disposable = commandsConsumerImpl.receiveCommands {
91
+ commands.add(it)
92
+ Mono .empty()
93
+ }.subscribe()
94
+
95
+ await().atMost(30 , TimeUnit .SECONDS ).until {
96
+ commands.size == 2
97
+ }
98
+
99
+ disposable.dispose()
100
+
101
+ assertThat(commands).hasSize(2 )
102
+
103
+ with (commands[0 ]) {
104
+ assertThat(commandName).isEqualTo(" example command 1" )
105
+ assertThat(id).isEqualTo(UUID .fromString(" 4cb5294b-1034-4bc4-9b3d-899adb782d89" ))
106
+ assertThat(timestamp).isEqualTo(Instant .parse(" 2020-06-28T08:53:35.168283Z" ))
107
+ assertThat(get<String >(" attribute1" )).isEqualTo(" value1" )
108
+ assertThat(get<Int >(" attribute2" )).isEqualTo(123 )
109
+ }
110
+
111
+ with (commands[1 ]) {
112
+ assertThat(commandName).isEqualTo(" example command 2" )
113
+ assertThat(id).isEqualTo(UUID .fromString(" 4cb5294b-1034-4bc4-9b3d-542adb232a21" ))
114
+ assertThat(timestamp).isEqualTo(Instant .parse(" 2020-06-28T11:22:13.456732Z" ))
115
+ assertThat(get<Boolean >(" attribute1" )).isEqualTo(false )
116
+ assertThat(get<Double >(" attribute2" )).isEqualTo(125.5 )
117
+ }
102
118
}
103
119
104
120
@Test
105
121
@Order(1 )
106
122
fun `we should error on invalid command` () {
107
123
assertThat(KAFKA_CONTAINER .sendMessage(commandsConsumerConfig.topic, INVALID_COMMAND )).isTrue()
108
124
109
- StepVerifier .create(commandsConsumerImpl.receiveCommands() )
125
+ StepVerifier .create(commandsConsumerImpl.receiveCommands { Mono .empty() } )
110
126
.expectSubscription()
111
127
.thenRequest(Long .MAX_VALUE )
112
128
.expectErrorMatches {
@@ -120,7 +136,7 @@ internal class CommandsConsumerImplTest(
120
136
fun `we should error on empty command` () {
121
137
assertThat(KAFKA_CONTAINER .sendMessage(commandsConsumerConfig.topic, EMPTY_COMMAND )).isTrue()
122
138
123
- StepVerifier .create(commandsConsumerImpl.receiveCommands() )
139
+ StepVerifier .create(commandsConsumerImpl.receiveCommands { Mono .empty() } )
124
140
.expectSubscription()
125
141
.thenRequest(Long .MAX_VALUE )
126
142
.expectErrorMatches {
@@ -140,7 +156,7 @@ internal class CommandsConsumerImplTest(
140
156
141
157
@Order(2 )
142
158
@Test
143
- fun `we can get check if noy alive` () {
159
+ fun `we can get check if not alive` () {
144
160
if (KAFKA_CONTAINER .isRunning) KAFKA_CONTAINER .stop()
145
161
146
162
StepVerifier .create(commandsConsumerImpl.isKafkaAvailable)
@@ -156,7 +172,7 @@ internal class CommandsConsumerImplTest(
156
172
fun `we should error on failure to connect` () {
157
173
if (KAFKA_CONTAINER .isRunning) KAFKA_CONTAINER .stop()
158
174
159
- StepVerifier .create(commandsConsumerImpl.receiveCommands() )
175
+ StepVerifier .create(commandsConsumerImpl.receiveCommands { Mono .empty() } )
160
176
.expectSubscription()
161
177
.thenRequest(Long .MAX_VALUE )
162
178
.expectErrorMatches {
0 commit comments