fd47592cca6ef3dd32ce19e02f5665e69a273238
[Sone.git] / src / test / java / net / pterodactylus / sone / fcp / Verifiers.java
1 /*
2  * Sone - Verifiers.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.fcp;
19
20 import static com.google.common.collect.FluentIterable.from;
21 import static java.lang.String.format;
22 import static net.pterodactylus.sone.data.Reply.FUTURE_REPLY_FILTER;
23 import static org.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.Matchers.is;
25
26 import java.util.Collection;
27
28 import net.pterodactylus.sone.data.Post;
29 import net.pterodactylus.sone.data.PostReply;
30
31 import freenet.node.FSParseException;
32 import freenet.support.SimpleFieldSet;
33
34 import org.hamcrest.CoreMatchers;
35
36 /**
37  * Verifiers used throughout the {@link net.pterodactylus.sone.fcp} package.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class Verifiers {
42
43         static void verifyPost(SimpleFieldSet replyParameters, String prefix, Post post) throws FSParseException {
44                 assertThat(replyParameters.get(format("%sID", prefix)), is(post.getId()));
45                 assertThat(replyParameters.get(format("%sSone", prefix)), is(post.getSone().getId()));
46                 assertThat(replyParameters.get(format("%sRecipient", prefix)), is(post.getRecipientId().orNull()));
47                 assertThat(replyParameters.getLong(format("%sTime", prefix)), is(post.getTime()));
48                 assertThat(replyParameters.get(format("%sText", prefix)), is(post.getText()));
49         }
50
51         static void verifyPostReply(SimpleFieldSet replyParameters, String prefix, PostReply postReply) throws FSParseException {
52                 assertThat(replyParameters.get(format("%sID", prefix)), is(postReply.getId()));
53                 assertThat(replyParameters.get(format("%sSone", prefix)), is(postReply.getSone().getId()));
54                 assertThat(replyParameters.getLong(format("%sTime", prefix)), is(postReply.getTime()));
55                 assertThat(replyParameters.get(format("%sText", prefix)), is(postReply.getText()));
56         }
57
58         static void verifyPostReplies(SimpleFieldSet postFieldSet, String prefix, Collection<PostReply> postReplies) throws FSParseException {
59                 assertThat(postFieldSet.getInt(prefix + "Count"), CoreMatchers.is(from(postReplies).filter(FUTURE_REPLY_FILTER).size()));
60                 int postReplyIndex = 0;
61                 for (PostReply postReply : from(postReplies).filter(FUTURE_REPLY_FILTER)) {
62                         verifyPostReply(postFieldSet, prefix + postReplyIndex + ".", postReply);
63                         postReplyIndex++;
64                 }
65         }
66
67         static void verifyPostsWithReplies(SimpleFieldSet postFieldSet, String prefix, Collection<Post> posts) throws FSParseException {
68                 assertThat(postFieldSet.getInt(prefix + "Count"), CoreMatchers.is(posts.size()));
69                 int postIndex = 0;
70                 for (Post post : posts) {
71                         verifyPost(postFieldSet, prefix + postIndex + ".", post);
72                         verifyPostReplies(postFieldSet, prefix + postIndex + ".Replies.", post.getReplies());
73                         postIndex++;
74                 }
75         }
76
77 }