845f7c37cc977ec1b57845799e31ec797c248926
[Sone.git] / src / test / java / net / pterodactylus / sone / data / Mocks.java
1 /*
2  * Sone - Mocks.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.data;
19
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;
34
35 import java.net.URI;
36 import java.net.URISyntaxException;
37 import java.util.Collection;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Set;
41
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;
59
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;
64
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;
76
77 /**
78  * Mocks reusable in multiple tests.
79  *
80  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
81  */
82 public class Mocks {
83
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;
95
96         public Mocks() {
97                 database = mockDatabase();
98                 core = mockCore(database);
99                 webInterface = mockWebInterface(core);
100                 when(database.getSone()).thenReturn(new Function<String, Optional<Sone>>() {
101                         @Override
102                         public Optional<Sone> apply(String soneId) {
103                                 return (soneId == null) ? Optional.<Sone>absent() : fromNullable(sones.get(soneId));
104                         }
105                 });
106                 Answer<Sone> returnCurrentSone = new Answer<Sone>() {
107                         @Override
108                         public Sone answer(InvocationOnMock invocation) throws Throwable {
109                                 return currentSone.orNull();
110                         }
111                 };
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>>() {
115                         @Override
116                         public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
117                                 return sones.values();
118                         }
119                 });
120                 when(core.getLocalSone(anyString())).then(new Answer<Optional<Sone>>() {
121                         @Override
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);
125                         }
126                 });
127                 when(core.getLocalSones()).then(new Answer<Collection<Sone>>() {
128                         @Override
129                         public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
130                                 return FluentIterable.from(sones.values()).filter(Sone.LOCAL_SONE_FILTER).toList();
131                         }
132                 });
133                 when(core.postCreated()).thenReturn(Optional.<PostCreated>of(new PostCreated() {
134                         @Override
135                         public void postCreated(Post post) {
136                                 posts.put(post.getId(), post);
137                                 sonePosts.put(post.getSone(), post);
138                         }
139                 }));
140                 when(core.postReplyCreated()).then(new Answer<Optional<PostReplyCreated>>() {
141                         @Override
142                         public Optional<PostReplyCreated> answer(InvocationOnMock invocation) throws Throwable {
143                                 return Optional.<PostReplyCreated>of(new PostReplyCreated() {
144                                         @Override
145                                         public void postReplyCreated(PostReply postReply) {
146                                                 postReplies.put(postReply.getPost().get(), postReply);
147                                         }
148                                 });
149                         }
150                 });
151                 Options options = createOptions();
152                 when(core.getPreferences()).thenReturn(new Preferences(options));
153                 when(database.getDirectedPosts(anyString())).then(new Answer<Collection<Post>>() {
154                         @Override
155                         public Collection<Post> answer(InvocationOnMock invocation) throws Throwable {
156                                 return directedPosts.get((String) invocation.getArguments()[0]);
157                         }
158                 });
159         }
160
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));
174                 return options;
175         }
176
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());
181                 return core;
182         }
183
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>>() {
188                         @Override
189                         public Optional<Post> answer(InvocationOnMock invocation) throws Throwable {
190                                 return fromNullable(posts.get(invocation.getArguments()[0]));
191                         }
192                 });
193                 when(database.getPostReply(anyString())).thenReturn(Optional.<PostReply>absent());
194                 return database;
195         }
196
197         private static WebInterface mockWebInterface(Core core) {
198                 WebInterface webInterface = mock(WebInterface.class);
199                 when(webInterface.getCore()).thenReturn(core);
200                 return webInterface;
201         }
202
203         public SoneMocker mockSone(String id) {
204                 return new SoneMocker(id);
205         }
206
207         public PostMocker mockPost(Sone sone, String postId) {
208                 return new PostMocker(postId, sone);
209         }
210
211         public PostReplyMocker mockPostReply(Sone sone, String replyId) {
212                 return new PostReplyMocker(replyId, sone);
213         }
214
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);
221                 return request;
222         }
223
224         public class SoneMocker {
225
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();
231                 private long time;
232                 private Profile profile = new Profile(mockedSone);
233                 private Collection<String> friends = emptySet();
234
235                 private SoneMocker(String id) {
236                         this.id = id;
237                 }
238
239                 public SoneMocker local() {
240                         local = true;
241                         return this;
242                 }
243
244                 public SoneMocker current() {
245                         current = true;
246                         return this;
247                 }
248
249                 public SoneMocker withName(String name) {
250                         this.name = fromNullable(name);
251                         return this;
252                 }
253
254                 public SoneMocker withTime(long time) {
255                         this.time = time;
256                         return this;
257                 }
258
259                 public SoneMocker withProfileName(String firstName, String middleName, String lastName) {
260                         profile.modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
261                         return this;
262                 }
263
264                 public SoneMocker addProfileField(String fieldName, String fieldValue) {
265                         profile.setField(profile.addField(fieldName), fieldValue);
266                         return this;
267                 }
268
269                 public SoneMocker withFriends(Collection<String> friends) {
270                         this.friends = friends;
271                         return this;
272                 }
273
274                 public Sone create() {
275                         when(mockedSone.getId()).thenReturn(id);
276                         when(mockedSone.isLocal()).thenReturn(local);
277                         if (current) {
278                                 currentSone = of(mockedSone);
279                         }
280                         if (name.isPresent()) {
281                                 when(mockedSone.getName()).thenReturn(name.get());
282                         }
283                         when(mockedSone.getTime()).thenReturn(time);
284                         when(mockedSone.getProfile()).thenReturn(profile);
285                         if (local) {
286                                 when(mockedSone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
287                                 when(mockedSone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
288                                         @Override
289                                         public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
290                                                 return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]);
291                                         }
292                                 });
293                                 when(mockedSone.hasFriend(anyString())).thenReturn(false);
294                                 when(mockedSone.getFriends()).thenReturn(friends);
295                                 when(mockedSone.hasFriend(anyString())).then(new Answer<Boolean>() {
296                                         @Override
297                                         public Boolean answer(InvocationOnMock invocation) throws Throwable {
298                                                 return friends.contains(invocation.getArguments()[0]);
299                                         }
300                                 });
301                         } else {
302                                 when(mockedSone.newPostBuilder()).thenThrow(IllegalStateException.class);
303                                 when(mockedSone.newPostReplyBuilder(anyString())).thenThrow(IllegalStateException.class);
304                         }
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>>() {
308                                 @Override
309                                 public List<Post> answer(InvocationOnMock invocationOnMock) throws Throwable {
310                                         return from(Post.TIME_COMPARATOR).sortedCopy(sonePosts.get(mockedSone));
311                                 }
312                         });
313                         when(mockedSone.toString()).thenReturn(String.format("Sone[%s]", id));
314                         sones.put(id, mockedSone);
315                         return mockedSone;
316                 }
317
318         }
319
320         public class PostMocker {
321
322                 private final Post post = mock(Post.class);
323                 private final String id;
324                 private final Sone sone;
325                 private Optional<String> recipientId = absent();
326                 private long time;
327                 private Optional<String> text = absent();
328
329                 public PostMocker(String id, Sone sone) {
330                         this.id = id;
331                         this.sone = sone;
332                 }
333
334                 public PostMocker withRecipient(String recipientId) {
335                         this.recipientId = fromNullable(recipientId);
336                         return this;
337                 }
338
339                 public PostMocker withTime(long time) {
340                         this.time = time;
341                         return this;
342                 }
343
344                 public PostMocker withText(String text) {
345                         this.text = fromNullable(text);
346                         return this;
347                 }
348
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);
355                         }
356                         when(post.getTime()).thenReturn(time);
357                         if (text.isPresent()) {
358                                 when(post.getText()).thenReturn(text.get());
359                         }
360                         when(database.getPost(eq(id))).thenReturn(of(post));
361                         sonePosts.put(sone, post);
362                         when(post.getReplies()).then(new Answer<List<PostReply>>() {
363                                 @Override
364                                 public List<PostReply> answer(InvocationOnMock invocation) throws Throwable {
365                                         return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(postReplies.get(post));
366                                 }
367                         });
368                         doAnswer(new Answer<Void>() {
369                                 @Override
370                                 public Void answer(InvocationOnMock invocation) throws Throwable {
371                                         postLikingSones.put(post, (Sone) invocation.getArguments()[0]);
372                                         return null;
373                                 }
374                         }).when(post).like(Matchers.<Sone>any());
375                         doAnswer(new Answer<Void>() {
376                                 @Override
377                                 public Void answer(InvocationOnMock invocation) throws Throwable {
378                                         postLikingSones.remove(post, (Sone) invocation.getArguments()[0]);
379                                         return null;
380                                 }
381                         }).when(post).unlike(Matchers.<Sone>any());
382                         when(post.getLikes()).thenAnswer(new Answer<Set<Sone>>() {
383                                 @Override
384                                 public Set<Sone> answer(InvocationOnMock invocation) throws Throwable {
385                                         return postLikingSones.get(post);
386                                 }
387                         });
388                         return post;
389                 }
390
391         }
392
393         public class PostReplyMocker {
394
395                 private final PostReply postReply = mock(PostReply.class);
396                 private final String id;
397                 private final Sone sone;
398                 private Optional<Post> post = absent();
399                 private long time;
400                 private Optional<String> text = absent();
401
402                 public PostReplyMocker(String id, Sone sone) {
403                         this.id = id;
404                         this.sone = sone;
405                 }
406
407                 public PostReplyMocker inReplyTo(Post post) {
408                         this.post = fromNullable(post);
409                         return this;
410                 }
411
412                 public PostReplyMocker withTime(long time) {
413                         this.time = time;
414                         return this;
415                 }
416
417                 public PostReplyMocker withText(String text) {
418                         this.text = fromNullable(text);
419                         return this;
420                 }
421
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);
429                         }
430                         if (text.isPresent()) {
431                                 when(postReply.getText()).thenReturn(text.get());
432                         }
433                         doAnswer(new Answer<Void>() {
434                                 @Override
435                                 public Void answer(InvocationOnMock invocation) throws Throwable {
436                                         postReplyLikingSones.put(postReply, (Sone) invocation.getArguments()[0]);
437                                         return null;
438                                 }
439                         }).when(postReply).like(Matchers.<Sone>any());
440                         doAnswer(new Answer<Void>() {
441                                 @Override
442                                 public Void answer(InvocationOnMock invocation) throws Throwable {
443                                         postReplyLikingSones.remove(postReply, invocation.getArguments()[0]);
444                                         return null;
445                                 }
446                         }).when(postReply).unlike(Matchers.<Sone>any());
447                         when(postReply.getLikes()).thenAnswer(new Answer<Set<Sone>>() {
448                                 @Override
449                                 public Set<Sone> answer(InvocationOnMock invocation) throws Throwable {
450                                         return postReplyLikingSones.get(postReply);
451                                 }
452                         });
453                         return postReply;
454                 }
455         }
456
457 }