Search in sources :

Example 1 with VelocityTemplateParsingException

use of org.motechproject.security.exception.VelocityTemplateParsingException in project motech by motech.

the class EmailSenderImplTest method shouldNotSendPasswordResetReminderIfParsingThrowsException.

@Test
public void shouldNotSendPasswordResetReminderIfParsingThrowsException() throws Exception {
    when(templateParser.mergeTemplateIntoString(matches(PASSWORD_CHANGE_REMINDER_TEMPLATE), eq(params))).thenThrow(new VelocityTemplateParsingException("Couldn't send password reminder email", null));
    emailSender.sendPasswordResetReminder(params);
    verify(templateParser).mergeTemplateIntoString(matches(PASSWORD_CHANGE_REMINDER_TEMPLATE), eq(params));
    verify(eventRelay, never()).sendEventMessage(any(MotechEvent.class));
}
Also used : MotechEvent(org.motechproject.event.MotechEvent) VelocityTemplateParsingException(org.motechproject.security.exception.VelocityTemplateParsingException) Test(org.junit.Test)

Example 2 with VelocityTemplateParsingException

use of org.motechproject.security.exception.VelocityTemplateParsingException in project motech by motech.

the class VelocityTemplateParser method mergeTemplateIntoString.

public String mergeTemplateIntoString(String templateFilename, Map<String, Object> params) throws VelocityTemplateParsingException {
    try (InputStream is = settingsFacade.getRawConfig(templateFilename)) {
        StringReader reader = new StringReader(IOUtils.toString(is));
        SimpleNode node = rs.parse(reader, templateFilename);
        Template template = new Template();
        template.setRuntimeServices(rs);
        template.setData(node);
        template.initDocument();
        StringWriter writer = new StringWriter();
        template.merge(new VelocityContext(params), writer);
        return writer.toString();
    } catch (ParseException | IOException e) {
        throw new VelocityTemplateParsingException("Couldn't merge template into string", e);
    }
}
Also used : StringWriter(java.io.StringWriter) InputStream(java.io.InputStream) VelocityContext(org.apache.velocity.VelocityContext) StringReader(java.io.StringReader) ParseException(org.apache.velocity.runtime.parser.ParseException) IOException(java.io.IOException) SimpleNode(org.apache.velocity.runtime.parser.node.SimpleNode) Template(org.apache.velocity.Template) VelocityTemplateParsingException(org.motechproject.security.exception.VelocityTemplateParsingException)

Aggregations

VelocityTemplateParsingException (org.motechproject.security.exception.VelocityTemplateParsingException)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Template (org.apache.velocity.Template)1 VelocityContext (org.apache.velocity.VelocityContext)1 ParseException (org.apache.velocity.runtime.parser.ParseException)1 SimpleNode (org.apache.velocity.runtime.parser.node.SimpleNode)1 Test (org.junit.Test)1 MotechEvent (org.motechproject.event.MotechEvent)1