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