-
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.
Merge branch 'master' into MSEARCH-635-Update-module-descriptor
- Loading branch information
Showing
6 changed files
with
136 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
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
28 changes: 28 additions & 0 deletions
28
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,28 @@ | ||
package org.folio.search.integration; | ||
|
||
import static org.folio.search.utils.SearchUtils.AUTHORITY_RESOURCE; | ||
|
||
import lombok.extern.log4j.Log4j2; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.folio.search.domain.dto.ResourceDeleteEventSubType; | ||
import org.folio.search.domain.dto.ResourceEvent; | ||
import org.folio.search.domain.dto.ResourceEventType; | ||
import org.springframework.kafka.listener.adapter.RecordFilterStrategy; | ||
|
||
@Log4j2 | ||
public class ResourceChangeFilterStrategy implements RecordFilterStrategy<String, ResourceEvent> { | ||
|
||
@Override | ||
public boolean filter(ConsumerRecord<String, ResourceEvent> consumerRecord) { | ||
log.debug("Processing resource event [id: {}]", consumerRecord.value().getId()); | ||
var resourceEvent = consumerRecord.value(); | ||
var resourceName = resourceEvent.getResourceName(); | ||
var resourceEventType = resourceEvent.getType(); | ||
if (ResourceEventType.DELETE == resourceEventType && AUTHORITY_RESOURCE.equals(resourceName) | ||
&& ResourceDeleteEventSubType.HARD_DELETE == resourceEvent.getDeleteEventSubType()) { | ||
log.debug("Skip hard-delete event. No need to process event for authority resource"); | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/resources/swagger.api/schemas/resourceDeleteEventSubType.json
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,6 @@ | ||
{ | ||
"type": "string", | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"description": "Resource event delete operation type - one of [soft-delete, hard-delete]", | ||
"enum": [ "SOFT_DELETE", "HARD_DELETE"] | ||
} |
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
95 changes: 95 additions & 0 deletions
95
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,95 @@ | ||
package org.folio.search.integration; | ||
|
||
import static org.folio.search.utils.SearchUtils.AUTHORITY_RESOURCE; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.folio.search.domain.dto.ResourceDeleteEventSubType; | ||
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; | ||
|
||
@UnitTest | ||
@ExtendWith(MockitoExtension.class) | ||
class ResourceChangeFilterStrategyTest { | ||
|
||
private final ResourceChangeFilterStrategy filterStrategy = new ResourceChangeFilterStrategy(); | ||
|
||
@Mock | ||
private ConsumerRecord<String, ResourceEvent> consumerRecord; | ||
|
||
@Test | ||
void shouldNotFilterResourceEventWithoutName() { | ||
var event = createResourceEvent(ResourceEventType.DELETE, ResourceDeleteEventSubType.HARD_DELETE, null); | ||
mockConsumerRecord(event); | ||
|
||
var actual = filterStrategy.filter(consumerRecord); | ||
|
||
assertFalse(actual); | ||
} | ||
|
||
@ValueSource(strings = {"instance", "contributor", "instance_subject"}) | ||
@ParameterizedTest | ||
void shouldNotFilterNonAuthResourceEvent(String resourceName) { | ||
var event = createResourceEvent(ResourceEventType.DELETE, ResourceDeleteEventSubType.HARD_DELETE, resourceName); | ||
mockConsumerRecord(event); | ||
|
||
var actual = filterStrategy.filter(consumerRecord); | ||
|
||
assertFalse(actual); | ||
} | ||
|
||
@Test | ||
void shouldFilterHardDeleteAuthResourceEvent() { | ||
var event = createResourceEvent(ResourceEventType.DELETE, ResourceDeleteEventSubType.HARD_DELETE, | ||
AUTHORITY_RESOURCE); | ||
mockConsumerRecord(event); | ||
var actual = filterStrategy.filter(consumerRecord); | ||
|
||
assertTrue(actual); | ||
} | ||
|
||
@Test | ||
void shouldNotFilterSoftDeleteAuthResourceEvent() { | ||
var event = createResourceEvent(ResourceEventType.DELETE, ResourceDeleteEventSubType.SOFT_DELETE, | ||
AUTHORITY_RESOURCE); | ||
mockConsumerRecord(event); | ||
|
||
var actual = filterStrategy.filter(consumerRecord); | ||
|
||
assertFalse(actual); | ||
} | ||
|
||
@Test | ||
void shouldNotFilterNonDeleteAuthResourceEvent() { | ||
var event = createResourceEvent(ResourceEventType.CREATE, ResourceDeleteEventSubType.HARD_DELETE, | ||
AUTHORITY_RESOURCE); | ||
mockConsumerRecord(event); | ||
|
||
var actual = filterStrategy.filter(consumerRecord); | ||
|
||
assertFalse(actual); | ||
} | ||
|
||
private void mockConsumerRecord(ResourceEvent event) { | ||
when(consumerRecord.value()).thenReturn(event); | ||
} | ||
|
||
private ResourceEvent createResourceEvent(ResourceEventType eventType, ResourceDeleteEventSubType subType, | ||
String resourceName) { | ||
var event = new ResourceEvent(); | ||
event.setId("1"); | ||
event.setType(eventType); | ||
event.setDeleteEventSubType(subType); | ||
event.setResourceName(resourceName); | ||
return event; | ||
} | ||
} |