Verify Lists instead of Collections.
[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 import static org.hamcrest.Matchers.notNullValue;
26
27 import java.util.List;
28
29 import net.pterodactylus.sone.data.Post;
30 import net.pterodactylus.sone.data.PostReply;
31 import net.pterodactylus.sone.freenet.fcp.Command.Response;
32
33 import freenet.node.FSParseException;
34 import freenet.support.SimpleFieldSet;
35
36 import org.hamcrest.CoreMatchers;
37
38 /**
39  * Verifiers used throughout the {@link net.pterodactylus.sone.fcp} package.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class Verifiers {
44
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));
49         }
50
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()));
57         }
58
59         static void verifyPosts(SimpleFieldSet postFieldSet, String prefix, List<Post> posts) throws FSParseException {
60                 assertThat(postFieldSet.getInt(prefix + "Count"), CoreMatchers.is(posts.size()));
61                 int postIndex = 0;
62                 for (Post post : posts) {
63                         verifyPost(postFieldSet, prefix + postIndex + ".", post);
64                         postIndex++;
65                 }
66         }
67
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()));
73         }
74
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);
80                         postReplyIndex++;
81                 }
82         }
83
84         static void verifyPostsWithReplies(SimpleFieldSet postFieldSet, String prefix, List<Post> posts) throws FSParseException {
85                 assertThat(postFieldSet.getInt(prefix + "Count"), CoreMatchers.is(posts.size()));
86                 int postIndex = 0;
87                 for (Post post : posts) {
88                         verifyPost(postFieldSet, prefix + postIndex + ".", post);
89                         verifyPostReplies(postFieldSet, prefix + postIndex + ".Replies.", post.getReplies());
90                         postIndex++;
91                 }
92         }
93
94         static void verifyPostWithReplies(SimpleFieldSet postFieldSet, String prefix, Post post) throws FSParseException {
95                 verifyPost(postFieldSet, prefix, post);
96                 verifyPostReplies(postFieldSet, prefix + "Replies.", post.getReplies());
97         }
98
99 }