FluentEmailsAssert.java

  1. package fr.sii.ogham.testing.assertion.email;

  2. import static fr.sii.ogham.testing.assertion.util.AssertionHelper.assertThat;
  3. import static fr.sii.ogham.testing.assertion.util.AssertionHelper.overrideDescription;
  4. import static org.hamcrest.Matchers.lessThan;

  5. import java.util.List;

  6. import javax.mail.Message;

  7. import org.hamcrest.Matcher;

  8. import fr.sii.ogham.testing.assertion.util.AssertionRegistry;
  9. import fr.sii.ogham.testing.util.HasParent;

  10. public class FluentEmailsAssert<P> extends HasParent<P> {
  11.     /**
  12.      * The list of messages that will be used for assertions
  13.      */
  14.     private final List<? extends Message> actual;
  15.     /**
  16.      * Registry to register assertions
  17.      */
  18.     private final AssertionRegistry registry;


  19.     public FluentEmailsAssert(List<? extends Message> actual, P parent, AssertionRegistry registry) {
  20.         super(parent);
  21.         this.actual = actual;
  22.         this.registry = registry;
  23.     }

  24.     /**
  25.      * Assertion on the number of received messages:
  26.      *
  27.      * <pre>
  28.      * .count(is(1))
  29.      * </pre>
  30.      *
  31.      * @param matcher
  32.      *            the assertion applied on the number of received messages
  33.      * @return the fluent API for chaining assertions on received messages
  34.      */
  35.     public FluentEmailsAssert<P> count(Matcher<Integer> matcher) {
  36.         registry.register(() -> assertThat("Received messages count", actual.size(), matcher));
  37.         return this;
  38.     }

  39.     /**
  40.      * Access a particular message to write assertions for it:
  41.      *
  42.      * <pre>
  43.      * .message(0).subject(is("foobar"))
  44.      * </pre>
  45.      *
  46.      * You can use this method to chain several assertions on different
  47.      * messages:
  48.      *
  49.      * <pre>
  50.      * .message(0).subject(is("foobar"))
  51.      * .and()
  52.      * .message(1).subject(is("toto"))
  53.      * </pre>
  54.      *
  55.      *
  56.      * @param index
  57.      *            the index of the message in the received list
  58.      * @return the fluent API for chaining assertions on received messages
  59.      */
  60.     public FluentEmailAssert<FluentEmailsAssert<P>> message(int index) {
  61.         registry.register(() -> assertThat(index, overrideDescription("Assertions on message "+index+" can't be executed because "+actual.size()+" messages were received", lessThan(actual.size()))));
  62.         return new FluentEmailAssert<>(index<actual.size() ? actual.get(index) : null, index, this, registry);
  63.     }

  64.     /**
  65.      * Fluent API to write assertions on every received messages. Any defined
  66.      * assertion will be applied on every message:
  67.      *
  68.      * <pre>
  69.      * .receivedMessages().every().subject(is("foobar"))
  70.      * </pre>
  71.      *
  72.      * Will check that subject of every message is "foobar".
  73.      *
  74.      * <p>
  75.      * You can use this method to factorize several assertions on a message and
  76.      * then make dedicated assertions on some messages:
  77.      *
  78.      * <pre>
  79.      * .receivedMessages().every().subject(is("foobar"))
  80.      *                    .and()
  81.      *                    .message(0).body().contentAsString(is("toto"))
  82.      * </pre>
  83.      *
  84.      * Will check that subject of every message is "foobar" and that body of
  85.      * first received message is "toto".
  86.      *
  87.      * @return the fluent API for chaining assertions on received messages
  88.      */
  89.     public FluentEmailAssert<FluentEmailsAssert<P>> every() {
  90.         return new FluentEmailAssert<>(actual, this, registry);
  91.     }
  92. }