X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ffcp%2FAbstractSoneCommandTest.java;h=0595a9bff176ae7362c285d6a6d011a707687536;hb=84ff475ffaf47ed2461babf431babff6592d39a9;hp=5c210712e8d490d7c0d5fe1a4b110f178ba88271;hpb=ec0589a84453ee2018a5aab6341b90924fde467f;p=Sone.git diff --git a/src/test/java/net/pterodactylus/sone/fcp/AbstractSoneCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/AbstractSoneCommandTest.java index 5c21071..0595a9b 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/AbstractSoneCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/AbstractSoneCommandTest.java @@ -19,8 +19,10 @@ package net.pterodactylus.sone.fcp; import static com.google.common.base.Optional.fromNullable; import static com.google.common.base.Optional.of; +import static com.google.common.collect.FluentIterable.from; import static java.util.Arrays.asList; import static java.util.UUID.randomUUID; +import static net.pterodactylus.sone.data.Reply.FUTURE_REPLY_FILTER; import static net.pterodactylus.sone.fcp.AbstractSoneCommand.encodeSone; import static net.pterodactylus.sone.fcp.AbstractSoneCommand.encodeString; import static net.pterodactylus.sone.template.SoneAccessor.getNiceName; @@ -32,6 +34,8 @@ import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import java.util.Collection; +import java.util.Collections; import java.util.List; import net.pterodactylus.sone.core.Core; @@ -430,11 +434,19 @@ public class AbstractSoneCommandTest { SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostWithReplies(post, "Post."); assertThat(postFieldSet, notNullValue()); verifyPost(postFieldSet, "Post.", post); - assertThat(postFieldSet.getInt("Post.Replies.Count"), is(1)); - assertThat(postFieldSet.get("Post.Replies.0.ID"), is(postReply.getId())); - assertThat(postFieldSet.get("Post.Replies.0.Sone"), is(postReply.getSone().getId())); - assertThat(postFieldSet.getLong("Post.Replies.0.Time"), is(postReply.getTime())); - assertThat(postFieldSet.get("Post.Replies.0.Text"), is(postReply.getText())); + verifyPostReplies(postFieldSet, "Post.", asList(postReply)); + } + + private void verifyPostReplies(SimpleFieldSet postFieldSet, String prefix, Collection postReplies) throws FSParseException { + assertThat(postFieldSet.getInt(prefix + "Replies.Count"), is(from(postReplies).filter(FUTURE_REPLY_FILTER).size())); + int postReplyIndex = 0; + for (PostReply postReply : from(postReplies).filter(FUTURE_REPLY_FILTER)) { + assertThat(postFieldSet.get(prefix + "Replies." + postReplyIndex + ".ID"), is(postReply.getId())); + assertThat(postFieldSet.get(prefix + "Replies." + postReplyIndex + ".Sone"), is(postReply.getSone().getId())); + assertThat(postFieldSet.getLong(prefix + "Replies." + postReplyIndex + ".Time"), is(postReply.getTime())); + assertThat(postFieldSet.get(prefix + "Replies." + postReplyIndex + ".Text"), is(postReply.getText())); + postReplyIndex++; + } } @Test @@ -446,11 +458,7 @@ public class AbstractSoneCommandTest { SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostWithReplies(post, "Post."); assertThat(postFieldSet, notNullValue()); verifyPost(postFieldSet, "Post.", post); - assertThat(postFieldSet.getInt("Post.Replies.Count"), is(0)); - assertThat(postFieldSet.get("Post.Replies.0.ID"), nullValue()); - assertThat(postFieldSet.get("Post.Replies.0.Sone"), nullValue()); - assertThat(postFieldSet.get("Post.Replies.0.Time"), nullValue()); - assertThat(postFieldSet.get("Post.Replies.0.Text"), nullValue()); + verifyPostReplies(postFieldSet, "Post.", Collections.emptyList()); } @Test @@ -462,11 +470,7 @@ public class AbstractSoneCommandTest { SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostWithReplies(post, "Post."); assertThat(postFieldSet, notNullValue()); verifyPost(postFieldSet, "Post.", post); - assertThat(postFieldSet.getInt("Post.Replies.Count"), is(1)); - assertThat(postFieldSet.get("Post.Replies.0.ID"), is(postReply.getId())); - assertThat(postFieldSet.get("Post.Replies.0.Sone"), is(postReply.getSone().getId())); - assertThat(postFieldSet.getLong("Post.Replies.0.Time"), is(postReply.getTime())); - assertThat(postFieldSet.get("Post.Replies.0.Text"), is(postReply.getText())); + verifyPostReplies(postFieldSet, "Post.", asList(postReply)); } @Test @@ -477,17 +481,16 @@ public class AbstractSoneCommandTest { Post post2 = createPost(sone2, null, (long) (Math.random() * Long.MAX_VALUE), "Some other Text."); SimpleFieldSet postFieldSet = abstractSoneCommand.encodePosts(asList(post1, post2), "Posts."); assertThat(postFieldSet, notNullValue()); - assertThat(postFieldSet.getInt("Posts.Count"), is(2)); - assertThat(postFieldSet.get("Posts.0.ID"), is(post1.getId())); - assertThat(postFieldSet.get("Posts.0.Sone"), is(sone1.getId())); - assertThat(postFieldSet.get("Posts.0.Recipient"), nullValue()); - assertThat(postFieldSet.getLong("Posts.0.Time"), is(post1.getTime())); - assertThat(postFieldSet.get("Posts.0.Text"), is(post1.getText())); - assertThat(postFieldSet.get("Posts.1.ID"), is(post2.getId())); - assertThat(postFieldSet.get("Posts.1.Sone"), is(sone2.getId())); - assertThat(postFieldSet.get("Posts.1.Recipient"), nullValue()); - assertThat(postFieldSet.getLong("Posts.1.Time"), is(post2.getTime())); - assertThat(postFieldSet.get("Posts.1.Text"), is(post2.getText())); + verifyPosts(postFieldSet, "Posts.", asList(post1, post2)); + } + + private void verifyPosts(SimpleFieldSet postFieldSet, String prefix, Collection posts) throws FSParseException { + assertThat(postFieldSet.getInt(prefix + "Count"), is(posts.size())); + int postIndex = 0; + for (Post post : posts) { + verifyPost(postFieldSet, prefix + postIndex + ".", post); + postIndex++; + } } @Test @@ -498,17 +501,7 @@ public class AbstractSoneCommandTest { Post post2 = createPost(sone2, "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", (long) (Math.random() * Long.MAX_VALUE), "Some other Text."); SimpleFieldSet postFieldSet = abstractSoneCommand.encodePosts(asList(post1, post2), "Posts."); assertThat(postFieldSet, notNullValue()); - assertThat(postFieldSet.getInt("Posts.Count"), is(2)); - assertThat(postFieldSet.get("Posts.0.ID"), is(post1.getId())); - assertThat(postFieldSet.get("Posts.0.Sone"), is(sone1.getId())); - assertThat(postFieldSet.get("Posts.0.Recipient"), is("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg")); - assertThat(postFieldSet.getLong("Posts.0.Time"), is(post1.getTime())); - assertThat(postFieldSet.get("Posts.0.Text"), is(post1.getText())); - assertThat(postFieldSet.get("Posts.1.ID"), is(post2.getId())); - assertThat(postFieldSet.get("Posts.1.Sone"), is(sone2.getId())); - assertThat(postFieldSet.get("Posts.1.Recipient"), is("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E")); - assertThat(postFieldSet.getLong("Posts.1.Time"), is(post2.getTime())); - assertThat(postFieldSet.get("Posts.1.Text"), is(post2.getText())); + verifyPosts(postFieldSet, "Posts.", asList(post1, post2)); } @Test @@ -523,27 +516,17 @@ public class AbstractSoneCommandTest { when(post2.getReplies()).thenReturn(asList(postReply2)); SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostsWithReplies(asList(post1, post2), "Posts."); assertThat(postFieldSet, notNullValue()); - assertThat(postFieldSet.getInt("Posts.Count"), is(2)); - assertThat(postFieldSet.get("Posts.0.ID"), is(post1.getId())); - assertThat(postFieldSet.get("Posts.0.Sone"), is(sone1.getId())); - assertThat(postFieldSet.get("Posts.0.Recipient"), nullValue()); - assertThat(postFieldSet.getLong("Posts.0.Time"), is(post1.getTime())); - assertThat(postFieldSet.get("Posts.0.Text"), is(post1.getText())); - assertThat(postFieldSet.getInt("Posts.0.Replies.Count"), is(1)); - assertThat(postFieldSet.get("Posts.0.Replies.0.ID"), is(postReply1.getId())); - assertThat(postFieldSet.get("Posts.0.Replies.0.Sone"), is(postReply1.getSone().getId())); - assertThat(postFieldSet.getLong("Posts.0.Replies.0.Time"), is(postReply1.getTime())); - assertThat(postFieldSet.get("Posts.0.Replies.0.Text"), is(postReply1.getText())); - assertThat(postFieldSet.get("Posts.1.ID"), is(post2.getId())); - assertThat(postFieldSet.get("Posts.1.Sone"), is(sone2.getId())); - assertThat(postFieldSet.get("Posts.1.Recipient"), nullValue()); - assertThat(postFieldSet.getLong("Posts.1.Time"), is(post2.getTime())); - assertThat(postFieldSet.get("Posts.1.Text"), is(post2.getText())); - assertThat(postFieldSet.getInt("Posts.1.Replies.Count"), is(1)); - assertThat(postFieldSet.get("Posts.1.Replies.0.ID"), is(postReply2.getId())); - assertThat(postFieldSet.get("Posts.1.Replies.0.Sone"), is(postReply2.getSone().getId())); - assertThat(postFieldSet.getLong("Posts.1.Replies.0.Time"), is(postReply2.getTime())); - assertThat(postFieldSet.get("Posts.1.Replies.0.Text"), is(postReply2.getText())); + verifyPostsWithReplies(postFieldSet, "Posts.", asList(post1, post2)); + } + + private void verifyPostsWithReplies(SimpleFieldSet postFieldSet, String prefix, Collection posts) throws FSParseException { + assertThat(postFieldSet.getInt(prefix + "Count"), is(posts.size())); + int postIndex = 0; + for (Post post : posts) { + verifyPost(postFieldSet, prefix + postIndex + ".", post); + verifyPostReplies(postFieldSet, prefix + postIndex + ".", post.getReplies()); + postIndex++; + } } @Test @@ -558,27 +541,7 @@ public class AbstractSoneCommandTest { when(post2.getReplies()).thenReturn(asList(postReply2)); SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostsWithReplies(asList(post1, post2), "Posts."); assertThat(postFieldSet, notNullValue()); - assertThat(postFieldSet.getInt("Posts.Count"), is(2)); - assertThat(postFieldSet.get("Posts.0.ID"), is(post1.getId())); - assertThat(postFieldSet.get("Posts.0.Sone"), is(sone1.getId())); - assertThat(postFieldSet.get("Posts.0.Recipient"), is("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg")); - assertThat(postFieldSet.getLong("Posts.0.Time"), is(post1.getTime())); - assertThat(postFieldSet.get("Posts.0.Text"), is(post1.getText())); - assertThat(postFieldSet.getInt("Posts.0.Replies.Count"), is(1)); - assertThat(postFieldSet.get("Posts.0.Replies.0.ID"), is(postReply1.getId())); - assertThat(postFieldSet.get("Posts.0.Replies.0.Sone"), is(postReply1.getSone().getId())); - assertThat(postFieldSet.getLong("Posts.0.Replies.0.Time"), is(postReply1.getTime())); - assertThat(postFieldSet.get("Posts.0.Replies.0.Text"), is(postReply1.getText())); - assertThat(postFieldSet.get("Posts.1.ID"), is(post2.getId())); - assertThat(postFieldSet.get("Posts.1.Sone"), is(sone2.getId())); - assertThat(postFieldSet.get("Posts.1.Recipient"), is("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E")); - assertThat(postFieldSet.getLong("Posts.1.Time"), is(post2.getTime())); - assertThat(postFieldSet.get("Posts.1.Text"), is(post2.getText())); - assertThat(postFieldSet.getInt("Posts.1.Replies.Count"), is(1)); - assertThat(postFieldSet.get("Posts.1.Replies.0.ID"), is(postReply2.getId())); - assertThat(postFieldSet.get("Posts.1.Replies.0.Sone"), is(postReply2.getSone().getId())); - assertThat(postFieldSet.getLong("Posts.1.Replies.0.Time"), is(postReply2.getTime())); - assertThat(postFieldSet.get("Posts.1.Replies.0.Text"), is(postReply2.getText())); + verifyPostsWithReplies(postFieldSet, "Posts.", asList(post1, post2)); } @Test @@ -593,23 +556,7 @@ public class AbstractSoneCommandTest { when(post2.getReplies()).thenReturn(asList(postReply2)); SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostsWithReplies(asList(post1, post2), "Posts."); assertThat(postFieldSet, notNullValue()); - assertThat(postFieldSet.getInt("Posts.Count"), is(2)); - assertThat(postFieldSet.get("Posts.0.ID"), is(post1.getId())); - assertThat(postFieldSet.get("Posts.0.Sone"), is(sone1.getId())); - assertThat(postFieldSet.get("Posts.0.Recipient"), is("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg")); - assertThat(postFieldSet.getLong("Posts.0.Time"), is(post1.getTime())); - assertThat(postFieldSet.get("Posts.0.Text"), is(post1.getText())); - assertThat(postFieldSet.getInt("Posts.0.Replies.Count"), is(1)); - assertThat(postFieldSet.get("Posts.0.Replies.0.ID"), is(postReply1.getId())); - assertThat(postFieldSet.get("Posts.0.Replies.0.Sone"), is(postReply1.getSone().getId())); - assertThat(postFieldSet.getLong("Posts.0.Replies.0.Time"), is(postReply1.getTime())); - assertThat(postFieldSet.get("Posts.0.Replies.0.Text"), is(postReply1.getText())); - assertThat(postFieldSet.get("Posts.1.ID"), is(post2.getId())); - assertThat(postFieldSet.get("Posts.1.Sone"), is(sone2.getId())); - assertThat(postFieldSet.get("Posts.1.Recipient"), is("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E")); - assertThat(postFieldSet.getLong("Posts.1.Time"), is(post2.getTime())); - assertThat(postFieldSet.get("Posts.1.Text"), is(post2.getText())); - assertThat(postFieldSet.getInt("Posts.1.Replies.Count"), is(0)); + verifyPostsWithReplies(postFieldSet, "Posts.", asList(post1, post2)); } }