Use a builder-style mocker for post replies, too.
[Sone.git] / src / test / java / net / pterodactylus / sone / data / Mocks.java
index d5c2c08..98de409 100644 (file)
 
 package net.pterodactylus.sone.data;
 
+import static com.google.common.base.Optional.absent;
 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 net.pterodactylus.sone.data.Post.TIME_COMPARATOR;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.mock;
@@ -39,6 +39,7 @@ import net.pterodactylus.sone.database.PostReplyBuilder;
 import com.google.common.base.Optional;
 import com.google.common.collect.FluentIterable;
 import com.google.common.collect.Multimap;
+import com.google.common.collect.Ordering;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
 
@@ -51,6 +52,7 @@ public class Mocks {
 
        private final Multimap<Sone, Post> sonePosts = create();
        private final Collection<Sone> sones = newHashSet();
+       private final Multimap<Post, PostReply> postReplies = create();
        public final Database database;
        public final Core core;
 
@@ -88,12 +90,8 @@ public class Mocks {
                return new PostMocker(postId, sone);
        }
 
-       public PostReply mockPostReply(Sone sone, String replyId) {
-               PostReply postReply = mock(PostReply.class);
-               when(postReply.getId()).thenReturn(replyId);
-               when(postReply.getSone()).thenReturn(sone);
-               when(database.getPostReply(eq(replyId))).thenReturn(of(postReply));
-               return postReply;
+       public PostReplyMocker mockPostReply(Sone sone, String replyId) {
+               return new PostReplyMocker(replyId, sone);
        }
 
        public class SoneMocker {
@@ -133,7 +131,7 @@ public class Mocks {
                        when(mockedSone.getPosts()).then(new Answer<List<Post>>() {
                                @Override
                                public List<Post> answer(InvocationOnMock invocationOnMock) throws Throwable {
-                                       return from(TIME_COMPARATOR).sortedCopy(sonePosts.get(mockedSone));
+                                       return from(Post.TIME_COMPARATOR).sortedCopy(sonePosts.get(mockedSone));
                                }
                        });
                        when(mockedSone.toString()).thenReturn(String.format("Sone[%s]", id));
@@ -159,9 +157,44 @@ public class Mocks {
                        when(post.getSone()).thenReturn(sone);
                        when(database.getPost(eq(id))).thenReturn(of(post));
                        sonePosts.put(sone, post);
+                       when(post.getReplies()).then(new Answer<List<PostReply>>() {
+                               @Override
+                               public List<PostReply> answer(InvocationOnMock invocation) throws Throwable {
+                                       return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(postReplies.get(post));
+                               }
+                       });
                        return post;
                }
 
        }
 
+       public class PostReplyMocker {
+
+               private final PostReply postReply = mock(PostReply.class);
+               private final String id;
+               private final Sone sone;
+               private Optional<Post> post = absent();
+
+               public PostReplyMocker(String id, Sone sone) {
+                       this.id = id;
+                       this.sone = sone;
+               }
+
+               public PostReplyMocker inReplyTo(Post post) {
+                       this.post = of(post);
+                       return this;
+               }
+
+               public PostReply create() {
+                       when(postReply.getId()).thenReturn(id);
+                       when(postReply.getSone()).thenReturn(sone);
+                       when(database.getPostReply(eq(id))).thenReturn(of(postReply));
+                       if (post.isPresent()) {
+                               postReplies.put(post.get(), postReply);
+                       }
+                       return postReply;
+               }
+
+       }
+
 }