f1b0a09b2bf877b6306095a399d73d479b4acd7b
[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.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;
32
33 import java.util.Collection;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Set;
37
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;
43
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;
54
55 /**
56  * Mocks reusable in multiple tests.
57  *
58  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
59  */
60 public class Mocks {
61
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;
70
71         public Mocks() {
72                 database = mockDatabase();
73                 core = mockCore(database);
74                 when(database.getSone()).thenReturn(new Function<String, Optional<Sone>>() {
75                         @Override
76                         public Optional<Sone> apply(String soneId) {
77                                 return (soneId == null) ? Optional.<Sone>absent() : fromNullable(sones.get(soneId));
78                         }
79                 });
80                 when(core.getSones()).then(new Answer<Collection<Sone>>() {
81                         @Override
82                         public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
83                                 return sones.values();
84                         }
85                 });
86                 when(core.getLocalSones()).then(new Answer<Collection<Sone>>() {
87                         @Override
88                         public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
89                                 return FluentIterable.from(sones.values()).filter(Sone.LOCAL_SONE_FILTER).toList();
90                         }
91                 });
92                 when(database.getDirectedPosts(anyString())).then(new Answer<Collection<Post>>() {
93                         @Override
94                         public Collection<Post> answer(InvocationOnMock invocation) throws Throwable {
95                                 return directedPosts.get((String) invocation.getArguments()[0]);
96                         }
97                 });
98         }
99
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());
104                 return core;
105         }
106
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());
112                 return database;
113         }
114
115         public SoneMocker mockSone(String id) {
116                 return new SoneMocker(id);
117         }
118
119         public PostMocker mockPost(Sone sone, String postId) {
120                 return new PostMocker(postId, sone);
121         }
122
123         public PostReplyMocker mockPostReply(Sone sone, String replyId) {
124                 return new PostReplyMocker(replyId, sone);
125         }
126
127         public class SoneMocker {
128
129                 private final Sone mockedSone = mock(Sone.class);
130                 private final String id;
131                 private boolean local;
132                 private Optional<String> name = absent();
133                 private long time;
134                 private Profile profile = new Profile(mockedSone);
135                 private Collection<String> friends = emptySet();
136
137                 private SoneMocker(String id) {
138                         this.id = id;
139                 }
140
141                 public SoneMocker local() {
142                         local = true;
143                         return this;
144                 }
145
146                 public SoneMocker withName(String name) {
147                         this.name = fromNullable(name);
148                         return this;
149                 }
150
151                 public SoneMocker withTime(long time) {
152                         this.time = time;
153                         return this;
154                 }
155
156                 public SoneMocker withProfileName(String firstName, String middleName, String lastName) {
157                         profile.modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
158                         return this;
159                 }
160
161                 public SoneMocker addProfileField(String fieldName, String fieldValue) {
162                         profile.setField(profile.addField(fieldName), fieldValue);
163                         return this;
164                 }
165
166                 public SoneMocker withFriends(Collection<String> friends) {
167                         this.friends = friends;
168                         return this;
169                 }
170
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());
176                         }
177                         when(mockedSone.getTime()).thenReturn(time);
178                         when(mockedSone.getProfile()).thenReturn(profile);
179                         if (local) {
180                                 when(mockedSone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
181                                 when(mockedSone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
182                                         @Override
183                                         public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
184                                                 return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]);
185                                         }
186                                 });
187                                 when(mockedSone.hasFriend(anyString())).thenReturn(false);
188                                 when(mockedSone.getFriends()).thenReturn(friends);
189                                 when(mockedSone.hasFriend(anyString())).then(new Answer<Boolean>() {
190                                         @Override
191                                         public Boolean answer(InvocationOnMock invocation) throws Throwable {
192                                                 return friends.contains(invocation.getArguments()[0]);
193                                         }
194                                 });
195                         } else {
196                                 when(mockedSone.newPostBuilder()).thenThrow(IllegalStateException.class);
197                                 when(mockedSone.newPostReplyBuilder(anyString())).thenThrow(IllegalStateException.class);
198                         }
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>>() {
202                                 @Override
203                                 public List<Post> answer(InvocationOnMock invocationOnMock) throws Throwable {
204                                         return from(Post.TIME_COMPARATOR).sortedCopy(sonePosts.get(mockedSone));
205                                 }
206                         });
207                         when(mockedSone.toString()).thenReturn(String.format("Sone[%s]", id));
208                         sones.put(id, mockedSone);
209                         return mockedSone;
210                 }
211
212         }
213
214         public class PostMocker {
215
216                 private final Post post = mock(Post.class);
217                 private final String id;
218                 private final Sone sone;
219                 private Optional<String> recipientId = absent();
220                 private long time;
221                 private Optional<String> text = absent();
222
223                 public PostMocker(String id, Sone sone) {
224                         this.id = id;
225                         this.sone = sone;
226                 }
227
228                 public PostMocker withRecipient(String recipientId) {
229                         this.recipientId = fromNullable(recipientId);
230                         return this;
231                 }
232
233                 public PostMocker withTime(long time) {
234                         this.time = time;
235                         return this;
236                 }
237
238                 public PostMocker withText(String text) {
239                         this.text = fromNullable(text);
240                         return this;
241                 }
242
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);
249                         }
250                         when(post.getTime()).thenReturn(time);
251                         if (text.isPresent()) {
252                                 when(post.getText()).thenReturn(text.get());
253                         }
254                         when(database.getPost(eq(id))).thenReturn(of(post));
255                         sonePosts.put(sone, post);
256                         when(post.getReplies()).then(new Answer<List<PostReply>>() {
257                                 @Override
258                                 public List<PostReply> answer(InvocationOnMock invocation) throws Throwable {
259                                         return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(postReplies.get(post));
260                                 }
261                         });
262                         doAnswer(new Answer<Void>() {
263                                 @Override
264                                 public Void answer(InvocationOnMock invocation) throws Throwable {
265                                         postLikingSones.put(post, (Sone) invocation.getArguments()[0]);
266                                         return null;
267                                 }
268                         }).when(post).like(Matchers.<Sone>any());
269                         doAnswer(new Answer<Void>() {
270                                 @Override
271                                 public Void answer(InvocationOnMock invocation) throws Throwable {
272                                         postLikingSones.remove(post, (Sone) invocation.getArguments()[0]);
273                                         return null;
274                                 }
275                         }).when(post).unlike(Matchers.<Sone>any());
276                         when(post.getLikes()).thenAnswer(new Answer<Set<Sone>>() {
277                                 @Override
278                                 public Set<Sone> answer(InvocationOnMock invocation) throws Throwable {
279                                         return postLikingSones.get(post);
280                                 }
281                         });
282                         return post;
283                 }
284
285         }
286
287         public class PostReplyMocker {
288
289                 private final PostReply postReply = mock(PostReply.class);
290                 private final String id;
291                 private final Sone sone;
292                 private Optional<Post> post = absent();
293                 private long time;
294                 private Optional<String> text = absent();
295
296                 public PostReplyMocker(String id, Sone sone) {
297                         this.id = id;
298                         this.sone = sone;
299                 }
300
301                 public PostReplyMocker inReplyTo(Post post) {
302                         this.post = fromNullable(post);
303                         return this;
304                 }
305
306                 public PostReplyMocker withTime(long time) {
307                         this.time = time;
308                         return this;
309                 }
310
311                 public PostReplyMocker withText(String text) {
312                         this.text = fromNullable(text);
313                         return this;
314                 }
315
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);
323                         }
324                         if (text.isPresent()) {
325                                 when(postReply.getText()).thenReturn(text.get());
326                         }
327                         doAnswer(new Answer<Void>() {
328                                 @Override
329                                 public Void answer(InvocationOnMock invocation) throws Throwable {
330                                         postReplyLikingSones.put(postReply, (Sone) invocation.getArguments()[0]);
331                                         return null;
332                                 }
333                         }).when(postReply).like(Matchers.<Sone>any());
334                         doAnswer(new Answer<Void>() {
335                                 @Override
336                                 public Void answer(InvocationOnMock invocation) throws Throwable {
337                                         postReplyLikingSones.remove(postReply, invocation.getArguments()[0]);
338                                         return null;
339                                 }
340                         }).when(postReply).unlike(Matchers.<Sone>any());
341                         when(postReply.getLikes()).thenAnswer(new Answer<Set<Sone>>() {
342                                 @Override
343                                 public Set<Sone> answer(InvocationOnMock invocation) throws Throwable {
344                                         return postReplyLikingSones.get(postReply);
345                                 }
346                         });
347                         return postReply;
348                 }
349         }
350
351 }