MetadataSelectorMatcher { public Set<String> filterNamespaces(@Nullable String namespaceSelector, @Nullable Locale locale) { if (namespaceSelector == null || namespaceSelector.isEmpty()) { return Collections.emptySet(); } else { Set<String> originalNamespaces = Arrays.stream(namespaceSelector.split(",")) .filter(n -> !metadataRegistry.isInternalNamespace(n)) .map(n -> n.trim()) .collect(Collectors.toSet()); String namespacePattern = originalNamespaces.stream().collect(Collectors.joining("|")); Pattern pattern = Pattern.compile(METADATA_SCHEME_PREFIX + "(" + namespacePattern + ")$"); Collection<ConfigDescription> configDescriptions = configDescriptionRegistry.getConfigDescriptions(locale); Set<String> configNamespaces = configDescriptions.stream() .filter(cd -> METADATA_SCHEME.equals(cd.getUID().getScheme())).map(cd -> cd.getUID().toString()) .filter(pattern.asPredicate()).map(uri -> uri.substring(METADATA_SCHEME_PREFIX.length())) .collect(toSet()); Set<String> result = new HashSet<>(originalNamespaces); result.addAll(configNamespaces); return result.stream().filter(namespace -> namespace.matches(AbstractUID.SEGMENT_PATTERN)).collect(toSet()); } } @Activate  MetadataSelectorMatcher(final @Reference MetadataRegistry metadataRegistry,
            final @Reference ConfigDescriptionRegistry configDescriptionRegistry); Set<String> filterNamespaces(@Nullable String namespaceSelector, @Nullable Locale locale);  }
@Test public void specificSelectorShouldReturnSpecificNamespace() { assertThat(matcher.filterNamespaces("alexa", null), hasSize(1)); assertThat(matcher.filterNamespaces("alexa", null), hasItem("alexa")); assertThat(matcher.filterNamespaces("magic", null), hasSize(1)); assertThat(matcher.filterNamespaces("magic", null), hasItem("magic")); assertThat(matcher.filterNamespaces("magic2", null), hasSize(1)); assertThat(matcher.filterNamespaces("magic2", null), hasItem("magic2")); assertThat(matcher.filterNamespaces("unknown", null), hasSize(1)); }
@Test public void regularExpressionShouldMatchSubset() { assertThat(matcher.filterNamespaces(".*", null), hasSize(4)); assertThat(matcher.filterNamespaces("magic.?", null), hasSize(2)); }
@Test public void nonConfigDescriptionSelectorShouldBeResult() { assertThat(matcher.filterNamespaces("magic, foo, bar", null), hasItems("magic", "foo", "bar")); }
@Test public void nullSelectorShouldReturnEmptySet() { assertThat(matcher.filterNamespaces(null, null), is(Collections.emptySet())); }
@Test public void emptySelectorShouldReturnEmptySet() { assertThat(matcher.filterNamespaces("", null), is(Collections.emptySet())); }