-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move common Command logic to AbstractCommand
- Loading branch information
1 parent
e0fdb58
commit 7a0cd00
Showing
8 changed files
with
211 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 15 additions & 2 deletions
17
...ley/wheel/terra/service/EchoListener.java → ...iley/wheel/terra/service/EchoCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 0 additions & 39 deletions
39
terra/src/main/java/dev/jacksonbailey/wheel/terra/service/Foo.java
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
terra/src/test/java/dev/jacksonbailey/wheel/terra/TestUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package dev.jacksonbailey.wheel.terra; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
|
||
import java.util.function.Consumer; | ||
|
||
public final class TestUtils { | ||
|
||
private TestUtils() { | ||
throw new AssertionError(); | ||
} | ||
|
||
public static <T> Consumer<T> anyConsumerOf(T t) { | ||
@SuppressWarnings("unchecked") | ||
Consumer<T> toReturn = any(Consumer.class); | ||
return toReturn; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
terra/src/test/java/dev/jacksonbailey/wheel/terra/service/AbstractCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package dev.jacksonbailey.wheel.terra.service; | ||
|
||
import static dev.jacksonbailey.wheel.terra.TestUtils.anyConsumerOf; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import java.util.function.Consumer; | ||
import net.dv8tion.jda.api.JDA; | ||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; | ||
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.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.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class AbstractCommandTest { | ||
|
||
@Mock | ||
private CommandData commandData; | ||
|
||
@Mock | ||
private JDA jda; | ||
|
||
@InjectMocks | ||
private AbstractCommandStub abstractCommandStub; | ||
|
||
@Mock | ||
RestAction<Command> upsertRestAction; | ||
|
||
@Mock | ||
Consumer<Throwable> upsertCommandFailure; | ||
|
||
private static class AbstractCommandStub extends AbstractCommand { | ||
|
||
private final CommandData commandData; | ||
|
||
private final Consumer<Throwable> upsertCommandFailure; | ||
|
||
public AbstractCommandStub(JDA jda, CommandData commandData, | ||
Consumer<Throwable> upsertCommandFailure) { | ||
super(jda); | ||
this.commandData = commandData; | ||
this.upsertCommandFailure = upsertCommandFailure; | ||
} | ||
|
||
@Override | ||
public CommandData getCommandData() { | ||
return commandData; | ||
} | ||
|
||
@Override | ||
public MessageCreateData doCommand(@NotNull SlashCommandInteractionEvent event) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getCommandName() { | ||
return "stub"; | ||
} | ||
|
||
@Override | ||
public boolean isEphemeral() { | ||
return false; | ||
} | ||
|
||
protected Consumer<? super Throwable> onUpsertCommandFailure() { | ||
return upsertCommandFailure; | ||
} | ||
} | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
given(jda.upsertCommand(any(CommandData.class))).willReturn(upsertRestAction); | ||
} | ||
|
||
@Test | ||
void registerCommandAddsListener() { | ||
abstractCommandStub.registerCommand(); | ||
verify(jda).addEventListener(abstractCommandStub); | ||
} | ||
|
||
@Test | ||
void registerCommandUpsertsCommand() { | ||
abstractCommandStub.registerCommand(); | ||
verify(jda).upsertCommand(commandData); | ||
verify(upsertRestAction).queue(anyConsumerOf(Command.class), eq(upsertCommandFailure)); | ||
} | ||
|
||
// TODO Add more unit tests | ||
} |
38 changes: 38 additions & 0 deletions
38
terra/src/test/java/dev/jacksonbailey/wheel/terra/service/EchoCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package dev.jacksonbailey.wheel.terra.service; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import net.dv8tion.jda.api.JDA; | ||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; | ||
import net.dv8tion.jda.api.interactions.commands.OptionMapping; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class EchoCommandTest { | ||
|
||
@Mock | ||
private JDA jda; | ||
|
||
@InjectMocks | ||
private EchoCommand echoCommand; | ||
|
||
@Test | ||
void echoReturnsInput() { | ||
var event = mock(SlashCommandInteractionEvent.class); | ||
var optionMapping = mock(OptionMapping.class); | ||
given(event.getOption(EchoCommand.ECHO_PARAM_NAME)).willReturn(optionMapping); | ||
var input = "Hello, World!"; | ||
given(optionMapping.getAsString()).willReturn(input); | ||
|
||
try (var messageCreateData = echoCommand.doCommand(event)) { | ||
assertEquals(input, messageCreateData.getContent()); | ||
} | ||
} | ||
|
||
} |