-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GRAD2-2920 Scheduled purge of old events and event history (#343)
* Added cascade deletion of old events including history. * Added cron schedule every day at midnight * Added more test coverage. * Added more test coverage. * Updated exception to be thrown from repository layer. --------- Co-authored-by: chris.ditcher <[email protected]>
- Loading branch information
Showing
9 changed files
with
167 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,4 @@ | |
|
||
@Repository | ||
public interface EventHistoryRepository extends JpaRepository<EventHistoryEntity, UUID> { | ||
|
||
|
||
} |
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
31 changes: 31 additions & 0 deletions
31
api/src/main/java/ca/bc/gov/educ/api/trax/service/EventHistoryService.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,31 @@ | ||
package ca.bc.gov.educ.api.trax.service; | ||
|
||
import ca.bc.gov.educ.api.trax.exception.ServiceException; | ||
import ca.bc.gov.educ.api.trax.repository.EventRepository; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Slf4j | ||
@Service | ||
public class EventHistoryService { | ||
|
||
private EventRepository eventRepository; | ||
|
||
@Autowired | ||
public EventHistoryService(EventRepository eventRepository) { | ||
this.eventRepository = eventRepository; | ||
} | ||
|
||
public void purgeOldEventAndEventHistoryRecords(LocalDateTime sinceBefore) throws ServiceException { | ||
try { | ||
this.eventRepository.deleteByCreateDateLessThan(sinceBefore); | ||
} catch (Exception e) { | ||
throw new ServiceException(String.format("Exception encountered when attempting old Event History Purge: %s", e.getMessage())); | ||
} | ||
} | ||
|
||
|
||
} |
32 changes: 32 additions & 0 deletions
32
api/src/test/java/ca/bc/gov/educ/api/trax/service/EventHistoryServiceMockTest.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,32 @@ | ||
package ca.bc.gov.educ.api.trax.service; | ||
|
||
import ca.bc.gov.educ.api.trax.exception.ServiceException; | ||
import ca.bc.gov.educ.api.trax.repository.EventRepository; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.doThrow; | ||
|
||
|
||
class EventHistoryServiceMockTest extends BaseReplicationServiceTest { | ||
|
||
@Autowired | ||
EventHistoryService eventHistoryService; | ||
|
||
@MockBean | ||
EventRepository eventRepository; | ||
|
||
@Test | ||
void purgeOldEventAndEventHistoryRecords_givenExceptionThrown_shouldThrowException() { | ||
final String ERROR_MSG = "Exception encountered"; | ||
final LocalDateTime localDateTime = LocalDateTime.now(); | ||
doThrow(new RuntimeException(ERROR_MSG)).when(eventRepository).deleteByCreateDateLessThan(localDateTime); | ||
Exception exception = assertThrows(ServiceException.class, () -> eventHistoryService.purgeOldEventAndEventHistoryRecords(localDateTime)); | ||
assertTrue(exception.getMessage().contains(ERROR_MSG)); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
api/src/test/java/ca/bc/gov/educ/api/trax/service/EventHistoryServiceTest.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,64 @@ | ||
package ca.bc.gov.educ.api.trax.service; | ||
|
||
import ca.bc.gov.educ.api.trax.model.entity.EventEntity; | ||
import ca.bc.gov.educ.api.trax.model.entity.EventHistoryEntity; | ||
import ca.bc.gov.educ.api.trax.repository.EventHistoryRepository; | ||
import ca.bc.gov.educ.api.trax.repository.EventRepository; | ||
import ca.bc.gov.educ.api.trax.support.TestUtils; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Optional; | ||
|
||
public class EventHistoryServiceTest extends BaseReplicationServiceTest { | ||
@Autowired | ||
private EventHistoryService eventHistoryService; | ||
final LocalDateTime purgeTimeInDays = LocalDateTime.now().minusDays(2); | ||
|
||
@Test | ||
public void testDeleteEvent_giventEventHistory_ShouldCascadeDelete() throws JsonProcessingException { | ||
// set up | ||
EventRepository eventRepository = this.replicationTestUtils.getEventRepository(); | ||
EventHistoryRepository eventHistoryRepository = this.replicationTestUtils.getEventHistoryRepository(); | ||
var event = TestUtils.createEvent("DELETE_DISTRICT_CONTACT", TestUtils.createDistrictContact(), LocalDateTime.now(), eventRepository); | ||
var eventHistory = TestUtils.createEventHistory(event, LocalDateTime.now(), eventHistoryRepository); | ||
eventRepository.deleteById(event.getReplicationEventId()); | ||
Optional<EventEntity> eventThatShouldBePurgedOptional = eventRepository.findById(event.getReplicationEventId()); | ||
Optional<EventHistoryEntity> eventHistoryThatShouldBePurgedAlso = eventHistoryRepository.findById(eventHistory.getId()); | ||
Assert.assertTrue(eventHistoryThatShouldBePurgedAlso.isEmpty() && eventThatShouldBePurgedOptional.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void purgeOldEventAndEventHistoryRecords_givenNoExceptionAndOldRecord_shouldPurgeRecords() throws JsonProcessingException { | ||
// set up | ||
EventRepository eventRepository = this.replicationTestUtils.getEventRepository(); | ||
EventHistoryRepository eventHistoryRepository = this.replicationTestUtils.getEventHistoryRepository(); | ||
var eventAge = LocalDateTime.now().minusDays(3); | ||
var eventThatShouldBePurged = TestUtils.createEvent("DELETE_DISTRICT_CONTACT", TestUtils.createDistrictContact(), eventAge, eventRepository); | ||
// set up event history for eventThatShouldBePurged | ||
var eventHistory = TestUtils.createEventHistory(eventThatShouldBePurged, eventAge, eventHistoryRepository); | ||
// call purge | ||
eventHistoryService.purgeOldEventAndEventHistoryRecords(purgeTimeInDays); | ||
// check repo and ensure that older record purged | ||
Optional<EventEntity> eventThatShouldBePurgedOptional = eventRepository.findById(eventThatShouldBePurged.getReplicationEventId()); | ||
Optional<EventHistoryEntity> eventHistoryThatShouldBePurgedAlso = eventHistoryRepository.findById(eventHistory.getId()); | ||
Assert.assertTrue(eventHistoryThatShouldBePurgedAlso.isEmpty() && eventThatShouldBePurgedOptional.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void purgeOldEventAndEventHistoryRecords_givenNoExceptionAndNewRecord_shouldNotPurgeRecords() throws JsonProcessingException { | ||
EventRepository eventRepository = this.replicationTestUtils.getEventRepository(); | ||
EventHistoryRepository eventHistoryRepository = this.replicationTestUtils.getEventHistoryRepository(); | ||
var eventThatShouldNotBePurged = TestUtils.createEvent("DELETE_DISTRICT_CONTACT", TestUtils.createDistrictContact(), eventRepository); | ||
var eventHistory = TestUtils.createEventHistory(eventThatShouldNotBePurged, LocalDateTime.now(), eventHistoryRepository); | ||
// call purge | ||
eventHistoryService.purgeOldEventAndEventHistoryRecords(purgeTimeInDays); | ||
// check repo and ensure that new record is not purged | ||
Optional<EventEntity> eventThatShouldNotBePurgedOptional = eventRepository.findById(eventThatShouldNotBePurged.getReplicationEventId()); | ||
Optional<EventHistoryEntity> eventHistoryThatShouldNotBePurgedAlso = eventHistoryRepository.findById(eventHistory.getId()); | ||
Assert.assertTrue(eventHistoryThatShouldNotBePurgedAlso.isPresent() && eventThatShouldNotBePurgedOptional.isPresent()); | ||
} | ||
} |
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