SseUtil { public static List<String> convertToRegex(@Nullable String topicFilter) { List<String> filters = new ArrayList<>(); if (topicFilter == null || topicFilter.isEmpty()) { filters.add(".*"); } else { StringTokenizer tokenizer = new StringTokenizer(topicFilter, ","); while (tokenizer.hasMoreElements()) { String regex = tokenizer.nextToken().trim().replace("*", ".*") + ".*"; filters.add(regex); } } return filters; }  static EventDTO buildDTO(final Event event); static OutboundSseEvent buildEvent(OutboundSseEvent.Builder eventBuilder, EventDTO event); static boolean isValidTopicFilter(@Nullable String topicFilter); static List<String> convertToRegex(@Nullable String topicFilter);  }
@Test public void testFilterMatchers() { List<String> regexes = SseUtil .convertToRegex("openhabtest/test/test, openhab,qivicon"); assertThat("openhab/test/test/test/test/test".matches(regexes.get(0)), is(true)); assertThat("openhab/asdf/test/test/test/test".matches(regexes.get(0)), is(true)); assertThat("openhab/asdf/ASDF/test/test/test".matches(regexes.get(0)), is(false)); assertThat("openhab/test/test/test/test/test".matches(regexes.get(1)), is(true)); assertThat("openhab/asdf/test/test/test/test".matches(regexes.get(1)), is(false)); assertThat("openhab/asdf/ASDF/test/test/test".matches(regexes.get(1)), is(false)); assertThat("openhab/test/test/test/test/test".matches(regexes.get(2)), is(true)); assertThat("openhab/asdf/test/test/test/test".matches(regexes.get(2)), is(true)); assertThat("openhab/asdf/ASDF/test/test/test".matches(regexes.get(2)), is(true)); assertThat("openhab/test/test/test/test/test".matches(regexes.get(3)), is(false)); assertThat("openhab/asdf/test/test/test/test".matches(regexes.get(3)), is(false)); assertThat("qivicon/asdf/ASDF/test/test/test".matches(regexes.get(3)), is(true)); }
@Test public void testMoreFilterMatchers() { List<String> regexes = SseUtil.convertToRegex(", *, openhab/items/*/added, openhab/items"); assertThat("openhab/test/test/test/test/test".matches(regexes.get(0)), is(true)); assertThat("openhab/asdf/test/test/test/test".matches(regexes.get(0)), is(true)); assertThat("openhab/asdf/ASDF/test/test/test".matches(regexes.get(0)), is(true)); assertThat("openhab/test/test/test/test/test".matches(regexes.get(1)), is(false)); assertThat("openhab/items/anyitem/added".matches(regexes.get(1)), is(true)); assertThat("openhab/items/anyitem/removed".matches(regexes.get(1)), is(false)); assertThat("openhab/items/anyitem/added".matches(regexes.get(2)), is(true)); assertThat("openhab/items/anyitem/removed".matches(regexes.get(2)), is(true)); assertThat("openhab/items/anyitem/updated".matches(regexes.get(2)), is(true)); assertThat("openhab/things/anything/updated".matches(regexes.get(2)), is(false)); }
@Test public void testEvenMoreFilterMatchers() { List<String> regexes = SseUtil.convertToRegex(""); assertThat("openhab/test/test/test/test/test".matches(regexes.get(0)), is(true)); assertThat("openhab/asdf/test/test/test/test".matches(regexes.get(0)), is(true)); assertThat("openhab/asdf/ASDF/test/test/test".matches(regexes.get(0)), is(true)); regexes = SseUtil.convertToRegex("*/added"); assertThat("openhab/items/anyitem/added".matches(regexes.get(0)), is(true)); assertThat("openhab/items/anyitem/removed".matches(regexes.get(0)), is(false)); regexes = SseUtil.convertToRegex("*added"); assertThat("openhab/items/anyitem/added".matches(regexes.get(0)), is(true)); assertThat("openhab/items/anyitem/removed".matches(regexes.get(0)), is(false)); }