X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ffcp%2FAbstractSoneCommandTest.java;h=54c19080a00073a07fe33927144688333f41b774;hb=ba696c9462be0d282dce5dfde38ecf99a59ed864;hp=c7c46a0a631d37c3f536829e17bd43500cbb7de2;hpb=4993c92530ee553da52dbb4642c002f1c4e35615;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 c7c46a0..54c1908 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/AbstractSoneCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/AbstractSoneCommandTest.java @@ -27,17 +27,24 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; 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; import freenet.node.FSParseException; import freenet.support.SimpleFieldSet; +import freenet.support.api.Bucket; import com.google.common.base.Optional; import org.junit.Test; @@ -50,6 +57,19 @@ 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 { + return null; + } + }; + + public AbstractSoneCommandTest() { + when(core.getDatabase()).thenReturn(database); + } + @Test public void testStringEncoding() { String testString = prepareStringToBeEncoded(); @@ -159,6 +179,16 @@ public class AbstractSoneCommandTest { return sone; } + private Sone createLocalSone(String id, String name, String firstName, String middleName, String lastName, long time) { + Sone sone = mock(Sone.class); + when(sone.getId()).thenReturn(id); + when(sone.getName()).thenReturn(name); + when(sone.getProfile()).thenReturn(prepareProfile(sone, firstName, middleName, lastName)); + when(sone.getTime()).thenReturn(time); + when(sone.isLocal()).thenReturn(true); + return sone; + } + private Profile prepareProfile(Sone sone, String firstName, String middleName, String lastName) { Profile profile = new Profile(sone).modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update(); profile.setField(profile.addField("Test1"), "Value1"); @@ -204,4 +234,158 @@ public class AbstractSoneCommandTest { return postReply; } + @Test + public void testEncodingLikes() throws FSParseException { + List likes = prepareMultipleSones(); + SimpleFieldSet likesFieldSet = AbstractSoneCommand.encodeLikes(likes, "Prefix."); + assertThat(likesFieldSet, notNullValue()); + assertThat(likesFieldSet.getInt("Prefix.Count"), is(likes.size())); + assertThat(likesFieldSet.get("Prefix.0.ID"), is(likes.get(0).getId())); + assertThat(likesFieldSet.get("Prefix.1.ID"), is(likes.get(1).getId())); + assertThat(likesFieldSet.get("Prefix.2.ID"), is(likes.get(2).getId())); + } + + @Test + public void testParsingAMandatorySone() 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(); + Sone parsedSone = abstractSoneCommand.getMandatorySone(soneFieldSet, "Sone"); + assertThat(parsedSone, notNullValue()); + assertThat(parsedSone, is(sone)); + } + + @Test(expected = FcpException.class) + public void testParsingANonExistingMandatorySoneCausesAnError() throws FcpException { + when(core.getSone(Matchers.any())).thenReturn(Optional.absent()); + SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get(); + abstractSoneCommand.getMandatorySone(soneFieldSet, "Sone"); + } + + @Test(expected = FcpException.class) + public void testParsingAMandatorySoneFromANonExistingFieldCausesAnError() throws FcpException { + when(core.getSone(Matchers.any())).thenReturn(Optional.absent()); + SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get(); + abstractSoneCommand.getMandatorySone(soneFieldSet, "RealSone"); + } + + @Test + public void testParsingAMandatoryLocalSone() throws FcpException { + Sone sone = createLocalSone("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(); + Sone parsedSone = abstractSoneCommand.getMandatoryLocalSone(soneFieldSet, "Sone"); + assertThat(parsedSone, notNullValue()); + assertThat(parsedSone, is(sone)); + assertThat(parsedSone.isLocal(), is(true)); + } + + @Test(expected = FcpException.class) + public void testParsingANonLocalSoneAsMandatoryLocalSoneCausesAnError() 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(); + abstractSoneCommand.getMandatoryLocalSone(soneFieldSet, "Sone"); + } + + @Test(expected = FcpException.class) + public void testParsingAMandatoryLocalSoneFromANonExistingFieldCausesAnError() throws FcpException { + when(core.getSone(Matchers.any())).thenReturn(Optional.absent()); + SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get(); + 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 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.any())).thenReturn(Optional.absent()); + SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get(); + Optional 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.any())).thenReturn(Optional.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, (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, long time, 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.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, (long) (Math.random() * Long.MAX_VALUE), "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"); + } + }