From: David ‘Bombe’ Roden Date: Wed, 30 Oct 2013 05:58:03 +0000 (+0100) Subject: Replace Sone mock methods with a mock builder. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=68cf128b60ff1e9a27964de5bc07cebe666c41c6;p=Sone.git Replace Sone mock methods with a mock builder. --- diff --git a/src/test/java/net/pterodactylus/sone/data/Mocks.java b/src/test/java/net/pterodactylus/sone/data/Mocks.java index 00c1522..8720312 100644 --- a/src/test/java/net/pterodactylus/sone/data/Mocks.java +++ b/src/test/java/net/pterodactylus/sone/data/Mocks.java @@ -39,7 +39,6 @@ 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 org.mockito.Matchers; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -81,42 +80,8 @@ public class Mocks { return database; } - public Sone mockLocalSone(final String id) { - final Sone sone = mock(Sone.class); - when(sone.isLocal()).thenReturn(true); - initializeSoneMock(id, sone); - when(sone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id)); - when(sone.newPostReplyBuilder(anyString())).then(new Answer() { - @Override - public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable { - return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]); - } - }); - return sone; - } - - public Sone mockRemoteSone(final String id) { - final Sone sone = mock(Sone.class); - when(sone.isLocal()).thenReturn(false); - initializeSoneMock(id, sone); - when(sone.newPostBuilder()).thenThrow(IllegalStateException.class); - when(sone.newPostReplyBuilder(Matchers.anyObject())).thenThrow(IllegalStateException.class); - return sone; - } - - private void initializeSoneMock(String id, final Sone sone) { - when(sone.getId()).thenReturn(id); - when(sone.getProfile()).thenReturn(new Profile(sone)); - when(core.getSone(eq(id))).thenReturn(of(sone)); - when(database.getSone(eq(id))).thenReturn(of(sone)); - when(sone.getPosts()).then(new Answer>() { - @Override - public List answer(InvocationOnMock invocationOnMock) throws Throwable { - return from(TIME_COMPARATOR).sortedCopy(sonePosts.get(sone)); - } - }); - when(sone.toString()).thenReturn(String.format("Sone[%s]", id)); - sones.add(sone); + public SoneMocker mockSone(String id) { + return new SoneMocker(id); } public Post mockPost(Sone sone, String postId) { @@ -136,4 +101,51 @@ public class Mocks { return postReply; } + public class SoneMocker { + + private final Sone mockedSone = mock(Sone.class); + private final String id; + private boolean local; + private Profile profile = new Profile(mockedSone); + + private SoneMocker(String id) { + this.id = id; + } + + public SoneMocker local() { + local = true; + return this; + } + + public Sone create() { + when(mockedSone.getId()).thenReturn(id); + when(mockedSone.isLocal()).thenReturn(local); + when(mockedSone.getProfile()).thenReturn(profile); + if (local) { + when(mockedSone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id)); + when(mockedSone.newPostReplyBuilder(anyString())).then(new Answer() { + @Override + public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable { + return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]); + } + }); + } else { + when(mockedSone.newPostBuilder()).thenThrow(IllegalStateException.class); + when(mockedSone.newPostReplyBuilder(anyString())).thenThrow(IllegalStateException.class); + } + when(core.getSone(eq(id))).thenReturn(of(mockedSone)); + when(database.getSone(eq(id))).thenReturn(of(mockedSone)); + when(mockedSone.getPosts()).then(new Answer>() { + @Override + public List answer(InvocationOnMock invocationOnMock) throws Throwable { + return from(TIME_COMPARATOR).sortedCopy(sonePosts.get(mockedSone)); + } + }); + when(mockedSone.toString()).thenReturn(String.format("Sone[%s]", id)); + sones.add(mockedSone); + return mockedSone; + } + + } + } diff --git a/src/test/java/net/pterodactylus/sone/data/SoneTest.java b/src/test/java/net/pterodactylus/sone/data/SoneTest.java index 25e58bb..6276fcf 100644 --- a/src/test/java/net/pterodactylus/sone/data/SoneTest.java +++ b/src/test/java/net/pterodactylus/sone/data/SoneTest.java @@ -36,7 +36,7 @@ public class SoneTest { @Test public void verifyThatTransformingASoneIntoItsPostsWorks() { - Sone sone = mocks.mockLocalSone("Sone"); + Sone sone = mocks.mockSone("Sone").local().create(); Post post1 = mocks.mockPost(sone, "Post1"); when(post1.getTime()).thenReturn(1000L); Post post2 = mocks.mockPost(sone, "Post2"); diff --git a/src/test/java/net/pterodactylus/sone/fcp/CreatePostCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/CreatePostCommandTest.java index 6cc6f61..f9cb1ab 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/CreatePostCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/CreatePostCommandTest.java @@ -55,8 +55,8 @@ public class CreatePostCommandTest { @Test public void verifyThatCreatingAPostWorks() throws FcpException { - Sone sone = mocks.mockLocalSone("Sone"); - mocks.mockLocalSone("OtherSone"); + Sone sone = mocks.mockSone("Sone").local().create(); + mocks.mockSone("OtherSone").local().create(); CapturingPostCreated capturingPostCreated = new CapturingPostCreated(); when(mocks.core.postCreated()).thenReturn(Optional.of(capturingPostCreated)); @@ -78,7 +78,7 @@ public class CreatePostCommandTest { @Test public void verifyThatCreatingAPostWithoutRecipientWorks() throws FcpException { - Sone sone = mocks.mockLocalSone("Sone"); + Sone sone = mocks.mockSone("Sone").local().create(); CapturingPostCreated capturingPostCreated = new CapturingPostCreated(); when(mocks.core.postCreated()).thenReturn(Optional.of(capturingPostCreated)); @@ -99,7 +99,7 @@ public class CreatePostCommandTest { @Test public void verifyThatCreatingAPostDirectedToTheSendingSoneCausesAnError() throws FcpException { - mocks.mockLocalSone("Sone"); + mocks.mockSone("Sone").local().create(); CapturingPostCreated capturingPostCreated = new CapturingPostCreated(); when(mocks.core.postCreated()).thenReturn(Optional.of(capturingPostCreated)); @@ -116,7 +116,7 @@ public class CreatePostCommandTest { @Test(expected = FcpException.class) public void verifyThatCreatingAPostWithoutTextCausesAnError() throws FcpException { - mocks.mockLocalSone("Sone"); + mocks.mockSone("Sone").create(); CapturingPostCreated capturingPostCreated = new CapturingPostCreated(); when(mocks.core.postCreated()).thenReturn(Optional.of(capturingPostCreated)); @@ -128,7 +128,7 @@ public class CreatePostCommandTest { @Test(expected = FcpException.class) public void verifyThatCreatingAPostWithoutSoneCausesAnError() throws FcpException { - mocks.mockLocalSone("Sone"); + mocks.mockSone("Sone").local().create(); CapturingPostCreated capturingPostCreated = new CapturingPostCreated(); when(mocks.core.postCreated()).thenReturn(Optional.of(capturingPostCreated)); diff --git a/src/test/java/net/pterodactylus/sone/fcp/CreateReplyCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/CreateReplyCommandTest.java index 70f9996..7acad6f 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/CreateReplyCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/CreateReplyCommandTest.java @@ -53,7 +53,7 @@ public class CreateReplyCommandTest { @Test public void verifyThatCreatingAFullySpecifiedReplyWorks() throws FcpException { - Sone sone = mocks.mockLocalSone("SoneId"); + Sone sone = mocks.mockSone("SoneId").local().create(); mocks.mockPost(sone, "PostId"); CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated(); when(mocks.core.postReplyCreated()).thenReturn(Optional.of(capturingPostReplyCreated)); @@ -78,7 +78,7 @@ public class CreateReplyCommandTest { @Test(expected = FcpException.class) public void verifyThatCreatingAReplyWithoutSoneCausesAnError() throws FcpException { - Sone sone = mocks.mockLocalSone("SoneId"); + Sone sone = mocks.mockSone("SoneId").local().create(); mocks.mockPost(sone, "PostId"); CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated(); when(mocks.core.postReplyCreated()).thenReturn(Optional.of(capturingPostReplyCreated)); @@ -92,7 +92,7 @@ public class CreateReplyCommandTest { @Test(expected = FcpException.class) public void verifyThatCreatingAReplyWithoutPostCausesAnError() throws FcpException { - mocks.mockLocalSone("SoneId"); + mocks.mockSone("SoneId").local().create(); CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated(); when(mocks.core.postReplyCreated()).thenReturn(Optional.of(capturingPostReplyCreated)); SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder() @@ -105,7 +105,7 @@ public class CreateReplyCommandTest { @Test(expected = FcpException.class) public void verifyThatCreatingAReplyWithoutTextCausesAnError() throws FcpException { - Sone sone = mocks.mockLocalSone("SoneId"); + Sone sone = mocks.mockSone("SoneId").local().create(); mocks.mockPost(sone, "PostId"); CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated(); when(mocks.core.postReplyCreated()).thenReturn(Optional.of(capturingPostReplyCreated)); diff --git a/src/test/java/net/pterodactylus/sone/fcp/DeletePostCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/DeletePostCommandTest.java index 5b06277..2dc0382 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/DeletePostCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/DeletePostCommandTest.java @@ -48,7 +48,7 @@ public class DeletePostCommandTest { @Test public void verifyThatDeletingAPostWorks() throws FcpException { - Sone sone = mocks.mockLocalSone("Sone"); + Sone sone = mocks.mockSone("Sone").local().create(); Post post = mocks.mockPost(sone, "PostId"); ArgumentCaptor deletedPost = forClass(Post.class); doNothing().when(mocks.core).deletePost(deletedPost.capture()); @@ -65,7 +65,7 @@ public class DeletePostCommandTest { @Test public void verifyThatDeletingAPostFromANonLocalSoneCausesAnError() throws FcpException { - Sone sone = mocks.mockRemoteSone("Sone"); + Sone sone = mocks.mockSone("Sone").create(); Post post = mocks.mockPost(sone, "PostId"); SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder() .put("Message", "DeletePost") @@ -88,7 +88,7 @@ public class DeletePostCommandTest { @Test(expected = FcpException.class) public void verifyThatDeletingAPostWithAnInvalidPostIdCausesAnError() throws FcpException { - Sone sone = mocks.mockLocalSone("Sone"); + Sone sone = mocks.mockSone("Sone").local().create(); mocks.mockPost(sone, "PostId"); SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder() .put("Message", "DeletePost") diff --git a/src/test/java/net/pterodactylus/sone/fcp/DeleteReplyCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/DeleteReplyCommandTest.java index 422a470..122074f 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/DeleteReplyCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/DeleteReplyCommandTest.java @@ -48,7 +48,7 @@ public class DeleteReplyCommandTest { @Test public void verifyThatDeletingAReplyWorks() throws FcpException { - Sone sone = mocks.mockLocalSone("SoneId"); + Sone sone = mocks.mockSone("SoneId").local().create(); PostReply postReply = mocks.mockPostReply(sone, "ReplyId"); ArgumentCaptor postReplyCaptor = forClass(PostReply.class); doNothing().when(mocks.core).deleteReply(postReplyCaptor.capture()); @@ -82,7 +82,7 @@ public class DeleteReplyCommandTest { @Test public void verifyThatDeletingAReplyFromANonLocalSoneCausesAnError() throws FcpException { - Sone sone = mocks.mockRemoteSone("SoneId"); + Sone sone = mocks.mockSone("SoneId").create(); mocks.mockPostReply(sone, "ReplyId"); SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder() .put("Message", "DeleteReply") diff --git a/src/test/java/net/pterodactylus/sone/fcp/GetLocalSonesCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/GetLocalSonesCommandTest.java index 002ca9b..0ed5204 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/GetLocalSonesCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/GetLocalSonesCommandTest.java @@ -53,9 +53,13 @@ public class GetLocalSonesCommandTest { @Test public void verifyThatOnlyLocalSonesAreReturned() throws FSParseException { - Collection localSones = asList(mocks.mockLocalSone("LSone1"), mocks.mockLocalSone("LSone2"), mocks.mockLocalSone("LSone3")); - mocks.mockRemoteSone("RSone1"); - mocks.mockRemoteSone("RSone2"); + Collection localSones = asList( + mocks.mockSone("LSone1").local().create(), + mocks.mockSone("LSone2").local().create(), + mocks.mockSone("LSone3").local().create() + ); + mocks.mockSone("RSone1").create(); + mocks.mockSone("RSone2").create(); SimpleFieldSet getLocalSonesFieldSet = new SimpleFieldSetBuilder().put("Message", "GetLocalSones").get(); Response response = getLocalSonesCommand.execute(getLocalSonesFieldSet, null, DIRECT); assertThat(response, notNullValue()); diff --git a/src/test/java/net/pterodactylus/sone/fcp/GetPostCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/GetPostCommandTest.java index 09c21ec..03dc205 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/GetPostCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/GetPostCommandTest.java @@ -52,7 +52,7 @@ public class GetPostCommandTest { @Test public void verifyThatGettingAPostWithoutRepliesAndRecipientWorks() throws FcpException, FSParseException { - Sone sone = mocks.mockRemoteSone("SoneId"); + Sone sone = mocks.mockSone("SoneId").create(); Post post = preparePostWithoutRecipient(sone); SimpleFieldSet getPostFieldSet = new SimpleFieldSetBuilder() .put("Message", "GetPost") @@ -65,8 +65,8 @@ public class GetPostCommandTest { @Test public void verifyThatGettingAPostWithoutRepliesAndWithRecipientWorks() throws FcpException, FSParseException { - Sone sone = mocks.mockRemoteSone("SoneId"); - Sone otherSone = mocks.mockRemoteSone("OtherSoneId"); + Sone sone = mocks.mockSone("SoneId").create(); + Sone otherSone = mocks.mockSone("OtherSoneId").create(); Post post = preparePostWithRecipient(sone, otherSone); SimpleFieldSet getPostFieldSet = new SimpleFieldSetBuilder() .put("Message", "GetPost") @@ -79,7 +79,7 @@ public class GetPostCommandTest { @Test public void verifyThatGettingAPostWithRepliesWorks() throws FcpException, FSParseException { - Sone sone = mocks.mockRemoteSone("SoneId"); + Sone sone = mocks.mockSone("SoneId").create(); Post post = preparePostWithoutRecipient(sone); PostReply postReply1 = mocks.mockPostReply(sone, "Reply1"); when(postReply1.getText()).thenReturn("Reply 1.");