MailerWithTemplate { public MailerResult sendMailAsSeparateMails(List<? extends OLATPrincipal> recipientsTO, List<? extends OLATPrincipal> recipientsCC, List<? extends OLATPrincipal> recipientsBCC, MailTemplate template, OLATPrincipal sender) { MailerResult result = new MailerResult(); if (webappAndMailHelper.getMailhost() == null) { result.setReturnCode(MailerResult.MAILHOST_UNDEFINED); log.warn("sendMailAsSeparateMails - webappAndMailHelper.getMailhost(): " + webappAndMailHelper.getMailhost()); return result; } boolean isMailSendToRecipient = false; if (recipientsTO != null) { for (OLATPrincipal recipient : recipientsTO) { VelocityContext context = new VelocityContext(); template.putVariablesInMailContext(context, recipient); sendWithContext(context, recipient, null, null, template, sender, result); if (isMailSentToRecipient(result, recipient)) { isMailSendToRecipient = true; } } } if (recipientsCC != null) { for (OLATPrincipal recipient : recipientsCC) { List<OLATPrincipal> cc = new ArrayList<OLATPrincipal>(); cc.add(recipient); VelocityContext context = new VelocityContext(); template.putVariablesInMailContext(context, recipient); sendWithContext(context, null, cc, null, template, sender, result); if (isMailSentToRecipient(result, recipient)) { isMailSendToRecipient = true; } } } if (recipientsBCC != null) { for (OLATPrincipal recipient : recipientsBCC) { VelocityContext context = new VelocityContext(); template.putVariablesInMailContext(context, recipient); sendWithContext(context, recipient, null, null, template, sender, result); if (isMailSentToRecipient(result, recipient)) { isMailSendToRecipient = true; } } } if (!isMailSendToRecipient) { if (result.getReturnCode() == MailerResult.OK) { result.setReturnCode(MailerResult.RECIPIENT_ADDRESS_ERROR); } log.warn("sendMailAsSeparateMails - result.getReturnCode(): " + result.getReturnCode() + " . See MailerResult for return codes."); } return result; } private  MailerWithTemplate(); private  MailerWithTemplate(MailPackageStaticDependenciesWrapper webappAndMailHelper); static MailerWithTemplate getInstance(); MailerResult sendMailUsingTemplateContext(OLATPrincipal recipientTO, List<? extends OLATPrincipal> recipientsCC, List<? extends OLATPrincipal> recipientsBCC,
            MailTemplate template, OLATPrincipal sender); String[] previewSubjectAndBody(OLATPrincipal recipientTO, List<? extends OLATPrincipal> recipientsCC, List<? extends OLATPrincipal> recipientsBCC,
            MailTemplate template, OLATPrincipal sender); MailerResult sendMail(OLATPrincipal recipientTO, List<? extends OLATPrincipal> recipientsCC, List<? extends OLATPrincipal> recipientsBCC,
            MailTemplate template, OLATPrincipal sender); MailerResult sendMailAsSeparateMails(List<? extends OLATPrincipal> recipientsTO, List<? extends OLATPrincipal> recipientsCC,
            List<? extends OLATPrincipal> recipientsBCC, MailTemplate template, OLATPrincipal sender);  }
@Test public void testEachRecipientOnToForSendMailAsSeparateMails() { MailerResult result = null; int numberOfSeparateMails = recipients.size(); result = mailerWithTemplate.sendMailAsSeparateMails(recipients, null, null, template, ruedi); assertEquals(MailerResult.OK, result.getReturnCode()); verifyTimesCallingCreateAndSendMessageInvocationsFor(numberOfSeparateMails, recipients, null, null); }
@Test public void testEachRecipientOnToAndCCForSendMailAsSeparateMails() { MailerResult result = new MailerResult(); int numberOfSeparateMails = recipients.size(); numberOfSeparateMails += recipientsCC.size(); result = mailerWithTemplate.sendMailAsSeparateMails(recipients, recipientsCC, null, template, ruedi); assertEquals(MailerResult.OK, result.getReturnCode()); verifyTimesCallingCreateAndSendMessageInvocationsFor(numberOfSeparateMails, recipients, recipientsCC, null); }
@Test public void testEachRecipientOnToAndBCCForSendMailAsSeparateMails() { MailerResult result = new MailerResult(); int numberOfSeparateMails = recipients.size(); numberOfSeparateMails += recipientsBCC.size(); result = mailerWithTemplate.sendMailAsSeparateMails(recipients, null, recipientsBCC, template, ruedi); assertEquals(MailerResult.OK, result.getReturnCode()); verifyTimesCallingCreateAndSendMessageInvocationsFor(numberOfSeparateMails, recipients, null, recipientsBCC); }
@Test public void testWithoutMailhostConfiguredTheSendAsSeparateMailsFails() { when(webappAndMailhelperMock.getMailhost()).thenReturn(null); MailerResult result = mailerWithTemplate.sendMailAsSeparateMails(recipients, null, null, template, ruedi); assertEquals(MailerResult.MAILHOST_UNDEFINED, result.getReturnCode()); }
@Test public void testForRecipientAddressErrorIfNoRecipientsProvidedForSendAsSeparateMails() { MailerResult result = mailerWithTemplate.sendMailAsSeparateMails(null, null, null, template, ruedi); assertEquals(MailerResult.RECIPIENT_ADDRESS_ERROR, result.getReturnCode()); verifyTimesCallingCreateAndSendMessageInvocationsFor(0, null, null, null); }
@Test public void testForRecipientAddressErrorIfInvalidRecipientProvidedForSendAsSeparateMails() { OLATPrincipal invalidRecipient = ObjectMother.createInvalidPrincipal(); List<OLATPrincipal> invalidRecipients = new ArrayList<OLATPrincipal>(); invalidRecipients.add(invalidRecipient); MailerResult result = mailerWithTemplate.sendMailAsSeparateMails(invalidRecipients, null, null, template, ruedi); assertEquals(MailerResult.RECIPIENT_ADDRESS_ERROR, result.getReturnCode()); verifyTimesCallingCreateAndSendMessageInvocationsFor(0, null, null, null); }
@Test public void testForRecipientAddressErrorIfInvalidSenderProvidedForSendAsSeparateMails() { OLATPrincipal invalidRecipient = ObjectMother.createInvalidPrincipal(); List<OLATPrincipal> invalidRecipients = new ArrayList<OLATPrincipal>(); invalidRecipients.add(invalidRecipient); MailerResult result = mailerWithTemplate.sendMailAsSeparateMails(invalidRecipients, null, null, template, ruedi); assertEquals(MailerResult.RECIPIENT_ADDRESS_ERROR, result.getReturnCode()); verifyTimesCallingCreateAndSendMessageInvocationsFor(0, null, null, null); }
@Test public void testValidAndInvalidPrincipalsInRecipientToForSendAsSeparateMails() { OLATPrincipal invalidRecipient = ObjectMother.createInvalidPrincipal(); recipients.add(invalidRecipient); int numberOfSeparateMails = recipients.size(); MailerResult result = new MailerResult(); result = mailerWithTemplate.sendMailAsSeparateMails(recipients, null, recipientsBCC, template, ruedi); assertEquals(MailerResult.OK, result.getReturnCode()); assertNotNull("failed email must be reported, hence not null", result.getFailedIdentites()); assertEquals(1, result.getFailedIdentites().size()); verifyTimesCallingCreateAndSendMessageInvocationsFor(numberOfSeparateMails, recipients, null, recipientsBCC); }
@Test public void testValidRecipientsButInvalidSenderForSendAsSeparateMails() { OLATPrincipal invalidSender = ObjectMother.createInvalidPrincipal(); recipients.add(invalidSender); MailerResult result = new MailerResult(); result = mailerWithTemplate.sendMailAsSeparateMails(recipients, null, recipientsBCC, template, invalidSender); assertEquals(MailerResult.SENDER_ADDRESS_ERROR, result.getReturnCode()); verifyTimesCallingCreateAndSendMessageInvocationsFor(0, null, null, null); }
@Test public void testValidToAndInvalidCCAndInvalidBCCForSendAsSeparateMails() { OLATPrincipal invalidPrincipal = ObjectMother.createInvalidPrincipal(); recipientsCC.add(invalidPrincipal); recipientsBCC.add(invalidPrincipal); int failedIdentities = 2; int numberOfSeparateMails = recipients.size(); numberOfSeparateMails += recipientsCC.size(); numberOfSeparateMails += recipientsBCC.size() - 1; MailerResult result = new MailerResult(); result = mailerWithTemplate.sendMailAsSeparateMails(recipients, recipientsCC, recipientsBCC, template, ruedi); assertEquals(MailerResult.OK, result.getReturnCode()); assertNotNull("failed email must be reported, hence not null", result.getFailedIdentites()); assertEquals(failedIdentities, result.getFailedIdentites().size()); verifyTimesCallingCreateAndSendMessageInvocationsFor(numberOfSeparateMails, recipients, recipientsBCC, recipientsBCC); }