From d624902cfdbf2d1ce7c28814c924b0867aa8d77e Mon Sep 17 00:00:00 2001 From: Jonjo McKay Date: Mon, 25 Jan 2016 13:03:42 +0000 Subject: [PATCH] Issue #1: Add support for nested multipart messages --- .../wiser/assertions/WiserAssertions.java | 8 +++++++- .../wiser/assertions/AbstractWiserTest.java | 1 + .../kemitix/wiser/assertions/Issue1Test.java | 18 +++++++++--------- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java index a2fc2b6..4b74ade 100644 --- a/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java +++ b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java @@ -269,7 +269,13 @@ public final class WiserAssertions { throws MessagingException, IOException { final StringBuilder sb = new StringBuilder(); for (int i = 0; i < mimeMultipart.getCount(); i++) { - sb.append(mimeMultipart.getBodyPart(i).getContent()); + Object content = mimeMultipart.getBodyPart(i).getContent(); + + if (content instanceof MimeMultipart) { + sb.append(getMimeMultipartAsString((MimeMultipart) content)); + } else { + sb.append(content); + } } return sb.toString(); } diff --git a/src/test/java/net/kemitix/wiser/assertions/AbstractWiserTest.java b/src/test/java/net/kemitix/wiser/assertions/AbstractWiserTest.java index 56282f8..675bbfa 100644 --- a/src/test/java/net/kemitix/wiser/assertions/AbstractWiserTest.java +++ b/src/test/java/net/kemitix/wiser/assertions/AbstractWiserTest.java @@ -54,6 +54,7 @@ public abstract class AbstractWiserTest { protected Session getSession() { Properties properties = new Properties(); + properties.setProperty("mail.transport.protocol", "smtp"); properties.setProperty("mail.smtp.host", "localhost"); properties.setProperty("mail.smtp.port", "" + WiserAssertionsTest.PORT); Session session = Session.getDefaultInstance(properties); diff --git a/src/test/java/net/kemitix/wiser/assertions/Issue1Test.java b/src/test/java/net/kemitix/wiser/assertions/Issue1Test.java index 5c313b3..d26fd90 100644 --- a/src/test/java/net/kemitix/wiser/assertions/Issue1Test.java +++ b/src/test/java/net/kemitix/wiser/assertions/Issue1Test.java @@ -14,24 +14,24 @@ import javax.mail.Message; */ public class Issue1Test extends AbstractWiserTest { + /** + * Test {@link WiserAssertions#withContentContains(String)} where + * the nested multipart message contains the expected text + */ @Test public void shouldParseNestedMultiPartEmails() { //given final Email email = new Email(); - email.addRecipient("Jonjo McKay", "jonjo.mckay@manywho.com", + email.addRecipient("Carl", "carl@b.com", Message.RecipientType.TO); - email.setFromAddress("ManyWho", "no-reply@manywho.com"); - email.setSubject("New activity"); - email.setText("Hi Jonjo McKay,\n\nA new message was just posted in a " - + "stream you follow on ManyWho. The message was:\n\nLance " - + "Drake Mandrell: \"This is a test message\"\n\nJoin the flow " - + "at https://flow.manywho.com to read the stream and reply.\n" - + "\nManyWho Email Bot"); + email.setFromAddress("Bob", "bob@a.com"); + email.setSubject("Subject"); + email.setText("Hi Carl,\n\nA new message was just posted."); Mailer mailer = new Mailer(getSession()); //when mailer.sendMail(email); //then - getAssertions().withContent("Hi Jonjo McKay"); + getAssertions().withContentContains("Hi Carl"); } }