*/
public class Mocks {
- private static final Multimap<Sone, Post> sonePosts = create();
+ private final Multimap<Sone, Post> sonePosts = create();
+ public final Database database;
+ public final Core core;
- public static Core mockCore(Database database) {
+ public Mocks() {
+ database = mockDatabase();
+ core = mockCore(database);
+ }
+
+ private static Core mockCore(Database database) {
Core core = mock(Core.class);
when(core.getDatabase()).thenReturn(database);
when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
return core;
}
- public static Database mockDatabase() {
+ private static Database mockDatabase() {
Database database = mock(Database.class);
when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
when(database.getPost(anyString())).thenReturn(Optional.<Post>absent());
return database;
}
- public static Sone mockLocalSone(Core core, final String id) {
+ public Sone mockLocalSone(final String id) {
final Sone sone = mock(Sone.class);
when(sone.getId()).thenReturn(id);
when(sone.isLocal()).thenReturn(true);
when(sone.getProfile()).thenReturn(new Profile(sone));
- final Database database = core.getDatabase();
when(sone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
when(sone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
@Override
return sone;
}
- public static Sone mockRemoteSone(Core core, final String id) {
+ public Sone mockRemoteSone(final String id) {
final Sone sone = mock(Sone.class);
when(sone.getId()).thenReturn(id);
when(sone.isLocal()).thenReturn(false);
when(sone.getProfile()).thenReturn(new Profile(sone));
- final Database database = core.getDatabase();
when(sone.newPostBuilder()).thenThrow(IllegalStateException.class);
when(sone.newPostReplyBuilder(Matchers.<String>anyObject())).thenThrow(IllegalStateException.class);
when(core.getSone(eq(id))).thenReturn(of(sone));
return sone;
}
- public static Post mockPost(Core core, Sone sone, String postId) {
+ public Post mockPost(Sone sone, String postId) {
Post post = mock(Post.class);
when(post.getId()).thenReturn(postId);
when(post.getSone()).thenReturn(sone);
- Database database = core.getDatabase();
when(database.getPost(eq(postId))).thenReturn(of(post));
sonePosts.put(sone, post);
return post;
}
- public static PostReply mockPostReply(Core core, Sone sone, String replyId) {
+ public PostReply mockPostReply(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;
}
import static com.google.common.base.Optional.of;
import static java.lang.System.currentTimeMillis;
-import static net.pterodactylus.sone.data.Mocks.mockCore;
-import static net.pterodactylus.sone.data.Mocks.mockDatabase;
-import static net.pterodactylus.sone.data.Mocks.mockLocalSone;
import static net.pterodactylus.sone.database.PostBuilder.PostCreated;
import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Mockito.when;
-import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Mocks;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.database.Database;
import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
import net.pterodactylus.sone.freenet.fcp.Command.Response;
import net.pterodactylus.sone.freenet.fcp.FcpException;
public class CreatePostCommandTest {
private final long now = currentTimeMillis();
- private final Database database = mockDatabase();
- private final Core core = mockCore(database);
- private final CreatePostCommand createPostCommand = new CreatePostCommand(core);
+ private final Mocks mocks = new Mocks();
+ private final CreatePostCommand createPostCommand = new CreatePostCommand(mocks.core);
@Test
public void verifyThatCreatingAPostWorks() throws FcpException {
- Sone sone = mockLocalSone(core, "Sone");
- mockLocalSone(core, "OtherSone");
+ Sone sone = mocks.mockLocalSone("Sone");
+ mocks.mockLocalSone("OtherSone");
CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
- when(core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
+ when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
.put("Sone", "Sone")
@Test
public void verifyThatCreatingAPostWithoutRecipientWorks() throws FcpException {
- Sone sone = mockLocalSone(core, "Sone");
+ Sone sone = mocks.mockLocalSone("Sone");
CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
- when(core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
+ when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
.put("Sone", "Sone")
@Test
public void verifyThatCreatingAPostDirectedToTheSendingSoneCausesAnError() throws FcpException {
- mockLocalSone(core, "Sone");
+ mocks.mockLocalSone("Sone");
CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
- when(core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
+ when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
.put("Sone", "Sone")
@Test(expected = FcpException.class)
public void verifyThatCreatingAPostWithoutTextCausesAnError() throws FcpException {
- mockLocalSone(core, "Sone");
+ mocks.mockLocalSone("Sone");
CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
- when(core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
+ when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
.put("Sone", "Sone")
@Test(expected = FcpException.class)
public void verifyThatCreatingAPostWithoutSoneCausesAnError() throws FcpException {
- mockLocalSone(core, "Sone");
+ mocks.mockLocalSone("Sone");
CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
- when(core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
+ when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
.put("Text", "Text of the post.")
package net.pterodactylus.sone.fcp;
import static java.lang.System.currentTimeMillis;
-import static net.pterodactylus.sone.data.Mocks.mockCore;
-import static net.pterodactylus.sone.data.Mocks.mockDatabase;
-import static net.pterodactylus.sone.data.Mocks.mockLocalSone;
-import static net.pterodactylus.sone.data.Mocks.mockPost;
import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.core.AllOf.allOf;
import static org.mockito.Mockito.when;
-import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Mocks;
import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.database.Database;
import net.pterodactylus.sone.database.PostReplyBuilder.PostReplyCreated;
import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
import net.pterodactylus.sone.freenet.fcp.Command.Response;
public class CreateReplyCommandTest {
private final long now = currentTimeMillis();
- private final Database database = mockDatabase();
- private final Core core = mockCore(database);
- private final CreateReplyCommand createReplyCommand = new CreateReplyCommand(core);
+ private final Mocks mocks = new Mocks();
+ private final CreateReplyCommand createReplyCommand = new CreateReplyCommand(mocks.core);
@Test
public void verifyThatCreatingAFullySpecifiedReplyWorks() throws FcpException {
- Sone sone = mockLocalSone(core, "SoneId");
- mockPost(core, sone, "PostId");
+ Sone sone = mocks.mockLocalSone("SoneId");
+ mocks.mockPost(sone, "PostId");
CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated();
- when(core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
+ when(mocks.core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder()
.put("Message", "CreateReply")
.put("Sone", "SoneId")
@Test(expected = FcpException.class)
public void verifyThatCreatingAReplyWithoutSoneCausesAnError() throws FcpException {
- Sone sone = mockLocalSone(core, "SoneId");
- mockPost(core, sone, "PostId");
+ Sone sone = mocks.mockLocalSone("SoneId");
+ mocks.mockPost(sone, "PostId");
CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated();
- when(core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
+ when(mocks.core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder()
.put("Message", "CreateReply")
.put("Post", "PostId")
@Test(expected = FcpException.class)
public void verifyThatCreatingAReplyWithoutPostCausesAnError() throws FcpException {
- mockLocalSone(core, "SoneId");
+ mocks.mockLocalSone("SoneId");
CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated();
- when(core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
+ when(mocks.core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder()
.put("Message", "CreateReply")
.put("Sone", "SoneId")
@Test(expected = FcpException.class)
public void verifyThatCreatingAReplyWithoutTextCausesAnError() throws FcpException {
- Sone sone = mockLocalSone(core, "SoneId");
- mockPost(core, sone, "PostId");
+ Sone sone = mocks.mockLocalSone("SoneId");
+ mocks.mockPost(sone, "PostId");
CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated();
- when(core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
+ when(mocks.core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder()
.put("Message", "CreateReply")
.put("Sone", "SoneId")
package net.pterodactylus.sone.fcp;
-import static net.pterodactylus.sone.data.Mocks.mockCore;
-import static net.pterodactylus.sone.data.Mocks.mockDatabase;
-import static net.pterodactylus.sone.data.Mocks.mockLocalSone;
-import static net.pterodactylus.sone.data.Mocks.mockPost;
-import static net.pterodactylus.sone.data.Mocks.mockRemoteSone;
import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentCaptor.forClass;
import static org.mockito.Mockito.doNothing;
-import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Mocks;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.database.Database;
import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
import net.pterodactylus.sone.freenet.fcp.Command.Response;
import net.pterodactylus.sone.freenet.fcp.FcpException;
*/
public class DeletePostCommandTest {
- private final Database database = mockDatabase();
- private final Core core = mockCore(database);
- private final DeletePostCommand deletePostCommand = new DeletePostCommand(core);
+ private final Mocks mocks = new Mocks();
+ private final DeletePostCommand deletePostCommand = new DeletePostCommand(mocks.core);
@Test
public void verifyThatDeletingAPostWorks() throws FcpException {
- Sone sone = mockLocalSone(core, "Sone");
- Post post = mockPost(core, sone, "PostId");
+ Sone sone = mocks.mockLocalSone("Sone");
+ Post post = mocks.mockPost(sone, "PostId");
ArgumentCaptor<Post> deletedPost = forClass(Post.class);
- doNothing().when(core).deletePost(deletedPost.capture());
+ doNothing().when(mocks.core).deletePost(deletedPost.capture());
SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder()
.put("Message", "DeletePost")
.put("Post", "PostId")
@Test
public void verifyThatDeletingAPostFromANonLocalSoneCausesAnError() throws FcpException {
- Sone sone = mockRemoteSone(core, "Sone");
- Post post = mockPost(core, sone, "PostId");
+ Sone sone = mocks.mockRemoteSone("Sone");
+ Post post = mocks.mockPost(sone, "PostId");
SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder()
.put("Message", "DeletePost")
.put("Post", "PostId")
@Test(expected = FcpException.class)
public void verifyThatDeletingAPostWithAnInvalidPostIdCausesAnError() throws FcpException {
- Sone sone = mockLocalSone(core, "Sone");
- mockPost(core, sone, "PostId");
+ Sone sone = mocks.mockLocalSone("Sone");
+ mocks.mockPost(sone, "PostId");
SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder()
.put("Message", "DeletePost")
.put("Post", "OtherPostId")
package net.pterodactylus.sone.fcp;
-import static net.pterodactylus.sone.data.Mocks.mockCore;
-import static net.pterodactylus.sone.data.Mocks.mockDatabase;
-import static net.pterodactylus.sone.data.Mocks.mockLocalSone;
-import static net.pterodactylus.sone.data.Mocks.mockPostReply;
-import static net.pterodactylus.sone.data.Mocks.mockRemoteSone;
import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentCaptor.forClass;
import static org.mockito.Mockito.doNothing;
-import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Mocks;
import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.database.Database;
import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
import net.pterodactylus.sone.freenet.fcp.Command.Response;
import net.pterodactylus.sone.freenet.fcp.FcpException;
*/
public class DeleteReplyCommandTest {
- private final Database database = mockDatabase();
- private final Core core = mockCore(database);
- private final DeleteReplyCommand deleteReplyCommand = new DeleteReplyCommand(core);
+ private final Mocks mocks = new Mocks();
+ private final DeleteReplyCommand deleteReplyCommand = new DeleteReplyCommand(mocks.core);
@Test
public void verifyThatDeletingAReplyWorks() throws FcpException {
- Sone sone = mockLocalSone(core, "SoneId");
- PostReply postReply = mockPostReply(core, sone, "ReplyId");
+ Sone sone = mocks.mockLocalSone("SoneId");
+ PostReply postReply = mocks.mockPostReply(sone, "ReplyId");
ArgumentCaptor<PostReply> postReplyCaptor = forClass(PostReply.class);
- doNothing().when(core).deleteReply(postReplyCaptor.capture());
+ doNothing().when(mocks.core).deleteReply(postReplyCaptor.capture());
SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder()
.put("Message", "DeleteReply")
.put("Reply", "ReplyId")
@Test
public void verifyThatDeletingAReplyFromANonLocalSoneCausesAnError() throws FcpException {
- Sone sone = mockRemoteSone(core, "SoneId");
- mockPostReply(core, sone, "ReplyId");
+ Sone sone = mocks.mockRemoteSone("SoneId");
+ mocks.mockPostReply(sone, "ReplyId");
SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder()
.put("Message", "DeleteReply")
.put("Reply", "ReplyId")