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