From 555f21af74922b7749df3e6e5de55c0eb7ab9d20 Mon Sep 17 00:00:00 2001 From: Matthew Gardner <8501252+matthewgardner@users.noreply.github.com> Date: Mon, 20 May 2019 05:18:53 +0100 Subject: [PATCH] Issue26-Add support for filter.mode=named (#31) #26 Add support for filter.mode=named --- ruuvi-collector.properties.example | 3 ++- .../java/fi/tkgwf/ruuvi/config/Config.java | 9 ++++++++- .../java/fi/tkgwf/ruuvi/config/ConfigTest.java | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/ruuvi-collector.properties.example b/ruuvi-collector.properties.example index 3dd3260..8f2166f 100644 --- a/ruuvi-collector.properties.example +++ b/ruuvi-collector.properties.example @@ -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 diff --git a/src/main/java/fi/tkgwf/ruuvi/config/Config.java b/src/main/java/fi/tkgwf/ruuvi/config/Config.java index c785921..c00602b 100644 --- a/src/main/java/fi/tkgwf/ruuvi/config/Config.java +++ b/src/main/java/fi/tkgwf/ruuvi/config/Config.java @@ -82,8 +82,8 @@ public static void reload() { public static void reload(final Function configFileFinder) { Config.configFileFinder = configFileFinder; loadDefaults(); - readConfig(); readTagNames(); + readConfig(); } private static void loadDefaults() { @@ -253,6 +253,13 @@ private static Predicate 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; diff --git a/src/test/java/fi/tkgwf/ruuvi/config/ConfigTest.java b/src/test/java/fi/tkgwf/ruuvi/config/ConfigTest.java index 8874107..44f4e20 100644 --- a/src/test/java/fi/tkgwf/ruuvi/config/ConfigTest.java +++ b/src/test/java/fi/tkgwf/ruuvi/config/ConfigTest.java @@ -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)); + } }