Files
TelegaBot/src/test/java/ru/cathub/telegabot/service/impl/BookCreationServiceImplTest.java
T

135 lines
5.8 KiB
Java

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.ArgumentCaptor;
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.BotUser;
import ru.cathub.telegabot.repository.BookRepository;
import ru.cathub.telegabot.service.BookCacheService;
import ru.cathub.telegabot.service.BookCommonService;
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.utils.Constants.*;
@ExtendWith(MockitoExtension.class)
class BookCreationServiceImplTest {
@Mock private BookCacheService bookCacheService;
@Mock private TelegramClientService telegramClientService;
@Mock private BookRepository bookRepository;
@Mock private BookCommonService bookCommonService;
@InjectMocks private BookCreationServiceImpl bookCreationService;
private BotUser testUser;
private BookCreationCache testCache;
@BeforeEach
void setUp() {
testUser = new BotUser();
testUser.setId(1L);
testUser.setChatId(1L); // Явная установка ID
testCache = new BookCreationCache();
}
@Test
void handleNewAddRequest_ShouldInitializeCache() {
// Используем ArgumentCaptor для захвата аргументов
ArgumentCaptor<Long> userIdCaptor = ArgumentCaptor.forClass(Long.class);
ArgumentCaptor<BookCreationCache> cacheCaptor = ArgumentCaptor.forClass(BookCreationCache.class);
// Мокируем вызов с конкретными аргументами
when(bookCacheService.updateCache(eq(1L), any(BookCreationCache.class)))
.thenReturn(testCache);
bookCreationService.handleNewAddRequest(testUser, ADD_BOOK);
// Проверяем аргументы вызова
verify(bookCacheService).updateCache(userIdCaptor.capture(), cacheCaptor.capture());
// Убеждаемся в корректности параметров
assertEquals(1L, userIdCaptor.getValue());
assertEquals(BookCreationCache.CreationState.ADDING_TITLE, cacheCaptor.getValue().getCreationState());
verify(telegramClientService).sendMessageWithMarkdown(
eq(testUser),
contains("Введите название книги"),
isNull()
);
}
@Test
void handleExistingAddCache_ShouldProgressThroughStates() {
// Title stage
testCache.setCreationState(BookCreationCache.CreationState.ADDING_TITLE);
when(bookCacheService.getCache(1L)).thenReturn(testCache);
// Process title
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
));
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());
}
}