Add and use a matcher for incomplete posts.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 27 Nov 2014 05:50:49 +0000 (06:50 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 27 Nov 2014 05:50:49 +0000 (06:50 +0100)
src/test/java/net/pterodactylus/sone/Matchers.java
src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java

index c16322c..3ac4972 100644 (file)
@@ -27,6 +27,7 @@ import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
 
+import com.google.common.base.Objects;
 import com.google.common.base.Optional;
 import org.hamcrest.Description;
 import org.hamcrest.Matcher;
@@ -93,13 +94,103 @@ public class Matchers {
                };
        }
 
+       public static class IncompletePostMatcher extends TypeSafeDiagnosingMatcher<Post> {
+
+               private String id;
+               private String sender;
+               private boolean checkRecipient;
+               private String recipient;
+               private long time;
+               private String text;
+
+               private IncompletePostMatcher() {
+               }
+
+               public static IncompletePostMatcher matches() {
+                       return new IncompletePostMatcher();
+               }
+
+               public IncompletePostMatcher id(String id) {
+                       this.id = id;
+                       return this;
+               }
+
+               public IncompletePostMatcher sender(String sender) {
+                       this.sender = sender;
+                       return this;
+               }
+
+               public IncompletePostMatcher recipient(String recipient) {
+                       this.checkRecipient = true;
+                       this.recipient = recipient;
+                       return this;
+               }
+
+               public IncompletePostMatcher time(long time) {
+                       this.time = time;
+                       return this;
+               }
+
+               public IncompletePostMatcher text(String text) {
+                       this.text = text;
+                       return this;
+               }
+
+               @Override
+               protected boolean matchesSafely(Post post, Description mismatchDescription) {
+                       if ((id != null) && !id.matches(post.getId())) {
+                               mismatchDescription.appendText("ID is ").appendValue(post.getId());
+                               return false;
+                       }
+                       if ((sender != null) && !sender.matches(post.getSone().getId())) {
+                               mismatchDescription.appendText("sender is ").appendValue(post.getSone().getId());
+                               return false;
+                       }
+                       if (checkRecipient && !Objects.equal(recipient, post.getRecipientId().orNull())) {
+                               mismatchDescription.appendText("recipient is ").appendValue(post.getRecipientId().orNull());
+                               return false;
+                       }
+                       if ((time != 0) && (time != post.getTime())) {
+                               mismatchDescription.appendText("time is ").appendValue(post.getTime());
+                               return false;
+                       }
+                       if ((text != null) && !text.equals(post.getText())) {
+                               mismatchDescription.appendText("text is ").appendValue(post.getText());
+                               return false;
+                       }
+                       return true;
+               }
+
+               @Override
+               public void describeTo(Description description) {
+                       description.appendText("is post");
+                       if (id != null) {
+                               description.appendText(" with ID ").appendValue(id);
+                       }
+                       if (sender != null) {
+                               description.appendText(" with sender ").appendValue(sender);
+                       }
+                       if (checkRecipient) {
+                               description.appendText(" with recipient ").appendValue(recipient);
+                       }
+                       if (time != 0) {
+                               description.appendText(" with time ").appendValue(time);
+                       }
+                       if (text != null) {
+                               description.appendText(" with text ").appendValue(text);
+                       }
+               }
+
+       }
+
+
        public static Matcher<Post> isPost(String postId, long time,
                        String text, Optional<String> recipient) {
-               return new PostMatcher(postId, time, text, recipient);
+               return IncompletePostMatcher.matches().id(postId).time(time).text(text).recipient(recipient.orNull());
        }
 
        public static Matcher<Post> isPostWithId(String postId) {
-               return new PostIdMatcher(postId);
+               return IncompletePostMatcher.matches().id(postId);
        }
 
        public static Matcher<PostReply> isPostReply(String postReplyId,
@@ -249,98 +340,6 @@ public class Matchers {
                };
        }
 
-       private static class PostMatcher extends TypeSafeDiagnosingMatcher<Post> {
-
-               private final String postId;
-               private final long time;
-               private final String text;
-               private final Optional<String> recipient;
-
-               private PostMatcher(String postId, long time, String text,
-                               Optional<String> recipient) {
-                       this.postId = postId;
-                       this.time = time;
-                       this.text = text;
-                       this.recipient = recipient;
-               }
-
-               @Override
-               protected boolean matchesSafely(Post post,
-                               Description mismatchDescription) {
-                       if (!post.getId().equals(postId)) {
-                               mismatchDescription.appendText("ID is not ")
-                                               .appendValue(postId);
-                               return false;
-                       }
-                       if (post.getTime() != time) {
-                               mismatchDescription.appendText("Time is not @")
-                                               .appendValue(time);
-                               return false;
-                       }
-                       if (!post.getText().equals(text)) {
-                               mismatchDescription.appendText("Text is not ")
-                                               .appendValue(text);
-                               return false;
-                       }
-                       if (recipient.isPresent()) {
-                               if (!post.getRecipientId().isPresent()) {
-                                       mismatchDescription.appendText(
-                                                       "Recipient not present");
-                                       return false;
-                               }
-                               if (!post.getRecipientId().get().equals(recipient.get())) {
-                                       mismatchDescription.appendText("Recipient is not ")
-                                                       .appendValue(recipient.get());
-                                       return false;
-                               }
-                       } else {
-                               if (post.getRecipientId().isPresent()) {
-                                       mismatchDescription.appendText("Recipient is present");
-                                       return false;
-                               }
-                       }
-                       return true;
-               }
-
-               @Override
-               public void describeTo(Description description) {
-                       description.appendText("is post with ID ")
-                                       .appendValue(postId);
-                       description.appendText(", created at @").appendValue(time);
-                       description.appendText(", text ").appendValue(text);
-                       if (recipient.isPresent()) {
-                               description.appendText(", directed at ")
-                                               .appendValue(recipient.get());
-                       }
-               }
-
-       }
-
-       private static class PostIdMatcher extends TypeSafeDiagnosingMatcher<Post> {
-
-               private final String id;
-
-               private PostIdMatcher(String id) {
-                       this.id = id;
-               }
-
-               @Override
-               protected boolean matchesSafely(Post item,
-                               Description mismatchDescription) {
-                       if (!item.getId().equals(id)) {
-                               mismatchDescription.appendText("post has ID ").appendValue(item.getId());
-                               return false;
-                       }
-                       return true;
-               }
-
-               @Override
-               public void describeTo(Description description) {
-                       description.appendText("post with ID ").appendValue(id);
-               }
-
-       }
-
        private static class PostReplyMatcher
                        extends TypeSafeDiagnosingMatcher<PostReply> {
 
index cc5babb..7cba93b 100644 (file)
@@ -44,6 +44,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import net.pterodactylus.sone.Matchers.IncompletePostMatcher;
 import net.pterodactylus.sone.TestAlbumBuilder;
 import net.pterodactylus.sone.TestImageBuilder;
 import net.pterodactylus.sone.TestPostBuilder;
@@ -227,7 +228,7 @@ public class MemoryDatabaseTest {
                Post postWithoutRecipient = createPost(Optional.<String>absent());
                memoryDatabase.storePost(postWithoutRecipient);
                assertThat(memoryDatabase.getDirectedPosts(RECIPIENT_ID),
-                               contains(postWithRecipient));
+                               contains(IncompletePostMatcher.matches().recipient(RECIPIENT_ID)));
        }
 
        private Post createPost(Optional<String> recipient) {