-
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.
Merge pull request #7 from stelitop/v0.0.3
V0.0.3
- Loading branch information
Showing
11 changed files
with
340 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,75 @@ | ||
package net.stelitop.mad4j; | ||
|
||
import discord4j.core.GatewayDiscordClient; | ||
import net.stelitop.mad4j.autocomplete.AutocompletionExecutor; | ||
import net.stelitop.mad4j.listeners.CommandOptionAutocompleteListener; | ||
import net.stelitop.mad4j.listeners.ComponentEventListener; | ||
import net.stelitop.mad4j.listeners.SlashCommandListener; | ||
import net.stelitop.mad4j.requirements.CommandRequirementExecutor; | ||
import org.checkerframework.checker.units.qual.C; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.env.Environment; | ||
|
||
import java.util.List; | ||
|
||
@ComponentScan("net.stelitop.mad4j") | ||
@Configuration | ||
public class Mad4jConfig { | ||
|
||
} | ||
//@Configuration | ||
//public class Mad4jConfig { | ||
// | ||
// @Bean | ||
// public SlashCommandRegistrar slashCommandRegistrar( | ||
// GatewayDiscordClient gatewayDiscordClient, | ||
// CommandOptionAutocompleteListener commandOptionAutocompleteListener, | ||
// ApplicationContext applicationContext, | ||
// Environment environment | ||
// ) { | ||
// return new SlashCommandRegistrar( | ||
// gatewayDiscordClient, | ||
// commandOptionAutocompleteListener, | ||
// applicationContext, | ||
// environment | ||
// ); | ||
// } | ||
// | ||
// @Bean | ||
// public CommandOptionAutocompleteListener commandOptionAutocompleteListener( | ||
// GatewayDiscordClient client, | ||
// List<AutocompletionExecutor> autocompletionExecutors | ||
// ) { | ||
// return new CommandOptionAutocompleteListener( | ||
// client, | ||
// autocompletionExecutors | ||
// ); | ||
// } | ||
// | ||
// @Bean | ||
// public ComponentEventListener componentEventListener( | ||
// GatewayDiscordClient client, | ||
// ApplicationContext applicationContext | ||
// ) { | ||
// return new ComponentEventListener( | ||
// client, | ||
// applicationContext | ||
// ); | ||
// } | ||
// | ||
// @Bean | ||
// public SlashCommandListener slashCommandListener( | ||
// ApplicationContext applicationContext, | ||
// GatewayDiscordClient client, | ||
// List<CommandRequirementExecutor> possibleRequirements | ||
// ) { | ||
// return new SlashCommandListener( | ||
// applicationContext, | ||
// client, | ||
// possibleRequirements | ||
// ); | ||
// } | ||
//} |
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
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
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
45 changes: 45 additions & 0 deletions
45
src/test/java/slashcommands/registering/BaseTestConfiguration.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,45 @@ | ||
package slashcommands.registering; | ||
|
||
import discord4j.core.GatewayDiscordClient; | ||
import discord4j.rest.RestClient; | ||
import discord4j.rest.service.ApplicationService; | ||
import org.springframework.boot.SpringBootConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
@SpringBootConfiguration | ||
@ComponentScan("net.stelitop.mad4j") | ||
public class BaseTestConfiguration { | ||
|
||
public static long TEST_APPLICATION_ID = 1L; | ||
|
||
@Bean | ||
public GatewayDiscordClient gatewayDiscordClient(RestClient restClientMock) { | ||
GatewayDiscordClient gatewayDiscordClientMock = mock(GatewayDiscordClient.class); | ||
when(gatewayDiscordClientMock.getRestClient()).thenReturn(restClientMock); | ||
when(gatewayDiscordClientMock.on(any(), any())).thenReturn(Flux.empty()); | ||
return gatewayDiscordClientMock; | ||
} | ||
|
||
@Bean | ||
public RestClient restClient(ApplicationService applicationServiceMock) { | ||
RestClient restClientMock = mock(RestClient.class); | ||
when(restClientMock.getApplicationId()).thenReturn(Mono.just(TEST_APPLICATION_ID)); | ||
when(restClientMock.getApplicationService()).thenReturn(applicationServiceMock); | ||
return restClientMock; | ||
} | ||
|
||
@Bean | ||
ApplicationService applicationService() { | ||
ApplicationService applicationServiceMock = mock(ApplicationService.class); | ||
when(applicationServiceMock.bulkOverwriteGlobalApplicationCommand(eq(TEST_APPLICATION_ID), any())).thenReturn(Flux.empty()); | ||
return applicationServiceMock; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/test/java/slashcommands/registering/BaseTestConfigurationTest.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,18 @@ | ||
package slashcommands.registering; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
@Import(BaseTestConfiguration.class) | ||
public class BaseTestConfigurationTest { | ||
|
||
@Test | ||
void testSpringTestContext() { | ||
|
||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/test/java/slashcommands/registering/CommandGroupTest.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,73 @@ | ||
package slashcommands.registering; | ||
|
||
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent; | ||
import discord4j.discordjson.json.ApplicationCommandOptionData; | ||
import discord4j.discordjson.json.ApplicationCommandRequest; | ||
import discord4j.rest.service.ApplicationService; | ||
import net.stelitop.mad4j.DiscordEventsComponent; | ||
import net.stelitop.mad4j.InteractionEvent; | ||
import net.stelitop.mad4j.commands.CommandParam; | ||
import net.stelitop.mad4j.commands.SlashCommand; | ||
import net.stelitop.mad4j.utils.OptionType; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
@Import({BaseTestConfiguration.class, CommandGroupTest.TestComponent.class}) | ||
public class CommandGroupTest { | ||
|
||
@Autowired | ||
private ApplicationService applicationServiceMock; | ||
|
||
@DiscordEventsComponent | ||
public static class TestComponent { | ||
|
||
static final String commandDescription = "Adds two numbers."; | ||
static final String param1Description = "The first number."; | ||
static final String param2Description = "The second number."; | ||
|
||
@SlashCommand(name = "add numbers", description = commandDescription) | ||
public Mono<Void> addTwoNumbersCommand( | ||
@InteractionEvent ChatInputInteractionEvent event, | ||
@CommandParam(name = "x", description = param1Description) long x, | ||
@CommandParam(name = "y", description = param2Description) long y | ||
) { | ||
return event.reply("The sum is " + x + y); | ||
} | ||
} | ||
|
||
@Test | ||
public void testLoadingCommand() { | ||
ArgumentCaptor<List<ApplicationCommandRequest>> argumentCaptor = ArgumentCaptor.forClass(List.class); | ||
verify(applicationServiceMock, times(1)).bulkOverwriteGlobalApplicationCommand( | ||
eq(BaseTestConfiguration.TEST_APPLICATION_ID), argumentCaptor.capture()); | ||
|
||
assertThat(argumentCaptor.getValue()).hasSize(1); | ||
ApplicationCommandRequest request = argumentCaptor.getValue().get(0); | ||
assertThat(request.name()).isEqualTo("add"); | ||
assertThat(request.options().get()).hasSize(1); | ||
assertThat(request.options().get().get(0).type()).isEqualTo(OptionType.SUB_COMMAND); | ||
assertThat(request.options().get().get(0).name()).isEqualTo("numbers"); | ||
assertThat(request.options().get().get(0).description()).isEqualTo(TestComponent.commandDescription); | ||
assertThat(request.options().get().get(0).options().get()).containsExactly( | ||
ApplicationCommandOptionData.builder().name("x").type(OptionType.INTEGER).required(true) | ||
.description(TestComponent.param1Description).build(), | ||
ApplicationCommandOptionData.builder().name("y").type(OptionType.INTEGER).required(true) | ||
.description(TestComponent.param2Description).build() | ||
); | ||
} | ||
} |
Oops, something went wrong.