Ciao a tutti, miei cari amici, futuri ingegneri software senior. Continuiamo a sviluppare un bot di Telegram. In questa fase del nostro progetto, prenderemo in considerazione tre attività che hanno un valore più visibile rispetto al valore del programma. Dobbiamo imparare come rimuovere un abbonamento a nuovi articoli da un gruppo specifico: utilizzare il comando /stop per disattivare il bot e utilizzare il comando /start per attivarlo. Inoltre, tutte le richieste e gli aggiornamenti riguardano solo gli utenti attivi del bot. Come al solito, aggiorneremo il ramo principale per ottenere tutte le modifiche e crearne uno nuovo: STEP_7_JRTB-7. In questa parte parleremo dell'eliminazione di un abbonamento e considereremo 5 opzioni per gli eventi: sarà interessante.
Aggiorniamo la versione del nostro progetto a 0.6.0-SNAPSHOT Aggiorniamo RELEASE_NOTES.md, aggiungendo una descrizione di quanto fatto nella nuova versione:
metti mi piace - iscriviti - suona il campanello, dai una stella al nostro progetto , scrivi commenti e valuta l'articolo! Grazie a tutti. Fino alla parte successiva. Parleremo presto di come aggiungere la disattivazione e l'attivazione dei bot tramite i comandi /stop e /start e di come utilizzarli al meglio. Arrivederci!
JRTB-7: rimozione di un abbonamento a nuovi articoli da un gruppo
È chiaro che tutti gli utenti vorranno poter cancellare la propria iscrizione per non ricevere notifiche sui nuovi articoli. La sua logica sarà molto simile alla logica di aggiunta di un abbonamento. Se inviamo un solo comando, in risposta riceveremo l'elenco dei gruppi e dei relativi ID a cui l'utente è già iscritto, in modo da poter capire cosa esattamente occorre eliminare. E se l'utente invia l'ID del gruppo insieme al team, elimineremo l'abbonamento. Andiamo quindi a sviluppare questo comando dal lato bot di Telegram.-
Aggiungiamo il nome del nuovo comando - /deleteGroupSub e in CommandName - la riga:
DELETE_GROUP_SUB("/deleteGroupSub")
-
Successivamente, creiamo il comando DeleteGroupSubCommand :
package com.github.javarushcommunity.jrtb.command; import com.github.javarushcommunity.jrtb.repository.entity.GroupSub; import com.github.javarushcommunity.jrtb.repository.entity.TelegramUser; import com.github.javarushcommunity.jrtb.service.GroupSubService; import com.github.javarushcommunity.jrtb.service.SendBotMessageService; import com.github.javarushcommunity.jrtb.service.TelegramUserService; import org.springframework.util.CollectionUtils; import org.telegram.telegrambots.meta.api.objects.Update; import javax.ws.rs.NotFoundException; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; import static com.github.javarushcommunity.jrtb.command.CommandName.DELETE_GROUP_SUB; import static com.github.javarushcommunity.jrtb.command.CommandUtils.getChatId; import static com.github.javarushcommunity.jrtb.command.CommandUtils.getMessage; import static java.lang.String.format; 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 = "Пока нет подписок на группы. Whatбы добавить подписку напиши /addGroupSub"; } else { message = "Whatбы удалить подписку на группу - передай комадну вместе с ID группы. \n" + "Например: /deleteGroupSub 16 \n\n" + "я подготовил список всех групп, на которые ты подписан) \n\n" + "Name группы - 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)); } }
@Data
@Entity
@Table(name = "tg_user")
@EqualsAndHashCode(exclude = "groupSubs")
public class TelegramUser {
@Id
@Column(name = "chat_id")
private String chatId;
@Column(name = "active")
private boolean active;
@ManyToMany(mappedBy = "users", fetch = FetchType.EAGER)
private List<GroupSub> groupSubs;
}
Di conseguenza, tutto è andato come volevamo. Ci sono diversi eventi possibili in una squadra, quindi scrivere un buon test per ciascuno di essi è un'ottima idea. A proposito di test: mentre li scrivevo ho riscontrato un difetto nella logica e l'ho corretto prima che venisse messo in produzione. Se non ci fosse stato il test, non è chiaro quanto velocemente sarebbe stato rilevato. EliminaGruppoSubCommandTest:
package com.github.javarushcommunity.jrtb.command;
import com.github.javarushcommunity.jrtb.repository.entity.GroupSub;
import com.github.javarushcommunity.jrtb.repository.entity.TelegramUser;
import com.github.javarushcommunity.jrtb.service.GroupSubService;
import com.github.javarushcommunity.jrtb.service.SendBotMessageService;
import com.github.javarushcommunity.jrtb.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.javarushcommunity.jrtb.command.AbstractCommandTest.prepareUpdate;
import static com.github.javarushcommunity.jrtb.command.CommandName.DELETE_GROUP_SUB;
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 = "Пока нет подписок на группы. Whatбы добавить подписку напиши /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 = "Whatбы удалить подписку на группу - передай комадну вместе с ID группы. \n" +
"Например: /deleteGroupSub 16 \n\n" +
"я подготовил список всех групп, на которые ты подписан) \n\n" +
"Name группы - 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);
}
}
Qui, ogni test controlla uno scenario separato e, lascia che te lo ricordi, ce ne sono solo cinque:
- quando hai semplicemente passato il comando /deleteGroupSub e non ci sono iscrizioni ai gruppi;
- quando hai semplicemente passato il comando /deleteGroupSub e ci sono iscrizioni ai gruppi;
- quando è stato passato un ID di gruppo non valido, ad esempio /deleteGroupSub abc ;
- uno scenario in cui tutto viene cancellato correttamente, come previsto;
- uno scenario in cui l'ID del gruppo è valido, ma tale gruppo non è presente nel database.
.put(DELETE_GROUP_SUB.getCommandName(),
new DeleteGroupSubCommand(sendBotMessageService, groupSubService, telegramUserService))
Ora puoi testare la funzionalità su un bot di prova. Lanciamo il nostro database utilizzando docker-compose-test.yml: docker-compose -f docker-compose-test.yml up E lanciamo SpringBoot tramite IDEA. Cancellerò completamente la corrispondenza con il bot e ricomincerò. Esaminerò tutte le opzioni che potrebbero sorgere quando si lavora con questo team. Come puoi vedere dallo screenshot, tutte le opzioni sono state eseguite e hanno avuto successo.
Amici! Vuoi sapere subito quando viene rilasciato il nuovo codice per un progetto? Quando esce un nuovo articolo? Unisciti al mio canale Telegram . Lì raccolgo insieme i miei articoli, pensieri e sviluppo open source. |
## 0.6.0-SNAPSHOT * JRTB-7: aggiunta la possibilità di eliminare l'iscrizione al gruppo.
Il codice funziona, sono stati scritti dei test per questo: è ora di inserire l'attività nel repository e creare una richiesta pull.
GO TO FULL VERSION