Skip to content

Commit

Permalink
Issue26-Add support for filter.mode=named (#31)
Browse files Browse the repository at this point in the history
#26 Add support for filter.mode=named
  • Loading branch information
matthewgardner authored and Scrin committed May 20, 2019
1 parent f34c045 commit 555f21a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
3 changes: 2 additions & 1 deletion ruuvi-collector.properties.example
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
# Valid values "none", "blacklist" and "whitelist".
# none = Allows any source to be stored (default)
# blacklist = Allows all sources EXCEPT those listed
# whitelist = Allows ONLY sources that are listed
# whitelist = Allows ONLY sources that are listed in filter.macs
# named = Allows ONLY sources that are listed in ruuvi-names.properties
#filter.mode=none

# Mac addresses to blacklist/whitelist. This has no effect if filter.mode is set to none
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/fi/tkgwf/ruuvi/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public static void reload() {
public static void reload(final Function<String, File> configFileFinder) {
Config.configFileFinder = configFileFinder;
loadDefaults();
readConfig();
readTagNames();
readConfig();
}

private static void loadDefaults() {
Expand Down Expand Up @@ -253,6 +253,13 @@ private static Predicate<String> parseFilterMode(final Properties props) {
return (s) -> !FILTER_MACS.contains(s);
case "whitelist":
return FILTER_MACS::contains;
case "named":
if (TAG_NAMES.isEmpty()) {
throw new IllegalStateException(
"You have set filter.mode=named but left ruuvi-names.properties empty. " +
"Please select a different filter.mode value or populate ruuvi-names.properties.");
}
return TAG_NAMES.keySet()::contains;
}
}
return filterMode;
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/fi/tkgwf/ruuvi/config/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,22 @@ void testInfluxDbFieldFilter() {
assertTrue(predicate.test("temperature")); // Allowed
assertTrue(predicate.test("dewPoint")); // Allowed
}

@Test
void testparseFilterMode() {

assertTrue(Config.isAllowedMAC("AB12CD34EF56"));
assertTrue(Config.isAllowedMAC("XX12CD34EF56"));
assertTrue(Config.isAllowedMAC("ABCDEFG"));
assertFalse(Config.isAllowedMAC(null));

//Change to named
final Properties properties = new Properties();
properties.put("filter.mode", "named");
Config.readConfigFromProperties(properties);
assertTrue(Config.isAllowedMAC("AB12CD34EF56"));
assertFalse(Config.isAllowedMAC("XX12CD34EF56"));
assertFalse(Config.isAllowedMAC("ABCDEFG"));
assertFalse(Config.isAllowedMAC(null));
}
}

0 comments on commit 555f21a

Please sign in to comment.