Add unit test for returning not-yet loaded bookmarked posts.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 9 Nov 2014 11:29:12 +0000 (12:29 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 22 Nov 2014 11:38:33 +0000 (12:38 +0100)
src/test/java/net/pterodactylus/sone/Matchers.java
src/test/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabaseTest.java

index c1052d5..c16322c 100644 (file)
@@ -98,6 +98,10 @@ public class Matchers {
                return new PostMatcher(postId, time, text, recipient);
        }
 
+       public static Matcher<Post> isPostWithId(String postId) {
+               return new PostIdMatcher(postId);
+       }
+
        public static Matcher<PostReply> isPostReply(String postReplyId,
                        String postId, long time, String text) {
                return new PostReplyMatcher(postReplyId, postId, time, text);
@@ -312,6 +316,31 @@ public class Matchers {
 
        }
 
+       private static class PostIdMatcher extends TypeSafeDiagnosingMatcher<Post> {
+
+               private final String id;
+
+               private PostIdMatcher(String id) {
+                       this.id = id;
+               }
+
+               @Override
+               protected boolean matchesSafely(Post item,
+                               Description mismatchDescription) {
+                       if (!item.getId().equals(id)) {
+                               mismatchDescription.appendText("post has ID ").appendValue(item.getId());
+                               return false;
+                       }
+                       return true;
+               }
+
+               @Override
+               public void describeTo(Description description) {
+                       description.appendText("post with ID ").appendValue(id);
+               }
+
+       }
+
        private static class PostReplyMatcher
                        extends TypeSafeDiagnosingMatcher<PostReply> {
 
index c5ce475..06c5b96 100644 (file)
@@ -1,7 +1,9 @@
 package net.pterodactylus.sone.database.memory;
 
 import static com.google.common.base.Optional.fromNullable;
+import static net.pterodactylus.sone.Matchers.isPostWithId;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.contains;
 import static org.hamcrest.Matchers.is;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyString;
@@ -52,14 +54,20 @@ public class MemoryBookmarkDatabaseTest {
 
        @Before
        public void setupPosts() {
-               createPost("PostId1");
-               createPost("PostId2");
+               createAndRegisterPost("PostId1");
+               createAndRegisterPost("PostId2");
        }
 
-       private void createPost(String postId) {
+       private Post createAndRegisterPost(String postId) {
+               Post post = createPost(postId);
+               posts.put(postId, post);
+               return post;
+       }
+
+       private Post createPost(String postId) {
                Post post = mock(Post.class);
                when(post.getId()).thenReturn(postId);
-               posts.put(postId, post);
+               return post;
        }
 
        @Test
@@ -121,4 +129,14 @@ public class MemoryBookmarkDatabaseTest {
                verify(configurationLoader).saveBookmarkedPosts(any(Set.class));
        }
 
+       @Test
+       public void bookmarkedPostsIncludeNotYetLoadedPosts() {
+               bookmarkDatabase.bookmarkPost(posts.get("PostId1"));
+               bookmarkDatabase.bookmarkPost(createPost("PostId3"));
+               final Set<Post> bookmarkedPosts =
+                               bookmarkDatabase.getBookmarkedPosts();
+               assertThat(bookmarkedPosts,
+                               contains(isPostWithId("PostId1"), isPostWithId("PostId3")));
+       }
+
 }