1
1
package org .togetherjava .tjbot .jda ;
2
2
3
3
import net .dv8tion .jda .api .AccountType ;
4
+ import net .dv8tion .jda .api .JDA ;
4
5
import net .dv8tion .jda .api .Permission ;
5
6
import net .dv8tion .jda .api .entities .*;
6
7
import net .dv8tion .jda .api .events .interaction .command .SlashCommandInteractionEvent ;
38
39
import java .util .concurrent .ScheduledExecutorService ;
39
40
import java .util .concurrent .ScheduledThreadPoolExecutor ;
40
41
import java .util .function .Consumer ;
42
+ import java .util .function .Function ;
41
43
import java .util .function .Supplier ;
42
44
import java .util .function .UnaryOperator ;
43
45
@@ -130,6 +132,7 @@ public JdaTester() {
130
132
doReturn (APPLICATION_ID ).when (selfUser ).getApplicationIdLong ();
131
133
doReturn (selfUser ).when (jda ).getSelfUser ();
132
134
when (jda .getGuildChannelById (anyLong ())).thenReturn (textChannel );
135
+ when (jda .getTextChannelById (anyLong ())).thenReturn (textChannel );
133
136
when (jda .getPrivateChannelById (anyLong ())).thenReturn (privateChannel );
134
137
when (jda .getGuildById (anyLong ())).thenReturn (guild );
135
138
when (jda .getEntityBuilder ()).thenReturn (entityBuilder );
@@ -140,8 +143,6 @@ public JdaTester() {
140
143
doReturn (new Requester (jda , new AuthorizationConfig (TEST_TOKEN ))).when (jda ).getRequester ();
141
144
when (jda .getAccountType ()).thenReturn (AccountType .BOT );
142
145
143
- doReturn (messageAction ).when (privateChannel ).sendMessage (anyString ());
144
-
145
146
replyAction = mock (ReplyCallbackActionImpl .class );
146
147
when (replyAction .setEphemeral (anyBoolean ())).thenReturn (replyAction );
147
148
when (replyAction .addActionRow (anyCollection ())).thenReturn (replyAction );
@@ -155,7 +156,6 @@ public JdaTester() {
155
156
auditableRestAction = (AuditableRestActionImpl <Void >) mock (AuditableRestActionImpl .class );
156
157
doNothing ().when (auditableRestAction ).queue ();
157
158
158
- doNothing ().when (messageAction ).queue ();
159
159
doNothing ().when (webhookMessageUpdateAction ).queue ();
160
160
doReturn (webhookMessageUpdateAction ).when (webhookMessageUpdateAction )
161
161
.setActionRow (any (ItemComponent .class ));
@@ -164,6 +164,9 @@ public JdaTester() {
164
164
doReturn (selfMember ).when (guild ).getMember (selfUser );
165
165
doReturn (member ).when (guild ).getMember (not (eq (selfUser )));
166
166
167
+ RestAction <User > userAction = createSucceededActionMock (member .getUser ());
168
+ when (jda .retrieveUserById (anyLong ())).thenReturn (userAction );
169
+
167
170
doReturn (null ).when (textChannel ).retrieveMessageById (any ());
168
171
169
172
interactionHook = mock (InteractionHook .class );
@@ -172,6 +175,20 @@ public JdaTester() {
172
175
.thenReturn (webhookMessageUpdateAction );
173
176
when (interactionHook .editOriginal (any (byte [].class ), any (), any ()))
174
177
.thenReturn (webhookMessageUpdateAction );
178
+
179
+ doReturn (messageAction ).when (textChannel ).sendMessageEmbeds (any (), any ());
180
+ doReturn (messageAction ).when (textChannel ).sendMessageEmbeds (any ());
181
+
182
+ doNothing ().when (messageAction ).queue ();
183
+ when (messageAction .content (any ())).thenReturn (messageAction );
184
+
185
+ RestAction <PrivateChannel > privateChannelAction = createSucceededActionMock (privateChannel );
186
+ when (jda .openPrivateChannelById (anyLong ())).thenReturn (privateChannelAction );
187
+ when (jda .openPrivateChannelById (anyString ())).thenReturn (privateChannelAction );
188
+ doReturn (null ).when (privateChannel ).retrieveMessageById (any ());
189
+ doReturn (messageAction ).when (privateChannel ).sendMessage (anyString ());
190
+ doReturn (messageAction ).when (privateChannel ).sendMessageEmbeds (any (), any ());
191
+ doReturn (messageAction ).when (privateChannel ).sendMessageEmbeds (any ());
175
192
}
176
193
177
194
/**
@@ -246,6 +263,8 @@ public JdaTester() {
246
263
247
264
/**
248
265
* Creates a Mockito spy for a member with the given user id.
266
+ * <p>
267
+ * See {@link #getMemberSpy()} to get the default member used by this tester.
249
268
*
250
269
* @param userId the id of the member to create
251
270
* @return the created spy
@@ -255,6 +274,18 @@ public JdaTester() {
255
274
return spy (new MemberImpl (guild , user ));
256
275
}
257
276
277
+ /**
278
+ * Creates a Mockito spy for a text channel with the given channel id.
279
+ * <p>
280
+ * See {@link #getTextChannelSpy()} to get the default text channel used by this tester.
281
+ *
282
+ * @param channelId the id of the text channel to create
283
+ * @return the created spy
284
+ */
285
+ public @ NotNull TextChannel createTextChannelSpy (long channelId ) {
286
+ return spy (new TextChannelImpl (channelId , guild ));
287
+ }
288
+
258
289
/**
259
290
* Gets the Mockito mock used as universal reply action by all mocks created by this tester
260
291
* instance.
@@ -294,6 +325,42 @@ public JdaTester() {
294
325
return textChannel ;
295
326
}
296
327
328
+ /**
329
+ * Gets the private channel spy used as universal private channel by all mocks created by this
330
+ * tester instance.
331
+ * <p>
332
+ * For example {@link JDA#openPrivateChannelById(long)} will return this spy if used on the
333
+ * instance returned by {@link #getJdaMock()}.
334
+ *
335
+ * @return the private channel spy used by this tester
336
+ */
337
+ public @ NotNull PrivateChannel getPrivateChannelSpy () {
338
+ return privateChannel ;
339
+ }
340
+
341
+ /**
342
+ * Gets the member spy used as universal member by all mocks created by this tester instance.
343
+ * <p>
344
+ * For example the events created by {@link #createSlashCommandInteractionEvent(SlashCommand)}
345
+ * will return this spy on several of their methods.
346
+ * <p>
347
+ * See {@link #createMemberSpy(long)} to create other members.
348
+ *
349
+ * @return the member spy used by this tester
350
+ */
351
+ public @ NotNull Member getMemberSpy () {
352
+ return member ;
353
+ }
354
+
355
+ /**
356
+ * Gets the JDA mock used as universal instance by all mocks created by this tester instance.
357
+ *
358
+ * @return the JDA mock used by this tester
359
+ */
360
+ public @ NotNull JDA getJdaMock () {
361
+ return jda ;
362
+ }
363
+
297
364
/**
298
365
* Creates a mocked action that always succeeds and consumes the given object.
299
366
* <p>
@@ -325,11 +392,25 @@ public JdaTester() {
325
392
successConsumer .accept (t );
326
393
return null ;
327
394
};
395
+ Answer <RestAction <?>> mapExecution = invocation -> {
396
+ Function <? super T , ?> mapFunction = invocation .getArgument (0 );
397
+ Object result = mapFunction .apply (t );
398
+ return createSucceededActionMock (result );
399
+ };
400
+ Answer <RestAction <?>> flatMapExecution = invocation -> {
401
+ Function <? super T , RestAction <?>> flatMapFunction = invocation .getArgument (0 );
402
+ return flatMapFunction .apply (t );
403
+ };
328
404
329
405
doNothing ().when (action ).queue ();
330
406
331
407
doAnswer (successExecution ).when (action ).queue (any ());
332
408
doAnswer (successExecution ).when (action ).queue (any (), any ());
409
+ when (action .onErrorMap (any ())).thenReturn (action );
410
+ when (action .onErrorMap (any (), any ())).thenReturn (action );
411
+
412
+ doAnswer (mapExecution ).when (action ).map (any ());
413
+ doAnswer (flatMapExecution ).when (action ).flatMap (any ());
333
414
334
415
return action ;
335
416
}
@@ -366,11 +447,27 @@ public JdaTester() {
366
447
return null ;
367
448
};
368
449
450
+ Answer <RestAction <?>> errorMapExecution = invocation -> {
451
+ Function <? super Throwable , ?> mapFunction = invocation .getArgument (0 );
452
+ Object result = mapFunction .apply (failureReason );
453
+ return createSucceededActionMock (result );
454
+ };
455
+
456
+ Answer <RestAction <?>> mapExecution = invocation -> createFailedActionMock (failureReason );
457
+ Answer <RestAction <?>> flatMapExecution =
458
+ invocation -> createFailedActionMock (failureReason );
459
+
369
460
doNothing ().when (action ).queue ();
370
461
doNothing ().when (action ).queue (any ());
371
462
463
+ doAnswer (errorMapExecution ).when (action ).onErrorMap (any ());
464
+ doAnswer (errorMapExecution ).when (action ).onErrorMap (any (), any ());
465
+
372
466
doAnswer (failureExecution ).when (action ).queue (any (), any ());
373
467
468
+ doAnswer (mapExecution ).when (action ).map (any ());
469
+ doAnswer (flatMapExecution ).when (action ).flatMap (any ());
470
+
374
471
return action ;
375
472
}
376
473
0 commit comments