Fix: cannot change lower and upper case letters in metadata (#1602)

This commit is contained in:
Aditya Chandel
2025-11-21 10:30:29 -07:00
committed by GitHub
parent 9a70fb5d81
commit 0190ba8677
3 changed files with 10 additions and 98 deletions
@@ -15,6 +15,8 @@ public interface AuthorRepository extends JpaRepository<AuthorEntity, Long> {
Optional<AuthorEntity> findByName(String name);
Optional<AuthorEntity> findByNameIgnoreCase(String name);
@Query("SELECT a FROM AuthorEntity a JOIN a.bookMetadataEntityList bm WHERE bm.bookId = :bookId")
List<AuthorEntity> findAuthorsByBookId(@Param("bookId") Long bookId);
}
@@ -88,7 +88,11 @@ public class MetadataManagementService {
private void consolidateAuthors(List<String> targetValues, List<String> valuesToMerge, boolean writeToFile, boolean moveFile) {
List<AuthorEntity> targetAuthors = targetValues.stream()
.map(name -> authorRepository.findByName(name)
.map(name -> authorRepository.findByNameIgnoreCase(name)
.map(existing -> {
existing.setName(name);
return authorRepository.save(existing);
})
.orElseGet(() -> {
AuthorEntity author = new AuthorEntity();
author.setName(name);
@@ -97,7 +101,7 @@ public class MetadataManagementService {
.toList();
List<AuthorEntity> authorsToMerge = valuesToMerge.stream()
.map(authorRepository::findByName)
.map(authorRepository::findByNameIgnoreCase)
.filter(java.util.Optional::isPresent)
.map(java.util.Optional::get)
.toList();
@@ -65,14 +65,14 @@ class MetadataManagementServiceTest {
AuthorEntity oldAuthor = new AuthorEntity();
oldAuthor.setName(oldName);
when(authorRepository.findByName(targetName)).thenReturn(Optional.empty());
when(authorRepository.findByNameIgnoreCase(targetName)).thenReturn(Optional.empty());
when(authorRepository.save(any(AuthorEntity.class))).thenAnswer(invocation -> {
AuthorEntity a = invocation.getArgument(0);
a.setName(a.getName());
return a;
});
when(authorRepository.findByName(oldName)).thenReturn(Optional.of(oldAuthor));
when(authorRepository.findByNameIgnoreCase(oldName)).thenReturn(Optional.of(oldAuthor));
BookMetadataEntity metadata = mock(BookMetadataEntity.class);
Set<AuthorEntity> authorsSet = new HashSet<>();
@@ -93,28 +93,6 @@ class MetadataManagementServiceTest {
verify(authorRepository).delete(oldAuthor);
}
@Test
void deleteAuthors_removesFromBooksAndDeletes() {
String name = "Author To Delete";
AuthorEntity author = new AuthorEntity();
author.setName(name);
when(authorRepository.findByName(name)).thenReturn(Optional.of(author));
BookMetadataEntity metadata = mock(BookMetadataEntity.class);
Set<AuthorEntity> authorsSet = new HashSet<>();
authorsSet.add(author);
when(metadata.getAuthors()).thenReturn(authorsSet);
when(bookMetadataRepository.findAllByAuthorsContaining(author)).thenReturn(List.of(metadata));
service.deleteMetadata(MergeMetadataType.authors, List.of(name));
assertThat(authorsSet).doesNotContain(author);
verify(bookMetadataRepository).saveAll(bookListCaptor.capture());
verify(authorRepository).delete(author);
}
@Test
void mergeCategories_movesAndDeletesOldCategory() {
String targetName = "New Category";
@@ -299,37 +277,6 @@ class MetadataManagementServiceTest {
() -> service.consolidateMetadata(MergeMetadataType.languages, List.of("L1", "L2"), List.of("Old")));
}
@Test
void mergeAuthors_targetExists_usesExistingTargetAndDeletesOld() {
String targetName = "Existing Author";
String oldName = "Old Author";
AuthorEntity existingAuthor = new AuthorEntity();
existingAuthor.setName(targetName);
AuthorEntity oldAuthor = new AuthorEntity();
oldAuthor.setName(oldName);
when(authorRepository.findByName(targetName)).thenReturn(Optional.of(existingAuthor));
when(authorRepository.findByName(oldName)).thenReturn(Optional.of(oldAuthor));
BookMetadataEntity metadata = mock(BookMetadataEntity.class);
Set<AuthorEntity> authorsSet = new HashSet<>();
authorsSet.add(oldAuthor);
when(metadata.getAuthors()).thenReturn(authorsSet);
when(bookMetadataRepository.findAllByAuthorsContaining(oldAuthor)).thenReturn(List.of(metadata));
service.consolidateMetadata(MergeMetadataType.authors, List.of(targetName), List.of(oldName));
assertThat(authorsSet).doesNotContain(oldAuthor);
assertThat(authorsSet).contains(existingAuthor);
verify(authorRepository, never()).save(any(AuthorEntity.class));
verify(authorRepository).delete(oldAuthor);
verify(bookMetadataRepository).saveAll(bookListCaptor.capture());
}
@Test
void mergeTags_mergesMultipleOldTagsIntoSingleTarget() {
String targetName = "UnifiedTag";
@@ -367,37 +314,6 @@ class MetadataManagementServiceTest {
verify(bookMetadataRepository, times(2)).saveAll(anyList());
}
@Test
void mergeAuthors_ignoresNonExistentOldAuthor() {
String targetName = "TargetAuthor";
String existingOld = "ExistingOld";
String missingOld = "MissingOld";
AuthorEntity target = new AuthorEntity();
target.setName(targetName);
AuthorEntity old = new AuthorEntity();
old.setName(existingOld);
when(authorRepository.findByName(targetName)).thenReturn(Optional.of(target));
when(authorRepository.findByName(existingOld)).thenReturn(Optional.of(old));
when(authorRepository.findByName(missingOld)).thenReturn(Optional.empty());
BookMetadataEntity metadata = mock(BookMetadataEntity.class);
Set<AuthorEntity> authors = new HashSet<>();
authors.add(old);
when(metadata.getAuthors()).thenReturn(authors);
when(bookMetadataRepository.findAllByAuthorsContaining(old)).thenReturn(List.of(metadata));
service.consolidateMetadata(MergeMetadataType.authors, List.of(targetName), List.of(existingOld, missingOld));
assertThat(authors).doesNotContain(old);
assertThat(authors).contains(target);
verify(authorRepository).delete(old);
verify(authorRepository, never()).delete(argThat(a -> missingOld.equals(a.getName())));
verify(bookMetadataRepository).saveAll(anyList());
}
@Test
void mergeCategories_doesNotDuplicateExistingTarget() {
String targetName = "CatTarget";
@@ -464,16 +380,6 @@ class MetadataManagementServiceTest {
verify(tagRepository).delete(old);
}
@Test
void deleteAuthors_noMatchingAuthor_noOps() {
when(authorRepository.findByName("NoAuthor")).thenReturn(Optional.empty());
service.deleteMetadata(MergeMetadataType.authors, List.of("NoAuthor"));
verify(bookMetadataRepository, never()).saveAll(anyList());
verify(authorRepository, never()).delete(any());
}
@Test
void deleteTags_partialMissing_ignoresMissing() {
String present = "PresentTag";