@Test public void testNoMeticMeta(){ List<PulsarRestMetricMeta> metricsList=new ArrayList<PulsarRestMetricMeta>(); PulsarRestMetricMeta meta=new PulsarRestMetricMeta(); metricsList.add(meta); PulsarRestMetricRegistry pulsarRestMetricRegistry=new PulsarRestMetricRegistry(metricsList); PulsarRestValidator pulsarRestValidator = new PulsarRestValidator(pulsarRestMetricRegistry); CoreRequest coreRequest=new CoreRequest(); coreRequest.setEndTime("2015-09-15 23:59:59"); coreRequest.setStartTime("2015-09-09 00:00:00"); coreRequest.setDimensions(Lists.newArrayList("testDim")); coreRequest.setFilter("testFilter"); coreRequest.setGranularity("testGranularity"); coreRequest.setHaving("testHaving"); coreRequest.setMaxResults(0); coreRequest.setMetrics(Lists.newArrayList("testMatic")); coreRequest.setNamespace(Constants.RequestNameSpace.core); coreRequest.setSort("testSort"); try{ pulsarRestValidator.validate(coreRequest); fail("Expect InvalidQueryParameterException"); }catch(InvalidQueryParameterException ex){ assertTrue(true); } }
@Override public void validate(BaseRequest req){ List<String> metricList = req.getMetrics(); if(metricList == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.MISSING_METRIC.getErrorMessage()); } else if(metricList.isEmpty()) { throw new InvalidQueryParameterException(ExceptionErrorCode.MISSING_METRIC.getErrorMessage()); } else if(req.getMetrics().size() > 1) { throw new InvalidQueryParameterException(ExceptionErrorCode.MULTI_METRICS_ERROR.getErrorMessage()); } PulsarRestMetricMeta metricMeta = null; String metricName = null; for(String metric : metricList) { metricName = metric.toLowerCase(); metricMeta = pulsarRestMetricRegistry.getMetricsMetaFromName(metricName); if (metricMeta == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_METRIC.getErrorMessage() + metricName); } Set<String> metricEndpointSet = metricMeta.getMetricEndpoints(); if (metricEndpointSet == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_ENDPOINTS.getErrorMessage() + metricName); } if(req instanceof RealtimeRequest) { if (!metricEndpointSet.contains(ENDPOINT_REALTIME)) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_ENDPOINTS.getErrorMessage() + metricName); } } else if(req instanceof CoreRequest) { if (!metricEndpointSet.contains(ENDPOINT_CORE)) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_ENDPOINTS.getErrorMessage() + metricName); } } else { } } Integer maxResults = req.getMaxResults(); if(maxResults != null && maxResults <= 0) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_MAXRESULT.getErrorMessage() + maxResults); } if(req instanceof RealtimeRequest) { Integer duration = ((RealtimeRequest)req).getDuration(); if(duration != null && (duration <= 0 || duration > 1800)) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_DURATION.getErrorMessage() + duration); } } String tableName = metricMeta.getTableName(); if (tableName == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_METRIC.getErrorMessage() + metricName); } DataSourceProvider pulsarDB = DataSourceMetaRepo.getInstance().getDBMetaFromCache(DataSourceConfigurationLoader.PULSAR_DATASOURCE); Table pulsarTable = pulsarDB.getTableByName(tableName); if (pulsarTable == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_METRIC.getErrorMessage() + metricName); } if(req.getDimensions() != null) { for(String dimension : req.getDimensions()) { if(pulsarTable.getDimensionByName(dimension) == null){ throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_DIMENSION.getErrorMessage() + dimension); } } } }
PulsarRestValidator implements QueryValidator<BaseRequest> { @Override public void validate(BaseRequest req){ List<String> metricList = req.getMetrics(); if(metricList == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.MISSING_METRIC.getErrorMessage()); } else if(metricList.isEmpty()) { throw new InvalidQueryParameterException(ExceptionErrorCode.MISSING_METRIC.getErrorMessage()); } else if(req.getMetrics().size() > 1) { throw new InvalidQueryParameterException(ExceptionErrorCode.MULTI_METRICS_ERROR.getErrorMessage()); } PulsarRestMetricMeta metricMeta = null; String metricName = null; for(String metric : metricList) { metricName = metric.toLowerCase(); metricMeta = pulsarRestMetricRegistry.getMetricsMetaFromName(metricName); if (metricMeta == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_METRIC.getErrorMessage() + metricName); } Set<String> metricEndpointSet = metricMeta.getMetricEndpoints(); if (metricEndpointSet == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_ENDPOINTS.getErrorMessage() + metricName); } if(req instanceof RealtimeRequest) { if (!metricEndpointSet.contains(ENDPOINT_REALTIME)) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_ENDPOINTS.getErrorMessage() + metricName); } } else if(req instanceof CoreRequest) { if (!metricEndpointSet.contains(ENDPOINT_CORE)) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_ENDPOINTS.getErrorMessage() + metricName); } } else { } } Integer maxResults = req.getMaxResults(); if(maxResults != null && maxResults <= 0) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_MAXRESULT.getErrorMessage() + maxResults); } if(req instanceof RealtimeRequest) { Integer duration = ((RealtimeRequest)req).getDuration(); if(duration != null && (duration <= 0 || duration > 1800)) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_DURATION.getErrorMessage() + duration); } } String tableName = metricMeta.getTableName(); if (tableName == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_METRIC.getErrorMessage() + metricName); } DataSourceProvider pulsarDB = DataSourceMetaRepo.getInstance().getDBMetaFromCache(DataSourceConfigurationLoader.PULSAR_DATASOURCE); Table pulsarTable = pulsarDB.getTableByName(tableName); if (pulsarTable == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_METRIC.getErrorMessage() + metricName); } if(req.getDimensions() != null) { for(String dimension : req.getDimensions()) { if(pulsarTable.getDimensionByName(dimension) == null){ throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_DIMENSION.getErrorMessage() + dimension); } } } } }
PulsarRestValidator implements QueryValidator<BaseRequest> { @Override public void validate(BaseRequest req){ List<String> metricList = req.getMetrics(); if(metricList == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.MISSING_METRIC.getErrorMessage()); } else if(metricList.isEmpty()) { throw new InvalidQueryParameterException(ExceptionErrorCode.MISSING_METRIC.getErrorMessage()); } else if(req.getMetrics().size() > 1) { throw new InvalidQueryParameterException(ExceptionErrorCode.MULTI_METRICS_ERROR.getErrorMessage()); } PulsarRestMetricMeta metricMeta = null; String metricName = null; for(String metric : metricList) { metricName = metric.toLowerCase(); metricMeta = pulsarRestMetricRegistry.getMetricsMetaFromName(metricName); if (metricMeta == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_METRIC.getErrorMessage() + metricName); } Set<String> metricEndpointSet = metricMeta.getMetricEndpoints(); if (metricEndpointSet == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_ENDPOINTS.getErrorMessage() + metricName); } if(req instanceof RealtimeRequest) { if (!metricEndpointSet.contains(ENDPOINT_REALTIME)) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_ENDPOINTS.getErrorMessage() + metricName); } } else if(req instanceof CoreRequest) { if (!metricEndpointSet.contains(ENDPOINT_CORE)) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_ENDPOINTS.getErrorMessage() + metricName); } } else { } } Integer maxResults = req.getMaxResults(); if(maxResults != null && maxResults <= 0) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_MAXRESULT.getErrorMessage() + maxResults); } if(req instanceof RealtimeRequest) { Integer duration = ((RealtimeRequest)req).getDuration(); if(duration != null && (duration <= 0 || duration > 1800)) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_DURATION.getErrorMessage() + duration); } } String tableName = metricMeta.getTableName(); if (tableName == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_METRIC.getErrorMessage() + metricName); } DataSourceProvider pulsarDB = DataSourceMetaRepo.getInstance().getDBMetaFromCache(DataSourceConfigurationLoader.PULSAR_DATASOURCE); Table pulsarTable = pulsarDB.getTableByName(tableName); if (pulsarTable == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_METRIC.getErrorMessage() + metricName); } if(req.getDimensions() != null) { for(String dimension : req.getDimensions()) { if(pulsarTable.getDimensionByName(dimension) == null){ throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_DIMENSION.getErrorMessage() + dimension); } } } } PulsarRestValidator(PulsarRestMetricRegistry pulsarRestMetricRegistry); }
PulsarRestValidator implements QueryValidator<BaseRequest> { @Override public void validate(BaseRequest req){ List<String> metricList = req.getMetrics(); if(metricList == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.MISSING_METRIC.getErrorMessage()); } else if(metricList.isEmpty()) { throw new InvalidQueryParameterException(ExceptionErrorCode.MISSING_METRIC.getErrorMessage()); } else if(req.getMetrics().size() > 1) { throw new InvalidQueryParameterException(ExceptionErrorCode.MULTI_METRICS_ERROR.getErrorMessage()); } PulsarRestMetricMeta metricMeta = null; String metricName = null; for(String metric : metricList) { metricName = metric.toLowerCase(); metricMeta = pulsarRestMetricRegistry.getMetricsMetaFromName(metricName); if (metricMeta == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_METRIC.getErrorMessage() + metricName); } Set<String> metricEndpointSet = metricMeta.getMetricEndpoints(); if (metricEndpointSet == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_ENDPOINTS.getErrorMessage() + metricName); } if(req instanceof RealtimeRequest) { if (!metricEndpointSet.contains(ENDPOINT_REALTIME)) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_ENDPOINTS.getErrorMessage() + metricName); } } else if(req instanceof CoreRequest) { if (!metricEndpointSet.contains(ENDPOINT_CORE)) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_ENDPOINTS.getErrorMessage() + metricName); } } else { } } Integer maxResults = req.getMaxResults(); if(maxResults != null && maxResults <= 0) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_MAXRESULT.getErrorMessage() + maxResults); } if(req instanceof RealtimeRequest) { Integer duration = ((RealtimeRequest)req).getDuration(); if(duration != null && (duration <= 0 || duration > 1800)) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_DURATION.getErrorMessage() + duration); } } String tableName = metricMeta.getTableName(); if (tableName == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_METRIC.getErrorMessage() + metricName); } DataSourceProvider pulsarDB = DataSourceMetaRepo.getInstance().getDBMetaFromCache(DataSourceConfigurationLoader.PULSAR_DATASOURCE); Table pulsarTable = pulsarDB.getTableByName(tableName); if (pulsarTable == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_METRIC.getErrorMessage() + metricName); } if(req.getDimensions() != null) { for(String dimension : req.getDimensions()) { if(pulsarTable.getDimensionByName(dimension) == null){ throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_DIMENSION.getErrorMessage() + dimension); } } } } PulsarRestValidator(PulsarRestMetricRegistry pulsarRestMetricRegistry); @Override void validate(BaseRequest req); }
PulsarRestValidator implements QueryValidator<BaseRequest> { @Override public void validate(BaseRequest req){ List<String> metricList = req.getMetrics(); if(metricList == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.MISSING_METRIC.getErrorMessage()); } else if(metricList.isEmpty()) { throw new InvalidQueryParameterException(ExceptionErrorCode.MISSING_METRIC.getErrorMessage()); } else if(req.getMetrics().size() > 1) { throw new InvalidQueryParameterException(ExceptionErrorCode.MULTI_METRICS_ERROR.getErrorMessage()); } PulsarRestMetricMeta metricMeta = null; String metricName = null; for(String metric : metricList) { metricName = metric.toLowerCase(); metricMeta = pulsarRestMetricRegistry.getMetricsMetaFromName(metricName); if (metricMeta == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_METRIC.getErrorMessage() + metricName); } Set<String> metricEndpointSet = metricMeta.getMetricEndpoints(); if (metricEndpointSet == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_ENDPOINTS.getErrorMessage() + metricName); } if(req instanceof RealtimeRequest) { if (!metricEndpointSet.contains(ENDPOINT_REALTIME)) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_ENDPOINTS.getErrorMessage() + metricName); } } else if(req instanceof CoreRequest) { if (!metricEndpointSet.contains(ENDPOINT_CORE)) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_ENDPOINTS.getErrorMessage() + metricName); } } else { } } Integer maxResults = req.getMaxResults(); if(maxResults != null && maxResults <= 0) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_MAXRESULT.getErrorMessage() + maxResults); } if(req instanceof RealtimeRequest) { Integer duration = ((RealtimeRequest)req).getDuration(); if(duration != null && (duration <= 0 || duration > 1800)) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_DURATION.getErrorMessage() + duration); } } String tableName = metricMeta.getTableName(); if (tableName == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_METRIC.getErrorMessage() + metricName); } DataSourceProvider pulsarDB = DataSourceMetaRepo.getInstance().getDBMetaFromCache(DataSourceConfigurationLoader.PULSAR_DATASOURCE); Table pulsarTable = pulsarDB.getTableByName(tableName); if (pulsarTable == null) { throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_METRIC.getErrorMessage() + metricName); } if(req.getDimensions() != null) { for(String dimension : req.getDimensions()) { if(pulsarTable.getDimensionByName(dimension) == null){ throw new InvalidQueryParameterException(ExceptionErrorCode.INVALID_DIMENSION.getErrorMessage() + dimension); } } } } PulsarRestValidator(PulsarRestMetricRegistry pulsarRestMetricRegistry); @Override void validate(BaseRequest req);  }