Merge pull request #820 from alexengrig/command-registry-lazy-bot-username
CommandRegistry: Replace botUsername with botUsernameSupplier
This commit is contained in:
commit
3898480a87
@ -53,7 +53,7 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB
|
|||||||
*/
|
*/
|
||||||
public TelegramLongPollingCommandBot(DefaultBotOptions options, boolean allowCommandsWithUsername) {
|
public TelegramLongPollingCommandBot(DefaultBotOptions options, boolean allowCommandsWithUsername) {
|
||||||
super(options);
|
super(options);
|
||||||
this.commandRegistry = new CommandRegistry(allowCommandsWithUsername, this.getBotUsername());
|
this.commandRegistry = new CommandRegistry(allowCommandsWithUsername, this::getBotUsername);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -7,7 +7,9 @@ import java.util.Arrays;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -19,17 +21,32 @@ public final class CommandRegistry implements ICommandRegistry {
|
|||||||
|
|
||||||
private final Map<String, IBotCommand> commandRegistryMap = new HashMap<>();
|
private final Map<String, IBotCommand> commandRegistryMap = new HashMap<>();
|
||||||
private final boolean allowCommandsWithUsername;
|
private final boolean allowCommandsWithUsername;
|
||||||
private final String botUsername;
|
private final Supplier<String> botUsernameSupplier;
|
||||||
private BiConsumer<AbsSender, Message> defaultConsumer;
|
private BiConsumer<AbsSender, Message> defaultConsumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a Command registry
|
* Creates a Command registry
|
||||||
|
*
|
||||||
* @param allowCommandsWithUsername True to allow commands with username, false otherwise
|
* @param allowCommandsWithUsername True to allow commands with username, false otherwise
|
||||||
* @param botUsername Bot username
|
* @param botUsername Bot username
|
||||||
|
* @throws java.lang.NullPointerException if {@code botUsername} is {@code null}
|
||||||
|
* @deprecated Use {@link #CommandRegistry(boolean, java.util.function.Supplier)} instead
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public CommandRegistry(boolean allowCommandsWithUsername, String botUsername) {
|
public CommandRegistry(boolean allowCommandsWithUsername, String botUsername) {
|
||||||
|
Objects.requireNonNull(botUsername, "Bot username must not be null");
|
||||||
this.allowCommandsWithUsername = allowCommandsWithUsername;
|
this.allowCommandsWithUsername = allowCommandsWithUsername;
|
||||||
this.botUsername = botUsername;
|
this.botUsernameSupplier = () -> botUsername;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Command registry
|
||||||
|
* @param allowCommandsWithUsername True to allow commands with username, false otherwise
|
||||||
|
* @param botUsernameSupplier Bot username supplier
|
||||||
|
*/
|
||||||
|
public CommandRegistry(boolean allowCommandsWithUsername, Supplier<String> botUsernameSupplier) {
|
||||||
|
this.allowCommandsWithUsername = allowCommandsWithUsername;
|
||||||
|
this.botUsernameSupplier = botUsernameSupplier;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -120,9 +137,12 @@ public final class CommandRegistry implements ICommandRegistry {
|
|||||||
* the command
|
* the command
|
||||||
* @param command Command to simplify
|
* @param command Command to simplify
|
||||||
* @return Simplified command
|
* @return Simplified command
|
||||||
|
* @throws java.lang.NullPointerException if {@code allowCommandsWithUsername} is {@code true}
|
||||||
|
* and {@code botUsernameSupplier} returns {@code null}
|
||||||
*/
|
*/
|
||||||
private String removeUsernameFromCommandIfNeeded(String command) {
|
private String removeUsernameFromCommandIfNeeded(String command) {
|
||||||
if (allowCommandsWithUsername) {
|
if (allowCommandsWithUsername) {
|
||||||
|
String botUsername = Objects.requireNonNull(botUsernameSupplier.get(), "Bot username must not be null");
|
||||||
return command.replaceAll("(?i)@" + Pattern.quote(botUsername), "").trim();
|
return command.replaceAll("(?i)@" + Pattern.quote(botUsername), "").trim();
|
||||||
}
|
}
|
||||||
return command;
|
return command;
|
||||||
|
@ -0,0 +1,51 @@
|
|||||||
|
package org.telegram.telegrambots.extensions.bots.commandbot.commands;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.telegram.telegrambots.meta.api.objects.Message;
|
||||||
|
import org.telegram.telegrambots.meta.bots.AbsSender;
|
||||||
|
|
||||||
|
class CommandRegistryTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_create_registry() {
|
||||||
|
CommandRegistry registry = new CommandRegistry(true, "BotUsername");
|
||||||
|
Assertions.assertNotNull(registry, "CommandRegistry is not created");
|
||||||
|
NullPointerException exception = Assertions.assertThrows(NullPointerException.class,
|
||||||
|
() -> new CommandRegistry(true, (String) null));
|
||||||
|
Assertions.assertEquals("Bot username must not be null", exception.getMessage(), "Invalid exception message");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_executes_commandWithBotUsername() {
|
||||||
|
CommandRegistry registry = new CommandRegistry(true, () -> "BotUsername");
|
||||||
|
IBotCommand command = Mockito.mock(IBotCommand.class);
|
||||||
|
Mockito.when(command.getCommandIdentifier()).thenReturn("command");
|
||||||
|
registry.register(command);
|
||||||
|
AbsSender absSender = Mockito.mock(AbsSender.class);
|
||||||
|
Message message = Mockito.mock(Message.class);
|
||||||
|
Mockito.when(message.hasText()).thenReturn(true);
|
||||||
|
Mockito.when(message.getText()).thenReturn("/command@BotUsername I'll be test!");
|
||||||
|
Assertions.assertTrue(registry.executeCommand(absSender, message), "Command must be executed");
|
||||||
|
Mockito.verify(message).hasText();
|
||||||
|
Mockito.verify(message).getText();
|
||||||
|
Mockito.verify(command).processMessage(
|
||||||
|
Mockito.same(absSender), Mockito.same(message), Mockito.eq(new String[]{"I'll", "be", "test!"}));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_throws_NPE_on_executes_commandWithNullableBotUsername() {
|
||||||
|
CommandRegistry registry = new CommandRegistry(true, () -> null);
|
||||||
|
IBotCommand command = Mockito.mock(IBotCommand.class);
|
||||||
|
Mockito.when(command.getCommandIdentifier()).thenReturn("command");
|
||||||
|
registry.register(command);
|
||||||
|
AbsSender absSender = Mockito.mock(AbsSender.class);
|
||||||
|
Message message = Mockito.mock(Message.class);
|
||||||
|
Mockito.when(message.hasText()).thenReturn(true);
|
||||||
|
Mockito.when(message.getText()).thenReturn("/command@BotUsername ignore");
|
||||||
|
NullPointerException exception = Assertions.assertThrows(
|
||||||
|
NullPointerException.class, () -> registry.executeCommand(absSender, message), "Bot username is null");
|
||||||
|
Assertions.assertEquals("Bot username must not be null", exception.getMessage(), "Invalid exception message");
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user