Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
Add conditions to filter age (#147)
Browse files Browse the repository at this point in the history
* Add condition for age filter and additional tests

* Remove unnecessary log
  • Loading branch information
nhanaa authored Aug 7, 2023
1 parent 023d9a7 commit 06ed21a
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
Expand Down

0 comments on commit 06ed21a

Please sign in to comment.