From 47b990f90dad5c76a5b830b0fceeac50b8aba54a Mon Sep 17 00:00:00 2001 From: JBolivarLi <147824016+JBolivarLi@users.noreply.github.com> Date: Wed, 20 Dec 2023 14:31:41 +0300 Subject: [PATCH] Step 6 (#21) * JRTB-1: added repository layer. * STEP_6 JRTB-5: added ability to subscribe on group JRTB-6: added ability to see the list of the group subscription. * STEP_6 --- .github/workflows/maven.yml | 35 ++--- RELEASE_NOTES.md | 8 +- pom.xml | 16 +++ .../bot/bot/JavarushTelegramBot.java | 8 +- .../bot/command/AddGroupSubCommand.java | 93 ++++++++++++++ .../bot/command/CommandContainer.java | 13 +- .../bot/command/CommandName.java | 6 +- .../bot/command/CommandUtils.java | 30 +++++ .../bot/command/HelpCommand.java | 18 ++- .../bot/command/ListGroupSubCommand.java | 39 ++++++ .../javarushclient/JavaRushGroupClient.java | 44 +++++++ .../JavaRushGroupClientImpl.java | 71 +++++++++++ .../dto/GroupDiscussionInfo.java | 17 +++ .../bot/javarushclient/dto/GroupFilter.java | 9 ++ .../bot/javarushclient/dto/GroupInfo.java | 25 ++++ .../bot/javarushclient/dto/GroupInfoType.java | 8 ++ .../javarushclient/dto/GroupRequestArgs.java | 50 ++++++++ .../dto/GroupVisibilityStatus.java | 7 + .../dto/GroupsCountRequestArgs.java | 34 +++++ .../bot/javarushclient/dto/MeGroupInfo.java | 12 ++ .../javarushclient/dto/MeGroupInfoStatus.java | 7 + .../dto/UserDiscussionInfo.java | 13 ++ .../bot/repository/GroupSubRepository.java | 12 ++ .../bot/repository/entity/GroupSub.java | 42 ++++++ .../bot/repository/entity/TelegramUser.java | 7 + .../bot/service/GroupSubService.java | 16 +++ .../bot/service/GroupSubServiceImpl.java | 46 +++++++ .../bot/service/SendBotMessageService.java | 16 ++- .../resources/application-test.properties | 4 +- src/main/resources/application.properties | 2 + .../V00002__created_groupsub_many_to_many.sql | 21 +++ src/test/command/ListGroupSubCommandTest.java | 68 ++++++++++ src/test/java/CommandContainerTest.java | 6 +- .../JavaRushGroupClientTest.java | 120 ++++++++++++++++++ src/test/repository/GroupSubRepositoryIT.java | 46 +++++++ src/test/resources/fiveGroupSubsForUser.sql | 15 +++ src/test/resources/fiveUsersForGroupSub.sql | 15 +++ src/test/service/GroupSubServiceTest.java | 92 ++++++++++++++ 38 files changed, 1060 insertions(+), 31 deletions(-) create mode 100644 src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/AddGroupSubCommand.java create mode 100644 src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/CommandUtils.java create mode 100644 src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/ListGroupSubCommand.java create mode 100644 src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/JavaRushGroupClient.java create mode 100644 src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/JavaRushGroupClientImpl.java create mode 100644 src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupDiscussionInfo.java create mode 100644 src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupFilter.java create mode 100644 src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupInfo.java create mode 100644 src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupInfoType.java create mode 100644 src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupRequestArgs.java create mode 100644 src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupVisibilityStatus.java create mode 100644 src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupsCountRequestArgs.java create mode 100644 src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/MeGroupInfo.java create mode 100644 src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/MeGroupInfoStatus.java create mode 100644 src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/UserDiscussionInfo.java create mode 100644 src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/repository/GroupSubRepository.java create mode 100644 src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/repository/entity/GroupSub.java create mode 100644 src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/service/GroupSubService.java create mode 100644 src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/service/GroupSubServiceImpl.java create mode 100644 src/main/resources/db/migration/V00002__created_groupsub_many_to_many.sql create mode 100644 src/test/command/ListGroupSubCommandTest.java create mode 100644 src/test/javarushclient/JavaRushGroupClientTest.java create mode 100644 src/test/repository/GroupSubRepositoryIT.java create mode 100644 src/test/resources/fiveGroupSubsForUser.sql create mode 100644 src/test/resources/fiveUsersForGroupSub.sql create mode 100644 src/test/service/GroupSubServiceTest.java diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 9df7778..4e86678 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -19,23 +19,24 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - name: Set up MySQL - uses: mirromutth/mysql-action@v1.1 - with: - mysql version: '8.0.15' - mysql database: 'dev_jrtb_db' - mysql root password: 'root' - mysql user: 'dev_jrtb_db_user' - mysql password: 'dev_jrtb_db_password' - - name: Set up JDK 17 - uses: actions/setup-java@v3 - with: - java-version: '17' - distribution: 'temurin' - cache: maven - - name: Build with Maven - run: mvn -B package --file pom.xml + - uses: actions/checkout@v3 + - name: Set up MySQL + uses: mirromutth/mysql-action@v1.1 + with: + mysql version: '8.0.15' + mysql database: 'dev_jrtb_db' + mysql root password: 'root' + mysql user: 'dev_jrtb_db_user' + mysql password: 'dev_jrtb_db_password' + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + cache: maven + - name: Build with Maven + run: mvn -B package --file pom.xml + diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 15455d6..b8ce77b 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -8,4 +8,10 @@ JRTB-0: added SpringBoot skeleton project * JRTB-13: added deployment process to the project * ## 0.4.0-SNAPSHOT -* JRTB-1: added repository layer. \ No newline at end of file + +* JRTB-1: added repository layer. +## 0.5.0-SNAPSHOT + +* JRTB-5: added ability to subscribe on group +* JRTB-6: added ability to get a list of group subscriptions. + diff --git a/pom.xml b/pom.xml index 7eaf73f..1b85bd0 100644 --- a/pom.xml +++ b/pom.xml @@ -15,6 +15,8 @@ Telegram bot for Javarush from community to community 11 + 3.11.01 + 3.11 @@ -38,6 +40,13 @@ provided + + org.apache.commons + commons-lang3 + ${apache.commons.version} + + + org.flywaydb flyway-core @@ -51,6 +60,13 @@ telegrambots-spring-boot-starter 6.8.0 + + + com.konghq + unirest-java + ${unirest.version} + + org.springframework.boot spring-boot-starter-test diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/bot/JavarushTelegramBot.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/bot/JavarushTelegramBot.java index ae22a7b..ed8859e 100644 --- a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/bot/JavarushTelegramBot.java +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/bot/JavarushTelegramBot.java @@ -1,5 +1,7 @@ package com.github.JBolivarLi.javarushtelegrambot.bot.bot; import com.github.JBolivarLi.javarushtelegrambot.bot.command.CommandContainer; +import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.JavaRushGroupClient; +import com.github.JBolivarLi.javarushtelegrambot.bot.service.GroupSubService; import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageServiceImpl; import com.github.JBolivarLi.javarushtelegrambot.bot.service.TelegramUserService; import org.springframework.beans.factory.annotation.Autowired; @@ -32,8 +34,10 @@ public String getBotToken() { @Autowired - public JavarushTelegramBot(TelegramUserService telegramUserService) { - this.commandContainer = new CommandContainer(new SendBotMessageServiceImpl(this), telegramUserService); + + public JavarushTelegramBot(TelegramUserService telegramUserService, JavaRushGroupClient groupClient, GroupSubService groupSubService) { + this.commandContainer = new CommandContainer(new SendBotMessageServiceImpl(this), telegramUserService,groupClient,groupSubService); + } public void onUpdateReceived(Update update) { if(update.hasMessage() && update.getMessage().hasText()) { diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/AddGroupSubCommand.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/AddGroupSubCommand.java new file mode 100644 index 0000000..6945a75 --- /dev/null +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/AddGroupSubCommand.java @@ -0,0 +1,93 @@ +package com.github.JBolivarLi.javarushtelegrambot.bot.command; + +import com.github.JBolivarLi.javarushtelegrambot.bot.command.Command; +import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.JavaRushGroupClient; +import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.GroupDiscussionInfo; +import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.GroupRequestArgs; +import com.github.JBolivarLi.javarushtelegrambot.bot.repository.entity.GroupSub; +import com.github.JBolivarLi.javarushtelegrambot.bot.service.GroupSubService; +import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageService; +import org.telegram.telegrambots.meta.api.objects.Update; + +import java.util.stream.Collectors; + +import static com.github.JBolivarLi.javarushtelegrambot.bot.command.CommandName.ADD_GROUP_SUB; +import static com.github.JBolivarLi.javarushtelegrambot.bot.command.CommandUtils.getChatId; +import static com.github.JBolivarLi.javarushtelegrambot.bot.command.CommandUtils.getMessage; +import static java.util.Objects.isNull; +import static org.apache.commons.lang3.StringUtils.SPACE; + + +/** + * Add Group subscription {@link Command}. + */ +//todo add unit test for the command logic. +public class AddGroupSubCommand implements Command { + + private final SendBotMessageService sendBotMessageService; + private final JavaRushGroupClient javaRushGroupClient; + private final GroupSubService groupSubService; + + public AddGroupSubCommand(SendBotMessageService sendBotMessageService, JavaRushGroupClient javaRushGroupClient, + GroupSubService groupSubService) { + this.sendBotMessageService = sendBotMessageService; + this.javaRushGroupClient = javaRushGroupClient; + this.groupSubService = groupSubService; + } + private boolean isNumeric(String str) { + if (str == null || str.isEmpty()) { + return false; + } + for (char c : str.toCharArray()) { + if (!Character.isDigit(c)) { + return false; + } + } + return true; + } + + + @Override + public void execute(Update update) { + if (getMessage(update).equalsIgnoreCase(ADD_GROUP_SUB.getCommandName())) { + sendGroupIdList(getChatId(update)); + return; + } + String groupId = getMessage(update).split(SPACE)[1]; + String chatId = getChatId(update); + if (isNumeric(groupId)) { + GroupDiscussionInfo groupById = javaRushGroupClient.getGroupById(Integer.parseInt(groupId)); + if (isNull(groupById.getId())) { + sendGroupNotFound(chatId, groupId); + } + GroupSub savedGroupSub = groupSubService.save(chatId, groupById); + sendBotMessageService.sendMessage(chatId, "Подписал на группу " + savedGroupSub.getTitle()); + } else { + sendNotValidGroupID(chatId, groupId); + } + } + + private void sendGroupNotFound(String chatId, String groupId) { + String groupNotFoundMessage = "Нет группы с ID = \"%s\""; + sendBotMessageService.sendMessage(chatId, String.format(groupNotFoundMessage, groupId)); + } + + private void sendNotValidGroupID(String chatId, String groupId) { + String groupNotFoundMessage = "Неправильный ID группы = \"%s\""; + sendBotMessageService.sendMessage(chatId, String.format(groupNotFoundMessage, groupId)); + ; } + + private void sendGroupIdList(String chatId) { + String groupIds = javaRushGroupClient.getGroupList(GroupRequestArgs.builder().build()).stream() + .map(group -> String.format("%s - %s \n", group.getTitle(), group.getId())) + .collect(Collectors.joining()); + + String message = "Чтобы подписаться на группу - передай комадну вместе с ID группы. \n" + + "Например: /addGroupSub 16 \n\n" + + "я подготовил список всех групп - выбирай какую хочешь :) \n\n" + + "имя группы - ID группы \n\n" + + "%s"; + + sendBotMessageService.sendMessage(chatId, String.format(message, groupIds)); + } +} \ No newline at end of file diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/CommandContainer.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/CommandContainer.java index caeb98f..73f2105 100644 --- a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/CommandContainer.java +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/CommandContainer.java @@ -1,5 +1,7 @@ package com.github.JBolivarLi.javarushtelegrambot.bot.command; +import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.JavaRushGroupClient; +import com.github.JBolivarLi.javarushtelegrambot.bot.service.GroupSubService; import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageService; import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageServiceImpl; import com.github.JBolivarLi.javarushtelegrambot.bot.service.TelegramUserService; @@ -13,12 +15,17 @@ public class CommandContainer { private final Map commandMap; private final Command unknownCommand; - public CommandContainer(SendBotMessageService sendBotMessageService, TelegramUserService telegramUserService) { - commandMap = Map.ofEntries(Map.entry(START.getCommandName(), new StartCommand(sendBotMessageService,telegramUserService)), + + public CommandContainer(SendBotMessageService sendBotMessageService, TelegramUserService telegramUserService, JavaRushGroupClient javaRushGroupClient, GroupSubService groupSubService) { + commandMap = + Map.ofEntries(Map.entry(START.getCommandName(), new StartCommand(sendBotMessageService,telegramUserService)), Map.entry(STOP.getCommandName(), new StopCommand(sendBotMessageService,telegramUserService)), Map.entry(HELP.getCommandName(), new HelpCommand(sendBotMessageService)), Map.entry(NO.getCommandName(), new NoCommand(sendBotMessageService)), - Map.entry(STAT.getCommandName(),new StatCommand(sendBotMessageService,telegramUserService))); + Map.entry(STAT.getCommandName(),new StatCommand(sendBotMessageService,telegramUserService)), + Map.entry(ADD_GROUP_SUB.getCommandName(),new AddGroupSubCommand(sendBotMessageService, javaRushGroupClient, groupSubService)), + Map.entry(LIST_GROUP_SUB.getCommandName(),new ListGroupSubCommand(sendBotMessageService,telegramUserService))); + unknownCommand = new UnknownCommand(sendBotMessageService); } diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/CommandName.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/CommandName.java index dcdcb9c..43623fc 100644 --- a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/CommandName.java +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/CommandName.java @@ -6,7 +6,11 @@ public enum CommandName { STOP("/stop"), HELP("/help"), NO("nocommand"), - STAT("/stat"); + + STAT("/stat"), + ADD_GROUP_SUB("/addgroupsub"), + LIST_GROUP_SUB("/listgroupsub"); + private final String commandName; diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/CommandUtils.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/CommandUtils.java new file mode 100644 index 0000000..30e449a --- /dev/null +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/CommandUtils.java @@ -0,0 +1,30 @@ +package com.github.JBolivarLi.javarushtelegrambot.bot.command; + + +import org.telegram.telegrambots.meta.api.objects.Update; + +/** + * Utils class for Commands. + */ +public class CommandUtils { + + /** + * Retrieve chatId from {@link Update} object. + * + * @param update provided {@link Update} + * @return chatID from the provided {@link Update} object. + */ + public static String getChatId(Update update) { + return update.getMessage().getChatId().toString(); + } + + /** + * Retrieve text of the message from {@link Update} object. + * + * @param update provided {@link Update} + * @return the text of the message from the provided {@link Update} object. + */ + public static String getMessage(Update update) { + return update.getMessage().getText(); + } +} \ No newline at end of file diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/HelpCommand.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/HelpCommand.java index 1e9b90c..4856378 100644 --- a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/HelpCommand.java +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/HelpCommand.java @@ -10,13 +10,23 @@ public class HelpCommand implements Command { private final SendBotMessageService sendBotMessageService; - public static final String HELP_MESSAGE = String.format("✨Дотупные команды✨\n\n" - + "Начать\\закончить работу с ботом\n" + + public static final String HELP_MESSAGE = String.format("✨Дотупные команды✨\n\n" + + + "Начать\\закончить работу с ботом:\n" + "%s - начать работу со мной\n" + "%s - приостановить работу со мной\n\n" + + + "Работа с подписками на группы:\n" + + "%s - подписаться на группу статей\n" + + "%s - получить список групп, на которые подписан\n\n" + + "%s - получить помощь в работе со мной\n" - + "%s - получить статистику \n", - START.getCommandName(), STOP.getCommandName(), HELP.getCommandName(), STAT.getCommandName()); + + "%s - получить мою статистику использования\n", + START.getCommandName(), STOP.getCommandName(), ADD_GROUP_SUB.getCommandName(), + LIST_GROUP_SUB.getCommandName(), HELP.getCommandName(), STAT.getCommandName()); + + public HelpCommand(SendBotMessageService sendBotMessageService) { this.sendBotMessageService = sendBotMessageService; diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/ListGroupSubCommand.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/ListGroupSubCommand.java new file mode 100644 index 0000000..e1bc145 --- /dev/null +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/ListGroupSubCommand.java @@ -0,0 +1,39 @@ +package com.github.JBolivarLi.javarushtelegrambot.bot.command; + +import com.github.JBolivarLi.javarushtelegrambot.bot.repository.entity.TelegramUser; +import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageService; +import com.github.JBolivarLi.javarushtelegrambot.bot.service.TelegramUserService; +import org.telegram.telegrambots.meta.api.objects.Update; +import com.github.JBolivarLi.javarushtelegrambot.bot.repository.entity.GroupSub; +import java.util.stream.Collectors; +import javax.ws.rs.NotFoundException; + +import static com.github.JBolivarLi.javarushtelegrambot.bot.command.CommandUtils.getChatId; + +/** + * {@link Command} for getting list of {@link GroupSub}. + */ +public class ListGroupSubCommand implements Command { + + private final SendBotMessageService sendBotMessageService; + private final TelegramUserService telegramUserService; + + public ListGroupSubCommand(SendBotMessageService sendBotMessageService, TelegramUserService telegramUserService) { + this.sendBotMessageService = sendBotMessageService; + this.telegramUserService = telegramUserService; + } + + @Override + public void execute(Update update) { + //todo add exception handling + TelegramUser telegramUser = telegramUserService.findByChatId(getChatId(update)) + .orElseThrow(NotFoundException::new); + + String message = "Я нашел все подписки на группы: \n\n"; + String collectedGroups = telegramUser.getGroupSubs().stream() + .map(it -> "Группа: " + it.getTitle() + " , ID = " + it.getId() + " \n") + .collect(Collectors.joining()); + + sendBotMessageService.sendMessage(telegramUser.getChatId(), message + collectedGroups); + } +} \ No newline at end of file diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/JavaRushGroupClient.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/JavaRushGroupClient.java new file mode 100644 index 0000000..c624f60 --- /dev/null +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/JavaRushGroupClient.java @@ -0,0 +1,44 @@ +package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient; +import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.*; + + +import java.util.List; + +/** + * Client for Javarush Open API corresponds to Groups. + */ +public interface JavaRushGroupClient { + + /** + * Get all the {@link GroupInfo} filtered by provided {@link GroupRequestArgs}. + * + * @param requestArgs provided {@link GroupRequestArgs}. + * @return the collection of the {@link GroupInfo} objects. + */ + List getGroupList(GroupRequestArgs requestArgs); + + /** + * Get all the {@link GroupDiscussionInfo} filtered by provided {@link GroupRequestArgs}. + * + * @param requestArgs provided {@link GroupRequestArgs} + * @return the collection of the {@link GroupDiscussionInfo} objects. + */ + List getGroupDiscussionList(GroupRequestArgs requestArgs); + + /** + * Get count of groups filtered by provided {@link GroupRequestArgs}. + * + * @param countRequestArgs provided {@link GroupsCountRequestArgs}. + * @return the count of the groups. + */ + Integer getGroupCount(GroupsCountRequestArgs countRequestArgs); + + /** + * Get {@link GroupDiscussionInfo} by provided ID. + * + * @param id provided ID. + * @return {@link GroupDiscussionInfo} object. + */ + GroupDiscussionInfo getGroupById(Integer id); +} + diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/JavaRushGroupClientImpl.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/JavaRushGroupClientImpl.java new file mode 100644 index 0000000..4917d46 --- /dev/null +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/JavaRushGroupClientImpl.java @@ -0,0 +1,71 @@ +package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient; + + +import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.GroupDiscussionInfo; +import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.GroupInfo; +import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.GroupRequestArgs; +import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.GroupsCountRequestArgs; +import kong.unirest.GenericType; +import kong.unirest.HttpResponse; +import kong.unirest.Unirest; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * Implementation of the {@link JavaRushGroupClient} interface. + */ + + + +/** + * Implementation of the {@link JavaRushGroupClient} interface. + */ +@Component +public class JavaRushGroupClientImpl implements JavaRushGroupClient { + + private final String javarushApiGroupPath; + + public JavaRushGroupClientImpl(@Value("${javarush.api.path}") String javarushApi) { + this.javarushApiGroupPath = javarushApi + "/groups"; + } + + @Override + public List getGroupList(GroupRequestArgs requestArgs) { + return Unirest.get(javarushApiGroupPath) + .queryString(requestArgs.populateQueries()) + .asObject(new GenericType>() { + }) + .getBody(); + } + + + @Override + public List getGroupDiscussionList(GroupRequestArgs requestArgs) { + return Unirest.get(javarushApiGroupPath) + .queryString(requestArgs.populateQueries()) + .asObject(new GenericType>() { + }) + .getBody(); + } + + @Override + public Integer getGroupCount(GroupsCountRequestArgs countRequestArgs) { + return Integer.valueOf( + Unirest.get(String.format("%s/count", javarushApiGroupPath)) + .queryString(countRequestArgs.populateQueries()) + .asString() + .getBody() + ); + } + + @Override + public GroupDiscussionInfo getGroupById(Integer id) { + return Unirest.get(String.format("%s/group%s", javarushApiGroupPath, id.toString())) + .asObject(GroupDiscussionInfo.class) + .getBody(); + } + + +} \ No newline at end of file diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupDiscussionInfo.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupDiscussionInfo.java new file mode 100644 index 0000000..fc26610 --- /dev/null +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupDiscussionInfo.java @@ -0,0 +1,17 @@ +package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; + +/** + * Group discussion info class. + */ +@EqualsAndHashCode(callSuper = true) +@Data +@ToString(callSuper = true) +public class GroupDiscussionInfo extends GroupInfo { + + private UserDiscussionInfo userDiscussionInfo; + private Integer commentsCount; +} \ No newline at end of file diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupFilter.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupFilter.java new file mode 100644 index 0000000..5c96a02 --- /dev/null +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupFilter.java @@ -0,0 +1,9 @@ +package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto; + +/** + * Filters for group requests. + */ +public enum GroupFilter { + + UNKNOWN, MY, ALL +} \ No newline at end of file diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupInfo.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupInfo.java new file mode 100644 index 0000000..b2c324a --- /dev/null +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupInfo.java @@ -0,0 +1,25 @@ +package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto; + +import lombok.Data; +import lombok.ToString; + +/** + * Group Info DTO class. + */ +@Data +@ToString +public class GroupInfo { + + private Integer id; + private String avatarUrl; + private String createTime; + private String description; + private String key; + private Integer levelToEditor; + private MeGroupInfo meGroupInfo; + private String pictureUrl; + private String title; + private GroupInfoType type; + private Integer userCount; + private GroupVisibilityStatus visibilityStatus; +} diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupInfoType.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupInfoType.java new file mode 100644 index 0000000..305942e --- /dev/null +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupInfoType.java @@ -0,0 +1,8 @@ +package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto; + +/** + * Group Info type; + */ +public enum GroupInfoType { + UNKNOWN, CITY, COMPANY, COLLEGE, TECH, SPECIAL, COUNTRY +} \ No newline at end of file diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupRequestArgs.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupRequestArgs.java new file mode 100644 index 0000000..7d16821 --- /dev/null +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupRequestArgs.java @@ -0,0 +1,50 @@ +package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto; + +import lombok.Builder; +import lombok.Getter; + +import java.util.HashMap; +import java.util.Map; + +import static java.util.Objects.nonNull; + +/** + * Request arguments for group requests. + */ +@Builder +@Getter +public class GroupRequestArgs { + + private final String query; + private final GroupInfoType type; + private final GroupFilter filter; + + /** + * specified where to start getting groups + */ + private final Integer offset; + /** + * Limited number of groups. + */ + private final Integer limit; + + public Map populateQueries() { + Map queries = new HashMap<>(); + if(nonNull(query)) { + queries.put("query", query); + } + if(nonNull(type)) { + queries.put("type", type); + } + if(nonNull(filter)) { + queries.put("filter", filter); + } + if(nonNull(offset)) { + queries.put("offset", offset); + } + if(nonNull(limit)) { + queries.put("limit", limit); + } + return queries; + } +} \ No newline at end of file diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupVisibilityStatus.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupVisibilityStatus.java new file mode 100644 index 0000000..75c4da6 --- /dev/null +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupVisibilityStatus.java @@ -0,0 +1,7 @@ +package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto; +/** + * Group Visibility status. + */ +public enum GroupVisibilityStatus { + UNKNOWN, RESTRICTED, PUBLIC, PROTECTED, PRIVATE, DISABLED, DELETED +} diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupsCountRequestArgs.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupsCountRequestArgs.java new file mode 100644 index 0000000..a97d026 --- /dev/null +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/GroupsCountRequestArgs.java @@ -0,0 +1,34 @@ +package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto; + + +import lombok.Builder; +import lombok.Getter; +import java.util.HashMap; +import java.util.Map; + +import static java.util.Objects.nonNull; + +/** + * Request arguments for group count requests. + */ +@Builder +@Getter +public class GroupsCountRequestArgs { + private final String query; + private final GroupInfoType type; + private final GroupFilter filter; + + public Map populateQueries() { + Map queries = new HashMap<>(); + if (nonNull(query)) { + queries.put("query", query); + } + if (nonNull(type)) { + queries.put("type", type); + } + if (nonNull(filter)) { + queries.put("filter", filter); + } + return queries; + } +} diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/MeGroupInfo.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/MeGroupInfo.java new file mode 100644 index 0000000..dc1d473 --- /dev/null +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/MeGroupInfo.java @@ -0,0 +1,12 @@ +package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto; + +import lombok.Data; + +@Data +/** + * Group information related to authorized user. If there is no user - will be null. + */ +public class MeGroupInfo { + private MeGroupInfoStatus status; + private Integer userGroupId; +} diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/MeGroupInfoStatus.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/MeGroupInfoStatus.java new file mode 100644 index 0000000..73f1fa9 --- /dev/null +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/MeGroupInfoStatus.java @@ -0,0 +1,7 @@ +package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto; +/** + * Member group status. + */ +public enum MeGroupInfoStatus { + UNKNOWN, CANDIDATE, INVITEE, MEMBER, EDITOR, MODERATOR, ADMINISTRATOR, BANNED +} diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/UserDiscussionInfo.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/UserDiscussionInfo.java new file mode 100644 index 0000000..e8d4b39 --- /dev/null +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/dto/UserDiscussionInfo.java @@ -0,0 +1,13 @@ +package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto; +import lombok.Data; + +/** + * DTO for User discussion info. + */ +@Data +public class UserDiscussionInfo { + + private Boolean isBookmarked; + private Integer lastTime; + private Integer newCommentsCount; +} \ No newline at end of file diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/repository/GroupSubRepository.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/repository/GroupSubRepository.java new file mode 100644 index 0000000..8027a42 --- /dev/null +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/repository/GroupSubRepository.java @@ -0,0 +1,12 @@ +package com.github.JBolivarLi.javarushtelegrambot.bot.repository; + +import com.github.JBolivarLi.javarushtelegrambot.bot.repository.entity.GroupSub; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +/** + * {@link Repository} for {@link GroupSub} entity. + */ +@Repository +public interface GroupSubRepository extends JpaRepository { +} \ No newline at end of file diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/repository/entity/GroupSub.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/repository/entity/GroupSub.java new file mode 100644 index 0000000..d6c20fd --- /dev/null +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/repository/entity/GroupSub.java @@ -0,0 +1,42 @@ +package com.github.JBolivarLi.javarushtelegrambot.bot.repository.entity; + + +import lombok.Data; +import lombok.EqualsAndHashCode; + +import javax.persistence.*; +import java.util.ArrayList; +import java.util.List; + +import static java.util.Objects.isNull; + +@Data +@Entity +@Table(name = "group_sub") +@EqualsAndHashCode +public class GroupSub { + + @Id + private Integer id; + + @Column(name = "title") + private String title; + + @Column(name = "last_article_id") + private Integer lastArticleId; + + @ManyToMany(fetch = FetchType.EAGER) + @JoinTable( + name = "group_x_user", + joinColumns = @JoinColumn(name = "group_sub_id"), + inverseJoinColumns = @JoinColumn(name = "user_id") + ) + private List users; + + public void addUser(TelegramUser telegramUser) { + if (isNull(users)) { + users = new ArrayList<>(); + } + users.add(telegramUser); + } +} \ No newline at end of file diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/repository/entity/TelegramUser.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/repository/entity/TelegramUser.java index b363095..1df9ab0 100644 --- a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/repository/entity/TelegramUser.java +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/repository/entity/TelegramUser.java @@ -4,6 +4,9 @@ import javax.persistence.*; +import java.util.List; + + /** * Telegram User entity. */ @@ -18,4 +21,8 @@ public class TelegramUser { @Column(name = "active") private boolean active; + + @ManyToMany(mappedBy = "users", fetch = FetchType.EAGER) + private List groupSubs; + } diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/service/GroupSubService.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/service/GroupSubService.java new file mode 100644 index 0000000..e6c0524 --- /dev/null +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/service/GroupSubService.java @@ -0,0 +1,16 @@ +package com.github.JBolivarLi.javarushtelegrambot.bot.service; + +import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.GroupDiscussionInfo; +import com.github.JBolivarLi.javarushtelegrambot.bot.repository.entity.GroupSub; + +/** + * Service for manipulating with {@link GroupSub}. + */ +/** + * Service for manipulating with {@link GroupSub}. + */ +public interface GroupSubService { + + GroupSub save(String chatId, GroupDiscussionInfo groupDiscussionInfo); + +} \ No newline at end of file diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/service/GroupSubServiceImpl.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/service/GroupSubServiceImpl.java new file mode 100644 index 0000000..bf52ef9 --- /dev/null +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/service/GroupSubServiceImpl.java @@ -0,0 +1,46 @@ +package com.github.JBolivarLi.javarushtelegrambot.bot.service; + +import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.GroupDiscussionInfo; +import com.github.JBolivarLi.javarushtelegrambot.bot.repository.GroupSubRepository; +import com.github.JBolivarLi.javarushtelegrambot.bot.repository.entity.GroupSub; +import com.github.JBolivarLi.javarushtelegrambot.bot.repository.entity.TelegramUser; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import javax.ws.rs.NotFoundException; +import java.util.Optional; + +@Service +public class GroupSubServiceImpl implements GroupSubService { + + private final GroupSubRepository groupSubRepository; + private final TelegramUserService telegramUserService; + + @Autowired + public GroupSubServiceImpl(GroupSubRepository groupSubRepository, TelegramUserService telegramUserService) { + this.groupSubRepository = groupSubRepository; + this.telegramUserService = telegramUserService; + } + + @Override + public GroupSub save(String chatId, GroupDiscussionInfo groupDiscussionInfo) { + TelegramUser telegramUser = telegramUserService.findByChatId(chatId).orElseThrow(NotFoundException::new); + //TODO add exception handling + GroupSub groupSub; + Optional groupSubFromDB = groupSubRepository.findById(groupDiscussionInfo.getId()); + if(groupSubFromDB.isPresent()) { + groupSub = groupSubFromDB.get(); + Optional first = groupSub.getUsers().stream() + .filter(it -> it.getChatId().equalsIgnoreCase(chatId)) + .findFirst(); + if(first.isEmpty()) { + groupSub.addUser(telegramUser); + } + } else { + groupSub = new GroupSub(); + groupSub.addUser(telegramUser); + groupSub.setId(groupDiscussionInfo.getId()); + groupSub.setTitle(groupDiscussionInfo.getTitle()); + } + return groupSubRepository.save(groupSub); + } +} \ No newline at end of file diff --git a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/service/SendBotMessageService.java b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/service/SendBotMessageService.java index 54545de..ff4e3c4 100644 --- a/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/service/SendBotMessageService.java +++ b/src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/service/SendBotMessageService.java @@ -1,11 +1,25 @@ package com.github.JBolivarLi.javarushtelegrambot.bot.service; +import java.util.List; + +/** + * Service for sending messages via telegram-bot. + */ public interface SendBotMessageService { + /** * Send message via telegram bot. * - * @param chatId provided chatId in which messages would be sent. + * @param chatId provided chatId in which would be sent. * @param message provided message to be sent. */ void sendMessage(String chatId, String message); + + /** + * Send messages via telegram bot. + * + * @param chatId provided chatId in which would be sent. + * @param message collection of provided messages to be sent. + */ +// void sendMessage(Long chatId, List message); } diff --git a/src/main/resources/application-test.properties b/src/main/resources/application-test.properties index df5c387..3dbb3a9 100644 --- a/src/main/resources/application-test.properties +++ b/src/main/resources/application-test.properties @@ -6,4 +6,6 @@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # TelegramBot configurations: bot.username=tes -bot.token=1375780501:AAE4A6Rz0BSnIGzeu896OjQnjzsMEG6_uso \ No newline at end of file + +bot.token=1375780501:AAE4A6Rz0BSnIGzeu896OjQnjzsMEG6_uso + diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index ecf88a3..136c02f 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -7,3 +7,5 @@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # TelegramBot configurations: bot.username=TestJBolivarBot bot.token=6907574172:AAH_1pY6v_koa6K77p6GdRa6XT7ph7IRDBY + +javarush.api.path=https://javarush.com/api/1.0/rest \ No newline at end of file diff --git a/src/main/resources/db/migration/V00002__created_groupsub_many_to_many.sql b/src/main/resources/db/migration/V00002__created_groupsub_many_to_many.sql new file mode 100644 index 0000000..692a8bd --- /dev/null +++ b/src/main/resources/db/migration/V00002__created_groupsub_many_to_many.sql @@ -0,0 +1,21 @@ +-- add PRIMARY KEY FOR tg_user +ALTER TABLE tg_user ADD PRIMARY KEY (chat_id); + +-- ensure that the tables with these names are removed before creating a new one. +DROP TABLE IF EXISTS group_sub; +DROP TABLE IF EXISTS group_x_user; + +CREATE TABLE group_sub ( + id INT, + title VARCHAR(100), + last_article_id INT, + PRIMARY KEY (id) +); + +CREATE TABLE group_x_user ( + group_sub_id INT NOT NULL, + user_id VARCHAR(100) NOT NULL, + FOREIGN KEY (user_id) REFERENCES tg_user(chat_id), + FOREIGN KEY (group_sub_id) REFERENCES group_sub(id), + UNIQUE(user_id, group_sub_id) +); diff --git a/src/test/command/ListGroupSubCommandTest.java b/src/test/command/ListGroupSubCommandTest.java new file mode 100644 index 0000000..4b7affe --- /dev/null +++ b/src/test/command/ListGroupSubCommandTest.java @@ -0,0 +1,68 @@ +package command; + +import com.github.JBolivarLi.javarushtelegrambot.bot.command.ListGroupSubCommand; +import com.github.JBolivarLi.javarushtelegrambot.bot.repository.entity.GroupSub; +import com.github.JBolivarLi.javarushtelegrambot.bot.repository.entity.TelegramUser; +import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageService; +import com.github.JBolivarLi.javarushtelegrambot.bot.service.TelegramUserService; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.telegram.telegrambots.meta.api.objects.Message; +import org.telegram.telegrambots.meta.api.objects.Update; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +import static com.github.JBolivarLi.javarushtelegrambot.bot.command.CommandName.LIST_GROUP_SUB; + +public class ListGroupSubCommandTest { + + @Test + public void shouldProperlyShowsListGroupSub() { + //given + TelegramUser telegramUser = new TelegramUser(); + telegramUser.setActive(true); + telegramUser.setChatId("1"); + + List groupSubList = new ArrayList<>(); + groupSubList.add(populateGroupSub(1, "gs1")); + groupSubList.add(populateGroupSub(2, "gs2")); + groupSubList.add(populateGroupSub(3, "gs3")); + groupSubList.add(populateGroupSub(4, "gs4")); + + telegramUser.setGroupSubs(groupSubList); + + SendBotMessageService sendBotMessageService = Mockito.mock(SendBotMessageService.class); + TelegramUserService telegramUserService = Mockito.mock(TelegramUserService.class); + + Mockito.when(telegramUserService.findByChatId(telegramUser.getChatId())).thenReturn(Optional.of(telegramUser)); + + ListGroupSubCommand command = new ListGroupSubCommand(sendBotMessageService, telegramUserService); + + Update update = new Update(); + Message message = Mockito.mock(Message.class); + Mockito.when(message.getChatId()).thenReturn(Long.valueOf(telegramUser.getChatId())); + Mockito.when(message.getText()).thenReturn(LIST_GROUP_SUB.getCommandName()); + update.setMessage(message); + + String collectedGroups = "Я нашел все подписки на группы: \n\n" + + telegramUser.getGroupSubs().stream() + .map(it -> "Группа: " + it.getTitle() + " , ID = " + it.getId() + " \n") + .collect(Collectors.joining()); + + //when + command.execute(update); + + //then + Mockito.verify(sendBotMessageService).sendMessage(telegramUser.getChatId(), collectedGroups); + } + + private GroupSub populateGroupSub(Integer id, String title) { + GroupSub gs = new GroupSub(); + gs.setId(id); + gs.setTitle(title); + return gs; + } +} diff --git a/src/test/java/CommandContainerTest.java b/src/test/java/CommandContainerTest.java index 3a657ff..f0dd7c3 100644 --- a/src/test/java/CommandContainerTest.java +++ b/src/test/java/CommandContainerTest.java @@ -2,6 +2,8 @@ import com.github.JBolivarLi.javarushtelegrambot.bot.command.CommandContainer; import com.github.JBolivarLi.javarushtelegrambot.bot.command.CommandName; import com.github.JBolivarLi.javarushtelegrambot.bot.command.UnknownCommand; +import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.JavaRushGroupClient; +import com.github.JBolivarLi.javarushtelegrambot.bot.service.GroupSubService; import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageService; import com.github.JBolivarLi.javarushtelegrambot.bot.service.TelegramUserService; import org.junit.jupiter.api.Assertions; @@ -19,7 +21,9 @@ class CommandContainerTest { public void init() { SendBotMessageService sendBotMessageService = Mockito.mock(SendBotMessageService.class); TelegramUserService telegramUserService = Mockito.mock(TelegramUserService.class); - commandContainer = new CommandContainer(sendBotMessageService, telegramUserService); + JavaRushGroupClient groupClient = Mockito.mock(JavaRushGroupClient.class); + GroupSubService groupSubService = Mockito.mock(GroupSubService.class); + commandContainer = new CommandContainer(sendBotMessageService, telegramUserService,groupClient,groupSubService); } @Test public void shouldGetAllTheExistingCommands() { diff --git a/src/test/javarushclient/JavaRushGroupClientTest.java b/src/test/javarushclient/JavaRushGroupClientTest.java new file mode 100644 index 0000000..bfa00a4 --- /dev/null +++ b/src/test/javarushclient/JavaRushGroupClientTest.java @@ -0,0 +1,120 @@ +package javarushclient; + +import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.JavaRushGroupClient; +import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.JavaRushGroupClientImpl; +import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.GroupDiscussionInfo; +import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.GroupInfo; +import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.GroupRequestArgs; +import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.GroupsCountRequestArgs; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.GroupInfoType.TECH; + +@DisplayName("Integration-level testing for JavaRushGroupClientImplTest") +class JavaRushGroupClientTest { + + private final JavaRushGroupClient groupClient = new JavaRushGroupClientImpl("https://javarush.com/api/1.0/rest"); + + @Test + public void shouldProperlyGetGroupsWithEmptyArgs() { + //given + GroupRequestArgs args = GroupRequestArgs.builder().build(); + + //when + List groupList = groupClient.getGroupList(args); + + //then + Assertions.assertNotNull(groupList); + Assertions.assertFalse(groupList.isEmpty()); + } + + @Test + public void shouldProperlyGetWithOffSetAndLimit() { + //given + GroupRequestArgs args = GroupRequestArgs.builder() + .offset(1) + .limit(3) + .build(); + + //when + List groupList = groupClient.getGroupList(args); + + //then + Assertions.assertNotNull(groupList); + Assertions.assertEquals(3, groupList.size()); + } + + @Test + public void shouldProperlyGetGroupsDiscWithEmptyArgs() { + //given + GroupRequestArgs args = GroupRequestArgs.builder().build(); + + //when + List groupList = groupClient.getGroupDiscussionList(args); + + //then + Assertions.assertNotNull(groupList); + Assertions.assertFalse(groupList.isEmpty()); + } + + @Test + public void shouldProperlyGetGroupDiscWithOffSetAndLimit() { + //given + GroupRequestArgs args = GroupRequestArgs.builder() + .offset(1) + .limit(3) + .build(); + + //when + List groupList = groupClient.getGroupDiscussionList(args); + + //then + Assertions.assertNotNull(groupList); + Assertions.assertEquals(3, groupList.size()); + } + + @Test + public void shouldProperlyGetGroupCount() { + //given + GroupsCountRequestArgs args = GroupsCountRequestArgs.builder().build(); + + //when + Integer groupCount = groupClient.getGroupCount(args); + + //then + Assertions.assertEquals(30, groupCount); + } + + @Test + public void shouldProperlyGetGroupTECHCount() { + //given + GroupsCountRequestArgs args = GroupsCountRequestArgs.builder() + .type(TECH) + .build(); + + //when + Integer groupCount = groupClient.getGroupCount(args); + + //then + Assertions.assertEquals(7, groupCount); + } + + @Test + public void shouldProperlyGetGroupById() { + //given + Integer androidGroupId = 16; + + //when + GroupDiscussionInfo groupById = groupClient.getGroupById(androidGroupId); + + //then + Assertions.assertNotNull(groupById); + Assertions.assertEquals(16, groupById.getId()); + Assertions.assertEquals(TECH, groupById.getType()); + Assertions.assertEquals("android", groupById.getKey()); + } +} \ No newline at end of file diff --git a/src/test/repository/GroupSubRepositoryIT.java b/src/test/repository/GroupSubRepositoryIT.java new file mode 100644 index 0000000..47da586 --- /dev/null +++ b/src/test/repository/GroupSubRepositoryIT.java @@ -0,0 +1,46 @@ +package repository; + + +import com.github.JBolivarLi.javarushtelegrambot.bot.repository.GroupSubRepository; +import com.github.JBolivarLi.javarushtelegrambot.bot.repository.entity.GroupSub; +import com.github.JBolivarLi.javarushtelegrambot.bot.repository.entity.TelegramUser; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.jdbc.Sql; + +import java.util.List; +import java.util.Optional; + +import static org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace.NONE; + +/** + * Integration-level testing for {@link GroupSubRepository}. + */ +@ActiveProfiles("test") +@DataJpaTest +@AutoConfigureTestDatabase(replace = NONE) +public class GroupSubRepositoryIT { + + @Autowired + private GroupSubRepository groupSubRepository; + + @Sql(scripts = {"/sql/clearDbs.sql", "/sql/fiveUsersForGroupSub.sql"}) + @Test + public void shouldProperlyGetAllUsersForGroupSub() { + //when + Optional groupSubFromDB = groupSubRepository.findById(1); + + //then + Assertions.assertTrue(groupSubFromDB.isPresent()); + Assertions.assertEquals(1, groupSubFromDB.get().getId()); + List users = groupSubFromDB.get().getUsers(); + for(int i=0; i