From 5ea6fc9f09722af55336db94b800738fa8cc8662 Mon Sep 17 00:00:00 2001 From: Grig Alex Date: Wed, 21 Oct 2020 22:16:38 +0300 Subject: [PATCH] Add constructor with bot username --- .../bots/commandbot/commands/CommandRegistry.java | 12 ++++++++++++ .../commandbot/commands/CommandRegistryTest.java | 9 +++++++++ 2 files changed, 21 insertions(+) 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 8277503d..b7e9d2a2 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 @@ -24,6 +24,18 @@ public final class CommandRegistry implements ICommandRegistry { 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 + */ + public CommandRegistry(boolean allowCommandsWithUsername, String botUsername) { + Objects.requireNonNull(botUsername, "Bot username must not be null"); + this.allowCommandsWithUsername = allowCommandsWithUsername; + this.botUsernameSupplier = () -> botUsername; + } + /** * Creates a Command registry * @param allowCommandsWithUsername True to allow commands with username, false otherwise 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 index 232ea85b..2c22473c 100644 --- 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 @@ -8,6 +8,15 @@ 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");