@Test public void testTemplateNotFound() throws MessagingException { UserActionRequest rqst = new UserActionRequest("SomeOther" + ACTION_NAME, null, params); mockery.checking(new Expectations() { { allowing(exceptionSanitizer).transform(actionException); will(returnValue(cleanException)); allowing(emailerFactory).createMessage(); will(returnValue(response)); oneOf(emailerFactory).setTo(response, USER_EMAIL); oneOf(emailerFactory).setSubject(with(same(response)), with(any(String.class))); oneOf(emailerFactory).setTextBody(with(same(response)), with(any(String.class))); allowing(emailerFactory).setHtmlBody(with(same(response)), with(any(String.class))); oneOf(emailerFactory).addAttachmentMessage(response, message); } }); sut.reply(message, user, rqst, actionException, responseMessages); mockery.assertIsSatisfied(); assertEquals(1, responseMessages.size()); assertSame(response, responseMessages.get(0)); }
public void reply(final Message message, final PersonModelView user, final UserActionRequest actionSelection, final Exception inException, final List<Message> inResponseMessages) { final String actionName = actionSelection.getActionKey(); try { Exception cleanException = exceptionSanitizer.transform(inException); MimeMessage response = emailerFactory.createMessage(); emailerFactory.setTo(response, user.getEmail()); EmailTemplate template = errorMessageTemplates.get(actionName); if (template == null) { log.warn("Missing template for error response message for action {}. Sending generic response.", actionName); emailerFactory.setSubject(response, "Error processing received email"); emailerFactory.setTextBody(response, "There was an error processing your email. " + cleanException.getMessage() + " Original message is attached."); } else { Context velocityContext = new VelocityContext(velocityGlobalContext); velocityContext.put("action", actionName); velocityContext.put("params", actionSelection.getParams()); velocityContext.put("user", user); velocityContext.put("exception", cleanException); velocityContext.put("originalException", inException); velocityContext.put("settings", systemSettingsDao.execute(null)); StringWriter writer = new StringWriter(); velocityEngine.evaluate(velocityContext, writer, "EmailSubject-" + actionName, template.getSubjectTemplate()); emailerFactory.setSubject(response, writer.toString()); Template vt = velocityEngine.getTemplate(template.getTextBodyTemplateResourcePath()); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); emailerFactory.setTextBody(response, writer.toString()); vt = velocityEngine.getTemplate(template.getHtmlBodyTemplateResourcePath()); EventCartridge ec = new EventCartridge(); ec.addEventHandler(new EscapeHtmlReference()); ec.attachToContext(velocityContext); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); emailerFactory.setHtmlBody(response, writer.toString()); } emailerFactory.addAttachmentMessage(response, message); inResponseMessages.add(response); } catch (Exception ex) { log.error("Error building error response message for failed action '{}'.", actionName, ex); } }
MessageReplier { public void reply(final Message message, final PersonModelView user, final UserActionRequest actionSelection, final Exception inException, final List<Message> inResponseMessages) { final String actionName = actionSelection.getActionKey(); try { Exception cleanException = exceptionSanitizer.transform(inException); MimeMessage response = emailerFactory.createMessage(); emailerFactory.setTo(response, user.getEmail()); EmailTemplate template = errorMessageTemplates.get(actionName); if (template == null) { log.warn("Missing template for error response message for action {}. Sending generic response.", actionName); emailerFactory.setSubject(response, "Error processing received email"); emailerFactory.setTextBody(response, "There was an error processing your email. " + cleanException.getMessage() + " Original message is attached."); } else { Context velocityContext = new VelocityContext(velocityGlobalContext); velocityContext.put("action", actionName); velocityContext.put("params", actionSelection.getParams()); velocityContext.put("user", user); velocityContext.put("exception", cleanException); velocityContext.put("originalException", inException); velocityContext.put("settings", systemSettingsDao.execute(null)); StringWriter writer = new StringWriter(); velocityEngine.evaluate(velocityContext, writer, "EmailSubject-" + actionName, template.getSubjectTemplate()); emailerFactory.setSubject(response, writer.toString()); Template vt = velocityEngine.getTemplate(template.getTextBodyTemplateResourcePath()); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); emailerFactory.setTextBody(response, writer.toString()); vt = velocityEngine.getTemplate(template.getHtmlBodyTemplateResourcePath()); EventCartridge ec = new EventCartridge(); ec.addEventHandler(new EscapeHtmlReference()); ec.attachToContext(velocityContext); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); emailerFactory.setHtmlBody(response, writer.toString()); } emailerFactory.addAttachmentMessage(response, message); inResponseMessages.add(response); } catch (Exception ex) { log.error("Error building error response message for failed action '{}'.", actionName, ex); } } }
MessageReplier { public void reply(final Message message, final PersonModelView user, final UserActionRequest actionSelection, final Exception inException, final List<Message> inResponseMessages) { final String actionName = actionSelection.getActionKey(); try { Exception cleanException = exceptionSanitizer.transform(inException); MimeMessage response = emailerFactory.createMessage(); emailerFactory.setTo(response, user.getEmail()); EmailTemplate template = errorMessageTemplates.get(actionName); if (template == null) { log.warn("Missing template for error response message for action {}. Sending generic response.", actionName); emailerFactory.setSubject(response, "Error processing received email"); emailerFactory.setTextBody(response, "There was an error processing your email. " + cleanException.getMessage() + " Original message is attached."); } else { Context velocityContext = new VelocityContext(velocityGlobalContext); velocityContext.put("action", actionName); velocityContext.put("params", actionSelection.getParams()); velocityContext.put("user", user); velocityContext.put("exception", cleanException); velocityContext.put("originalException", inException); velocityContext.put("settings", systemSettingsDao.execute(null)); StringWriter writer = new StringWriter(); velocityEngine.evaluate(velocityContext, writer, "EmailSubject-" + actionName, template.getSubjectTemplate()); emailerFactory.setSubject(response, writer.toString()); Template vt = velocityEngine.getTemplate(template.getTextBodyTemplateResourcePath()); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); emailerFactory.setTextBody(response, writer.toString()); vt = velocityEngine.getTemplate(template.getHtmlBodyTemplateResourcePath()); EventCartridge ec = new EventCartridge(); ec.addEventHandler(new EscapeHtmlReference()); ec.attachToContext(velocityContext); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); emailerFactory.setHtmlBody(response, writer.toString()); } emailerFactory.addAttachmentMessage(response, message); inResponseMessages.add(response); } catch (Exception ex) { log.error("Error building error response message for failed action '{}'.", actionName, ex); } } MessageReplier(final EmailerFactory inEmailerFactory, final VelocityEngine inVelocityEngine,
            final Context inVelocityGlobalContext, final Transformer<Exception, Exception> inExceptionSanitizer,
            final DomainMapper< ? extends Serializable, SystemSettings> inSystemSettingsDao,
            final Map<String, EmailTemplate> inErrorMessageTemplates); }
MessageReplier { public void reply(final Message message, final PersonModelView user, final UserActionRequest actionSelection, final Exception inException, final List<Message> inResponseMessages) { final String actionName = actionSelection.getActionKey(); try { Exception cleanException = exceptionSanitizer.transform(inException); MimeMessage response = emailerFactory.createMessage(); emailerFactory.setTo(response, user.getEmail()); EmailTemplate template = errorMessageTemplates.get(actionName); if (template == null) { log.warn("Missing template for error response message for action {}. Sending generic response.", actionName); emailerFactory.setSubject(response, "Error processing received email"); emailerFactory.setTextBody(response, "There was an error processing your email. " + cleanException.getMessage() + " Original message is attached."); } else { Context velocityContext = new VelocityContext(velocityGlobalContext); velocityContext.put("action", actionName); velocityContext.put("params", actionSelection.getParams()); velocityContext.put("user", user); velocityContext.put("exception", cleanException); velocityContext.put("originalException", inException); velocityContext.put("settings", systemSettingsDao.execute(null)); StringWriter writer = new StringWriter(); velocityEngine.evaluate(velocityContext, writer, "EmailSubject-" + actionName, template.getSubjectTemplate()); emailerFactory.setSubject(response, writer.toString()); Template vt = velocityEngine.getTemplate(template.getTextBodyTemplateResourcePath()); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); emailerFactory.setTextBody(response, writer.toString()); vt = velocityEngine.getTemplate(template.getHtmlBodyTemplateResourcePath()); EventCartridge ec = new EventCartridge(); ec.addEventHandler(new EscapeHtmlReference()); ec.attachToContext(velocityContext); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); emailerFactory.setHtmlBody(response, writer.toString()); } emailerFactory.addAttachmentMessage(response, message); inResponseMessages.add(response); } catch (Exception ex) { log.error("Error building error response message for failed action '{}'.", actionName, ex); } } MessageReplier(final EmailerFactory inEmailerFactory, final VelocityEngine inVelocityEngine,
            final Context inVelocityGlobalContext, final Transformer<Exception, Exception> inExceptionSanitizer,
            final DomainMapper< ? extends Serializable, SystemSettings> inSystemSettingsDao,
            final Map<String, EmailTemplate> inErrorMessageTemplates); void reply(final Message message, final PersonModelView user, final UserActionRequest actionSelection,
            final Exception inException, final List<Message> inResponseMessages); }
MessageReplier { public void reply(final Message message, final PersonModelView user, final UserActionRequest actionSelection, final Exception inException, final List<Message> inResponseMessages) { final String actionName = actionSelection.getActionKey(); try { Exception cleanException = exceptionSanitizer.transform(inException); MimeMessage response = emailerFactory.createMessage(); emailerFactory.setTo(response, user.getEmail()); EmailTemplate template = errorMessageTemplates.get(actionName); if (template == null) { log.warn("Missing template for error response message for action {}. Sending generic response.", actionName); emailerFactory.setSubject(response, "Error processing received email"); emailerFactory.setTextBody(response, "There was an error processing your email. " + cleanException.getMessage() + " Original message is attached."); } else { Context velocityContext = new VelocityContext(velocityGlobalContext); velocityContext.put("action", actionName); velocityContext.put("params", actionSelection.getParams()); velocityContext.put("user", user); velocityContext.put("exception", cleanException); velocityContext.put("originalException", inException); velocityContext.put("settings", systemSettingsDao.execute(null)); StringWriter writer = new StringWriter(); velocityEngine.evaluate(velocityContext, writer, "EmailSubject-" + actionName, template.getSubjectTemplate()); emailerFactory.setSubject(response, writer.toString()); Template vt = velocityEngine.getTemplate(template.getTextBodyTemplateResourcePath()); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); emailerFactory.setTextBody(response, writer.toString()); vt = velocityEngine.getTemplate(template.getHtmlBodyTemplateResourcePath()); EventCartridge ec = new EventCartridge(); ec.addEventHandler(new EscapeHtmlReference()); ec.attachToContext(velocityContext); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); emailerFactory.setHtmlBody(response, writer.toString()); } emailerFactory.addAttachmentMessage(response, message); inResponseMessages.add(response); } catch (Exception ex) { log.error("Error building error response message for failed action '{}'.", actionName, ex); } } MessageReplier(final EmailerFactory inEmailerFactory, final VelocityEngine inVelocityEngine,
            final Context inVelocityGlobalContext, final Transformer<Exception, Exception> inExceptionSanitizer,
            final DomainMapper< ? extends Serializable, SystemSettings> inSystemSettingsDao,
            final Map<String, EmailTemplate> inErrorMessageTemplates); void reply(final Message message, final PersonModelView user, final UserActionRequest actionSelection,
            final Exception inException, final List<Message> inResponseMessages);  }