Implement Database.getSone() function.
[Sone.git] / src / test / java / net / pterodactylus / sone / data / Mocks.java
index ce95638..cbe9844 100644 (file)
@@ -21,8 +21,9 @@ import static com.google.common.base.Optional.absent;
 import static com.google.common.base.Optional.fromNullable;
 import static com.google.common.base.Optional.of;
 import static com.google.common.collect.ArrayListMultimap.create;
+import static com.google.common.collect.Maps.newHashMap;
 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;
@@ -30,6 +31,7 @@ import static org.mockito.Mockito.when;
 
 import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 
 import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.data.impl.DefaultPostBuilder;
@@ -37,6 +39,7 @@ import net.pterodactylus.sone.data.impl.DefaultPostReplyBuilder;
 import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.sone.database.PostReplyBuilder;
 
+import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.collect.FluentIterable;
 import com.google.common.collect.Multimap;
@@ -52,18 +55,31 @@ import org.mockito.stubbing.Answer;
 public class Mocks {
 
        private final Multimap<Sone, Post> sonePosts = create();
-       private final Collection<Sone> sones = newHashSet();
+       private final Map<String, Sone> sones = newHashMap();
        private final Multimap<Post, PostReply> postReplies = create();
+       private final Multimap<String, Post> directedPosts = create();
        public final Database database;
        public final Core core;
 
        public Mocks() {
                database = mockDatabase();
                core = mockCore(database);
+               when(database.getSone()).thenReturn(new Function<String, Optional<Sone>>() {
+                       @Override
+                       public Optional<Sone> apply(String soneId) {
+                               return (soneId == null) ? Optional.<Sone>absent() : fromNullable(sones.get(soneId));
+                       }
+               });
                when(core.getLocalSones()).then(new Answer<Collection<Sone>>() {
                        @Override
                        public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
-                               return FluentIterable.from(sones).filter(Sone.LOCAL_SONE_FILTER).toList();
+                               return FluentIterable.from(sones.values()).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]);
                        }
                });
        }
@@ -103,7 +119,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 +150,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 +172,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);
@@ -172,7 +192,7 @@ public class Mocks {
                                }
                        });
                        when(mockedSone.toString()).thenReturn(String.format("Sone[%s]", id));
-                       sones.add(mockedSone);
+                       sones.put(id, mockedSone);
                        return mockedSone;
                }
 
@@ -211,6 +231,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());