Don’t use an argument captor when using an answer.
[Sone.git] / src / test / java / net / pterodactylus / sone / data / Mocks.java
1 /*
2  * Sone - Mocks.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.data;
19
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;
26
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;
32
33 import com.google.common.base.Optional;
34 import org.mockito.ArgumentCaptor;
35 import org.mockito.Matchers;
36 import org.mockito.invocation.InvocationOnMock;
37 import org.mockito.stubbing.Answer;
38
39 /**
40  * Mocks reusable in multiple tests.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class Mocks {
45
46         public static Core mockCore(Database database) {
47                 Core core = mock(Core.class);
48                 when(core.getDatabase()).thenReturn(database);
49                 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
50                 return core;
51         }
52
53         public static Database mockDatabase() {
54                 Database database = mock(Database.class);
55                 when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
56                 when(database.getPost(anyString())).thenReturn(Optional.<Post>absent());
57                 when(database.getPostReply(anyString())).thenReturn(Optional.<PostReply>absent());
58                 return database;
59         }
60
61         public static Sone mockLocalSone(Core core, final String id) {
62                 Sone sone = mockRemoteSone(core, id);
63                 when(sone.isLocal()).thenReturn(true);
64                 final Database database = core.getDatabase();
65                 when(sone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
66                 when(sone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
67                         @Override
68                         public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
69                                 return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]);
70                         }
71                 });
72                 return sone;
73         }
74
75         public static Sone mockRemoteSone(Core core, final String id) {
76                 Sone sone = mock(Sone.class);
77                 when(sone.getId()).thenReturn(id);
78                 when(sone.isLocal()).thenReturn(false);
79                 when(sone.getProfile()).thenReturn(new Profile(sone));
80                 final Database database = core.getDatabase();
81                 when(sone.newPostBuilder()).thenThrow(IllegalStateException.class);
82                 when(sone.newPostReplyBuilder(Matchers.<String>anyObject())).thenThrow(IllegalStateException.class);
83                 when(core.getSone(eq(id))).thenReturn(of(sone));
84                 when(database.getSone(eq(id))).thenReturn(of(sone));
85                 return sone;
86         }
87
88         public static Post mockPost(Core core, Sone sone, String postId) {
89                 Post post = mock(Post.class);
90                 when(post.getId()).thenReturn(postId);
91                 when(post.getSone()).thenReturn(sone);
92                 Database database = core.getDatabase();
93                 when(database.getPost(eq(postId))).thenReturn(of(post));
94                 return post;
95         }
96
97         public static PostReply mockPostReply(Core core, Sone sone, String replyId) {
98                 PostReply postReply = mock(PostReply.class);
99                 when(postReply.getId()).thenReturn(replyId);
100                 when(postReply.getSone()).thenReturn(sone);
101                 Database database = core.getDatabase();
102                 when(database.getPostReply(eq(replyId))).thenReturn(of(postReply));
103                 return postReply;
104         }
105
106 }