From 8d257d649b372ca1a240a8e26ce33205c7019d0d Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 8 Sep 2014 18:59:31 +0200 Subject: [PATCH] Move liked post IDs parsing to new configuration parser. --- .../pterodactylus/sone/core/ConfigurationSoneParser.java | 14 ++++++++++++++ src/main/java/net/pterodactylus/sone/core/Core.java | 10 ++-------- .../sone/core/ConfigurationSoneParserTest.java | 16 ++++++++++++++++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/ConfigurationSoneParser.java b/src/main/java/net/pterodactylus/sone/core/ConfigurationSoneParser.java index 01d3221..3cb3b68 100644 --- a/src/main/java/net/pterodactylus/sone/core/ConfigurationSoneParser.java +++ b/src/main/java/net/pterodactylus/sone/core/ConfigurationSoneParser.java @@ -136,6 +136,20 @@ public class ConfigurationSoneParser { return replies; } + public Set parseLikedPostIds() { + Set likedPostIds = new HashSet(); + while (true) { + String likedPostId = + getString("/Likes/Post/" + likedPostIds.size() + "/ID", + null); + if (likedPostId == null) { + break; + } + likedPostIds.add(likedPostId); + } + return likedPostIds; + } + public static class InvalidPostFound extends RuntimeException { } public static class InvalidPostReplyFound extends RuntimeException { } diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 1541066..5dd1eb6 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -1132,14 +1132,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } /* load post likes. */ - Set likedPostIds = new HashSet(); - while (true) { - String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null); - if (likedPostId == null) { - break; - } - likedPostIds.add(likedPostId); - } + Set likedPostIds = + configurationSoneParser.parseLikedPostIds(); /* load reply likes. */ Set likedReplyIds = new HashSet(); diff --git a/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java b/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java index 4e9e863..8ab729b 100644 --- a/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java +++ b/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java @@ -21,6 +21,7 @@ import static org.mockito.Mockito.when; import java.util.Collection; import java.util.List; +import java.util.Set; import java.util.concurrent.atomic.AtomicReference; import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostFound; @@ -269,6 +270,21 @@ public class ConfigurationSoneParserTest { setupPostReply("0", "R0", "P0", 1000L, null); } + @Test + public void likedPostIdsParsedCorrectly() { + setupLikedPostIds(); + Set likedPostIds = + configurationSoneParser.parseLikedPostIds(); + assertThat(likedPostIds, containsInAnyOrder("P1", "P2", "P3")); + } + + private void setupLikedPostIds() { + setupString("Sone/1/Likes/Post/0/ID", "P1"); + setupString("Sone/1/Likes/Post/1/ID", "P2"); + setupString("Sone/1/Likes/Post/2/ID", "P3"); + setupString("Sone/1/Likes/Post/3/ID", null); + } + private static class TestValue implements Value { private final AtomicReference value = new AtomicReference(); -- 2.7.4