X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdatabase%2Fmemory%2FMemoryBookmarkDatabaseTest.java;h=188b74019ea484a707695b40672084f6dd81f5a4;hb=a57dae72e505fec934a9516b88fea75c473f7641;hp=28cd2008e70fea1fe3d5a6d5c8e99bdaca048da9;hpb=ee2aef900b4a7ebabac8f3e42b0d3ec6447bfe98;p=Sone.git
diff --git a/src/test/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabaseTest.java b/src/test/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabaseTest.java
index 28cd200..188b740 100644
--- a/src/test/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabaseTest.java
+++ b/src/test/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabaseTest.java
@@ -1,12 +1,13 @@
package net.pterodactylus.sone.database.memory;
-import static com.google.common.base.Optional.fromNullable;
+import static net.pterodactylus.sone.test.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;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -17,7 +18,6 @@ import java.util.Set;
import net.pterodactylus.sone.data.Post;
-import com.google.common.base.Optional;
import org.junit.Before;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
@@ -25,45 +25,49 @@ import org.mockito.stubbing.Answer;
/**
* Unit test for {@link MemoryBookmarkDatabase}.
- *
- * @author David âBombeâ Roden
*/
public class MemoryBookmarkDatabaseTest {
private final MemoryDatabase memoryDatabase = mock(MemoryDatabase.class);
- private final ConfigurationLoader configurationLoader = mock(ConfigurationLoader.class);
+ private final ConfigurationLoader configurationLoader =
+ mock(ConfigurationLoader.class);
private final MemoryBookmarkDatabase bookmarkDatabase =
new MemoryBookmarkDatabase(memoryDatabase, configurationLoader);
- private final Map posts = new HashMap();
+ private final Map posts = new HashMap<>();
@Before
public void setupMemoryDatabase() {
when(memoryDatabase.getPost(anyString())).thenAnswer(
- new Answer>() {
+ new Answer() {
@Override
- public Optional answer(
+ public Post answer(
InvocationOnMock invocation) {
- return fromNullable(
- posts.get(invocation.getArguments()[0]));
+ return posts.get(invocation.getArguments()[0]);
}
});
}
@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
public void bookmarkDatabaseRetainsBookmarkedPosts() {
- Set allPosts = new HashSet(posts.values());
+ Set allPosts = new HashSet<>(posts.values());
for (Post post : allPosts) {
bookmarkDatabase.bookmarkPost(post);
}
@@ -74,8 +78,27 @@ public class MemoryBookmarkDatabaseTest {
}
@Test
+ public void bookmarkingAPostSavesTheDatabase() {
+ for (Post post : posts.values()) {
+ bookmarkDatabase.bookmarkPost(post);
+ }
+ verify(configurationLoader, times(posts.size()))
+ .saveBookmarkedPosts(any(Set.class));
+ }
+
+ @Test
+ public void unbookmarkingAPostSavesTheDatabase() {
+ for (Post post : posts.values()) {
+ bookmarkDatabase.bookmarkPost(post);
+ bookmarkDatabase.unbookmarkPost(post);
+ }
+ verify(configurationLoader, times(posts.size() * 2))
+ .saveBookmarkedPosts(any(Set.class));
+ }
+
+ @Test
public void removingABookmarkRemovesTheCorrectBookmark() {
- Set allPosts = new HashSet(posts.values());
+ Set allPosts = new HashSet<>(posts.values());
for (Post post : allPosts) {
bookmarkDatabase.bookmarkPost(post);
}
@@ -101,4 +124,14 @@ public class MemoryBookmarkDatabaseTest {
verify(configurationLoader).saveBookmarkedPosts(any(Set.class));
}
+ @Test
+ public void bookmarkedPostsIncludeNotYetLoadedPosts() {
+ bookmarkDatabase.bookmarkPost(posts.get("PostId1"));
+ bookmarkDatabase.bookmarkPost(createPost("PostId3"));
+ final Set bookmarkedPosts =
+ bookmarkDatabase.getBookmarkedPosts();
+ assertThat(bookmarkedPosts,
+ contains(isPostWithId("PostId1"), isPostWithId("PostId3")));
+ }
+
}