From 14cc99fd886997db83eda9346bb23d893c58425c Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 21 Oct 2015 23:01:45 +0100 Subject: [PATCH 1/4] WiserAssertions: improve conversion of mime Multipart message to string Previously the message was written to an output stream. Headers and mime boundaries included. Assertions which use this method are looking at the content of the message, rather than the wrappings. Now we just look at the content of the multiparts. Signed-off-by: Paul Campbell --- .../kemitix/wiser/assertions/WiserAssertions.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java index 9ed4f29..a2fc2b6 100644 --- a/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java +++ b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java @@ -3,7 +3,6 @@ package net.kemitix.wiser.assertions; import org.subethamail.wiser.Wiser; import org.subethamail.wiser.WiserMessage; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.text.MessageFormat; import java.util.List; @@ -256,7 +255,8 @@ public final class WiserAssertions { } /** - * Converts a {@link MimeMultipart} into a {@link String}. + * Converts a {@link MimeMultipart} into a {@link String} stripping out the + * mime part boundary and headers.. * * @param mimeMultipart the message part to convert * @@ -267,9 +267,11 @@ public final class WiserAssertions { */ private String getMimeMultipartAsString(final MimeMultipart mimeMultipart) throws MessagingException, IOException { - ByteArrayOutputStream os = new ByteArrayOutputStream(); - mimeMultipart.writeTo(os); - return os.toString("UTF-8"); + final StringBuilder sb = new StringBuilder(); + for (int i = 0; i < mimeMultipart.getCount(); i++) { + sb.append(mimeMultipart.getBodyPart(i).getContent()); + } + return sb.toString(); } /** From 9cd5b0c69c53908d930483525fad09ba690a1458 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 21 Oct 2015 23:03:12 +0100 Subject: [PATCH 2/4] pom.xml: Upgrade kemitix-spring-parent to 1.2.1 This gives us: * Build is no longer platform dependent * Upgrade maven plugins: surefire, surefire-report and failsafe to 2.19 * Upgrade maven plugin checkstyle to 2.17 Signed-off-by: Paul Campbell --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 3af9d95..27a2ad1 100644 --- a/pom.xml +++ b/pom.xml @@ -31,7 +31,7 @@ net.kemitix kemitix-spring-parent - 1.1.7 + 1.2.1 From 1215fe8fa9d0be77b12198148022822980fa1c0b Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 21 Oct 2015 23:03:36 +0100 Subject: [PATCH 3/4] pom.xml: add junit and mockito-core dependencies for testing Signed-off-by: Paul Campbell --- pom.xml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pom.xml b/pom.xml index 27a2ad1..b158f25 100644 --- a/pom.xml +++ b/pom.xml @@ -45,6 +45,17 @@ subethasmtp 3.1.7 + + junit + junit + test + + + org.mockito + mockito-core + test + jar + \ No newline at end of file From bee688fe9f8a6c57823bd81d6ae00a953fb4852b Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 21 Oct 2015 23:03:46 +0100 Subject: [PATCH 4/4] WiserAssertionsTest: added Signed-off-by: Paul Campbell --- .../wiser/assertions/WiserAssertionsTest.java | 288 ++++++++++++++++++ 1 file changed, 288 insertions(+) create mode 100644 src/test/java/net/kemitix/wiser/assertions/WiserAssertionsTest.java diff --git a/src/test/java/net/kemitix/wiser/assertions/WiserAssertionsTest.java b/src/test/java/net/kemitix/wiser/assertions/WiserAssertionsTest.java new file mode 100644 index 0000000..891f608 --- /dev/null +++ b/src/test/java/net/kemitix/wiser/assertions/WiserAssertionsTest.java @@ -0,0 +1,288 @@ +package net.kemitix.wiser.assertions; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.subethamail.wiser.Wiser; + +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; +import java.util.Properties; +import java.util.logging.Level; +import java.util.logging.Logger; + +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.Multipart; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeBodyPart; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMultipart; + +/** + * Tests for {@link WiserAssertions}. + * + * @author pcampbell + */ +public class WiserAssertionsTest { + + /** + * Logger. + */ + private static final Logger LOG + = Logger.getLogger(WiserAssertionsTest.class.getName()); + + /** + * Test mail server. + */ + private Wiser wiser; + + /** + * Prepare each test. + */ + @Before + @SuppressWarnings("magicnumber") + public void setUp() { + wiser = new Wiser(PORT); + wiser.start(); + } + + /** + * Test mail server port. + */ + private static final int PORT = 12345; + + /** + * Clean up after each test. + */ + @After + public void tearDown() { + wiser.stop(); + } + + /** + * Sends a mime multipart message to the Wiser server. + * + * @param from the sender + * @param to the recipient + * @param subject the subject of the email + * @param body the body of the email + */ + private void sendMimeMultipartMessage( + final String from, + final String to, + final String subject, + final String body) { + Properties properties = new Properties(); + properties.setProperty("mail.smtp.host", "localhost"); + properties.setProperty("mail.smtp.port", "" + PORT); + Session session = Session.getDefaultInstance(properties); + try { + MimeMessage message = new MimeMessage(session); + message.setFrom(new InternetAddress(from)); + message.setRecipients(Message.RecipientType.TO, to); + message.setSubject(subject, "UTF-8"); + final Multipart mimeMultipart = new MimeMultipart(); + final MimeBodyPart mimeBodyPart = new MimeBodyPart(); + mimeBodyPart.setText(body); + mimeMultipart.addBodyPart(mimeBodyPart); + message.setContent(mimeMultipart); + Transport.send(message); + } catch (MessagingException ex) { + LOG.log(Level.SEVERE, null, ex); + } + } + + /** + * Instantiates the WiserAssertions. + * + * @return the wiser assertions + */ + private WiserAssertions getAssertions() { + return WiserAssertions.assertReceivedMessage(wiser); + } + + /** + * Test {@link WiserAssertions#withContent(java.lang.String)} where the + * content of the email matches. + */ + @Test + public void testContentMatches() { + //given + final String body = "message body"; + //when + sendMimeMultipartMessage("from", "to", "subject", body); + //then + getAssertions().withContent(body); + } + + /** + * Test {@link WiserAssertions#withContent(java.lang.String)} where the + * content of the email does not match. + */ + @Test(expected = AssertionError.class) + public void testContentNotMatches() { + //given + final String body = "message body"; + //when + sendMimeMultipartMessage("from", "to", "subject", body); + //then + getAssertions().withContent("Other body"); + } + + /** + * Test {@link WiserAssertions#withContentContains(String)} where the + * content of the email matches. + */ + @Test + public void testContentContainsMatches() { + //given + final String body = "message body"; + //when + sendMimeMultipartMessage("from", "to", "subject", body); + //then + getAssertions().withContentContains("age bo"); + } + + /** + * Test {@link WiserAssertions#withContentContains(String)} where the + * content of the email does not match. + */ + @Test(expected = AssertionError.class) + public void testContentContainsNotMatches() { + //given + final String body = "message body"; + //when + sendMimeMultipartMessage("from", "to", "subject", body); + //then + getAssertions().withContentContains("agebo"); + } + + /** + * Test {@link WiserAssertions#from(java.lang.String)} can detect when mail + * is sent from a user. + * + * @throws java.io.IOException if error delivering test message + */ + @Test + public void testFromMatches() throws IOException { + //given + final String from = "bob@a.com"; + //when + sendMimeMultipartMessage(from, "to", "subject", "body"); + //then + getAssertions().from(from); + } + + /** + * Test {@link WiserAssertions#from(java.lang.String)} can detect when mail + * is not sent from a user. + */ + @Test(expected = AssertionError.class) + public void testFromNotMatches() { + //given + final String from = "bob@a.com"; + //when + sendMimeMultipartMessage(from, "to", "subject", "body"); + //then + getAssertions().from("lisa@c.com"); + } + + /** + * Test {@link WiserAssertions#assertReceivedMessage(Wiser)} creates and + * returns a WiserAssertions instance. + */ + @Test + public void testInstantiate() { + assertNotNull(getAssertions()); + } + + /** + * Test {@link WiserAssertions#withSubjectContains(java.lang.String)} where + * the subject contains the expected fragment. + */ + @Test + public void testSubjectContainsMatches() { + //given + final String fragment = "foo"; + //when + sendMimeMultipartMessage("from", "to", "subject " + fragment + " tail", "body"); + //then + getAssertions().withSubjectContains(fragment); + } + + /** + * Test {@link WiserAssertions#withSubjectContains(java.lang.String)} where + * the subject does not contain the expected fragment. + */ + @Test(expected = AssertionError.class) + public void testSubjectContainsNotMatches() { + //given + final String fragment = "foo"; + //when + sendMimeMultipartMessage("from", "to", "subject tail", "body"); + //then + getAssertions().withSubjectContains(fragment); + } + + /** + * Test {@link WiserAssertions#withSubject(java.lang.String)} where the + * message has the subject expected. + */ + @Test + public void testSubjectMatches() { + //given + final String subject = "message subject"; + //when + sendMimeMultipartMessage("from", "to", subject, "body"); + //then + getAssertions().withSubject(subject); + } + + /** + * Test {@link WiserAssertions#withSubject(java.lang.String)} where the + * message does not have the subject expected. + */ + @Test(expected = AssertionError.class) + public void testSubjectNotMatches() { + //given + final String subject = "message subject"; + //when + sendMimeMultipartMessage("from", "to", subject, "body"); + //then + getAssertions().withSubject("other subject"); + } + + /** + * Test {@link WiserAssertions#from(java.lang.String)} can detect when mail + * is sent to a user. + */ + @Test + public void testToMatches() { + //given + final String to = "carl@b.com"; + //when + sendMimeMultipartMessage("from", to, "subject", "body"); + //then + getAssertions().to(to); + } + + /** + * Test {@link WiserAssertions#from(java.lang.String)} can detect when mail + * is not sent from a user. + * + */ + @Test(expected = AssertionError.class) + public void testToNotMatches() { + //given + final String to = "carl@b.com"; + //when + sendMimeMultipartMessage("from", to, "subject", "body"); + //then + getAssertions().to("bob@a.com"); + } + +}