Fix javadoc.
[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 net.pterodactylus.sone.template.SoneAccessor.getNiceName;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.hamcrest.Matchers.is;
26 import static org.hamcrest.Matchers.notNullValue;
27
28 import java.util.List;
29
30 import net.pterodactylus.sone.data.Post;
31 import net.pterodactylus.sone.data.PostReply;
32 import net.pterodactylus.sone.data.Profile.Field;
33 import net.pterodactylus.sone.data.Sone;
34 import net.pterodactylus.sone.freenet.fcp.Command.Response;
35
36 import freenet.node.FSParseException;
37 import freenet.support.SimpleFieldSet;
38
39 import org.hamcrest.CoreMatchers;
40
41 /**
42  * Verifiers used throughout the {@link net.pterodactylus.sone.fcp} package.
43  *
44  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45  */
46 public class Verifiers {
47
48         static void verifyAnswer(Response response, String messageName) {
49                 assertThat(response, notNullValue());
50                 assertThat(response.getReplyParameters(), notNullValue());
51                 assertThat(response.getReplyParameters().get("Message"), is(messageName));
52         }
53
54         static void verifyPost(SimpleFieldSet replyParameters, String prefix, Post post) throws FSParseException {
55                 assertThat(replyParameters.get(format("%sID", prefix)), is(post.getId()));
56                 assertThat(replyParameters.get(format("%sSone", prefix)), is(post.getSone().getId()));
57                 assertThat(replyParameters.get(format("%sRecipient", prefix)), is(post.getRecipientId().orNull()));
58                 assertThat(replyParameters.getLong(format("%sTime", prefix)), is(post.getTime()));
59                 assertThat(replyParameters.get(format("%sText", prefix)), is(post.getText()));
60         }
61
62         static void verifyPosts(SimpleFieldSet postFieldSet, String prefix, List<Post> posts) throws FSParseException {
63                 assertThat(postFieldSet.getInt(prefix + "Count"), CoreMatchers.is(posts.size()));
64                 int postIndex = 0;
65                 for (Post post : posts) {
66                         verifyPost(postFieldSet, prefix + postIndex + ".", post);
67                         postIndex++;
68                 }
69         }
70
71         static void verifyPostReply(SimpleFieldSet replyParameters, String prefix, PostReply postReply) throws FSParseException {
72                 assertThat(replyParameters.get(format("%sID", prefix)), is(postReply.getId()));
73                 assertThat(replyParameters.get(format("%sSone", prefix)), is(postReply.getSone().getId()));
74                 assertThat(replyParameters.getLong(format("%sTime", prefix)), is(postReply.getTime()));
75                 assertThat(replyParameters.get(format("%sText", prefix)), is(postReply.getText()));
76         }
77
78         static void verifyPostReplies(SimpleFieldSet postFieldSet, String prefix, List<PostReply> postReplies) throws FSParseException {
79                 assertThat(postFieldSet.getInt(prefix + "Count"), CoreMatchers.is(from(postReplies).filter(FUTURE_REPLY_FILTER).size()));
80                 int postReplyIndex = 0;
81                 for (PostReply postReply : from(postReplies).filter(FUTURE_REPLY_FILTER)) {
82                         verifyPostReply(postFieldSet, prefix + postReplyIndex + ".", postReply);
83                         postReplyIndex++;
84                 }
85         }
86
87         static void verifyPostsWithReplies(SimpleFieldSet postFieldSet, String prefix, List<Post> posts) throws FSParseException {
88                 assertThat(postFieldSet.getInt(prefix + "Count"), CoreMatchers.is(posts.size()));
89                 int postIndex = 0;
90                 for (Post post : posts) {
91                         verifyPost(postFieldSet, prefix + postIndex + ".", post);
92                         verifyPostReplies(postFieldSet, prefix + postIndex + ".Replies.", post.getReplies());
93                         postIndex++;
94                 }
95         }
96
97         static void verifyPostWithReplies(SimpleFieldSet postFieldSet, String prefix, Post post) throws FSParseException {
98                 verifyPost(postFieldSet, prefix, post);
99                 verifyPostReplies(postFieldSet, prefix + "Replies.", post.getReplies());
100         }
101
102         static void verifyFollowedSone(SimpleFieldSet simpleFieldSet, String prefix, Sone sone) throws FSParseException {
103                 verifyNotFollowedSone(simpleFieldSet, prefix, sone);
104                 assertThat(simpleFieldSet.getBoolean(prefix + "Followed"), is(true));
105         }
106
107         static void verifyNotFollowedSone(SimpleFieldSet simpleFieldSet, String prefix, Sone sone) throws FSParseException {
108                 assertThat(simpleFieldSet.get(prefix + "Name"), is(sone.getName()));
109                 assertThat(simpleFieldSet.get(prefix + "NiceName"), is(getNiceName(sone)));
110                 assertThat(simpleFieldSet.getLong(prefix + "LastUpdated"), is(sone.getTime()));
111                 assertThat(simpleFieldSet.getInt(prefix + "Field.Count"), is(sone.getProfile().getFields().size()));
112                 int fieldIndex = 0;
113                 for (Field field : sone.getProfile().getFields()) {
114                         assertThat(simpleFieldSet.get(prefix + "Field." + fieldIndex + ".Name"), is(field.getName()));
115                         assertThat(simpleFieldSet.get(prefix + "Field." + fieldIndex + ".Value"), is(field.getValue()));
116                         fieldIndex++;
117                 }
118         }
119
120         static void verifySones(SimpleFieldSet simpleFieldSet, String prefix, List<Sone> sones) throws FSParseException {
121                 assertThat(simpleFieldSet.getInt(prefix + "Count"), is(sones.size()));
122                 int soneIndex = 0;
123                 for (Sone sone : sones) {
124                         assertThat(simpleFieldSet.get(prefix + soneIndex + ".ID"), is(sone.getId()));
125                         assertThat(simpleFieldSet.get(prefix + soneIndex + ".Name"), is(sone.getName()));
126                         assertThat(simpleFieldSet.get(prefix + soneIndex + ".NiceName"), is(getNiceName(sone)));
127                         assertThat(simpleFieldSet.getLong(prefix + soneIndex + ".Time"), is(sone.getTime()));
128                         soneIndex++;
129                 }
130         }
131
132 }