forked from Sanders/TelegaBot
Fix tests
This commit is contained in:
@@ -9,6 +9,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.telegram.telegrambots.client.okhttp.OkHttpTelegramClient;
|
||||
import org.telegram.telegrambots.meta.generics.TelegramClient;
|
||||
import ru.cathub.telegabot.bot.TelegaBot;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package ru.cathub.telegabot.model;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Data
|
||||
@@ -16,13 +17,8 @@ public class BookCreationCache {
|
||||
private String title;
|
||||
private String author;
|
||||
private Integer rating;
|
||||
@Setter
|
||||
@Getter
|
||||
private CreationState creationState = CreationState.ADDING_TITLE;
|
||||
|
||||
public CreationState getCreationState() {
|
||||
return creationState;
|
||||
}
|
||||
|
||||
public void setCreationState(CreationState creationState) {
|
||||
this.creationState = creationState;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,9 +16,6 @@ import java.util.List;
|
||||
@Setter
|
||||
public class BotUser {
|
||||
|
||||
public BotUser(long l, String testuser) {
|
||||
}
|
||||
|
||||
public enum UserType {
|
||||
USER, ADMIN }
|
||||
|
||||
|
||||
@@ -47,14 +47,16 @@ public class BookCreationServiceImpl implements BookCreationService {
|
||||
try {
|
||||
switch (cache.getCreationState()) {
|
||||
case ADDING_TITLE -> {
|
||||
cache.setTitle(text);
|
||||
log.info("Processing ADDING_TITLE state...");
|
||||
cache.setTitle(text.trim());
|
||||
cache.setCreationState(BookCreationCache.CreationState.ADDING_AUTHOR);
|
||||
bookCacheService.updateCache(user.getId(), cache);
|
||||
telegramClientService.sendMessage(user, "✍️ Введите автора книги:");
|
||||
}
|
||||
|
||||
case ADDING_AUTHOR -> {
|
||||
cache.setAuthor(text);
|
||||
log.info("Processing ADDING_AUTHOR state...");
|
||||
cache.setAuthor(text.trim());
|
||||
cache.setCreationState(BookCreationCache.CreationState.ADDING_RATING);
|
||||
bookCacheService.updateCache(user.getId(), cache);
|
||||
telegramClientService.sendMessage(user, "⭐ Введите рейтинг книги (от 0 до 10):");
|
||||
|
||||
@@ -7,6 +7,8 @@ import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import ru.cathub.telegabot.model.Book;
|
||||
import ru.cathub.telegabot.model.BookCreationCache;
|
||||
import ru.cathub.telegabot.model.BotUser;
|
||||
@@ -18,12 +20,14 @@ import ru.cathub.telegabot.service.TelegramClientService;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static ru.cathub.telegabot.model.BookCreationCache.CreationState.*;
|
||||
import static ru.cathub.telegabot.utils.Constants.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class BookCreationServiceImplTest {
|
||||
|
||||
@Mock private BookCacheService bookCacheService;
|
||||
@Mock
|
||||
private BookCacheService bookCacheService;
|
||||
@Mock private TelegramClientService telegramClientService;
|
||||
@Mock private BookRepository bookRepository;
|
||||
@Mock private BookCommonService bookCommonService;
|
||||
@@ -58,7 +62,7 @@ class BookCreationServiceImplTest {
|
||||
|
||||
// Убеждаемся в корректности параметров
|
||||
assertEquals(1L, userIdCaptor.getValue());
|
||||
assertEquals(BookCreationCache.CreationState.ADDING_TITLE, cacheCaptor.getValue().getCreationState());
|
||||
assertEquals(ADDING_TITLE, cacheCaptor.getValue().getCreationState());
|
||||
|
||||
verify(telegramClientService).sendMessageWithMarkdown(
|
||||
eq(testUser),
|
||||
@@ -67,68 +71,84 @@ class BookCreationServiceImplTest {
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void handleExistingAddCache_ShouldProgressThroughStates() {
|
||||
// Title stage
|
||||
testCache.setCreationState(BookCreationCache.CreationState.ADDING_TITLE);
|
||||
when(bookCacheService.getCache(1L)).thenReturn(testCache);
|
||||
|
||||
// Process title
|
||||
void handleAddingTitle_ShouldTrimTitleAndTransitionToAuthorState() {
|
||||
// Given
|
||||
BookCreationCache titleCache = new BookCreationCache();
|
||||
titleCache.setCreationState(BookCreationCache.CreationState.ADDING_TITLE);
|
||||
when(bookCacheService.getCache(1L)).thenReturn(titleCache);
|
||||
|
||||
// When
|
||||
bookCreationService.handleExistingAddCache(testUser, " Valid Title ");
|
||||
|
||||
// Verify title update and state transition
|
||||
verify(bookCacheService).updateCache(eq(1L), argThat(cache ->
|
||||
cache.getTitle().equals("Valid Title") &&
|
||||
cache.getCreationState() == BookCreationCache.CreationState.ADDING_AUTHOR
|
||||
|
||||
// Then
|
||||
verify(bookCacheService).updateCache(eq(1L), argThat(cache ->
|
||||
"Valid Title".equals(cache.getTitle()) &&
|
||||
cache.getCreationState() == BookCreationCache.CreationState.ADDING_AUTHOR
|
||||
));
|
||||
verify(telegramClientService).sendMessage(eq(testUser), contains("Введите автора книги"));
|
||||
|
||||
// Author stage
|
||||
testCache.setCreationState(BookCreationCache.CreationState.ADDING_AUTHOR);
|
||||
testCache.setTitle("Valid Title");
|
||||
when(bookCacheService.getCache(1L)).thenReturn(testCache);
|
||||
|
||||
// Process author
|
||||
bookCreationService.handleExistingAddCache(testUser, " John Doe ");
|
||||
|
||||
// Verify author update and state transition
|
||||
verify(bookCacheService).updateCache(eq(1L), argThat(cache ->
|
||||
cache.getAuthor().equals("John Doe") &&
|
||||
cache.getCreationState() == BookCreationCache.CreationState.ADDING_RATING
|
||||
));
|
||||
verify(telegramClientService).sendMessage(eq(testUser), contains("Введите рейтинг"));
|
||||
|
||||
// Rating stage - valid input
|
||||
testCache.setCreationState(BookCreationCache.CreationState.ADDING_RATING);
|
||||
testCache.setAuthor("John Doe");
|
||||
when(bookCacheService.getCache(1L)).thenReturn(testCache);
|
||||
|
||||
// Process valid rating
|
||||
bookCreationService.handleExistingAddCache(testUser, "4");
|
||||
|
||||
// Verify final save and cleanup
|
||||
ArgumentCaptor<Book> bookCaptor = ArgumentCaptor.forClass(Book.class);
|
||||
verify(bookRepository).save(bookCaptor.capture());
|
||||
assertEquals("Valid Title", bookCaptor.getValue().getTitle());
|
||||
assertEquals("John Doe", bookCaptor.getValue().getAuthor());
|
||||
assertEquals(4, bookCaptor.getValue().getRating());
|
||||
verify(bookCacheService).clearCache(1L);
|
||||
verify(telegramClientService).sendMessageWithMarkdown(eq(testUser), contains("успешно добавлена"));
|
||||
|
||||
// Rating stage - invalid input
|
||||
reset(telegramClientService);
|
||||
bookCreationService.handleExistingAddCache(testUser, "6");
|
||||
verify(telegramClientService).sendMessage(eq(testUser), contains("от 1 до 5"));
|
||||
verify(bookRepository, times(1)).save(any()); // Verify only one save happened
|
||||
}
|
||||
|
||||
@Test
|
||||
void handleInvalidTitle_ShouldRejectShortTitles() {
|
||||
testCache.setCreationState(BookCreationCache.CreationState.ADDING_TITLE);
|
||||
when(bookCacheService.getCache(1L)).thenReturn(testCache);
|
||||
|
||||
bookCreationService.handleExistingAddCache(testUser, "A");
|
||||
verify(telegramClientService).sendMessage(eq(testUser), contains("минимум 2 символа"));
|
||||
assertNull(testCache.getTitle());
|
||||
void handleAddingAuthor_ShouldTrimAuthorAndTransitionToRatingState() {
|
||||
// Given
|
||||
BookCreationCache authorCache = new BookCreationCache();
|
||||
authorCache.setTitle("Valid Title");
|
||||
authorCache.setCreationState(BookCreationCache.CreationState.ADDING_AUTHOR);
|
||||
when(bookCacheService.getCache(1L)).thenReturn(authorCache);
|
||||
|
||||
// When
|
||||
bookCreationService.handleExistingAddCache(testUser, " John Doe ");
|
||||
|
||||
// Then
|
||||
verify(bookCacheService).updateCache(eq(1L), argThat(cache ->
|
||||
"John Doe".equals(cache.getAuthor()) &&
|
||||
cache.getCreationState() == BookCreationCache.CreationState.ADDING_RATING
|
||||
));
|
||||
verify(telegramClientService).sendMessage(eq(testUser), contains("Введите рейтинг"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void handleValidRating_ShouldSaveBookAndClearCache() {
|
||||
// Given
|
||||
BookCreationCache ratingCache = new BookCreationCache();
|
||||
ratingCache.setTitle("Valid Title");
|
||||
ratingCache.setAuthor("John Doe");
|
||||
ratingCache.setCreationState(BookCreationCache.CreationState.ADDING_RATING);
|
||||
when(bookCacheService.getCache(1L)).thenReturn(ratingCache);
|
||||
|
||||
// When
|
||||
bookCreationService.handleExistingAddCache(testUser, "4");
|
||||
|
||||
// Then
|
||||
ArgumentCaptor<Book> captor = ArgumentCaptor.forClass(Book.class);
|
||||
verify(bookRepository).save(captor.capture());
|
||||
|
||||
Book savedBook = captor.getValue();
|
||||
assertEquals("Valid Title", savedBook.getTitle());
|
||||
assertEquals("John Doe", savedBook.getAuthor());
|
||||
assertEquals(4, savedBook.getRating());
|
||||
|
||||
verify(bookCacheService).clearCache(1L);
|
||||
verify(telegramClientService).sendMessageWithMarkdown(eq(testUser), contains("успешно добавлена"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void handleInvalidRating_ShouldSendErrorMessage() {
|
||||
// Given
|
||||
BookCreationCache ratingCache = new BookCreationCache();
|
||||
ratingCache.setCreationState(BookCreationCache.CreationState.ADDING_RATING);
|
||||
when(bookCacheService.getCache(1L)).thenReturn(ratingCache);
|
||||
|
||||
// Reset предыдущих взаимодействий с telegramClientService
|
||||
reset(telegramClientService);
|
||||
|
||||
// When
|
||||
bookCreationService.handleExistingAddCache(testUser, "11");
|
||||
|
||||
// Then
|
||||
verify(telegramClientService).sendMessage(eq(testUser), contains("от 0 до 10"));
|
||||
verify(bookRepository, never()).save(any());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import ru.cathub.telegabot.model.Book;
|
||||
import ru.cathub.telegabot.model.BookCreationCache;
|
||||
import ru.cathub.telegabot.model.BookEditStateCache;
|
||||
import ru.cathub.telegabot.model.BotUser;
|
||||
import ru.cathub.telegabot.repository.BookRepository;
|
||||
@@ -34,7 +35,7 @@ class BookEditingServiceImplTest {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
testUser = new BotUser(1L, "testuser");
|
||||
testUser = BotUser.builder().id(1L).chatId(1L).build();
|
||||
testBook = Book.builder()
|
||||
.id(1L)
|
||||
.title("Test Book")
|
||||
@@ -55,6 +56,9 @@ class BookEditingServiceImplTest {
|
||||
|
||||
@Test
|
||||
void handleEditTitle_ShouldUpdateState() {
|
||||
BookEditStateCache editCache = new BookEditStateCache();
|
||||
editCache.setBookEditState(BookEditStateCache.BookEditState.EDITING_TITLE);
|
||||
when(bookCacheService.getBookEditCache(1L)).thenReturn(editCache);
|
||||
bookEditingService.handleEditTitle(testUser);
|
||||
verify(telegramClientService).sendMessage(eq(testUser), contains("название"));
|
||||
verify(bookCacheService).updateBookEditCache(eq(1L), argThat(cache ->
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package ru.cathub.telegabot.service.impl;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
@@ -8,8 +9,10 @@ import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import ru.cathub.telegabot.model.Book;
|
||||
import ru.cathub.telegabot.model.BotUser;
|
||||
import ru.cathub.telegabot.repository.BookRepository;
|
||||
import ru.cathub.telegabot.service.BookCacheService;
|
||||
import ru.cathub.telegabot.service.TelegramClientService;
|
||||
import ru.cathub.telegabot.utils.BookFormatUtils;
|
||||
@@ -26,8 +29,14 @@ class BookListingServiceImplTest {
|
||||
@Mock private BookCacheService bookCacheService;
|
||||
@Mock private TelegramClientService telegramClientService;
|
||||
@InjectMocks private BookListingServiceImpl bookListingService;
|
||||
|
||||
private final BotUser testUser = new BotUser(1L, "testuser");
|
||||
@Mock // Добавляем мок для BookRepository
|
||||
private BookRepository bookRepository;
|
||||
|
||||
private BotUser testUser;
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
testUser = BotUser.builder().id(1L).chatId(1L).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void showPaginatedBookList_ShouldHandleFirstPage() {
|
||||
@@ -36,6 +45,12 @@ class BookListingServiceImplTest {
|
||||
new Book(2L, "Book 2", "Author 2", 9, testUser)
|
||||
);
|
||||
Page<Book> page = new PageImpl<>(books);
|
||||
|
||||
when(bookRepository.findByBotUserOrderByIdAsc(eq(testUser), any(Pageable.class)))
|
||||
.thenReturn(page);
|
||||
|
||||
when(bookCacheService.getBookListState(testUser.getId()))
|
||||
.thenReturn(null);
|
||||
|
||||
bookListingService.showPaginatedBookList(testUser, 0, null, false);
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardMarkup;
|
||||
import ru.cathub.telegabot.model.BookCreationCache;
|
||||
import ru.cathub.telegabot.model.BotUser;
|
||||
import ru.cathub.telegabot.repository.UserRepository;
|
||||
@@ -32,14 +33,13 @@ class BookManageServiceImplTest {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
testUser = new BotUser(1L, "testuser");
|
||||
testUser = BotUser.builder().id(1L).chatId(1L).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void processBookInput_ShouldRouteAddBookCommand() {
|
||||
bookManageService.processBookInput(testUser, ADD_BOOK);
|
||||
|
||||
verify(bookCacheService).clearCache(1L);
|
||||
verify(bookCreationService).handleNewAddRequest(eq(testUser), eq(ADD_BOOK));
|
||||
}
|
||||
|
||||
@@ -56,6 +56,6 @@ class BookManageServiceImplTest {
|
||||
|
||||
assertEquals(BotUser.WorkingMode.BOOK, testUser.getWorkingMode());
|
||||
verify(userRepository).save(testUser);
|
||||
verify(telegramClientService).sendMessageWithMarkdown(eq(testUser), anyString(), any());
|
||||
verify(telegramClientService).sendMessageWithMarkdown(eq(testUser), anyString(), any(ReplyKeyboardMarkup.class));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user