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;
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;
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<PostReply> 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
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.<PostReply>emptyList());
}
@Test
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
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<Post> 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
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
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<Post> 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
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
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));
}
}