diff --git a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramLongPollingCommandBot.java b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramLongPollingCommandBot.java index 05de2c72..24aadaf0 100644 --- a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramLongPollingCommandBot.java +++ b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramLongPollingCommandBot.java @@ -53,7 +53,7 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB */ public TelegramLongPollingCommandBot(DefaultBotOptions options, boolean allowCommandsWithUsername) { super(options); - this.commandRegistry = new CommandRegistry(allowCommandsWithUsername, this.getBotUsername()); + this.commandRegistry = new CommandRegistry(allowCommandsWithUsername, this::getBotUsername); } @Override diff --git a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/CommandRegistry.java b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/CommandRegistry.java index f06e15fb..33fdfe17 100644 --- a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/CommandRegistry.java +++ b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/CommandRegistry.java @@ -7,7 +7,9 @@ import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Map; +import java.util.Objects; import java.util.function.BiConsumer; +import java.util.function.Supplier; import java.util.regex.Pattern; /** @@ -19,17 +21,32 @@ public final class CommandRegistry implements ICommandRegistry { private final Map commandRegistryMap = new HashMap<>(); private final boolean allowCommandsWithUsername; - private final String botUsername; + private final Supplier botUsernameSupplier; private BiConsumer defaultConsumer; /** * Creates a Command registry + * * @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) { + Objects.requireNonNull(botUsername, "Bot username must not be null"); 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 botUsernameSupplier) { + this.allowCommandsWithUsername = allowCommandsWithUsername; + this.botUsernameSupplier = botUsernameSupplier; } @Override @@ -120,9 +137,12 @@ public final class CommandRegistry implements ICommandRegistry { * the command * @param command Command to simplify * @return Simplified command + * @throws java.lang.NullPointerException if {@code allowCommandsWithUsername} is {@code true} + * and {@code botUsernameSupplier} returns {@code null} */ private String removeUsernameFromCommandIfNeeded(String command) { if (allowCommandsWithUsername) { + String botUsername = Objects.requireNonNull(botUsernameSupplier.get(), "Bot username must not be null"); return command.replaceAll("(?i)@" + Pattern.quote(botUsername), "").trim(); } return command; diff --git a/telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/CommandRegistryTest.java b/telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/CommandRegistryTest.java new file mode 100644 index 00000000..2c22473c --- /dev/null +++ b/telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/CommandRegistryTest.java @@ -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"); + } +} \ No newline at end of file