Move more post and reply verifiers to the Verifiers class.
[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 verifyPosts(SimpleFieldSet postFieldSet, String prefix, Collection<Post> posts) throws FSParseException {
52                 assertThat(postFieldSet.getInt(prefix + "Count"), CoreMatchers.is(posts.size()));
53                 int postIndex = 0;
54                 for (Post post : posts) {
55                         verifyPost(postFieldSet, prefix + postIndex + ".", post);
56                         postIndex++;
57                 }
58         }
59
60         static void verifyPostReply(SimpleFieldSet replyParameters, String prefix, PostReply postReply) throws FSParseException {
61                 assertThat(replyParameters.get(format("%sID", prefix)), is(postReply.getId()));
62                 assertThat(replyParameters.get(format("%sSone", prefix)), is(postReply.getSone().getId()));
63                 assertThat(replyParameters.getLong(format("%sTime", prefix)), is(postReply.getTime()));
64                 assertThat(replyParameters.get(format("%sText", prefix)), is(postReply.getText()));
65         }
66
67         static void verifyPostReplies(SimpleFieldSet postFieldSet, String prefix, Collection<PostReply> postReplies) throws FSParseException {
68                 assertThat(postFieldSet.getInt(prefix + "Count"), CoreMatchers.is(from(postReplies).filter(FUTURE_REPLY_FILTER).size()));
69                 int postReplyIndex = 0;
70                 for (PostReply postReply : from(postReplies).filter(FUTURE_REPLY_FILTER)) {
71                         verifyPostReply(postFieldSet, prefix + postReplyIndex + ".", postReply);
72                         postReplyIndex++;
73                 }
74         }
75
76         static void verifyPostsWithReplies(SimpleFieldSet postFieldSet, String prefix, Collection<Post> posts) throws FSParseException {
77                 assertThat(postFieldSet.getInt(prefix + "Count"), CoreMatchers.is(posts.size()));
78                 int postIndex = 0;
79                 for (Post post : posts) {
80                         verifyPost(postFieldSet, prefix + postIndex + ".", post);
81                         verifyPostReplies(postFieldSet, prefix + postIndex + ".Replies.", post.getReplies());
82                         postIndex++;
83                 }
84         }
85
86         static void verifyPostWithReplies(SimpleFieldSet postFieldSet, String prefix, Post post) throws FSParseException {
87                 verifyPost(postFieldSet, prefix, post);
88                 verifyPostReplies(postFieldSet, prefix + "Replies.", post.getReplies());
89         }
90
91 }