scraping and improvements
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.storycove.service;
|
||||
|
||||
import com.storycove.entity.Author;
|
||||
import com.storycove.entity.Story;
|
||||
import com.storycove.repository.AuthorRepository;
|
||||
import com.storycove.service.exception.DuplicateResourceException;
|
||||
import com.storycove.service.exception.ResourceNotFoundException;
|
||||
@@ -24,6 +25,7 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.Mockito.times;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@DisplayName("Author Service Unit Tests")
|
||||
@@ -32,7 +34,6 @@ class AuthorServiceTest {
|
||||
@Mock
|
||||
private AuthorRepository authorRepository;
|
||||
|
||||
@InjectMocks
|
||||
private AuthorService authorService;
|
||||
|
||||
private Author testAuthor;
|
||||
@@ -44,6 +45,9 @@ class AuthorServiceTest {
|
||||
testAuthor = new Author("Test Author");
|
||||
testAuthor.setId(testId);
|
||||
testAuthor.setNotes("Test notes");
|
||||
|
||||
// Initialize service with null TypesenseService (which is allowed)
|
||||
authorService = new AuthorService(authorRepository, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -307,4 +311,133 @@ class AuthorServiceTest {
|
||||
assertEquals(5L, count);
|
||||
verify(authorRepository).countRecentAuthors(any(java.time.LocalDateTime.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should set author rating with validation")
|
||||
void shouldSetAuthorRating() {
|
||||
when(authorRepository.findById(testId)).thenReturn(Optional.of(testAuthor));
|
||||
when(authorRepository.save(any(Author.class))).thenReturn(testAuthor);
|
||||
|
||||
Author result = authorService.setRating(testId, 4);
|
||||
|
||||
assertEquals(4, testAuthor.getAuthorRating());
|
||||
verify(authorRepository, times(2)).findById(testId); // Called twice: once initially, once after flush
|
||||
verify(authorRepository).save(testAuthor);
|
||||
verify(authorRepository).flush();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception for invalid rating range")
|
||||
void shouldThrowExceptionForInvalidRating() {
|
||||
assertThrows(IllegalArgumentException.class, () -> authorService.setRating(testId, 0));
|
||||
assertThrows(IllegalArgumentException.class, () -> authorService.setRating(testId, 6));
|
||||
|
||||
verify(authorRepository, never()).findById(any());
|
||||
verify(authorRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should handle null rating")
|
||||
void shouldHandleNullRating() {
|
||||
when(authorRepository.findById(testId)).thenReturn(Optional.of(testAuthor));
|
||||
when(authorRepository.save(any(Author.class))).thenReturn(testAuthor);
|
||||
|
||||
Author result = authorService.setRating(testId, null);
|
||||
|
||||
assertNull(testAuthor.getAuthorRating());
|
||||
verify(authorRepository, times(2)).findById(testId); // Called twice: once initially, once after flush
|
||||
verify(authorRepository).save(testAuthor);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should find all authors with stories")
|
||||
void shouldFindAllAuthorsWithStories() {
|
||||
List<Author> authors = List.of(testAuthor);
|
||||
when(authorRepository.findAll()).thenReturn(authors);
|
||||
|
||||
List<Author> result = authorService.findAllWithStories();
|
||||
|
||||
assertEquals(1, result.size());
|
||||
verify(authorRepository).findAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should get author rating from database")
|
||||
void shouldGetAuthorRatingFromDb() {
|
||||
when(authorRepository.findAuthorRatingById(testId)).thenReturn(4);
|
||||
|
||||
Integer rating = authorService.getAuthorRatingFromDb(testId);
|
||||
|
||||
assertEquals(4, rating);
|
||||
verify(authorRepository).findAuthorRatingById(testId);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should calculate average story rating")
|
||||
void shouldCalculateAverageStoryRating() {
|
||||
// Setup test author with stories
|
||||
Story story1 = new Story("Story 1");
|
||||
story1.setRating(4);
|
||||
Story story2 = new Story("Story 2");
|
||||
story2.setRating(5);
|
||||
|
||||
testAuthor.getStories().add(story1);
|
||||
testAuthor.getStories().add(story2);
|
||||
|
||||
when(authorRepository.findById(testId)).thenReturn(Optional.of(testAuthor));
|
||||
|
||||
Double avgRating = authorService.calculateAverageStoryRating(testId);
|
||||
|
||||
assertEquals(4.5, avgRating);
|
||||
verify(authorRepository).findById(testId);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should find authors with stories using repository method")
|
||||
void shouldFindAuthorsWithStoriesFromRepository() {
|
||||
List<Author> authors = List.of(testAuthor);
|
||||
when(authorRepository.findAuthorsWithStories()).thenReturn(authors);
|
||||
|
||||
List<Author> result = authorService.findAuthorsWithStories();
|
||||
|
||||
assertEquals(1, result.size());
|
||||
verify(authorRepository).findAuthorsWithStories();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should find top rated authors")
|
||||
void shouldFindTopRatedAuthors() {
|
||||
List<Author> authors = List.of(testAuthor);
|
||||
when(authorRepository.findTopRatedAuthors()).thenReturn(authors);
|
||||
|
||||
List<Author> result = authorService.findTopRatedAuthors();
|
||||
|
||||
assertEquals(1, result.size());
|
||||
verify(authorRepository).findTopRatedAuthors();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should find most prolific authors")
|
||||
void shouldFindMostProlificAuthors() {
|
||||
List<Author> authors = List.of(testAuthor);
|
||||
when(authorRepository.findMostProlificAuthors()).thenReturn(authors);
|
||||
|
||||
List<Author> result = authorService.findMostProlificAuthors();
|
||||
|
||||
assertEquals(1, result.size());
|
||||
verify(authorRepository).findMostProlificAuthors();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should find authors by URL domain")
|
||||
void shouldFindAuthorsByUrlDomain() {
|
||||
List<Author> authors = List.of(testAuthor);
|
||||
when(authorRepository.findByUrlDomain("example.com")).thenReturn(authors);
|
||||
|
||||
List<Author> result = authorService.findByUrlDomain("example.com");
|
||||
|
||||
assertEquals(1, result.size());
|
||||
verify(authorRepository).findByUrlDomain("example.com");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user