Implement Database.getSone() function.
[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.mock;
30 import static org.mockito.Mockito.when;
31
32 import java.util.Collection;
33 import java.util.List;
34 import java.util.Map;
35
36 import net.pterodactylus.sone.core.Core;
37 import net.pterodactylus.sone.data.impl.DefaultPostBuilder;
38 import net.pterodactylus.sone.data.impl.DefaultPostReplyBuilder;
39 import net.pterodactylus.sone.database.Database;
40 import net.pterodactylus.sone.database.PostReplyBuilder;
41
42 import com.google.common.base.Function;
43 import com.google.common.base.Optional;
44 import com.google.common.collect.FluentIterable;
45 import com.google.common.collect.Multimap;
46 import com.google.common.collect.Ordering;
47 import org.mockito.invocation.InvocationOnMock;
48 import org.mockito.stubbing.Answer;
49
50 /**
51  * Mocks reusable in multiple tests.
52  *
53  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
54  */
55 public class Mocks {
56
57         private final Multimap<Sone, Post> sonePosts = create();
58         private final Map<String, Sone> sones = newHashMap();
59         private final Multimap<Post, PostReply> postReplies = create();
60         private final Multimap<String, Post> directedPosts = create();
61         public final Database database;
62         public final Core core;
63
64         public Mocks() {
65                 database = mockDatabase();
66                 core = mockCore(database);
67                 when(database.getSone()).thenReturn(new Function<String, Optional<Sone>>() {
68                         @Override
69                         public Optional<Sone> apply(String soneId) {
70                                 return (soneId == null) ? Optional.<Sone>absent() : fromNullable(sones.get(soneId));
71                         }
72                 });
73                 when(core.getLocalSones()).then(new Answer<Collection<Sone>>() {
74                         @Override
75                         public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
76                                 return FluentIterable.from(sones.values()).filter(Sone.LOCAL_SONE_FILTER).toList();
77                         }
78                 });
79                 when(database.getDirectedPosts(anyString())).then(new Answer<Collection<Post>>() {
80                         @Override
81                         public Collection<Post> answer(InvocationOnMock invocation) throws Throwable {
82                                 return directedPosts.get((String) invocation.getArguments()[0]);
83                         }
84                 });
85         }
86
87         private static Core mockCore(Database database) {
88                 Core core = mock(Core.class);
89                 when(core.getDatabase()).thenReturn(database);
90                 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
91                 return core;
92         }
93
94         private static Database mockDatabase() {
95                 Database database = mock(Database.class);
96                 when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
97                 when(database.getPost(anyString())).thenReturn(Optional.<Post>absent());
98                 when(database.getPostReply(anyString())).thenReturn(Optional.<PostReply>absent());
99                 return database;
100         }
101
102         public SoneMocker mockSone(String id) {
103                 return new SoneMocker(id);
104         }
105
106         public PostMocker mockPost(Sone sone, String postId) {
107                 return new PostMocker(postId, sone);
108         }
109
110         public PostReplyMocker mockPostReply(Sone sone, String replyId) {
111                 return new PostReplyMocker(replyId, sone);
112         }
113
114         public class SoneMocker {
115
116                 private final Sone mockedSone = mock(Sone.class);
117                 private final String id;
118                 private boolean local;
119                 private Optional<String> name = absent();
120                 private long time;
121                 private Profile profile = new Profile(mockedSone);
122                 private Collection<String> friends = emptySet();
123
124                 private SoneMocker(String id) {
125                         this.id = id;
126                 }
127
128                 public SoneMocker local() {
129                         local = true;
130                         return this;
131                 }
132
133                 public SoneMocker withName(String name) {
134                         this.name = fromNullable(name);
135                         return this;
136                 }
137
138                 public SoneMocker withTime(long time) {
139                         this.time = time;
140                         return this;
141                 }
142
143                 public SoneMocker withProfileName(String firstName, String middleName, String lastName) {
144                         profile.modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
145                         return this;
146                 }
147
148                 public SoneMocker addProfileField(String fieldName, String fieldValue) {
149                         profile.setField(profile.addField(fieldName), fieldValue);
150                         return this;
151                 }
152
153                 public SoneMocker withFriends(Collection<String> friends) {
154                         this.friends = friends;
155                         return this;
156                 }
157
158                 public Sone create() {
159                         when(mockedSone.getId()).thenReturn(id);
160                         when(mockedSone.isLocal()).thenReturn(local);
161                         if (name.isPresent()) {
162                                 when(mockedSone.getName()).thenReturn(name.get());
163                         }
164                         when(mockedSone.getTime()).thenReturn(time);
165                         when(mockedSone.getProfile()).thenReturn(profile);
166                         if (local) {
167                                 when(mockedSone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
168                                 when(mockedSone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
169                                         @Override
170                                         public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
171                                                 return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]);
172                                         }
173                                 });
174                                 when(mockedSone.hasFriend(anyString())).thenReturn(false);
175                                 when(mockedSone.getFriends()).thenReturn(friends);
176                                 when(mockedSone.hasFriend(anyString())).then(new Answer<Boolean>() {
177                                         @Override
178                                         public Boolean answer(InvocationOnMock invocation) throws Throwable {
179                                                 return friends.contains(invocation.getArguments()[0]);
180                                         }
181                                 });
182                         } else {
183                                 when(mockedSone.newPostBuilder()).thenThrow(IllegalStateException.class);
184                                 when(mockedSone.newPostReplyBuilder(anyString())).thenThrow(IllegalStateException.class);
185                         }
186                         when(core.getSone(eq(id))).thenReturn(of(mockedSone));
187                         when(database.getSone(eq(id))).thenReturn(of(mockedSone));
188                         when(mockedSone.getPosts()).then(new Answer<List<Post>>() {
189                                 @Override
190                                 public List<Post> answer(InvocationOnMock invocationOnMock) throws Throwable {
191                                         return from(Post.TIME_COMPARATOR).sortedCopy(sonePosts.get(mockedSone));
192                                 }
193                         });
194                         when(mockedSone.toString()).thenReturn(String.format("Sone[%s]", id));
195                         sones.put(id, mockedSone);
196                         return mockedSone;
197                 }
198
199         }
200
201         public class PostMocker {
202
203                 private final Post post = mock(Post.class);
204                 private final String id;
205                 private final Sone sone;
206                 private Optional<String> recipientId = absent();
207                 private long time;
208                 private Optional<String> text = absent();
209
210                 public PostMocker(String id, Sone sone) {
211                         this.id = id;
212                         this.sone = sone;
213                 }
214
215                 public PostMocker withRecipient(String recipientId) {
216                         this.recipientId = fromNullable(recipientId);
217                         return this;
218                 }
219
220                 public PostMocker withTime(long time) {
221                         this.time = time;
222                         return this;
223                 }
224
225                 public PostMocker withText(String text) {
226                         this.text = fromNullable(text);
227                         return this;
228                 }
229
230                 public Post create() {
231                         when(post.getId()).thenReturn(id);
232                         when(post.getSone()).thenReturn(sone);
233                         when(post.getRecipientId()).thenReturn(recipientId);
234                         if (recipientId.isPresent()) {
235                                 directedPosts.put(recipientId.get(), post);
236                         }
237                         when(post.getTime()).thenReturn(time);
238                         if (text.isPresent()) {
239                                 when(post.getText()).thenReturn(text.get());
240                         }
241                         when(database.getPost(eq(id))).thenReturn(of(post));
242                         sonePosts.put(sone, post);
243                         when(post.getReplies()).then(new Answer<List<PostReply>>() {
244                                 @Override
245                                 public List<PostReply> answer(InvocationOnMock invocation) throws Throwable {
246                                         return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(postReplies.get(post));
247                                 }
248                         });
249                         return post;
250                 }
251
252         }
253
254         public class PostReplyMocker {
255
256                 private final PostReply postReply = mock(PostReply.class);
257                 private final String id;
258                 private final Sone sone;
259                 private Optional<Post> post = absent();
260                 private long time;
261                 private Optional<String> text = absent();
262
263                 public PostReplyMocker(String id, Sone sone) {
264                         this.id = id;
265                         this.sone = sone;
266                 }
267
268                 public PostReplyMocker inReplyTo(Post post) {
269                         this.post = fromNullable(post);
270                         return this;
271                 }
272
273                 public PostReplyMocker withTime(long time) {
274                         this.time = time;
275                         return this;
276                 }
277
278                 public PostReplyMocker withText(String text) {
279                         this.text = fromNullable(text);
280                         return this;
281                 }
282
283                 public PostReply create() {
284                         when(postReply.getId()).thenReturn(id);
285                         when(postReply.getSone()).thenReturn(sone);
286                         when(postReply.getTime()).thenReturn(time);
287                         when(database.getPostReply(eq(id))).thenReturn(of(postReply));
288                         if (post.isPresent()) {
289                                 postReplies.put(post.get(), postReply);
290                         }
291                         if (text.isPresent()) {
292                                 when(postReply.getText()).thenReturn(text.get());
293                         }
294                         return postReply;
295                 }
296         }
297
298 }