From d59d3c3cc2837fd1aa5ac687554e02ec8a4bbe9e Mon Sep 17 00:00:00 2001 From: silamon <32477463+silamon@users.noreply.github.com> Date: Sat, 14 Nov 2020 17:58:46 +0100 Subject: [PATCH] [REST] Apply metadata selector regex on all namespaces (#1756) * Change metadata selector Fixes #1692 Signed-off-by: Simon Lamon --- .../item/MetadataSelectorMatcher.java | 35 ++++++++----------- .../item/MetadataSelectorMatcherTest.java | 29 +++++---------- 2 files changed, 23 insertions(+), 41 deletions(-) diff --git a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/item/MetadataSelectorMatcher.java b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/item/MetadataSelectorMatcher.java index fc81ac5c4..f42eceb5a 100644 --- a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/item/MetadataSelectorMatcher.java +++ b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/item/MetadataSelectorMatcher.java @@ -12,10 +12,7 @@ */ package org.openhab.core.io.rest.core.internal.item; -import static java.util.stream.Collectors.toSet; - import java.util.Arrays; -import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.Locale; @@ -26,8 +23,6 @@ import java.util.stream.Collectors; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.common.AbstractUID; -import org.openhab.core.config.core.ConfigDescription; -import org.openhab.core.config.core.ConfigDescriptionRegistry; import org.openhab.core.items.MetadataRegistry; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; @@ -42,17 +37,11 @@ import org.osgi.service.component.annotations.Reference; @NonNullByDefault public class MetadataSelectorMatcher { - private static final String METADATA_SCHEME = "metadata"; - private static final String METADATA_SCHEME_PREFIX = METADATA_SCHEME + ":"; - private final MetadataRegistry metadataRegistry; - private final ConfigDescriptionRegistry configDescriptionRegistry; @Activate - public MetadataSelectorMatcher(final @Reference MetadataRegistry metadataRegistry, - final @Reference ConfigDescriptionRegistry configDescriptionRegistry) { + public MetadataSelectorMatcher(final @Reference MetadataRegistry metadataRegistry) { this.metadataRegistry = metadataRegistry; - this.configDescriptionRegistry = configDescriptionRegistry; } /** @@ -72,22 +61,26 @@ public class MetadataSelectorMatcher { .map(n -> n.trim()) // .collect(Collectors.toSet()); + Set allMetadataNamespaces = metadataRegistry.getAll().stream() // + .map(metadata -> metadata.getUID().getNamespace()) // + .distinct() // + .collect(Collectors.toSet()); + String namespacePattern = originalNamespaces.stream().collect(Collectors.joining("|")); - Pattern pattern = Pattern.compile(METADATA_SCHEME_PREFIX + "(" + namespacePattern + ")$"); - Collection configDescriptions = configDescriptionRegistry.getConfigDescriptions(locale); + Pattern pattern = Pattern.compile("(" + namespacePattern + ")$"); - Set 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 metadataNamespaces = allMetadataNamespaces.stream() // + .filter(n -> !metadataRegistry.isInternalNamespace(n)) // + .filter(pattern.asPredicate()).collect(Collectors.toSet()); - // merge configDescription namespaces and namespaces from the namespace selector: + // merge metadata namespaces and namespaces from the namespace selector: Set result = new HashSet<>(originalNamespaces); - result.addAll(configNamespaces); + result.addAll(metadataNamespaces); // filter all name spaces which do not match the UID segment pattern (this will be the regex tokens): - return result.stream().filter(namespace -> namespace.matches(AbstractUID.SEGMENT_PATTERN)).collect(toSet()); + return result.stream().filter(namespace -> namespace.matches(AbstractUID.SEGMENT_PATTERN)) + .collect(Collectors.toSet()); } } } diff --git a/bundles/org.openhab.core.io.rest.core/src/test/java/org/openhab/core/io/rest/core/internal/item/MetadataSelectorMatcherTest.java b/bundles/org.openhab.core.io.rest.core/src/test/java/org/openhab/core/io/rest/core/internal/item/MetadataSelectorMatcherTest.java index 5fffed066..f84241831 100644 --- a/bundles/org.openhab.core.io.rest.core/src/test/java/org/openhab/core/io/rest/core/internal/item/MetadataSelectorMatcherTest.java +++ b/bundles/org.openhab.core.io.rest.core/src/test/java/org/openhab/core/io/rest/core/internal/item/MetadataSelectorMatcherTest.java @@ -19,11 +19,9 @@ import static org.hamcrest.core.IsIterableContaining.hasItem; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when; -import java.net.URI; -import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Map; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -32,9 +30,8 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; -import org.openhab.core.config.core.ConfigDescription; -import org.openhab.core.config.core.ConfigDescriptionBuilder; -import org.openhab.core.config.core.ConfigDescriptionRegistry; +import org.openhab.core.items.Metadata; +import org.openhab.core.items.MetadataKey; import org.openhab.core.items.MetadataRegistry; /** @@ -48,26 +45,18 @@ public class MetadataSelectorMatcherTest { private MetadataSelectorMatcher matcher; - private @Mock ConfigDescriptionRegistry configDescriptionRegistry; private @Mock MetadataRegistry metadataRegistry; @BeforeEach public void setup() throws Exception { - when(configDescriptionRegistry.getConfigDescriptions(null)).thenReturn(mockConfigDescriptions()); + when(metadataRegistry.getAll()) + .thenReturn(List.of(new Metadata(new MetadataKey("magic", "test_item"), "test", Map.of()), + new Metadata(new MetadataKey("magic2", "test_item"), "test", Map.of()), + new Metadata(new MetadataKey("homekit", "test_item"), "test", Map.of()), + new Metadata(new MetadataKey("alexa", "test_item"), "test", Map.of()))); when(metadataRegistry.isInternalNamespace(anyString())).thenReturn(false); - matcher = new MetadataSelectorMatcher(metadataRegistry, configDescriptionRegistry); - } - - private Collection mockConfigDescriptions() throws Exception { - List configDescriptions = new ArrayList<>(); - - configDescriptions.add(ConfigDescriptionBuilder.create(new URI("metadata:magic")).build()); - configDescriptions.add(ConfigDescriptionBuilder.create(new URI("metadata:magic2")).build()); - configDescriptions.add(ConfigDescriptionBuilder.create(new URI("metadata:homekit")).build()); - configDescriptions.add(ConfigDescriptionBuilder.create(new URI("metadata:alexa")).build()); - - return configDescriptions; + matcher = new MetadataSelectorMatcher(metadataRegistry); } @Test