Loader { static public List<Rule> load(Dictionary<String, Object> configuration) { List<Rule> rules = new ArrayList<>(); if (configuration == null) { return rules; } Enumeration<String> keys = configuration.keys(); while (keys.hasMoreElements()) { String key = keys.nextElement(); if (key.startsWith("rule.")) { Rule rule = new Rule(); rule.setName(key.substring("rule.".length())); String ruleDefinition = (String) configuration.get(key); ruleDefinition = ruleDefinition.replaceAll("'", "\""); if (ruleDefinition != null && !ruleDefinition.isEmpty()) { JsonReader jsonReader = Json.createReader(new StringReader(ruleDefinition)); JsonObject jsonObject = jsonReader.readObject(); if (jsonObject.isNull("condition")) { LOGGER.error("Can't load rule {} as condition is null", ruleDefinition); } else { rule.setCondition(jsonObject.getString("condition")); if (jsonObject.get("period") == null) { rule.setPeriod(null); } else { rule.setPeriod(jsonObject.getString("period")); } if (jsonObject.get("level") == null) { rule.setLevel("WARN"); } else { rule.setLevel(jsonObject.getString("level")); } if (jsonObject.get("recoverable") == null) { rule.setRecoverable(false); } else { rule.setRecoverable(jsonObject.getBoolean("recoverable")); } rules.add(rule); } } } } return rules; }  static List<Rule> load(Dictionary<String, Object> configuration); static Long oldestPeriod(List<Rule> rules);  }
@Test public void testLoad() throws Exception { List<Rule> rules = Loader.load(null); Assert.assertEquals(0, rules.size()); Dictionary<String, Object> configuration = new Hashtable<>(); rules = Loader.load(configuration); Assert.assertEquals(0, rules.size()); configuration.put("foo", "bar"); rules = Loader.load(configuration); Assert.assertEquals(0, rules.size()); configuration.put("rule.1", "{\"condition\":\"foo:bar AND @date:[X TO Y]\",\"period\":\"1MINUTE\",\"level\":\"CRITICAL\",\"recoverable\":true}"); configuration.put("rule.2", "{\"condition\":\"foo:bar AND NOT other:te*\",\"period\":\"1HOUR\",\"level\":\"CRITICAL\",\"recoverable\":false}"); configuration.put("rule.3", "{\"condition\":\"threadCount:[0 TO 200]\",\"level\":\"SEVERE\"}"); rules = Loader.load(configuration); Assert.assertEquals(3, rules.size()); Rule rule1 = rules.get(2); Assert.assertEquals("1", rule1.getName()); Assert.assertEquals("foo:bar AND @date:[X TO Y]", rule1.getCondition()); Assert.assertEquals("1MINUTE", rule1.getPeriod()); Assert.assertEquals("CRITICAL", rule1.getLevel()); Assert.assertEquals(true, rule1.isRecoverable()); Rule rule2 = rules.get(1); Assert.assertEquals("2", rule2.getName()); Assert.assertEquals("foo:bar AND NOT other:te*", rule2.getCondition()); Assert.assertEquals("1HOUR", rule2.getPeriod()); Assert.assertEquals("CRITICAL", rule2.getLevel()); Assert.assertEquals(false, rule2.isRecoverable()); Rule rule3 = rules.get(0); Assert.assertEquals("3", rule3.getName()); Assert.assertEquals("threadCount:[0 TO 200]", rule3.getCondition()); Assert.assertNull(rule3.getPeriod()); Assert.assertEquals("SEVERE", rule3.getLevel()); Assert.assertEquals(false, rule3.isRecoverable()); configuration = new Hashtable<>(); configuration.put("rule.empty", ""); rules = Loader.load(configuration); Assert.assertEquals(0, rules.size()); }