-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathChatTestBase.java
More file actions
132 lines (123 loc) · 4.68 KB
/
ChatTestBase.java
File metadata and controls
132 lines (123 loc) · 4.68 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package io.getstream;
import io.getstream.models.*;
import java.util.*;
import java.util.stream.Collectors;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.Assertions;
/**
* Base class for chat integration tests. Extends BasicTest to inherit client initialization and
* provides helpers for user/channel/message creation, cleanup, and task polling.
*/
public class ChatTestBase extends BasicTest {
/**
* Creates n users with unique IDs. Returns the list of created user IDs. Callers should register
* cleanup via deleteUsersWithRetry in @AfterAll.
*/
protected List<String> createTestUsers(int n) throws Exception {
List<String> ids = new ArrayList<>();
Map<String, UserRequest> users = new HashMap<>();
for (int i = 0; i < n; i++) {
String id = "tu-" + UUID.randomUUID().toString().replace("-", "").substring(0, 16);
ids.add(id);
users.put(id, UserRequest.builder().id(id).name("Test User " + id).role("user").build());
}
client.updateUsers(UpdateUsersRequest.builder().users(users).build()).execute();
return ids;
}
/**
* Creates a messaging channel with the given creator. Returns the channel ID. Callers should
* hard-delete the channel in @AfterAll cleanup.
*/
protected String createTestChannel(String creatorId) throws Exception {
String channelId = "tc-" + RandomStringUtils.randomAlphabetic(12).toLowerCase();
chat.getOrCreateChannel(
"messaging",
channelId,
GetOrCreateChannelRequest.builder()
.data(ChannelInput.builder().createdByID(creatorId).build())
.build())
.execute();
return channelId;
}
/**
* Creates a messaging channel with the given creator and members. Returns the channel ID. Callers
* should hard-delete the channel in @AfterAll cleanup.
*/
protected String createTestChannelWithMembers(String creatorId, List<String> memberIds)
throws Exception {
String channelId = "tc-" + RandomStringUtils.randomAlphabetic(12).toLowerCase();
List<ChannelMemberRequest> members =
memberIds.stream()
.map(id -> ChannelMemberRequest.builder().userID(id).build())
.collect(Collectors.toList());
chat.getOrCreateChannel(
"messaging",
channelId,
GetOrCreateChannelRequest.builder()
.data(ChannelInput.builder().createdByID(creatorId).members(members).build())
.build())
.execute();
return channelId;
}
/**
* Sends a message to the specified channel as the given user. Returns the message ID. Asserts
* that the message ID is non-null.
*/
protected String sendTestMessage(String channelType, String channelId, String userId, String text)
throws Exception {
var resp =
chat.sendMessage(
channelType,
channelId,
SendMessageRequest.builder()
.message(MessageRequest.builder().text(text).userID(userId).build())
.build())
.execute();
Assertions.assertNotNull(resp.getData().getMessage().getId());
return resp.getData().getMessage().getId();
}
/**
* Hard-deletes users with retry on rate-limit errors. Retries up to 10 times with exponential
* backoff (3s, 6s, 9s...).
*/
protected void deleteUsersWithRetry(List<String> userIds) {
for (int i = 0; i < 10; i++) {
try {
client
.deleteUsers(
DeleteUsersRequest.builder()
.userIds(userIds)
.user("hard")
.messages("hard")
.conversations("hard")
.build())
.execute();
return;
} catch (Exception e) {
String msg = e.getMessage();
if (msg == null || !msg.contains("Too many requests")) return;
try {
Thread.sleep((long) (i + 1) * 3000L);
} catch (InterruptedException ignored) {
}
}
}
}
/**
* Polls the given task ID until it reaches "completed" or "failed" status. Polls up to 30 times
* with 1-second intervals.
*/
protected void waitForTask(String taskId) throws Exception {
for (int i = 0; i < 30; i++) {
var result = client.getTask(taskId).execute();
String status = result.getData().getStatus();
if ("completed".equals(status) || "failed".equals(status)) return;
Thread.sleep(1000);
}
Assertions.fail("Task " + taskId + " did not complete after 30 attempts");
}
/** Generates a random lowercase alphanumeric string of the given length. */
protected String randomString(int n) {
return RandomStringUtils.randomAlphanumeric(n).toLowerCase();
}
}