Move liked post IDs parsing to new configuration parser.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 8 Sep 2014 16:59:31 +0000 (18:59 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 8 Sep 2014 17:05:27 +0000 (19:05 +0200)
src/main/java/net/pterodactylus/sone/core/ConfigurationSoneParser.java
src/main/java/net/pterodactylus/sone/core/Core.java
src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java

index 01d3221..3cb3b68 100644 (file)
@@ -136,6 +136,20 @@ public class ConfigurationSoneParser {
                return replies;
        }
 
+       public Set<String> parseLikedPostIds() {
+               Set<String> likedPostIds = new HashSet<String>();
+               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 { }
index 1541066..5dd1eb6 100644 (file)
@@ -1132,14 +1132,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                }
 
                /* load post likes. */
-               Set<String> likedPostIds = new HashSet<String>();
-               while (true) {
-                       String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
-                       if (likedPostId == null) {
-                               break;
-                       }
-                       likedPostIds.add(likedPostId);
-               }
+               Set<String> likedPostIds =
+                               configurationSoneParser.parseLikedPostIds();
 
                /* load reply likes. */
                Set<String> likedReplyIds = new HashSet<String>();
index 4e9e863..8ab729b 100644 (file)
@@ -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<String> 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<T> implements Value<T> {
 
                private final AtomicReference<T> value = new AtomicReference<T>();