X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ffcp%2FAbstractSoneCommandTest.java;h=59287e1bbeab8c13ec337a3f268d56b21e9cf19e;hb=2d37242d19f2e726cd402b99f935a0eba282f630;hp=964f97a8c4f5f4cc95b5943de324081ff880bde2;hpb=ae1f3460b6a09cd85c7ac0b07d724dd47284434c;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 964f97a..59287e1 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/AbstractSoneCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/AbstractSoneCommandTest.java @@ -18,14 +18,15 @@ package net.pterodactylus.sone.fcp; import static com.google.common.base.Optional.of; -import static com.google.common.collect.FluentIterable.from; import static java.lang.System.currentTimeMillis; import static java.util.Arrays.asList; import static java.util.UUID.randomUUID; import static java.util.concurrent.TimeUnit.DAYS; -import static net.pterodactylus.sone.data.Reply.FUTURE_REPLY_FILTER; import static net.pterodactylus.sone.fcp.AbstractSoneCommand.encodeSone; import static net.pterodactylus.sone.fcp.AbstractSoneCommand.encodeString; +import static net.pterodactylus.sone.Verifiers.verifyPostWithReplies; +import static net.pterodactylus.sone.Verifiers.verifyPosts; +import static net.pterodactylus.sone.Verifiers.verifyPostsWithReplies; import static net.pterodactylus.sone.template.SoneAccessor.getNiceName; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; @@ -34,8 +35,6 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import java.util.Collection; -import java.util.Collections; import java.util.List; import net.pterodactylus.sone.data.Mocks; @@ -271,10 +270,12 @@ public class AbstractSoneCommandTest { assertThat(parsedSone.isPresent(), is(false)); } - @Test(expected = FcpException.class) - public void testParsingAnOptionalSoneFromANonExistingFieldCausesAnError() throws FcpException { + @Test + public void testParsingAnOptionalSoneFromANonExistingField() throws FcpException { SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get(); - abstractSoneCommand.getOptionalSone(soneFieldSet, "RealSone"); + Optional sone = abstractSoneCommand.getOptionalSone(soneFieldSet, "RealSone"); + assertThat(sone, notNullValue()); + assertThat(sone.isPresent(), is(false)); } @Test @@ -355,20 +356,7 @@ public class AbstractSoneCommandTest { when(post.getReplies()).thenReturn(asList(postReply)); SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostWithReplies(post, "Post."); assertThat(postFieldSet, notNullValue()); - verifyPost(postFieldSet, "Post.", post); - verifyPostReplies(postFieldSet, "Post.", asList(postReply)); - } - - private void verifyPostReplies(SimpleFieldSet postFieldSet, String prefix, Collection postReplies) throws FSParseException { - assertThat(postFieldSet.getInt(prefix + "Replies.Count"), is(from(postReplies).filter(FUTURE_REPLY_FILTER).size())); - int postReplyIndex = 0; - for (PostReply postReply : from(postReplies).filter(FUTURE_REPLY_FILTER)) { - assertThat(postFieldSet.get(prefix + "Replies." + postReplyIndex + ".ID"), is(postReply.getId())); - assertThat(postFieldSet.get(prefix + "Replies." + postReplyIndex + ".Sone"), is(postReply.getSone().getId())); - assertThat(postFieldSet.getLong(prefix + "Replies." + postReplyIndex + ".Time"), is(postReply.getTime())); - assertThat(postFieldSet.get(prefix + "Replies." + postReplyIndex + ".Text"), is(postReply.getText())); - postReplyIndex++; - } + verifyPostWithReplies(postFieldSet, "Post.", post); } @Test @@ -379,8 +367,7 @@ public class AbstractSoneCommandTest { when(post.getReplies()).thenReturn(asList(postReply)); SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostWithReplies(post, "Post."); assertThat(postFieldSet, notNullValue()); - verifyPost(postFieldSet, "Post.", post); - verifyPostReplies(postFieldSet, "Post.", Collections.emptyList()); + verifyPostWithReplies(postFieldSet, "Post.", post); } @Test @@ -391,8 +378,7 @@ public class AbstractSoneCommandTest { when(post.getReplies()).thenReturn(asList(postReply)); SimpleFieldSet postFieldSet = abstractSoneCommand.encodePostWithReplies(post, "Post."); assertThat(postFieldSet, notNullValue()); - verifyPost(postFieldSet, "Post.", post); - verifyPostReplies(postFieldSet, "Post.", asList(postReply)); + verifyPostWithReplies(postFieldSet, "Post.", post); } @Test @@ -406,15 +392,6 @@ public class AbstractSoneCommandTest { verifyPosts(postFieldSet, "Posts.", asList(post1, post2)); } - private void verifyPosts(SimpleFieldSet postFieldSet, String prefix, Collection posts) throws FSParseException { - assertThat(postFieldSet.getInt(prefix + "Count"), is(posts.size())); - int postIndex = 0; - for (Post post : posts) { - verifyPost(postFieldSet, prefix + postIndex + ".", post); - postIndex++; - } - } - @Test public void testEncodingPostsWithRecipientWithoutReplies() throws FSParseException { Sone sone1 = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test1").withProfileName("Alpha", "A.", "First").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create(); @@ -441,16 +418,6 @@ public class AbstractSoneCommandTest { verifyPostsWithReplies(postFieldSet, "Posts.", asList(post1, post2)); } - private void verifyPostsWithReplies(SimpleFieldSet postFieldSet, String prefix, Collection posts) throws FSParseException { - assertThat(postFieldSet.getInt(prefix + "Count"), is(posts.size())); - int postIndex = 0; - for (Post post : posts) { - verifyPost(postFieldSet, prefix + postIndex + ".", post); - verifyPostReplies(postFieldSet, prefix + postIndex + ".", post.getReplies()); - postIndex++; - } - } - @Test public void testEncodingPostsWithRecipientAndReplies() throws FSParseException { Sone sone1 = mocks.mockSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").withName("Test1").withProfileName("Alpha", "A.", "First").addProfileField("Test1", "Value1").withTime((long) (Math.random() * Long.MAX_VALUE)).create();