From d2f623f33cc0dd8d57bcc43bf50aec6985512167 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 8 Apr 2015 11:51:50 +0100 Subject: [PATCH] Initial import Signed-off-by: Paul Campbell --- .gitignore | 1 + pom.xml | 105 ++++++++++++++++++ .../wiser/assertions/WiserAssertions.java | 98 ++++++++++++++++ 3 files changed, 204 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java diff --git a/.gitignore b/.gitignore index 32858aa..e43e9a3 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* +/target diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..e550f10 --- /dev/null +++ b/pom.xml @@ -0,0 +1,105 @@ + + + 4.0.0 + net.kemitix + wiser-assertions + 1.0-SNAPSHOT + jar + + wiser-assertions + Assertions for Wiser SMTP test server from subethamail + + https://github.com/kemitix/wiser-assertions + + + + Paul Campbell + pcampbell@kemitix.net + Kemitix + https://github.com/kemitix/ + + + + + + MIT License + http://www.opensource.org/licenses/mit-license.php + + + + 2015 + + + 3.0.4 + + + + https://github.com/kemitix/wiser-assertions/issues + GitHub Issues + + + + scm:git:git@github.com:kemitix/wiser-assertions.git + scm:git:git@github.com:kemitix/wiser-assertions.git + git@github.com:kemitix/wiser-assertions.git + + + + UTF-8 + 1.8 + 1.8 + + + + org.sonatype.oss + oss-parent + 9 + + + + + javax.mail + mail + 1.4.4 + + + org.subethamail + subethasmtp + 3.1.7 + + + + + + release-sign-artifacts + + + performRelease + true + + + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.6 + + ${gpg.passphrase} + + + + sign-artifacts + verify + + sign + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java new file mode 100644 index 0000000..113beff --- /dev/null +++ b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java @@ -0,0 +1,98 @@ +package net.kemitix.wiser.assertions; + +import java.text.MessageFormat; +import java.util.List; +import java.util.function.Predicate; +import java.util.function.Supplier; +import javax.mail.internet.MimeMessage; +import org.subethamail.wiser.Wiser; +import org.subethamail.wiser.WiserMessage; + +/** + * Taken from the WiserAssetions class from Rafal Browiec at + * http://blog.codeleak.pl/2014/09/testing-mail-code-in-spring-boot.html + */ +public class WiserAssertions { + + private final List messages; + + public static WiserAssertions assertReceivedMessage(Wiser wiser) { + return new WiserAssertions(wiser.getMessages()); + } + + private WiserAssertions(List messages) { + this.messages = messages; + } + + public WiserAssertions from(String from) { + findFirstOrElseThrow(m -> m.getEnvelopeSender().equals(from), + assertionError("No message from [{0}] found!", from)); + return this; + } + + public WiserAssertions to(String to) { + findFirstOrElseThrow(m -> m.getEnvelopeReceiver().equals(to), + assertionError("No message to [{0}] found!", to)); + return this; + } + + public WiserAssertions withSubject(String subject) { + Predicate predicate = m -> subject.equals(unchecked(getMimeMessage(m)::getSubject)); + findFirstOrElseThrow(predicate, + assertionError("No message with subject [{0}] found!", subject)); + return this; + } + + public WiserAssertions withSubjectContains(String subject) { + Predicate predicate = m -> unchecked(getMimeMessage(m)::getSubject).contains(subject); + findFirstOrElseThrow(predicate, + assertionError("No message with subject [{0}] found!", subject)); + return this; + } + + public WiserAssertions withContent(String content) { + findFirstOrElseThrow(m -> { + ThrowingSupplier contentAsString + = () -> ((String) getMimeMessage(m).getContent()).trim(); + return content.equals(unchecked(contentAsString)); + }, assertionError("No message with content [{0}] found!", content)); + return this; + } + + public WiserAssertions withContentContains(String content) { + StringBuilder messageContent = new StringBuilder(); + findFirstOrElseThrow((WiserMessage m) -> { + ThrowingSupplier contentAsString + = () -> ((String) getMimeMessage(m).getContent()).trim(); + messageContent.append(unchecked(contentAsString)); + return unchecked(contentAsString).contains(content); + }, assertionError("No message with content containing [{0}] found! Was {1}", content, messageContent)); + return this; + } + + private void findFirstOrElseThrow(Predicate predicate, Supplier exceptionSupplier) { + messages.stream().filter(predicate) + .findFirst().orElseThrow(exceptionSupplier); + } + + private MimeMessage getMimeMessage(WiserMessage wiserMessage) { + return unchecked(wiserMessage::getMimeMessage); + } + + private static Supplier assertionError(String errorMessage, Object... args) { + return () -> new AssertionError(MessageFormat.format(errorMessage, args)); + } + + public static T unchecked(ThrowingSupplier supplier) { + try { + return supplier.get(); + } catch (Throwable e) { + throw new RuntimeException(e); + } + } + + public interface ThrowingSupplier { + + T get() throws Throwable; + } +}