d75f5f670c38519e56bec1c41467db1d88c00c24
[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 com.google.common.collect.ArrayListMultimap.create;
22 import static com.google.common.collect.Ordering.from;
23 import static net.pterodactylus.sone.data.Post.TIME_COMPARATOR;
24 import static org.mockito.Matchers.anyString;
25 import static org.mockito.Matchers.eq;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28
29 import java.util.List;
30
31 import net.pterodactylus.sone.core.Core;
32 import net.pterodactylus.sone.data.impl.DefaultPostBuilder;
33 import net.pterodactylus.sone.data.impl.DefaultPostReplyBuilder;
34 import net.pterodactylus.sone.database.Database;
35 import net.pterodactylus.sone.database.PostReplyBuilder;
36
37 import com.google.common.base.Optional;
38 import com.google.common.collect.Multimap;
39 import org.mockito.Matchers;
40 import org.mockito.invocation.InvocationOnMock;
41 import org.mockito.stubbing.Answer;
42
43 /**
44  * Mocks reusable in multiple tests.
45  *
46  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47  */
48 public class Mocks {
49
50         private final Multimap<Sone, Post> sonePosts = create();
51         public final Database database;
52         public final Core core;
53
54         public Mocks() {
55                 database = mockDatabase();
56                 core = mockCore(database);
57         }
58
59         private static Core mockCore(Database database) {
60                 Core core = mock(Core.class);
61                 when(core.getDatabase()).thenReturn(database);
62                 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
63                 return core;
64         }
65
66         private static Database mockDatabase() {
67                 Database database = mock(Database.class);
68                 when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
69                 when(database.getPost(anyString())).thenReturn(Optional.<Post>absent());
70                 when(database.getPostReply(anyString())).thenReturn(Optional.<PostReply>absent());
71                 return database;
72         }
73
74         public Sone mockLocalSone(final String id) {
75                 final Sone sone = mock(Sone.class);
76                 when(sone.getId()).thenReturn(id);
77                 when(sone.isLocal()).thenReturn(true);
78                 when(sone.getProfile()).thenReturn(new Profile(sone));
79                 when(sone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
80                 when(sone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
81                         @Override
82                         public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
83                                 return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]);
84                         }
85                 });
86                 when(core.getSone(eq(id))).thenReturn(of(sone));
87                 when(database.getSone(eq(id))).thenReturn(of(sone));
88                 when(sone.getPosts()).then(new Answer<List<Post>>() {
89                         @Override
90                         public List<Post> answer(InvocationOnMock invocationOnMock) throws Throwable {
91                                 return from(TIME_COMPARATOR).sortedCopy(sonePosts.get(sone));
92                         }
93                 });
94                 return sone;
95         }
96
97         public Sone mockRemoteSone(final String id) {
98                 final Sone sone = mock(Sone.class);
99                 when(sone.getId()).thenReturn(id);
100                 when(sone.isLocal()).thenReturn(false);
101                 when(sone.getProfile()).thenReturn(new Profile(sone));
102                 when(sone.newPostBuilder()).thenThrow(IllegalStateException.class);
103                 when(sone.newPostReplyBuilder(Matchers.<String>anyObject())).thenThrow(IllegalStateException.class);
104                 when(core.getSone(eq(id))).thenReturn(of(sone));
105                 when(database.getSone(eq(id))).thenReturn(of(sone));
106                 when(sone.getPosts()).then(new Answer<List<Post>>() {
107                         @Override
108                         public List<Post> answer(InvocationOnMock invocationOnMock) throws Throwable {
109                                 return from(TIME_COMPARATOR).sortedCopy(sonePosts.get(sone));
110                         }
111                 });
112                 return sone;
113         }
114
115         public Post mockPost(Sone sone, String postId) {
116                 Post post = mock(Post.class);
117                 when(post.getId()).thenReturn(postId);
118                 when(post.getSone()).thenReturn(sone);
119                 when(database.getPost(eq(postId))).thenReturn(of(post));
120                 sonePosts.put(sone, post);
121                 return post;
122         }
123
124         public PostReply mockPostReply(Sone sone, String replyId) {
125                 PostReply postReply = mock(PostReply.class);
126                 when(postReply.getId()).thenReturn(replyId);
127                 when(postReply.getSone()).thenReturn(sone);
128                 when(database.getPostReply(eq(replyId))).thenReturn(of(postReply));
129                 return postReply;
130         }
131
132 }