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.of;
21 import static com.google.common.collect.ArrayListMultimap.create;
22 import static com.google.common.collect.Ordering.from;
23 import static net.pterodactylus.sone.data.Post.TIME_COMPARATOR;
24 import static org.mockito.Matchers.anyString;
25 import static org.mockito.Matchers.eq;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
29 import java.util.List;
31 import net.pterodactylus.sone.core.Core;
32 import net.pterodactylus.sone.data.impl.DefaultPostBuilder;
33 import net.pterodactylus.sone.data.impl.DefaultPostReplyBuilder;
34 import net.pterodactylus.sone.database.Database;
35 import net.pterodactylus.sone.database.PostReplyBuilder;
37 import com.google.common.base.Optional;
38 import com.google.common.collect.Multimap;
39 import org.mockito.Matchers;
40 import org.mockito.invocation.InvocationOnMock;
41 import org.mockito.stubbing.Answer;
44 * Mocks reusable in multiple tests.
46 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
50 private static final Multimap<Sone, Post> sonePosts = create();
52 public static Core mockCore(Database database) {
53 Core core = mock(Core.class);
54 when(core.getDatabase()).thenReturn(database);
55 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
59 public static Database mockDatabase() {
60 Database database = mock(Database.class);
61 when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
62 when(database.getPost(anyString())).thenReturn(Optional.<Post>absent());
63 when(database.getPostReply(anyString())).thenReturn(Optional.<PostReply>absent());
67 public static Sone mockLocalSone(Core core, final String id) {
68 Sone sone = mockRemoteSone(core, id);
69 when(sone.isLocal()).thenReturn(true);
70 final Database database = core.getDatabase();
71 when(sone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
72 when(sone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
74 public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
75 return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]);
81 public static Sone mockRemoteSone(Core core, final String id) {
82 final Sone sone = mock(Sone.class);
83 when(sone.getId()).thenReturn(id);
84 when(sone.isLocal()).thenReturn(false);
85 when(sone.getProfile()).thenReturn(new Profile(sone));
86 final Database database = core.getDatabase();
87 when(sone.newPostBuilder()).thenThrow(IllegalStateException.class);
88 when(sone.newPostReplyBuilder(Matchers.<String>anyObject())).thenThrow(IllegalStateException.class);
89 when(core.getSone(eq(id))).thenReturn(of(sone));
90 when(database.getSone(eq(id))).thenReturn(of(sone));
91 when(sone.getPosts()).then(new Answer<List<Post>>() {
93 public List<Post> answer(InvocationOnMock invocationOnMock) throws Throwable {
94 return from(TIME_COMPARATOR).sortedCopy(sonePosts.get(sone));
100 public static Post mockPost(Core core, Sone sone, String postId) {
101 Post post = mock(Post.class);
102 when(post.getId()).thenReturn(postId);
103 when(post.getSone()).thenReturn(sone);
104 Database database = core.getDatabase();
105 when(database.getPost(eq(postId))).thenReturn(of(post));
106 sonePosts.put(sone, post);
110 public static PostReply mockPostReply(Core core, Sone sone, String replyId) {
111 PostReply postReply = mock(PostReply.class);
112 when(postReply.getId()).thenReturn(replyId);
113 when(postReply.getSone()).thenReturn(sone);
114 Database database = core.getDatabase();
115 when(database.getPostReply(eq(replyId))).thenReturn(of(postReply));