Don’t use raw Answer.
[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         public final Database database;
68         public final Core core;
69
70         public Mocks() {
71                 database = mockDatabase();
72                 core = mockCore(database);
73                 when(database.getSone()).thenReturn(new Function<String, Optional<Sone>>() {
74                         @Override
75                         public Optional<Sone> apply(String soneId) {
76                                 return (soneId == null) ? Optional.<Sone>absent() : fromNullable(sones.get(soneId));
77                         }
78                 });
79                 when(core.getSones()).then(new Answer<Collection<Sone>>() {
80                         @Override
81                         public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
82                                 return sones.values();
83                         }
84                 });
85                 when(core.getLocalSones()).then(new Answer<Collection<Sone>>() {
86                         @Override
87                         public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
88                                 return FluentIterable.from(sones.values()).filter(Sone.LOCAL_SONE_FILTER).toList();
89                         }
90                 });
91                 when(database.getDirectedPosts(anyString())).then(new Answer<Collection<Post>>() {
92                         @Override
93                         public Collection<Post> answer(InvocationOnMock invocation) throws Throwable {
94                                 return directedPosts.get((String) invocation.getArguments()[0]);
95                         }
96                 });
97         }
98
99         private static Core mockCore(Database database) {
100                 Core core = mock(Core.class);
101                 when(core.getDatabase()).thenReturn(database);
102                 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
103                 return core;
104         }
105
106         private static Database mockDatabase() {
107                 Database database = mock(Database.class);
108                 when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
109                 when(database.getPost(anyString())).thenReturn(Optional.<Post>absent());
110                 when(database.getPostReply(anyString())).thenReturn(Optional.<PostReply>absent());
111                 return database;
112         }
113
114         public SoneMocker mockSone(String id) {
115                 return new SoneMocker(id);
116         }
117
118         public PostMocker mockPost(Sone sone, String postId) {
119                 return new PostMocker(postId, sone);
120         }
121
122         public PostReplyMocker mockPostReply(Sone sone, String replyId) {
123                 return new PostReplyMocker(replyId, sone);
124         }
125
126         public class SoneMocker {
127
128                 private final Sone mockedSone = mock(Sone.class);
129                 private final String id;
130                 private boolean local;
131                 private Optional<String> name = absent();
132                 private long time;
133                 private Profile profile = new Profile(mockedSone);
134                 private Collection<String> friends = emptySet();
135
136                 private SoneMocker(String id) {
137                         this.id = id;
138                 }
139
140                 public SoneMocker local() {
141                         local = true;
142                         return this;
143                 }
144
145                 public SoneMocker withName(String name) {
146                         this.name = fromNullable(name);
147                         return this;
148                 }
149
150                 public SoneMocker withTime(long time) {
151                         this.time = time;
152                         return this;
153                 }
154
155                 public SoneMocker withProfileName(String firstName, String middleName, String lastName) {
156                         profile.modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
157                         return this;
158                 }
159
160                 public SoneMocker addProfileField(String fieldName, String fieldValue) {
161                         profile.setField(profile.addField(fieldName), fieldValue);
162                         return this;
163                 }
164
165                 public SoneMocker withFriends(Collection<String> friends) {
166                         this.friends = friends;
167                         return this;
168                 }
169
170                 public Sone create() {
171                         when(mockedSone.getId()).thenReturn(id);
172                         when(mockedSone.isLocal()).thenReturn(local);
173                         if (name.isPresent()) {
174                                 when(mockedSone.getName()).thenReturn(name.get());
175                         }
176                         when(mockedSone.getTime()).thenReturn(time);
177                         when(mockedSone.getProfile()).thenReturn(profile);
178                         if (local) {
179                                 when(mockedSone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
180                                 when(mockedSone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
181                                         @Override
182                                         public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
183                                                 return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]);
184                                         }
185                                 });
186                                 when(mockedSone.hasFriend(anyString())).thenReturn(false);
187                                 when(mockedSone.getFriends()).thenReturn(friends);
188                                 when(mockedSone.hasFriend(anyString())).then(new Answer<Boolean>() {
189                                         @Override
190                                         public Boolean answer(InvocationOnMock invocation) throws Throwable {
191                                                 return friends.contains(invocation.getArguments()[0]);
192                                         }
193                                 });
194                         } else {
195                                 when(mockedSone.newPostBuilder()).thenThrow(IllegalStateException.class);
196                                 when(mockedSone.newPostReplyBuilder(anyString())).thenThrow(IllegalStateException.class);
197                         }
198                         when(core.getSone(eq(id))).thenReturn(of(mockedSone));
199                         when(database.getSone(eq(id))).thenReturn(of(mockedSone));
200                         when(mockedSone.getPosts()).then(new Answer<List<Post>>() {
201                                 @Override
202                                 public List<Post> answer(InvocationOnMock invocationOnMock) throws Throwable {
203                                         return from(Post.TIME_COMPARATOR).sortedCopy(sonePosts.get(mockedSone));
204                                 }
205                         });
206                         when(mockedSone.toString()).thenReturn(String.format("Sone[%s]", id));
207                         sones.put(id, mockedSone);
208                         return mockedSone;
209                 }
210
211         }
212
213         public class PostMocker {
214
215                 private final Post post = mock(Post.class);
216                 private final String id;
217                 private final Sone sone;
218                 private Optional<String> recipientId = absent();
219                 private long time;
220                 private Optional<String> text = absent();
221
222                 public PostMocker(String id, Sone sone) {
223                         this.id = id;
224                         this.sone = sone;
225                 }
226
227                 public PostMocker withRecipient(String recipientId) {
228                         this.recipientId = fromNullable(recipientId);
229                         return this;
230                 }
231
232                 public PostMocker withTime(long time) {
233                         this.time = time;
234                         return this;
235                 }
236
237                 public PostMocker withText(String text) {
238                         this.text = fromNullable(text);
239                         return this;
240                 }
241
242                 public Post create() {
243                         when(post.getId()).thenReturn(id);
244                         when(post.getSone()).thenReturn(sone);
245                         when(post.getRecipientId()).thenReturn(recipientId);
246                         if (recipientId.isPresent()) {
247                                 directedPosts.put(recipientId.get(), post);
248                         }
249                         when(post.getTime()).thenReturn(time);
250                         if (text.isPresent()) {
251                                 when(post.getText()).thenReturn(text.get());
252                         }
253                         when(database.getPost(eq(id))).thenReturn(of(post));
254                         sonePosts.put(sone, post);
255                         when(post.getReplies()).then(new Answer<List<PostReply>>() {
256                                 @Override
257                                 public List<PostReply> answer(InvocationOnMock invocation) throws Throwable {
258                                         return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(postReplies.get(post));
259                                 }
260                         });
261                         doAnswer(new Answer<Void>() {
262                                 @Override
263                                 public Void answer(InvocationOnMock invocation) throws Throwable {
264                                         postLikingSones.put(post, (Sone) invocation.getArguments()[0]);
265                                         return null;
266                                 }
267                         }).when(post).like(Matchers.<Sone>any());
268                         doAnswer(new Answer<Void>() {
269                                 @Override
270                                 public Void answer(InvocationOnMock invocation) throws Throwable {
271                                         postLikingSones.remove(post, (Sone) invocation.getArguments()[0]);
272                                         return null;
273                                 }
274                         }).when(post).unlike(Matchers.<Sone>any());
275                         when(post.getLikes()).thenAnswer(new Answer<Set<Sone>>() {
276                                 @Override
277                                 public Set<Sone> answer(InvocationOnMock invocation) throws Throwable {
278                                         return postLikingSones.get(post);
279                                 }
280                         });
281                         return post;
282                 }
283
284         }
285
286         public class PostReplyMocker {
287
288                 private final PostReply postReply = mock(PostReply.class);
289                 private final String id;
290                 private final Sone sone;
291                 private Optional<Post> post = absent();
292                 private long time;
293                 private Optional<String> text = absent();
294
295                 public PostReplyMocker(String id, Sone sone) {
296                         this.id = id;
297                         this.sone = sone;
298                 }
299
300                 public PostReplyMocker inReplyTo(Post post) {
301                         this.post = fromNullable(post);
302                         return this;
303                 }
304
305                 public PostReplyMocker withTime(long time) {
306                         this.time = time;
307                         return this;
308                 }
309
310                 public PostReplyMocker withText(String text) {
311                         this.text = fromNullable(text);
312                         return this;
313                 }
314
315                 public PostReply create() {
316                         when(postReply.getId()).thenReturn(id);
317                         when(postReply.getSone()).thenReturn(sone);
318                         when(postReply.getTime()).thenReturn(time);
319                         when(database.getPostReply(eq(id))).thenReturn(of(postReply));
320                         if (post.isPresent()) {
321                                 postReplies.put(post.get(), postReply);
322                         }
323                         if (text.isPresent()) {
324                                 when(postReply.getText()).thenReturn(text.get());
325                         }
326                         return postReply;
327                 }
328         }
329
330 }