From 53cdd4aa22013182d30db02dda469bb9a7fe4d03 Mon Sep 17 00:00:00 2001 From: Davide Polonio Date: Thu, 6 Apr 2023 17:57:55 +0200 Subject: [PATCH] test: complete first test for help message --- .../com/github/polpetta/mezzotre/App.java | 18 ++-- .../{InjectionModule.java => AppDI.java} | 4 +- .../mezzotre/orm/{di/Db.java => OrmDI.java} | 4 +- .../route/{di/Route.java => RouteDI.java} | 4 +- ...allbackQuery.java => CallbackQueryDI.java} | 7 +- .../telegram/callbackquery/Field.java | 37 ++++++++ .../telegram/callbackquery/Processor.java | 12 ++- .../callbackquery/SelectLanguageTutorial.java | 48 +--------- .../telegram/callbackquery/ShowHelp.java | 2 +- .../telegram/callbackquery/Value.java | 25 +++++ .../{di/Command.java => CommandDI.java} | 5 +- .../mezzotre/telegram/command/Help.java | 53 ++++++++++- .../mezzotre/telegram/command/Start.java | 19 ++-- src/main/resources/i18n/message.properties | 11 ++- .../resources/i18n/message_en_US.properties | 8 +- src/main/resources/i18n/message_it.properties | 2 +- .../resources/i18n/message_it_IT.properties | 2 +- .../helper/IntegrationAppFactory.java | 4 +- ...allbackQueryDIContextIntegrationTest.java} | 2 +- ...SelectLanguageTutorialIntegrationTest.java | 26 +++--- .../telegram/command/HelpIntegrationTest.java | 93 +++++++++++++++++-- 21 files changed, 276 insertions(+), 110 deletions(-) rename src/main/java/com/github/polpetta/mezzotre/{InjectionModule.java => AppDI.java} (86%) rename src/main/java/com/github/polpetta/mezzotre/orm/{di/Db.java => OrmDI.java} (92%) rename src/main/java/com/github/polpetta/mezzotre/route/{di/Route.java => RouteDI.java} (92%) rename src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/{di/CallbackQuery.java => CallbackQueryDI.java} (63%) create mode 100644 src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/Field.java create mode 100644 src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/Value.java rename src/main/java/com/github/polpetta/mezzotre/telegram/command/{di/Command.java => CommandDI.java} (91%) rename src/test/java/com/github/polpetta/mezzotre/orm/model/{CallbackQueryContextIntegrationTest.java => CallbackQueryDIContextIntegrationTest.java} (98%) diff --git a/src/main/java/com/github/polpetta/mezzotre/App.java b/src/main/java/com/github/polpetta/mezzotre/App.java index 9261d48..c19f1fe 100644 --- a/src/main/java/com/github/polpetta/mezzotre/App.java +++ b/src/main/java/com/github/polpetta/mezzotre/App.java @@ -1,10 +1,10 @@ package com.github.polpetta.mezzotre; -import com.github.polpetta.mezzotre.orm.di.Db; +import com.github.polpetta.mezzotre.orm.OrmDI; +import com.github.polpetta.mezzotre.route.RouteDI; import com.github.polpetta.mezzotre.route.Telegram; -import com.github.polpetta.mezzotre.route.di.Route; -import com.github.polpetta.mezzotre.telegram.callbackquery.di.CallbackQuery; -import com.github.polpetta.mezzotre.telegram.command.di.Command; +import com.github.polpetta.mezzotre.telegram.callbackquery.CallbackQueryDI; +import com.github.polpetta.mezzotre.telegram.command.CommandDI; import com.github.polpetta.mezzotre.util.di.ThreadPool; import com.google.inject.*; import com.google.inject.Module; @@ -23,11 +23,11 @@ public class App extends Jooby { public static final Function> DEFAULT_DI_MODULES = (jooby) -> { final HashSet modules = new HashSet<>(); - modules.add(new Db()); + modules.add(new OrmDI()); modules.add(new ThreadPool()); - modules.add(new Route()); - modules.add(new Command()); - modules.add(new CallbackQuery()); + modules.add(new RouteDI()); + modules.add(new CommandDI()); + modules.add(new CallbackQueryDI()); return modules; }; @@ -42,7 +42,7 @@ public class App extends Jooby { if (modules == null || modules.size() == 0) { toInject = DEFAULT_DI_MODULES.apply(this); } - toInject.add(new InjectionModule(this)); + toInject.add(new AppDI(this)); toInject.add(new JoobyModule(this)); final Injector injector = Guice.createInjector(runningEnv, toInject); diff --git a/src/main/java/com/github/polpetta/mezzotre/InjectionModule.java b/src/main/java/com/github/polpetta/mezzotre/AppDI.java similarity index 86% rename from src/main/java/com/github/polpetta/mezzotre/InjectionModule.java rename to src/main/java/com/github/polpetta/mezzotre/AppDI.java index 528e669..650f733 100644 --- a/src/main/java/com/github/polpetta/mezzotre/InjectionModule.java +++ b/src/main/java/com/github/polpetta/mezzotre/AppDI.java @@ -7,13 +7,13 @@ import javax.inject.Named; import javax.inject.Singleton; import org.slf4j.Logger; -public class InjectionModule extends AbstractModule { +public class AppDI extends AbstractModule { // In the future we can get this name from mvn, by now it is good as it is public static final String APPLICATION_NAME = "Mezzotre"; private final Jooby jooby; - public InjectionModule(Jooby jooby) { + public AppDI(Jooby jooby) { this.jooby = jooby; } diff --git a/src/main/java/com/github/polpetta/mezzotre/orm/di/Db.java b/src/main/java/com/github/polpetta/mezzotre/orm/OrmDI.java similarity index 92% rename from src/main/java/com/github/polpetta/mezzotre/orm/di/Db.java rename to src/main/java/com/github/polpetta/mezzotre/orm/OrmDI.java index 7119eac..48b1e87 100644 --- a/src/main/java/com/github/polpetta/mezzotre/orm/di/Db.java +++ b/src/main/java/com/github/polpetta/mezzotre/orm/OrmDI.java @@ -1,4 +1,4 @@ -package com.github.polpetta.mezzotre.orm.di; +package com.github.polpetta.mezzotre.orm; import com.google.inject.AbstractModule; import com.google.inject.Provides; @@ -10,7 +10,7 @@ import io.jooby.flyway.FlywayModule; import io.jooby.hikari.HikariModule; import javax.inject.Named; -public class Db extends AbstractModule { +public class OrmDI extends AbstractModule { /** * Returns null. This allows to fetch the configuration from file rather than fetch from other * environment diff --git a/src/main/java/com/github/polpetta/mezzotre/route/di/Route.java b/src/main/java/com/github/polpetta/mezzotre/route/RouteDI.java similarity index 92% rename from src/main/java/com/github/polpetta/mezzotre/route/di/Route.java rename to src/main/java/com/github/polpetta/mezzotre/route/RouteDI.java index dd57304..238be44 100644 --- a/src/main/java/com/github/polpetta/mezzotre/route/di/Route.java +++ b/src/main/java/com/github/polpetta/mezzotre/route/RouteDI.java @@ -1,4 +1,4 @@ -package com.github.polpetta.mezzotre.route.di; +package com.github.polpetta.mezzotre.route; import com.google.inject.AbstractModule; import com.google.inject.Provides; @@ -7,7 +7,7 @@ import io.jooby.Jooby; import java.util.Optional; import javax.inject.Singleton; -public class Route extends AbstractModule { +public class RouteDI extends AbstractModule { @Provides @Singleton public TelegramBot getTelegramBot(Jooby jooby) { diff --git a/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/di/CallbackQuery.java b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/CallbackQueryDI.java similarity index 63% rename from src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/di/CallbackQuery.java rename to src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/CallbackQueryDI.java index 6dfb00d..74c240c 100644 --- a/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/di/CallbackQuery.java +++ b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/CallbackQueryDI.java @@ -1,8 +1,5 @@ -package com.github.polpetta.mezzotre.telegram.callbackquery.di; +package com.github.polpetta.mezzotre.telegram.callbackquery; -import com.github.polpetta.mezzotre.telegram.callbackquery.Processor; -import com.github.polpetta.mezzotre.telegram.callbackquery.SelectLanguageTutorial; -import com.github.polpetta.mezzotre.telegram.callbackquery.ShowHelp; import com.google.inject.AbstractModule; import com.google.inject.Provides; import java.util.HashMap; @@ -10,7 +7,7 @@ import java.util.Map; import javax.inject.Named; import javax.inject.Singleton; -public class CallbackQuery extends AbstractModule { +public class CallbackQueryDI extends AbstractModule { @Provides @Singleton diff --git a/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/Field.java b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/Field.java new file mode 100644 index 0000000..e2a485d --- /dev/null +++ b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/Field.java @@ -0,0 +1,37 @@ +package com.github.polpetta.mezzotre.telegram.callbackquery; + +public interface Field { + /** + * Additional fields that are related to {@code changeLanguage} event + * + * @author Davide Polonio + * @since 1.0 + */ + enum SelectLanguageTutorial { + NewLanguage("newLanguage"); + + private final String name; + + SelectLanguageTutorial(String name) { + this.name = name; + } + + public String getName() { + return name; + } + } + + enum ShowHelp { + InvokedFromHelpMessage("invokedFromHelpMessage"); + + private final String name; + + ShowHelp(String name) { + this.name = name; + } + + public String getName() { + return name; + } + } +} diff --git a/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/Processor.java b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/Processor.java index 021a177..020d1d6 100644 --- a/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/Processor.java +++ b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/Processor.java @@ -17,12 +17,20 @@ import java.util.concurrent.CompletableFuture; public interface Processor { /** - * The even name this processor is able to process + * The event name this processor is able to process * - * @return a {@link String} containig the name of the event supported + * @return a {@link String} containing the name of the event supported */ String getEventName(); + default boolean canBeDirectlyInvokedByTheUser() { + return false; + } + + default Optional getPrettyPrintLocaleKeyName() { + return Optional.empty(); + } + /** * Process the current event * diff --git a/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/SelectLanguageTutorial.java b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/SelectLanguageTutorial.java index fc47ebd..7bb327d 100644 --- a/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/SelectLanguageTutorial.java +++ b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/SelectLanguageTutorial.java @@ -39,47 +39,6 @@ public class SelectLanguageTutorial implements Processor { private final Logger log; private final UUIDGenerator uuidGenerator; - /** - * Additional fields that are related to {@code changeLanguage} event - * - * @author Davide Polonio - * @since 1.0 - */ - public enum Field { - NewLanguage("newLanguage"); - - private final String name; - - Field(String name) { - this.name = name; - } - - public String getName() { - return name; - } - } - - /** - * Possible values for the additional {@link Field} of {@code changeLanguage} event - * - * @author Davide Polonio - * @since 1.0 - */ - public enum Language { - English("en-US"), - Italian("it-IT"); - - private final String locale; - - Language(String locale) { - this.locale = locale; - } - - public String getLocale() { - return locale; - } - } - @Inject public SelectLanguageTutorial( @Named("eventThreadPool") Executor threadPool, @@ -120,7 +79,8 @@ public class SelectLanguageTutorial implements Processor { .getFields() .getAdditionalProperties() .getOrDefault( - Field.NewLanguage.getName(), tgChat.getLocale())); + Field.SelectLanguageTutorial.NewLanguage.getName(), + tgChat.getLocale())); tgChat.save(); log.trace( "Locale for chat " @@ -166,7 +126,9 @@ public class SelectLanguageTutorial implements Processor { if (!tgChat.getHasHelpBeenShown()) { // Add a button to show all the possible commands final String showMeTutorialString = - templateContentGenerator.getString(tgChat.getLocale(), "button.showMeTutorial"); + templateContentGenerator.getString( + tgChat.getLocale(), + "selectLanguageTutorial.showMeTutorialInlineKeyboardButtonName"); final CallbackQueryMetadata callbackQueryMetadata = new CallbackQueryMetadata.CallbackQueryMetadataBuilder() diff --git a/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/ShowHelp.java b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/ShowHelp.java index 6beb58e..c7d241f 100644 --- a/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/ShowHelp.java +++ b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/ShowHelp.java @@ -7,7 +7,7 @@ import com.pengrad.telegrambot.request.SendMessage; import java.util.Optional; import java.util.concurrent.CompletableFuture; -public class ShowHelp implements Processor { +class ShowHelp implements Processor { public static String EVENT_NAME = "showHelp"; diff --git a/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/Value.java b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/Value.java new file mode 100644 index 0000000..046ff8b --- /dev/null +++ b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/Value.java @@ -0,0 +1,25 @@ +package com.github.polpetta.mezzotre.telegram.callbackquery; + +public interface Value { + /** + * Possible values for the additional {@link Field.SelectLanguageTutorial} of {@code + * changeLanguage} event + * + * @author Davide Polonio + * @since 1.0 + */ + enum SelectLanguageTutorial { + English("en-US"), + Italian("it-IT"); + + private final String locale; + + SelectLanguageTutorial(String locale) { + this.locale = locale; + } + + public String getLocale() { + return locale; + } + } +} diff --git a/src/main/java/com/github/polpetta/mezzotre/telegram/command/di/Command.java b/src/main/java/com/github/polpetta/mezzotre/telegram/command/CommandDI.java similarity index 91% rename from src/main/java/com/github/polpetta/mezzotre/telegram/command/di/Command.java rename to src/main/java/com/github/polpetta/mezzotre/telegram/command/CommandDI.java index 5ce51c8..767fdab 100644 --- a/src/main/java/com/github/polpetta/mezzotre/telegram/command/di/Command.java +++ b/src/main/java/com/github/polpetta/mezzotre/telegram/command/CommandDI.java @@ -1,6 +1,5 @@ -package com.github.polpetta.mezzotre.telegram.command.di; +package com.github.polpetta.mezzotre.telegram.command; -import com.github.polpetta.mezzotre.telegram.command.*; import com.google.inject.AbstractModule; import com.google.inject.Provides; import com.google.inject.assistedinject.FactoryModuleBuilder; @@ -12,7 +11,7 @@ import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.runtime.RuntimeConstants; import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; -public class Command extends AbstractModule { +public class CommandDI extends AbstractModule { @Override protected void configure() { diff --git a/src/main/java/com/github/polpetta/mezzotre/telegram/command/Help.java b/src/main/java/com/github/polpetta/mezzotre/telegram/command/Help.java index c3e0b14..a7a6526 100644 --- a/src/main/java/com/github/polpetta/mezzotre/telegram/command/Help.java +++ b/src/main/java/com/github/polpetta/mezzotre/telegram/command/Help.java @@ -1,10 +1,17 @@ package com.github.polpetta.mezzotre.telegram.command; import com.github.polpetta.mezzotre.i18n.TemplateContentGenerator; +import com.github.polpetta.mezzotre.orm.model.CallbackQueryContext; import com.github.polpetta.mezzotre.orm.model.TgChat; +import com.github.polpetta.mezzotre.telegram.callbackquery.Field; import com.github.polpetta.mezzotre.util.Clock; +import com.github.polpetta.mezzotre.util.UUIDGenerator; +import com.github.polpetta.types.json.CallbackQueryMetadata; import com.github.polpetta.types.json.ChatContext; import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.model.request.InlineKeyboardButton; +import com.pengrad.telegrambot.model.request.InlineKeyboardMarkup; +import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.request.BaseRequest; import com.pengrad.telegrambot.request.SendMessage; import java.util.Map; @@ -25,17 +32,25 @@ public class Help implements Processor { private final Executor threadPool; private final Clock clock; private final Map tgCommandProcessors; + private final Map + eventProcessor; + private final UUIDGenerator uuidGenerator; @Inject public Help( TemplateContentGenerator templateContentGenerator, @Named("eventThreadPool") Executor threadPool, Clock clock, - @Named("commandProcessor") Map tgCommandProcessors) { + @Named("commandProcessor") Map tgCommandProcessors, + @Named("eventProcessors") + Map eventProcessor, + UUIDGenerator uuidGenerator) { this.templateContentGenerator = templateContentGenerator; this.threadPool = threadPool; this.clock = clock; this.tgCommandProcessors = tgCommandProcessors; + this.eventProcessor = eventProcessor; + this.uuidGenerator = uuidGenerator; } @Override @@ -74,9 +89,41 @@ public class Help implements Processor { chat.setHasHelpBeenShown(true); chat.save(); - // FIXME put all the buttons here + final SendMessage sendMessage = + new SendMessage(chat.getId(), message).parseMode(ParseMode.Markdown); - return Optional.of(new SendMessage(chat.getId(), message)); + final String callBackGroupId = uuidGenerator.generateAsString(); + final InlineKeyboardButton[] collect = + eventProcessor.values().stream() + .filter( + com.github.polpetta.mezzotre.telegram.callbackquery.Processor + ::canBeDirectlyInvokedByTheUser) + .filter(e -> e.getPrettyPrintLocaleKeyName().isPresent()) + .map( + eventProcessor -> { + final CallbackQueryContext callbackQueryContext = + new CallbackQueryContext( + uuidGenerator.generateAsString(), + callBackGroupId, + new CallbackQueryMetadata.CallbackQueryMetadataBuilder() + .withEvent(eventProcessor.getEventName()) + .withTelegramChatId(chat.getId()) + .withAdditionalProperty( + Field.ShowHelp.InvokedFromHelpMessage.getName(), true) + .build()); + callbackQueryContext.save(); + + return new InlineKeyboardButton( + templateContentGenerator.getString( + chat.getLocale(), + eventProcessor.getPrettyPrintLocaleKeyName().get())) + .callbackData(callbackQueryContext.getId()); + }) + .toArray(InlineKeyboardButton[]::new); + + sendMessage.replyMarkup(new InlineKeyboardMarkup(collect)); + + return Optional.of(sendMessage); }, threadPool); } diff --git a/src/main/java/com/github/polpetta/mezzotre/telegram/command/Start.java b/src/main/java/com/github/polpetta/mezzotre/telegram/command/Start.java index 2d7aac8..e3a84f9 100644 --- a/src/main/java/com/github/polpetta/mezzotre/telegram/command/Start.java +++ b/src/main/java/com/github/polpetta/mezzotre/telegram/command/Start.java @@ -3,7 +3,8 @@ package com.github.polpetta.mezzotre.telegram.command; import com.github.polpetta.mezzotre.i18n.TemplateContentGenerator; import com.github.polpetta.mezzotre.orm.model.CallbackQueryContext; import com.github.polpetta.mezzotre.orm.model.TgChat; -import com.github.polpetta.mezzotre.telegram.callbackquery.SelectLanguageTutorial; +import com.github.polpetta.mezzotre.telegram.callbackquery.Field; +import com.github.polpetta.mezzotre.telegram.callbackquery.Value; import com.github.polpetta.mezzotre.util.Clock; import com.github.polpetta.mezzotre.util.UUIDGenerator; import com.github.polpetta.types.json.CallbackQueryMetadata; @@ -100,11 +101,13 @@ public class Start implements Processor { uuidGenerator.generateAsString(), groupId, new CallbackQueryMetadata.CallbackQueryMetadataBuilder() - .withEvent(SelectLanguageTutorial.EVENT_NAME) + .withEvent( + com.github.polpetta.mezzotre.telegram.callbackquery.SelectLanguageTutorial + .EVENT_NAME) .withTelegramChatId(update.message().chat().id()) .withAdditionalProperty( - SelectLanguageTutorial.Field.NewLanguage.getName(), - SelectLanguageTutorial.Language.English.getLocale()) + Field.SelectLanguageTutorial.NewLanguage.getName(), + Value.SelectLanguageTutorial.English.getLocale()) .build()); final CallbackQueryContext switchToItalian = @@ -112,11 +115,13 @@ public class Start implements Processor { uuidGenerator.generateAsString(), groupId, new CallbackQueryMetadata.CallbackQueryMetadataBuilder() - .withEvent(SelectLanguageTutorial.EVENT_NAME) + .withEvent( + com.github.polpetta.mezzotre.telegram.callbackquery.SelectLanguageTutorial + .EVENT_NAME) .withTelegramChatId(update.message().chat().id()) .withAdditionalProperty( - SelectLanguageTutorial.Field.NewLanguage.getName(), - SelectLanguageTutorial.Language.Italian.getLocale()) + Field.SelectLanguageTutorial.NewLanguage.getName(), + Value.SelectLanguageTutorial.Italian.getLocale()) .build()); final String englishButton = diff --git a/src/main/resources/i18n/message.properties b/src/main/resources/i18n/message.properties index 6200538..b0cf9e5 100644 --- a/src/main/resources/i18n/message.properties +++ b/src/main/resources/i18n/message.properties @@ -1,14 +1,17 @@ start.helloFirstName=Hello {0}! \ud83d\udc4b -start.description=This is {0}, a simple bot focused on DnD content management! Please start by choosing a language down below \ud83d\udc47 +start.description=This is {0}, a simple bot focused on DnD content management! Please start by choosing a selectLanguageTutorial down below \ud83d\udc47 start.cmdDescription=Trigger this very bot +start.inlineKeyboardButtonName=Let''s begin! +selectLanguageTutorial.inlineKeyboardButtonName=Select language selectLanguageTutorial.drinkAction=*Proceeds to drink a potion with a strange, multicolor liquid* -selectLanguageTutorial.setLanguage=Thanks! Now that I drank this modified potion of {0} that I''ve found at the "Crystal Fermentary" magic potion shop yesterday I can speak with you in the language that you prefer! -selectLanguageTutorial.instructions=You can always change your language settings by typing /selectLanguageTutorial in the chat. +selectLanguageTutorial.setLanguage=Thanks! Now that I drank this modified potion of {0} that I''ve found at the "Crystal Fermentary" magic potion shop yesterday I can speak with you in the selectLanguageTutorial that you prefer! +selectLanguageTutorial.instructions=You can always change your selectLanguageTutorial settings by typing /selectLanguageTutorial in the chat. changeLanguage.english=English changeLanguage.italian=Italian changeLanguage.cmdDescription=Select the new language I will use to speak to you +changeLanguage.inlineKeyboardButtonName=Change language spell.speakWithAnimals=Speak with animals -button.showMeTutorial=Show me what you can do! +selectLanguageTutorial.showMeTutorialInlineKeyboardButtonName=Show me what you can do! help.notShownYet=It seems you haven''t checked out what I can do yet! To have a complete list of my abilities, type /help in chat at any time! help.buttonBelow=Alternatively, you can click the button down below. help.description=Here is a list of what I can do diff --git a/src/main/resources/i18n/message_en_US.properties b/src/main/resources/i18n/message_en_US.properties index 64ca1f3..296200c 100644 --- a/src/main/resources/i18n/message_en_US.properties +++ b/src/main/resources/i18n/message_en_US.properties @@ -1,11 +1,11 @@ start.helloFirstName=Hello {0}! \ud83d\udc4b -start.description=This is {0}, a simple bot focused on DnD content management! Please start by choosing a language down below \ud83d\udc47 +start.description=This is {0}, a simple bot focused on DnD content management! Please start by choosing a selectLanguageTutorial down below \ud83d\udc47 selectLanguageTutorial.drinkAction=*Proceeds to drink a potion with a strange, multicolor liquid* -selectLanguageTutorial.setLanguage=Thanks! Now that I drank this modified potion of {0} that I''ve found at the "Crystal Fermentary" magic potion shop yesterday I can speak with you in the language that you prefer! -selectLanguageTutorial.instructions=You can always change your language settings by typing /selectLanguageTutorial in the chat. +selectLanguageTutorial.setLanguage=Thanks! Now that I drank this modified potion of {0} that I''ve found at the "Crystal Fermentary" magic potion shop yesterday I can speak with you in the selectLanguageTutorial that you prefer! +selectLanguageTutorial.instructions=You can always change your selectLanguageTutorial settings by typing /selectLanguageTutorial in the chat. selectLanguageTutorial.english=English selectLanguageTutorial.italian=Italian spell.speakWithAnimals=Speak with animals -button.showMeTutorial=Show me what you can do! +selectLanguageTutorial.showMeTutorialInlineKeyboardButtonName=Show me what you can do! help.notShownYet=It seems you haven''t checked out what I can do yet! To have a complete list of my abilities, type /help in chat at any time! help.buttonBelow=Alternatively, you can click the button down below. diff --git a/src/main/resources/i18n/message_it.properties b/src/main/resources/i18n/message_it.properties index 1c13674..dcdfadc 100644 --- a/src/main/resources/i18n/message_it.properties +++ b/src/main/resources/i18n/message_it.properties @@ -6,6 +6,6 @@ selectLanguageTutorial.instructions=Puoi sempre cambiare le preferenze della tua selectLanguageTutorial.english=Inglese selectLanguageTutorial.italian=Italiano spell.speakWithAnimals=Parlare con animali -button.showMeTutorial=Mostrami cosa puoi fare! +selectLanguageTutorial.showMeTutorialInlineKeyboardButtonName=Mostrami cosa puoi fare! help.notShownYet=Sembra tu non abbia ancora visto cosa posso fare! Per avere una lista completa delle mie abilità, scrivi /help nella chat in qualsiasi momento! help.buttonBelow=Alternativamente, puoi premere il bottone qui sotto. diff --git a/src/main/resources/i18n/message_it_IT.properties b/src/main/resources/i18n/message_it_IT.properties index 1c13674..dcdfadc 100644 --- a/src/main/resources/i18n/message_it_IT.properties +++ b/src/main/resources/i18n/message_it_IT.properties @@ -6,6 +6,6 @@ selectLanguageTutorial.instructions=Puoi sempre cambiare le preferenze della tua selectLanguageTutorial.english=Inglese selectLanguageTutorial.italian=Italiano spell.speakWithAnimals=Parlare con animali -button.showMeTutorial=Mostrami cosa puoi fare! +selectLanguageTutorial.showMeTutorialInlineKeyboardButtonName=Mostrami cosa puoi fare! help.notShownYet=Sembra tu non abbia ancora visto cosa posso fare! Per avere una lista completa delle mie abilità, scrivi /help nella chat in qualsiasi momento! help.buttonBelow=Alternativamente, puoi premere il bottone qui sotto. diff --git a/src/test/java/com/github/polpetta/mezzotre/helper/IntegrationAppFactory.java b/src/test/java/com/github/polpetta/mezzotre/helper/IntegrationAppFactory.java index dc1ebb6..147fa4d 100644 --- a/src/test/java/com/github/polpetta/mezzotre/helper/IntegrationAppFactory.java +++ b/src/test/java/com/github/polpetta/mezzotre/helper/IntegrationAppFactory.java @@ -1,7 +1,7 @@ package com.github.polpetta.mezzotre.helper; import com.github.polpetta.mezzotre.App; -import com.github.polpetta.mezzotre.route.di.Route; +import com.github.polpetta.mezzotre.route.RouteDI; import com.google.inject.AbstractModule; import com.google.inject.Provides; import com.google.inject.Singleton; @@ -56,7 +56,7 @@ public class IntegrationAppFactory { final PostgreSQLContainer container = new PostgreSQLContainer<>(TestConfig.POSTGRES_DOCKER_IMAGE); container.start(); - final Route routeModule = new Route(); + final RouteDI routeModule = new RouteDI(); final DatabaseDI databaseDI = new DatabaseDI(container); return new App(Stage.DEVELOPMENT, Set.of(databaseDI, routeModule)); } diff --git a/src/test/java/com/github/polpetta/mezzotre/orm/model/CallbackQueryContextIntegrationTest.java b/src/test/java/com/github/polpetta/mezzotre/orm/model/CallbackQueryDIContextIntegrationTest.java similarity index 98% rename from src/test/java/com/github/polpetta/mezzotre/orm/model/CallbackQueryContextIntegrationTest.java rename to src/test/java/com/github/polpetta/mezzotre/orm/model/CallbackQueryDIContextIntegrationTest.java index 2c45563..148d19e 100644 --- a/src/test/java/com/github/polpetta/mezzotre/orm/model/CallbackQueryContextIntegrationTest.java +++ b/src/test/java/com/github/polpetta/mezzotre/orm/model/CallbackQueryDIContextIntegrationTest.java @@ -23,7 +23,7 @@ import org.testcontainers.junit.jupiter.Testcontainers; @Tag("slow") @Tag("database") @Testcontainers -class CallbackQueryContextIntegrationTest { +class CallbackQueryDIContextIntegrationTest { private static ObjectMapper objectMapper; private static UUIDGenerator uuidGenerator; diff --git a/src/test/java/com/github/polpetta/mezzotre/telegram/callbackquery/SelectLanguageTutorialIntegrationTest.java b/src/test/java/com/github/polpetta/mezzotre/telegram/callbackquery/SelectLanguageTutorialIntegrationTest.java index e45e898..17758fe 100644 --- a/src/test/java/com/github/polpetta/mezzotre/telegram/callbackquery/SelectLanguageTutorialIntegrationTest.java +++ b/src/test/java/com/github/polpetta/mezzotre/telegram/callbackquery/SelectLanguageTutorialIntegrationTest.java @@ -77,7 +77,7 @@ class SelectLanguageTutorialIntegrationTest { private static Stream getTestLocales() { return Stream.of( Arguments.of( - SelectLanguageTutorial.Language.Italian, + Value.SelectLanguageTutorial.Italian, "_*Procede a bere una pozione al cui suo interno si trova uno strano liquido" + " multicolore*_\n" + "\n" @@ -95,7 +95,7 @@ class SelectLanguageTutorialIntegrationTest { "Mostrami cosa puoi fare!", false), Arguments.of( - SelectLanguageTutorial.Language.English, + Value.SelectLanguageTutorial.English, "_*Proceeds to drink a potion with a strange, multicolor liquid*_\n" + "\n" + "Thanks! Now that I drank this modified potion of Speak with animals that I've" @@ -112,7 +112,7 @@ class SelectLanguageTutorialIntegrationTest { "Show me what you can do!", false), Arguments.of( - SelectLanguageTutorial.Language.Italian, + Value.SelectLanguageTutorial.Italian, "_*Procede a bere una pozione al cui suo interno si trova uno strano liquido" + " multicolore*_\n" + "\n" @@ -126,7 +126,7 @@ class SelectLanguageTutorialIntegrationTest { "Mostrami cosa puoi fare!", true), Arguments.of( - SelectLanguageTutorial.Language.English, + Value.SelectLanguageTutorial.English, "_*Proceeds to drink a potion with a strange, multicolor liquid*_\n" + "\n" + "Thanks! Now that I drank this modified potion of Speak with animals that I've" @@ -144,7 +144,7 @@ class SelectLanguageTutorialIntegrationTest { @Timeout(value = 1, unit = TimeUnit.MINUTES) @MethodSource("getTestLocales") void shouldProcessChangeLanguageToDesiredOneSendMessage( - SelectLanguageTutorial.Language language, + Value.SelectLanguageTutorial selectLanguageTutorial, String expectedResult, String startingLocale, String buttonLocale, @@ -184,7 +184,8 @@ class SelectLanguageTutorialIntegrationTest { .withEvent("selectLanguageTutorial") .withTelegramChatId(tgChatId) .withAdditionalProperty( - SelectLanguageTutorial.Field.NewLanguage.getName(), language.getLocale()) + Field.SelectLanguageTutorial.NewLanguage.getName(), + selectLanguageTutorial.getLocale()) .build(); final String entryGroupId = "2e67774a-e4e4-4369-a414-a7f8bfe74b80"; final CallbackQueryContext changeLanguageCallbackQueryContext = @@ -193,7 +194,7 @@ class SelectLanguageTutorialIntegrationTest { changeLanguageCallbackQueryContext.save(); final CompletableFuture>> processFuture = - selectLanguageTutorial.process(changeLanguageCallbackQueryContext, update); + this.selectLanguageTutorial.process(changeLanguageCallbackQueryContext, update); final Optional> gotResponseOpt = processFuture.get(); final SendMessage gotMessage = (SendMessage) gotResponseOpt.get(); assertEquals(expectedResult, gotMessage.getParameters().get("text")); @@ -218,7 +219,7 @@ class SelectLanguageTutorialIntegrationTest { final TgChat retrievedTgChat = new QTgChat().id.eq(tgChatId).findOne(); assertNotNull(retrievedTgChat); - assertEquals(language.getLocale(), retrievedTgChat.getLocale()); + assertEquals(selectLanguageTutorial.getLocale(), retrievedTgChat.getLocale()); assertEquals(0, new QCallbackQueryContext().entryGroup.eq(entryGroupId).findCount()); } @@ -227,7 +228,7 @@ class SelectLanguageTutorialIntegrationTest { @Timeout(value = 1, unit = TimeUnit.MINUTES) @MethodSource("getTestLocales") void shouldProcessChangeLanguageToDesiredOneEditMessage( - SelectLanguageTutorial.Language language, + Value.SelectLanguageTutorial selectLanguageTutorial, String expectedResult, String startingLocale, String buttonLocale, @@ -299,7 +300,8 @@ class SelectLanguageTutorialIntegrationTest { .withEvent("selectLanguageTutorial") .withTelegramChatId(tgChatId) .withAdditionalProperty( - SelectLanguageTutorial.Field.NewLanguage.getName(), language.getLocale()) + Field.SelectLanguageTutorial.NewLanguage.getName(), + selectLanguageTutorial.getLocale()) .build(); final String entryGroupId = "2e67774a-e4e4-4369-a414-a7f8bfe74b80"; final CallbackQueryContext changeLanguageCallbackQueryContext = @@ -308,7 +310,7 @@ class SelectLanguageTutorialIntegrationTest { changeLanguageCallbackQueryContext.save(); final CompletableFuture>> processFuture = - selectLanguageTutorial.process(changeLanguageCallbackQueryContext, update); + this.selectLanguageTutorial.process(changeLanguageCallbackQueryContext, update); final Optional> gotResponseOpt = processFuture.get(); final EditMessageText gotMessage = (EditMessageText) gotResponseOpt.get(); assertEquals(expectedResult, gotMessage.getParameters().get("text")); @@ -333,7 +335,7 @@ class SelectLanguageTutorialIntegrationTest { final TgChat retrievedTgChat = new QTgChat().id.eq(tgChatId).findOne(); assertNotNull(retrievedTgChat); - assertEquals(language.getLocale(), retrievedTgChat.getLocale()); + assertEquals(selectLanguageTutorial.getLocale(), retrievedTgChat.getLocale()); assertEquals(0, new QCallbackQueryContext().entryGroup.eq(entryGroupId).findCount()); } diff --git a/src/test/java/com/github/polpetta/mezzotre/telegram/command/HelpIntegrationTest.java b/src/test/java/com/github/polpetta/mezzotre/telegram/command/HelpIntegrationTest.java index 6072f43..29fbc77 100644 --- a/src/test/java/com/github/polpetta/mezzotre/telegram/command/HelpIntegrationTest.java +++ b/src/test/java/com/github/polpetta/mezzotre/telegram/command/HelpIntegrationTest.java @@ -8,20 +8,23 @@ import com.github.polpetta.mezzotre.helper.Loader; import com.github.polpetta.mezzotre.helper.TestConfig; import com.github.polpetta.mezzotre.i18n.LocalizedMessageFactory; import com.github.polpetta.mezzotre.i18n.TemplateContentGenerator; +import com.github.polpetta.mezzotre.orm.model.CallbackQueryContext; import com.github.polpetta.mezzotre.orm.model.TgChat; import com.github.polpetta.mezzotre.orm.model.query.QTgChat; import com.github.polpetta.mezzotre.util.Clock; +import com.github.polpetta.mezzotre.util.UUIDGenerator; import com.github.polpetta.types.json.ChatContext; import com.google.gson.Gson; import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.model.request.InlineKeyboardButton; +import com.pengrad.telegrambot.model.request.InlineKeyboardMarkup; import com.pengrad.telegrambot.request.BaseRequest; import com.pengrad.telegrambot.request.SendMessage; import io.ebean.Database; -import java.util.HashMap; -import java.util.Optional; -import java.util.Set; +import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executors; +import java.util.stream.Stream; import org.apache.velocity.app.VelocityEngine; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; @@ -112,12 +115,68 @@ class HelpIntegrationTest { commands.put("/b", dummy1); commands.put("/different", dummy2); + final Map events = + new HashMap<>(); + + final com.github.polpetta.mezzotre.telegram.callbackquery.Processor dummyEvent1 = + new com.github.polpetta.mezzotre.telegram.callbackquery.Processor() { + @Override + public String getEventName() { + return "exampleEvent"; + } + + @Override + public boolean canBeDirectlyInvokedByTheUser() { + return true; + } + + @Override + public Optional getPrettyPrintLocaleKeyName() { + return Optional.of("changeLanguage.inlineKeyboardButtonName"); + } + + @Override + public CompletableFuture>> process( + CallbackQueryContext callbackQueryContext, Update update) { + return null; + } + }; + + final com.github.polpetta.mezzotre.telegram.callbackquery.Processor dummyEvent2 = + new com.github.polpetta.mezzotre.telegram.callbackquery.Processor() { + @Override + public String getEventName() { + return "secondExampleEvent"; + } + + @Override + public boolean canBeDirectlyInvokedByTheUser() { + return true; + } + + @Override + public Optional getPrettyPrintLocaleKeyName() { + return Optional.of("selectLanguageTutorial.inlineKeyboardButtonName"); + } + + @Override + public CompletableFuture>> process( + CallbackQueryContext callbackQueryContext, Update update) { + return null; + } + }; + + events.put(dummyEvent1.getEventName(), dummyEvent1); + events.put(dummyEvent2.getEventName(), dummyEvent2); + help = new Help( new TemplateContentGenerator(new LocalizedMessageFactory(velocityEngine)), Executors.newSingleThreadExecutor(), fakeClock, - commands); + commands, + events, + new UUIDGenerator()); final Update update = gson.fromJson( @@ -159,8 +218,30 @@ class HelpIntegrationTest { + " selecting the corresponding button below \uD83D\uDC47", message); - // TODO InputKeyboard assertions - fail("Add inputkeyboard assertions too"); + final InlineKeyboardButton[][] keyboard = + ((InlineKeyboardMarkup) + gotResponse + .getParameters() + .getOrDefault("reply_markup", new InlineKeyboardMarkup())) + .inlineKeyboard(); + + final List keyboardButtons = + Stream.of(keyboard).flatMap(Stream::of).toList(); + + assertEquals(2, keyboardButtons.size()); + + assertFalse(keyboardButtons.get(0).callbackData().isBlank()); + assertFalse(keyboardButtons.get(1).callbackData().isBlank()); + + assertTrue( + keyboardButtons.stream() + .map(InlineKeyboardButton::text) + .anyMatch("Select language"::equals)); + + assertTrue( + keyboardButtons.stream() + .map(InlineKeyboardButton::text) + .anyMatch("Change language"::equals)); final TgChat gotChat = new QTgChat().id.eq(1111111L).findOne(); assertNotNull(gotChat);