Skip to content

Commit fc9083a

Browse files
committed
Added tests for the tag-system
* and improved ping tests
1 parent 1980a50 commit fc9083a

9 files changed

Lines changed: 816 additions & 16 deletions

File tree

application/src/main/java/org/togetherjava/tjbot/commands/tags/TagCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
public final class TagCommand extends SlashCommandAdapter {
2323
private final TagSystem tagSystem;
2424

25-
private static final String ID_OPTION = "id";
26-
private static final String REPLY_TO_USER_OPTION = "reply-to";
25+
static final String ID_OPTION = "id";
26+
static final String REPLY_TO_USER_OPTION = "reply-to";
2727

2828
/**
2929
* Creates a new instance, using the given tag system as base.

application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@
4343
*/
4444
public final class TagManageCommand extends SlashCommandAdapter {
4545
private static final Logger logger = LoggerFactory.getLogger(TagManageCommand.class);
46-
private static final String ID_OPTION = "id";
46+
static final String ID_OPTION = "id";
4747
private static final String ID_DESCRIPTION = "the id of the tag";
48-
private static final String CONTENT_OPTION = "content";
48+
static final String CONTENT_OPTION = "content";
4949
private static final String CONTENT_DESCRIPTION = "the content of the tag";
50-
private static final String MESSAGE_ID_OPTION = "message-id";
50+
static final String MESSAGE_ID_OPTION = "message-id";
5151
private static final String MESSAGE_ID_DESCRIPTION = "the id of the message to refer to";
5252
private final TagSystem tagSystem;
5353
private final Predicate<String> hasRequiredRole;
@@ -295,7 +295,7 @@ private enum TagStatus {
295295
}
296296

297297

298-
private enum Subcommand {
298+
enum Subcommand {
299299
RAW("raw"),
300300
CREATE("create"),
301301
CREATE_WITH_MESSAGE("create-with-message"),
@@ -305,11 +305,16 @@ private enum Subcommand {
305305

306306
private final String name;
307307

308-
Subcommand(String name) {
308+
Subcommand(@NotNull String name) {
309309
this.name = name;
310310
}
311311

312-
static Subcommand fromName(String name) {
312+
@NotNull
313+
String getName() {
314+
return name;
315+
}
316+
317+
static Subcommand fromName(@NotNull String name) {
313318
for (Subcommand subcommand : Subcommand.values()) {
314319
if (subcommand.name.equals(name)) {
315320
return subcommand;

application/src/main/java/org/togetherjava/tjbot/commands/tags/TagsCommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
import net.dv8tion.jda.api.events.interaction.ButtonClickEvent;
66
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
77
import org.jetbrains.annotations.NotNull;
8+
import org.slf4j.Logger;
89
import org.slf4j.LoggerFactory;
910
import org.togetherjava.tjbot.commands.SlashCommandAdapter;
1011
import org.togetherjava.tjbot.commands.SlashCommandVisibility;
12+
1113
import java.time.Instant;
12-
import org.slf4j.Logger;
1314
import java.util.Collection;
1415
import java.util.List;
1516
import java.util.Objects;

application/src/main/java/org/togetherjava/tjbot/config/Config.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
/**
1515
* Configuration of the application. Create instances using {@link #load(Path)}.
1616
*/
17-
@SuppressWarnings("ClassCanBeRecord")
1817
public final class Config {
1918

2019
private final String token;
Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
11
package org.togetherjava.tjbot.commands.basic;
22

33
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.DisplayName;
47
import org.junit.jupiter.api.Test;
58
import org.togetherjava.tjbot.commands.SlashCommand;
69
import org.togetherjava.tjbot.jda.JdaTester;
710

8-
import static org.mockito.Mockito.times;
911
import static org.mockito.Mockito.verify;
1012

1113
final class PingCommandTest {
12-
@Test
13-
void pingCommand() {
14-
SlashCommand command = new PingCommand();
15-
JdaTester jdaTester = new JdaTester();
14+
private JdaTester jdaTester;
15+
private SlashCommand command;
1616

17+
private @NotNull SlashCommandEvent triggerSlashCommand() {
1718
SlashCommandEvent event = jdaTester.createSlashCommandEvent(command).build();
1819
command.onSlashCommand(event);
20+
return event;
21+
}
22+
23+
@BeforeEach
24+
void setUp() {
25+
jdaTester = new JdaTester();
26+
command = jdaTester.spySlashCommand(new PingCommand());
27+
}
28+
29+
@Test
30+
@DisplayName("'/ping' responds with pong")
31+
void pingRespondsWithPong() {
32+
// GIVEN
33+
// WHEN using '/ping'
34+
SlashCommandEvent event = triggerSlashCommand();
1935

20-
verify(event, times(1)).reply("Pong!");
36+
// THEN the bot replies with pong
37+
verify(event).reply("Pong!");
2138
}
2239
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package org.togetherjava.tjbot.commands.tags;
2+
3+
import net.dv8tion.jda.api.entities.Member;
4+
import net.dv8tion.jda.api.entities.MessageEmbed;
5+
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
6+
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.DisplayName;
10+
import org.junit.jupiter.api.Test;
11+
import org.togetherjava.tjbot.commands.SlashCommand;
12+
import org.togetherjava.tjbot.db.Database;
13+
import org.togetherjava.tjbot.db.generated.tables.Tags;
14+
import org.togetherjava.tjbot.jda.JdaTester;
15+
import org.togetherjava.tjbot.jda.SlashCommandEventBuilder;
16+
17+
import static org.mockito.Mockito.*;
18+
19+
final class TagCommandTest {
20+
private TagSystem system;
21+
private JdaTester jdaTester;
22+
private SlashCommand command;
23+
24+
@BeforeEach
25+
void setUp() {
26+
Database database = Database.createMemoryDatabase(Tags.TAGS);
27+
system = spy(new TagSystem(database));
28+
jdaTester = new JdaTester();
29+
command = new TagCommand(system);
30+
}
31+
32+
private @NotNull SlashCommandEvent triggerSlashCommand(@NotNull String id,
33+
@Nullable Member userToReplyTo) {
34+
SlashCommandEventBuilder builder =
35+
jdaTester.createSlashCommandEvent(command).setOption(TagCommand.ID_OPTION, id);
36+
if (userToReplyTo != null) {
37+
builder.setOption(TagCommand.REPLY_TO_USER_OPTION, userToReplyTo);
38+
}
39+
40+
SlashCommandEvent event = builder.build();
41+
command.onSlashCommand(event);
42+
return event;
43+
}
44+
45+
@Test
46+
@DisplayName("Respond that the tag could not be found if the system has no tags registered yet")
47+
void canNotFindTagInEmptySystem() {
48+
// GIVEN a system without any tags registered
49+
// WHEN triggering the slash command '/tag id:first'
50+
SlashCommandEvent event = triggerSlashCommand("first", null);
51+
52+
// THEN responds that the tag could not be found
53+
verify(event).reply("Could not find any tag with id 'first'.");
54+
}
55+
56+
@Test
57+
@DisplayName("Respond that the tag could not be found but suggest a different tag instead, if the system has a different tag registered")
58+
void canNotFindTagSuggestDifferentTag() {
59+
// GIVEN a system with the tag "first" registered
60+
system.putTag("first", "foo");
61+
62+
// WHEN triggering the slash command '/tag id:second'
63+
SlashCommandEvent event = triggerSlashCommand("second", null);
64+
65+
// THEN responds that the tag could not be found and instead suggests using the other tag
66+
verify(event)
67+
.reply("Could not find any tag with id 'second', did you perhaps mean 'first'?");
68+
}
69+
70+
@Test
71+
@DisplayName("Respond with the tags content if the tag could be found")
72+
void canFindTheTagAndRespondWithContent() {
73+
// GIVEN a system with the tag "first" registered
74+
system.putTag("first", "foo");
75+
76+
// WHEN triggering the slash command '/tag id:first'
77+
SlashCommandEvent event = triggerSlashCommand("first", null);
78+
79+
// THEN finds the tag and responds with its content
80+
verify(event).replyEmbeds(any(MessageEmbed.class));
81+
}
82+
83+
@Test
84+
@DisplayName("Replies to given users and responds with the tags content if the tag could be found and a user is given")
85+
void canFindTagsAndRepliesToUser() {
86+
// GIVEN a system with the tag "first" registered and a user to reply to
87+
system.putTag("first", "foo");
88+
Member userToReplyTo = jdaTester.createMemberSpy(1);
89+
90+
// WHEN triggering the slash command '/tag id:first reply-to:...' with that user
91+
SlashCommandEvent event = triggerSlashCommand("first", userToReplyTo);
92+
93+
// THEN responds with the tags content and replies to the user
94+
verify(event).replyEmbeds(any(MessageEmbed.class));
95+
verify(jdaTester.getReplyActionMock()).setContent(userToReplyTo.getAsMention());
96+
}
97+
}

0 commit comments

Comments
 (0)