EmailFormatter { public String formatBody(String bodyTemplateLocation, Event event) { VelocityContext velocityContext = new VelocityContext(); velocityContext.put("event", getEventAsData(event)); Template bodyTemplate; if (event.getProperty("body.template.location") != null) { bodyTemplate = velocityEngine.getTemplate(event.getProperty("body.template.location").toString()); } else if (bodyTemplateLocation != null) { bodyTemplate = velocityEngine.getTemplate(bodyTemplateLocation); } else { bodyTemplate = velocityEngine.getTemplate("/defaultBodyTemplate.vm"); } StringWriter writer = new StringWriter(); bodyTemplate.merge(velocityContext, writer); return writer.toString(); } EmailFormatter(); String formatSubject(String subjectTemplateLocation, Event event); String formatBody(String bodyTemplateLocation, Event event);  }
@Test public void testBodyDefaultTemplate() { String body = formatter.formatBody(null, createEvent()); System.out.println(body); Assert.assertTrue(body.contains("SEVERE alert: condition TestPattern alert")); Assert.assertTrue(body.contains("other : test")); Assert.assertTrue(body.contains("alertPattern : TestPattern")); Assert.assertTrue(body.contains("event.topics : foo")); Assert.assertTrue(body.contains("foo : bar")); Assert.assertTrue(body.contains("alertLevel : SEVERE")); }
@Test public void testBodyExternalTemplate() { String body = formatter.formatBody(this.getClass().getClassLoader().getResource("bodyTemplate.vm").getPath(), createEvent()); Assert.assertTrue(body.contains("<html>")); Assert.assertTrue(body.contains("<b>Hello bar !</b>")); Assert.assertTrue(body.contains("<td>other</td>")); Assert.assertTrue(body.contains("<td>test</td>")); Assert.assertTrue(body.contains("<td>event.topics</td>")); Assert.assertTrue(body.contains("<td>foo</td>")); }
@Test public void testBodyEventDefinedTemplate() { Map<String, Object> data = new HashMap<>(); data.put("body.template.location", this.getClass().getClassLoader().getResource("propertyBodyTemplate.vm").getPath()); data.put("test.name", "test"); data.put("threadCount", 100); Event event = new Event("foo", data); String body = formatter.formatBody(null, event); Assert.assertTrue(body.contains("This is test using property defined template.")); Assert.assertTrue(body.contains("It seems to work fine for 100")); }