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 private final SetMultimap<PostReply, Sone> postReplyLikingSones = HashMultimap.create();
68 public final Database database;
69 public final Core core;
72 database = mockDatabase();
73 core = mockCore(database);
74 when(database.getSone()).thenReturn(new Function<String, Optional<Sone>>() {
76 public Optional<Sone> apply(String soneId) {
77 return (soneId == null) ? Optional.<Sone>absent() : fromNullable(sones.get(soneId));
80 when(core.getSones()).then(new Answer<Collection<Sone>>() {
82 public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
83 return sones.values();
86 when(core.getLocalSones()).then(new Answer<Collection<Sone>>() {
88 public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
89 return FluentIterable.from(sones.values()).filter(Sone.LOCAL_SONE_FILTER).toList();
92 when(database.getDirectedPosts(anyString())).then(new Answer<Collection<Post>>() {
94 public Collection<Post> answer(InvocationOnMock invocation) throws Throwable {
95 return directedPosts.get((String) invocation.getArguments()[0]);
100 private static Core mockCore(Database database) {
101 Core core = mock(Core.class);
102 when(core.getDatabase()).thenReturn(database);
103 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
107 private static Database mockDatabase() {
108 Database database = mock(Database.class);
109 when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
110 when(database.getPost(anyString())).thenReturn(Optional.<Post>absent());
111 when(database.getPostReply(anyString())).thenReturn(Optional.<PostReply>absent());
115 public SoneMocker mockSone(String id) {
116 return new SoneMocker(id);
119 public PostMocker mockPost(Sone sone, String postId) {
120 return new PostMocker(postId, sone);
123 public PostReplyMocker mockPostReply(Sone sone, String replyId) {
124 return new PostReplyMocker(replyId, sone);
127 public class SoneMocker {
129 private final Sone mockedSone = mock(Sone.class);
130 private final String id;
131 private boolean local;
132 private Optional<String> name = absent();
134 private Profile profile = new Profile(mockedSone);
135 private Collection<String> friends = emptySet();
137 private SoneMocker(String id) {
141 public SoneMocker local() {
146 public SoneMocker withName(String name) {
147 this.name = fromNullable(name);
151 public SoneMocker withTime(long time) {
156 public SoneMocker withProfileName(String firstName, String middleName, String lastName) {
157 profile.modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
161 public SoneMocker addProfileField(String fieldName, String fieldValue) {
162 profile.setField(profile.addField(fieldName), fieldValue);
166 public SoneMocker withFriends(Collection<String> friends) {
167 this.friends = friends;
171 public Sone create() {
172 when(mockedSone.getId()).thenReturn(id);
173 when(mockedSone.isLocal()).thenReturn(local);
174 if (name.isPresent()) {
175 when(mockedSone.getName()).thenReturn(name.get());
177 when(mockedSone.getTime()).thenReturn(time);
178 when(mockedSone.getProfile()).thenReturn(profile);
180 when(mockedSone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
181 when(mockedSone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
183 public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
184 return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]);
187 when(mockedSone.hasFriend(anyString())).thenReturn(false);
188 when(mockedSone.getFriends()).thenReturn(friends);
189 when(mockedSone.hasFriend(anyString())).then(new Answer<Boolean>() {
191 public Boolean answer(InvocationOnMock invocation) throws Throwable {
192 return friends.contains(invocation.getArguments()[0]);
196 when(mockedSone.newPostBuilder()).thenThrow(IllegalStateException.class);
197 when(mockedSone.newPostReplyBuilder(anyString())).thenThrow(IllegalStateException.class);
199 when(core.getSone(eq(id))).thenReturn(of(mockedSone));
200 when(database.getSone(eq(id))).thenReturn(of(mockedSone));
201 when(mockedSone.getPosts()).then(new Answer<List<Post>>() {
203 public List<Post> answer(InvocationOnMock invocationOnMock) throws Throwable {
204 return from(Post.TIME_COMPARATOR).sortedCopy(sonePosts.get(mockedSone));
207 when(mockedSone.toString()).thenReturn(String.format("Sone[%s]", id));
208 sones.put(id, mockedSone);
214 public class PostMocker {
216 private final Post post = mock(Post.class);
217 private final String id;
218 private final Sone sone;
219 private Optional<String> recipientId = absent();
221 private Optional<String> text = absent();
223 public PostMocker(String id, Sone sone) {
228 public PostMocker withRecipient(String recipientId) {
229 this.recipientId = fromNullable(recipientId);
233 public PostMocker withTime(long time) {
238 public PostMocker withText(String text) {
239 this.text = fromNullable(text);
243 public Post create() {
244 when(post.getId()).thenReturn(id);
245 when(post.getSone()).thenReturn(sone);
246 when(post.getRecipientId()).thenReturn(recipientId);
247 if (recipientId.isPresent()) {
248 directedPosts.put(recipientId.get(), post);
250 when(post.getTime()).thenReturn(time);
251 if (text.isPresent()) {
252 when(post.getText()).thenReturn(text.get());
254 when(database.getPost(eq(id))).thenReturn(of(post));
255 sonePosts.put(sone, post);
256 when(post.getReplies()).then(new Answer<List<PostReply>>() {
258 public List<PostReply> answer(InvocationOnMock invocation) throws Throwable {
259 return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(postReplies.get(post));
262 doAnswer(new Answer<Void>() {
264 public Void answer(InvocationOnMock invocation) throws Throwable {
265 postLikingSones.put(post, (Sone) invocation.getArguments()[0]);
268 }).when(post).like(Matchers.<Sone>any());
269 doAnswer(new Answer<Void>() {
271 public Void answer(InvocationOnMock invocation) throws Throwable {
272 postLikingSones.remove(post, (Sone) invocation.getArguments()[0]);
275 }).when(post).unlike(Matchers.<Sone>any());
276 when(post.getLikes()).thenAnswer(new Answer<Set<Sone>>() {
278 public Set<Sone> answer(InvocationOnMock invocation) throws Throwable {
279 return postLikingSones.get(post);
287 public class PostReplyMocker {
289 private final PostReply postReply = mock(PostReply.class);
290 private final String id;
291 private final Sone sone;
292 private Optional<Post> post = absent();
294 private Optional<String> text = absent();
296 public PostReplyMocker(String id, Sone sone) {
301 public PostReplyMocker inReplyTo(Post post) {
302 this.post = fromNullable(post);
306 public PostReplyMocker withTime(long time) {
311 public PostReplyMocker withText(String text) {
312 this.text = fromNullable(text);
316 public PostReply create() {
317 when(postReply.getId()).thenReturn(id);
318 when(postReply.getSone()).thenReturn(sone);
319 when(postReply.getTime()).thenReturn(time);
320 when(database.getPostReply(eq(id))).thenReturn(of(postReply));
321 if (post.isPresent()) {
322 postReplies.put(post.get(), postReply);
324 if (text.isPresent()) {
325 when(postReply.getText()).thenReturn(text.get());
327 doAnswer(new Answer<Void>() {
329 public Void answer(InvocationOnMock invocation) throws Throwable {
330 postReplyLikingSones.put(postReply, (Sone) invocation.getArguments()[0]);
333 }).when(postReply).like(Matchers.<Sone>any());
334 doAnswer(new Answer<Void>() {
336 public Void answer(InvocationOnMock invocation) throws Throwable {
337 postReplyLikingSones.remove(postReply, invocation.getArguments()[0]);
340 }).when(postReply).unlike(Matchers.<Sone>any());
341 when(postReply.getLikes()).thenAnswer(new Answer<Set<Sone>>() {
343 public Set<Sone> answer(InvocationOnMock invocation) throws Throwable {
344 return postReplyLikingSones.get(postReply);