Add reusable mocks.
[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.database.Database;
29
30 import com.google.common.base.Optional;
31
32 /**
33  * Mocks reusable in multiple tests.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class Mocks {
38
39         public static Core mockCore(Database database) {
40                 Core core = mock(Core.class);
41                 when(core.getDatabase()).thenReturn(database);
42                 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
43                 return core;
44         }
45
46         public static Database mockDatabase() {
47                 Database database = mock(Database.class);
48                 when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
49                 return database;
50         }
51
52         public static Sone mockLocalSone(Core core, String id) {
53                 Sone sone = mock(Sone.class);
54                 when(sone.getId()).thenReturn(id);
55                 when(sone.isLocal()).thenReturn(true);
56                 Database database = core.getDatabase();
57                 when(sone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
58                 when(core.getSone(eq(id))).thenReturn(of(sone));
59                 when(database.getSone(eq(id))).thenReturn(of(sone));
60                 return sone;
61         }
62
63 }