2 * Sone - Verifiers.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.fcp;
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;
26 import java.util.Collection;
28 import net.pterodactylus.sone.data.Post;
29 import net.pterodactylus.sone.data.PostReply;
31 import freenet.node.FSParseException;
32 import freenet.support.SimpleFieldSet;
34 import org.hamcrest.CoreMatchers;
37 * Verifiers used throughout the {@link net.pterodactylus.sone.fcp} package.
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 public class Verifiers {
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()));
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()));
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);
67 static void verifyPostsWithReplies(SimpleFieldSet postFieldSet, String prefix, Collection<Post> posts) throws FSParseException {
68 assertThat(postFieldSet.getInt(prefix + "Count"), CoreMatchers.is(posts.size()));
70 for (Post post : posts) {
71 verifyPost(postFieldSet, prefix + postIndex + ".", post);
72 verifyPostReplies(postFieldSet, prefix + postIndex + ".Replies.", post.getReplies());