+ * {@code + * {@literal @}Before + * public void setUp() throws IOException { + * wiser = new Wiser(PORT); + * wiser.start(); + * } + * + * {@literal @}After public void tearDown() { wiser.stop(); } + * + * {@literal @}Test public void testMail() { //given ... + * + * //when ... + * + * //then WiserAssertions.assertReceivedMessage(wiser) .from(sender) + * .to(recipient_alpha) .to(recipient_beta) .withSubjectContains(subject_prefix) + * .withSubjectContains(subject_suffix) .withContentContains(message_element_1) + * .withContentContains(message_element_2) + * .withContentContains(message_element_3); } + * } + **/ -public class WiserAssertions { +public final class WiserAssertions { + /** + * The messages received by Wiser. + */ private final List
+ * The MIT License. + *
+ * Copyright 2015 pcampbell. + *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *
+ * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.kemitix.wiser.assertions; 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"); + } + +}