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;
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) {
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;
+ }
+
+ }
+
}
@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");
@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));
@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));
@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));
@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));
@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));
@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));
@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));
@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()
@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));
@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());
@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")
@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")
@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());
@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")
@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());
@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")
@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")
@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.");