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 org.mockito.Matchers.anyString;
22 import static org.mockito.Matchers.eq;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
26 import net.pterodactylus.sone.core.Core;
27 import net.pterodactylus.sone.data.impl.DefaultPostBuilder;
28 import net.pterodactylus.sone.data.impl.DefaultPostReplyBuilder;
29 import net.pterodactylus.sone.database.Database;
30 import net.pterodactylus.sone.database.PostReplyBuilder;
32 import com.google.common.base.Optional;
33 import org.mockito.Matchers;
34 import org.mockito.invocation.InvocationOnMock;
35 import org.mockito.stubbing.Answer;
38 * Mocks reusable in multiple tests.
40 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public static Core mockCore(Database database) {
45 Core core = mock(Core.class);
46 when(core.getDatabase()).thenReturn(database);
47 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
51 public static Database mockDatabase() {
52 Database database = mock(Database.class);
53 when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
54 when(database.getPost(anyString())).thenReturn(Optional.<Post>absent());
55 when(database.getPostReply(anyString())).thenReturn(Optional.<PostReply>absent());
59 public static Sone mockLocalSone(Core core, final String id) {
60 Sone sone = mockRemoteSone(core, id);
61 when(sone.isLocal()).thenReturn(true);
62 final Database database = core.getDatabase();
63 when(sone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
64 when(sone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
66 public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
67 return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]);
73 public static Sone mockRemoteSone(Core core, final String id) {
74 Sone sone = mock(Sone.class);
75 when(sone.getId()).thenReturn(id);
76 when(sone.isLocal()).thenReturn(false);
77 when(sone.getProfile()).thenReturn(new Profile(sone));
78 final Database database = core.getDatabase();
79 when(sone.newPostBuilder()).thenThrow(IllegalStateException.class);
80 when(sone.newPostReplyBuilder(Matchers.<String>anyObject())).thenThrow(IllegalStateException.class);
81 when(core.getSone(eq(id))).thenReturn(of(sone));
82 when(database.getSone(eq(id))).thenReturn(of(sone));
86 public static Post mockPost(Core core, Sone sone, String postId) {
87 Post post = mock(Post.class);
88 when(post.getId()).thenReturn(postId);
89 when(post.getSone()).thenReturn(sone);
90 Database database = core.getDatabase();
91 when(database.getPost(eq(postId))).thenReturn(of(post));
95 public static PostReply mockPostReply(Core core, Sone sone, String replyId) {
96 PostReply postReply = mock(PostReply.class);
97 when(postReply.getId()).thenReturn(replyId);
98 when(postReply.getSone()).thenReturn(sone);
99 Database database = core.getDatabase();
100 when(database.getPostReply(eq(replyId))).thenReturn(of(postReply));