2 * Sone - Mocks.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.data;
20 import static com.google.common.base.Optional.absent;
21 import static com.google.common.base.Optional.fromNullable;
22 import static com.google.common.base.Optional.of;
23 import static com.google.common.collect.ArrayListMultimap.create;
24 import static com.google.common.collect.Maps.newHashMap;
25 import static com.google.common.collect.Ordering.from;
26 import static java.util.Collections.emptySet;
27 import static org.mockito.Matchers.anyString;
28 import static org.mockito.Matchers.eq;
29 import static org.mockito.Mockito.doAnswer;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
33 import java.util.Collection;
34 import java.util.List;
38 import net.pterodactylus.sone.core.Core;
39 import net.pterodactylus.sone.data.impl.DefaultPostBuilder;
40 import net.pterodactylus.sone.data.impl.DefaultPostReplyBuilder;
41 import net.pterodactylus.sone.database.Database;
42 import net.pterodactylus.sone.database.PostReplyBuilder;
44 import com.google.common.base.Function;
45 import com.google.common.base.Optional;
46 import com.google.common.collect.FluentIterable;
47 import com.google.common.collect.HashMultimap;
48 import com.google.common.collect.Multimap;
49 import com.google.common.collect.Ordering;
50 import com.google.common.collect.SetMultimap;
51 import org.mockito.Matchers;
52 import org.mockito.invocation.InvocationOnMock;
53 import org.mockito.stubbing.Answer;
56 * Mocks reusable in multiple tests.
58 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
62 private final Multimap<Sone, Post> sonePosts = create();
63 private final Map<String, Sone> sones = newHashMap();
64 private final Multimap<Post, PostReply> postReplies = create();
65 private final Multimap<String, Post> directedPosts = create();
66 private final SetMultimap<Post, Sone> postLikingSones = HashMultimap.create();
67 public final Database database;
68 public final Core core;
71 database = mockDatabase();
72 core = mockCore(database);
73 when(database.getSone()).thenReturn(new Function<String, Optional<Sone>>() {
75 public Optional<Sone> apply(String soneId) {
76 return (soneId == null) ? Optional.<Sone>absent() : fromNullable(sones.get(soneId));
79 when(core.getSones()).then(new Answer<Collection<Sone>>() {
81 public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
82 return sones.values();
85 when(core.getLocalSones()).then(new Answer<Collection<Sone>>() {
87 public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
88 return FluentIterable.from(sones.values()).filter(Sone.LOCAL_SONE_FILTER).toList();
91 when(database.getDirectedPosts(anyString())).then(new Answer<Collection<Post>>() {
93 public Collection<Post> answer(InvocationOnMock invocation) throws Throwable {
94 return directedPosts.get((String) invocation.getArguments()[0]);
99 private static Core mockCore(Database database) {
100 Core core = mock(Core.class);
101 when(core.getDatabase()).thenReturn(database);
102 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
106 private static Database mockDatabase() {
107 Database database = mock(Database.class);
108 when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
109 when(database.getPost(anyString())).thenReturn(Optional.<Post>absent());
110 when(database.getPostReply(anyString())).thenReturn(Optional.<PostReply>absent());
114 public SoneMocker mockSone(String id) {
115 return new SoneMocker(id);
118 public PostMocker mockPost(Sone sone, String postId) {
119 return new PostMocker(postId, sone);
122 public PostReplyMocker mockPostReply(Sone sone, String replyId) {
123 return new PostReplyMocker(replyId, sone);
126 public class SoneMocker {
128 private final Sone mockedSone = mock(Sone.class);
129 private final String id;
130 private boolean local;
131 private Optional<String> name = absent();
133 private Profile profile = new Profile(mockedSone);
134 private Collection<String> friends = emptySet();
136 private SoneMocker(String id) {
140 public SoneMocker local() {
145 public SoneMocker withName(String name) {
146 this.name = fromNullable(name);
150 public SoneMocker withTime(long time) {
155 public SoneMocker withProfileName(String firstName, String middleName, String lastName) {
156 profile.modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
160 public SoneMocker addProfileField(String fieldName, String fieldValue) {
161 profile.setField(profile.addField(fieldName), fieldValue);
165 public SoneMocker withFriends(Collection<String> friends) {
166 this.friends = friends;
170 public Sone create() {
171 when(mockedSone.getId()).thenReturn(id);
172 when(mockedSone.isLocal()).thenReturn(local);
173 if (name.isPresent()) {
174 when(mockedSone.getName()).thenReturn(name.get());
176 when(mockedSone.getTime()).thenReturn(time);
177 when(mockedSone.getProfile()).thenReturn(profile);
179 when(mockedSone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
180 when(mockedSone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
182 public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
183 return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]);
186 when(mockedSone.hasFriend(anyString())).thenReturn(false);
187 when(mockedSone.getFriends()).thenReturn(friends);
188 when(mockedSone.hasFriend(anyString())).then(new Answer<Boolean>() {
190 public Boolean answer(InvocationOnMock invocation) throws Throwable {
191 return friends.contains(invocation.getArguments()[0]);
195 when(mockedSone.newPostBuilder()).thenThrow(IllegalStateException.class);
196 when(mockedSone.newPostReplyBuilder(anyString())).thenThrow(IllegalStateException.class);
198 when(core.getSone(eq(id))).thenReturn(of(mockedSone));
199 when(database.getSone(eq(id))).thenReturn(of(mockedSone));
200 when(mockedSone.getPosts()).then(new Answer<List<Post>>() {
202 public List<Post> answer(InvocationOnMock invocationOnMock) throws Throwable {
203 return from(Post.TIME_COMPARATOR).sortedCopy(sonePosts.get(mockedSone));
206 when(mockedSone.toString()).thenReturn(String.format("Sone[%s]", id));
207 sones.put(id, mockedSone);
213 public class PostMocker {
215 private final Post post = mock(Post.class);
216 private final String id;
217 private final Sone sone;
218 private Optional<String> recipientId = absent();
220 private Optional<String> text = absent();
222 public PostMocker(String id, Sone sone) {
227 public PostMocker withRecipient(String recipientId) {
228 this.recipientId = fromNullable(recipientId);
232 public PostMocker withTime(long time) {
237 public PostMocker withText(String text) {
238 this.text = fromNullable(text);
242 public Post create() {
243 when(post.getId()).thenReturn(id);
244 when(post.getSone()).thenReturn(sone);
245 when(post.getRecipientId()).thenReturn(recipientId);
246 if (recipientId.isPresent()) {
247 directedPosts.put(recipientId.get(), post);
249 when(post.getTime()).thenReturn(time);
250 if (text.isPresent()) {
251 when(post.getText()).thenReturn(text.get());
253 when(database.getPost(eq(id))).thenReturn(of(post));
254 sonePosts.put(sone, post);
255 when(post.getReplies()).then(new Answer<List<PostReply>>() {
257 public List<PostReply> answer(InvocationOnMock invocation) throws Throwable {
258 return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(postReplies.get(post));
261 doAnswer(new Answer() {
263 public Object answer(InvocationOnMock invocation) throws Throwable {
264 postLikingSones.put(post, (Sone) invocation.getArguments()[0]);
267 }).when(post).like(Matchers.<Sone>any());
268 doAnswer(new Answer() {
270 public Object answer(InvocationOnMock invocation) throws Throwable {
271 postLikingSones.remove(post, (Sone) invocation.getArguments()[0]);
274 }).when(post).unlike(Matchers.<Sone>any());
275 when(post.getLikes()).thenAnswer(new Answer<Set<Sone>>() {
277 public Set<Sone> answer(InvocationOnMock invocation) throws Throwable {
278 return postLikingSones.get(post);
286 public class PostReplyMocker {
288 private final PostReply postReply = mock(PostReply.class);
289 private final String id;
290 private final Sone sone;
291 private Optional<Post> post = absent();
293 private Optional<String> text = absent();
295 public PostReplyMocker(String id, Sone sone) {
300 public PostReplyMocker inReplyTo(Post post) {
301 this.post = fromNullable(post);
305 public PostReplyMocker withTime(long time) {
310 public PostReplyMocker withText(String text) {
311 this.text = fromNullable(text);
315 public PostReply create() {
316 when(postReply.getId()).thenReturn(id);
317 when(postReply.getSone()).thenReturn(sone);
318 when(postReply.getTime()).thenReturn(time);
319 when(database.getPostReply(eq(id))).thenReturn(of(postReply));
320 if (post.isPresent()) {
321 postReplies.put(post.get(), postReply);
323 if (text.isPresent()) {
324 when(postReply.getText()).thenReturn(text.get());