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 public final Database database;
59 public final Core core;
62 database = mockDatabase();
63 core = mockCore(database);
64 when(core.getLocalSones()).then(new Answer<Collection<Sone>>() {
66 public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
67 return FluentIterable.from(sones).filter(Sone.LOCAL_SONE_FILTER).toList();
72 private static Core mockCore(Database database) {
73 Core core = mock(Core.class);
74 when(core.getDatabase()).thenReturn(database);
75 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
79 private static Database mockDatabase() {
80 Database database = mock(Database.class);
81 when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
82 when(database.getPost(anyString())).thenReturn(Optional.<Post>absent());
83 when(database.getPostReply(anyString())).thenReturn(Optional.<PostReply>absent());
87 public SoneMocker mockSone(String id) {
88 return new SoneMocker(id);
91 public PostMocker mockPost(Sone sone, String postId) {
92 return new PostMocker(postId, sone);
95 public PostReplyMocker mockPostReply(Sone sone, String replyId) {
96 return new PostReplyMocker(replyId, sone);
99 public class SoneMocker {
101 private final Sone mockedSone = mock(Sone.class);
102 private final String id;
103 private boolean local;
104 private Optional<String> name = absent();
106 private Profile profile = new Profile(mockedSone);
107 private Collection<String> friends = emptySet();
109 private SoneMocker(String id) {
113 public SoneMocker local() {
118 public SoneMocker withName(String name) {
119 this.name = fromNullable(name);
123 public SoneMocker withTime(long time) {
128 public SoneMocker withProfileName(String firstName, String middleName, String lastName) {
129 profile.modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
133 public SoneMocker addProfileField(String fieldName, String fieldValue) {
134 profile.setField(profile.addField(fieldName), fieldValue);
138 public SoneMocker withFriends(Collection<String> friends) {
139 this.friends = friends;
143 public Sone create() {
144 when(mockedSone.getId()).thenReturn(id);
145 when(mockedSone.isLocal()).thenReturn(local);
146 if (name.isPresent()) {
147 when(mockedSone.getName()).thenReturn(name.get());
149 when(mockedSone.getTime()).thenReturn(time);
150 when(mockedSone.getProfile()).thenReturn(profile);
152 when(mockedSone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
153 when(mockedSone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
155 public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
156 return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]);
159 when(mockedSone.hasFriend(anyString())).thenReturn(false);
160 when(mockedSone.getFriends()).thenReturn(friends);
161 when(mockedSone.hasFriend(anyString())).then(new Answer<Boolean>() {
163 public Boolean answer(InvocationOnMock invocation) throws Throwable {
164 return friends.contains(invocation.getArguments()[0]);
168 when(mockedSone.newPostBuilder()).thenThrow(IllegalStateException.class);
169 when(mockedSone.newPostReplyBuilder(anyString())).thenThrow(IllegalStateException.class);
171 when(core.getSone(eq(id))).thenReturn(of(mockedSone));
172 when(database.getSone(eq(id))).thenReturn(of(mockedSone));
173 when(mockedSone.getPosts()).then(new Answer<List<Post>>() {
175 public List<Post> answer(InvocationOnMock invocationOnMock) throws Throwable {
176 return from(Post.TIME_COMPARATOR).sortedCopy(sonePosts.get(mockedSone));
179 when(mockedSone.toString()).thenReturn(String.format("Sone[%s]", id));
180 sones.add(mockedSone);
186 public class PostMocker {
188 private final Post post = mock(Post.class);
189 private final String id;
190 private final Sone sone;
191 private Optional<String> recipientId = absent();
193 private Optional<String> text = absent();
195 public PostMocker(String id, Sone sone) {
200 public PostMocker withRecipient(String recipientId) {
201 this.recipientId = fromNullable(recipientId);
205 public PostMocker withTime(long time) {
210 public PostMocker withText(String text) {
211 this.text = fromNullable(text);
215 public Post create() {
216 when(post.getId()).thenReturn(id);
217 when(post.getSone()).thenReturn(sone);
218 when(post.getRecipientId()).thenReturn(recipientId);
219 when(post.getTime()).thenReturn(time);
220 if (text.isPresent()) {
221 when(post.getText()).thenReturn(text.get());
223 when(database.getPost(eq(id))).thenReturn(of(post));
224 sonePosts.put(sone, post);
225 when(post.getReplies()).then(new Answer<List<PostReply>>() {
227 public List<PostReply> answer(InvocationOnMock invocation) throws Throwable {
228 return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(postReplies.get(post));
236 public class PostReplyMocker {
238 private final PostReply postReply = mock(PostReply.class);
239 private final String id;
240 private final Sone sone;
241 private Optional<Post> post = absent();
243 private Optional<String> text = absent();
245 public PostReplyMocker(String id, Sone sone) {
250 public PostReplyMocker inReplyTo(Post post) {
251 this.post = fromNullable(post);
255 public PostReplyMocker withTime(long time) {
260 public PostReplyMocker withText(String text) {
261 this.text = fromNullable(text);
265 public PostReply create() {
266 when(postReply.getId()).thenReturn(id);
267 when(postReply.getSone()).thenReturn(sone);
268 when(postReply.getTime()).thenReturn(time);
269 when(database.getPostReply(eq(id))).thenReturn(of(postReply));
270 if (post.isPresent()) {
271 postReplies.put(post.get(), postReply);
273 if (text.isPresent()) {
274 when(postReply.getText()).thenReturn(text.get());