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.Ordering.from;
25 import static com.google.common.collect.Sets.newHashSet;
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;
35 import net.pterodactylus.sone.core.Core;
36 import net.pterodactylus.sone.data.impl.DefaultPostBuilder;
37 import net.pterodactylus.sone.data.impl.DefaultPostReplyBuilder;
38 import net.pterodactylus.sone.database.Database;
39 import net.pterodactylus.sone.database.PostReplyBuilder;
41 import com.google.common.base.Optional;
42 import com.google.common.collect.FluentIterable;
43 import com.google.common.collect.Multimap;
44 import com.google.common.collect.Ordering;
45 import org.mockito.invocation.InvocationOnMock;
46 import org.mockito.stubbing.Answer;
49 * Mocks reusable in multiple tests.
51 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
55 private final Multimap<Sone, Post> sonePosts = create();
56 private final Collection<Sone> sones = newHashSet();
57 private final Multimap<Post, PostReply> postReplies = create();
58 private final Multimap<String, Post> directedPosts = create();
59 public final Database database;
60 public final Core core;
63 database = mockDatabase();
64 core = mockCore(database);
65 when(core.getLocalSones()).then(new Answer<Collection<Sone>>() {
67 public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
68 return FluentIterable.from(sones).filter(Sone.LOCAL_SONE_FILTER).toList();
71 when(database.getDirectedPosts(anyString())).then(new Answer<Collection<Post>>() {
73 public Collection<Post> answer(InvocationOnMock invocation) throws Throwable {
74 return directedPosts.get((String) invocation.getArguments()[0]);
79 private static Core mockCore(Database database) {
80 Core core = mock(Core.class);
81 when(core.getDatabase()).thenReturn(database);
82 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
86 private static Database mockDatabase() {
87 Database database = mock(Database.class);
88 when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
89 when(database.getPost(anyString())).thenReturn(Optional.<Post>absent());
90 when(database.getPostReply(anyString())).thenReturn(Optional.<PostReply>absent());
94 public SoneMocker mockSone(String id) {
95 return new SoneMocker(id);
98 public PostMocker mockPost(Sone sone, String postId) {
99 return new PostMocker(postId, sone);
102 public PostReplyMocker mockPostReply(Sone sone, String replyId) {
103 return new PostReplyMocker(replyId, sone);
106 public class SoneMocker {
108 private final Sone mockedSone = mock(Sone.class);
109 private final String id;
110 private boolean local;
111 private Optional<String> name = absent();
113 private Profile profile = new Profile(mockedSone);
114 private Collection<String> friends = emptySet();
116 private SoneMocker(String id) {
120 public SoneMocker local() {
125 public SoneMocker withName(String name) {
126 this.name = fromNullable(name);
130 public SoneMocker withTime(long time) {
135 public SoneMocker withProfileName(String firstName, String middleName, String lastName) {
136 profile.modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
140 public SoneMocker addProfileField(String fieldName, String fieldValue) {
141 profile.setField(profile.addField(fieldName), fieldValue);
145 public SoneMocker withFriends(Collection<String> friends) {
146 this.friends = friends;
150 public Sone create() {
151 when(mockedSone.getId()).thenReturn(id);
152 when(mockedSone.isLocal()).thenReturn(local);
153 if (name.isPresent()) {
154 when(mockedSone.getName()).thenReturn(name.get());
156 when(mockedSone.getTime()).thenReturn(time);
157 when(mockedSone.getProfile()).thenReturn(profile);
159 when(mockedSone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
160 when(mockedSone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
162 public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
163 return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]);
166 when(mockedSone.hasFriend(anyString())).thenReturn(false);
167 when(mockedSone.getFriends()).thenReturn(friends);
168 when(mockedSone.hasFriend(anyString())).then(new Answer<Boolean>() {
170 public Boolean answer(InvocationOnMock invocation) throws Throwable {
171 return friends.contains(invocation.getArguments()[0]);
175 when(mockedSone.newPostBuilder()).thenThrow(IllegalStateException.class);
176 when(mockedSone.newPostReplyBuilder(anyString())).thenThrow(IllegalStateException.class);
178 when(core.getSone(eq(id))).thenReturn(of(mockedSone));
179 when(database.getSone(eq(id))).thenReturn(of(mockedSone));
180 when(mockedSone.getPosts()).then(new Answer<List<Post>>() {
182 public List<Post> answer(InvocationOnMock invocationOnMock) throws Throwable {
183 return from(Post.TIME_COMPARATOR).sortedCopy(sonePosts.get(mockedSone));
186 when(mockedSone.toString()).thenReturn(String.format("Sone[%s]", id));
187 sones.add(mockedSone);
193 public class PostMocker {
195 private final Post post = mock(Post.class);
196 private final String id;
197 private final Sone sone;
198 private Optional<String> recipientId = absent();
200 private Optional<String> text = absent();
202 public PostMocker(String id, Sone sone) {
207 public PostMocker withRecipient(String recipientId) {
208 this.recipientId = fromNullable(recipientId);
212 public PostMocker withTime(long time) {
217 public PostMocker withText(String text) {
218 this.text = fromNullable(text);
222 public Post create() {
223 when(post.getId()).thenReturn(id);
224 when(post.getSone()).thenReturn(sone);
225 when(post.getRecipientId()).thenReturn(recipientId);
226 if (recipientId.isPresent()) {
227 directedPosts.put(recipientId.get(), post);
229 when(post.getTime()).thenReturn(time);
230 if (text.isPresent()) {
231 when(post.getText()).thenReturn(text.get());
233 when(database.getPost(eq(id))).thenReturn(of(post));
234 sonePosts.put(sone, post);
235 when(post.getReplies()).then(new Answer<List<PostReply>>() {
237 public List<PostReply> answer(InvocationOnMock invocation) throws Throwable {
238 return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(postReplies.get(post));
246 public class PostReplyMocker {
248 private final PostReply postReply = mock(PostReply.class);
249 private final String id;
250 private final Sone sone;
251 private Optional<Post> post = absent();
253 private Optional<String> text = absent();
255 public PostReplyMocker(String id, Sone sone) {
260 public PostReplyMocker inReplyTo(Post post) {
261 this.post = fromNullable(post);
265 public PostReplyMocker withTime(long time) {
270 public PostReplyMocker withText(String text) {
271 this.text = fromNullable(text);
275 public PostReply create() {
276 when(postReply.getId()).thenReturn(id);
277 when(postReply.getSone()).thenReturn(sone);
278 when(postReply.getTime()).thenReturn(time);
279 when(database.getPostReply(eq(id))).thenReturn(of(postReply));
280 if (post.isPresent()) {
281 postReplies.put(post.get(), postReply);
283 if (text.isPresent()) {
284 when(postReply.getText()).thenReturn(text.get());