@Test public void testConstraintTest() throws Exception { Date date = Date.from(Instant.from(DateTimeFormatter.ISO_DATE_TIME.parse("2014-02-13T13:42:45+00:00"))); HashMap<String, Boolean> testCases = new HashMap<>(); testCases.put("", true); testCases.put("isAny", true); testCases.put("isBefore:2/13/2014", false); testCases.put("isBefore:2/14/2014", true); testCases.put("isAfter:2/13/2014", false); testCases.put("isAfter:2/12/2014", true); testCases.put("isOn:2/13/2014", true); testCases.put("isOn:2/12/2014", false); testCases.put("isOn:2/13/2014", true); testCases.put("isOn:2/12/2014", false); testCases.put("isOn:2/2/2014", false); testCases.put("isOn:02/02/2014", false); testCases.put("isOn:02/02/2014\t+0000", false); testCases.put("isBetween:2/5/2013:12/23/2014", true); testCases.put("isBetween:2/5/2013:12/23/2013", false); testCases.put("isBetween:02/13/2014:02/15/2014", true); testCases.put("isBetween:2/01/2013:02/13/2014", true); testCases.put("isBetween:2/5/2013:12/12/2013", false); for (Map.Entry<String, Boolean> testCase : testCases.entrySet()) { Assert.assertEquals("Test case " + testCase.getKey() + " failed.", testCase.getValue(), ExperimentFilter.constraintTest(date, testCase.getKey())); } List<String> throwCases = new ArrayList<>(); throwCases.add("isBefore:"); throwCases.add("isAfter:"); throwCases.add(":12/5/2013"); throwCases.add("isBefore:13/5/2013"); throwCases.add("isBefore:11/3/13"); throwCases.add("isBetween:11/3/2013:12/3/14"); throwCases.add("isBetween:11/3/2013:"); throwCases.add("isBetween:11/3/2013"); throwCases.add("isOn:13/12/2013"); throwCases.add("isSomeday:5/21/2011"); for (String throwCase : throwCases) { try { ExperimentFilter.constraintTest(date, throwCase); Assert.fail(throwCase + " passed, but should have thrown."); } catch (PaginationException ignored) { } catch (Exception exception) { Assert.fail(throwCase + " threw the wrong exception: " + exception); } } }
static boolean constraintTest(Date experimentDate, String filter) { String[] extracted = FilterUtil.extractTimeZone(filter); String originalFilter = extracted[0]; String timeZoneOffset = "+0000"; try { timeZoneOffset = extracted[1]; } catch (ArrayIndexOutOfBoundsException ignored) { } String[] structuredFilter = originalFilter.split(":"); if (structuredFilter.length < 2) { if (StringUtils.isBlank(structuredFilter[0]) || structuredFilter[0].equalsIgnoreCase("isAny")) { return true; } throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format for constraint date ( " + filter + " ), " + "use: is[any|on|before|after|between]:MM/dd/yyyy[:MM/dd/yyyy]"); } if (StringUtils.isBlank(structuredFilter[0])) { throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format for constraint date ( " + filter + " ), " + "needs one of is[any|on|before|after|between] before the colon."); } LocalDate experimentLocalDate = FilterUtil.convertDateToOffsetDateTime(experimentDate).toLocalDate(); if (structuredFilter[0].equalsIgnoreCase("isBetween")) { try { LocalDate beforeExperimentDate = FilterUtil.parseUIDate(structuredFilter[1], timeZoneOffset) .minusDays(1).toLocalDate(); LocalDate afterExperimentDate = FilterUtil.parseUIDate(structuredFilter[2], timeZoneOffset) .plusDays(1).toLocalDate(); return experimentLocalDate.isAfter(beforeExperimentDate) && experimentLocalDate.isBefore(afterExperimentDate); } catch (ArrayIndexOutOfBoundsException aioobe) { throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format for isBetween ( " + filter + " ), " + "use: isbetween:MM/dd/yyyy:MM/dd/yyyy .", aioobe); } } LocalDate filterDate = FilterUtil.parseUIDate(structuredFilter[1], timeZoneOffset).toLocalDate(); switch (structuredFilter[0].toLowerCase()) { case "isbefore": return experimentLocalDate.isBefore(filterDate); case "isafter": return experimentLocalDate.isAfter(filterDate); case "ison": return experimentLocalDate.isEqual(filterDate); } throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format: not processable filter format ( " + filter + " )."); }
ExperimentFilter extends PaginationFilter<Experiment> { static boolean constraintTest(Date experimentDate, String filter) { String[] extracted = FilterUtil.extractTimeZone(filter); String originalFilter = extracted[0]; String timeZoneOffset = "+0000"; try { timeZoneOffset = extracted[1]; } catch (ArrayIndexOutOfBoundsException ignored) { } String[] structuredFilter = originalFilter.split(":"); if (structuredFilter.length < 2) { if (StringUtils.isBlank(structuredFilter[0]) || structuredFilter[0].equalsIgnoreCase("isAny")) { return true; } throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format for constraint date ( " + filter + " ), " + "use: is[any|on|before|after|between]:MM/dd/yyyy[:MM/dd/yyyy]"); } if (StringUtils.isBlank(structuredFilter[0])) { throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format for constraint date ( " + filter + " ), " + "needs one of is[any|on|before|after|between] before the colon."); } LocalDate experimentLocalDate = FilterUtil.convertDateToOffsetDateTime(experimentDate).toLocalDate(); if (structuredFilter[0].equalsIgnoreCase("isBetween")) { try { LocalDate beforeExperimentDate = FilterUtil.parseUIDate(structuredFilter[1], timeZoneOffset) .minusDays(1).toLocalDate(); LocalDate afterExperimentDate = FilterUtil.parseUIDate(structuredFilter[2], timeZoneOffset) .plusDays(1).toLocalDate(); return experimentLocalDate.isAfter(beforeExperimentDate) && experimentLocalDate.isBefore(afterExperimentDate); } catch (ArrayIndexOutOfBoundsException aioobe) { throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format for isBetween ( " + filter + " ), " + "use: isbetween:MM/dd/yyyy:MM/dd/yyyy .", aioobe); } } LocalDate filterDate = FilterUtil.parseUIDate(structuredFilter[1], timeZoneOffset).toLocalDate(); switch (structuredFilter[0].toLowerCase()) { case "isbefore": return experimentLocalDate.isBefore(filterDate); case "isafter": return experimentLocalDate.isAfter(filterDate); case "ison": return experimentLocalDate.isEqual(filterDate); } throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format: not processable filter format ( " + filter + " )."); } }
ExperimentFilter extends PaginationFilter<Experiment> { static boolean constraintTest(Date experimentDate, String filter) { String[] extracted = FilterUtil.extractTimeZone(filter); String originalFilter = extracted[0]; String timeZoneOffset = "+0000"; try { timeZoneOffset = extracted[1]; } catch (ArrayIndexOutOfBoundsException ignored) { } String[] structuredFilter = originalFilter.split(":"); if (structuredFilter.length < 2) { if (StringUtils.isBlank(structuredFilter[0]) || structuredFilter[0].equalsIgnoreCase("isAny")) { return true; } throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format for constraint date ( " + filter + " ), " + "use: is[any|on|before|after|between]:MM/dd/yyyy[:MM/dd/yyyy]"); } if (StringUtils.isBlank(structuredFilter[0])) { throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format for constraint date ( " + filter + " ), " + "needs one of is[any|on|before|after|between] before the colon."); } LocalDate experimentLocalDate = FilterUtil.convertDateToOffsetDateTime(experimentDate).toLocalDate(); if (structuredFilter[0].equalsIgnoreCase("isBetween")) { try { LocalDate beforeExperimentDate = FilterUtil.parseUIDate(structuredFilter[1], timeZoneOffset) .minusDays(1).toLocalDate(); LocalDate afterExperimentDate = FilterUtil.parseUIDate(structuredFilter[2], timeZoneOffset) .plusDays(1).toLocalDate(); return experimentLocalDate.isAfter(beforeExperimentDate) && experimentLocalDate.isBefore(afterExperimentDate); } catch (ArrayIndexOutOfBoundsException aioobe) { throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format for isBetween ( " + filter + " ), " + "use: isbetween:MM/dd/yyyy:MM/dd/yyyy .", aioobe); } } LocalDate filterDate = FilterUtil.parseUIDate(structuredFilter[1], timeZoneOffset).toLocalDate(); switch (structuredFilter[0].toLowerCase()) { case "isbefore": return experimentLocalDate.isBefore(filterDate); case "isafter": return experimentLocalDate.isAfter(filterDate); case "ison": return experimentLocalDate.isEqual(filterDate); } throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format: not processable filter format ( " + filter + " )."); } ExperimentFilter(); }
ExperimentFilter extends PaginationFilter<Experiment> { static boolean constraintTest(Date experimentDate, String filter) { String[] extracted = FilterUtil.extractTimeZone(filter); String originalFilter = extracted[0]; String timeZoneOffset = "+0000"; try { timeZoneOffset = extracted[1]; } catch (ArrayIndexOutOfBoundsException ignored) { } String[] structuredFilter = originalFilter.split(":"); if (structuredFilter.length < 2) { if (StringUtils.isBlank(structuredFilter[0]) || structuredFilter[0].equalsIgnoreCase("isAny")) { return true; } throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format for constraint date ( " + filter + " ), " + "use: is[any|on|before|after|between]:MM/dd/yyyy[:MM/dd/yyyy]"); } if (StringUtils.isBlank(structuredFilter[0])) { throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format for constraint date ( " + filter + " ), " + "needs one of is[any|on|before|after|between] before the colon."); } LocalDate experimentLocalDate = FilterUtil.convertDateToOffsetDateTime(experimentDate).toLocalDate(); if (structuredFilter[0].equalsIgnoreCase("isBetween")) { try { LocalDate beforeExperimentDate = FilterUtil.parseUIDate(structuredFilter[1], timeZoneOffset) .minusDays(1).toLocalDate(); LocalDate afterExperimentDate = FilterUtil.parseUIDate(structuredFilter[2], timeZoneOffset) .plusDays(1).toLocalDate(); return experimentLocalDate.isAfter(beforeExperimentDate) && experimentLocalDate.isBefore(afterExperimentDate); } catch (ArrayIndexOutOfBoundsException aioobe) { throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format for isBetween ( " + filter + " ), " + "use: isbetween:MM/dd/yyyy:MM/dd/yyyy .", aioobe); } } LocalDate filterDate = FilterUtil.parseUIDate(structuredFilter[1], timeZoneOffset).toLocalDate(); switch (structuredFilter[0].toLowerCase()) { case "isbefore": return experimentLocalDate.isBefore(filterDate); case "isafter": return experimentLocalDate.isAfter(filterDate); case "ison": return experimentLocalDate.isEqual(filterDate); } throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format: not processable filter format ( " + filter + " )."); } ExperimentFilter(); @Override boolean test(Experiment experiment); }
ExperimentFilter extends PaginationFilter<Experiment> { static boolean constraintTest(Date experimentDate, String filter) { String[] extracted = FilterUtil.extractTimeZone(filter); String originalFilter = extracted[0]; String timeZoneOffset = "+0000"; try { timeZoneOffset = extracted[1]; } catch (ArrayIndexOutOfBoundsException ignored) { } String[] structuredFilter = originalFilter.split(":"); if (structuredFilter.length < 2) { if (StringUtils.isBlank(structuredFilter[0]) || structuredFilter[0].equalsIgnoreCase("isAny")) { return true; } throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format for constraint date ( " + filter + " ), " + "use: is[any|on|before|after|between]:MM/dd/yyyy[:MM/dd/yyyy]"); } if (StringUtils.isBlank(structuredFilter[0])) { throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format for constraint date ( " + filter + " ), " + "needs one of is[any|on|before|after|between] before the colon."); } LocalDate experimentLocalDate = FilterUtil.convertDateToOffsetDateTime(experimentDate).toLocalDate(); if (structuredFilter[0].equalsIgnoreCase("isBetween")) { try { LocalDate beforeExperimentDate = FilterUtil.parseUIDate(structuredFilter[1], timeZoneOffset) .minusDays(1).toLocalDate(); LocalDate afterExperimentDate = FilterUtil.parseUIDate(structuredFilter[2], timeZoneOffset) .plusDays(1).toLocalDate(); return experimentLocalDate.isAfter(beforeExperimentDate) && experimentLocalDate.isBefore(afterExperimentDate); } catch (ArrayIndexOutOfBoundsException aioobe) { throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format for isBetween ( " + filter + " ), " + "use: isbetween:MM/dd/yyyy:MM/dd/yyyy .", aioobe); } } LocalDate filterDate = FilterUtil.parseUIDate(structuredFilter[1], timeZoneOffset).toLocalDate(); switch (structuredFilter[0].toLowerCase()) { case "isbefore": return experimentLocalDate.isBefore(filterDate); case "isafter": return experimentLocalDate.isAfter(filterDate); case "ison": return experimentLocalDate.isEqual(filterDate); } throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format: not processable filter format ( " + filter + " )."); } ExperimentFilter(); @Override boolean test(Experiment experiment);  }