Mock posts with a mocker, 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.of;
21 import static com.google.common.collect.ArrayListMultimap.create;
22 import static com.google.common.collect.Ordering.from;
23 import static com.google.common.collect.Sets.newHashSet;
24 import static net.pterodactylus.sone.data.Post.TIME_COMPARATOR;
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 org.mockito.invocation.InvocationOnMock;
43 import org.mockito.stubbing.Answer;
44
45 /**
46  * Mocks reusable in multiple tests.
47  *
48  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49  */
50 public class Mocks {
51
52         private final Multimap<Sone, Post> sonePosts = create();
53         private final Collection<Sone> sones = newHashSet();
54         public final Database database;
55         public final Core core;
56
57         public Mocks() {
58                 database = mockDatabase();
59                 core = mockCore(database);
60                 when(core.getLocalSones()).then(new Answer<Collection<Sone>>() {
61                         @Override
62                         public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
63                                 return FluentIterable.from(sones).filter(Sone.LOCAL_SONE_FILTER).toList();
64                         }
65                 });
66         }
67
68         private static Core mockCore(Database database) {
69                 Core core = mock(Core.class);
70                 when(core.getDatabase()).thenReturn(database);
71                 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
72                 return core;
73         }
74
75         private static Database mockDatabase() {
76                 Database database = mock(Database.class);
77                 when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
78                 when(database.getPost(anyString())).thenReturn(Optional.<Post>absent());
79                 when(database.getPostReply(anyString())).thenReturn(Optional.<PostReply>absent());
80                 return database;
81         }
82
83         public SoneMocker mockSone(String id) {
84                 return new SoneMocker(id);
85         }
86
87         public PostMocker mockPost(Sone sone, String postId) {
88                 return new PostMocker(postId, sone);
89         }
90
91         public PostReply mockPostReply(Sone sone, String replyId) {
92                 PostReply postReply = mock(PostReply.class);
93                 when(postReply.getId()).thenReturn(replyId);
94                 when(postReply.getSone()).thenReturn(sone);
95                 when(database.getPostReply(eq(replyId))).thenReturn(of(postReply));
96                 return postReply;
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 Profile profile = new Profile(mockedSone);
105
106                 private SoneMocker(String id) {
107                         this.id = id;
108                 }
109
110                 public SoneMocker local() {
111                         local = true;
112                         return this;
113                 }
114
115                 public Sone create() {
116                         when(mockedSone.getId()).thenReturn(id);
117                         when(mockedSone.isLocal()).thenReturn(local);
118                         when(mockedSone.getProfile()).thenReturn(profile);
119                         if (local) {
120                                 when(mockedSone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
121                                 when(mockedSone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
122                                         @Override
123                                         public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable {
124                                                 return new DefaultPostReplyBuilder(database, id, (String) invocation.getArguments()[0]);
125                                         }
126                                 });
127                         } else {
128                                 when(mockedSone.newPostBuilder()).thenThrow(IllegalStateException.class);
129                                 when(mockedSone.newPostReplyBuilder(anyString())).thenThrow(IllegalStateException.class);
130                         }
131                         when(core.getSone(eq(id))).thenReturn(of(mockedSone));
132                         when(database.getSone(eq(id))).thenReturn(of(mockedSone));
133                         when(mockedSone.getPosts()).then(new Answer<List<Post>>() {
134                                 @Override
135                                 public List<Post> answer(InvocationOnMock invocationOnMock) throws Throwable {
136                                         return from(TIME_COMPARATOR).sortedCopy(sonePosts.get(mockedSone));
137                                 }
138                         });
139                         when(mockedSone.toString()).thenReturn(String.format("Sone[%s]", id));
140                         sones.add(mockedSone);
141                         return mockedSone;
142                 }
143
144         }
145
146         public class PostMocker {
147
148                 private final Post post = mock(Post.class);
149                 private final String id;
150                 private final Sone sone;
151
152                 public PostMocker(String id, Sone sone) {
153                         this.id = id;
154                         this.sone = sone;
155                 }
156
157                 public Post create() {
158                         when(post.getId()).thenReturn(id);
159                         when(post.getSone()).thenReturn(sone);
160                         when(database.getPost(eq(id))).thenReturn(of(post));
161                         sonePosts.put(sone, post);
162                         return post;
163                 }
164
165         }
166
167 }