- public static Sone mockRemoteSone(Core core, final String id) {
- 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));
- when(database.getSone(eq(id))).thenReturn(of(sone));
- return sone;
+ public PostMocker mockPost(Sone sone, String postId) {
+ return new PostMocker(postId, sone);
+ }
+
+ public PostReplyMocker mockPostReply(Sone sone, String replyId) {
+ return new PostReplyMocker(replyId, sone);
+ }
+
+ public class SoneMocker {
+
+ private final Sone mockedSone = mock(Sone.class);
+ private final String id;
+ private boolean local;
+ private Optional<String> name = absent();
+ private long time;
+ private Profile profile = new Profile(mockedSone);
+ private Collection<String> friends = emptySet();
+
+ private SoneMocker(String id) {
+ this.id = id;
+ }
+
+ public SoneMocker local() {
+ local = true;
+ return this;
+ }
+
+ public SoneMocker withName(String name) {
+ this.name = fromNullable(name);
+ return this;
+ }
+
+ public SoneMocker withTime(long time) {
+ this.time = time;
+ return this;
+ }
+
+ public SoneMocker withProfileName(String firstName, String middleName, String lastName) {
+ profile.modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
+ return this;
+ }
+
+ public SoneMocker addProfileField(String fieldName, String fieldValue) {
+ profile.setField(profile.addField(fieldName), fieldValue);
+ return this;
+ }
+
+ public SoneMocker withFriends(Collection<String> friends) {
+ this.friends = friends;
+ return this;
+ }
+
+ public Sone create() {
+ when(mockedSone.getId()).thenReturn(id);
+ when(mockedSone.isLocal()).thenReturn(local);
+ if (name.isPresent()) {
+ when(mockedSone.getName()).thenReturn(name.get());
+ }
+ when(mockedSone.getTime()).thenReturn(time);
+ 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]);
+ }
+ });
+ when(mockedSone.hasFriend(anyString())).thenReturn(false);
+ when(mockedSone.getFriends()).thenReturn(friends);
+ when(mockedSone.hasFriend(anyString())).then(new Answer<Boolean>() {
+ @Override
+ public Boolean answer(InvocationOnMock invocation) throws Throwable {
+ return friends.contains(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(Post.TIME_COMPARATOR).sortedCopy(sonePosts.get(mockedSone));
+ }
+ });
+ when(mockedSone.toString()).thenReturn(String.format("Sone[%s]", id));
+ sones.put(id, mockedSone);
+ return mockedSone;
+ }
+