From: David ‘Bombe’ Roden Date: Wed, 19 Feb 2014 17:11:53 +0000 (+0100) Subject: Mock some post database interactions. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=57054fd650db40dbf3ceba5fa5b094ef8629bef0;p=Sone.git Mock some post database interactions. --- diff --git a/src/test/java/net/pterodactylus/sone/data/Mocks.java b/src/test/java/net/pterodactylus/sone/data/Mocks.java index f56a1d4..3615828 100644 --- a/src/test/java/net/pterodactylus/sone/data/Mocks.java +++ b/src/test/java/net/pterodactylus/sone/data/Mocks.java @@ -47,6 +47,7 @@ import net.pterodactylus.sone.core.SoneInserter; import net.pterodactylus.sone.data.impl.DefaultPostBuilder; import net.pterodactylus.sone.data.impl.DefaultPostReplyBuilder; import net.pterodactylus.sone.database.Database; +import net.pterodactylus.sone.database.PostBuilder.PostCreated; import net.pterodactylus.sone.database.PostReplyBuilder; import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired; import net.pterodactylus.sone.utils.IntegerRangePredicate; @@ -77,6 +78,7 @@ public class Mocks { private final Multimap sonePosts = create(); private final Map sones = newHashMap(); + private final Map posts = newHashMap(); private final Multimap postReplies = create(); private final Multimap directedPosts = create(); private final SetMultimap postLikingSones = HashMultimap.create(); @@ -114,6 +116,13 @@ public class Mocks { return FluentIterable.from(sones.values()).filter(Sone.LOCAL_SONE_FILTER).toList(); } }); + when(core.postCreated()).thenReturn(Optional.of(new PostCreated() { + @Override + public void postCreated(Post post) { + posts.put(post.getId(), post); + sonePosts.put(post.getSone(), post); + } + })); Options options = createOptions(); when(core.getPreferences()).thenReturn(new Preferences(options)); when(database.getDirectedPosts(anyString())).then(new Answer>() { @@ -147,10 +156,15 @@ public class Mocks { return core; } - private static Database mockDatabase() { + private Database mockDatabase() { Database database = mock(Database.class); when(database.getSone(anyString())).thenReturn(Optional.absent()); - when(database.getPost(anyString())).thenReturn(Optional.absent()); + when(database.getPost(anyString())).then(new Answer>() { + @Override + public Optional answer(InvocationOnMock invocation) throws Throwable { + return fromNullable(posts.get(invocation.getArguments()[0])); + } + }); when(database.getPostReply(anyString())).thenReturn(Optional.absent()); return database; }