-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBasicTest.java
More file actions
271 lines (235 loc) · 8.44 KB
/
BasicTest.java
File metadata and controls
271 lines (235 loc) · 8.44 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package io.getstream;
import io.getstream.exceptions.StreamException;
import io.getstream.models.*;
import io.getstream.services.Chat;
import io.getstream.services.Video;
import io.getstream.services.framework.StreamSDKClient;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeoutException;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
public class BasicTest {
static StreamSDKClient client;
protected static FullUserResponse testUser;
protected static List<FullUserResponse> testUsers = new ArrayList<>();
protected static ChannelStateResponse testChannelGetResponse;
protected static ChannelResponse testChannel;
protected static MessageResponse testMessage;
static Chat chat;
static Video video;
@BeforeAll
static void setup() throws Exception {
client = new StreamSDKClient();
chat = client.chat();
video = client.video();
setProperties();
// cleanChannels();
// cleanChannelTypes();
// cleanBlocklists();
// cleanCommands();
upsertUsers();
createTestChannel();
createTestMessage();
pause();
}
private static void cleanChannels() throws StreamException, NullPointerException {
while (true) {
List<String> channels =
chat
.queryChannels(QueryChannelsRequest.builder().build())
.execute()
.getData()
.getChannels()
.stream()
.map(channel -> channel.getChannel().getCid())
.collect(Collectors.toList());
if (channels.isEmpty()) {
break;
}
var deleteManyResponse =
chat.deleteChannels(
DeleteChannelsRequest.builder().cids(channels).hardDelete(true).build())
.execute();
String taskId = deleteManyResponse.getData().getTaskID();
Assertions.assertNotNull(taskId);
System.out.printf("Waiting for channel deletion task %s to complete...\n", taskId);
while (true) {
var response = client.getTask(taskId).execute();
String status = response.getData().getStatus();
if (status.equals("completed") || status.equals("ok")) {
break;
}
if (status.equals("failed") || status.equals("error")) {
throw new StreamException(
String.format(
"Failed to delete channel(task_id: %s): %s",
response.getData().getTaskID(), status),
(Throwable) null);
}
// wait for the channels to delete
Assertions.assertDoesNotThrow(() -> Thread.sleep(500));
}
}
}
private static void cleanChannelTypes() throws Exception {
chat.listChannelTypes()
.execute()
.getData()
.getChannelTypes()
.values()
.forEach(
channelType -> {
try {
chat.deleteChannelType(channelType.getName()).execute();
} catch (Exception e) {
// Do nothing. Happens when there are channels of that type
}
});
}
private static void cleanBlocklists() throws Exception {
client
.listBlockLists()
.execute()
.getData()
.getBlocklists()
.forEach(
blocklist -> {
try {
client.deleteBlockList(blocklist.getName()).execute();
} catch (Exception e) {
// Do nothing this happens for built in
}
});
}
private static void cleanCommands() throws Exception {
chat.listCommands()
.execute()
.getData()
.getCommands()
.forEach(
command -> {
try {
chat.deleteCommand(command.getName()).execute();
} catch (Exception e) {
// Do nothing
}
});
waitFor(
() -> {
var commands =
Assertions.assertDoesNotThrow(
() -> chat.listCommands().execute().getData().getCommands());
return commands.size() == 5; // Built-in 5 commands
});
}
private static void createTestMessage() throws Exception {
testMessage = sendTestMessage();
}
private static void createTestChannel() throws Exception {
testChannelGetResponse = createRandomChannel();
testChannel = testChannelGetResponse.getChannel();
}
static void upsertUsers() throws Exception {
String id1 = RandomStringUtils.randomAlphabetic(10);
String id2 = RandomStringUtils.randomAlphabetic(10);
String id3 = RandomStringUtils.randomAlphabetic(10);
String id4 = RandomStringUtils.randomAlphabetic(10);
UserRequest testUserRequestObject =
UserRequest.builder().id(id1).name("Gandalf " + id1).build();
List<UserRequest> testUsersRequestObjects = new ArrayList<>();
testUsersRequestObjects.add(testUserRequestObject);
testUsersRequestObjects.add(UserRequest.builder().id(id2).name("Frodo " + id2).build());
testUsersRequestObjects.add(UserRequest.builder().id(id3).name("Hobbit " + id3).build());
testUsersRequestObjects.add(UserRequest.builder().id(id4).name("Samwise " + id4).build());
UpdateUsersRequest updateUsersRequest =
UpdateUsersRequest.builder()
.users(
testUsersRequestObjects.stream()
.collect(Collectors.toMap(UserRequest::getId, x -> x)))
.build();
testUsers =
client.updateUsers(updateUsersRequest).execute().getData().getUsers().values().stream()
.toList();
testUser = testUsers.get(0);
}
static void setProperties() {
System.setProperty(
"java.util.logging.SimpleFormatter.format",
"%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s %2$s %5$s%6$s%n");
}
protected static List<ChannelMemberRequest> buildChannelMembersList() {
return testUsers.stream()
.map(user -> ChannelMemberRequest.builder().userID(user.getId()).build())
.collect(Collectors.toList());
}
protected static ChannelStateResponse createRandomChannel() throws Exception {
return chat.getOrCreateChannel(
"team",
RandomStringUtils.randomAlphabetic(12),
GetOrCreateChannelRequest.builder()
.data(
ChannelInput.builder()
.createdBy(UserRequest.builder().id(testUser.getId()).build())
.members(buildChannelMembersList())
.build())
.build())
.execute()
.getData();
}
protected static MessageResponse sendTestMessage() throws Exception {
String text = UUID.randomUUID().toString();
MessageRequest messageRequest =
MessageRequest.builder().text(text).userID(testUser.getId()).build();
return chat.sendMessage(
testChannel.getType(),
testChannel.getId(),
SendMessageRequest.builder().message(messageRequest).build())
.execute()
.getData()
.getMessage();
}
/**
* This is used to pause after creation, as there can be a small delay before we can act upon the
* resource
*/
protected static void pause() {
try {
java.lang.Thread.sleep(6000);
} catch (InterruptedException e) {
// Do nothing
}
}
protected static void waitFor(Supplier<Boolean> predicate) {
waitFor(predicate, 500L, 15000L);
}
protected static void waitFor(Supplier<Boolean> predicate, Long askInterval, Long timeout) {
var start = System.currentTimeMillis();
while (true) {
if (timeout < (System.currentTimeMillis() - start)) {
Assertions.fail(new TimeoutException());
}
if (Assertions.assertDoesNotThrow(predicate::get)) {
return;
}
Assertions.assertDoesNotThrow(() -> java.lang.Thread.sleep(askInterval));
}
}
/**
* Polls the given task ID until it reaches "completed" or "failed" status. Polls up to 30 times
* with 1-second intervals.
*/
protected static void waitForAsyncTask(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");
}
}