X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftext%2FSoneTextParserTest.java;h=ef98e3633c51fe2e0f54ae392fa6fd927487c28a;hb=616d826774d0ac43e1152ec6a4f5c3c198b95483;hp=25ec62a2c5f4ef8a0f8c30dcf6df3731304d448f;hpb=52c5e4136f8251b504941d662dba7b2ffc33863d;p=Sone.git diff --git a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java index 25ec62a..ef98e36 100644 --- a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java +++ b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java @@ -29,21 +29,19 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; -import java.util.Set; +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; import com.google.common.base.Function; import com.google.common.base.Optional; import org.junit.Test; -import org.mockito.Mockito; -import org.mockito.stubbing.OngoingStubbing; /** * JUnit test case for {@link SoneTextParser}. @@ -54,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. @@ -145,9 +144,23 @@ public class SoneTextParserTest { @Test public void linksToPostAreParsedCorrectly() throws IOException { - postProvider.addValidPostId("foo", "Post about foo..."); + postProvider.addValidPostId("foo", "internal", "Post about foo..."); Iterable parts = soneTextParser.parse(null, new StringReader("This post://foo is awesome.")); - assertThat(convertText(parts, PlainTextPart.class, PostPart.class), is("This [post|foo|Post about foo...] is awesome.")); + assertThat(convertText(parts, PlainTextPart.class, PostPart.class), is("This [post|new|foo|Post about foo...] is awesome.")); + } + + @Test + public void linksToPostsWithOldIdsAreParsedCorrectly() throws IOException { + postProvider.addValidPostId("foo", "internal", "Post about foo..."); + Iterable parts = soneTextParser.parse(null, new StringReader("This post://internal is awesome.")); + 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 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!")); } // @@ -203,7 +216,16 @@ public class SoneTextParserTest { text.append("[Sone|").append(sonePart.getSone().getId()).append(']'); } else if (part instanceof PostPart) { PostPart postPart = (PostPart) part; - text.append("[post|").append(postPart.getPost().getId()).append('|').append(postPart.getPost().getText()).append(']'); + text.append("[post|") + .append(postPart.usesDeprecatedLink() ? "old" : "new") + .append('|') + .append(postPart.getPost().getId()) + .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(); @@ -262,10 +284,14 @@ public class SoneTextParserTest { private static class TestPostProvider implements PostProvider { - private final Map posts = new HashMap(); + private final Map postTexts = new HashMap(); + private final Map postInternalIds = new HashMap(); + private final Map internalIdPosts = new HashMap(); - private void addValidPostId(String validPostId, String text) { - posts.put(validPostId, text); + private void addValidPostId(String validPostId, String internalId, String text) { + postTexts.put(validPostId, text); + postInternalIds.put(validPostId, internalId); + internalIdPosts.put(internalId, validPostId); } @Override @@ -280,10 +306,17 @@ public class SoneTextParserTest { @Override public Optional getPost(String postId) { - if (posts.containsKey(postId)) { + if (postTexts.containsKey(postId)) { Post post = mock(Post.class); when(post.getId()).thenReturn(postId); - when(post.getText()).thenReturn(posts.get(postId)); + when(post.getInternalId()).thenReturn(postInternalIds.get(postId)); + when(post.getText()).thenReturn(postTexts.get(postId)); + return Optional.of(post); + } else if (internalIdPosts.containsKey(postId)) { + Post post = mock(Post.class); + when(post.getId()).thenReturn(internalIdPosts.get(postId)); + when(post.getInternalId()).thenReturn(postId); + when(post.getText()).thenReturn(postTexts.get(internalIdPosts.get(postId))); return Optional.of(post); } return Optional.absent(); @@ -291,4 +324,25 @@ public class SoneTextParserTest { } + private static class TestAlbumProvider implements AlbumProvider { + + private final Map albumTitles = new HashMap(); + + public void addAlbumTitle(String albumId, String albumTitle) { + albumTitles.put(albumId, albumTitle); + } + + @Override + public Optional 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(); + } + + } + }