Parse album:// links
[Sone.git] / src / test / java / net / pterodactylus / sone / text / SoneTextParserTest.java
index 8dc1d4a..ef98e36 100644 (file)
@@ -31,9 +31,11 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
+import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.impl.IdOnlySone;
+import net.pterodactylus.sone.database.AlbumProvider;
 import net.pterodactylus.sone.database.PostProvider;
 import net.pterodactylus.sone.database.SoneProvider;
 
@@ -50,7 +52,8 @@ public class SoneTextParserTest {
 
        private final SoneProvider soneProvider = new TestSoneProvider();
        private final TestPostProvider postProvider = new TestPostProvider();
-       private final SoneTextParser soneTextParser = new SoneTextParser(soneProvider, postProvider);
+       private final TestAlbumProvider albumProvider = new TestAlbumProvider();
+       private final SoneTextParser soneTextParser = new SoneTextParser(soneProvider, postProvider, albumProvider);
 
        /**
         * Tests basic plain-text operation of the parser.
@@ -153,6 +156,13 @@ public class SoneTextParserTest {
                assertThat(convertText(parts, PlainTextPart.class, PostPart.class), is("This [post|old|foo|Post about foo...] is awesome."));
        }
 
+       @Test
+       public void linksToAlbumIsParsedCorrectly() throws IOException {
+               albumProvider.addAlbumTitle("album-id", "Super Album");
+               Iterable<Part> parts = soneTextParser.parse(null, new StringReader("This album://album-id rocks!"));
+               assertThat(convertText(parts, PlainTextPart.class, AlbumPart.class), is("This [album|album-id|Super Album] rocks!"));
+       }
+
        //
        // PRIVATE METHODS
        //
@@ -213,6 +223,9 @@ public class SoneTextParserTest {
                                        .append('|')
                                        .append(postPart.getPost().getText())
                                        .append(']');
+                       } else if (part instanceof AlbumPart) {
+                               Album album = ((AlbumPart) part).getAlbum();
+                               text.append(String.format("[album|%s|%s]", album.getId(), album.getTitle()));
                        }
                }
                return text.toString();
@@ -311,4 +324,25 @@ public class SoneTextParserTest {
 
        }
 
+       private static class TestAlbumProvider implements AlbumProvider {
+
+               private final Map<String, String> albumTitles = new HashMap<String, String>();
+
+               public void addAlbumTitle(String albumId, String albumTitle) {
+                       albumTitles.put(albumId, albumTitle);
+               }
+
+               @Override
+               public Optional<Album> getAlbum(String albumId) {
+                       if (albumTitles.containsKey(albumId)) {
+                               Album album = mock(Album.class);
+                               when(album.getId()).thenReturn(albumId);
+                               when(album.getTitle()).thenReturn(albumTitles.get(albumId));
+                               return Optional.of(album);
+                       }
+                       return Optional.absent();
+               }
+
+       }
+
 }