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.anyBoolean;
29 import static org.mockito.Matchers.anyString;
30 import static org.mockito.Matchers.eq;
31 import static org.mockito.Mockito.doAnswer;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.when;
36 import java.net.URISyntaxException;
37 import java.util.Collection;
38 import java.util.List;
42 import net.pterodactylus.sone.core.Core;
43 import net.pterodactylus.sone.core.Options;
44 import net.pterodactylus.sone.core.Options.DefaultOption;
45 import net.pterodactylus.sone.core.Options.Option;
46 import net.pterodactylus.sone.core.Options.OptionWatcher;
47 import net.pterodactylus.sone.core.Preferences;
48 import net.pterodactylus.sone.core.SoneInserter;
49 import net.pterodactylus.sone.data.impl.DefaultPostBuilder;
50 import net.pterodactylus.sone.data.impl.DefaultPostReplyBuilder;
51 import net.pterodactylus.sone.database.Database;
52 import net.pterodactylus.sone.database.PostBuilder.PostCreated;
53 import net.pterodactylus.sone.database.PostReplyBuilder;
54 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
55 import net.pterodactylus.sone.database.PostReplyBuilder.PostReplyCreated;
56 import net.pterodactylus.sone.utils.IntegerRangePredicate;
57 import net.pterodactylus.sone.web.WebInterface;
58 import net.pterodactylus.sone.web.page.FreenetRequest;
60 import freenet.clients.http.HTTPRequestImpl;
61 import freenet.clients.http.SessionManager.Session;
62 import freenet.clients.http.ToadletContext;
63 import freenet.support.api.HTTPRequest;
65 import com.google.common.base.Function;
66 import com.google.common.base.Optional;
67 import com.google.common.base.Predicates;
68 import com.google.common.collect.FluentIterable;
69 import com.google.common.collect.HashMultimap;
70 import com.google.common.collect.Multimap;
71 import com.google.common.collect.Ordering;
72 import com.google.common.collect.SetMultimap;
73 import org.mockito.Matchers;
74 import org.mockito.invocation.InvocationOnMock;
75 import org.mockito.stubbing.Answer;
78 * Mocks reusable in multiple tests.
80 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
84 private final Multimap<Sone, Post> sonePosts = create();
85 private final Map<String, Sone> sones = newHashMap();
86 private Optional<Sone> currentSone = absent();
87 private final Map<String, Post> posts = newHashMap();
88 private final Multimap<Post, PostReply> postReplies = create();
89 private final Multimap<String, Post> directedPosts = create();
90 private final SetMultimap<Post, Sone> postLikingSones = HashMultimap.create();
91 private final SetMultimap<PostReply, Sone> postReplyLikingSones = HashMultimap.create();
92 public final Database database;
93 public final Core core;
94 public final WebInterface webInterface;
97 database = mockDatabase();
98 core = mockCore(database);
99 webInterface = mockWebInterface(core);
100 when(database.getSone()).thenReturn(new Function<String, Optional<Sone>>() {
102 public Optional<Sone> apply(String soneId) {
103 return (soneId == null) ? Optional.<Sone>absent() : fromNullable(sones.get(soneId));
106 Answer<Sone> returnCurrentSone = new Answer<Sone>() {
108 public Sone answer(InvocationOnMock invocation) throws Throwable {
109 return currentSone.orNull();
112 when(webInterface.getCurrentSone(any(ToadletContext.class))).then(returnCurrentSone);
113 when(webInterface.getCurrentSone(any(Session.class))).then(returnCurrentSone);
114 when(core.getSones()).then(new Answer<Collection<Sone>>() {
116 public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
117 return sones.values();
120 when(core.getLocalSone(anyString())).then(new Answer<Optional<Sone>>() {
122 public Optional<Sone> answer(InvocationOnMock invocation) throws Throwable {
123 Sone localSone = sones.get(invocation.getArguments()[0]);
124 return ((localSone == null) || (!localSone.isLocal())) ? Optional.<Sone>absent() : of(localSone);
127 when(core.getLocalSones()).then(new Answer<Collection<Sone>>() {
129 public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
130 return FluentIterable.from(sones.values()).filter(Sone.LOCAL_SONE_FILTER).toList();
133 when(core.postCreated()).thenReturn(Optional.<PostCreated>of(new PostCreated() {
135 public void postCreated(Post post) {
136 posts.put(post.getId(), post);
137 sonePosts.put(post.getSone(), post);
140 when(core.postReplyCreated()).then(new Answer<Optional<PostReplyCreated>>() {
142 public Optional<PostReplyCreated> answer(InvocationOnMock invocation) throws Throwable {
143 return Optional.<PostReplyCreated>of(new PostReplyCreated() {
145 public void postReplyCreated(PostReply postReply) {
146 postReplies.put(postReply.getPost().get(), postReply);
151 Options options = createOptions();
152 when(core.getPreferences()).thenReturn(new Preferences(options));
153 when(database.getDirectedPosts(anyString())).then(new Answer<Collection<Post>>() {
155 public Collection<Post> answer(InvocationOnMock invocation) throws Throwable {
156 return directedPosts.get((String) invocation.getArguments()[0]);
161 private Options createOptions() {
162 Options options = new Options();
163 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new IntegerRangePredicate(0, Integer.MAX_VALUE)));
164 options.addIntegerOption("PostsPerPage", new DefaultOption<Integer>(10, new IntegerRangePredicate(1, Integer.MAX_VALUE)));
165 options.addIntegerOption("ImagesPerPage", new DefaultOption<Integer>(9, new IntegerRangePredicate(1, Integer.MAX_VALUE)));
166 options.addIntegerOption("CharactersPerPost", new DefaultOption<Integer>(400, Predicates.<Integer>or(new IntegerRangePredicate(50, Integer.MAX_VALUE), Predicates.equalTo(-1))));
167 options.addIntegerOption("PostCutOffLength", new DefaultOption<Integer>(200, Predicates.<Integer>or(new IntegerRangePredicate(50, Integer.MAX_VALUE), Predicates.equalTo(-1))));
168 options.addBooleanOption("RequireFullAccess", new DefaultOption<Boolean>(false));
169 options.addIntegerOption("PositiveTrust", new DefaultOption<Integer>(75, new IntegerRangePredicate(0, 100)));
170 options.addIntegerOption("NegativeTrust", new DefaultOption<Integer>(-25, new IntegerRangePredicate(-100, 100)));
171 options.addStringOption("TrustComment", new DefaultOption<String>("Set from Sone Web Interface"));
172 options.addBooleanOption("ActivateFcpInterface", new DefaultOption<Boolean>(false));
173 options.addIntegerOption("FcpFullAccessRequired", new DefaultOption<Integer>(2));
177 private static Core mockCore(Database database) {
178 Core core = mock(Core.class);
179 when(core.getDatabase()).thenReturn(database);
180 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
184 private Database mockDatabase() {
185 Database database = mock(Database.class);
186 when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
187 when(database.getPost(anyString())).then(new Answer<Optional<Post>>() {
189 public Optional<Post> answer(InvocationOnMock invocation) throws Throwable {
190 return fromNullable(posts.get(invocation.getArguments()[0]));
193 when(database.getPostReply(anyString())).thenReturn(Optional.<PostReply>absent());
197 private static WebInterface mockWebInterface(Core core) {
198 WebInterface webInterface = mock(WebInterface.class);
199 when(webInterface.getCore()).thenReturn(core);
203 public SoneMocker mockSone(String id) {
204 return new SoneMocker(id);
207 public PostMocker mockPost(Sone sone, String postId) {
208 return new PostMocker(postId, sone);
211 public PostReplyMocker mockPostReply(Sone sone, String replyId) {
212 return new PostReplyMocker(replyId, sone);
215 public FreenetRequest mockRequest(String path) {
216 HTTPRequest httpRequest = mock(HTTPRequest.class);
217 when(httpRequest.getMethod()).thenReturn("GET");
218 when(httpRequest.getPath()).thenReturn(path);
219 FreenetRequest request = mock(FreenetRequest.class);
220 when(request.getHttpRequest()).thenReturn(httpRequest);
224 public class SoneMocker {
226 private final Sone mockedSone = mock(Sone.class);
227 private final String id;
228 private boolean local;
229 private boolean current;
230 private Optional<String> name = absent();
232 private Profile profile = new Profile(mockedSone);
233 private Collection<String> friends = emptySet();
235 private SoneMocker(String id) {
239 public SoneMocker local() {
244 public SoneMocker current() {
249 public SoneMocker withName(String name) {
250 this.name = fromNullable(name);
254 public SoneMocker withTime(long time) {
259 public SoneMocker withProfileName(String firstName, String middleName, String lastName) {
260 profile.modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
264 public SoneMocker addProfileField(String fieldName, String fieldValue) {
265 profile.setField(profile.addField(fieldName), fieldValue);
269 public SoneMocker withFriends(Collection<String> friends) {
270 this.friends = friends;
274 public Sone create() {
275 when(mockedSone.getId()).thenReturn(id);
276 when(mockedSone.isLocal()).thenReturn(local);
278 currentSone = of(mockedSone);
280 if (name.isPresent()) {
281 when(mockedSone.getName()).thenReturn(name.get());
283 when(mockedSone.getTime()).thenReturn(time);
284 when(mockedSone.getProfile()).thenReturn(profile);
286 when(mockedSone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
287 when(mockedSone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
289 public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
290 return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]);
293 when(mockedSone.hasFriend(anyString())).thenReturn(false);
294 when(mockedSone.getFriends()).thenReturn(friends);
295 when(mockedSone.hasFriend(anyString())).then(new Answer<Boolean>() {
297 public Boolean answer(InvocationOnMock invocation) throws Throwable {
298 return friends.contains(invocation.getArguments()[0]);
302 when(mockedSone.newPostBuilder()).thenThrow(IllegalStateException.class);
303 when(mockedSone.newPostReplyBuilder(anyString())).thenThrow(IllegalStateException.class);
305 when(core.getSone(eq(id))).thenReturn(of(mockedSone));
306 when(database.getSone(eq(id))).thenReturn(of(mockedSone));
307 when(mockedSone.getPosts()).then(new Answer<List<Post>>() {
309 public List<Post> answer(InvocationOnMock invocationOnMock) throws Throwable {
310 return from(Post.TIME_COMPARATOR).sortedCopy(sonePosts.get(mockedSone));
313 when(mockedSone.toString()).thenReturn(String.format("Sone[%s]", id));
314 sones.put(id, mockedSone);
320 public class PostMocker {
322 private final Post post = mock(Post.class);
323 private final String id;
324 private final Sone sone;
325 private Optional<String> recipientId = absent();
327 private Optional<String> text = absent();
329 public PostMocker(String id, Sone sone) {
334 public PostMocker withRecipient(String recipientId) {
335 this.recipientId = fromNullable(recipientId);
339 public PostMocker withTime(long time) {
344 public PostMocker withText(String text) {
345 this.text = fromNullable(text);
349 public Post create() {
350 when(post.getId()).thenReturn(id);
351 when(post.getSone()).thenReturn(sone);
352 when(post.getRecipientId()).thenReturn(recipientId);
353 if (recipientId.isPresent()) {
354 directedPosts.put(recipientId.get(), post);
356 when(post.getTime()).thenReturn(time);
357 if (text.isPresent()) {
358 when(post.getText()).thenReturn(text.get());
360 when(database.getPost(eq(id))).thenReturn(of(post));
361 sonePosts.put(sone, post);
362 when(post.getReplies()).then(new Answer<List<PostReply>>() {
364 public List<PostReply> answer(InvocationOnMock invocation) throws Throwable {
365 return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(postReplies.get(post));
368 doAnswer(new Answer<Void>() {
370 public Void answer(InvocationOnMock invocation) throws Throwable {
371 postLikingSones.put(post, (Sone) invocation.getArguments()[0]);
374 }).when(post).like(Matchers.<Sone>any());
375 doAnswer(new Answer<Void>() {
377 public Void answer(InvocationOnMock invocation) throws Throwable {
378 postLikingSones.remove(post, (Sone) invocation.getArguments()[0]);
381 }).when(post).unlike(Matchers.<Sone>any());
382 when(post.getLikes()).thenAnswer(new Answer<Set<Sone>>() {
384 public Set<Sone> answer(InvocationOnMock invocation) throws Throwable {
385 return postLikingSones.get(post);
393 public class PostReplyMocker {
395 private final PostReply postReply = mock(PostReply.class);
396 private final String id;
397 private final Sone sone;
398 private Optional<Post> post = absent();
400 private Optional<String> text = absent();
402 public PostReplyMocker(String id, Sone sone) {
407 public PostReplyMocker inReplyTo(Post post) {
408 this.post = fromNullable(post);
412 public PostReplyMocker withTime(long time) {
417 public PostReplyMocker withText(String text) {
418 this.text = fromNullable(text);
422 public PostReply create() {
423 when(postReply.getId()).thenReturn(id);
424 when(postReply.getSone()).thenReturn(sone);
425 when(postReply.getTime()).thenReturn(time);
426 when(database.getPostReply(eq(id))).thenReturn(of(postReply));
427 if (post.isPresent()) {
428 postReplies.put(post.get(), postReply);
430 if (text.isPresent()) {
431 when(postReply.getText()).thenReturn(text.get());
433 doAnswer(new Answer<Void>() {
435 public Void answer(InvocationOnMock invocation) throws Throwable {
436 postReplyLikingSones.put(postReply, (Sone) invocation.getArguments()[0]);
439 }).when(postReply).like(Matchers.<Sone>any());
440 doAnswer(new Answer<Void>() {
442 public Void answer(InvocationOnMock invocation) throws Throwable {
443 postReplyLikingSones.remove(postReply, invocation.getArguments()[0]);
446 }).when(postReply).unlike(Matchers.<Sone>any());
447 when(postReply.getLikes()).thenAnswer(new Answer<Set<Sone>>() {
449 public Set<Sone> answer(InvocationOnMock invocation) throws Throwable {
450 return postReplyLikingSones.get(postReply);