Move more post and reply verifiers to the Verifiers class.
[Sone.git] / src / test / java / net / pterodactylus / sone / fcp / Verifiers.java
index 3b6e3a0..82f28d9 100644 (file)
 
 package net.pterodactylus.sone.fcp;
 
+import static com.google.common.collect.FluentIterable.from;
 import static java.lang.String.format;
+import static net.pterodactylus.sone.data.Reply.FUTURE_REPLY_FILTER;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
 
+import java.util.Collection;
+
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
 
 import freenet.node.FSParseException;
 import freenet.support.SimpleFieldSet;
 
+import org.hamcrest.CoreMatchers;
+
 /**
  * Verifiers used throughout the {@link net.pterodactylus.sone.fcp} package.
  *
@@ -42,6 +48,15 @@ public class Verifiers {
                assertThat(replyParameters.get(format("%sText", prefix)), is(post.getText()));
        }
 
+       static void verifyPosts(SimpleFieldSet postFieldSet, String prefix, Collection<Post> posts) throws FSParseException {
+               assertThat(postFieldSet.getInt(prefix + "Count"), CoreMatchers.is(posts.size()));
+               int postIndex = 0;
+               for (Post post : posts) {
+                       verifyPost(postFieldSet, prefix + postIndex + ".", post);
+                       postIndex++;
+               }
+       }
+
        static void verifyPostReply(SimpleFieldSet replyParameters, String prefix, PostReply postReply) throws FSParseException {
                assertThat(replyParameters.get(format("%sID", prefix)), is(postReply.getId()));
                assertThat(replyParameters.get(format("%sSone", prefix)), is(postReply.getSone().getId()));
@@ -49,4 +64,28 @@ public class Verifiers {
                assertThat(replyParameters.get(format("%sText", prefix)), is(postReply.getText()));
        }
 
+       static void verifyPostReplies(SimpleFieldSet postFieldSet, String prefix, Collection<PostReply> postReplies) throws FSParseException {
+               assertThat(postFieldSet.getInt(prefix + "Count"), CoreMatchers.is(from(postReplies).filter(FUTURE_REPLY_FILTER).size()));
+               int postReplyIndex = 0;
+               for (PostReply postReply : from(postReplies).filter(FUTURE_REPLY_FILTER)) {
+                       verifyPostReply(postFieldSet, prefix + postReplyIndex + ".", postReply);
+                       postReplyIndex++;
+               }
+       }
+
+       static void verifyPostsWithReplies(SimpleFieldSet postFieldSet, String prefix, Collection<Post> posts) throws FSParseException {
+               assertThat(postFieldSet.getInt(prefix + "Count"), CoreMatchers.is(posts.size()));
+               int postIndex = 0;
+               for (Post post : posts) {
+                       verifyPost(postFieldSet, prefix + postIndex + ".", post);
+                       verifyPostReplies(postFieldSet, prefix + postIndex + ".Replies.", post.getReplies());
+                       postIndex++;
+               }
+       }
+
+       static void verifyPostWithReplies(SimpleFieldSet postFieldSet, String prefix, Post post) throws FSParseException {
+               verifyPost(postFieldSet, prefix, post);
+               verifyPostReplies(postFieldSet, prefix + "Replies.", post.getReplies());
+       }
+
 }