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;
import java.util.List;
import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.data.Profile;
import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.database.Database;
import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
import net.pterodactylus.sone.freenet.fcp.FcpException;
public class AbstractSoneCommandTest {
private final Core core = mock(Core.class);
+ private final Database database = mock(Database.class);
private final AbstractSoneCommand abstractSoneCommand = new AbstractSoneCommand(core) {
@Override
public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException {
}
};
+ public AbstractSoneCommandTest() {
+ when(core.getDatabase()).thenReturn(database);
+ }
+
@Test
public void testStringEncoding() {
String testString = prepareStringToBeEncoded();
PostReply postReply = mock(PostReply.class);
when(postReply.getId()).thenReturn(randomUUID().toString());
when(postReply.getSone()).thenReturn(sone);
- when(postReply.getTime()).thenReturn((long) (Math.random() * Long.MAX_VALUE));
+ when(postReply.getTime()).thenReturn(System.currentTimeMillis());
when(postReply.getText()).thenReturn(text);
return postReply;
}
+ private PostReply createFuturePostReply(Sone sone, String text) {
+ PostReply postReply = createPostReply(sone, text);
+ when(postReply.getTime()).thenReturn(System.currentTimeMillis() + 86400000);
+ return postReply;
+ }
+
@Test
public void testEncodingLikes() throws FSParseException {
List<Sone> likes = prepareMultipleSones();
abstractSoneCommand.getMandatoryLocalSone(soneFieldSet, "RealSone");
}
+ @Test
+ public void testParsingAnExistingOptionalSone() throws FcpException {
+ Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
+ when(core.getSone(eq("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E"))).thenReturn(of(sone));
+ SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
+ Optional<Sone> parsedSone = abstractSoneCommand.getOptionalSone(soneFieldSet, "Sone");
+ assertThat(parsedSone, notNullValue());
+ assertThat(parsedSone.isPresent(), is(true));
+ assertThat(parsedSone.get(), is(sone));
+ }
+
+ @Test
+ public void testParsingANonExistingOptionalSone() throws FcpException {
+ when(core.getSone(Matchers.<String>any())).thenReturn(Optional.<Sone>absent());
+ SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
+ Optional<Sone> parsedSone = abstractSoneCommand.getOptionalSone(soneFieldSet, "Sone");
+ assertThat(parsedSone, notNullValue());
+ assertThat(parsedSone.isPresent(), is(false));
+ }
+
+ @Test(expected = FcpException.class)
+ public void testParsingAnOptionalSoneFromANonExistingFieldCausesAnError() throws FcpException {
+ when(core.getSone(Matchers.<String>any())).thenReturn(Optional.<Sone>absent());
+ SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
+ abstractSoneCommand.getOptionalSone(soneFieldSet, "RealSone");
+ }
+
+ @Test
+ public void testParsingAPost() throws FcpException {
+ 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, notNullValue());
+ assertThat(parsedPost, is(post));
+ }
+
+ 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 {
+ 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");
+ }
+
+ @Test(expected = FcpException.class)
+ public void testThatTryingToParseAPostFromANonExistingFieldCausesAnError() throws FcpException {
+ SimpleFieldSet postFieldSet = new SimpleFieldSetBuilder().get();
+ 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");
+ }
+
+ @Test
+ public void testEncodingAPostWithoutRecipientAndReplies() throws FSParseException {
+ 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.");
+ SimpleFieldSet postFieldSet = abstractSoneCommand.encodePost(post, "Post.");
+ assertThat(postFieldSet, notNullValue());
+ assertThat(postFieldSet.get("Post.ID"), is(post.getId()));
+ assertThat(postFieldSet.get("Post.Sone"), is(sone.getId()));
+ assertThat(postFieldSet.get("Post.Recipient"), nullValue());
+ assertThat(postFieldSet.getLong("Post.Time"), is(post.getTime()));
+ assertThat(postFieldSet.get("Post.Text"), is(post.getText()));
+ }
+
+ @Test
+ public void testEncodingAPostWithRecipientWithoutReplies() throws FSParseException {
+ Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
+ Post post = createPost(sone, "KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", (long) (Math.random() * Long.MAX_VALUE), "Some Text.");
+ SimpleFieldSet postFieldSet = abstractSoneCommand.encodePost(post, "Post.");
+ assertThat(postFieldSet, notNullValue());
+ assertThat(postFieldSet.get("Post.ID"), is(post.getId()));
+ assertThat(postFieldSet.get("Post.Sone"), is(sone.getId()));
+ assertThat(postFieldSet.get("Post.Recipient"), is("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg"));
+ assertThat(postFieldSet.getLong("Post.Time"), is(post.getTime()));
+ assertThat(postFieldSet.get("Post.Text"), is(post.getText()));
+ }
+
+ @Test
+ public void testEncodingAPostWithoutRecipientWithReplies() throws FSParseException {
+ 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.");
+ PostReply postReply = createPostReply(sone, "Reply.");
+ when(post.getReplies()).thenReturn(asList(postReply));
+ SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostWithReplies(post, "Post.");
+ assertThat(postFieldSet, notNullValue());
+ assertThat(postFieldSet.get("Post.ID"), is(post.getId()));
+ assertThat(postFieldSet.get("Post.Sone"), is(sone.getId()));
+ assertThat(postFieldSet.get("Post.Recipient"), nullValue());
+ assertThat(postFieldSet.getLong("Post.Time"), is(post.getTime()));
+ assertThat(postFieldSet.get("Post.Text"), is(post.getText()));
+ 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()));
+ }
+
+ @Test
+ public void testEncodingAPostWithoutRecipientWithFutureReplies() throws FSParseException {
+ 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.");
+ PostReply postReply = createFuturePostReply(sone, "Reply.");
+ when(post.getReplies()).thenReturn(asList(postReply));
+ SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostWithReplies(post, "Post.");
+ assertThat(postFieldSet, notNullValue());
+ assertThat(postFieldSet.get("Post.ID"), is(post.getId()));
+ assertThat(postFieldSet.get("Post.Sone"), is(sone.getId()));
+ assertThat(postFieldSet.get("Post.Recipient"), nullValue());
+ assertThat(postFieldSet.getLong("Post.Time"), is(post.getTime()));
+ assertThat(postFieldSet.get("Post.Text"), is(post.getText()));
+ 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());
+ }
+
+ @Test
+ public void testEncodingAPostWithRecipientAndReplies() throws FSParseException {
+ Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
+ Post post = createPost(sone, "KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", (long) (Math.random() * Long.MAX_VALUE), "Some Text.");
+ PostReply postReply = createPostReply(sone, "Reply.");
+ when(post.getReplies()).thenReturn(asList(postReply));
+ SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostWithReplies(post, "Post.");
+ assertThat(postFieldSet, notNullValue());
+ assertThat(postFieldSet.get("Post.ID"), is(post.getId()));
+ assertThat(postFieldSet.get("Post.Sone"), is(sone.getId()));
+ assertThat(postFieldSet.get("Post.Recipient"), is("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg"));
+ assertThat(postFieldSet.getLong("Post.Time"), is(post.getTime()));
+ assertThat(postFieldSet.get("Post.Text"), is(post.getText()));
+ 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()));
+ }
+
}