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 com.google.common.collect.Sets.newHashSet;
24 import static net.pterodactylus.sone.data.Post.TIME_COMPARATOR;
25 import static org.mockito.Matchers.anyString;
26 import static org.mockito.Matchers.eq;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
30 import java.util.Collection;
31 import java.util.List;
33 import net.pterodactylus.sone.core.Core;
34 import net.pterodactylus.sone.data.impl.DefaultPostBuilder;
35 import net.pterodactylus.sone.data.impl.DefaultPostReplyBuilder;
36 import net.pterodactylus.sone.database.Database;
37 import net.pterodactylus.sone.database.PostReplyBuilder;
39 import com.google.common.base.Optional;
40 import com.google.common.collect.FluentIterable;
41 import com.google.common.collect.Multimap;
42 import org.mockito.Matchers;
43 import org.mockito.invocation.InvocationOnMock;
44 import org.mockito.stubbing.Answer;
47 * Mocks reusable in multiple tests.
49 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
53 private final Multimap<Sone, Post> sonePosts = create();
54 private final Collection<Sone> sones = newHashSet();
55 public final Database database;
56 public final Core core;
59 database = mockDatabase();
60 core = mockCore(database);
61 when(core.getLocalSones()).then(new Answer<Collection<Sone>>() {
63 public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
64 return FluentIterable.from(sones).filter(Sone.LOCAL_SONE_FILTER).toList();
69 private static Core mockCore(Database database) {
70 Core core = mock(Core.class);
71 when(core.getDatabase()).thenReturn(database);
72 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
76 private static Database mockDatabase() {
77 Database database = mock(Database.class);
78 when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
79 when(database.getPost(anyString())).thenReturn(Optional.<Post>absent());
80 when(database.getPostReply(anyString())).thenReturn(Optional.<PostReply>absent());
84 public Sone mockLocalSone(final String id) {
85 final Sone sone = mock(Sone.class);
86 when(sone.isLocal()).thenReturn(true);
87 initializeSoneMock(id, sone);
88 when(sone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
89 when(sone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
91 public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
92 return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]);
98 public Sone mockRemoteSone(final String id) {
99 final Sone sone = mock(Sone.class);
100 when(sone.isLocal()).thenReturn(false);
101 initializeSoneMock(id, sone);
102 when(sone.newPostBuilder()).thenThrow(IllegalStateException.class);
103 when(sone.newPostReplyBuilder(Matchers.<String>anyObject())).thenThrow(IllegalStateException.class);
107 private void initializeSoneMock(String id, final Sone sone) {
108 when(sone.getId()).thenReturn(id);
109 when(sone.getProfile()).thenReturn(new Profile(sone));
110 when(core.getSone(eq(id))).thenReturn(of(sone));
111 when(database.getSone(eq(id))).thenReturn(of(sone));
112 when(sone.getPosts()).then(new Answer<List<Post>>() {
114 public List<Post> answer(InvocationOnMock invocationOnMock) throws Throwable {
115 return from(TIME_COMPARATOR).sortedCopy(sonePosts.get(sone));
118 when(sone.toString()).thenReturn(String.format("Sone[%s]", id));
122 public Post mockPost(Sone sone, String postId) {
123 Post post = mock(Post.class);
124 when(post.getId()).thenReturn(postId);
125 when(post.getSone()).thenReturn(sone);
126 when(database.getPost(eq(postId))).thenReturn(of(post));
127 sonePosts.put(sone, post);
131 public PostReply mockPostReply(Sone sone, String replyId) {
132 PostReply postReply = mock(PostReply.class);
133 when(postReply.getId()).thenReturn(replyId);
134 when(postReply.getSone()).thenReturn(sone);
135 when(database.getPostReply(eq(replyId))).thenReturn(of(postReply));