ExceptionHandlingMailSendingTemplate { public MessageSendStatus send(){ final boolean useInstitutionalEmail = false; final Emailer emailer = new Emailer(getFromIdentity(), useInstitutionalEmail, getMailTemplate()); MessageSendStatus messageSendStatus = createSuccessfullSentEmailMessageStatus(); boolean sendEmailSuccess = false; try { sendEmailSuccess = doSend(emailer); } catch (final AddressException e) { messageSendStatus = handleAddressException(sendEmailSuccess); } catch (final SendFailedException e) { messageSendStatus = handleSendFailedException(e); } catch (final MessagingException e) { messageSendStatus = handleMessagingException(); } return messageSendStatus; }  MessageSendStatus send();  }
@Test public void testSendWithoutException() { ExceptionHandlingMailSendingTemplate doSendWithoutException = new ExceptionHandlingMailSendingTemplate() { @Override protected boolean doSend(Emailer emailer) throws AddressException, SendFailedException, MessagingException { assertNotNull("emailer was constructed", emailer); return true; } @Override protected MailTemplate getMailTemplate() { return emptyMailTemplate; }; @Override protected List<ContactList> getTheToContactLists() { return theContactLists; } @Override protected Identity getFromIdentity() { return theFromIdentity; } }; MessageSendStatus sendStatus = doSendWithoutException.send(); assertEquals(MessageSendStatusCode.SUCCESSFULL_SENT_EMAILS, sendStatus.getStatusCode()); assertTrue(sendStatus.getStatusCode().isSuccessfullSentMails()); assertFalse(sendStatus.isSeverityError()); assertFalse(sendStatus.isSeverityWarn()); assertTrue(sendStatus.canProceedWithWorkflow()); }
@Test public void shouldFailWithAddressExcpetion(){ ExceptionHandlingMailSendingTemplate doSendWithAddressException = new ExceptionHandlingMailSendingTemplate() { @Override protected boolean doSend(Emailer emailer) throws AddressException, SendFailedException, MessagingException { assertNotNull("emailer was constructed", emailer); throw new AddressException(); } @Override protected MailTemplate getMailTemplate() { return emptyMailTemplate; }; @Override protected List<ContactList> getTheToContactLists() { return theContactLists; } @Override protected Identity getFromIdentity() { return theFromIdentity; } }; MessageSendStatus sendStatus = doSendWithAddressException.send(); assertEquals(MessageSendStatusCode.SENDER_OR_RECIPIENTS_NOK_553, sendStatus.getStatusCode()); verifyStatusCodeIndicateAddressExceptionOnly(sendStatus); verifySendStatusIsWarn(sendStatus); assertFalse(sendStatus.canProceedWithWorkflow()); }
@Test public void shouldFailWithSendFailedExceptionWithDomainErrorCode553(){ ExceptionHandlingMailSendingTemplate doSendWithSendFailedException = new ExceptionHandlingMailSendingTemplate() { @Override protected boolean doSend(Emailer emailer) throws AddressException, SendFailedException, MessagingException { assertNotNull("emailer was constructed", emailer); MessagingException firstInnerException = new SendFailedException("553 some domain error message from the mailsystem"); throw new SendFailedException(BY_OLAT_UNHANDLED_TEXT, firstInnerException); } @Override protected MailTemplate getMailTemplate() { return emptyMailTemplate; }; @Override protected List<ContactList> getTheToContactLists() { return theContactLists; } @Override protected Identity getFromIdentity() { return theFromIdentity; } }; MessageSendStatus sendStatus = doSendWithSendFailedException.send(); assertEquals(MessageSendStatusCode.SEND_FAILED_DUE_INVALID_DOMAIN_NAME_553, sendStatus.getStatusCode()); verifyStatusCodeIndicateSendFailedOnly(sendStatus); verifySendStatusIsError(sendStatus); assertFalse(sendStatus.canProceedWithWorkflow()); }
@Test public void shouldFailWithSendFailedExceptionWithInvalidAddresses(){ ExceptionHandlingMailSendingTemplate doSendWithSendFailedException = new ExceptionHandlingMailSendingTemplate() { @Override protected boolean doSend(Emailer emailer) throws AddressException, SendFailedException, MessagingException { assertNotNull("emailer was constructed", emailer); MessagingException firstInnerException = new SendFailedException("Invalid Addresses <followed by a list of invalid addresses>"); throw new SendFailedException(BY_OLAT_UNHANDLED_TEXT, firstInnerException); } @Override protected MailTemplate getMailTemplate() { return emptyMailTemplate; }; @Override protected List<ContactList> getTheToContactLists() { return theContactLists; } @Override protected Identity getFromIdentity() { return theFromIdentity; } }; MessageSendStatus sendStatus = doSendWithSendFailedException.send(); assertEquals(MessageSendStatusCode.SEND_FAILED_DUE_INVALID_ADDRESSES_550, sendStatus.getStatusCode()); verifyStatusCodeIndicateSendFailedOnly(sendStatus); verifySendStatusIsError(sendStatus); assertFalse(sendStatus.canProceedWithWorkflow()); }
@Test public void shouldFailWithSendFailedExceptionBecauseNoRecipient(){ ExceptionHandlingMailSendingTemplate doSendWithSendFailedException = new ExceptionHandlingMailSendingTemplate() { @Override protected boolean doSend(Emailer emailer) throws AddressException, SendFailedException, MessagingException { assertNotNull("emailer was constructed", emailer); MessagingException firstInnerException = new SendFailedException("503 5.0.0 .... "); throw new SendFailedException(BY_OLAT_UNHANDLED_TEXT, firstInnerException); } @Override protected MailTemplate getMailTemplate() { return emptyMailTemplate; }; @Override protected List<ContactList> getTheToContactLists() { return theContactLists; } @Override protected Identity getFromIdentity() { return theFromIdentity; } }; MessageSendStatus sendStatus = doSendWithSendFailedException.send(); assertEquals(MessageSendStatusCode.SEND_FAILED_DUE_NO_RECIPIENTS_503, sendStatus.getStatusCode()); verifyStatusCodeIndicateSendFailedOnly(sendStatus); verifySendStatusIsError(sendStatus); assertFalse(sendStatus.canProceedWithWorkflow()); }
@Test public void shouldFailWithSendFailedExceptionBecauseUnknownSMTPHost(){ ExceptionHandlingMailSendingTemplate doSendWithSendFailedException = new ExceptionHandlingMailSendingTemplate() { @Override protected boolean doSend(Emailer emailer) throws AddressException, SendFailedException, MessagingException { assertNotNull("emailer was constructed", emailer); MessagingException firstInnerException = new SendFailedException("Unknown SMTP host"); throw new SendFailedException(BY_OLAT_UNHANDLED_TEXT, firstInnerException); } @Override protected MailTemplate getMailTemplate() { return emptyMailTemplate; }; @Override protected List<ContactList> getTheToContactLists() { return theContactLists; } @Override protected Identity getFromIdentity() { return theFromIdentity; } }; MessageSendStatus sendStatus = doSendWithSendFailedException.send(); assertEquals(MessageSendStatusCode.SEND_FAILED_DUE_UNKNOWN_SMTP_HOST, sendStatus.getStatusCode()); verifyStatusCodeIndicateSendFailedOnly(sendStatus); verifySendStatusIsError(sendStatus); assertTrue(sendStatus.canProceedWithWorkflow()); }
@Test public void shouldFailWithSendFailedExceptionBecauseCouldNotConnectToSMTPHost(){ ExceptionHandlingMailSendingTemplate doSendWithSendFailedException = new ExceptionHandlingMailSendingTemplate() { @Override protected boolean doSend(Emailer emailer) throws AddressException, SendFailedException, MessagingException { assertNotNull("emailer was constructed", emailer); MessagingException firstInnerException = new SendFailedException("Could not connect to SMTP host"); throw new SendFailedException(BY_OLAT_UNHANDLED_TEXT, firstInnerException); } @Override protected MailTemplate getMailTemplate() { return emptyMailTemplate; }; @Override protected List<ContactList> getTheToContactLists() { return theContactLists; } @Override protected Identity getFromIdentity() { return theFromIdentity; } }; MessageSendStatus sendStatus = doSendWithSendFailedException.send(); assertEquals(MessageSendStatusCode.SEND_FAILED_DUE_COULD_NOT_CONNECT_TO_SMTP_HOST, sendStatus.getStatusCode()); verifyStatusCodeIndicateSendFailedOnly(sendStatus); verifySendStatusIsError(sendStatus); assertTrue(sendStatus.canProceedWithWorkflow()); }
@Test public void shouldFailWithSendFailedExceptionWithAnAuthenticationFailedException(){ ExceptionHandlingMailSendingTemplate doSendWithSendFailedException = new ExceptionHandlingMailSendingTemplate() { @Override protected boolean doSend(Emailer emailer) throws AddressException, SendFailedException, MessagingException { assertNotNull("emailer was constructed", emailer); MessagingException firstInnerException = new AuthenticationFailedException("<some authentication failed message from the mailsystem>"); throw new SendFailedException(BY_OLAT_UNHANDLED_TEXT, firstInnerException); } @Override protected MailTemplate getMailTemplate() { return emptyMailTemplate; }; @Override protected List<ContactList> getTheToContactLists() { return theContactLists; } @Override protected Identity getFromIdentity() { return theFromIdentity; } }; MessageSendStatus sendStatus = doSendWithSendFailedException.send(); assertEquals(MessageSendStatusCode.SMTP_AUTHENTICATION_FAILED, sendStatus.getStatusCode()); verifyStatusCodeIndicateSendFailedOnly(sendStatus); verifySendStatusIsError(sendStatus); assertTrue(sendStatus.canProceedWithWorkflow()); }
@Test public void shouldFailWithOLATRuntimeExceptionWithAnUnhandletSendFailedException(){ ExceptionHandlingMailSendingTemplate doSendWithSendFailedException = new ExceptionHandlingMailSendingTemplate() { @Override protected boolean doSend(Emailer emailer) throws AddressException, SendFailedException, MessagingException { assertNotNull("emailer was constructed", emailer); MessagingException firstInnerException = new SendFailedException("unhandled sendfail exception"); throw new SendFailedException(BY_OLAT_UNHANDLED_TEXT, firstInnerException); } @Override protected MailTemplate getMailTemplate() { return emptyMailTemplate; }; @Override protected List<ContactList> getTheToContactLists() { return theContactLists; } @Override protected Identity getFromIdentity() { return theFromIdentity; } }; try{ MessageSendStatus sendStatus = doSendWithSendFailedException.send(); fail("OLATRuntime exception is not thrown."); }catch(OLATRuntimeException ore){ assertNotNull(ore); } }