From 479f6b7187938eb68d2f00d9e10394ceeb18e0c5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 30 Oct 2013 07:20:28 +0100 Subject: [PATCH] Use a builder-style mocker for post replies, too. --- .../java/net/pterodactylus/sone/data/Mocks.java | 38 ++++++++++++++++++---- .../sone/fcp/DeleteReplyCommandTest.java | 4 +-- .../pterodactylus/sone/fcp/GetPostCommandTest.java | 4 +-- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/test/java/net/pterodactylus/sone/data/Mocks.java b/src/test/java/net/pterodactylus/sone/data/Mocks.java index 21e5925..98de409 100644 --- a/src/test/java/net/pterodactylus/sone/data/Mocks.java +++ b/src/test/java/net/pterodactylus/sone/data/Mocks.java @@ -17,6 +17,7 @@ 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; @@ -89,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 { @@ -171,4 +168,33 @@ public class Mocks { } + public class PostReplyMocker { + + private final PostReply postReply = mock(PostReply.class); + private final String id; + private final Sone sone; + private Optional 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; + } + + } + } diff --git a/src/test/java/net/pterodactylus/sone/fcp/DeleteReplyCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/DeleteReplyCommandTest.java index 122074f..8b7cadd 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/DeleteReplyCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/DeleteReplyCommandTest.java @@ -49,7 +49,7 @@ public class DeleteReplyCommandTest { @Test public void verifyThatDeletingAReplyWorks() throws FcpException { Sone sone = mocks.mockSone("SoneId").local().create(); - PostReply postReply = mocks.mockPostReply(sone, "ReplyId"); + PostReply postReply = mocks.mockPostReply(sone, "ReplyId").create(); ArgumentCaptor postReplyCaptor = forClass(PostReply.class); doNothing().when(mocks.core).deleteReply(postReplyCaptor.capture()); SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder() @@ -83,7 +83,7 @@ public class DeleteReplyCommandTest { @Test public void verifyThatDeletingAReplyFromANonLocalSoneCausesAnError() throws FcpException { Sone sone = mocks.mockSone("SoneId").create(); - mocks.mockPostReply(sone, "ReplyId"); + mocks.mockPostReply(sone, "ReplyId").create(); SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder() .put("Message", "DeleteReply") .put("Reply", "ReplyId") diff --git a/src/test/java/net/pterodactylus/sone/fcp/GetPostCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/GetPostCommandTest.java index c6664b9..d303535 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/GetPostCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/GetPostCommandTest.java @@ -81,9 +81,9 @@ public class GetPostCommandTest { public void verifyThatGettingAPostWithRepliesWorks() throws FcpException, FSParseException { Sone sone = mocks.mockSone("SoneId").create(); Post post = preparePostWithoutRecipient(sone); - PostReply postReply1 = mocks.mockPostReply(sone, "Reply1"); + PostReply postReply1 = mocks.mockPostReply(sone, "Reply1").create(); when(postReply1.getText()).thenReturn("Reply 1."); - PostReply postReply2 = mocks.mockPostReply(sone, "Reply2"); + PostReply postReply2 = mocks.mockPostReply(sone, "Reply2").create(); when(postReply2.getText()).thenReturn("Reply 2."); when(post.getReplies()).thenReturn(asList(postReply1, postReply2)); SimpleFieldSet getPostFieldSet = new SimpleFieldSetBuilder() -- 2.7.4