VelocityTemplateParser { public void validateContext(Map<String, Object> context) throws MethodInvocationException { VelocityContext velocityContext = new VelocityContext(); for (Map.Entry<String, Object> entry : context.entrySet()) { velocityContext.put(entry.getKey(), entry.getValue()); } template.merge(velocityContext, new StringWriter()); } VelocityTemplateParser(String templateString); List<String> getReferenceNames(); Template getTemplate(); void validateContext(Map<String, Object> context);  }
@Test public void testValidateVelocityContext() { String templateString = "This alert ($category) was generated because $reason and $REASON from $source at $created_time"; VelocityTemplateParser parser = new VelocityTemplateParser(templateString); Map<String,Object> context = new HashMap<>(); context.put("category", "UNKNOWN"); context.put("reason", "timeout"); context.put("REASON", "IO error"); context.put("source","localhost"); context.put("created_time", "2016-11-30 05:52:47,053"); parser.validateContext(context); }
@Test(expected = MethodInvocationException.class) public void testValidateInvalidVelocityContext() { String templateString = "This alert ($category) was generated because $reason and $REASON from $source at $created_time"; VelocityTemplateParser parser = new VelocityTemplateParser(templateString); parser.validateContext(new HashMap<>()); }