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;
25 import static org.hamcrest.Matchers.notNullValue;
27 import java.util.List;
29 import net.pterodactylus.sone.data.Post;
30 import net.pterodactylus.sone.data.PostReply;
31 import net.pterodactylus.sone.freenet.fcp.Command.Response;
33 import freenet.node.FSParseException;
34 import freenet.support.SimpleFieldSet;
36 import org.hamcrest.CoreMatchers;
39 * Verifiers used throughout the {@link net.pterodactylus.sone.fcp} package.
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43 public class Verifiers {
45 static void verifyAnswer(Response response, String messageName) {
46 assertThat(response, notNullValue());
47 assertThat(response.getReplyParameters(), notNullValue());
48 assertThat(response.getReplyParameters().get("Message"), is(messageName));
51 static void verifyPost(SimpleFieldSet replyParameters, String prefix, Post post) throws FSParseException {
52 assertThat(replyParameters.get(format("%sID", prefix)), is(post.getId()));
53 assertThat(replyParameters.get(format("%sSone", prefix)), is(post.getSone().getId()));
54 assertThat(replyParameters.get(format("%sRecipient", prefix)), is(post.getRecipientId().orNull()));
55 assertThat(replyParameters.getLong(format("%sTime", prefix)), is(post.getTime()));
56 assertThat(replyParameters.get(format("%sText", prefix)), is(post.getText()));
59 static void verifyPosts(SimpleFieldSet postFieldSet, String prefix, List<Post> posts) throws FSParseException {
60 assertThat(postFieldSet.getInt(prefix + "Count"), CoreMatchers.is(posts.size()));
62 for (Post post : posts) {
63 verifyPost(postFieldSet, prefix + postIndex + ".", post);
68 static void verifyPostReply(SimpleFieldSet replyParameters, String prefix, PostReply postReply) throws FSParseException {
69 assertThat(replyParameters.get(format("%sID", prefix)), is(postReply.getId()));
70 assertThat(replyParameters.get(format("%sSone", prefix)), is(postReply.getSone().getId()));
71 assertThat(replyParameters.getLong(format("%sTime", prefix)), is(postReply.getTime()));
72 assertThat(replyParameters.get(format("%sText", prefix)), is(postReply.getText()));
75 static void verifyPostReplies(SimpleFieldSet postFieldSet, String prefix, List<PostReply> postReplies) throws FSParseException {
76 assertThat(postFieldSet.getInt(prefix + "Count"), CoreMatchers.is(from(postReplies).filter(FUTURE_REPLY_FILTER).size()));
77 int postReplyIndex = 0;
78 for (PostReply postReply : from(postReplies).filter(FUTURE_REPLY_FILTER)) {
79 verifyPostReply(postFieldSet, prefix + postReplyIndex + ".", postReply);
84 static void verifyPostsWithReplies(SimpleFieldSet postFieldSet, String prefix, List<Post> posts) throws FSParseException {
85 assertThat(postFieldSet.getInt(prefix + "Count"), CoreMatchers.is(posts.size()));
87 for (Post post : posts) {
88 verifyPost(postFieldSet, prefix + postIndex + ".", post);
89 verifyPostReplies(postFieldSet, prefix + postIndex + ".Replies.", post.getReplies());
94 static void verifyPostWithReplies(SimpleFieldSet postFieldSet, String prefix, Post post) throws FSParseException {
95 verifyPost(postFieldSet, prefix, post);
96 verifyPostReplies(postFieldSet, prefix + "Replies.", post.getReplies());