X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ffcp%2FAbstractSoneCommandTest.java;h=d045be2e1688f29caaa0fc39bb389efc052589e8;hb=17a1fc74298d28fca1de3a973f1f52c2faeb2708;hp=90f78cc60e35a9a0d9705fc8640ea6b08a8ea549;hpb=4182d9b5708c7b6cd3f9667ed36793722d393484;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 90f78cc..d045be2 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/AbstractSoneCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/AbstractSoneCommandTest.java @@ -34,9 +34,11 @@ import static org.mockito.Mockito.when; 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; @@ -56,6 +58,7 @@ import org.mockito.Matchers; 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 { @@ -63,6 +66,10 @@ public class AbstractSoneCommandTest { } }; + public AbstractSoneCommandTest() { + when(core.getDatabase()).thenReturn(database); + } + @Test public void testStringEncoding() { String testString = prepareStringToBeEncoded(); @@ -315,4 +322,69 @@ public class AbstractSoneCommandTest { 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, "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 text) { + Post post = mock(Post.class); + when(post.getId()).thenReturn(randomUUID().toString()); + when(post.getSone()).thenReturn(sone); + when(post.getRecipientId()).thenReturn(Optional.absent()); + 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, "Some Text."); + when(database.getPost(Matchers.any())).thenReturn(Optional.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.any())).thenReturn(Optional.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"); + } + }