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.ArgumentCaptor.forClass;
22 import static org.mockito.Matchers.anyString;
23 import static org.mockito.Matchers.eq;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
27 import net.pterodactylus.sone.core.Core;
28 import net.pterodactylus.sone.data.impl.DefaultPostBuilder;
29 import net.pterodactylus.sone.data.impl.DefaultPostReplyBuilder;
30 import net.pterodactylus.sone.database.Database;
31 import net.pterodactylus.sone.database.PostReplyBuilder;
33 import com.google.common.base.Optional;
34 import org.mockito.ArgumentCaptor;
35 import org.mockito.invocation.InvocationOnMock;
36 import org.mockito.stubbing.Answer;
39 * Mocks reusable in multiple tests.
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 public static Core mockCore(Database database) {
46 Core core = mock(Core.class);
47 when(core.getDatabase()).thenReturn(database);
48 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
52 public static Database mockDatabase() {
53 Database database = mock(Database.class);
54 when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
55 when(database.getPost(anyString())).thenReturn(Optional.<Post>absent());
59 public static Sone mockLocalSone(Core core, final String id) {
60 Sone sone = mock(Sone.class);
61 when(sone.getId()).thenReturn(id);
62 when(sone.isLocal()).thenReturn(true);
63 final Database database = core.getDatabase();
64 when(sone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
65 final ArgumentCaptor<String> postIdCaptor = forClass(String.class);
66 when(sone.newPostReplyBuilder(postIdCaptor.capture())).then(new Answer<PostReplyBuilder>() {
68 public PostReplyBuilder answer(InvocationOnMock invocationOnMock) throws Throwable {
69 return new DefaultPostReplyBuilder(database, id, postIdCaptor.getValue());
72 when(core.getSone(eq(id))).thenReturn(of(sone));
73 when(database.getSone(eq(id))).thenReturn(of(sone));
77 public static Post mockPost(Core core, String postId) {
78 Post post = mock(Post.class);
79 when(post.getId()).thenReturn(postId);
80 Database database = core.getDatabase();
81 when(database.getPost(eq(postId))).thenReturn(of(post));