Skip to content

Commit

Permalink
MODLD-617: special handling when authority is reverted to a previous …
Browse files Browse the repository at this point in the history
…state
  • Loading branch information
pkjacob committed Feb 20, 2025
1 parent cfcf284 commit 0ff6d93
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public interface FolioMetadataRepository extends JpaRepository<FolioMetadata, Lo

Optional<IdOnly> findIdByInventoryId(String inventoryId);

Optional<IdOnly> findIdBySrsId(String srsId);

boolean existsBySrsId(String srsId);

Optional<FolioMetadata> findByInventoryId(String inventoryId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,46 @@ private RequestProcessingException notFoundException(String srsId) {
@Override
public Long saveMarcAuthority(org.folio.ld.dictionary.model.Resource modelResource) {
var mapped = resourceModelMapper.toEntity(modelResource);
if (!mapped.isAuthority()) {
var message = "Resource is not an authority";
log.error(message);
throw new IllegalArgumentException(message);
}
if (resourceRepo.existsById(modelResource.getId())) {
validateResource(mapped);
if (sameResourceExistsByIdAndSrsId(modelResource)) {
return updateAuthority(mapped);
}
if (folioMetadataRepository.existsBySrsId(modelResource.getFolioMetadata().getSrsId())) {
if (resourceExistsBySrsId(modelResource)) {
return replaceAuthority(mapped);
}
return createAuthority(mapped);
}

private static void validateResource(Resource resource) {
if (!resource.isAuthority()) {
logAndThrow("Resource is not an authority");
}

if (resource.getFolioMetadata() == null || resource.getFolioMetadata().getSrsId() == null) {
logAndThrow("SRS ID is missing in the resource");
}
}

private static void logAndThrow(String message) {
log.error(message);
throw new IllegalArgumentException(message);
}

private boolean sameResourceExistsByIdAndSrsId(org.folio.ld.dictionary.model.Resource resource) {
var id = resource.getId();
var srsId = resource.getFolioMetadata().getSrsId();
return resourceRepo.existsById(id) && getIdBySrsId(srsId).filter(id::equals).isPresent();
}

private Optional<Long> getIdBySrsId(String srsId) {
return folioMetadataRepository.findIdBySrsId(srsId)
.map(FolioMetadataRepository.IdOnly::getId);
}

private boolean resourceExistsBySrsId(org.folio.ld.dictionary.model.Resource resource) {
return folioMetadataRepository.existsBySrsId(resource.getFolioMetadata().getSrsId());
}

private Long updateAuthority(Resource resource) {
var id = resource.getId();
var srsId = resource.getFolioMetadata().getSrsId();
Expand Down Expand Up @@ -176,9 +202,6 @@ private void logMarcAction(Resource resource, String existence, String action) {
private Long saveAndPublishEvent(Resource resource, Function<Resource, ResourceEvent> resourceEventSupplier) {
var newResource = resourceGraphService.saveMergingGraph(resource);
var event = resourceEventSupplier.apply(newResource);
if (event instanceof ResourceReplacedEvent rre) {
resourceRepo.save(rre.previous());
}
applicationEventPublisher.publishEvent(event);
return newResource.getId();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.folio.ld.dictionary.PredicateDictionary.REPLACED_BY;
import static org.folio.ld.dictionary.PropertyDictionary.RESOURCE_PREFERRED;
import static org.folio.ld.dictionary.ResourceTypeDictionary.PERSON;
import static org.folio.ld.dictionary.ResourceTypeDictionary.WORK;
import static org.folio.linked.data.test.TestUtil.OBJECT_MAPPER;
import static org.folio.linked.data.test.TestUtil.emptyRequestProcessingException;
import static org.folio.linked.data.test.TestUtil.randomLong;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.folio.linked.data.test.TestUtil.randomString;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
Expand All @@ -20,6 +22,7 @@
import feign.FeignException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import org.folio.ld.dictionary.model.FolioMetadata;
import org.folio.linked.data.client.SrsClient;
Expand Down Expand Up @@ -152,8 +155,8 @@ void fetchAuthorityOrCreateFromSrsRecord_shouldThrowNotFound_ifRecordNotExistsIn
when(exceptionBuilder.notFoundSourceRecordException(any(), any())).thenReturn(emptyRequestProcessingException());

// then
assertThrows(RequestProcessingException.class,
() -> resourceMarcAuthorityService.fetchAuthorityOrCreateFromSrsRecord(agent));
assertThatThrownBy(() -> resourceMarcAuthorityService.fetchAuthorityOrCreateFromSrsRecord(agent))
.isInstanceOf(RequestProcessingException.class);
}

@Test
Expand All @@ -179,7 +182,7 @@ void saveMarcAuthority_shouldCreateNewAuthority_ifGivenModelDoesNotExistsByIdAnd
}

@Test
void saveMarcAuthority_shouldUpdateAuthority_ifGivenModelExistsById() {
void saveMarcAuthority_shouldUpdateAuthority_ifGivenModelExistsByIdAndSrsId() {
// given
var id = randomLong();
var srsId = UUID.randomUUID().toString();
Expand All @@ -189,6 +192,7 @@ void saveMarcAuthority_shouldUpdateAuthority_ifGivenModelExistsById() {
mapped.setFolioMetadata(new org.folio.linked.data.model.entity.FolioMetadata(mapped).setSrsId(srsId));
doReturn(mapped).when(resourceModelMapper).toEntity(model);
doReturn(true).when(resourceRepo).existsById(id);
doReturn(Optional.of((FolioMetadataRepository.IdOnly) () -> id)).when(folioMetadataRepo).findIdBySrsId(srsId);
doReturn(mapped).when(resourceGraphService).saveMergingGraph(mapped);

// when
Expand Down Expand Up @@ -216,7 +220,6 @@ void saveMarcAuthority_shouldCreateNewAuthorityVersionAndMarkOldAsObsolete_ifGiv
mapped.setFolioMetadata(new org.folio.linked.data.model.entity.FolioMetadata(mapped).setSrsId(srsId));
doReturn(mapped).when(resourceModelMapper).toEntity(model);
doReturn(false).when(resourceRepo).existsById(id);
doReturn(existed).when(resourceRepo).save(existed);

doReturn(mapped).when(resourceGraphService).saveMergingGraph(mapped);

Expand All @@ -228,10 +231,36 @@ void saveMarcAuthority_shouldCreateNewAuthorityVersionAndMarkOldAsObsolete_ifGiv
assertThat(existed.isActive()).isFalse();
assertThat(existed.getDoc().get(RESOURCE_PREFERRED.getValue()).get(0).textValue()).isEqualTo("false");
assertThat(existed.getFolioMetadata()).isNull();
verify(resourceRepo).save(existed);
verify(resourceGraphService).saveMergingGraph(mapped);
verify(applicationEventPublisher).publishEvent(new ResourceReplacedEvent(existed, mapped.getId()));
assertThat(mapped.getDoc().get(RESOURCE_PREFERRED.getValue()).get(0).textValue()).isEqualTo("true");
assertThat(mapped.getIncomingEdges()).contains(new ResourceEdge(existed, mapped, REPLACED_BY));
}

@Test
void saveMarcAuthority_shouldThrowException_whenSrsIdIsMissing() {
// given
var model = new org.folio.ld.dictionary.model.Resource();
var personWithoutSrsId = new Resource().setId(randomLong()).addTypes(PERSON);
doReturn(personWithoutSrsId).when(resourceModelMapper).toEntity(model);

// then
assertThatThrownBy(() -> resourceMarcAuthorityService.saveMarcAuthority(model))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("SRS ID is missing in the resource");
}

@Test
void saveMarcAuthority_shouldThrowException_whenResourceIsNotAuthority() {
// given
var model = new org.folio.ld.dictionary.model.Resource();
var work = new Resource().setId(randomLong()).addTypes(WORK);
work.setFolioMetadata(new org.folio.linked.data.model.entity.FolioMetadata(work).setSrsId(randomString()));
doReturn(work).when(resourceModelMapper).toEntity(model);

// then
assertThatThrownBy(() -> resourceMarcAuthorityService.saveMarcAuthority(model))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Resource is not an authority");
}
}

0 comments on commit 0ff6d93

Please sign in to comment.