Mock local Sones by mocking remote Sones and re-mocking some methods.
[Sone.git] / src / test / java / net / pterodactylus / sone / data / Mocks.java
index 10d40a1..fd95180 100644 (file)
@@ -32,6 +32,7 @@ import net.pterodactylus.sone.database.PostReplyBuilder;
 
 import com.google.common.base.Optional;
 import org.mockito.ArgumentCaptor;
+import org.mockito.Matchers;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
 
@@ -53,12 +54,12 @@ public class Mocks {
                Database database = mock(Database.class);
                when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
                when(database.getPost(anyString())).thenReturn(Optional.<Post>absent());
+               when(database.getPostReply(anyString())).thenReturn(Optional.<PostReply>absent());
                return database;
        }
 
        public static Sone mockLocalSone(Core core, final String id) {
-               Sone sone = mock(Sone.class);
-               when(sone.getId()).thenReturn(id);
+               Sone sone = mockRemoteSone(core, id);
                when(sone.isLocal()).thenReturn(true);
                final Database database = core.getDatabase();
                when(sone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
@@ -69,6 +70,16 @@ public class Mocks {
                                return new DefaultPostReplyBuilder(database, id, postIdCaptor.getValue());
                        }
                });
+               return sone;
+       }
+
+       public static Sone mockRemoteSone(Core core, final String id) {
+               Sone sone = mock(Sone.class);
+               when(sone.getId()).thenReturn(id);
+               when(sone.isLocal()).thenReturn(false);
+               final Database database = core.getDatabase();
+               when(sone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
+               when(sone.newPostReplyBuilder(Matchers.<String>anyObject())).thenThrow(IllegalStateException.class);
                when(core.getSone(eq(id))).thenReturn(of(sone));
                when(database.getSone(eq(id))).thenReturn(of(sone));
                return sone;
@@ -83,4 +94,13 @@ public class Mocks {
                return post;
        }
 
+       public static PostReply mockPostReply(Core core, Sone sone, String replyId) {
+               PostReply postReply = mock(PostReply.class);
+               when(postReply.getId()).thenReturn(replyId);
+               when(postReply.getSone()).thenReturn(sone);
+               Database database = core.getDatabase();
+               when(database.getPostReply(eq(replyId))).thenReturn(of(postReply));
+               return postReply;
+       }
+
 }