Move reply like functionality from Sone to Reply.
[Sone.git] / src / test / java / net / pterodactylus / sone / data / Mocks.java
index 0aa6035..f1b0a09 100644 (file)
@@ -21,16 +21,19 @@ 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.doAnswer;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 import java.util.Collection;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.data.impl.DefaultPostBuilder;
@@ -38,10 +41,14 @@ 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.HashMultimap;
 import com.google.common.collect.Multimap;
 import com.google.common.collect.Ordering;
+import com.google.common.collect.SetMultimap;
+import org.mockito.Matchers;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
 
@@ -53,18 +60,39 @@ 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();
+       private final SetMultimap<Post, Sone> postLikingSones = HashMultimap.create();
+       private final SetMultimap<PostReply, Sone> postReplyLikingSones = HashMultimap.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.getSones()).then(new Answer<Collection<Sone>>() {
+                       @Override
+                       public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
+                               return sones.values();
+                       }
+               });
                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]);
                        }
                });
        }
@@ -177,7 +205,7 @@ public class Mocks {
                                }
                        });
                        when(mockedSone.toString()).thenReturn(String.format("Sone[%s]", id));
-                       sones.add(mockedSone);
+                       sones.put(id, mockedSone);
                        return mockedSone;
                }
 
@@ -216,6 +244,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());
@@ -228,6 +259,26 @@ public class Mocks {
                                        return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(postReplies.get(post));
                                }
                        });
+                       doAnswer(new Answer<Void>() {
+                               @Override
+                               public Void answer(InvocationOnMock invocation) throws Throwable {
+                                       postLikingSones.put(post, (Sone) invocation.getArguments()[0]);
+                                       return null;
+                               }
+                       }).when(post).like(Matchers.<Sone>any());
+                       doAnswer(new Answer<Void>() {
+                               @Override
+                               public Void answer(InvocationOnMock invocation) throws Throwable {
+                                       postLikingSones.remove(post, (Sone) invocation.getArguments()[0]);
+                                       return null;
+                               }
+                       }).when(post).unlike(Matchers.<Sone>any());
+                       when(post.getLikes()).thenAnswer(new Answer<Set<Sone>>() {
+                               @Override
+                               public Set<Sone> answer(InvocationOnMock invocation) throws Throwable {
+                                       return postLikingSones.get(post);
+                               }
+                       });
                        return post;
                }
 
@@ -273,6 +324,26 @@ public class Mocks {
                        if (text.isPresent()) {
                                when(postReply.getText()).thenReturn(text.get());
                        }
+                       doAnswer(new Answer<Void>() {
+                               @Override
+                               public Void answer(InvocationOnMock invocation) throws Throwable {
+                                       postReplyLikingSones.put(postReply, (Sone) invocation.getArguments()[0]);
+                                       return null;
+                               }
+                       }).when(postReply).like(Matchers.<Sone>any());
+                       doAnswer(new Answer<Void>() {
+                               @Override
+                               public Void answer(InvocationOnMock invocation) throws Throwable {
+                                       postReplyLikingSones.remove(postReply, invocation.getArguments()[0]);
+                                       return null;
+                               }
+                       }).when(postReply).unlike(Matchers.<Sone>any());
+                       when(postReply.getLikes()).thenAnswer(new Answer<Set<Sone>>() {
+                               @Override
+                               public Set<Sone> answer(InvocationOnMock invocation) throws Throwable {
+                                       return postReplyLikingSones.get(postReply);
+                               }
+                       });
                        return postReply;
                }
        }