PNCVisitHappenedWithin2DaysOfDeliveryRule implements IRule { @Override public boolean apply(SafeMap reportFields) { long NUMBER_OF_MILLISECONDS_IN_A_DAY = 86400000; Date pncVisitDate = LocalDate.parse(reportFields.get(VISIT_DATE_FIELD_NAME)).toDate(); Date deliveryDate = LocalDate.parse(reportFields.get(REFERENCE_DATE)).toDate(); long numberOfDays = (long) Math.floor((pncVisitDate.getTime() - deliveryDate.getTime()) / NUMBER_OF_MILLISECONDS_IN_A_DAY); return numberOfDays <= 2; }  @Override boolean apply(SafeMap reportFields);  }
@Test public void shouldReturnTrueWhenPNCVisitHappenedWithin2DaysOfDelivery() { Map<String, String> reportFields = create("pncVisitDate", "2013-09-20") .put("referenceDate", "2013-09-20") .map(); boolean didRuleApplied = rule.apply(new SafeMap(reportFields)); assertTrue(didRuleApplied); reportFields = create("pncVisitDate", "2013-09-21") .put("referenceDate", "2013-09-20") .map(); didRuleApplied = rule.apply(new SafeMap(reportFields)); assertTrue(didRuleApplied); reportFields = create("pncVisitDate", "2013-09-22") .put("referenceDate", "2013-09-20") .map(); didRuleApplied = rule.apply(new SafeMap(reportFields)); assertTrue(didRuleApplied); }
@Test public void shouldReturnFalseWhenPNCVisitHappenedAfter2DaysOfDelivery() { Map<String, String> reportFields = create("pncVisitDate", "2013-09-23") .put("referenceDate", "2013-09-20") .map(); boolean didRuleApplied = rule.apply(new SafeMap(reportFields)); assertFalse(didRuleApplied); }