Mock a Sone’s friends in a better way.
[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.absent;
21 import static com.google.common.base.Optional.fromNullable;
22 import static com.google.common.base.Optional.of;
23 import static com.google.common.collect.ArrayListMultimap.create;
24 import static com.google.common.collect.Ordering.from;
25 import static com.google.common.collect.Sets.newHashSet;
26 import static java.util.Collections.emptySet;
27 import static org.mockito.Matchers.anyString;
28 import static org.mockito.Matchers.eq;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.when;
31
32 import java.util.Collection;
33 import java.util.List;
34
35 import net.pterodactylus.sone.core.Core;
36 import net.pterodactylus.sone.data.impl.DefaultPostBuilder;
37 import net.pterodactylus.sone.data.impl.DefaultPostReplyBuilder;
38 import net.pterodactylus.sone.database.Database;
39 import net.pterodactylus.sone.database.PostReplyBuilder;
40
41 import com.google.common.base.Optional;
42 import com.google.common.collect.FluentIterable;
43 import com.google.common.collect.Multimap;
44 import com.google.common.collect.Ordering;
45 import org.mockito.invocation.InvocationOnMock;
46 import org.mockito.stubbing.Answer;
47
48 /**
49  * Mocks reusable in multiple tests.
50  *
51  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
52  */
53 public class Mocks {
54
55         private final Multimap<Sone, Post> sonePosts = create();
56         private final Collection<Sone> sones = newHashSet();
57         private final Multimap<Post, PostReply> postReplies = create();
58         public final Database database;
59         public final Core core;
60
61         public Mocks() {
62                 database = mockDatabase();
63                 core = mockCore(database);
64                 when(core.getLocalSones()).then(new Answer<Collection<Sone>>() {
65                         @Override
66                         public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
67                                 return FluentIterable.from(sones).filter(Sone.LOCAL_SONE_FILTER).toList();
68                         }
69                 });
70         }
71
72         private static Core mockCore(Database database) {
73                 Core core = mock(Core.class);
74                 when(core.getDatabase()).thenReturn(database);
75                 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
76                 return core;
77         }
78
79         private static Database mockDatabase() {
80                 Database database = mock(Database.class);
81                 when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
82                 when(database.getPost(anyString())).thenReturn(Optional.<Post>absent());
83                 when(database.getPostReply(anyString())).thenReturn(Optional.<PostReply>absent());
84                 return database;
85         }
86
87         public SoneMocker mockSone(String id) {
88                 return new SoneMocker(id);
89         }
90
91         public PostMocker mockPost(Sone sone, String postId) {
92                 return new PostMocker(postId, sone);
93         }
94
95         public PostReplyMocker mockPostReply(Sone sone, String replyId) {
96                 return new PostReplyMocker(replyId, sone);
97         }
98
99         public class SoneMocker {
100
101                 private final Sone mockedSone = mock(Sone.class);
102                 private final String id;
103                 private boolean local;
104                 private Optional<String> name = absent();
105                 private long time;
106                 private Profile profile = new Profile(mockedSone);
107                 private Collection<String> friends = emptySet();
108
109                 private SoneMocker(String id) {
110                         this.id = id;
111                 }
112
113                 public SoneMocker local() {
114                         local = true;
115                         return this;
116                 }
117
118                 public SoneMocker withName(String name) {
119                         this.name = fromNullable(name);
120                         return this;
121                 }
122
123                 public SoneMocker withTime(long time) {
124                         this.time = time;
125                         return this;
126                 }
127
128                 public SoneMocker withProfileName(String firstName, String middleName, String lastName) {
129                         profile.modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
130                         return this;
131                 }
132
133                 public SoneMocker addProfileField(String fieldName, String fieldValue) {
134                         profile.setField(profile.addField(fieldName), fieldValue);
135                         return this;
136                 }
137
138                 public SoneMocker withFriends(Collection<String> friends) {
139                         this.friends = friends;
140                         return this;
141                 }
142
143                 public Sone create() {
144                         when(mockedSone.getId()).thenReturn(id);
145                         when(mockedSone.isLocal()).thenReturn(local);
146                         if (name.isPresent()) {
147                                 when(mockedSone.getName()).thenReturn(name.get());
148                         }
149                         when(mockedSone.getTime()).thenReturn(time);
150                         when(mockedSone.getProfile()).thenReturn(profile);
151                         if (local) {
152                                 when(mockedSone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
153                                 when(mockedSone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
154                                         @Override
155                                         public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
156                                                 return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]);
157                                         }
158                                 });
159                                 when(mockedSone.hasFriend(anyString())).thenReturn(false);
160                                 when(mockedSone.getFriends()).thenReturn(friends);
161                                 when(mockedSone.hasFriend(anyString())).then(new Answer<Boolean>() {
162                                         @Override
163                                         public Boolean answer(InvocationOnMock invocation) throws Throwable {
164                                                 return friends.contains(invocation.getArguments()[0]);
165                                         }
166                                 });
167                         } else {
168                                 when(mockedSone.newPostBuilder()).thenThrow(IllegalStateException.class);
169                                 when(mockedSone.newPostReplyBuilder(anyString())).thenThrow(IllegalStateException.class);
170                         }
171                         when(core.getSone(eq(id))).thenReturn(of(mockedSone));
172                         when(database.getSone(eq(id))).thenReturn(of(mockedSone));
173                         when(mockedSone.getPosts()).then(new Answer<List<Post>>() {
174                                 @Override
175                                 public List<Post> answer(InvocationOnMock invocationOnMock) throws Throwable {
176                                         return from(Post.TIME_COMPARATOR).sortedCopy(sonePosts.get(mockedSone));
177                                 }
178                         });
179                         when(mockedSone.toString()).thenReturn(String.format("Sone[%s]", id));
180                         sones.add(mockedSone);
181                         return mockedSone;
182                 }
183
184         }
185
186         public class PostMocker {
187
188                 private final Post post = mock(Post.class);
189                 private final String id;
190                 private final Sone sone;
191                 private Optional<String> recipientId = absent();
192                 private long time;
193                 private Optional<String> text = absent();
194
195                 public PostMocker(String id, Sone sone) {
196                         this.id = id;
197                         this.sone = sone;
198                 }
199
200                 public PostMocker withRecipient(String recipientId) {
201                         this.recipientId = fromNullable(recipientId);
202                         return this;
203                 }
204
205                 public PostMocker withTime(long time) {
206                         this.time = time;
207                         return this;
208                 }
209
210                 public PostMocker withText(String text) {
211                         this.text = fromNullable(text);
212                         return this;
213                 }
214
215                 public Post create() {
216                         when(post.getId()).thenReturn(id);
217                         when(post.getSone()).thenReturn(sone);
218                         when(post.getRecipientId()).thenReturn(recipientId);
219                         when(post.getTime()).thenReturn(time);
220                         if (text.isPresent()) {
221                                 when(post.getText()).thenReturn(text.get());
222                         }
223                         when(database.getPost(eq(id))).thenReturn(of(post));
224                         sonePosts.put(sone, post);
225                         when(post.getReplies()).then(new Answer<List<PostReply>>() {
226                                 @Override
227                                 public List<PostReply> answer(InvocationOnMock invocation) throws Throwable {
228                                         return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(postReplies.get(post));
229                                 }
230                         });
231                         return post;
232                 }
233
234         }
235
236         public class PostReplyMocker {
237
238                 private final PostReply postReply = mock(PostReply.class);
239                 private final String id;
240                 private final Sone sone;
241                 private Optional<Post> post = absent();
242                 private long time;
243                 private Optional<String> text = absent();
244
245                 public PostReplyMocker(String id, Sone sone) {
246                         this.id = id;
247                         this.sone = sone;
248                 }
249
250                 public PostReplyMocker inReplyTo(Post post) {
251                         this.post = fromNullable(post);
252                         return this;
253                 }
254
255                 public PostReplyMocker withTime(long time) {
256                         this.time = time;
257                         return this;
258                 }
259
260                 public PostReplyMocker withText(String text) {
261                         this.text = fromNullable(text);
262                         return this;
263                 }
264
265                 public PostReply create() {
266                         when(postReply.getId()).thenReturn(id);
267                         when(postReply.getSone()).thenReturn(sone);
268                         when(postReply.getTime()).thenReturn(time);
269                         when(database.getPostReply(eq(id))).thenReturn(of(postReply));
270                         if (post.isPresent()) {
271                                 postReplies.put(post.get(), postReply);
272                         }
273                         if (text.isPresent()) {
274                                 when(postReply.getText()).thenReturn(text.get());
275                         }
276                         return postReply;
277                 }
278         }
279
280 }