Add tests for parsing a post.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 25 Oct 2013 23:25:13 +0000 (01:25 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:45 +0000 (22:25 +0100)
src/test/java/net/pterodactylus/sone/fcp/AbstractSoneCommandTest.java

index 90f78cc..787baea 100644 (file)
@@ -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,34 @@ public class AbstractSoneCommandTest {
                abstractSoneCommand.getOptionalSone(soneFieldSet, "RealSone");
        }
 
+       @Test
+       public void testParsingAPost() throws FcpException {
+               Post post = createPost();
+               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() {
+               Post post = mock(Post.class);
+               when(post.getId()).thenReturn(randomUUID().toString());
+               return post;
+       }
+
+       @Test(expected = FcpException.class)
+       public void testThatTryingToParseANonExistingPostCausesAnError() throws FcpException {
+               Post post = createPost();
+               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");
+       }
+
 }