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);
}
+ 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> {
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;
@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
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")));
+ }
+
}