From 06ed21a16f0a6896e4713643a908ad98da5fe4a4 Mon Sep 17 00:00:00 2001 From: Pax Nguyen Date: Mon, 7 Aug 2023 15:16:56 -0400 Subject: [PATCH] Add conditions to filter age (#147) * Add condition for age filter and additional tests * Remove unnecessary log --- .../service/BursarFilterEvaluator.java | 32 +++++++--- .../service/BursarFilterEvaluatorTest.java | 61 ++++++++++++++++++- 2 files changed, 84 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/folio/dew/batch/bursarfeesfines/service/BursarFilterEvaluator.java b/src/main/java/org/folio/dew/batch/bursarfeesfines/service/BursarFilterEvaluator.java index b74b97b4c..8b4d55368 100644 --- a/src/main/java/org/folio/dew/batch/bursarfeesfines/service/BursarFilterEvaluator.java +++ b/src/main/java/org/folio/dew/batch/bursarfeesfines/service/BursarFilterEvaluator.java @@ -46,13 +46,7 @@ public static boolean evaluate( if (account.getAccount().getDateCreated() == null) { return true; } - return ( - ChronoUnit.DAYS.between( - account.getAccount().getDateCreated().toInstant(), - Instant.now() - ) > - filterAge.getNumDays() - ); + return evaluateFilterAge(account, filterAge); } else if (filter instanceof BursarExportFilterAmount filterAmount) { return evaluateFilterAmount(account, filterAmount); } else if (filter instanceof BursarExportFilterFeeType filterFeeType) { @@ -101,6 +95,30 @@ public static boolean evaluate( } } + private static boolean evaluateFilterAge( + AccountWithAncillaryData account, + BursarExportFilterAge filter + ) { + int numDaysFilter = filter.getNumDays(); + long accountAge = ChronoUnit.DAYS.between( + account.getAccount().getDateCreated().toInstant(), + Instant.now() + ); + + switch (filter.getCondition()) { + case LESS_THAN: + return accountAge < numDaysFilter; + case GREATER_THAN: + return accountAge > numDaysFilter; + case LESS_THAN_EQUAL: + return accountAge <= numDaysFilter; + case GREATER_THAN_EQUAL: + return accountAge >= numDaysFilter; + default: + return false; + } + } + private static boolean evaluateFilterAmount( AccountWithAncillaryData account, BursarExportFilterAmount filter diff --git a/src/test/java/org/folio/dew/batch/bursarfeesfines/service/BursarFilterEvaluatorTest.java b/src/test/java/org/folio/dew/batch/bursarfeesfines/service/BursarFilterEvaluatorTest.java index 0faeb6905..c5b9b29e5 100644 --- a/src/test/java/org/folio/dew/batch/bursarfeesfines/service/BursarFilterEvaluatorTest.java +++ b/src/test/java/org/folio/dew/batch/bursarfeesfines/service/BursarFilterEvaluatorTest.java @@ -73,7 +73,8 @@ void testFilterAccountByAge() { .build(); BursarExportFilterAge filterAge = new BursarExportFilterAge(); - filterAge.setNumDays(1); + filterAge.setNumDays(5); + filterAge.setCondition(BursarExportFilterAge.ConditionEnum.GREATER_THAN); assertThat( BursarFilterEvaluator.evaluate(accountWithAncillaryData, filterAge), @@ -89,15 +90,71 @@ void testFilterAccountByAge() { ); Calendar calendar = Calendar.getInstance(); - calendar.add(Calendar.DAY_OF_MONTH, -2); + calendar.add(Calendar.DAY_OF_MONTH, -10); + account.setDateCreated(calendar.getTime()); + accountWithAncillaryData.setAccount(account); + assertThat( + BursarFilterEvaluator.evaluate(accountWithAncillaryData, filterAge), + is(true) + ); + calendar.add(Calendar.DAY_OF_MONTH, 8); + account.setDateCreated(calendar.getTime()); + accountWithAncillaryData.setAccount(account); + assertThat( + BursarFilterEvaluator.evaluate(accountWithAncillaryData, filterAge), + is(false) + ); + + filterAge.setCondition( + BursarExportFilterAge.ConditionEnum.GREATER_THAN_EQUAL + ); + calendar.add(Calendar.DAY_OF_MONTH, -3); + account.setDateCreated(calendar.getTime()); + accountWithAncillaryData.setAccount(account); + assertThat( + BursarFilterEvaluator.evaluate(accountWithAncillaryData, filterAge), + is(true) + ); + calendar.add(Calendar.DAY_OF_MONTH, 2); + account.setDateCreated(calendar.getTime()); + accountWithAncillaryData.setAccount(account); + assertThat( + BursarFilterEvaluator.evaluate(accountWithAncillaryData, filterAge), + is(false) + ); + calendar.add(Calendar.DAY_OF_MONTH, -2); + account.setDateCreated(calendar.getTime()); + accountWithAncillaryData.setAccount(account); + filterAge.setCondition(BursarExportFilterAge.ConditionEnum.LESS_THAN_EQUAL); + assertThat( + BursarFilterEvaluator.evaluate(accountWithAncillaryData, filterAge), + is(true) + ); + calendar.add(Calendar.DAY_OF_MONTH, -5); account.setDateCreated(calendar.getTime()); accountWithAncillaryData.setAccount(account); + assertThat( + BursarFilterEvaluator.evaluate(accountWithAncillaryData, filterAge), + is(false) + ); + calendar.add(Calendar.DAY_OF_MONTH, 8); + account.setDateCreated(calendar.getTime()); + accountWithAncillaryData.setAccount(account); + filterAge.setCondition(BursarExportFilterAge.ConditionEnum.LESS_THAN); assertThat( BursarFilterEvaluator.evaluate(accountWithAncillaryData, filterAge), is(true) ); + calendar.add(Calendar.DAY_OF_MONTH, -5); + account.setDateCreated(calendar.getTime()); + accountWithAncillaryData.setAccount(account); + filterAge.setCondition(BursarExportFilterAge.ConditionEnum.LESS_THAN); + assertThat( + BursarFilterEvaluator.evaluate(accountWithAncillaryData, filterAge), + is(false) + ); } @Test