X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2FMatchers.java;h=3ac497245bbdfbc348c17a7820c4f0bef7503b0e;hp=14a8a008b07417f5c3389ad0670b34c61d472eda;hb=13f2ccb2b985cd206a593f3f40d397a6befcce6e;hpb=11f3457415c9122b5d11840d24186971af28add9 diff --git a/src/test/java/net/pterodactylus/sone/Matchers.java b/src/test/java/net/pterodactylus/sone/Matchers.java index 14a8a00..3ac4972 100644 --- a/src/test/java/net/pterodactylus/sone/Matchers.java +++ b/src/test/java/net/pterodactylus/sone/Matchers.java @@ -22,8 +22,12 @@ import static java.util.regex.Pattern.compile; 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; @@ -90,74 +94,300 @@ public class Matchers { }; } + public static class IncompletePostMatcher extends TypeSafeDiagnosingMatcher { + + 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 isPost(String postId, long time, String text, Optional recipient) { - return new PostMatcher(postId, time, text, recipient); + return IncompletePostMatcher.matches().id(postId).time(time).text(text).recipient(recipient.orNull()); + } + + public static Matcher isPostWithId(String postId) { + return IncompletePostMatcher.matches().id(postId); + } + + public static Matcher isPostReply(String postReplyId, + String postId, long time, String text) { + return new PostReplyMatcher(postReplyId, postId, time, text); + } + + public static Matcher isAlbum(final String albumId, + final String parentAlbumId, + final String title, final String albumDescription, + final String imageId) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(Album album, + Description mismatchDescription) { + if (!album.getId().equals(albumId)) { + mismatchDescription.appendText("ID is ") + .appendValue(album.getId()); + return false; + } + if (parentAlbumId == null) { + if (album.getParent() != null) { + mismatchDescription.appendText("has parent album"); + return false; + } + } else { + if (album.getParent() == null) { + mismatchDescription.appendText("has no parent album"); + return false; + } + if (!album.getParent().getId().equals(parentAlbumId)) { + mismatchDescription.appendText("parent album is ") + .appendValue(album.getParent().getId()); + return false; + } + } + if (!title.equals(album.getTitle())) { + mismatchDescription.appendText("has title ") + .appendValue(album.getTitle()); + return false; + } + if (!albumDescription.equals(album.getDescription())) { + mismatchDescription.appendText("has description ") + .appendValue(album.getDescription()); + return false; + } + if (imageId == null) { + if (album.getAlbumImage() != null) { + mismatchDescription.appendText("has album image"); + return false; + } + } else { + if (album.getAlbumImage() == null) { + mismatchDescription.appendText("has no album image"); + return false; + } + if (!album.getAlbumImage().getId().equals(imageId)) { + mismatchDescription.appendText("has album image ") + .appendValue(album.getAlbumImage().getId()); + return false; + } + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("is album ").appendValue(albumId); + if (parentAlbumId == null) { + description.appendText(", has no parent"); + } else { + description.appendText(", has parent ") + .appendValue(parentAlbumId); + } + description.appendText(", has title ").appendValue(title); + description.appendText(", has description ") + .appendValue(albumDescription); + if (imageId == null) { + description.appendText(", has no album image"); + } else { + description.appendText(", has album image ") + .appendValue(imageId); + } + } + }; + } + + public static Matcher 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() { + @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 (!image.getTitle().equals(title)) { + mismatchDescription.appendText("title is ") + .appendValue(image.getTitle()); + return false; + } + 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; + } + + @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 PostMatcher extends TypeSafeDiagnosingMatcher { + private static class PostReplyMatcher + extends TypeSafeDiagnosingMatcher { + private final String postReplyId; private final String postId; private final long time; private final String text; - private final Optional recipient; - private PostMatcher(String postId, long time, String text, - Optional recipient) { + private PostReplyMatcher(String postReplyId, String postId, long time, + String text) { + this.postReplyId = postReplyId; this.postId = postId; this.time = time; this.text = text; - this.recipient = recipient; } @Override - protected boolean matchesSafely(Post post, + protected boolean matchesSafely(PostReply postReply, Description mismatchDescription) { - if (!post.getId().equals(postId)) { - mismatchDescription.appendText("ID is not ") - .appendValue(postId); + if (!postReply.getId().equals(postReplyId)) { + mismatchDescription.appendText("is post reply ") + .appendValue(postReply.getId()); return false; } - if (post.getTime() != time) { - mismatchDescription.appendText("Time is not @") - .appendValue(time); + if (!postReply.getPostId().equals(postId)) { + mismatchDescription.appendText("is reply to ") + .appendValue(postReply.getPostId()); return false; } - if (!post.getText().equals(text)) { - mismatchDescription.appendText("Text is not ") - .appendValue(text); + if (postReply.getTime() != time) { + mismatchDescription.appendText("is created at @").appendValue( + postReply.getTime()); 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; - } + if (!postReply.getText().equals(text)) { + mismatchDescription.appendText("says ") + .appendValue(postReply.getText()); + 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()); - } + description.appendText("is post reply ").appendValue(postReplyId); + description.appendText(", replies to post ").appendValue(postId); + description.appendText(", is created at @").appendValue(time); + description.appendText(", says ").appendValue(text); } }