FluentReceivedSmsAssert.java

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

  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.Collection;
  6. import java.util.List;

  7. import org.hamcrest.Matcher;

  8. import fr.sii.ogham.testing.assertion.util.AssertionRegistry;
  9. import fr.sii.ogham.testing.sms.simulator.bean.SubmitSm;

  10. /**
  11.  * Make assertions on received messages
  12.  *
  13.  * @author AurĂ©lien Baudet
  14.  *
  15.  * @param <S>
  16.  *            the type of the {@link SubmitSm} to make assertions on
  17.  */
  18. public class FluentReceivedSmsAssert<S extends SubmitSm> {
  19.     /**
  20.      * List of received messages
  21.      */
  22.     private final List<S> actual;
  23.     /**
  24.      * the registry for assertions
  25.      */
  26.     private final AssertionRegistry registry;

  27.     /**
  28.      * Initializes with the list of received messages
  29.      *
  30.      * @param actual
  31.      *            received messages
  32.      * @param registry
  33.      *            used to register assertions
  34.      */
  35.     public FluentReceivedSmsAssert(List<S> actual, AssertionRegistry registry) {
  36.         this.actual = actual;
  37.         this.registry = registry;
  38.     }

  39.     /**
  40.      * Access fluent API to write assertions on a particular received message.
  41.      *
  42.      * If you want to make assertions on several messages, you may prefer using:
  43.      *
  44.      * <pre>
  45.      * .receivedMessages().message(0)
  46.      *                       .content(is("foobar"))
  47.      *                    .and()
  48.      *                    .message(1)
  49.      *                       .content(is("bar"))
  50.      * </pre>
  51.      *
  52.      * @param index
  53.      *            the index of the received message
  54.      * @return the fluent API for assertions on the particular message
  55.      */
  56.     public FluentSmsAssert<FluentReceivedSmsAssert<S>, S> receivedMessage(int index) {
  57.         registry.register(() -> assertThat(index, overrideDescription("Assertions on message "+index+" can't be executed because "+actual.size()+" messages were received", lessThan(actual.size()))));
  58.         return new FluentSmsAssert<>(index<actual.size() ? actual.get(index) : null, index, this, registry);
  59.     }

  60.     /**
  61.      * Fluent API to write assertions on received messages.
  62.      *
  63.      * You can write assertions for all messages or a particular message.
  64.      *
  65.      * For example, for writing assertion on a single message, you can write:
  66.      *
  67.      * <pre>
  68.      * .receivedMessages().message(0)
  69.      *                       .content(is("foobar"))
  70.      * </pre>
  71.      *
  72.      * For writing assertions that are applied on every received message, you
  73.      * can write:
  74.      *
  75.      * <pre>
  76.      * .receivedMessages().every()
  77.      *                       .content(is("foobar"))
  78.      * </pre>
  79.      *
  80.      * Will check that content of every message is "foobar".
  81.      *
  82.      * <p>
  83.      * You can use this method to factorize several assertions on a message and
  84.      * then make dedicated assertions on some messages:
  85.      *
  86.      * <pre>
  87.      * .receivedMessages().every()
  88.      *                       .content(is("foobar"))
  89.      *                    .and()
  90.      *                    .message(0)
  91.      *                       .from().number(is("+33102030405"))
  92.      * </pre>
  93.      *
  94.      * Will check that content of every message is "foobar" and that phone
  95.      * nuumber of sender of first received message is "+33102030405".
  96.      *
  97.      * @return the fluent API for assertions on messages
  98.      */
  99.     public FluentSmsListAssert<FluentReceivedSmsAssert<S>, S> receivedMessages() {
  100.         return new FluentSmsListAssert<>(actual, this, registry);
  101.     }

  102.     /**
  103.      * Fluent API to write assertions on received messages.
  104.      *
  105.      * Make an assertion on received messages list (JavaMail message).
  106.      *
  107.      * For example, for writing assertion on a single message, you can write:
  108.      *
  109.      * <pre>
  110.      * .receivedMessages(is(Matchers.&lt;Message&gt;empty()))
  111.      * </pre>
  112.      *
  113.      * @param matcher
  114.      *            the assertion to apply on message list
  115.      * @return the fluent API for assertions on messages
  116.      */
  117.     public FluentSmsListAssert<FluentReceivedSmsAssert<S>, S> receivedMessages(Matcher<Collection<? extends S>> matcher) {
  118.         registry.register(() -> assertThat("Received messages", actual, matcher));
  119.         return receivedMessages();
  120.     }

  121. }