Remove imports.
[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.Matchers.anyString;
22 import static org.mockito.Matchers.eq;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25
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;
31
32 import com.google.common.base.Optional;
33 import org.mockito.Matchers;
34 import org.mockito.invocation.InvocationOnMock;
35 import org.mockito.stubbing.Answer;
36
37 /**
38  * Mocks reusable in multiple tests.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public class Mocks {
43
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());
48                 return core;
49         }
50
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());
56                 return database;
57         }
58
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>() {
65                         @Override
66                         public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
67                                 return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]);
68                         }
69                 });
70                 return sone;
71         }
72
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));
83                 return sone;
84         }
85
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));
92                 return post;
93         }
94
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));
101                 return postReply;
102         }
103
104 }