X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FConfigurationSoneParserTest.java;h=4e9e863e9abf1a0a86fa713f0b30e0d97c4cfa43;hp=daf2cf995c82c9188e49175015d38756d581588f;hb=fdc519da8a4d5c994c3b2233fc049e3be728bb76;hpb=11f3457415c9122b5d11840d24186971af28add9 diff --git a/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java b/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java index daf2cf9..4e9e863 100644 --- a/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java +++ b/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java @@ -5,8 +5,10 @@ import static com.google.common.base.Optional.of; import static java.lang.System.currentTimeMillis; import static java.util.UUID.randomUUID; import static net.pterodactylus.sone.Matchers.isPost; +import static net.pterodactylus.sone.Matchers.isPostReply; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.emptyIterable; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; @@ -22,12 +24,16 @@ import java.util.List; import java.util.concurrent.atomic.AtomicReference; import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostFound; +import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostReplyFound; import net.pterodactylus.sone.data.Post; +import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.data.Profile; import net.pterodactylus.sone.data.Profile.Field; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.database.PostBuilder; import net.pterodactylus.sone.database.PostBuilderFactory; +import net.pterodactylus.sone.database.PostReplyBuilder; +import net.pterodactylus.sone.database.PostReplyBuilderFactory; import net.pterodactylus.util.config.Configuration; import net.pterodactylus.util.config.ConfigurationException; import net.pterodactylus.util.config.Value; @@ -200,6 +206,68 @@ public class ConfigurationSoneParserTest { setupPost("1", null, 0L, null, null); } + @Test + public void postRepliesAreParsedCorrectly() { + setupPostReplies(); + PostReplyBuilderFactory postReplyBuilderFactory = + new PostReplyBuilderFactory() { + @Override + public PostReplyBuilder newPostReplyBuilder() { + return new TestPostReplyBuilder(); + } + }; + Collection postReplies = + configurationSoneParser.parsePostReplies( + postReplyBuilderFactory); + assertThat(postReplies, hasSize(2)); + assertThat(postReplies, + containsInAnyOrder(isPostReply("R0", "P0", 1000L, "T0"), + isPostReply("R1", "P1", 1001L, "T1"))); + } + + private void setupPostReplies() { + setupPostReply("0", "R0", "P0", 1000L, "T0"); + setupPostReply("1", "R1", "P1", 1001L, "T1"); + setupPostReply("2", null, null, 0L, null); + } + + private void setupPostReply(String postReplyNumber, String postReplyId, + String postId, long time, String text) { + setupString("Sone/1/Replies/" + postReplyNumber + "/ID", postReplyId); + setupString("Sone/1/Replies/" + postReplyNumber + "/Post/ID", postId); + setupLong("Sone/1/Replies/" + postReplyNumber + "/Time", time); + setupString("Sone/1/Replies/" + postReplyNumber + "/Text", text); + } + + @Test(expected = InvalidPostReplyFound.class) + public void missingPostIdIsRecognized() { + setupPostReplyWithMissingPostId(); + configurationSoneParser.parsePostReplies(null); + } + + private void setupPostReplyWithMissingPostId() { + setupPostReply("0", "R0", null, 1000L, "T0"); + } + + @Test(expected = InvalidPostReplyFound.class) + public void missingPostReplyTimeIsRecognized() { + setupPostReplyWithMissingPostReplyTime(); + configurationSoneParser.parsePostReplies(null); + } + + private void setupPostReplyWithMissingPostReplyTime() { + setupPostReply("0", "R0", "P0", 0L, "T0"); + } + + @Test(expected = InvalidPostReplyFound.class) + public void missingPostReplyTextIsRecognized() { + setupPostReplyWithMissingPostReplyText(); + configurationSoneParser.parsePostReplies(null); + } + + private void setupPostReplyWithMissingPostReplyText() { + setupPostReply("0", "R0", "P0", 1000L, null); + } private static class TestValue implements Value { @@ -289,4 +357,59 @@ public class ConfigurationSoneParserTest { } + private static class TestPostReplyBuilder implements PostReplyBuilder { + + private final PostReply postReply = mock(PostReply.class); + + @Override + public PostReplyBuilder to(String postId) { + when(postReply.getPostId()).thenReturn(postId); + return this; + } + + @Override + public PostReply build() throws IllegalStateException { + return postReply; + } + + @Override + public PostReplyBuilder randomId() { + when(postReply.getId()).thenReturn(randomUUID().toString()); + return this; + } + + @Override + public PostReplyBuilder withId(String id) { + when(postReply.getId()).thenReturn(id); + return this; + } + + @Override + public PostReplyBuilder from(String senderId) { + Sone sone = mock(Sone.class); + when(sone.getId()).thenReturn(senderId); + when(postReply.getSone()).thenReturn(sone); + return this; + } + + @Override + public PostReplyBuilder currentTime() { + when(postReply.getTime()).thenReturn(currentTimeMillis()); + return this; + } + + @Override + public PostReplyBuilder withTime(long time) { + when(postReply.getTime()).thenReturn(time); + return this; + } + + @Override + public PostReplyBuilder withText(String text) { + when(postReply.getText()).thenReturn(text); + return this; + } + + } + }