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