package net.pterodactylus.sone.fcp;
+import static com.google.common.base.Optional.fromNullable;
import static com.google.common.base.Optional.of;
import static java.util.Arrays.asList;
import static java.util.UUID.randomUUID;
@Test
public void testParsingAPost() throws FcpException {
- Post post = createPost();
+ Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
+ Post post = createPost(sone, null, (long) (Math.random() * Long.MAX_VALUE), "Some Text.");
when(database.getPost(eq(post.getId()))).thenReturn(of(post));
SimpleFieldSet postFieldSet = new SimpleFieldSetBuilder().put("Post", post.getId()).get();
Post parsedPost = abstractSoneCommand.getPost(postFieldSet, "Post");
assertThat(parsedPost, is(post));
}
- private Post createPost() {
+ private Post createPost(Sone sone, String recipient, long time, String text) {
Post post = mock(Post.class);
when(post.getId()).thenReturn(randomUUID().toString());
+ when(post.getSone()).thenReturn(sone);
+ when(post.getRecipientId()).thenReturn(fromNullable(recipient));
+ when(post.getTime()).thenReturn(time);
+ when(post.getText()).thenReturn(text);
return post;
}
@Test(expected = FcpException.class)
public void testThatTryingToParseANonExistingPostCausesAnError() throws FcpException {
- Post post = createPost();
+ Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
+ Post post = createPost(sone, null, (long) (Math.random() * Long.MAX_VALUE), "Some Text.");
when(database.getPost(Matchers.<String>any())).thenReturn(Optional.<Post>absent());
SimpleFieldSet postFieldSet = new SimpleFieldSetBuilder().put("Post", post.getId()).get();
abstractSoneCommand.getPost(postFieldSet, "Post");
abstractSoneCommand.getPost(postFieldSet, "Post");
}
+ @Test
+ public void testParsingAReply() throws FcpException {
+ PostReply reply = createPostReply();
+ when(database.getPostReply(eq(reply.getId()))).thenReturn(of(reply));
+ SimpleFieldSet replyFieldSet = new SimpleFieldSetBuilder().put("Reply", reply.getId()).get();
+ PostReply parsedReply = abstractSoneCommand.getReply(replyFieldSet, "Reply");
+ assertThat(parsedReply, notNullValue());
+ assertThat(parsedReply, is(reply));
+ }
+
+ private PostReply createPostReply() {
+ PostReply postReply = mock(PostReply.class);
+ when(postReply.getId()).thenReturn(randomUUID().toString());
+ return postReply;
+ }
+
+ @Test(expected = FcpException.class)
+ public void testParsingANonExistingReply() throws FcpException {
+ PostReply reply = createPostReply();
+ when(database.getPostReply(Matchers.<String>any())).thenReturn(Optional.<PostReply>absent());
+ SimpleFieldSet replyFieldSet = new SimpleFieldSetBuilder().put("Reply", reply.getId()).get();
+ abstractSoneCommand.getReply(replyFieldSet, "Reply");
+ }
+
+ @Test(expected = FcpException.class)
+ public void testParsingAReplyFromANonExistingField() throws FcpException {
+ SimpleFieldSet replyFieldSet = new SimpleFieldSetBuilder().get();
+ abstractSoneCommand.getReply(replyFieldSet, "Reply");
+ }
+
}