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