1515import java .util .concurrent .TimeoutException ;
1616import java .util .function .Supplier ;
1717import java .util .stream .Collectors ;
18- import org .apache .commons .lang3 .RandomStringUtils ;
1918import org .junit .jupiter .api .Assertions ;
2019import org .junit .jupiter .api .BeforeAll ;
2120
2221public class BasicTest {
23- private static boolean environmentInitialized ;
2422 protected static UserRequestObject testUserRequestObject ;
2523 protected static List <UserRequestObject > testUsersRequestObjects = new ArrayList <>();
2624 protected static ChannelGetResponse testChannelGetResponse ;
2725 protected static Channel testChannel ;
2826 protected static Message testMessage ;
2927
3028 @ BeforeAll
31- static synchronized void setup ()
32- throws StreamException , SecurityException , IllegalArgumentException {
33- // failOnUnknownProperties();
29+ static void setup () throws StreamException , SecurityException , IllegalArgumentException {
3430 setProperties ();
35- if (!environmentInitialized ) {
36- cleanChannels ();
37- cleanChannelTypes ();
38- cleanBlocklists ();
39- cleanCommands ();
40- cleanUsers ();
41- environmentInitialized = true ;
42- }
4331 upsertUsers ();
4432 createTestChannel ();
4533 createTestMessage ();
4634 }
4735
48- private static void cleanChannels () throws StreamException {
49- while (true ) {
50- List <String > channels =
51- Channel .list ().request ().getChannels ().stream ()
52- .map (channel -> channel .getChannel ().getCId ())
53- .collect (Collectors .toList ());
54-
55- if (channels .size () == 0 ) {
56- break ;
57- }
58-
59- var deleteManyResponse =
60- Channel .deleteMany (channels ).setDeleteStrategy (DeleteStrategy .HARD ).request ();
61- String taskId = deleteManyResponse .getTaskId ();
62- Assertions .assertNotNull (taskId );
63-
64- System .out .printf ("Waiting for channel deletion task %s to complete...\n " , taskId );
65-
66- while (true ) {
67- TaskStatusGetResponse response = TaskStatus .get (taskId ).request ();
68- String status = response .getStatus ();
69-
70- if (status .equals ("completed" ) || status .equals ("ok" )) {
71- break ;
72- }
73- if (status .equals ("failed" ) || status .equals ("error" )) {
74- throw new StreamException (
75- String .format ("Failed to delete channel(task_id: %s): %s" , response .getId (), status ),
76- (Throwable ) null );
77- }
78-
79- // wait for the channels to delete
80- Assertions .assertDoesNotThrow (() -> java .lang .Thread .sleep (500 ));
81- }
82-
83- waitFor (
84- () ->
85- Assertions .assertDoesNotThrow (() -> Channel .list ().request ().getChannels ().isEmpty ()),
86- 1000L ,
87- 60000L );
88- }
89- }
90-
91- private static void cleanUsers () throws StreamException {
92- while (true ) {
93- List <String > users =
94- User .list ().request ().getUsers ().stream ()
95- .map (user -> user .getId ())
96- .collect (Collectors .toList ());
97-
98- if (users .size () == 0 ) {
99- break ;
100- }
101-
102- var deleteManyResponse =
103- User .deleteMany (users ).deleteUserStrategy (DeleteStrategy .HARD ).request ();
104- String taskId = deleteManyResponse .getTaskId ();
105- Assertions .assertNotNull (taskId );
106-
107- System .out .printf ("Waiting for user deletion task %s to complete...\n " , taskId );
108-
109- while (true ) {
110- TaskStatusGetResponse response = TaskStatus .get (taskId ).request ();
111- String status = response .getStatus ();
112-
113- if (status .equals ("completed" ) || status .equals ("ok" )) {
114- break ;
115- }
116- if (status .equals ("failed" ) || status .equals ("error" )) {
117- throw new StreamException (
118- String .format ("Failed to delete user(task_id: %s): %s" , response .getId (), status ),
119- (Throwable ) null );
120- }
121-
122- // wait for the channels to delete
123- Assertions .assertDoesNotThrow (() -> java .lang .Thread .sleep (500 ));
124- }
125-
126- waitFor (
127- () -> Assertions .assertDoesNotThrow (() -> User .list ().request ().getUsers ().isEmpty ()),
128- 1000L ,
129- 60000L );
130- }
131- }
132-
133- private static void cleanChannelTypes () throws StreamException {
134- ChannelType .list ()
135- .request ()
136- .getChannelTypes ()
137- .values ()
138- .forEach (
139- channelType -> {
140- try {
141- ChannelType .delete (channelType .getName ()).request ();
142- } catch (StreamException e ) {
143- // Do nothing. Happens when there are channels of that type
144- }
145- });
146- }
147-
148- private static void cleanBlocklists () throws StreamException {
149- Blocklist .list ()
150- .request ()
151- .getBlocklists ()
152- .forEach (
153- blocklist -> {
154- try {
155- Blocklist .delete (blocklist .getName ()).request ();
156- } catch (StreamException e ) {
157- // Do nothing this happens for built in
158- }
159- });
160- }
161-
162- private static void cleanCommands () throws StreamException {
163- Command .list ()
164- .request ()
165- .getCommands ()
166- .forEach (
167- command -> {
168- try {
169- Command .delete (command .getName ()).request ();
170- } catch (StreamException e ) {
171- // Do nothing
172- }
173- });
174-
175- waitFor (
176- () -> {
177- var commands =
178- Assertions .assertDoesNotThrow (() -> Command .list ().request ().getCommands ());
179- return commands .size () == 5 ; // Built-in 5 commands
180- });
181- }
182-
18336 private static void createTestMessage () throws StreamException {
18437 waitFor (
18538 () -> {
@@ -212,26 +65,14 @@ private static void createTestChannel() throws StreamException {
21265 static void upsertUsers () throws StreamException {
21366 testUsersRequestObjects .clear ();
21467 testUserRequestObject =
215- UserRequestObject .builder ()
216- .id (RandomStringUtils .randomAlphabetic (10 ))
217- .name ("Gandalf the Grey" )
218- .build ();
68+ UserRequestObject .builder ().id (uniqueId ("gandalf" )).name ("Gandalf the Grey" ).build ();
21969 testUsersRequestObjects .add (testUserRequestObject );
22070 testUsersRequestObjects .add (
221- UserRequestObject .builder ()
222- .id (RandomStringUtils .randomAlphabetic (10 ))
223- .name ("Frodo Baggins" )
224- .build ());
71+ UserRequestObject .builder ().id (uniqueId ("frodo" )).name ("Frodo Baggins" ).build ());
22572 testUsersRequestObjects .add (
226- UserRequestObject .builder ()
227- .id (RandomStringUtils .randomAlphabetic (10 ))
228- .name ("Frodo Baggins" )
229- .build ());
73+ UserRequestObject .builder ().id (uniqueId ("pippin" )).name ("Frodo Baggins" ).build ());
23074 testUsersRequestObjects .add (
231- UserRequestObject .builder ()
232- .id (RandomStringUtils .randomAlphabetic (10 ))
233- .name ("Samwise Gamgee" )
234- .build ());
75+ UserRequestObject .builder ().id (uniqueId ("sam" )).name ("Samwise Gamgee" ).build ());
23576 UserUpsertRequest usersUpsertRequest = User .upsert ();
23677 testUsersRequestObjects .forEach (user -> usersUpsertRequest .user (user ));
23778 usersUpsertRequest .request ();
@@ -268,7 +109,7 @@ protected static List<ChannelMemberRequestObject> buildChannelMembersList() {
268109 }
269110
270111 protected static ChannelGetResponse createRandomChannel () throws StreamException {
271- return Channel .getOrCreate ("team" , RandomStringUtils . randomAlphabetic ( 12 ))
112+ return Channel .getOrCreate ("team" , uniqueId ( "channel" ))
272113 .data (
273114 ChannelRequestObject .builder ()
274115 .createdBy (testUserRequestObject )
@@ -277,6 +118,10 @@ protected static ChannelGetResponse createRandomChannel() throws StreamException
277118 .request ();
278119 }
279120
121+ protected static String uniqueId (String prefix ) {
122+ return prefix + "-" + UUID .randomUUID ();
123+ }
124+
280125 protected static Message sendTestMessage () throws StreamException {
281126 String text = UUID .randomUUID ().toString ();
282127 MessageRequestObject messageRequest =
0 commit comments