Initial import
Signed-off-by: Paul Campbell <pcampbell@kemitix.net>
This commit is contained in:
parent
2ce4366e84
commit
d2f623f33c
3 changed files with 204 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -10,3 +10,4 @@
|
||||||
|
|
||||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||||
hs_err_pid*
|
hs_err_pid*
|
||||||
|
/target
|
||||||
|
|
105
pom.xml
Normal file
105
pom.xml
Normal file
|
@ -0,0 +1,105 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>net.kemitix</groupId>
|
||||||
|
<artifactId>wiser-assertions</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>wiser-assertions</name>
|
||||||
|
<description>Assertions for Wiser SMTP test server from subethamail</description>
|
||||||
|
|
||||||
|
<url>https://github.com/kemitix/wiser-assertions</url>
|
||||||
|
|
||||||
|
<developers>
|
||||||
|
<developer>
|
||||||
|
<name>Paul Campbell</name>
|
||||||
|
<email>pcampbell@kemitix.net</email>
|
||||||
|
<organization>Kemitix</organization>
|
||||||
|
<organizationUrl>https://github.com/kemitix/</organizationUrl>
|
||||||
|
</developer>
|
||||||
|
</developers>
|
||||||
|
|
||||||
|
<licenses>
|
||||||
|
<license>
|
||||||
|
<name>MIT License</name>
|
||||||
|
<url>http://www.opensource.org/licenses/mit-license.php</url>
|
||||||
|
</license>
|
||||||
|
</licenses>
|
||||||
|
|
||||||
|
<inceptionYear>2015</inceptionYear>
|
||||||
|
|
||||||
|
<prerequisites>
|
||||||
|
<maven>3.0.4</maven>
|
||||||
|
</prerequisites>
|
||||||
|
|
||||||
|
<issueManagement>
|
||||||
|
<url>https://github.com/kemitix/wiser-assertions/issues</url>
|
||||||
|
<system>GitHub Issues</system>
|
||||||
|
</issueManagement>
|
||||||
|
|
||||||
|
<scm>
|
||||||
|
<connection>scm:git:git@github.com:kemitix/wiser-assertions.git</connection>
|
||||||
|
<developerConnection>scm:git:git@github.com:kemitix/wiser-assertions.git</developerConnection>
|
||||||
|
<url>git@github.com:kemitix/wiser-assertions.git</url>
|
||||||
|
</scm>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.sonatype.oss</groupId>
|
||||||
|
<artifactId>oss-parent</artifactId>
|
||||||
|
<version>9</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.mail</groupId>
|
||||||
|
<artifactId>mail</artifactId>
|
||||||
|
<version>1.4.4</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.subethamail</groupId>
|
||||||
|
<artifactId>subethasmtp</artifactId>
|
||||||
|
<version>3.1.7</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>release-sign-artifacts</id>
|
||||||
|
<activation>
|
||||||
|
<property>
|
||||||
|
<name>performRelease</name>
|
||||||
|
<value>true</value>
|
||||||
|
</property>
|
||||||
|
</activation>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-gpg-plugin</artifactId>
|
||||||
|
<version>1.6</version>
|
||||||
|
<configuration>
|
||||||
|
<passphrase>${gpg.passphrase}</passphrase>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>sign-artifacts</id>
|
||||||
|
<phase>verify</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>sign</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
|
|
||||||
|
</project>
|
|
@ -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<WiserMessage> messages;
|
||||||
|
|
||||||
|
public static WiserAssertions assertReceivedMessage(Wiser wiser) {
|
||||||
|
return new WiserAssertions(wiser.getMessages());
|
||||||
|
}
|
||||||
|
|
||||||
|
private WiserAssertions(List<WiserMessage> 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<WiserMessage> 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<WiserMessage> 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<String> 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<String> 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<WiserMessage> predicate, Supplier<AssertionError> exceptionSupplier) {
|
||||||
|
messages.stream().filter(predicate)
|
||||||
|
.findFirst().orElseThrow(exceptionSupplier);
|
||||||
|
}
|
||||||
|
|
||||||
|
private MimeMessage getMimeMessage(WiserMessage wiserMessage) {
|
||||||
|
return unchecked(wiserMessage::getMimeMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Supplier<AssertionError> assertionError(String errorMessage, Object... args) {
|
||||||
|
return () -> new AssertionError(MessageFormat.format(errorMessage, args));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T unchecked(ThrowingSupplier<T> supplier) {
|
||||||
|
try {
|
||||||
|
return supplier.get();
|
||||||
|
} catch (Throwable e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface ThrowingSupplier<T> {
|
||||||
|
|
||||||
|
T get() throws Throwable;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue