-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MSEARCH-617 Ignore hard-delete domain authority events
- Loading branch information
1 parent
65c8eec
commit f911c3a
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/org/folio/search/integration/ResourceChangeFilterStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.folio.search.integration; | ||
|
||
import lombok.extern.log4j.Log4j2; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.folio.search.domain.dto.ResourceEvent; | ||
import org.springframework.kafka.listener.adapter.RecordFilterStrategy; | ||
|
||
import static org.folio.search.utils.SearchUtils.AUTHORITY_RESOURCE; | ||
|
||
@Log4j2 | ||
public class ResourceChangeFilterStrategy implements RecordFilterStrategy<String, ResourceEvent> { | ||
|
||
@Override | ||
public boolean filter(ConsumerRecord<String, ResourceEvent> consumerRecord) { | ||
log.info("Processing resource event [id: {}]", consumerRecord.value().getId()); | ||
var resourceEvent = consumerRecord.value(); | ||
var resourceName = resourceEvent.getResourceName(); | ||
|
||
var result = false; | ||
if (resourceName.equals(AUTHORITY_RESOURCE)) { | ||
log.info("Processing authority resource event [id: {}]", resourceEvent.getId()); | ||
result = checkAuthorityEventType(resourceEvent); | ||
} | ||
return result; | ||
} | ||
|
||
private boolean checkAuthorityEventType(ResourceEvent resourceEvent) { | ||
return switch (resourceEvent.getType()) { | ||
case UPDATE -> { | ||
if (resourceHasChanges(resourceEvent)) { | ||
yield false; | ||
} else { | ||
log.info("Skip message. No need to process update event for authority resource"); | ||
yield true; | ||
} | ||
} | ||
case CREATE, REINDEX -> true; | ||
case DELETE, DELETE_ALL -> { | ||
log.info("Skip message. No need to process delete event"); | ||
yield false; | ||
} | ||
}; | ||
} | ||
|
||
private boolean resourceHasChanges(ResourceEvent resourceEvent) { | ||
return resourceEvent.getOld() != null && resourceEvent.getNew() != null | ||
&& !resourceEvent.getOld().equals(resourceEvent.getNew()); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
src/test/java/org/folio/search/integration/ResourceChangeFilterStrategyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package org.folio.search.integration; | ||
|
||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.folio.search.domain.dto.Authority; | ||
import org.folio.search.domain.dto.ResourceEvent; | ||
import org.folio.search.domain.dto.ResourceEventType; | ||
import org.folio.spring.test.type.UnitTest; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import static org.folio.search.utils.SearchUtils.AUTHORITY_RESOURCE; | ||
import static org.folio.search.utils.SearchUtils.CONTRIBUTOR_RESOURCE; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.when; | ||
|
||
@UnitTest | ||
@ExtendWith(MockitoExtension.class) | ||
public class ResourceChangeFilterStrategyTest { | ||
|
||
private final ResourceChangeFilterStrategy filterStrategy = new ResourceChangeFilterStrategy(); | ||
|
||
@Mock | ||
private ConsumerRecord<String, ResourceEvent> consumerRecord; | ||
|
||
@Test | ||
void shouldNotFilterHardDeleteAuthResourceEvent() { | ||
var event = createResourceEvent(ResourceEventType.DELETE_ALL, AUTHORITY_RESOURCE); | ||
mockConsumerRecord(event); | ||
|
||
var actual = filterStrategy.filter(consumerRecord); | ||
|
||
assertFalse(actual); | ||
} | ||
|
||
@Test | ||
void shouldNotFilterNonAuthResourceEvent() { | ||
var event = createResourceEvent(ResourceEventType.DELETE_ALL, CONTRIBUTOR_RESOURCE); | ||
mockConsumerRecord(event); | ||
|
||
var actual = filterStrategy.filter(consumerRecord); | ||
|
||
assertFalse(actual); | ||
} | ||
|
||
@Test | ||
void shouldFilterUpdateAuthEventEvent_whenNewAndOldAreEqual() { | ||
var event = createResourceEvent(ResourceEventType.UPDATE, AUTHORITY_RESOURCE); | ||
event.setNew(new Authority().id("1")); | ||
event.setOld(new Authority().id("1")); | ||
mockConsumerRecord(event); | ||
|
||
var actual = filterStrategy.filter(consumerRecord); | ||
|
||
assertTrue(actual); | ||
} | ||
|
||
@Test | ||
void shouldNotFilterUpdateAuthEventEvent_whenNewAndOldAreNotEqual() { | ||
var event = createResourceEvent(ResourceEventType.UPDATE, AUTHORITY_RESOURCE); | ||
event.setNew(new Authority().id("1")); | ||
event.setOld(new Authority().id("2")); | ||
mockConsumerRecord(event); | ||
|
||
var actual = filterStrategy.filter(consumerRecord); | ||
|
||
assertFalse(actual); | ||
} | ||
|
||
@ValueSource(strings = {"REINDEX", "CREATE"}) | ||
@ParameterizedTest | ||
void shouldFilterAuthEvent_whenTypeIsNotSupported(String type) { | ||
var event = createResourceEvent(ResourceEventType.valueOf(type), AUTHORITY_RESOURCE); | ||
mockConsumerRecord(event); | ||
|
||
var actual = filterStrategy.filter(consumerRecord); | ||
|
||
assertTrue(actual); | ||
} | ||
|
||
private void mockConsumerRecord(ResourceEvent event) { | ||
when(consumerRecord.value()).thenReturn(event); | ||
} | ||
|
||
private ResourceEvent createResourceEvent(ResourceEventType type, String resourceName) { | ||
var event = new ResourceEvent(); | ||
event.setId("1"); | ||
event.setType(type); | ||
event.setResourceName(resourceName); | ||
return event; | ||
} | ||
} |