1 package net.pterodactylus.sone.database.memory;
3 import static com.google.common.base.Optional.fromNullable;
4 import static net.pterodactylus.sone.Matchers.isPostWithId;
5 import static org.hamcrest.MatcherAssert.assertThat;
6 import static org.hamcrest.Matchers.contains;
7 import static org.hamcrest.Matchers.is;
8 import static org.mockito.ArgumentMatchers.any;
9 import static org.mockito.ArgumentMatchers.anyString;
10 import static org.mockito.Mockito.mock;
11 import static org.mockito.Mockito.times;
12 import static org.mockito.Mockito.verify;
13 import static org.mockito.Mockito.when;
15 import java.util.HashMap;
16 import java.util.HashSet;
20 import net.pterodactylus.sone.data.Post;
22 import com.google.common.base.Optional;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.invocation.InvocationOnMock;
26 import org.mockito.stubbing.Answer;
29 * Unit test for {@link MemoryBookmarkDatabase}.
31 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33 public class MemoryBookmarkDatabaseTest {
35 private final MemoryDatabase memoryDatabase = mock(MemoryDatabase.class);
36 private final ConfigurationLoader configurationLoader =
37 mock(ConfigurationLoader.class);
38 private final MemoryBookmarkDatabase bookmarkDatabase =
39 new MemoryBookmarkDatabase(memoryDatabase, configurationLoader);
40 private final Map<String, Post> posts = new HashMap<String, Post>();
43 public void setupMemoryDatabase() {
44 when(memoryDatabase.getPost(anyString())).thenAnswer(
45 new Answer<Optional<Post>>() {
47 public Optional<Post> answer(
48 InvocationOnMock invocation) {
50 posts.get(invocation.getArguments()[0]));
56 public void setupPosts() {
57 createAndRegisterPost("PostId1");
58 createAndRegisterPost("PostId2");
61 private Post createAndRegisterPost(String postId) {
62 Post post = createPost(postId);
63 posts.put(postId, post);
67 private Post createPost(String postId) {
68 Post post = mock(Post.class);
69 when(post.getId()).thenReturn(postId);
74 public void bookmarkDatabaseRetainsBookmarkedPosts() {
75 Set<Post> allPosts = new HashSet<Post>(posts.values());
76 for (Post post : allPosts) {
77 bookmarkDatabase.bookmarkPost(post);
79 assertThat(bookmarkDatabase.getBookmarkedPosts(), is(allPosts));
80 for (Post post : allPosts) {
81 assertThat(bookmarkDatabase.isPostBookmarked(post), is(true));
86 public void bookmarkingAPostSavesTheDatabase() {
87 for (Post post : posts.values()) {
88 bookmarkDatabase.bookmarkPost(post);
90 verify(configurationLoader, times(posts.size()))
91 .saveBookmarkedPosts(any(Set.class));
95 public void unbookmarkingAPostSavesTheDatabase() {
96 for (Post post : posts.values()) {
97 bookmarkDatabase.bookmarkPost(post);
98 bookmarkDatabase.unbookmarkPost(post);
100 verify(configurationLoader, times(posts.size() * 2))
101 .saveBookmarkedPosts(any(Set.class));
105 public void removingABookmarkRemovesTheCorrectBookmark() {
106 Set<Post> allPosts = new HashSet<Post>(posts.values());
107 for (Post post : allPosts) {
108 bookmarkDatabase.bookmarkPost(post);
110 Post randomPost = posts.values().iterator().next();
111 bookmarkDatabase.unbookmarkPost(randomPost);
112 allPosts.remove(randomPost);
113 assertThat(bookmarkDatabase.getBookmarkedPosts(), is(allPosts));
114 for (Post post : posts.values()) {
115 assertThat(bookmarkDatabase.isPostBookmarked(post),
116 is(!post.equals(randomPost)));
121 public void startingTheDatabaseLoadsBookmarkedPosts() {
122 bookmarkDatabase.start();
123 verify(configurationLoader).loadBookmarkedPosts();
127 public void stoppingTheDatabaseSavesTheBookmarkedPosts() {
128 bookmarkDatabase.stop();
129 verify(configurationLoader).saveBookmarkedPosts(any(Set.class));
133 public void bookmarkedPostsIncludeNotYetLoadedPosts() {
134 bookmarkDatabase.bookmarkPost(posts.get("PostId1"));
135 bookmarkDatabase.bookmarkPost(createPost("PostId3"));
136 final Set<Post> bookmarkedPosts =
137 bookmarkDatabase.getBookmarkedPosts();
138 assertThat(bookmarkedPosts,
139 contains(isPostWithId("PostId1"), isPostWithId("PostId3")));