Skip to content

Commit

Permalink
Update the properties in ResourceReplacedEvent record
Browse files Browse the repository at this point in the history
  • Loading branch information
pkjacob committed Jan 17, 2025
1 parent e6a0545 commit bde4dd8
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void afterUpdate(ResourceUpdatedEvent resourceUpdatedEvent) {
@TransactionalEventListener
public void afterReplace(ResourceReplacedEvent resourceReplacedEvent) {
log.info("ResourceReplacedEvent received [{}]", resourceReplacedEvent);
resourceRepository.findById(resourceReplacedEvent.current().getId())
resourceRepository.findById(resourceReplacedEvent.currentResourceId())
.ifPresent(
resource -> replaceMessageSenders.forEach(sender -> sender.produce(resourceReplacedEvent.previous(), resource))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

import org.folio.linked.data.model.entity.Resource;

public record ResourceReplacedEvent(Resource previous, Resource current) implements ResourceEvent {
public record ResourceReplacedEvent(Resource previous, Long currentResourceId) implements ResourceEvent {
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public ResourceResponseDto updateResource(Long id, ResourceRequestDto resourceDt
if (Objects.equals(oldResource.getId(), newResource.getId())) {
applicationEventPublisher.publishEvent(new ResourceUpdatedEvent(newResource));
} else {
applicationEventPublisher.publishEvent(new ResourceReplacedEvent(oldResource, newResource));
applicationEventPublisher.publishEvent(new ResourceReplacedEvent(oldResource, newResource.getId()));
}
return resourceDtoMapper.toDto(newResource);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private Long replaceAuthority(Resource resource) {
resource.addIncomingEdge(re);
logMarcAction(resource, "not found by id, but found by srsId [" + srsId + "]",
"be saved as a new version of previously existed resource [id " + previous.getId() + "]");
return saveAndPublishEvent(resource, saved -> new ResourceReplacedEvent(previousObsolete, saved));
return saveAndPublishEvent(resource, saved -> new ResourceReplacedEvent(previousObsolete, saved.getId()));
})
.orElseThrow(() -> notFoundException(srsId));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ void clenUp() {
void merge_two_works_should_send_correct_events_to_inventory_and_search() throws Exception {
// given
// Create two works with 2 different titles
String work1Title = "simple_work1";
String work2Title = "simple_work2";
var work1Title = "simple_work1";
var work2Title = "simple_work2";
var work1 = getWork(work1Title);
var work2 = getWork(work2Title);
var work1Instance = getInstance(work1);
Expand Down Expand Up @@ -160,7 +160,7 @@ private String getWorkRequestDto(Long instanceId, String title) {
}

private Resource getWork(String titleStr) {
String titleDoc = """
var titleDoc = """
{
"http://bibfra.me/vocab/marc/mainTitle": ["%TITLE%"]
}
Expand Down Expand Up @@ -235,7 +235,6 @@ private boolean isExpectedSearchEvents(Set<ResourceIndexEvent> events, long dele

private boolean isSearchCreateEvent(ResourceIndexEvent event, Long createdWorkId) {
var work = getWorkFromEvent(event);
;
return event.getType().equals(ResourceIndexEventType.CREATE)
&& work.getId().equals(createdWorkId.toString())
&& work.getInstances().size() == 2
Expand All @@ -251,18 +250,18 @@ private boolean isSearchDeleteEvent(ResourceIndexEvent event, Long deletedWorkId
}

private LinkedDataWork getWorkFromEvent(ResourceIndexEvent event) {
Map<String, Object> data = (Map<String, Object>) event.getNew();
var data = (Map<String, Object>) event.getNew();
return objectMapper.convertValue(data, LinkedDataWork.class);
}

private boolean isExpectedInventoryEvents(Set<InstanceIngressEvent> inventoryEventList) {
return inventoryEventList.size() == 2 && inventoryEventList.stream()
.allMatch(
e -> {
var payload = e.getEventPayload();
event -> {
var payload = event.getEventPayload();
var marcStr = payload.getSourceRecordObject();
var marcRecord = marcReader.readMarc(marcStr).toList().get(0);
return e.getEventType().equals(InstanceIngressEvent.EventTypeEnum.UPDATE_INSTANCE)
return event.getEventType().equals(InstanceIngressEvent.EventTypeEnum.UPDATE_INSTANCE)
&& isExpectedMarc(marcRecord);
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ void update_shouldSaveUpdatedResourceAndSendReplaceEvent_forResourceWithDifferen
assertThat(expectedDto).isEqualTo(result);
verify(resourceGraphService).breakEdgesAndDelete(oldInstance);
verify(resourceGraphService).saveMergingGraph(mapped);
verify(applicationEventPublisher).publishEvent(new ResourceReplacedEvent(oldInstance, mapped));
verify(applicationEventPublisher).publishEvent(new ResourceReplacedEvent(oldInstance, mapped.getId()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ void saveMarcAuthority_shouldCreateNewAuthorityVersionAndMarkOldAsObsolete_ifGiv
assertThat(existed.getFolioMetadata()).isNull();
verify(resourceRepo).save(existed);
verify(resourceGraphService).saveMergingGraph(mapped);
verify(applicationEventPublisher).publishEvent(new ResourceReplacedEvent(existed, 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));
}
Expand Down

0 comments on commit bde4dd8

Please sign in to comment.