2 * Sone - Mocks.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.data;
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;
32 import java.util.Collection;
33 import java.util.List;
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;
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;
51 * Mocks reusable in multiple tests.
53 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
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;
65 database = mockDatabase();
66 core = mockCore(database);
67 when(database.getSone()).thenReturn(new Function<String, Optional<Sone>>() {
69 public Optional<Sone> apply(String soneId) {
70 return (soneId == null) ? Optional.<Sone>absent() : fromNullable(sones.get(soneId));
73 when(core.getSones()).then(new Answer<Collection<Sone>>() {
75 public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
76 return sones.values();
79 when(core.getLocalSones()).then(new Answer<Collection<Sone>>() {
81 public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
82 return FluentIterable.from(sones.values()).filter(Sone.LOCAL_SONE_FILTER).toList();
85 when(database.getDirectedPosts(anyString())).then(new Answer<Collection<Post>>() {
87 public Collection<Post> answer(InvocationOnMock invocation) throws Throwable {
88 return directedPosts.get((String) invocation.getArguments()[0]);
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());
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());
108 public SoneMocker mockSone(String id) {
109 return new SoneMocker(id);
112 public PostMocker mockPost(Sone sone, String postId) {
113 return new PostMocker(postId, sone);
116 public PostReplyMocker mockPostReply(Sone sone, String replyId) {
117 return new PostReplyMocker(replyId, sone);
120 public class SoneMocker {
122 private final Sone mockedSone = mock(Sone.class);
123 private final String id;
124 private boolean local;
125 private Optional<String> name = absent();
127 private Profile profile = new Profile(mockedSone);
128 private Collection<String> friends = emptySet();
130 private SoneMocker(String id) {
134 public SoneMocker local() {
139 public SoneMocker withName(String name) {
140 this.name = fromNullable(name);
144 public SoneMocker withTime(long time) {
149 public SoneMocker withProfileName(String firstName, String middleName, String lastName) {
150 profile.modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
154 public SoneMocker addProfileField(String fieldName, String fieldValue) {
155 profile.setField(profile.addField(fieldName), fieldValue);
159 public SoneMocker withFriends(Collection<String> friends) {
160 this.friends = friends;
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());
170 when(mockedSone.getTime()).thenReturn(time);
171 when(mockedSone.getProfile()).thenReturn(profile);
173 when(mockedSone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
174 when(mockedSone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
176 public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
177 return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]);
180 when(mockedSone.hasFriend(anyString())).thenReturn(false);
181 when(mockedSone.getFriends()).thenReturn(friends);
182 when(mockedSone.hasFriend(anyString())).then(new Answer<Boolean>() {
184 public Boolean answer(InvocationOnMock invocation) throws Throwable {
185 return friends.contains(invocation.getArguments()[0]);
189 when(mockedSone.newPostBuilder()).thenThrow(IllegalStateException.class);
190 when(mockedSone.newPostReplyBuilder(anyString())).thenThrow(IllegalStateException.class);
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>>() {
196 public List<Post> answer(InvocationOnMock invocationOnMock) throws Throwable {
197 return from(Post.TIME_COMPARATOR).sortedCopy(sonePosts.get(mockedSone));
200 when(mockedSone.toString()).thenReturn(String.format("Sone[%s]", id));
201 sones.put(id, mockedSone);
207 public class PostMocker {
209 private final Post post = mock(Post.class);
210 private final String id;
211 private final Sone sone;
212 private Optional<String> recipientId = absent();
214 private Optional<String> text = absent();
216 public PostMocker(String id, Sone sone) {
221 public PostMocker withRecipient(String recipientId) {
222 this.recipientId = fromNullable(recipientId);
226 public PostMocker withTime(long time) {
231 public PostMocker withText(String text) {
232 this.text = fromNullable(text);
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);
243 when(post.getTime()).thenReturn(time);
244 if (text.isPresent()) {
245 when(post.getText()).thenReturn(text.get());
247 when(database.getPost(eq(id))).thenReturn(of(post));
248 sonePosts.put(sone, post);
249 when(post.getReplies()).then(new Answer<List<PostReply>>() {
251 public List<PostReply> answer(InvocationOnMock invocation) throws Throwable {
252 return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(postReplies.get(post));
260 public class PostReplyMocker {
262 private final PostReply postReply = mock(PostReply.class);
263 private final String id;
264 private final Sone sone;
265 private Optional<Post> post = absent();
267 private Optional<String> text = absent();
269 public PostReplyMocker(String id, Sone sone) {
274 public PostReplyMocker inReplyTo(Post post) {
275 this.post = fromNullable(post);
279 public PostReplyMocker withTime(long time) {
284 public PostReplyMocker withText(String text) {
285 this.text = fromNullable(text);
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);
297 if (text.isPresent()) {
298 when(postReply.getText()).thenReturn(text.get());