@Test public void testValidator() { SQLRequest sqlRequest = new SQLRequest(); sqlRequest.setEndTime("2015-09-9 23:59:59"); sqlRequest.setGranularity("day"); sqlRequest.setIntervals("2015-08-20 00:00:00/2015-08-26 23:59:59"); sqlRequest.setNamespace(Constants.RequestNameSpace.sql); sqlRequest .setSql("select count(clickcount_ag) as \"clickcount_ag\", browserfamily from pulsar_ogmb group by limit 100"); sqlRequest.setStartTime("2015-09-15 23:59:59"); SQLRequest sqlRequest2 = new SQLRequest(); sqlRequest2.setGranularity("day"); sqlRequest2.setNamespace(Constants.RequestNameSpace.sql); sqlRequest2 .setSql("select count(clickcount_ag) as \"clickcount_ag\", browserfamily from pulsar_ogmb group by limit 100"); sqlRequest2.setCustomTime("today"); SQLQueryValidator validator = new SQLQueryValidator(); try { validator.validate(sqlRequest); } catch (InvalidQueryParameterException ex) { assertTrue(false); } try { validator.validate(sqlRequest2); fail("Expect InvalidQueryParameterException"); } catch (InvalidQueryParameterException ex) { assertTrue(true); } SQLRequest sqlRequest3 = new SQLRequest(); sqlRequest3.setGranularity("day"); sqlRequest3.setNamespace(Constants.RequestNameSpace.sql); sqlRequest3 .setSql("select count(clickcount_ag) as \"clickcount_ag\", browserfamily from pulsar_ogmb group by limit 100"); sqlRequest3.setCustomTime("yesterday"); try { validator.validate(sqlRequest3); fail("Expect InvalidQueryParameterException"); } catch (InvalidQueryParameterException ex) { assertTrue(true); } SQLRequest sqlRequest5 = new SQLRequest(); sqlRequest5.setGranularity("day"); sqlRequest5.setStartTime("test"); sqlRequest5.setNamespace(Constants.RequestNameSpace.sql); sqlRequest5 .setSql("select count(clickcount_ag) as \"clickcount_ag\", browserfamily from pulsar_ogmb group by limit 100"); sqlRequest5.setCustomTime("test"); try { validator.validate(sqlRequest5); fail("Expect InvalidQueryParameterException"); } catch (InvalidQueryParameterException ex) { assertTrue(true); } SQLRequest sqlRequest6 = new SQLRequest(); sqlRequest6.setEndTime("2015-09-9 23:59:59"); sqlRequest6.setGranularity("day"); sqlRequest6.setIntervals("/2015-08-26 23:59:59"); sqlRequest6.setNamespace(Constants.RequestNameSpace.sql); sqlRequest6 .setSql("select count(clickcount_ag) as \"clickcount_ag\", browserfamily from pulsar_ogmb group by limit 100"); sqlRequest.setStartTime(null); try { validator.validate(sqlRequest6); fail("Expect InvalidQueryParameterException"); } catch (InvalidQueryParameterException ex) { assertTrue(true); } SQLRequest sqlRequest7 = new SQLRequest(); sqlRequest7.setEndTime(null); sqlRequest7.setGranularity("day"); sqlRequest7.setIntervals("2015-08-20 00:00:00"); sqlRequest7.setNamespace(Constants.RequestNameSpace.sql); sqlRequest7 .setSql("select count(clickcount_ag) as \"clickcount_ag\", browserfamily from pulsar_ogmb group by limit 100"); sqlRequest.setStartTime("2015-09-9 23:59:59"); try { validator.validate(sqlRequest7); fail("Expect InvalidQueryParameterException"); } catch (InvalidQueryParameterException ex) { assertTrue(true); } SQLRequest sqlRequest8 = new SQLRequest(); sqlRequest8.setEndTime(null); sqlRequest8.setGranularity("day"); sqlRequest8.setIntervals("2015-08-20 00:00:00/2015-08-10 00:00:00"); sqlRequest8.setNamespace(Constants.RequestNameSpace.sql); sqlRequest8 .setSql("select count(clickcount_ag) as \"clickcount_ag\", browserfamily from pulsar_ogmb group by limit 100"); sqlRequest.setStartTime("2015-09-9 23:59:59"); try { validator.validate(sqlRequest8); fail("Expect InvalidQueryParameterException"); } catch (InvalidQueryParameterException ex) { assertTrue(true); } SQLRequest sqlRequest9 = new SQLRequest(); sqlRequest9.setEndTime(null); sqlRequest9.setGranularity("day"); sqlRequest9.setIntervals("3015-08-20 00:00:00/3015-08-30 00:00:00"); sqlRequest9.setNamespace(Constants.RequestNameSpace.sql); sqlRequest9 .setSql("select count(clickcount_ag) as \"clickcount_ag\", browserfamily from pulsar_ogmb group by limit 100"); sqlRequest.setStartTime("2015-09-9 23:59:59"); try { validator.validate(sqlRequest9); fail("Expect InvalidQueryParameterException"); } catch (InvalidQueryParameterException ex) { assertTrue(true); } SQLRequest sqlRequest10 = new SQLRequest(); sqlRequest10.setEndTime(null); sqlRequest10.setGranularity("day"); sqlRequest10.setIntervals("3015-08-20 00:00:00/3015-08-30 00:00:00"); sqlRequest10.setNamespace(Constants.RequestNameSpace.sql); sqlRequest.setStartTime("2015-09-9 23:59:59"); try { validator.validate(sqlRequest10); fail("Expect InvalidQueryParameterException"); } catch (InvalidQueryParameterException ex) { assertTrue(true); } }
@Override public void validate(BaseSQLRequest req) { if (Strings.isNullOrEmpty(req.getSql())) { throw new InvalidQueryParameterException(ExceptionErrorCode.MISSING_SQL.getErrorMessage()); } SQLRequest sqlReq = (SQLRequest) req; DateTime start = null; DateTime end = null; String intervalStr = sqlReq.getIntervals(); String customTime = sqlReq.getCustomTime(); String startTime = sqlReq.getStartTime(); String endTime = sqlReq.getEndTime(); if (customTime != null) { if (startTime == null && endTime == null && intervalStr == null) { if (customTime.equals("today")) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_CUSTOM_TIME.getErrorMessage()+ customTime); } else if (customTime.equals("yesterday")) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_CUSTOM_TIME.getErrorMessage()+ customTime); } } else { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage()); } } else { if (intervalStr != null) { String[] strArr = intervalStr.split("/"); if (strArr.length != 2) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage()); } else { startTime = strArr[0]; endTime = strArr[1]; } } try { start = PulsarDateTimeFormatter.INPUTTIME_FORMATTER.parseDateTime(startTime); end = PulsarDateTimeFormatter.INPUTTIME_FORMATTER.parseDateTime(endTime); } catch (Exception e) { } if(start == null) throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage()); if(end == null) throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage()); if (start.compareTo(end) >= 0) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage() + start + "/" + end); } DateTime now = new DateTime(); if (start.compareTo(now) >= 0) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage() + start); } } }
SQLQueryValidator implements QueryValidator<BaseSQLRequest> { @Override public void validate(BaseSQLRequest req) { if (Strings.isNullOrEmpty(req.getSql())) { throw new InvalidQueryParameterException(ExceptionErrorCode.MISSING_SQL.getErrorMessage()); } SQLRequest sqlReq = (SQLRequest) req; DateTime start = null; DateTime end = null; String intervalStr = sqlReq.getIntervals(); String customTime = sqlReq.getCustomTime(); String startTime = sqlReq.getStartTime(); String endTime = sqlReq.getEndTime(); if (customTime != null) { if (startTime == null && endTime == null && intervalStr == null) { if (customTime.equals("today")) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_CUSTOM_TIME.getErrorMessage()+ customTime); } else if (customTime.equals("yesterday")) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_CUSTOM_TIME.getErrorMessage()+ customTime); } } else { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage()); } } else { if (intervalStr != null) { String[] strArr = intervalStr.split("/"); if (strArr.length != 2) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage()); } else { startTime = strArr[0]; endTime = strArr[1]; } } try { start = PulsarDateTimeFormatter.INPUTTIME_FORMATTER.parseDateTime(startTime); end = PulsarDateTimeFormatter.INPUTTIME_FORMATTER.parseDateTime(endTime); } catch (Exception e) { } if(start == null) throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage()); if(end == null) throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage()); if (start.compareTo(end) >= 0) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage() + start + "/" + end); } DateTime now = new DateTime(); if (start.compareTo(now) >= 0) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage() + start); } } } }
SQLQueryValidator implements QueryValidator<BaseSQLRequest> { @Override public void validate(BaseSQLRequest req) { if (Strings.isNullOrEmpty(req.getSql())) { throw new InvalidQueryParameterException(ExceptionErrorCode.MISSING_SQL.getErrorMessage()); } SQLRequest sqlReq = (SQLRequest) req; DateTime start = null; DateTime end = null; String intervalStr = sqlReq.getIntervals(); String customTime = sqlReq.getCustomTime(); String startTime = sqlReq.getStartTime(); String endTime = sqlReq.getEndTime(); if (customTime != null) { if (startTime == null && endTime == null && intervalStr == null) { if (customTime.equals("today")) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_CUSTOM_TIME.getErrorMessage()+ customTime); } else if (customTime.equals("yesterday")) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_CUSTOM_TIME.getErrorMessage()+ customTime); } } else { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage()); } } else { if (intervalStr != null) { String[] strArr = intervalStr.split("/"); if (strArr.length != 2) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage()); } else { startTime = strArr[0]; endTime = strArr[1]; } } try { start = PulsarDateTimeFormatter.INPUTTIME_FORMATTER.parseDateTime(startTime); end = PulsarDateTimeFormatter.INPUTTIME_FORMATTER.parseDateTime(endTime); } catch (Exception e) { } if(start == null) throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage()); if(end == null) throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage()); if (start.compareTo(end) >= 0) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage() + start + "/" + end); } DateTime now = new DateTime(); if (start.compareTo(now) >= 0) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage() + start); } } }  }
SQLQueryValidator implements QueryValidator<BaseSQLRequest> { @Override public void validate(BaseSQLRequest req) { if (Strings.isNullOrEmpty(req.getSql())) { throw new InvalidQueryParameterException(ExceptionErrorCode.MISSING_SQL.getErrorMessage()); } SQLRequest sqlReq = (SQLRequest) req; DateTime start = null; DateTime end = null; String intervalStr = sqlReq.getIntervals(); String customTime = sqlReq.getCustomTime(); String startTime = sqlReq.getStartTime(); String endTime = sqlReq.getEndTime(); if (customTime != null) { if (startTime == null && endTime == null && intervalStr == null) { if (customTime.equals("today")) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_CUSTOM_TIME.getErrorMessage()+ customTime); } else if (customTime.equals("yesterday")) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_CUSTOM_TIME.getErrorMessage()+ customTime); } } else { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage()); } } else { if (intervalStr != null) { String[] strArr = intervalStr.split("/"); if (strArr.length != 2) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage()); } else { startTime = strArr[0]; endTime = strArr[1]; } } try { start = PulsarDateTimeFormatter.INPUTTIME_FORMATTER.parseDateTime(startTime); end = PulsarDateTimeFormatter.INPUTTIME_FORMATTER.parseDateTime(endTime); } catch (Exception e) { } if(start == null) throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage()); if(end == null) throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage()); if (start.compareTo(end) >= 0) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage() + start + "/" + end); } DateTime now = new DateTime(); if (start.compareTo(now) >= 0) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage() + start); } } }  @Override void validate(BaseSQLRequest req); }
SQLQueryValidator implements QueryValidator<BaseSQLRequest> { @Override public void validate(BaseSQLRequest req) { if (Strings.isNullOrEmpty(req.getSql())) { throw new InvalidQueryParameterException(ExceptionErrorCode.MISSING_SQL.getErrorMessage()); } SQLRequest sqlReq = (SQLRequest) req; DateTime start = null; DateTime end = null; String intervalStr = sqlReq.getIntervals(); String customTime = sqlReq.getCustomTime(); String startTime = sqlReq.getStartTime(); String endTime = sqlReq.getEndTime(); if (customTime != null) { if (startTime == null && endTime == null && intervalStr == null) { if (customTime.equals("today")) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_CUSTOM_TIME.getErrorMessage()+ customTime); } else if (customTime.equals("yesterday")) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_CUSTOM_TIME.getErrorMessage()+ customTime); } } else { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage()); } } else { if (intervalStr != null) { String[] strArr = intervalStr.split("/"); if (strArr.length != 2) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage()); } else { startTime = strArr[0]; endTime = strArr[1]; } } try { start = PulsarDateTimeFormatter.INPUTTIME_FORMATTER.parseDateTime(startTime); end = PulsarDateTimeFormatter.INPUTTIME_FORMATTER.parseDateTime(endTime); } catch (Exception e) { } if(start == null) throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage()); if(end == null) throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage()); if (start.compareTo(end) >= 0) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage() + start + "/" + end); } DateTime now = new DateTime(); if (start.compareTo(now) >= 0) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_QUERYTIME.getErrorMessage() + start); } } }  @Override void validate(BaseSQLRequest req);  }