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