-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathTeamUsageStatsIntegrationTest.java
More file actions
516 lines (435 loc) · 18.7 KB
/
TeamUsageStatsIntegrationTest.java
File metadata and controls
516 lines (435 loc) · 18.7 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
package io.getstream.chat.java;
import static org.junit.jupiter.api.Assertions.*;
import io.getstream.chat.java.exceptions.StreamException;
import io.getstream.chat.java.models.TeamUsageStats;
import io.getstream.chat.java.models.TeamUsageStats.QueryTeamUsageStatsResponse;
import io.getstream.chat.java.services.framework.DefaultClient;
import java.util.List;
import java.util.Properties;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
/**
* Integration tests for Team Usage Stats API. Uses dedicated multi-tenant test app credentials from
* STREAM_MULTI_TENANT_KEY and STREAM_MULTI_TENANT_SECRET environment variables.
*
* <p>These tests verify that the SDK correctly parses all response data from the backend.
*/
public class TeamUsageStatsIntegrationTest {
private static DefaultClient originalClient;
@BeforeAll
static void setup() {
// Save the original client to restore after tests
originalClient = DefaultClient.getInstance();
String apiKey = System.getenv("STREAM_MULTI_TENANT_KEY");
String apiSecret = System.getenv("STREAM_MULTI_TENANT_SECRET");
if (apiKey == null || apiKey.isEmpty() || apiSecret == null || apiSecret.isEmpty()) {
throw new IllegalStateException(
"Multi-tenant test app credentials are missing. "
+ "Set STREAM_MULTI_TENANT_KEY and STREAM_MULTI_TENANT_SECRET environment variables.");
}
Properties props = new Properties();
props.setProperty("io.getstream.chat.apiKey", apiKey);
props.setProperty("io.getstream.chat.apiSecret", apiSecret);
DefaultClient.setInstance(new DefaultClient(props));
}
@AfterAll
static void teardown() {
// Restore the original client so other tests use the correct credentials
if (originalClient != null) {
DefaultClient.setInstance(originalClient);
}
}
@Nested
@DisplayName("Basic Queries")
class BasicQueries {
@Test
@DisplayName("No parameters returns teams")
void noParametersReturnsTeams() throws StreamException {
QueryTeamUsageStatsResponse response = TeamUsageStats.queryTeamUsageStats().request();
assertNotNull(response);
assertNotNull(response.getTeams());
assertTrue(response.getTeams().size() > 0, "Should return at least one team");
assertNotNull(response.getDuration());
}
@Test
@DisplayName("Empty request returns teams")
void emptyRequestReturnsTeams() throws StreamException {
QueryTeamUsageStatsResponse response = TeamUsageStats.queryTeamUsageStats().request();
assertNotNull(response.getTeams());
}
}
@Nested
@DisplayName("Month Parameter")
class MonthParameter {
@Test
@DisplayName("Valid month format works")
void validMonthWorks() throws StreamException {
QueryTeamUsageStatsResponse response =
TeamUsageStats.queryTeamUsageStats().month("2026-02").request();
assertNotNull(response.getTeams());
assertTrue(response.getTeams().size() > 0);
}
@Test
@DisplayName("Past month with no data returns empty")
void pastMonthReturnsEmpty() throws StreamException {
QueryTeamUsageStatsResponse response =
TeamUsageStats.queryTeamUsageStats().month("2025-01").request();
assertNotNull(response.getTeams());
assertEquals(0, response.getTeams().size());
}
@Test
@DisplayName("Invalid month format throws error")
void invalidMonthThrows() {
assertThrows(
StreamException.class,
() -> TeamUsageStats.queryTeamUsageStats().month("invalid").request());
}
@Test
@DisplayName("Wrong length month throws error")
void wrongLengthMonthThrows() {
assertThrows(
StreamException.class,
() -> TeamUsageStats.queryTeamUsageStats().month("2026").request());
}
}
@Nested
@DisplayName("Date Range Parameters")
class DateRangeParameters {
@Test
@DisplayName("Valid date range works")
void validDateRangeWorks() throws StreamException {
QueryTeamUsageStatsResponse response =
TeamUsageStats.queryTeamUsageStats()
.startDate("2026-02-01")
.endDate("2026-02-17")
.request();
assertNotNull(response.getTeams());
assertTrue(response.getTeams().size() > 0);
}
@Test
@DisplayName("Single day range works")
void singleDayRangeWorks() throws StreamException {
QueryTeamUsageStatsResponse response =
TeamUsageStats.queryTeamUsageStats()
.startDate("2026-02-17")
.endDate("2026-02-17")
.request();
assertNotNull(response.getTeams());
}
@Test
@DisplayName("Invalid start_date throws error")
void invalidStartDateThrows() {
assertThrows(
StreamException.class,
() -> TeamUsageStats.queryTeamUsageStats().startDate("bad").request());
}
@Test
@DisplayName("end_date before start_date throws error")
void endBeforeStartThrows() {
assertThrows(
StreamException.class,
() ->
TeamUsageStats.queryTeamUsageStats()
.startDate("2026-02-20")
.endDate("2026-02-10")
.request());
}
}
@Nested
@DisplayName("Pagination")
class Pagination {
@Test
@DisplayName("limit=3 returns exactly 3 teams")
void limitReturnsCorrectCount() throws StreamException {
QueryTeamUsageStatsResponse response =
TeamUsageStats.queryTeamUsageStats().limit(3).request();
assertEquals(3, response.getTeams().size());
}
@Test
@DisplayName("limit returns next cursor when more data exists")
void limitReturnsNextCursor() throws StreamException {
QueryTeamUsageStatsResponse response =
TeamUsageStats.queryTeamUsageStats().limit(3).request();
assertNotNull(response.getNext());
assertFalse(response.getNext().isEmpty());
}
@Test
@DisplayName("Pagination with next cursor returns different teams")
void paginationReturnsDifferentTeams() throws StreamException {
QueryTeamUsageStatsResponse page1 = TeamUsageStats.queryTeamUsageStats().limit(3).request();
QueryTeamUsageStatsResponse page2 =
TeamUsageStats.queryTeamUsageStats().limit(3).next(page1.getNext()).request();
// Verify no overlap between pages
for (var t1 : page1.getTeams()) {
for (var t2 : page2.getTeams()) {
assertNotEquals(t1.getTeam(), t2.getTeam(), "Pages should not have overlapping teams");
}
}
}
@Test
@DisplayName("limit=30 (max) works")
void maxLimitWorks() throws StreamException {
QueryTeamUsageStatsResponse response =
TeamUsageStats.queryTeamUsageStats().limit(30).request();
assertNotNull(response.getTeams());
}
@Test
@DisplayName("limit > 30 throws error")
void overMaxLimitThrows() {
assertThrows(
StreamException.class, () -> TeamUsageStats.queryTeamUsageStats().limit(31).request());
}
@Test
@DisplayName("limit + month combined works")
void limitWithMonthWorks() throws StreamException {
QueryTeamUsageStatsResponse response =
TeamUsageStats.queryTeamUsageStats().limit(2).month("2026-02").request();
assertEquals(2, response.getTeams().size());
}
@Test
@DisplayName("limit + date range combined works")
void limitWithDateRangeWorks() throws StreamException {
QueryTeamUsageStatsResponse response =
TeamUsageStats.queryTeamUsageStats()
.limit(2)
.startDate("2026-02-01")
.endDate("2026-02-17")
.request();
assertEquals(2, response.getTeams().size());
}
}
@Nested
@DisplayName("Response Structure Validation")
class ResponseStructure {
@Test
@DisplayName("Response has duration field")
void responseHasDuration() throws StreamException {
QueryTeamUsageStatsResponse response = TeamUsageStats.queryTeamUsageStats().request();
assertNotNull(response.getDuration());
}
@Test
@DisplayName("Teams have team field")
void teamsHaveTeamField() throws StreamException {
QueryTeamUsageStatsResponse response = TeamUsageStats.queryTeamUsageStats().request();
// team field exists (may be empty string for default team)
assertDoesNotThrow(() -> response.getTeams().get(0).getTeam());
}
@Test
@DisplayName("All 16 metrics are present and parseable")
void allMetricsPresent() throws StreamException {
QueryTeamUsageStatsResponse response = TeamUsageStats.queryTeamUsageStats().request();
var team = response.getTeams().get(0);
// Daily activity metrics
assertNotNull(team.getUsersDaily(), "users_daily should be present");
assertNotNull(team.getMessagesDaily(), "messages_daily should be present");
assertNotNull(team.getTranslationsDaily(), "translations_daily should be present");
assertNotNull(team.getImageModerationDaily(), "image_moderations_daily should be present");
// Peak metrics
assertNotNull(team.getConcurrentUsers(), "concurrent_users should be present");
assertNotNull(team.getConcurrentConnections(), "concurrent_connections should be present");
// Rolling/cumulative metrics
assertNotNull(team.getUsersTotal(), "users_total should be present");
assertNotNull(team.getUsersLast24Hours(), "users_last_24_hours should be present");
assertNotNull(team.getUsersLast30Days(), "users_last_30_days should be present");
assertNotNull(team.getUsersMonthToDate(), "users_month_to_date should be present");
assertNotNull(
team.getUsersEngagedLast30Days(), "users_engaged_last_30_days should be present");
assertNotNull(
team.getUsersEngagedMonthToDate(), "users_engaged_month_to_date should be present");
assertNotNull(team.getMessagesTotal(), "messages_total should be present");
assertNotNull(team.getMessagesLast24Hours(), "messages_last_24_hours should be present");
assertNotNull(team.getMessagesLast30Days(), "messages_last_30_days should be present");
assertNotNull(team.getMessagesMonthToDate(), "messages_month_to_date should be present");
}
@Test
@DisplayName("Metrics have total field with valid value")
void metricsHaveTotal() throws StreamException {
QueryTeamUsageStatsResponse response = TeamUsageStats.queryTeamUsageStats().request();
var team = response.getTeams().get(0);
// Verify total field is present and non-null
assertNotNull(team.getMessagesTotal().getTotal());
assertNotNull(team.getUsersDaily().getTotal());
assertNotNull(team.getConcurrentUsers().getTotal());
}
@Test
@DisplayName("MetricStats total values are non-negative")
void metricTotalsNonNegative() throws StreamException {
QueryTeamUsageStatsResponse response = TeamUsageStats.queryTeamUsageStats().request();
for (var team : response.getTeams()) {
assertTrue(team.getMessagesTotal().getTotal() >= 0, "messages_total should be >= 0");
assertTrue(team.getUsersDaily().getTotal() >= 0, "users_daily should be >= 0");
assertTrue(team.getConcurrentUsers().getTotal() >= 0, "concurrent_users should be >= 0");
}
}
}
@Nested
@DisplayName("Data Correctness - Date Range Query")
class DataCorrectnessDateRange {
/**
* Verifies exact metric values for sdk-test-team-1/2 using date range query.
*
* <p>Expected values for each sdk-test-team-N:
*
* <ul>
* <li>users_daily: 0, messages_daily: 100
* <li>translations_daily: 0, image_moderations_daily: 0
* <li>concurrent_users: 0, concurrent_connections: 0
* <li>users_total: 5, users_last_24_hours: 5, users_last_30_days: 5, users_month_to_date: 5
* <li>users_engaged_last_30_days: 0, users_engaged_month_to_date: 0
* <li>messages_total: 100, messages_last_24_hours: 100, messages_last_30_days: 100,
* messages_month_to_date: 100
* </ul>
*/
@Test
@DisplayName("Date range: sdk-test-team-1 exact values")
void dateRangeSdkTestTeam1() throws StreamException {
QueryTeamUsageStatsResponse response =
TeamUsageStats.queryTeamUsageStats()
.startDate("2026-02-17")
.endDate("2026-02-18")
.request();
TeamUsageStats team = findTeamByName(response, "sdk-test-team-1");
assertNotNull(team, "sdk-test-team-1 should exist");
assertAllMetricsExact(team, "sdk-test-team-1");
}
@Test
@DisplayName("Date range: sdk-test-team-2 exact values")
void dateRangeSdkTestTeam2() throws StreamException {
QueryTeamUsageStatsResponse response =
TeamUsageStats.queryTeamUsageStats()
.startDate("2026-02-17")
.endDate("2026-02-18")
.request();
TeamUsageStats team = findTeamByName(response, "sdk-test-team-2");
assertNotNull(team, "sdk-test-team-2 should exist");
assertAllMetricsExact(team, "sdk-test-team-2");
}
@Test
@DisplayName("Date range: sdk-test-team-3 exists with valid metrics")
void dateRangeSdkTestTeam3() throws StreamException {
QueryTeamUsageStatsResponse response =
TeamUsageStats.queryTeamUsageStats()
.startDate("2026-02-17")
.endDate("2026-02-18")
.request();
TeamUsageStats team = findTeamByName(response, "sdk-test-team-3");
assertNotNull(team, "sdk-test-team-3 should exist");
assertMetricsNonNegative(team, "sdk-test-team-3");
}
}
@Nested
@DisplayName("Data Correctness - Month Query")
class DataCorrectnessMonth {
@Test
@DisplayName("Month query: test teams exist with valid metrics")
void monthQueryTestTeamsExist() throws StreamException {
QueryTeamUsageStatsResponse response =
TeamUsageStats.queryTeamUsageStats().month("2026-02").request();
for (String teamName : List.of("sdk-test-team-1", "sdk-test-team-2", "sdk-test-team-3")) {
TeamUsageStats team = findTeamByName(response, teamName);
assertNotNull(team, teamName + " should exist");
assertMetricsNonNegative(team, teamName);
}
}
}
@Nested
@DisplayName("Data Correctness - No Parameters Query")
class DataCorrectnessNoParams {
@Test
@DisplayName("No params: test teams exist with valid metrics")
void noParamsTestTeamsExist() throws StreamException {
QueryTeamUsageStatsResponse response = TeamUsageStats.queryTeamUsageStats().request();
for (String teamName : List.of("sdk-test-team-1", "sdk-test-team-2", "sdk-test-team-3")) {
TeamUsageStats team = findTeamByName(response, teamName);
assertNotNull(team, teamName + " should exist");
assertMetricsNonNegative(team, teamName);
}
}
}
@Nested
@DisplayName("Data Correctness - Pagination Query")
class DataCorrectnessPagination {
@Test
@DisplayName("Pagination: finds test teams across pages")
void paginationFindsTestTeams() throws StreamException {
for (String teamName : List.of("sdk-test-team-1", "sdk-test-team-2", "sdk-test-team-3")) {
TeamUsageStats team = findTeamAcrossPages(teamName);
assertNotNull(team, teamName + " should exist across paginated results");
assertMetricsNonNegative(team, teamName);
}
}
private TeamUsageStats findTeamAcrossPages(String teamName) throws StreamException {
String nextCursor = null;
int maxPages = 10; // Safety limit
for (int page = 0; page < maxPages; page++) {
var requestBuilder = TeamUsageStats.queryTeamUsageStats().limit(5);
if (nextCursor != null) {
requestBuilder = requestBuilder.next(nextCursor);
}
QueryTeamUsageStatsResponse response = requestBuilder.request();
TeamUsageStats found = findTeamByName(response, teamName);
if (found != null) {
return found;
}
nextCursor = response.getNext();
if (nextCursor == null || nextCursor.isEmpty()) {
break; // No more pages
}
}
return null;
}
}
// Helper methods shared across nested classes
private static TeamUsageStats findTeamByName(
QueryTeamUsageStatsResponse response, String teamName) {
for (TeamUsageStats team : response.getTeams()) {
if (teamName.equals(team.getTeam())) {
return team;
}
}
return null;
}
private static void assertMetricsNonNegative(TeamUsageStats team, String teamName) {
assertTrue(
team.getUsersDaily().getTotal() >= 0, teamName + " users_daily should be non-negative");
assertTrue(
team.getMessagesDaily().getTotal() >= 0,
teamName + " messages_daily should be non-negative");
assertTrue(
team.getUsersTotal().getTotal() >= 0, teamName + " users_total should be non-negative");
assertTrue(
team.getMessagesTotal().getTotal() >= 0,
teamName + " messages_total should be non-negative");
}
private static void assertAllMetricsExact(TeamUsageStats team, String teamName) {
// Daily activity metrics
assertEquals(0, team.getUsersDaily().getTotal(), teamName + " users_daily");
assertEquals(100, team.getMessagesDaily().getTotal(), teamName + " messages_daily");
assertEquals(0, team.getTranslationsDaily().getTotal(), teamName + " translations_daily");
assertEquals(
0, team.getImageModerationDaily().getTotal(), teamName + " image_moderations_daily");
// Peak metrics
assertEquals(0, team.getConcurrentUsers().getTotal(), teamName + " concurrent_users");
assertEquals(
0, team.getConcurrentConnections().getTotal(), teamName + " concurrent_connections");
// User rolling/cumulative metrics
assertEquals(5, team.getUsersTotal().getTotal(), teamName + " users_total");
assertEquals(5, team.getUsersLast24Hours().getTotal(), teamName + " users_last_24_hours");
assertEquals(5, team.getUsersLast30Days().getTotal(), teamName + " users_last_30_days");
assertEquals(5, team.getUsersMonthToDate().getTotal(), teamName + " users_month_to_date");
assertEquals(
0, team.getUsersEngagedLast30Days().getTotal(), teamName + " users_engaged_last_30_days");
assertEquals(
0, team.getUsersEngagedMonthToDate().getTotal(), teamName + " users_engaged_month_to_date");
// Message rolling/cumulative metrics
assertEquals(100, team.getMessagesTotal().getTotal(), teamName + " messages_total");
assertEquals(
100, team.getMessagesLast24Hours().getTotal(), teamName + " messages_last_24_hours");
assertEquals(100, team.getMessagesLast30Days().getTotal(), teamName + " messages_last_30_days");
assertEquals(
100, team.getMessagesMonthToDate().getTotal(), teamName + " messages_month_to_date");
}
}