Extract class for supplying commands, improve tests for FCP interface
[Sone.git] / src / test / java / net / pterodactylus / sone / database / memory / MemoryBookmarkDatabaseTest.java
1 package net.pterodactylus.sone.database.memory;
2
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;
14
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.Map;
18 import java.util.Set;
19
20 import net.pterodactylus.sone.data.Post;
21
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;
27
28 /**
29  * Unit test for {@link MemoryBookmarkDatabase}.
30  *
31  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32  */
33 public class MemoryBookmarkDatabaseTest {
34
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>();
41
42         @Before
43         public void setupMemoryDatabase() {
44                 when(memoryDatabase.getPost(anyString())).thenAnswer(
45                                 new Answer<Optional<Post>>() {
46                                         @Override
47                                         public Optional<Post> answer(
48                                                         InvocationOnMock invocation) {
49                                                 return fromNullable(
50                                                                 posts.get(invocation.getArguments()[0]));
51                                         }
52                                 });
53         }
54
55         @Before
56         public void setupPosts() {
57                 createAndRegisterPost("PostId1");
58                 createAndRegisterPost("PostId2");
59         }
60
61         private Post createAndRegisterPost(String postId) {
62                 Post post = createPost(postId);
63                 posts.put(postId, post);
64                 return post;
65         }
66
67         private Post createPost(String postId) {
68                 Post post = mock(Post.class);
69                 when(post.getId()).thenReturn(postId);
70                 return post;
71         }
72
73         @Test
74         public void bookmarkDatabaseRetainsBookmarkedPosts() {
75                 Set<Post> allPosts = new HashSet<Post>(posts.values());
76                 for (Post post : allPosts) {
77                         bookmarkDatabase.bookmarkPost(post);
78                 }
79                 assertThat(bookmarkDatabase.getBookmarkedPosts(), is(allPosts));
80                 for (Post post : allPosts) {
81                         assertThat(bookmarkDatabase.isPostBookmarked(post), is(true));
82                 }
83         }
84
85         @Test
86         public void bookmarkingAPostSavesTheDatabase() {
87                 for (Post post : posts.values()) {
88                         bookmarkDatabase.bookmarkPost(post);
89                 }
90                 verify(configurationLoader, times(posts.size()))
91                                 .saveBookmarkedPosts(any(Set.class));
92         }
93
94         @Test
95         public void unbookmarkingAPostSavesTheDatabase() {
96                 for (Post post : posts.values()) {
97                         bookmarkDatabase.bookmarkPost(post);
98                         bookmarkDatabase.unbookmarkPost(post);
99                 }
100                 verify(configurationLoader, times(posts.size() * 2))
101                                 .saveBookmarkedPosts(any(Set.class));
102         }
103
104         @Test
105         public void removingABookmarkRemovesTheCorrectBookmark() {
106                 Set<Post> allPosts = new HashSet<Post>(posts.values());
107                 for (Post post : allPosts) {
108                         bookmarkDatabase.bookmarkPost(post);
109                 }
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)));
117                 }
118         }
119
120         @Test
121         public void startingTheDatabaseLoadsBookmarkedPosts() {
122                 bookmarkDatabase.start();
123                 verify(configurationLoader).loadBookmarkedPosts();
124         }
125
126         @Test
127         public void stoppingTheDatabaseSavesTheBookmarkedPosts() {
128                 bookmarkDatabase.stop();
129                 verify(configurationLoader).saveBookmarkedPosts(any(Set.class));
130         }
131
132         @Test
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")));
140         }
141
142 }