Use a builder-style mocker for post replies, too.
[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.of;
22 import static com.google.common.collect.ArrayListMultimap.create;
23 import static com.google.common.collect.Ordering.from;
24 import static com.google.common.collect.Sets.newHashSet;
25 import static org.mockito.Matchers.anyString;
26 import static org.mockito.Matchers.eq;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29
30 import java.util.Collection;
31 import java.util.List;
32
33 import net.pterodactylus.sone.core.Core;
34 import net.pterodactylus.sone.data.impl.DefaultPostBuilder;
35 import net.pterodactylus.sone.data.impl.DefaultPostReplyBuilder;
36 import net.pterodactylus.sone.database.Database;
37 import net.pterodactylus.sone.database.PostReplyBuilder;
38
39 import com.google.common.base.Optional;
40 import com.google.common.collect.FluentIterable;
41 import com.google.common.collect.Multimap;
42 import com.google.common.collect.Ordering;
43 import org.mockito.invocation.InvocationOnMock;
44 import org.mockito.stubbing.Answer;
45
46 /**
47  * Mocks reusable in multiple tests.
48  *
49  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
50  */
51 public class Mocks {
52
53         private final Multimap<Sone, Post> sonePosts = create();
54         private final Collection<Sone> sones = newHashSet();
55         private final Multimap<Post, PostReply> postReplies = create();
56         public final Database database;
57         public final Core core;
58
59         public Mocks() {
60                 database = mockDatabase();
61                 core = mockCore(database);
62                 when(core.getLocalSones()).then(new Answer<Collection<Sone>>() {
63                         @Override
64                         public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
65                                 return FluentIterable.from(sones).filter(Sone.LOCAL_SONE_FILTER).toList();
66                         }
67                 });
68         }
69
70         private static Core mockCore(Database database) {
71                 Core core = mock(Core.class);
72                 when(core.getDatabase()).thenReturn(database);
73                 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
74                 return core;
75         }
76
77         private static Database mockDatabase() {
78                 Database database = mock(Database.class);
79                 when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
80                 when(database.getPost(anyString())).thenReturn(Optional.<Post>absent());
81                 when(database.getPostReply(anyString())).thenReturn(Optional.<PostReply>absent());
82                 return database;
83         }
84
85         public SoneMocker mockSone(String id) {
86                 return new SoneMocker(id);
87         }
88
89         public PostMocker mockPost(Sone sone, String postId) {
90                 return new PostMocker(postId, sone);
91         }
92
93         public PostReplyMocker mockPostReply(Sone sone, String replyId) {
94                 return new PostReplyMocker(replyId, sone);
95         }
96
97         public class SoneMocker {
98
99                 private final Sone mockedSone = mock(Sone.class);
100                 private final String id;
101                 private boolean local;
102                 private Profile profile = new Profile(mockedSone);
103
104                 private SoneMocker(String id) {
105                         this.id = id;
106                 }
107
108                 public SoneMocker local() {
109                         local = true;
110                         return this;
111                 }
112
113                 public Sone create() {
114                         when(mockedSone.getId()).thenReturn(id);
115                         when(mockedSone.isLocal()).thenReturn(local);
116                         when(mockedSone.getProfile()).thenReturn(profile);
117                         if (local) {
118                                 when(mockedSone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
119                                 when(mockedSone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
120                                         @Override
121                                         public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
122                                                 return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]);
123                                         }
124                                 });
125                         } else {
126                                 when(mockedSone.newPostBuilder()).thenThrow(IllegalStateException.class);
127                                 when(mockedSone.newPostReplyBuilder(anyString())).thenThrow(IllegalStateException.class);
128                         }
129                         when(core.getSone(eq(id))).thenReturn(of(mockedSone));
130                         when(database.getSone(eq(id))).thenReturn(of(mockedSone));
131                         when(mockedSone.getPosts()).then(new Answer<List<Post>>() {
132                                 @Override
133                                 public List<Post> answer(InvocationOnMock invocationOnMock) throws Throwable {
134                                         return from(Post.TIME_COMPARATOR).sortedCopy(sonePosts.get(mockedSone));
135                                 }
136                         });
137                         when(mockedSone.toString()).thenReturn(String.format("Sone[%s]", id));
138                         sones.add(mockedSone);
139                         return mockedSone;
140                 }
141
142         }
143
144         public class PostMocker {
145
146                 private final Post post = mock(Post.class);
147                 private final String id;
148                 private final Sone sone;
149
150                 public PostMocker(String id, Sone sone) {
151                         this.id = id;
152                         this.sone = sone;
153                 }
154
155                 public Post create() {
156                         when(post.getId()).thenReturn(id);
157                         when(post.getSone()).thenReturn(sone);
158                         when(database.getPost(eq(id))).thenReturn(of(post));
159                         sonePosts.put(sone, post);
160                         when(post.getReplies()).then(new Answer<List<PostReply>>() {
161                                 @Override
162                                 public List<PostReply> answer(InvocationOnMock invocation) throws Throwable {
163                                         return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(postReplies.get(post));
164                                 }
165                         });
166                         return post;
167                 }
168
169         }
170
171         public class PostReplyMocker {
172
173                 private final PostReply postReply = mock(PostReply.class);
174                 private final String id;
175                 private final Sone sone;
176                 private Optional<Post> post = absent();
177
178                 public PostReplyMocker(String id, Sone sone) {
179                         this.id = id;
180                         this.sone = sone;
181                 }
182
183                 public PostReplyMocker inReplyTo(Post post) {
184                         this.post = of(post);
185                         return this;
186                 }
187
188                 public PostReply create() {
189                         when(postReply.getId()).thenReturn(id);
190                         when(postReply.getSone()).thenReturn(sone);
191                         when(database.getPostReply(eq(id))).thenReturn(of(postReply));
192                         if (post.isPresent()) {
193                                 postReplies.put(post.get(), postReply);
194                         }
195                         return postReply;
196                 }
197
198         }
199
200 }