Move album 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.Post;
27 import net.pterodactylus.sone.data.PostReply;
28
29 import com.google.common.base.Optional;
30 import org.hamcrest.Description;
31 import org.hamcrest.Matcher;
32 import org.hamcrest.TypeSafeDiagnosingMatcher;
33 import org.hamcrest.TypeSafeMatcher;
34
35 /**
36  * Matchers used throughout the tests.
37  *
38  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39  */
40 public class Matchers {
41
42         public static Matcher<String> matchesRegex(final String regex) {
43                 return new TypeSafeMatcher<String>() {
44                         @Override
45                         protected boolean matchesSafely(String item) {
46                                 return compile(regex).matcher(item).matches();
47                         }
48
49                         @Override
50                         public void describeTo(Description description) {
51                                 description.appendText("matches: ").appendValue(regex);
52                         }
53                 };
54         }
55
56         public static Matcher<InputStream> delivers(final byte[] data) {
57                 return new TypeSafeMatcher<InputStream>() {
58                         byte[] readData = new byte[data.length];
59
60                         @Override
61                         protected boolean matchesSafely(InputStream inputStream) {
62                                 int offset = 0;
63                                 try {
64                                         while (true) {
65                                                 int r = inputStream.read();
66                                                 if (r == -1) {
67                                                         return offset == data.length;
68                                                 }
69                                                 if (offset == data.length) {
70                                                         return false;
71                                                 }
72                                                 if (data[offset] != (readData[offset] = (byte) r)) {
73                                                         return false;
74                                                 }
75                                                 offset++;
76                                         }
77                                 } catch (IOException ioe1) {
78                                         return false;
79                                 }
80                         }
81
82                         @Override
83                         public void describeTo(Description description) {
84                                 description.appendValue(data);
85                         }
86
87                         @Override
88                         protected void describeMismatchSafely(InputStream item,
89                                         Description mismatchDescription) {
90                                 mismatchDescription.appendValue(readData);
91                         }
92                 };
93         }
94
95         public static Matcher<Post> isPost(String postId, long time,
96                         String text, Optional<String> recipient) {
97                 return new PostMatcher(postId, time, text, recipient);
98         }
99
100         public static Matcher<PostReply> isPostReply(String postReplyId,
101                         String postId, long time, String text) {
102                 return new PostReplyMatcher(postReplyId, postId, time, text);
103         }
104
105         public static Matcher<Album> isAlbum(final String albumId,
106                         final String parentAlbumId,
107                         final String title, final String albumDescription,
108                         final String imageId) {
109                 return new TypeSafeDiagnosingMatcher<Album>() {
110                         @Override
111                         protected boolean matchesSafely(Album album,
112                                         Description mismatchDescription) {
113                                 if (!album.getId().equals(albumId)) {
114                                         mismatchDescription.appendText("ID is ")
115                                                         .appendValue(album.getId());
116                                         return false;
117                                 }
118                                 if (parentAlbumId == null) {
119                                         if (album.getParent() != null) {
120                                                 mismatchDescription.appendText("has parent album");
121                                                 return false;
122                                         }
123                                 } else {
124                                         if (album.getParent() == null) {
125                                                 mismatchDescription.appendText("has no parent album");
126                                                 return false;
127                                         }
128                                         if (!album.getParent().getId().equals(parentAlbumId)) {
129                                                 mismatchDescription.appendText("parent album is ")
130                                                                 .appendValue(album.getParent().getId());
131                                                 return false;
132                                         }
133                                 }
134                                 if (!title.equals(album.getTitle())) {
135                                         mismatchDescription.appendText("has title ")
136                                                         .appendValue(album.getTitle());
137                                         return false;
138                                 }
139                                 if (!albumDescription.equals(album.getDescription())) {
140                                         mismatchDescription.appendText("has description ")
141                                                         .appendValue(album.getDescription());
142                                         return false;
143                                 }
144                                 if (imageId == null) {
145                                         if (album.getAlbumImage() != null) {
146                                                 mismatchDescription.appendText("has album image");
147                                                 return false;
148                                         }
149                                 } else {
150                                         if (album.getAlbumImage() == null) {
151                                                 mismatchDescription.appendText("has no album image");
152                                                 return false;
153                                         }
154                                         if (!album.getAlbumImage().getId().equals(imageId)) {
155                                                 mismatchDescription.appendText("has album image ")
156                                                                 .appendValue(album.getAlbumImage().getId());
157                                                 return false;
158                                         }
159                                 }
160                                 return true;
161                         }
162
163                         @Override
164                         public void describeTo(Description description) {
165                                 description.appendText("is album ").appendValue(albumId);
166                                 if (parentAlbumId == null) {
167                                         description.appendText(", has no parent");
168                                 } else {
169                                         description.appendText(", has parent ")
170                                                         .appendValue(parentAlbumId);
171                                 }
172                                 description.appendText(", has title ").appendValue(title);
173                                 description.appendText(", has description ")
174                                                 .appendValue(albumDescription);
175                                 if (imageId == null) {
176                                         description.appendText(", has no album image");
177                                 } else {
178                                         description.appendText(", has album image ")
179                                                         .appendValue(imageId);
180                                 }
181                         }
182                 };
183         }
184
185         private static class PostMatcher extends TypeSafeDiagnosingMatcher<Post> {
186
187                 private final String postId;
188                 private final long time;
189                 private final String text;
190                 private final Optional<String> recipient;
191
192                 private PostMatcher(String postId, long time, String text,
193                                 Optional<String> recipient) {
194                         this.postId = postId;
195                         this.time = time;
196                         this.text = text;
197                         this.recipient = recipient;
198                 }
199
200                 @Override
201                 protected boolean matchesSafely(Post post,
202                                 Description mismatchDescription) {
203                         if (!post.getId().equals(postId)) {
204                                 mismatchDescription.appendText("ID is not ")
205                                                 .appendValue(postId);
206                                 return false;
207                         }
208                         if (post.getTime() != time) {
209                                 mismatchDescription.appendText("Time is not @")
210                                                 .appendValue(time);
211                                 return false;
212                         }
213                         if (!post.getText().equals(text)) {
214                                 mismatchDescription.appendText("Text is not ")
215                                                 .appendValue(text);
216                                 return false;
217                         }
218                         if (recipient.isPresent()) {
219                                 if (!post.getRecipientId().isPresent()) {
220                                         mismatchDescription.appendText(
221                                                         "Recipient not present");
222                                         return false;
223                                 }
224                                 if (!post.getRecipientId().get().equals(recipient.get())) {
225                                         mismatchDescription.appendText("Recipient is not ")
226                                                         .appendValue(recipient.get());
227                                         return false;
228                                 }
229                         } else {
230                                 if (post.getRecipientId().isPresent()) {
231                                         mismatchDescription.appendText("Recipient is present");
232                                         return false;
233                                 }
234                         }
235                         return true;
236                 }
237
238                 @Override
239                 public void describeTo(Description description) {
240                         description.appendText("is post with ID ")
241                                         .appendValue(postId);
242                         description.appendText(", created at @").appendValue(time);
243                         description.appendText(", text ").appendValue(text);
244                         if (recipient.isPresent()) {
245                                 description.appendText(", directed at ")
246                                                 .appendValue(recipient.get());
247                         }
248                 }
249
250         }
251
252         private static class PostReplyMatcher
253                         extends TypeSafeDiagnosingMatcher<PostReply> {
254
255                 private final String postReplyId;
256                 private final String postId;
257                 private final long time;
258                 private final String text;
259
260                 private PostReplyMatcher(String postReplyId, String postId, long time,
261                                 String text) {
262                         this.postReplyId = postReplyId;
263                         this.postId = postId;
264                         this.time = time;
265                         this.text = text;
266                 }
267
268                 @Override
269                 protected boolean matchesSafely(PostReply postReply,
270                                 Description mismatchDescription) {
271                         if (!postReply.getId().equals(postReplyId)) {
272                                 mismatchDescription.appendText("is post reply ")
273                                                 .appendValue(postReply.getId());
274                                 return false;
275                         }
276                         if (!postReply.getPostId().equals(postId)) {
277                                 mismatchDescription.appendText("is reply to ")
278                                                 .appendValue(postReply.getPostId());
279                                 return false;
280                         }
281                         if (postReply.getTime() != time) {
282                                 mismatchDescription.appendText("is created at @").appendValue(
283                                                 postReply.getTime());
284                                 return false;
285                         }
286                         if (!postReply.getText().equals(text)) {
287                                 mismatchDescription.appendText("says ")
288                                                 .appendValue(postReply.getText());
289                                 return false;
290                         }
291                         return true;
292                 }
293
294                 @Override
295                 public void describeTo(Description description) {
296                         description.appendText("is post reply ").appendValue(postReplyId);
297                         description.appendText(", replies to post ").appendValue(postId);
298                         description.appendText(", is created at @").appendValue(time);
299                         description.appendText(", says ").appendValue(text);
300                 }
301
302         }
303
304 }