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.getLocalSones()).then(new Answer<Collection<Sone>>() {
75 public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
76 return FluentIterable.from(sones.values()).filter(Sone.LOCAL_SONE_FILTER).toList();
79 when(database.getDirectedPosts(anyString())).then(new Answer<Collection<Post>>() {
81 public Collection<Post> answer(InvocationOnMock invocation) throws Throwable {
82 return directedPosts.get((String) invocation.getArguments()[0]);
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());
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());
102 public SoneMocker mockSone(String id) {
103 return new SoneMocker(id);
106 public PostMocker mockPost(Sone sone, String postId) {
107 return new PostMocker(postId, sone);
110 public PostReplyMocker mockPostReply(Sone sone, String replyId) {
111 return new PostReplyMocker(replyId, sone);
114 public class SoneMocker {
116 private final Sone mockedSone = mock(Sone.class);
117 private final String id;
118 private boolean local;
119 private Optional<String> name = absent();
121 private Profile profile = new Profile(mockedSone);
122 private Collection<String> friends = emptySet();
124 private SoneMocker(String id) {
128 public SoneMocker local() {
133 public SoneMocker withName(String name) {
134 this.name = fromNullable(name);
138 public SoneMocker withTime(long time) {
143 public SoneMocker withProfileName(String firstName, String middleName, String lastName) {
144 profile.modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
148 public SoneMocker addProfileField(String fieldName, String fieldValue) {
149 profile.setField(profile.addField(fieldName), fieldValue);
153 public SoneMocker withFriends(Collection<String> friends) {
154 this.friends = friends;
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());
164 when(mockedSone.getTime()).thenReturn(time);
165 when(mockedSone.getProfile()).thenReturn(profile);
167 when(mockedSone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
168 when(mockedSone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
170 public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
171 return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]);
174 when(mockedSone.hasFriend(anyString())).thenReturn(false);
175 when(mockedSone.getFriends()).thenReturn(friends);
176 when(mockedSone.hasFriend(anyString())).then(new Answer<Boolean>() {
178 public Boolean answer(InvocationOnMock invocation) throws Throwable {
179 return friends.contains(invocation.getArguments()[0]);
183 when(mockedSone.newPostBuilder()).thenThrow(IllegalStateException.class);
184 when(mockedSone.newPostReplyBuilder(anyString())).thenThrow(IllegalStateException.class);
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>>() {
190 public List<Post> answer(InvocationOnMock invocationOnMock) throws Throwable {
191 return from(Post.TIME_COMPARATOR).sortedCopy(sonePosts.get(mockedSone));
194 when(mockedSone.toString()).thenReturn(String.format("Sone[%s]", id));
195 sones.put(id, mockedSone);
201 public class PostMocker {
203 private final Post post = mock(Post.class);
204 private final String id;
205 private final Sone sone;
206 private Optional<String> recipientId = absent();
208 private Optional<String> text = absent();
210 public PostMocker(String id, Sone sone) {
215 public PostMocker withRecipient(String recipientId) {
216 this.recipientId = fromNullable(recipientId);
220 public PostMocker withTime(long time) {
225 public PostMocker withText(String text) {
226 this.text = fromNullable(text);
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);
237 when(post.getTime()).thenReturn(time);
238 if (text.isPresent()) {
239 when(post.getText()).thenReturn(text.get());
241 when(database.getPost(eq(id))).thenReturn(of(post));
242 sonePosts.put(sone, post);
243 when(post.getReplies()).then(new Answer<List<PostReply>>() {
245 public List<PostReply> answer(InvocationOnMock invocation) throws Throwable {
246 return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(postReplies.get(post));
254 public class PostReplyMocker {
256 private final PostReply postReply = mock(PostReply.class);
257 private final String id;
258 private final Sone sone;
259 private Optional<Post> post = absent();
261 private Optional<String> text = absent();
263 public PostReplyMocker(String id, Sone sone) {
268 public PostReplyMocker inReplyTo(Post post) {
269 this.post = fromNullable(post);
273 public PostReplyMocker withTime(long time) {
278 public PostReplyMocker withText(String text) {
279 this.text = fromNullable(text);
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);
291 if (text.isPresent()) {
292 when(postReply.getText()).thenReturn(text.get());