All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.kemitix.wiser.assertions.WiserAssertions Maven / Gradle / Ivy

There is a newer version: 0.6.0
Show newest version
package net.kemitix.wiser.assertions;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.MessageFormat;
import java.util.List;
import java.util.function.Predicate;
import java.util.function.Supplier;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
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
                    = () -> getMimeMessageBody(m).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
                    = () -> getMimeMessageBody(m).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 String getMimeMessageBody(WiserMessage m) throws IOException, MessagingException {
        Object content = getMimeMessage(m).getContent();
        if (content instanceof MimeMessage) {
            return (String) content;
        }
        if (content instanceof MimeMultipart) {
            return getMimeMultipartAsString((MimeMultipart) content);
        }
        throw new RuntimeException("Unexpected MimeMessage content");
    }

    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);
        }
    }

    private String getMimeMultipartAsString(MimeMultipart mimeMultipart) throws MessagingException, IOException {
        OutputStream os = new ByteArrayOutputStream();
        mimeMultipart.writeTo(os);
        return os.toString();
    }

    public interface ThrowingSupplier {

        T get() throws Throwable;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy