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