1 package net.pterodactylus.sone.database.memory;
3 import static net.pterodactylus.sone.test.Matchers.isPostWithId;
4 import static org.hamcrest.MatcherAssert.assertThat;
5 import static org.hamcrest.Matchers.contains;
6 import static org.hamcrest.Matchers.is;
7 import static org.mockito.ArgumentMatchers.any;
8 import static org.mockito.ArgumentMatchers.anyString;
9 import static org.mockito.Mockito.mock;
10 import static org.mockito.Mockito.times;
11 import static org.mockito.Mockito.verify;
12 import static org.mockito.Mockito.when;
14 import java.util.HashMap;
15 import java.util.HashSet;
19 import net.pterodactylus.sone.data.Post;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.invocation.InvocationOnMock;
24 import org.mockito.stubbing.Answer;
27 * Unit test for {@link MemoryBookmarkDatabase}.
29 public class MemoryBookmarkDatabaseTest {
31 private final MemoryDatabase memoryDatabase = mock(MemoryDatabase.class);
32 private final ConfigurationLoader configurationLoader =
33 mock(ConfigurationLoader.class);
34 private final MemoryBookmarkDatabase bookmarkDatabase =
35 new MemoryBookmarkDatabase(memoryDatabase, configurationLoader);
36 private final Map<String, Post> posts = new HashMap<>();
39 public void setupMemoryDatabase() {
40 when(memoryDatabase.getPost(anyString())).thenAnswer(
44 InvocationOnMock invocation) {
45 return posts.get(invocation.getArguments()[0]);
51 public void setupPosts() {
52 createAndRegisterPost("PostId1");
53 createAndRegisterPost("PostId2");
56 private Post createAndRegisterPost(String postId) {
57 Post post = createPost(postId);
58 posts.put(postId, post);
62 private Post createPost(String postId) {
63 Post post = mock(Post.class);
64 when(post.getId()).thenReturn(postId);
69 public void bookmarkDatabaseRetainsBookmarkedPosts() {
70 Set<Post> allPosts = new HashSet<>(posts.values());
71 for (Post post : allPosts) {
72 bookmarkDatabase.bookmarkPost(post);
74 assertThat(bookmarkDatabase.getBookmarkedPosts(), is(allPosts));
75 for (Post post : allPosts) {
76 assertThat(bookmarkDatabase.isPostBookmarked(post), is(true));
81 public void bookmarkingAPostSavesTheDatabase() {
82 for (Post post : posts.values()) {
83 bookmarkDatabase.bookmarkPost(post);
85 verify(configurationLoader, times(posts.size()))
86 .saveBookmarkedPosts(any(Set.class));
90 public void unbookmarkingAPostSavesTheDatabase() {
91 for (Post post : posts.values()) {
92 bookmarkDatabase.bookmarkPost(post);
93 bookmarkDatabase.unbookmarkPost(post);
95 verify(configurationLoader, times(posts.size() * 2))
96 .saveBookmarkedPosts(any(Set.class));
100 public void removingABookmarkRemovesTheCorrectBookmark() {
101 Set<Post> allPosts = new HashSet<>(posts.values());
102 for (Post post : allPosts) {
103 bookmarkDatabase.bookmarkPost(post);
105 Post randomPost = posts.values().iterator().next();
106 bookmarkDatabase.unbookmarkPost(randomPost);
107 allPosts.remove(randomPost);
108 assertThat(bookmarkDatabase.getBookmarkedPosts(), is(allPosts));
109 for (Post post : posts.values()) {
110 assertThat(bookmarkDatabase.isPostBookmarked(post),
111 is(!post.equals(randomPost)));
116 public void startingTheDatabaseLoadsBookmarkedPosts() {
117 bookmarkDatabase.start();
118 verify(configurationLoader).loadBookmarkedPosts();
122 public void stoppingTheDatabaseSavesTheBookmarkedPosts() {
123 bookmarkDatabase.stop();
124 verify(configurationLoader).saveBookmarkedPosts(any(Set.class));
128 public void bookmarkedPostsIncludeNotYetLoadedPosts() {
129 bookmarkDatabase.bookmarkPost(posts.get("PostId1"));
130 bookmarkDatabase.bookmarkPost(createPost("PostId3"));
131 final Set<Post> bookmarkedPosts =
132 bookmarkDatabase.getBookmarkedPosts();
133 assertThat(bookmarkedPosts,
134 contains(isPostWithId("PostId1"), isPostWithId("PostId3")));