PNCVisitHappenedBetween2And14DaysOfDeliveryRule 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 && numberOfDays <= 14; }  @Override boolean apply(SafeMap reportFields);  }
@Test public void shouldReturnTrueWhenPNCVisitHappenedBetween2And14DaysOfDelivery() { Map<String, String> reportFields = create("pncVisitDate", "2013-01-04") .put("referenceDate", "2013-01-01") .map(); boolean didRuleSucceed = rule.apply(new SafeMap(reportFields)); assertTrue(didRuleSucceed); reportFields = create("pncVisitDate", "2013-01-15") .put("referenceDate", "2013-01-01") .map(); didRuleSucceed = rule.apply(new SafeMap(reportFields)); assertTrue(didRuleSucceed); reportFields = create("pncVisitDate", "2013-01-12") .put("referenceDate", "2013-01-01") .map(); didRuleSucceed = rule.apply(new SafeMap(reportFields)); assertTrue(didRuleSucceed); }
@Test public void shouldReturnFalseWhenPNCVisitNotHappenedBetween2And14DaysOfDelivery() { Map<String, String> reportFields = create("pncVisitDate", "2013-01-03") .put("referenceDate", "2013-01-01") .map(); boolean didRuleSucceed = rule.apply(new SafeMap(reportFields)); assertFalse(didRuleSucceed); reportFields = create("pncVisitDate", "2013-01-02") .put("referenceDate", "2013-01-01") .map(); didRuleSucceed = rule.apply(new SafeMap(reportFields)); assertFalse(didRuleSucceed); reportFields = create("pncVisitDate", "2013-01-16") .put("referenceDate", "2013-01-01") .map(); didRuleSucceed = rule.apply(new SafeMap(reportFields)); assertFalse(didRuleSucceed); }