mirror of
https://github.com/danieldemus/openhab-core.git
synced 2025-01-25 19:55:48 +01:00
[REST] Apply metadata selector regex on all namespaces (#1756)
* Change metadata selector Fixes #1692 Signed-off-by: Simon Lamon <simonlamon93@hotmail.com>
This commit is contained in:
parent
4e045204ac
commit
d59d3c3cc2
@ -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<String> 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<ConfigDescription> configDescriptions = configDescriptionRegistry.getConfigDescriptions(locale);
|
||||
Pattern pattern = Pattern.compile("(" + namespacePattern + ")$");
|
||||
|
||||
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> 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<String> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<ConfigDescription> mockConfigDescriptions() throws Exception {
|
||||
List<ConfigDescription> 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
|
||||
|
Loading…
Reference in New Issue
Block a user