EmailJobErrorHandler implements JobErrorHandler { @Override public void handleException(final String jobName, final Throwable cause) { String errorContext = createErrorContext(jobName, cause); try { sendMessage(createMessage(errorContext)); } catch (final MessagingException ex) { log.error("Elastic job: email job handler error", ex); } } EmailJobErrorHandler(); @Override void handleException(final String jobName, final Throwable cause); @Override String getType(); static final String CONFIG_PREFIX; }
@Test public void assertHandleExceptionWithYAMLConfiguration() throws ReflectiveOperationException { EmailJobErrorHandler emailJobErrorHandler = getEmailJobErrorHandler(); emailJobErrorHandler.handleException("test job name", new RuntimeException("test exception")); Field field = emailJobErrorHandler.getClass().getDeclaredField("config"); field.setAccessible(true); EmailConfiguration config = (EmailConfiguration) field.get(emailJobErrorHandler); assertNotNull(config); assertThat(config.getHost(), equalTo("yaml.email.com")); assertThat(config.getPort(), equalTo(123)); assertThat(config.getUsername(), equalTo("yaml.username")); assertThat(config.getFrom(), equalTo("yaml.from@ejob.com")); assertThat(config.getTo(), equalTo("yaml.to@ejob.com")); assertThat(config.getBcc(), equalTo("yaml.bcc@ejob.com")); assertThat(config.getProtocol(), equalTo("yaml.smtp")); assertThat(config.getSubject(), equalTo("yaml.subject")); assertTrue(config.isUseSsl()); assertTrue(config.isDebug()); }
@Test public void assertHandleExceptionWithSession() throws ReflectiveOperationException { EmailJobErrorHandler emailJobErrorHandler = getEmailJobErrorHandler(); emailJobErrorHandler.handleException("test job name", new RuntimeException("test exception")); Field field = emailJobErrorHandler.getClass().getDeclaredField("session"); field.setAccessible(true); Session session = (Session) field.get(emailJobErrorHandler); assertNotNull(session); assertThat(session.getProperties().get("mail.smtp.host"), equalTo("yaml.email.com")); assertThat(session.getProperties().get("mail.debug"), equalTo("true")); assertThat(session.getProperties().get("mail.smtp.port"), equalTo(123)); assertThat(session.getProperties().get("mail.transport.protocol"), equalTo("yaml.smtp")); assertThat(session.getProperties().get("mail.smtp.auth"), equalTo("true")); }