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