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;
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;
28 import java.util.List;
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 import net.pterodactylus.sone.web.ajax.JsonErrorReturnObject;
36 import net.pterodactylus.sone.web.ajax.JsonReturnObject;
38 import freenet.node.FSParseException;
39 import freenet.support.SimpleFieldSet;
41 import org.hamcrest.CoreMatchers;
44 * Verifiers used throughout the {@link net.pterodactylus.sone.fcp} package.
46 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
48 public class Verifiers {
50 public static void verifyAnswer(Response response, String messageName) {
51 assertThat(response, notNullValue());
52 assertThat(response.getReplyParameters(), notNullValue());
53 assertThat(response.getReplyParameters().get("Message"), is(messageName));
56 public static void verifyPost(SimpleFieldSet replyParameters, String prefix, Post post) throws FSParseException {
57 assertThat(replyParameters.get(format("%sID", prefix)), is(post.getId()));
58 assertThat(replyParameters.get(format("%sSone", prefix)), is(post.getSone().getId()));
59 assertThat(replyParameters.get(format("%sRecipient", prefix)), is(post.getRecipientId().orNull()));
60 assertThat(replyParameters.getLong(format("%sTime", prefix)), is(post.getTime()));
61 assertThat(replyParameters.get(format("%sText", prefix)), is(post.getText()));
64 public static void verifyPosts(SimpleFieldSet postFieldSet, String prefix, List<Post> posts) throws FSParseException {
65 assertThat(postFieldSet.getInt(prefix + "Count"), CoreMatchers.is(posts.size()));
67 for (Post post : posts) {
68 verifyPost(postFieldSet, prefix + postIndex + ".", post);
73 public static void verifyPostReply(SimpleFieldSet replyParameters, String prefix, PostReply postReply) throws FSParseException {
74 assertThat(replyParameters.get(format("%sID", prefix)), is(postReply.getId()));
75 assertThat(replyParameters.get(format("%sSone", prefix)), is(postReply.getSone().getId()));
76 assertThat(replyParameters.getLong(format("%sTime", prefix)), is(postReply.getTime()));
77 assertThat(replyParameters.get(format("%sText", prefix)), is(postReply.getText()));
80 public static void verifyPostReplies(SimpleFieldSet postFieldSet, String prefix, List<PostReply> postReplies) throws FSParseException {
81 assertThat(postFieldSet.getInt(prefix + "Count"), CoreMatchers.is(from(postReplies).filter(FUTURE_REPLY_FILTER).size()));
82 int postReplyIndex = 0;
83 for (PostReply postReply : from(postReplies).filter(FUTURE_REPLY_FILTER)) {
84 verifyPostReply(postFieldSet, prefix + postReplyIndex + ".", postReply);
89 public static void verifyPostsWithReplies(SimpleFieldSet postFieldSet, String prefix, List<Post> posts) throws FSParseException {
90 assertThat(postFieldSet.getInt(prefix + "Count"), CoreMatchers.is(posts.size()));
92 for (Post post : posts) {
93 verifyPost(postFieldSet, prefix + postIndex + ".", post);
94 verifyPostReplies(postFieldSet, prefix + postIndex + ".Replies.", post.getReplies());
99 public static void verifyPostWithReplies(SimpleFieldSet postFieldSet, String prefix, Post post) throws FSParseException {
100 verifyPost(postFieldSet, prefix, post);
101 verifyPostReplies(postFieldSet, prefix + "Replies.", post.getReplies());
104 public static void verifyFollowedSone(SimpleFieldSet simpleFieldSet, String prefix, Sone sone) throws FSParseException {
105 verifyNotFollowedSone(simpleFieldSet, prefix, sone);
106 assertThat(simpleFieldSet.getBoolean(prefix + "Followed"), is(true));
109 public static void verifyNotFollowedSone(SimpleFieldSet simpleFieldSet, String prefix, Sone sone) throws FSParseException {
110 assertThat(simpleFieldSet.get(prefix + "Name"), is(sone.getName()));
111 assertThat(simpleFieldSet.get(prefix + "NiceName"), is(getNiceName(sone)));
112 assertThat(simpleFieldSet.getLong(prefix + "LastUpdated"), is(sone.getTime()));
113 assertThat(simpleFieldSet.getInt(prefix + "Field.Count"), is(sone.getProfile().getFields().size()));
115 for (Field field : sone.getProfile().getFields()) {
116 assertThat(simpleFieldSet.get(prefix + "Field." + fieldIndex + ".Name"), is(field.getName()));
117 assertThat(simpleFieldSet.get(prefix + "Field." + fieldIndex + ".Value"), is(field.getValue()));
122 public static void verifySones(SimpleFieldSet simpleFieldSet, String prefix, List<Sone> sones) throws FSParseException {
123 assertThat(simpleFieldSet.getInt(prefix + "Count"), is(sones.size()));
125 for (Sone sone : sones) {
126 assertThat(simpleFieldSet.get(prefix + soneIndex + ".ID"), is(sone.getId()));
127 assertThat(simpleFieldSet.get(prefix + soneIndex + ".Name"), is(sone.getName()));
128 assertThat(simpleFieldSet.get(prefix + soneIndex + ".NiceName"), is(getNiceName(sone)));
129 assertThat(simpleFieldSet.getLong(prefix + soneIndex + ".Time"), is(sone.getTime()));
134 public static void verifySuccessfulJsonResponse(JsonReturnObject jsonReturnObject) {
135 assertThat(jsonReturnObject, notNullValue());
136 assertThat(jsonReturnObject.isSuccess(), is(true));
139 public static void verifyJsonError(JsonReturnObject jsonReturnObject, String error) {
140 assertThat(jsonReturnObject, notNullValue());
141 assertThat(jsonReturnObject.isSuccess(), is(false));
142 assertThat(((JsonErrorReturnObject) jsonReturnObject).getError(), is(error));