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.any;
28 import static org.mockito.Matchers.anyString;
29 import static org.mockito.Matchers.eq;
30 import static org.mockito.Mockito.doAnswer;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
34 import java.util.Collection;
35 import java.util.List;
39 import net.pterodactylus.sone.core.Core;
40 import net.pterodactylus.sone.core.Options;
41 import net.pterodactylus.sone.core.Options.DefaultOption;
42 import net.pterodactylus.sone.core.Preferences;
43 import net.pterodactylus.sone.data.impl.DefaultPostBuilder;
44 import net.pterodactylus.sone.data.impl.DefaultPostReplyBuilder;
45 import net.pterodactylus.sone.database.Database;
46 import net.pterodactylus.sone.database.PostBuilder.PostCreated;
47 import net.pterodactylus.sone.database.PostReplyBuilder;
48 import net.pterodactylus.sone.database.PostReplyBuilder.PostReplyCreated;
49 import net.pterodactylus.sone.utils.IntegerRangePredicate;
50 import net.pterodactylus.sone.web.WebInterface;
51 import net.pterodactylus.sone.web.page.FreenetRequest;
53 import freenet.clients.http.SessionManager.Session;
54 import freenet.clients.http.ToadletContext;
55 import freenet.support.api.HTTPRequest;
57 import com.google.common.base.Function;
58 import com.google.common.base.Optional;
59 import com.google.common.base.Predicates;
60 import com.google.common.collect.FluentIterable;
61 import com.google.common.collect.HashMultimap;
62 import com.google.common.collect.Multimap;
63 import com.google.common.collect.Ordering;
64 import com.google.common.collect.SetMultimap;
65 import org.mockito.Matchers;
66 import org.mockito.invocation.InvocationOnMock;
67 import org.mockito.stubbing.Answer;
70 * Mocks reusable in multiple tests.
72 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
76 private final Multimap<Sone, Post> sonePosts = create();
77 private final Map<String, Sone> sones = newHashMap();
78 private Optional<Sone> currentSone = absent();
79 private final Map<String, Post> posts = newHashMap();
80 private final Multimap<Post, PostReply> postReplies = create();
81 private final Multimap<String, Post> directedPosts = create();
82 private final SetMultimap<Post, Sone> postLikingSones = HashMultimap.create();
83 private final SetMultimap<PostReply, Sone> postReplyLikingSones = HashMultimap.create();
84 public final Database database;
85 public final Core core;
86 public final WebInterface webInterface;
89 database = mockDatabase();
90 core = mockCore(database);
91 webInterface = mockWebInterface(core);
92 when(database.getSone()).thenReturn(new Function<String, Optional<Sone>>() {
94 public Optional<Sone> apply(String soneId) {
95 return (soneId == null) ? Optional.<Sone>absent() : fromNullable(sones.get(soneId));
98 Answer<Sone> returnCurrentSone = new Answer<Sone>() {
100 public Sone answer(InvocationOnMock invocation) throws Throwable {
101 return currentSone.orNull();
104 when(webInterface.getCurrentSone(any(ToadletContext.class))).then(returnCurrentSone);
105 when(webInterface.getCurrentSone(any(Session.class))).then(returnCurrentSone);
106 when(core.getSones()).then(new Answer<Collection<Sone>>() {
108 public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
109 return sones.values();
112 when(core.getLocalSone(anyString())).then(new Answer<Optional<Sone>>() {
114 public Optional<Sone> answer(InvocationOnMock invocation) throws Throwable {
115 Sone localSone = sones.get(invocation.getArguments()[0]);
116 return ((localSone == null) || (!localSone.isLocal())) ? Optional.<Sone>absent() : of(localSone);
119 when(core.getLocalSones()).then(new Answer<Collection<Sone>>() {
121 public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
122 return FluentIterable.from(sones.values()).filter(Sone.LOCAL_SONE_FILTER).toList();
125 when(core.postCreated()).thenReturn(Optional.<PostCreated>of(new PostCreated() {
127 public void postCreated(Post post) {
128 posts.put(post.getId(), post);
129 sonePosts.put(post.getSone(), post);
132 when(core.postReplyCreated()).then(new Answer<Optional<PostReplyCreated>>() {
134 public Optional<PostReplyCreated> answer(InvocationOnMock invocation) throws Throwable {
135 return Optional.<PostReplyCreated>of(new PostReplyCreated() {
137 public void postReplyCreated(PostReply postReply) {
138 postReplies.put(postReply.getPost().get(), postReply);
143 Options options = createOptions();
144 when(core.getPreferences()).thenReturn(new Preferences(options));
145 when(database.getDirectedPosts(anyString())).then(new Answer<Collection<Post>>() {
147 public Collection<Post> answer(InvocationOnMock invocation) throws Throwable {
148 return directedPosts.get((String) invocation.getArguments()[0]);
153 private Options createOptions() {
154 Options options = new Options();
155 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new IntegerRangePredicate(0, Integer.MAX_VALUE)));
156 options.addIntegerOption("PostsPerPage", new DefaultOption<Integer>(10, new IntegerRangePredicate(1, Integer.MAX_VALUE)));
157 options.addIntegerOption("ImagesPerPage", new DefaultOption<Integer>(9, new IntegerRangePredicate(1, Integer.MAX_VALUE)));
158 options.addIntegerOption("CharactersPerPost", new DefaultOption<Integer>(400, Predicates.<Integer>or(new IntegerRangePredicate(50, Integer.MAX_VALUE), Predicates.equalTo(-1))));
159 options.addIntegerOption("PostCutOffLength", new DefaultOption<Integer>(200, Predicates.<Integer>or(new IntegerRangePredicate(50, Integer.MAX_VALUE), Predicates.equalTo(-1))));
160 options.addBooleanOption("RequireFullAccess", new DefaultOption<Boolean>(false));
161 options.addIntegerOption("PositiveTrust", new DefaultOption<Integer>(75, new IntegerRangePredicate(0, 100)));
162 options.addIntegerOption("NegativeTrust", new DefaultOption<Integer>(-25, new IntegerRangePredicate(-100, 100)));
163 options.addStringOption("TrustComment", new DefaultOption<String>("Set from Sone Web Interface"));
164 options.addBooleanOption("ActivateFcpInterface", new DefaultOption<Boolean>(false));
165 options.addIntegerOption("FcpFullAccessRequired", new DefaultOption<Integer>(2));
169 private static Core mockCore(Database database) {
170 Core core = mock(Core.class);
171 when(core.getDatabase()).thenReturn(database);
172 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
176 private Database mockDatabase() {
177 Database database = mock(Database.class);
178 when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
179 when(database.getPost(anyString())).then(new Answer<Optional<Post>>() {
181 public Optional<Post> answer(InvocationOnMock invocation) throws Throwable {
182 return fromNullable(posts.get(invocation.getArguments()[0]));
185 when(database.getPostReply(anyString())).thenReturn(Optional.<PostReply>absent());
189 private static WebInterface mockWebInterface(Core core) {
190 WebInterface webInterface = mock(WebInterface.class);
191 when(webInterface.getCore()).thenReturn(core);
195 public SoneMocker mockSone(String id) {
196 return new SoneMocker(id);
199 public PostMocker mockPost(Sone sone, String postId) {
200 return new PostMocker(postId, sone);
203 public PostReplyMocker mockPostReply(Sone sone, String replyId) {
204 return new PostReplyMocker(replyId, sone);
207 public FreenetRequest mockRequest(String path) {
208 HTTPRequest httpRequest = mock(HTTPRequest.class);
209 when(httpRequest.getMethod()).thenReturn("GET");
210 when(httpRequest.getPath()).thenReturn(path);
211 FreenetRequest request = mock(FreenetRequest.class);
212 when(request.getHttpRequest()).thenReturn(httpRequest);
216 public class SoneMocker {
218 private final Sone mockedSone = mock(Sone.class);
219 private final String id;
220 private boolean local;
221 private boolean current;
222 private Optional<String> name = absent();
224 private Profile profile = new Profile(mockedSone);
225 private Collection<String> friends = emptySet();
227 private SoneMocker(String id) {
231 public SoneMocker local() {
236 public SoneMocker current() {
241 public SoneMocker withName(String name) {
242 this.name = fromNullable(name);
246 public SoneMocker withTime(long time) {
251 public SoneMocker withProfileName(String firstName, String middleName, String lastName) {
252 profile.modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
256 public SoneMocker addProfileField(String fieldName, String fieldValue) {
257 profile.setField(profile.addField(fieldName), fieldValue);
261 public SoneMocker withFriends(Collection<String> friends) {
262 this.friends = friends;
266 public Sone create() {
267 when(mockedSone.getId()).thenReturn(id);
268 when(mockedSone.isLocal()).thenReturn(local);
270 currentSone = of(mockedSone);
272 if (name.isPresent()) {
273 when(mockedSone.getName()).thenReturn(name.get());
275 when(mockedSone.getTime()).thenReturn(time);
276 when(mockedSone.getProfile()).thenReturn(profile);
278 when(mockedSone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
279 when(mockedSone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
281 public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
282 return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]);
285 when(mockedSone.hasFriend(anyString())).thenReturn(false);
286 when(mockedSone.getFriends()).thenReturn(friends);
287 when(mockedSone.hasFriend(anyString())).then(new Answer<Boolean>() {
289 public Boolean answer(InvocationOnMock invocation) throws Throwable {
290 return friends.contains(invocation.getArguments()[0]);
294 when(mockedSone.newPostBuilder()).thenThrow(IllegalStateException.class);
295 when(mockedSone.newPostReplyBuilder(anyString())).thenThrow(IllegalStateException.class);
297 when(core.getSone(eq(id))).thenReturn(of(mockedSone));
298 when(database.getSone(eq(id))).thenReturn(of(mockedSone));
299 when(mockedSone.getPosts()).then(new Answer<List<Post>>() {
301 public List<Post> answer(InvocationOnMock invocationOnMock) throws Throwable {
302 return from(Post.TIME_COMPARATOR).sortedCopy(sonePosts.get(mockedSone));
305 when(mockedSone.toString()).thenReturn(String.format("Sone[%s]", id));
306 sones.put(id, mockedSone);
312 public class PostMocker {
314 private final Post post = mock(Post.class);
315 private final String id;
316 private final Sone sone;
317 private Optional<String> recipientId = absent();
319 private Optional<String> text = absent();
321 public PostMocker(String id, Sone sone) {
326 public PostMocker withRecipient(String recipientId) {
327 this.recipientId = fromNullable(recipientId);
331 public PostMocker withTime(long time) {
336 public PostMocker withText(String text) {
337 this.text = fromNullable(text);
341 public Post create() {
342 when(post.getId()).thenReturn(id);
343 when(post.getSone()).thenReturn(sone);
344 when(post.getRecipientId()).thenReturn(recipientId);
345 if (recipientId.isPresent()) {
346 directedPosts.put(recipientId.get(), post);
348 when(post.getTime()).thenReturn(time);
349 if (text.isPresent()) {
350 when(post.getText()).thenReturn(text.get());
352 when(database.getPost(eq(id))).thenReturn(of(post));
353 sonePosts.put(sone, post);
354 when(post.getReplies()).then(new Answer<List<PostReply>>() {
356 public List<PostReply> answer(InvocationOnMock invocation) throws Throwable {
357 return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(postReplies.get(post));
360 doAnswer(new Answer<Void>() {
362 public Void answer(InvocationOnMock invocation) throws Throwable {
363 postLikingSones.put(post, (Sone) invocation.getArguments()[0]);
366 }).when(post).like(Matchers.<Sone>any());
367 doAnswer(new Answer<Void>() {
369 public Void answer(InvocationOnMock invocation) throws Throwable {
370 postLikingSones.remove(post, (Sone) invocation.getArguments()[0]);
373 }).when(post).unlike(Matchers.<Sone>any());
374 when(post.getLikes()).thenAnswer(new Answer<Set<Sone>>() {
376 public Set<Sone> answer(InvocationOnMock invocation) throws Throwable {
377 return postLikingSones.get(post);
385 public class PostReplyMocker {
387 private final PostReply postReply = mock(PostReply.class);
388 private final String id;
389 private final Sone sone;
390 private Optional<Post> post = absent();
392 private Optional<String> text = absent();
394 public PostReplyMocker(String id, Sone sone) {
399 public PostReplyMocker inReplyTo(Post post) {
400 this.post = fromNullable(post);
404 public PostReplyMocker withTime(long time) {
409 public PostReplyMocker withText(String text) {
410 this.text = fromNullable(text);
414 public PostReply create() {
415 when(postReply.getId()).thenReturn(id);
416 when(postReply.getSone()).thenReturn(sone);
417 when(postReply.getTime()).thenReturn(time);
418 when(database.getPostReply(eq(id))).thenReturn(of(postReply));
419 if (post.isPresent()) {
420 postReplies.put(post.get(), postReply);
422 if (text.isPresent()) {
423 when(postReply.getText()).thenReturn(text.get());
425 doAnswer(new Answer<Void>() {
427 public Void answer(InvocationOnMock invocation) throws Throwable {
428 postReplyLikingSones.put(postReply, (Sone) invocation.getArguments()[0]);
431 }).when(postReply).like(Matchers.<Sone>any());
432 doAnswer(new Answer<Void>() {
434 public Void answer(InvocationOnMock invocation) throws Throwable {
435 postReplyLikingSones.remove(postReply, invocation.getArguments()[0]);
438 }).when(postReply).unlike(Matchers.<Sone>any());
439 when(postReply.getLikes()).thenAnswer(new Answer<Set<Sone>>() {
441 public Set<Sone> answer(InvocationOnMock invocation) throws Throwable {
442 return postReplyLikingSones.get(postReply);