Add test for recognizing the old-element link correctly
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 24 Jul 2015 14:59:52 +0000 (16:59 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 24 Jul 2015 18:48:00 +0000 (20:48 +0200)
src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java

index 25ec62a..3133483 100644 (file)
@@ -145,9 +145,16 @@ public class SoneTextParserTest {
 
        @Test
        public void linksToPostAreParsedCorrectly() throws IOException {
-               postProvider.addValidPostId("foo", "Post about foo...");
+               postProvider.addValidPostId("foo", "internal", "Post about foo...");
                Iterable<Part> 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<Part> 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."));
        }
 
        //
@@ -203,7 +210,13 @@ 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(']');
                        }
                }
                return text.toString();
@@ -262,10 +275,14 @@ public class SoneTextParserTest {
 
        private static class TestPostProvider implements PostProvider {
 
-               private final Map<String, String> posts = new HashMap<String, String>();
+               private final Map<String, String> postTexts = new HashMap<String, String>();
+               private final Map<String, String> postInternalIds = new HashMap<String, String>();
+               private final Map<String, String> internalIdPosts = new HashMap<String, String>();
 
-               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 +297,17 @@ public class SoneTextParserTest {
 
                @Override
                public Optional<Post> 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();