Move post reply 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.Post;
26 import net.pterodactylus.sone.data.PostReply;
27
28 import com.google.common.base.Optional;
29 import org.hamcrest.Description;
30 import org.hamcrest.Matcher;
31 import org.hamcrest.TypeSafeDiagnosingMatcher;
32 import org.hamcrest.TypeSafeMatcher;
33
34 /**
35  * Matchers used throughout the tests.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
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<PostReply> isPostReply(String postReplyId,
100                         String postId, long time, String text) {
101                 return new PostReplyMatcher(postReplyId, postId, time, text);
102         }
103
104         private static class PostMatcher extends TypeSafeDiagnosingMatcher<Post> {
105
106                 private final String postId;
107                 private final long time;
108                 private final String text;
109                 private final Optional<String> recipient;
110
111                 private PostMatcher(String postId, long time, String text,
112                                 Optional<String> recipient) {
113                         this.postId = postId;
114                         this.time = time;
115                         this.text = text;
116                         this.recipient = recipient;
117                 }
118
119                 @Override
120                 protected boolean matchesSafely(Post post,
121                                 Description mismatchDescription) {
122                         if (!post.getId().equals(postId)) {
123                                 mismatchDescription.appendText("ID is not ")
124                                                 .appendValue(postId);
125                                 return false;
126                         }
127                         if (post.getTime() != time) {
128                                 mismatchDescription.appendText("Time is not @")
129                                                 .appendValue(time);
130                                 return false;
131                         }
132                         if (!post.getText().equals(text)) {
133                                 mismatchDescription.appendText("Text is not ")
134                                                 .appendValue(text);
135                                 return false;
136                         }
137                         if (recipient.isPresent()) {
138                                 if (!post.getRecipientId().isPresent()) {
139                                         mismatchDescription.appendText(
140                                                         "Recipient not present");
141                                         return false;
142                                 }
143                                 if (!post.getRecipientId().get().equals(recipient.get())) {
144                                         mismatchDescription.appendText("Recipient is not ")
145                                                         .appendValue(recipient.get());
146                                         return false;
147                                 }
148                         } else {
149                                 if (post.getRecipientId().isPresent()) {
150                                         mismatchDescription.appendText("Recipient is present");
151                                         return false;
152                                 }
153                         }
154                         return true;
155                 }
156
157                 @Override
158                 public void describeTo(Description description) {
159                         description.appendText("is post with ID ")
160                                         .appendValue(postId);
161                         description.appendText(", created at @").appendValue(time);
162                         description.appendText(", text ").appendValue(text);
163                         if (recipient.isPresent()) {
164                                 description.appendText(", directed at ")
165                                                 .appendValue(recipient.get());
166                         }
167                 }
168
169         }
170
171         private static class PostReplyMatcher
172                         extends TypeSafeDiagnosingMatcher<PostReply> {
173
174                 private final String postReplyId;
175                 private final String postId;
176                 private final long time;
177                 private final String text;
178
179                 private PostReplyMatcher(String postReplyId, String postId, long time,
180                                 String text) {
181                         this.postReplyId = postReplyId;
182                         this.postId = postId;
183                         this.time = time;
184                         this.text = text;
185                 }
186
187                 @Override
188                 protected boolean matchesSafely(PostReply postReply,
189                                 Description mismatchDescription) {
190                         if (!postReply.getId().equals(postReplyId)) {
191                                 mismatchDescription.appendText("is post reply ")
192                                                 .appendValue(postReply.getId());
193                                 return false;
194                         }
195                         if (!postReply.getPostId().equals(postId)) {
196                                 mismatchDescription.appendText("is reply to ")
197                                                 .appendValue(postReply.getPostId());
198                                 return false;
199                         }
200                         if (postReply.getTime() != time) {
201                                 mismatchDescription.appendText("is created at @").appendValue(
202                                                 postReply.getTime());
203                                 return false;
204                         }
205                         if (!postReply.getText().equals(text)) {
206                                 mismatchDescription.appendText("says ")
207                                                 .appendValue(postReply.getText());
208                                 return false;
209                         }
210                         return true;
211                 }
212
213                 @Override
214                 public void describeTo(Description description) {
215                         description.appendText("is post reply ").appendValue(postReplyId);
216                         description.appendText(", replies to post ").appendValue(postId);
217                         description.appendText(", is created at @").appendValue(time);
218                         description.appendText(", says ").appendValue(text);
219                 }
220
221         }
222
223 }