SQLStatisticsEventValidator { public static List<String> validateEvent(final StatisticEvent statisticEvent) { final List<String> warningList = new ArrayList<>(); for (final StatisticTag tag : statisticEvent.getTagList()) { if (DIRTY_CHARACTER_PATTERN.matcher(tag.getTag()).find()) { warningList.add(String.format( "Statistic tag [%s] contains unsupported characters, as defined by the regex %s. They will be replaced with a '%s'", tag.getTag(), DIRTY_CHARACTER_PATTERN.toString(), SQLStatisticConstants.DIRTY_CHARACTER_REPLACEMENT)); } if (DIRTY_CHARACTER_PATTERN.matcher(tag.getValue()).find()) { warningList.add(String.format( "Statistic value [%s] contains unsupported characters, as defined by the regex %s. They will be replaced with a '%s'", tag.getValue(), DIRTY_CHARACTER_PATTERN.toString(), SQLStatisticConstants.DIRTY_CHARACTER_REPLACEMENT)); } } return warningList; }  static List<String> validateEvent(final StatisticEvent statisticEvent); static String cleanString(final String dirtyString); static boolean isKeyTooLong(final String statisticKey);  }
@Test void testValidateEvent_NoTags() { final StatisticEvent event = StatisticEvent.createCount(123L, "statName", null, 1); final List<String> warnings = validator.validateEvent(event); assertThat(warnings.size()).isEqualTo(0); }
@Test void testValidateEvent_badTag() { final List<StatisticTag> tags = new ArrayList<>(); tags.add(new StatisticTag("x" + SQLStatisticConstants.NAME_SEPARATOR + "x", "someValue1")); tags.add(new StatisticTag("yy", "someValue2")); final StatisticEvent event = StatisticEvent.createCount(123L, "statName", tags, 1); final List<String> warnings = validator.validateEvent(event); assertThat(warnings.size()).isEqualTo(1); }
@Test void testValidateEvent_badValue() { final List<StatisticTag> tags = new ArrayList<>(); tags.add(new StatisticTag("xx", "someValue1")); tags.add(new StatisticTag("yy", "some" + SQLStatisticConstants.NAME_SEPARATOR + "Value2")); final StatisticEvent event = StatisticEvent.createCount(123L, "statName", tags, 1); final List<String> warnings = validator.validateEvent(event); assertThat(warnings.size()).isEqualTo(1); }
@Test void testValidateEvent_badTagsAndValues() { final List<StatisticTag> tags = new ArrayList<>(); tags.add(new StatisticTag("x" + SQLStatisticConstants.NAME_SEPARATOR + "x", "some" + SQLStatisticConstants.NAME_SEPARATOR + "Value1")); tags.add(new StatisticTag("y" + SQLStatisticConstants.NAME_SEPARATOR + "y", "some" + SQLStatisticConstants.NAME_SEPARATOR + "Value2")); final StatisticEvent event = StatisticEvent.createCount(123L, "statName", tags, 1); final List<String> warnings = validator.validateEvent(event); assertThat(warnings.size()).isEqualTo(4); }