Skip to content

Commit

Permalink
Add more tests for AbstractCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonBailey committed Jul 1, 2024
1 parent 21c0d2c commit 6640f2b
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 12 deletions.
13 changes: 13 additions & 0 deletions terra/src/test/java/dev/jacksonbailey/wheel/terra/TestUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package dev.jacksonbailey.wheel.terra;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.nullable;

import java.util.concurrent.Future;
import java.util.function.Consumer;

public final class TestUtils {
Expand All @@ -16,5 +18,16 @@ public static <T> Consumer<T> anyConsumerOf(T t) {
return toReturn;
}

public static <T> Consumer<T> nullableConsumerOf(T t) {
@SuppressWarnings("unchecked")
Consumer<T> toReturn = nullable(Consumer.class);
return toReturn;
}

public static <T> Future<T> anyFutureOf(T t) {
@SuppressWarnings("unchecked")
Future<T> toReturn = any(Future.class);
return toReturn;
}

}
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
package dev.jacksonbailey.wheel.terra.service;

import static dev.jacksonbailey.wheel.terra.TestUtils.anyConsumerOf;
import static dev.jacksonbailey.wheel.terra.TestUtils.nullableConsumerOf;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.InteractionHook;
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.requests.restaction.WebhookMessageCreateAction;
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
Expand All @@ -31,7 +45,7 @@ class AbstractCommandTest {
private JDA jda;

@InjectMocks
private AbstractCommandStub abstractCommandStub;
private AbstractCommandStub commandStub;

@Mock
RestAction<Command> upsertRestAction;
Expand Down Expand Up @@ -77,23 +91,83 @@ protected Consumer<? super Throwable> onUpsertCommandFailure() {
}
}

@BeforeEach
void beforeEach() {
@Test
void registerCommand_addsListener() {
given(jda.upsertCommand(any(CommandData.class))).willReturn(upsertRestAction);
}

@Test
void registerCommandAddsListener() {
abstractCommandStub.registerCommand();
verify(jda).addEventListener(abstractCommandStub);
commandStub.registerCommand();

verify(jda).addEventListener(commandStub);
}

@Test
void registerCommandUpsertsCommand() {
abstractCommandStub.registerCommand();
void registerCommand_upsertsCommand() {
given(jda.upsertCommand(any(CommandData.class))).willReturn(upsertRestAction);

commandStub.registerCommand();

verify(jda).upsertCommand(commandData);
verify(upsertRestAction).queue(anyConsumerOf(Command.class), eq(upsertCommandFailure));
}

// TODO Add more unit tests
@Test
void onSlashCommand_doesNotTriggersOnWrongCommand() {
var event = mock(SlashCommandInteractionEvent.class);
given(event.getName()).willReturn(UUID.randomUUID().toString());

commandStub.onSlashCommandInteraction(event);

verify(event).getName();
verifyNoMoreInteractions(event);
}

@Test
void onSlashCommand_defersReply() {
var event = mock(SlashCommandInteractionEvent.class);
var callback = mock(ReplyCallbackAction.class);
given(event.getName()).willReturn(commandStub.getCommandName());
given(event.deferReply(anyBoolean())).willReturn(callback);

commandStub.onSlashCommandInteraction(event);

verify(event).getName();
verify(event).deferReply(commandStub.isEphemeral());
verify(callback).queue(anyConsumerOf(InteractionHook.class),
nullableConsumerOf(Throwable.class));
}

@ParameterizedTest
@ValueSource(classes = {ExecutionException.class, InterruptedException.class})
void onDeferReplySuccess_throwsWhenMessageFutureFails(Class<? extends Throwable> thrown)
throws Exception {
InteractionHook hook = mock(InteractionHook.class);
Throwable thrownByMessageFuture = mock(thrown);
@SuppressWarnings("unchecked")
Future<MessageCreateData> messageFuture = mock(Future.class);
given(messageFuture.get()).willThrow(thrownByMessageFuture);

var actualException = assertThrows(RuntimeException.class,
() -> commandStub.onDeferReplySuccess(hook, messageFuture));

assertThat(actualException).cause().isSameAs(thrownByMessageFuture);
}

@Test
void onDeferReplySuccess_hookIsCalled() throws Exception {
@SuppressWarnings("unchecked")
Future<MessageCreateData> messageFuture = mock(Future.class);
var messageCreateData = mock(MessageCreateData.class);
given(messageFuture.get()).willReturn(messageCreateData);

var hook = mock(InteractionHook.class);
@SuppressWarnings("unchecked")
WebhookMessageCreateAction<Message> callback = mock(WebhookMessageCreateAction.class);
given(hook.sendMessage(messageCreateData)).willReturn(callback);

commandStub.onDeferReplySuccess(hook, messageFuture);

verify(hook).sendMessage(messageCreateData);
verify(callback).queue(anyConsumerOf(Message.class), nullableConsumerOf(Throwable.class));
}

}

0 comments on commit 6640f2b

Please sign in to comment.