-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChatMessageControllerTest.java
More file actions
executable file
·112 lines (96 loc) · 4.02 KB
/
ChatMessageControllerTest.java
File metadata and controls
executable file
·112 lines (96 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.ssechat.controller;
import com.ssechat.model.ChatMessage;
import com.ssechat.repository.ChatMessageRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.webtestclient.autoconfigure.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.BodyInserters;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
@Testcontainers
class ChatMessageControllerTest {
private static final String CHANNEL_1 = "1";
private static final String CHANNEL_2 = "2";
private static final String MESSAGE_CH1 = "Hello from channel 1";
private static final String MESSAGE_CH2 = "Hello from channel 2";
private static final String SENDER_CH1 = "test-sender-ch1";
private static final String SENDER_CH2 = "test-sender-ch2";
private static final String CHATS_URI = "/chats/stream?channelId=" + CHANNEL_1;
@Container
static MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:8"));
@DynamicPropertySource
static void setProperties(DynamicPropertyRegistry registry) {
registry.add("spring.mongodb.uri", mongoDBContainer::getReplicaSetUrl);
}
@Autowired
private WebTestClient webTestClient;
@Autowired
private ChatMessageRepository chatMessageRepository;
@BeforeEach
void setUp() {
chatMessageRepository.deleteAll().block();
}
@Test
void getChatStream() {
ChatMessage testMessageChannel1 = new ChatMessage();
testMessageChannel1.setChannelId(CHANNEL_1);
testMessageChannel1.setMessage(MESSAGE_CH1);
testMessageChannel1.setSender(SENDER_CH1);
ChatMessage testMessageChannel2 = new ChatMessage();
testMessageChannel2.setChannelId(CHANNEL_2);
testMessageChannel2.setMessage(MESSAGE_CH2);
testMessageChannel2.setSender(SENDER_CH2);
chatMessageRepository.save(testMessageChannel1).block();
chatMessageRepository.save(testMessageChannel2).block();
Flux<ChatMessage> chatMessageFlux = webTestClient.get().uri(CHATS_URI)
.accept(MediaType.TEXT_EVENT_STREAM)
.exchange()
.expectStatus().isOk()
.returnResult(ChatMessage.class)
.getResponseBody();
StepVerifier.create(chatMessageFlux)
.expectNextMatches(chatMessage ->
CHANNEL_1.equals(chatMessage.getChannelId()) &&
MESSAGE_CH1.equals(chatMessage.getMessage()) &&
SENDER_CH1.equals(chatMessage.getSender())
)
.thenCancel()
.verify();
}
@Test
void addStreamNoBody() {
webTestClient.post().uri("/chats")
.header("Content-Type","application/json")
.exchange()
.expectStatus().isBadRequest();
}
@Test
void addStream() {
webTestClient.post().uri("/chats")
.header("Content-Type","application/json")
.body(BodyInserters.fromValue(String.format("""
{
"message": "%s",
"channelId": "%s",
"sender": "%s",
"recipient": "recipient2"
}""", MESSAGE_CH1, CHANNEL_1, SENDER_CH1)))
.exchange()
.expectStatus().isCreated();
}
}