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 org.mockito.Matchers.anyString;
27 import static org.mockito.Matchers.eq;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.when;
31 import java.util.Collection;
32 import java.util.List;
34 import net.pterodactylus.sone.core.Core;
35 import net.pterodactylus.sone.data.impl.DefaultPostBuilder;
36 import net.pterodactylus.sone.data.impl.DefaultPostReplyBuilder;
37 import net.pterodactylus.sone.database.Database;
38 import net.pterodactylus.sone.database.PostReplyBuilder;
40 import com.google.common.base.Optional;
41 import com.google.common.collect.FluentIterable;
42 import com.google.common.collect.Multimap;
43 import com.google.common.collect.Ordering;
44 import org.mockito.invocation.InvocationOnMock;
45 import org.mockito.stubbing.Answer;
48 * Mocks reusable in multiple tests.
50 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
54 private final Multimap<Sone, Post> sonePosts = create();
55 private final Collection<Sone> sones = newHashSet();
56 private final Multimap<Post, PostReply> postReplies = create();
57 public final Database database;
58 public final Core core;
61 database = mockDatabase();
62 core = mockCore(database);
63 when(core.getLocalSones()).then(new Answer<Collection<Sone>>() {
65 public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
66 return FluentIterable.from(sones).filter(Sone.LOCAL_SONE_FILTER).toList();
71 private static Core mockCore(Database database) {
72 Core core = mock(Core.class);
73 when(core.getDatabase()).thenReturn(database);
74 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
78 private static Database mockDatabase() {
79 Database database = mock(Database.class);
80 when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
81 when(database.getPost(anyString())).thenReturn(Optional.<Post>absent());
82 when(database.getPostReply(anyString())).thenReturn(Optional.<PostReply>absent());
86 public SoneMocker mockSone(String id) {
87 return new SoneMocker(id);
90 public PostMocker mockPost(Sone sone, String postId) {
91 return new PostMocker(postId, sone);
94 public PostReplyMocker mockPostReply(Sone sone, String replyId) {
95 return new PostReplyMocker(replyId, sone);
98 public class SoneMocker {
100 private final Sone mockedSone = mock(Sone.class);
101 private final String id;
102 private boolean local;
103 private Optional<String> name = absent();
105 private Profile profile = new Profile(mockedSone);
106 private Optional<String> friend = absent();
108 private SoneMocker(String id) {
112 public SoneMocker local() {
117 public SoneMocker withName(String name) {
118 this.name = fromNullable(name);
122 public SoneMocker withTime(long time) {
127 public SoneMocker withProfileName(String firstName, String middleName, String lastName) {
128 profile.modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
132 public SoneMocker addProfileField(String fieldName, String fieldValue) {
133 profile.setField(profile.addField(fieldName), fieldValue);
137 public SoneMocker withFriends(String friend) {
138 this.friend = fromNullable(friend);
142 public Sone create() {
143 when(mockedSone.getId()).thenReturn(id);
144 when(mockedSone.isLocal()).thenReturn(local);
145 if (name.isPresent()) {
146 when(mockedSone.getName()).thenReturn(name.get());
148 when(mockedSone.getTime()).thenReturn(time);
149 when(mockedSone.getProfile()).thenReturn(profile);
151 when(mockedSone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
152 when(mockedSone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
154 public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
155 return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]);
158 when(mockedSone.hasFriend(anyString())).thenReturn(false);
159 if (friend.isPresent()) {
160 when(mockedSone.hasFriend(friend.get())).thenReturn(true);
163 when(mockedSone.newPostBuilder()).thenThrow(IllegalStateException.class);
164 when(mockedSone.newPostReplyBuilder(anyString())).thenThrow(IllegalStateException.class);
166 when(core.getSone(eq(id))).thenReturn(of(mockedSone));
167 when(database.getSone(eq(id))).thenReturn(of(mockedSone));
168 when(mockedSone.getPosts()).then(new Answer<List<Post>>() {
170 public List<Post> answer(InvocationOnMock invocationOnMock) throws Throwable {
171 return from(Post.TIME_COMPARATOR).sortedCopy(sonePosts.get(mockedSone));
174 when(mockedSone.toString()).thenReturn(String.format("Sone[%s]", id));
175 sones.add(mockedSone);
181 public class PostMocker {
183 private final Post post = mock(Post.class);
184 private final String id;
185 private final Sone sone;
186 private Optional<String> recipientId = absent();
188 private Optional<String> text = absent();
190 public PostMocker(String id, Sone sone) {
195 public PostMocker withRecipient(String recipientId) {
196 this.recipientId = fromNullable(recipientId);
200 public PostMocker withTime(long time) {
205 public PostMocker withText(String text) {
206 this.text = fromNullable(text);
210 public Post create() {
211 when(post.getId()).thenReturn(id);
212 when(post.getSone()).thenReturn(sone);
213 when(post.getRecipientId()).thenReturn(recipientId);
214 when(post.getTime()).thenReturn(time);
215 if (text.isPresent()) {
216 when(post.getText()).thenReturn(text.get());
218 when(database.getPost(eq(id))).thenReturn(of(post));
219 sonePosts.put(sone, post);
220 when(post.getReplies()).then(new Answer<List<PostReply>>() {
222 public List<PostReply> answer(InvocationOnMock invocation) throws Throwable {
223 return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(postReplies.get(post));
231 public class PostReplyMocker {
233 private final PostReply postReply = mock(PostReply.class);
234 private final String id;
235 private final Sone sone;
236 private Optional<Post> post = absent();
238 private Optional<String> text = absent();
240 public PostReplyMocker(String id, Sone sone) {
245 public PostReplyMocker inReplyTo(Post post) {
246 this.post = fromNullable(post);
250 public PostReplyMocker withTime(long time) {
255 public PostReplyMocker withText(String text) {
256 this.text = fromNullable(text);
260 public PostReply create() {
261 when(postReply.getId()).thenReturn(id);
262 when(postReply.getSone()).thenReturn(sone);
263 when(postReply.getTime()).thenReturn(time);
264 when(database.getPostReply(eq(id))).thenReturn(of(postReply));
265 if (post.isPresent()) {
266 postReplies.put(post.get(), postReply);
268 if (text.isPresent()) {
269 when(postReply.getText()).thenReturn(text.get());