Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
Fix item resource metadata retrieval (#5640)
Browse files Browse the repository at this point in the history
Fixes #5632.

Signed-off-by: Henning Treu <henning.treu@telekom.de>
  • Loading branch information
htreu authored and kaikreuzer committed May 27, 2018
1 parent a8fe70d commit 08032e8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
package org.eclipse.smarthome.io.rest.core.internal.item;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.junit.Assert.assertThat;
Expand Down Expand Up @@ -92,12 +92,17 @@ public void specificSelector_shouldReturnSpecificNamespace() {
assertThat(matcher.filterNamespaces("magic2", null), hasSize(1));
assertThat(matcher.filterNamespaces("magic2", null), hasItem("magic2"));

assertThat(matcher.filterNamespaces("unknown", null), hasSize(0));
assertThat(matcher.filterNamespaces("unknown", null), hasSize(1));
}

@Test
public void regularExpression_shouldMatchSubset() {
assertThat(matcher.filterNamespaces(".*", null), hasSize(4));
assertThat(matcher.filterNamespaces("magic.?", null), hasSize(2));
}

@Test
public void nonConfigDescriptionSelector_shouldBeResult() {
assertThat(matcher.filterNamespaces("magic, foo, bar", null), hasItems("magic", "foo", "bar"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.eclipse.smarthome.config.core.ConfigDescription;
import org.eclipse.smarthome.config.core.ConfigDescriptionRegistry;
import org.eclipse.smarthome.core.common.AbstractUID;
import org.eclipse.smarthome.core.items.MetadataRegistry;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
Expand Down Expand Up @@ -56,15 +58,27 @@ public Set<String> filterNamespaces(String namespaceSelector, Locale locale) {
if (namespaceSelector == null || namespaceSelector.isEmpty()) {
return Collections.emptySet();
} else {
String namespacePattern = Arrays.stream(namespaceSelector.split(",")) //
Set<String> originalNamespaces = Arrays.stream(namespaceSelector.split(",")) //
.filter(n -> !metadataRegistry.isInternalNamespace(n)) //
.collect(Collectors.joining("|"));
.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);
return configDescriptions.stream().filter(cd -> cd.getUID().getScheme().equals(METADATA_SCHEME))
.map(cd -> cd.getUID().toString()).filter(pattern.asPredicate())
.map(uri -> uri.substring(METADATA_SCHEME_PREFIX.length())).collect(toSet());

Set<String> configNamespaces = configDescriptions.stream()
.filter(cd -> cd.getUID().getScheme().equals(METADATA_SCHEME)).map(cd -> cd.getUID().toString())
.filter(pattern.asPredicate()).map(uri -> uri.substring(METADATA_SCHEME_PREFIX.length()))
.collect(toSet());

// merge configDescription namespaces and namespaces from the namespace selector:
Set<String> result = new HashSet<>(originalNamespaces);
result.addAll(configNamespaces);

// 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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
// watch changes to the main metadata value and load the corresponding
// config description for its configuration:
$scope.$watch(function watchMetadataValue(scope) {
return ctrl.metadata.value;
return ctrl.metadata ? ctrl.metadata.value : undefined;
}, function handleMetadataValueChange(newValue, oldValue) {
if (!ctrl.metadata.value || ctrl.metadata.value === '') {
return;
Expand Down

0 comments on commit 08032e8

Please sign in to comment.