Add and use a matcher for incomplete posts.
[Sone.git] / src / test / java / net / pterodactylus / sone / Matchers.java
index c73866e..3ac4972 100644 (file)
@@ -23,9 +23,11 @@ import java.io.IOException;
 import java.io.InputStream;
 
 import net.pterodactylus.sone.data.Album;
+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;
@@ -92,9 +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 IncompletePostMatcher.matches().id(postId);
        }
 
        public static Matcher<PostReply> isPostReply(String postReplyId,
@@ -182,71 +278,66 @@ 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");
+       public static Matcher<Image> isImage(final String id,
+                       final long creationTime,
+                       final String key, final String title,
+                       final String imageDescription,
+                       final int width, final int height) {
+               return new TypeSafeDiagnosingMatcher<Image>() {
+                       @Override
+                       protected boolean matchesSafely(Image image,
+                                       Description mismatchDescription) {
+                               if (!image.getId().equals(id)) {
+                                       mismatchDescription.appendText("ID is ")
+                                                       .appendValue(image.getId());
+                                       return false;
+                               }
+                               if (image.getCreationTime() != creationTime) {
+                                       mismatchDescription.appendText("created at @")
+                                                       .appendValue(image.getCreationTime());
+                                       return false;
+                               }
+                               if (!image.getKey().equals(key)) {
+                                       mismatchDescription.appendText("key is ")
+                                                       .appendValue(image.getKey());
                                        return false;
                                }
-                               if (!post.getRecipientId().get().equals(recipient.get())) {
-                                       mismatchDescription.appendText("Recipient is not ")
-                                                       .appendValue(recipient.get());
+                               if (!image.getTitle().equals(title)) {
+                                       mismatchDescription.appendText("title is ")
+                                                       .appendValue(image.getTitle());
                                        return false;
                                }
-                       } else {
-                               if (post.getRecipientId().isPresent()) {
-                                       mismatchDescription.appendText("Recipient is present");
+                               if (!image.getDescription().equals(imageDescription)) {
+                                       mismatchDescription.appendText("description is ")
+                                                       .appendValue(image.getDescription());
                                        return false;
                                }
+                               if (image.getWidth() != width) {
+                                       mismatchDescription.appendText("width is ")
+                                                       .appendValue(image.getWidth());
+                                       return false;
+                               }
+                               if (image.getHeight() != height) {
+                                       mismatchDescription.appendText("height is ")
+                                                       .appendValue(image.getHeight());
+                                       return false;
+                               }
+                               return true;
                        }
-                       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());
+                       @Override
+                       public void describeTo(Description description) {
+                               description.appendText("image with ID ").appendValue(id);
+                               description.appendText(", created at @")
+                                               .appendValue(creationTime);
+                               description.appendText(", has key ").appendValue(key);
+                               description.appendText(", has title ").appendValue(title);
+                               description.appendText(", has description ")
+                                               .appendValue(imageDescription);
+                               description.appendText(", has width ").appendValue(width);
+                               description.appendText(", has height ").appendValue(height);
                        }
-               }
-
+               };
        }
 
        private static class PostReplyMatcher