Replace Sone mock methods with a mock builder.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 30 Oct 2013 05:58:03 +0000 (06:58 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:49 +0000 (22:25 +0100)
src/test/java/net/pterodactylus/sone/data/Mocks.java
src/test/java/net/pterodactylus/sone/data/SoneTest.java
src/test/java/net/pterodactylus/sone/fcp/CreatePostCommandTest.java
src/test/java/net/pterodactylus/sone/fcp/CreateReplyCommandTest.java
src/test/java/net/pterodactylus/sone/fcp/DeletePostCommandTest.java
src/test/java/net/pterodactylus/sone/fcp/DeleteReplyCommandTest.java
src/test/java/net/pterodactylus/sone/fcp/GetLocalSonesCommandTest.java
src/test/java/net/pterodactylus/sone/fcp/GetPostCommandTest.java

index 00c1522..8720312 100644 (file)
@@ -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<PostReplyBuilder>() {
-                       @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.<String>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<List<Post>>() {
-                       @Override
-                       public List<Post> 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<PostReplyBuilder>() {
+                                       @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<List<Post>>() {
+                               @Override
+                               public List<Post> 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;
+               }
+
+       }
+
 }
index 25e58bb..6276fcf 100644 (file)
@@ -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");
index 6cc6f61..f9cb1ab 100644 (file)
@@ -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.<PostCreated>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.<PostCreated>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.<PostCreated>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.<PostCreated>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.<PostCreated>of(capturingPostCreated));
 
index 70f9996..7acad6f 100644 (file)
@@ -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.<PostReplyCreated>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.<PostReplyCreated>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.<PostReplyCreated>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.<PostReplyCreated>of(capturingPostReplyCreated));
index 5b06277..2dc0382 100644 (file)
@@ -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<Post> 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")
index 422a470..122074f 100644 (file)
@@ -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<PostReply> 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")
index 002ca9b..0ed5204 100644 (file)
@@ -53,9 +53,13 @@ public class GetLocalSonesCommandTest {
 
        @Test
        public void verifyThatOnlyLocalSonesAreReturned() throws FSParseException {
-               Collection<Sone> localSones = asList(mocks.mockLocalSone("LSone1"), mocks.mockLocalSone("LSone2"), mocks.mockLocalSone("LSone3"));
-               mocks.mockRemoteSone("RSone1");
-               mocks.mockRemoteSone("RSone2");
+               Collection<Sone> 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());
index 09c21ec..03dc205 100644 (file)
@@ -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.");