Mock posts with a recipient, too.
[Sone.git] / src / test / java / net / pterodactylus / sone / data / Mocks.java
index ce95638..51a24fd 100644 (file)
@@ -23,6 +23,7 @@ import static com.google.common.base.Optional.of;
 import static com.google.common.collect.ArrayListMultimap.create;
 import static com.google.common.collect.Ordering.from;
 import static com.google.common.collect.Sets.newHashSet;
+import static java.util.Collections.emptySet;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.mock;
@@ -54,6 +55,7 @@ public class Mocks {
        private final Multimap<Sone, Post> sonePosts = create();
        private final Collection<Sone> sones = newHashSet();
        private final Multimap<Post, PostReply> postReplies = create();
+       private final Multimap<String, Post> directedPosts = create();
        public final Database database;
        public final Core core;
 
@@ -66,6 +68,12 @@ public class Mocks {
                                return FluentIterable.from(sones).filter(Sone.LOCAL_SONE_FILTER).toList();
                        }
                });
+               when(database.getDirectedPosts(anyString())).then(new Answer<Collection<Post>>() {
+                       @Override
+                       public Collection<Post> answer(InvocationOnMock invocation) throws Throwable {
+                               return directedPosts.get((String) invocation.getArguments()[0]);
+                       }
+               });
        }
 
        private static Core mockCore(Database database) {
@@ -103,7 +111,7 @@ public class Mocks {
                private Optional<String> name = absent();
                private long time;
                private Profile profile = new Profile(mockedSone);
-               private Optional<String> friend = absent();
+               private Collection<String> friends = emptySet();
 
                private SoneMocker(String id) {
                        this.id = id;
@@ -134,8 +142,8 @@ public class Mocks {
                        return this;
                }
 
-               public SoneMocker withFriends(String friend) {
-                       this.friend = fromNullable(friend);
+               public SoneMocker withFriends(Collection<String> friends) {
+                       this.friends = friends;
                        return this;
                }
 
@@ -156,9 +164,13 @@ public class Mocks {
                                        }
                                });
                                when(mockedSone.hasFriend(anyString())).thenReturn(false);
-                               if (friend.isPresent()) {
-                                       when(mockedSone.hasFriend(friend.get())).thenReturn(true);
-                               }
+                               when(mockedSone.getFriends()).thenReturn(friends);
+                               when(mockedSone.hasFriend(anyString())).then(new Answer<Boolean>() {
+                                       @Override
+                                       public Boolean answer(InvocationOnMock invocation) throws Throwable {
+                                               return friends.contains(invocation.getArguments()[0]);
+                                       }
+                               });
                        } else {
                                when(mockedSone.newPostBuilder()).thenThrow(IllegalStateException.class);
                                when(mockedSone.newPostReplyBuilder(anyString())).thenThrow(IllegalStateException.class);
@@ -211,6 +223,9 @@ public class Mocks {
                        when(post.getId()).thenReturn(id);
                        when(post.getSone()).thenReturn(sone);
                        when(post.getRecipientId()).thenReturn(recipientId);
+                       if (recipientId.isPresent()) {
+                               directedPosts.put(recipientId.get(), post);
+                       }
                        when(post.getTime()).thenReturn(time);
                        if (text.isPresent()) {
                                when(post.getText()).thenReturn(text.get());