-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
S* JRTB-7: added the ability to delete group subscription. (#22)
- Loading branch information
Showing
18 changed files
with
309 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...ain/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/DeleteGroupSubCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.github.JBolivarLi.javarushtelegrambot.bot.command; | ||
|
||
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.GroupSubService; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageService; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.service.TelegramUserService; | ||
import org.springframework.util.CollectionUtils; | ||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.github.JBolivarLi.javarushtelegrambot.bot.command.CommandName.DELETE_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.lang.String.format; | ||
import javax.ws.rs.NotFoundException; | ||
import static org.apache.commons.lang3.StringUtils.SPACE; | ||
import static org.apache.commons.lang3.StringUtils.isNumeric; | ||
|
||
/** | ||
* Delete Group subscription {@link Command}. | ||
*/ | ||
public class DeleteGroupSubCommand implements Command { | ||
|
||
private final SendBotMessageService sendBotMessageService; | ||
private final TelegramUserService telegramUserService; | ||
private final GroupSubService groupSubService; | ||
|
||
public DeleteGroupSubCommand(SendBotMessageService sendBotMessageService, GroupSubService groupSubService, | ||
TelegramUserService telegramUserService) { | ||
this.sendBotMessageService = sendBotMessageService; | ||
this.groupSubService = groupSubService; | ||
this.telegramUserService = telegramUserService; | ||
} | ||
|
||
@Override | ||
public void execute(Update update) { | ||
if (getMessage(update).equalsIgnoreCase(DELETE_GROUP_SUB.getCommandName())) { | ||
sendGroupIdList(getChatId(update)); | ||
return; | ||
} | ||
String groupId = getMessage(update).split(SPACE)[1]; | ||
String chatId = getChatId(update); | ||
if (isNumeric(groupId)) { | ||
Optional<GroupSub> optionalGroupSub = groupSubService.findById(Integer.valueOf(groupId)); | ||
if (optionalGroupSub.isPresent()) { | ||
GroupSub groupSub = optionalGroupSub.get(); | ||
TelegramUser telegramUser = telegramUserService.findByChatId(chatId).orElseThrow(NotFoundException::new); | ||
groupSub.getUsers().remove(telegramUser); | ||
groupSubService.save(groupSub); | ||
sendBotMessageService.sendMessage(chatId, format("Удалил подписку на группу: %s", groupSub.getTitle())); | ||
} else { | ||
sendBotMessageService.sendMessage(chatId, "Не нашел такой группы =/"); | ||
} | ||
} else { | ||
sendBotMessageService.sendMessage(chatId, "неправильный формат ID группы.\n " + | ||
"ID должно быть целым положительным числом"); | ||
} | ||
} | ||
|
||
private void sendGroupIdList(String chatId) { | ||
String message; | ||
List<GroupSub> groupSubs = telegramUserService.findByChatId(chatId) | ||
.orElseThrow(NotFoundException::new) | ||
.getGroupSubs(); | ||
if (CollectionUtils.isEmpty(groupSubs)) { | ||
message = "Пока нет подписок на группы. Чтобы добавить подписку напиши /addGroupSub"; | ||
} else { | ||
message = "Чтобы удалить подписку на группу - передай комадну вместе с ID группы. \n" + | ||
"Например: /deleteGroupSub 16 \n\n" + | ||
"я подготовил список всех групп, на которые ты подписан) \n\n" + | ||
"имя группы - ID группы \n\n" + | ||
"%s"; | ||
|
||
} | ||
String userGroupSubData = groupSubs.stream() | ||
.map(group -> format("%s - %s \n", group.getTitle(), group.getId())) | ||
.collect(Collectors.joining()); | ||
|
||
sendBotMessageService.sendMessage(chatId, format(message, userGroupSubData)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/test/java/CommandContainerTest.java → src/test/command/CommandContainerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package command; | ||
|
||
import com.github.JBolivarLi.javarushtelegrambot.bot.command.Command; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.command.DeleteGroupSubCommand; | ||
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.GroupSubService; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageService; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.service.TelegramUserService; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Optional; | ||
|
||
import static com.github.JBolivarLi.javarushtelegrambot.bot.command.CommandName.DELETE_GROUP_SUB; | ||
import static command.AbstractCommandTest.prepareUpdate; | ||
import static java.util.Collections.singletonList; | ||
@DisplayName("Unit-level testing for DeleteGroupSubCommand") | ||
class DeleteGroupSubCommandTest { | ||
|
||
private Command command; | ||
private SendBotMessageService sendBotMessageService; | ||
GroupSubService groupSubService; | ||
TelegramUserService telegramUserService; | ||
|
||
|
||
@BeforeEach | ||
public void init() { | ||
sendBotMessageService = Mockito.mock(SendBotMessageService.class); | ||
groupSubService = Mockito.mock(GroupSubService.class); | ||
telegramUserService = Mockito.mock(TelegramUserService.class); | ||
|
||
command = new DeleteGroupSubCommand(sendBotMessageService, groupSubService, telegramUserService); | ||
} | ||
|
||
@Test | ||
public void shouldProperlyReturnEmptySubscriptionList() { | ||
//given | ||
Long chatId = 23456L; | ||
Update update = prepareUpdate(chatId, DELETE_GROUP_SUB.getCommandName()); | ||
|
||
Mockito.when(telegramUserService.findByChatId(String.valueOf(chatId))) | ||
.thenReturn(Optional.of(new TelegramUser())); | ||
|
||
String expectedMessage = "Пока нет подписок на группы. Чтобы добавить подписку напиши /addGroupSub"; | ||
|
||
//when | ||
command.execute(update); | ||
|
||
//then | ||
Mockito.verify(sendBotMessageService).sendMessage(chatId.toString(), expectedMessage); | ||
} | ||
|
||
@Test | ||
public void shouldProperlyReturnSubscriptionLit() { | ||
//given | ||
Long chatId = 23456L; | ||
Update update = prepareUpdate(chatId, DELETE_GROUP_SUB.getCommandName()); | ||
TelegramUser telegramUser = new TelegramUser(); | ||
GroupSub gs1 = new GroupSub(); | ||
gs1.setId(123); | ||
gs1.setTitle("GS1 Title"); | ||
telegramUser.setGroupSubs(singletonList(gs1)); | ||
Mockito.when(telegramUserService.findByChatId(String.valueOf(chatId))) | ||
.thenReturn(Optional.of(telegramUser)); | ||
|
||
String expectedMessage = "Чтобы удалить подписку на группу - передай комадну вместе с ID группы. \n" + | ||
"Например: /deleteGroupSub 16 \n\n" + | ||
"я подготовил список всех групп, на которые ты подписан) \n\n" + | ||
"имя группы - ID группы \n\n" + | ||
"GS1 Title - 123 \n"; | ||
|
||
//when | ||
command.execute(update); | ||
|
||
//then | ||
Mockito.verify(sendBotMessageService).sendMessage(chatId.toString(), expectedMessage); | ||
} | ||
|
||
@Test | ||
public void shouldRejectByInvalidGroupId() { | ||
//given | ||
Long chatId = 23456L; | ||
Update update = prepareUpdate(chatId, String.format("%s %s", DELETE_GROUP_SUB.getCommandName(), "groupSubId")); | ||
TelegramUser telegramUser = new TelegramUser(); | ||
GroupSub gs1 = new GroupSub(); | ||
gs1.setId(123); | ||
gs1.setTitle("GS1 Title"); | ||
telegramUser.setGroupSubs(singletonList(gs1)); | ||
Mockito.when(telegramUserService.findByChatId(String.valueOf(chatId))) | ||
.thenReturn(Optional.of(telegramUser)); | ||
|
||
String expectedMessage = "неправильный формат ID группы.\n " + | ||
"ID должно быть целым положительным числом"; | ||
|
||
//when | ||
command.execute(update); | ||
|
||
//then | ||
Mockito.verify(sendBotMessageService).sendMessage(chatId.toString(), expectedMessage); | ||
} | ||
|
||
@Test | ||
public void shouldProperlyDeleteByGroupId() { | ||
//given | ||
|
||
/// prepare update object | ||
Long chatId = 23456L; | ||
Integer groupId = 1234; | ||
Update update = prepareUpdate(chatId, String.format("%s %s", DELETE_GROUP_SUB.getCommandName(), groupId)); | ||
|
||
|
||
GroupSub gs1 = new GroupSub(); | ||
gs1.setId(123); | ||
gs1.setTitle("GS1 Title"); | ||
TelegramUser telegramUser = new TelegramUser(); | ||
telegramUser.setChatId(chatId.toString()); | ||
telegramUser.setGroupSubs(singletonList(gs1)); | ||
ArrayList<TelegramUser> users = new ArrayList<>(); | ||
users.add(telegramUser); | ||
gs1.setUsers(users); | ||
Mockito.when(groupSubService.findById(groupId)).thenReturn(Optional.of(gs1)); | ||
Mockito.when(telegramUserService.findByChatId(String.valueOf(chatId))) | ||
.thenReturn(Optional.of(telegramUser)); | ||
|
||
String expectedMessage = "Удалил подписку на группу: GS1 Title"; | ||
|
||
//when | ||
command.execute(update); | ||
|
||
//then | ||
users.remove(telegramUser); | ||
Mockito.verify(groupSubService).save(gs1); | ||
Mockito.verify(sendBotMessageService).sendMessage(chatId.toString(), expectedMessage); | ||
} | ||
|
||
@Test | ||
public void shouldDoesNotExistByGroupId() { | ||
//given | ||
Long chatId = 23456L; | ||
Integer groupId = 1234; | ||
Update update = prepareUpdate(chatId, String.format("%s %s", DELETE_GROUP_SUB.getCommandName(), groupId)); | ||
|
||
|
||
Mockito.when(groupSubService.findById(groupId)).thenReturn(Optional.empty()); | ||
|
||
String expectedMessage = "Не нашел такой группы =/"; | ||
|
||
//when | ||
command.execute(update); | ||
|
||
//then | ||
Mockito.verify(groupSubService).findById(groupId); | ||
Mockito.verify(sendBotMessageService).sendMessage(chatId.toString(), expectedMessage); | ||
} | ||
} | ||
|
2 changes: 2 additions & 0 deletions
2
src/test/java/HelpCommandTest.java → src/test/command/HelpCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.