Add filter for linked images
[Sone.git] / src / test / java / net / pterodactylus / sone / Matchers.java
1 /*
2  * Sone - Matchers.java - Copyright © 2013–2016 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;
19
20 import static java.util.regex.Pattern.compile;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24
25 import net.pterodactylus.sone.data.Album;
26 import net.pterodactylus.sone.data.Image;
27 import net.pterodactylus.sone.data.Post;
28 import net.pterodactylus.sone.data.PostReply;
29
30 import com.google.common.base.Optional;
31 import org.hamcrest.Description;
32 import org.hamcrest.Matcher;
33 import org.hamcrest.TypeSafeDiagnosingMatcher;
34 import org.hamcrest.TypeSafeMatcher;
35
36 /**
37  * Matchers used throughout the tests.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class Matchers {
42
43         public static Matcher<String> matchesRegex(final String regex) {
44                 return new TypeSafeMatcher<String>() {
45                         @Override
46                         protected boolean matchesSafely(String item) {
47                                 return compile(regex).matcher(item).matches();
48                         }
49
50                         @Override
51                         public void describeTo(Description description) {
52                                 description.appendText("matches: ").appendValue(regex);
53                         }
54                 };
55         }
56
57         public static Matcher<InputStream> delivers(final byte[] data) {
58                 return new TypeSafeMatcher<InputStream>() {
59                         byte[] readData = new byte[data.length];
60
61                         @Override
62                         protected boolean matchesSafely(InputStream inputStream) {
63                                 int offset = 0;
64                                 try {
65                                         while (true) {
66                                                 int r = inputStream.read();
67                                                 if (r == -1) {
68                                                         return offset == data.length;
69                                                 }
70                                                 if (offset == data.length) {
71                                                         return false;
72                                                 }
73                                                 if (data[offset] != (readData[offset] = (byte) r)) {
74                                                         return false;
75                                                 }
76                                                 offset++;
77                                         }
78                                 } catch (IOException ioe1) {
79                                         return false;
80                                 }
81                         }
82
83                         @Override
84                         public void describeTo(Description description) {
85                                 description.appendValue(data);
86                         }
87
88                         @Override
89                         protected void describeMismatchSafely(InputStream item,
90                                         Description mismatchDescription) {
91                                 mismatchDescription.appendValue(readData);
92                         }
93                 };
94         }
95
96         public static Matcher<Post> isPost(String postId, long time,
97                         String text, Optional<String> recipient) {
98                 return new PostMatcher(postId, time, text, recipient);
99         }
100
101         public static Matcher<Post> isPostWithId(String postId) {
102                 return new PostIdMatcher(postId);
103         }
104
105         public static Matcher<PostReply> isPostReply(String postReplyId,
106                         String postId, long time, String text) {
107                 return new PostReplyMatcher(postReplyId, postId, time, text);
108         }
109
110         public static Matcher<Album> isAlbum(final String albumId,
111                         final String parentAlbumId,
112                         final String title, final String albumDescription) {
113                 return new TypeSafeDiagnosingMatcher<Album>() {
114                         @Override
115                         protected boolean matchesSafely(Album album,
116                                         Description mismatchDescription) {
117                                 if (!album.getId().equals(albumId)) {
118                                         mismatchDescription.appendText("ID is ")
119                                                         .appendValue(album.getId());
120                                         return false;
121                                 }
122                                 if (parentAlbumId == null) {
123                                         if (album.getParent() != null) {
124                                                 mismatchDescription.appendText("has parent album");
125                                                 return false;
126                                         }
127                                 } else {
128                                         if (album.getParent() == null) {
129                                                 mismatchDescription.appendText("has no parent album");
130                                                 return false;
131                                         }
132                                         if (!album.getParent().getId().equals(parentAlbumId)) {
133                                                 mismatchDescription.appendText("parent album is ")
134                                                                 .appendValue(album.getParent().getId());
135                                                 return false;
136                                         }
137                                 }
138                                 if (!title.equals(album.getTitle())) {
139                                         mismatchDescription.appendText("has title ")
140                                                         .appendValue(album.getTitle());
141                                         return false;
142                                 }
143                                 if (!albumDescription.equals(album.getDescription())) {
144                                         mismatchDescription.appendText("has description ")
145                                                         .appendValue(album.getDescription());
146                                         return false;
147                                 }
148                                 return true;
149                         }
150
151                         @Override
152                         public void describeTo(Description description) {
153                                 description.appendText("is album ").appendValue(albumId);
154                                 if (parentAlbumId == null) {
155                                         description.appendText(", has no parent");
156                                 } else {
157                                         description.appendText(", has parent ")
158                                                         .appendValue(parentAlbumId);
159                                 }
160                                 description.appendText(", has title ").appendValue(title);
161                                 description.appendText(", has description ")
162                                                 .appendValue(albumDescription);
163                         }
164                 };
165         }
166
167         public static Matcher<Image> isImage(final String id,
168                         final long creationTime,
169                         final String key, final String title,
170                         final String imageDescription,
171                         final int width, final int height) {
172                 return new TypeSafeDiagnosingMatcher<Image>() {
173                         @Override
174                         protected boolean matchesSafely(Image image,
175                                         Description mismatchDescription) {
176                                 if (!image.getId().equals(id)) {
177                                         mismatchDescription.appendText("ID is ")
178                                                         .appendValue(image.getId());
179                                         return false;
180                                 }
181                                 if (image.getCreationTime() != creationTime) {
182                                         mismatchDescription.appendText("created at @")
183                                                         .appendValue(image.getCreationTime());
184                                         return false;
185                                 }
186                                 if (!image.getKey().equals(key)) {
187                                         mismatchDescription.appendText("key is ")
188                                                         .appendValue(image.getKey());
189                                         return false;
190                                 }
191                                 if (!image.getTitle().equals(title)) {
192                                         mismatchDescription.appendText("title is ")
193                                                         .appendValue(image.getTitle());
194                                         return false;
195                                 }
196                                 if (!image.getDescription().equals(imageDescription)) {
197                                         mismatchDescription.appendText("description is ")
198                                                         .appendValue(image.getDescription());
199                                         return false;
200                                 }
201                                 if (image.getWidth() != width) {
202                                         mismatchDescription.appendText("width is ")
203                                                         .appendValue(image.getWidth());
204                                         return false;
205                                 }
206                                 if (image.getHeight() != height) {
207                                         mismatchDescription.appendText("height is ")
208                                                         .appendValue(image.getHeight());
209                                         return false;
210                                 }
211                                 return true;
212                         }
213
214                         @Override
215                         public void describeTo(Description description) {
216                                 description.appendText("image with ID ").appendValue(id);
217                                 description.appendText(", created at @")
218                                                 .appendValue(creationTime);
219                                 description.appendText(", has key ").appendValue(key);
220                                 description.appendText(", has title ").appendValue(title);
221                                 description.appendText(", has description ")
222                                                 .appendValue(imageDescription);
223                                 description.appendText(", has width ").appendValue(width);
224                                 description.appendText(", has height ").appendValue(height);
225                         }
226                 };
227         }
228
229         private static class PostMatcher extends TypeSafeDiagnosingMatcher<Post> {
230
231                 private final String postId;
232                 private final long time;
233                 private final String text;
234                 private final Optional<String> recipient;
235
236                 private PostMatcher(String postId, long time, String text,
237                                 Optional<String> recipient) {
238                         this.postId = postId;
239                         this.time = time;
240                         this.text = text;
241                         this.recipient = recipient;
242                 }
243
244                 @Override
245                 protected boolean matchesSafely(Post post,
246                                 Description mismatchDescription) {
247                         if (!post.getId().equals(postId)) {
248                                 mismatchDescription.appendText("ID is not ")
249                                                 .appendValue(postId);
250                                 return false;
251                         }
252                         if (post.getTime() != time) {
253                                 mismatchDescription.appendText("Time is not @")
254                                                 .appendValue(time);
255                                 return false;
256                         }
257                         if (!post.getText().equals(text)) {
258                                 mismatchDescription.appendText("Text is not ")
259                                                 .appendValue(text);
260                                 return false;
261                         }
262                         if (recipient.isPresent()) {
263                                 if (!post.getRecipientId().isPresent()) {
264                                         mismatchDescription.appendText(
265                                                         "Recipient not present");
266                                         return false;
267                                 }
268                                 if (!post.getRecipientId().get().equals(recipient.get())) {
269                                         mismatchDescription.appendText("Recipient is not ")
270                                                         .appendValue(recipient.get());
271                                         return false;
272                                 }
273                         } else {
274                                 if (post.getRecipientId().isPresent()) {
275                                         mismatchDescription.appendText("Recipient is present");
276                                         return false;
277                                 }
278                         }
279                         return true;
280                 }
281
282                 @Override
283                 public void describeTo(Description description) {
284                         description.appendText("is post with ID ")
285                                         .appendValue(postId);
286                         description.appendText(", created at @").appendValue(time);
287                         description.appendText(", text ").appendValue(text);
288                         if (recipient.isPresent()) {
289                                 description.appendText(", directed at ")
290                                                 .appendValue(recipient.get());
291                         }
292                 }
293
294         }
295
296         private static class PostIdMatcher extends TypeSafeDiagnosingMatcher<Post> {
297
298                 private final String id;
299
300                 private PostIdMatcher(String id) {
301                         this.id = id;
302                 }
303
304                 @Override
305                 protected boolean matchesSafely(Post item,
306                                 Description mismatchDescription) {
307                         if (!item.getId().equals(id)) {
308                                 mismatchDescription.appendText("post has ID ").appendValue(item.getId());
309                                 return false;
310                         }
311                         return true;
312                 }
313
314                 @Override
315                 public void describeTo(Description description) {
316                         description.appendText("post with ID ").appendValue(id);
317                 }
318
319         }
320
321         private static class PostReplyMatcher
322                         extends TypeSafeDiagnosingMatcher<PostReply> {
323
324                 private final String postReplyId;
325                 private final String postId;
326                 private final long time;
327                 private final String text;
328
329                 private PostReplyMatcher(String postReplyId, String postId, long time,
330                                 String text) {
331                         this.postReplyId = postReplyId;
332                         this.postId = postId;
333                         this.time = time;
334                         this.text = text;
335                 }
336
337                 @Override
338                 protected boolean matchesSafely(PostReply postReply,
339                                 Description mismatchDescription) {
340                         if (!postReply.getId().equals(postReplyId)) {
341                                 mismatchDescription.appendText("is post reply ")
342                                                 .appendValue(postReply.getId());
343                                 return false;
344                         }
345                         if (!postReply.getPostId().equals(postId)) {
346                                 mismatchDescription.appendText("is reply to ")
347                                                 .appendValue(postReply.getPostId());
348                                 return false;
349                         }
350                         if (postReply.getTime() != time) {
351                                 mismatchDescription.appendText("is created at @").appendValue(
352                                                 postReply.getTime());
353                                 return false;
354                         }
355                         if (!postReply.getText().equals(text)) {
356                                 mismatchDescription.appendText("says ")
357                                                 .appendValue(postReply.getText());
358                                 return false;
359                         }
360                         return true;
361                 }
362
363                 @Override
364                 public void describeTo(Description description) {
365                         description.appendText("is post reply ").appendValue(postReplyId);
366                         description.appendText(", replies to post ").appendValue(postId);
367                         description.appendText(", is created at @").appendValue(time);
368                         description.appendText(", says ").appendValue(text);
369                 }
370
371         }
372
373 }