LambdaWithConfig { String getConfig(String key, String defaultValue) { return cache.computeIfAbsent(key, __ -> { String value = node().has(key) ? node.get(key).asText(): defaultValue; if (value == null) { String msg = configFile + " is missing key: " + key; LOGGER.error(msg); throw new IllegalStateException(msg); } else { LOGGER.info("Reading key {}, default {} got value {}", key, defaultValue, value); return value; } }); } LambdaWithConfig(String configFile); static String readConfig(String key); static String readConfig(String key, String defaultValue);  }
@Test(expected = IllegalStateException.class) public void testMissingFile() { new LambdaWithConfig("missingConfig.json").getConfig("foo", null); }
@Test(expected = IllegalStateException.class) public void testMissingKey() { new LambdaWithConfig("test-config.json").getConfig("missing", null); }
@Test public void testKey() { assertThat(new LambdaWithConfig("test-config.json").getConfig("foo", null)).isEqualTo("bar"); }
@Test public void testDefaultValue() { assertThat(new LambdaWithConfig("test-config.json").getConfig("missing", "default")).isEqualTo("default"); }