Search in sources :

Example 1 with ByteArrayDataSource

use of org.apache.commons.mail.ByteArrayDataSource in project acs-aem-commons by Adobe-Consulting-Services.

the class EmailServiceImplTest method testSendEmailAttachment.

@Test
public final void testSendEmailAttachment() throws Exception {
    final String expectedMessage = "This is just a message";
    final String expectedSenderName = "John Smith";
    final String expectedSenderEmailAddress = "john@smith.com";
    String attachment = "This is a attachment.";
    String attachmentName = "attachment.txt";
    // Subject is provided inside the HtmlTemplate directly
    final String expectedSubject = "Greetings";
    final Map<String, String> params = new HashMap<String, String>();
    params.put("message", expectedMessage);
    params.put("senderName", expectedSenderName);
    params.put("senderEmailAddress", expectedSenderEmailAddress);
    final String recipient = "upasanac@acs.com";
    Map<String, DataSource> attachments = new HashMap();
    attachments.put(attachmentName, new ByteArrayDataSource(attachment, "text/plain"));
    ArgumentCaptor<HtmlEmail> captor = ArgumentCaptor.forClass(HtmlEmail.class);
    List<String> failureList = emailService.sendEmail(emailTemplateAttachmentPath, params, attachments, recipient);
    verify(messageGatewayHtmlEmail, times(1)).send(captor.capture());
    assertEquals(expectedSenderEmailAddress, captor.getValue().getFromAddress().getAddress());
    assertEquals(expectedSenderName, captor.getValue().getFromAddress().getPersonal());
    assertEquals(expectedSubject, captor.getValue().getSubject());
    assertEquals(recipient, captor.getValue().getToAddresses().get(0).toString());
    Method getContainer = captor.getValue().getClass().getSuperclass().getDeclaredMethod("getContainer");
    getContainer.setAccessible(true);
    MimeMultipart mimeMultipart = (MimeMultipart) getContainer.invoke(captor.getValue());
    getContainer.setAccessible(false);
    assertEquals(attachment, mimeMultipart.getBodyPart(0).getContent().toString());
    // If email is sent to the recipient successfully, the response is an empty failureList
    assertTrue(failureList.isEmpty());
}
Also used : HashMap(java.util.HashMap) MimeMultipart(javax.mail.internet.MimeMultipart) HtmlEmail(org.apache.commons.mail.HtmlEmail) Method(java.lang.reflect.Method) ByteArrayDataSource(org.apache.commons.mail.ByteArrayDataSource) DataSource(javax.activation.DataSource) ByteArrayDataSource(org.apache.commons.mail.ByteArrayDataSource) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 DataSource (javax.activation.DataSource)1 MimeMultipart (javax.mail.internet.MimeMultipart)1 ByteArrayDataSource (org.apache.commons.mail.ByteArrayDataSource)1 HtmlEmail (org.apache.commons.mail.HtmlEmail)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1