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.Optional;
43 import com.google.common.collect.FluentIterable;
44 import com.google.common.collect.Multimap;
45 import com.google.common.collect.Ordering;
46 import org.mockito.invocation.InvocationOnMock;
47 import org.mockito.stubbing.Answer;
50 * Mocks reusable in multiple tests.
52 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
56 private final Multimap<Sone, Post> sonePosts = create();
57 private final Map<String, Sone> sones = newHashMap();
58 private final Multimap<Post, PostReply> postReplies = create();
59 private final Multimap<String, Post> directedPosts = create();
60 public final Database database;
61 public final Core core;
64 database = mockDatabase();
65 core = mockCore(database);
66 when(core.getLocalSones()).then(new Answer<Collection<Sone>>() {
68 public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
69 return FluentIterable.from(sones.values()).filter(Sone.LOCAL_SONE_FILTER).toList();
72 when(database.getDirectedPosts(anyString())).then(new Answer<Collection<Post>>() {
74 public Collection<Post> answer(InvocationOnMock invocation) throws Throwable {
75 return directedPosts.get((String) invocation.getArguments()[0]);
80 private static Core mockCore(Database database) {
81 Core core = mock(Core.class);
82 when(core.getDatabase()).thenReturn(database);
83 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
87 private static Database mockDatabase() {
88 Database database = mock(Database.class);
89 when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
90 when(database.getPost(anyString())).thenReturn(Optional.<Post>absent());
91 when(database.getPostReply(anyString())).thenReturn(Optional.<PostReply>absent());
95 public SoneMocker mockSone(String id) {
96 return new SoneMocker(id);
99 public PostMocker mockPost(Sone sone, String postId) {
100 return new PostMocker(postId, sone);
103 public PostReplyMocker mockPostReply(Sone sone, String replyId) {
104 return new PostReplyMocker(replyId, sone);
107 public class SoneMocker {
109 private final Sone mockedSone = mock(Sone.class);
110 private final String id;
111 private boolean local;
112 private Optional<String> name = absent();
114 private Profile profile = new Profile(mockedSone);
115 private Collection<String> friends = emptySet();
117 private SoneMocker(String id) {
121 public SoneMocker local() {
126 public SoneMocker withName(String name) {
127 this.name = fromNullable(name);
131 public SoneMocker withTime(long time) {
136 public SoneMocker withProfileName(String firstName, String middleName, String lastName) {
137 profile.modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
141 public SoneMocker addProfileField(String fieldName, String fieldValue) {
142 profile.setField(profile.addField(fieldName), fieldValue);
146 public SoneMocker withFriends(Collection<String> friends) {
147 this.friends = friends;
151 public Sone create() {
152 when(mockedSone.getId()).thenReturn(id);
153 when(mockedSone.isLocal()).thenReturn(local);
154 if (name.isPresent()) {
155 when(mockedSone.getName()).thenReturn(name.get());
157 when(mockedSone.getTime()).thenReturn(time);
158 when(mockedSone.getProfile()).thenReturn(profile);
160 when(mockedSone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
161 when(mockedSone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
163 public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
164 return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]);
167 when(mockedSone.hasFriend(anyString())).thenReturn(false);
168 when(mockedSone.getFriends()).thenReturn(friends);
169 when(mockedSone.hasFriend(anyString())).then(new Answer<Boolean>() {
171 public Boolean answer(InvocationOnMock invocation) throws Throwable {
172 return friends.contains(invocation.getArguments()[0]);
176 when(mockedSone.newPostBuilder()).thenThrow(IllegalStateException.class);
177 when(mockedSone.newPostReplyBuilder(anyString())).thenThrow(IllegalStateException.class);
179 when(core.getSone(eq(id))).thenReturn(of(mockedSone));
180 when(database.getSone(eq(id))).thenReturn(of(mockedSone));
181 when(mockedSone.getPosts()).then(new Answer<List<Post>>() {
183 public List<Post> answer(InvocationOnMock invocationOnMock) throws Throwable {
184 return from(Post.TIME_COMPARATOR).sortedCopy(sonePosts.get(mockedSone));
187 when(mockedSone.toString()).thenReturn(String.format("Sone[%s]", id));
188 sones.put(id, mockedSone);
194 public class PostMocker {
196 private final Post post = mock(Post.class);
197 private final String id;
198 private final Sone sone;
199 private Optional<String> recipientId = absent();
201 private Optional<String> text = absent();
203 public PostMocker(String id, Sone sone) {
208 public PostMocker withRecipient(String recipientId) {
209 this.recipientId = fromNullable(recipientId);
213 public PostMocker withTime(long time) {
218 public PostMocker withText(String text) {
219 this.text = fromNullable(text);
223 public Post create() {
224 when(post.getId()).thenReturn(id);
225 when(post.getSone()).thenReturn(sone);
226 when(post.getRecipientId()).thenReturn(recipientId);
227 if (recipientId.isPresent()) {
228 directedPosts.put(recipientId.get(), post);
230 when(post.getTime()).thenReturn(time);
231 if (text.isPresent()) {
232 when(post.getText()).thenReturn(text.get());
234 when(database.getPost(eq(id))).thenReturn(of(post));
235 sonePosts.put(sone, post);
236 when(post.getReplies()).then(new Answer<List<PostReply>>() {
238 public List<PostReply> answer(InvocationOnMock invocation) throws Throwable {
239 return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(postReplies.get(post));
247 public class PostReplyMocker {
249 private final PostReply postReply = mock(PostReply.class);
250 private final String id;
251 private final Sone sone;
252 private Optional<Post> post = absent();
254 private Optional<String> text = absent();
256 public PostReplyMocker(String id, Sone sone) {
261 public PostReplyMocker inReplyTo(Post post) {
262 this.post = fromNullable(post);
266 public PostReplyMocker withTime(long time) {
271 public PostReplyMocker withText(String text) {
272 this.text = fromNullable(text);
276 public PostReply create() {
277 when(postReply.getId()).thenReturn(id);
278 when(postReply.getSone()).thenReturn(sone);
279 when(postReply.getTime()).thenReturn(time);
280 when(database.getPostReply(eq(id))).thenReturn(of(postReply));
281 if (post.isPresent()) {
282 postReplies.put(post.get(), postReply);
284 if (text.isPresent()) {
285 when(postReply.getText()).thenReturn(text.get());