✅ Use real event bus in test
[Sone.git] / src / test / java / net / pterodactylus / sone / database / memory / MemoryBookmarkDatabaseTest.java
1 package net.pterodactylus.sone.database.memory;
2
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;
13
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.Map;
17 import java.util.Set;
18
19 import net.pterodactylus.sone.data.Post;
20
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.invocation.InvocationOnMock;
24 import org.mockito.stubbing.Answer;
25
26 /**
27  * Unit test for {@link MemoryBookmarkDatabase}.
28  */
29 public class MemoryBookmarkDatabaseTest {
30
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<>();
37
38         @Before
39         public void setupMemoryDatabase() {
40                 when(memoryDatabase.getPost(anyString())).thenAnswer(
41                                 new Answer<Post>() {
42                                         @Override
43                                         public Post answer(
44                                                         InvocationOnMock invocation) {
45                                                 return posts.get(invocation.getArguments()[0]);
46                                         }
47                                 });
48         }
49
50         @Before
51         public void setupPosts() {
52                 createAndRegisterPost("PostId1");
53                 createAndRegisterPost("PostId2");
54         }
55
56         private Post createAndRegisterPost(String postId) {
57                 Post post = createPost(postId);
58                 posts.put(postId, post);
59                 return post;
60         }
61
62         private Post createPost(String postId) {
63                 Post post = mock(Post.class);
64                 when(post.getId()).thenReturn(postId);
65                 return post;
66         }
67
68         @Test
69         public void bookmarkDatabaseRetainsBookmarkedPosts() {
70                 Set<Post> allPosts = new HashSet<>(posts.values());
71                 for (Post post : allPosts) {
72                         bookmarkDatabase.bookmarkPost(post);
73                 }
74                 assertThat(bookmarkDatabase.getBookmarkedPosts(), is(allPosts));
75                 for (Post post : allPosts) {
76                         assertThat(bookmarkDatabase.isPostBookmarked(post), is(true));
77                 }
78         }
79
80         @Test
81         public void bookmarkingAPostSavesTheDatabase() {
82                 for (Post post : posts.values()) {
83                         bookmarkDatabase.bookmarkPost(post);
84                 }
85                 verify(configurationLoader, times(posts.size()))
86                                 .saveBookmarkedPosts(any(Set.class));
87         }
88
89         @Test
90         public void unbookmarkingAPostSavesTheDatabase() {
91                 for (Post post : posts.values()) {
92                         bookmarkDatabase.bookmarkPost(post);
93                         bookmarkDatabase.unbookmarkPost(post);
94                 }
95                 verify(configurationLoader, times(posts.size() * 2))
96                                 .saveBookmarkedPosts(any(Set.class));
97         }
98
99         @Test
100         public void removingABookmarkRemovesTheCorrectBookmark() {
101                 Set<Post> allPosts = new HashSet<>(posts.values());
102                 for (Post post : allPosts) {
103                         bookmarkDatabase.bookmarkPost(post);
104                 }
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)));
112                 }
113         }
114
115         @Test
116         public void startingTheDatabaseLoadsBookmarkedPosts() {
117                 bookmarkDatabase.start();
118                 verify(configurationLoader).loadBookmarkedPosts();
119         }
120
121         @Test
122         public void stoppingTheDatabaseSavesTheBookmarkedPosts() {
123                 bookmarkDatabase.stop();
124                 verify(configurationLoader).saveBookmarkedPosts(any(Set.class));
125         }
126
127         @Test
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")));
135         }
136
137 }