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());
}
Aggregations