test: Enhance book creation state transition test with detailed verification

This commit is contained in:
2025-02-10 19:03:28 +03:00
parent 39f982d72f
commit 35a9092368
@@ -69,27 +69,57 @@ class BookCreationServiceImplTest {
@Test
void handleExistingAddCache_ShouldProgressThroughStates() {
// Test title handling
// Title stage
testCache.setCreationState(BookCreationCache.CreationState.ADDING_TITLE);
when(bookCacheService.getCache(1L)).thenReturn(testCache);
// Process title
bookCreationService.handleExistingAddCache(testUser, " Valid Title ");
assertEquals("Valid Title", testCache.getTitle());
assertEquals(BookCreationCache.CreationState.ADDING_AUTHOR, testCache.getCreationState());
// Test author handling
testCache.setCreationState(BookCreationCache.CreationState.ADDING_AUTHOR);
bookCreationService.handleExistingAddCache(testUser, " John Doe ");
assertEquals("John Doe", testCache.getAuthor());
assertEquals(BookCreationCache.CreationState.ADDING_RATING, testCache.getCreationState());
// Test rating handling
testCache.setCreationState(BookCreationCache.CreationState.ADDING_RATING);
when(bookCommonService.formatBookDetails(any())).thenReturn("Book Details");
bookCreationService.handleExistingAddCache(testUser, "8");
verify(bookRepository).save(any(Book.class));
// 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