2 * Sone - GetPostCommandTest.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.base.Optional.of;
21 import static java.lang.String.format;
22 import static java.util.Arrays.asList;
23 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.hamcrest.Matchers.is;
26 import static org.hamcrest.Matchers.notNullValue;
27 import static org.mockito.Mockito.when;
29 import net.pterodactylus.sone.data.Mocks;
30 import net.pterodactylus.sone.data.Post;
31 import net.pterodactylus.sone.data.PostReply;
32 import net.pterodactylus.sone.data.Sone;
33 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
34 import net.pterodactylus.sone.freenet.fcp.Command.Response;
35 import net.pterodactylus.sone.freenet.fcp.FcpException;
37 import freenet.node.FSParseException;
38 import freenet.support.SimpleFieldSet;
40 import com.google.common.base.Optional;
41 import org.junit.Test;
44 * Unit test for {@link GetPostCommand}.
46 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
48 public class GetPostCommandTest {
50 private final Mocks mocks = new Mocks();
51 private final GetPostCommand getPostCommand = new GetPostCommand(mocks.core);
54 public void verifyThatGettingAPostWithoutRepliesAndRecipientWorks() throws FcpException, FSParseException {
55 Sone sone = mocks.mockSone("SoneId").create();
56 Post post = preparePostWithoutRecipient(sone);
57 SimpleFieldSet getPostFieldSet = new SimpleFieldSetBuilder()
58 .put("Message", "GetPost")
59 .put("Post", "PostId")
61 Response response = getPostCommand.execute(getPostFieldSet, null, DIRECT);
62 verifyAnswer(response);
63 verifyPost(post, response);
67 public void verifyThatGettingAPostWithoutRepliesAndWithRecipientWorks() throws FcpException, FSParseException {
68 Sone sone = mocks.mockSone("SoneId").create();
69 Sone otherSone = mocks.mockSone("OtherSoneId").create();
70 Post post = preparePostWithRecipient(sone, otherSone);
71 SimpleFieldSet getPostFieldSet = new SimpleFieldSetBuilder()
72 .put("Message", "GetPost")
73 .put("Post", "PostId")
75 Response response = getPostCommand.execute(getPostFieldSet, null, DIRECT);
76 verifyAnswer(response);
77 verifyPost(post, response);
81 public void verifyThatGettingAPostWithRepliesWorks() throws FcpException, FSParseException {
82 Sone sone = mocks.mockSone("SoneId").create();
83 Post post = preparePostWithoutRecipient(sone);
84 PostReply postReply1 = mocks.mockPostReply(sone, "Reply1");
85 when(postReply1.getText()).thenReturn("Reply 1.");
86 PostReply postReply2 = mocks.mockPostReply(sone, "Reply2");
87 when(postReply2.getText()).thenReturn("Reply 2.");
88 when(post.getReplies()).thenReturn(asList(postReply1, postReply2));
89 SimpleFieldSet getPostFieldSet = new SimpleFieldSetBuilder()
90 .put("Message", "GetPost")
91 .put("Post", "PostId")
92 .put("IncludeReplies", "true")
94 Response response = getPostCommand.execute(getPostFieldSet, null, DIRECT);
95 verifyAnswer(response);
96 verifyPost(post, response);
97 assertThat(response.getReplyParameters().getInt("Post.Replies.Count"), is(post.getReplies().size()));
98 verifyReply(response.getReplyParameters(), "Post.Replies.0.", postReply1);
99 verifyReply(response.getReplyParameters(), "Post.Replies.1.", postReply2);
102 @Test(expected = FcpException.class)
103 public void verifyThatGettingANonExistingPostCausesAnError() throws FcpException {
104 SimpleFieldSet getPostFieldSet = new SimpleFieldSetBuilder()
105 .put("Message", "GetPost")
106 .put("Post", "PostId")
108 getPostCommand.execute(getPostFieldSet, null, DIRECT);
111 @Test(expected = FcpException.class)
112 public void verifyThatAMissingPostIdCausesAnError() throws FcpException {
113 SimpleFieldSet getPostFieldSet = new SimpleFieldSetBuilder()
114 .put("Message", "GetPost")
116 getPostCommand.execute(getPostFieldSet, null, DIRECT);
119 private Post preparePostWithoutRecipient(Sone sone) {
120 Post post = preparePost(sone);
121 when(post.getRecipientId()).thenReturn(Optional.<String>absent());
122 when(post.getRecipient()).thenReturn(Optional.<Sone>absent());
126 private Post preparePostWithRecipient(Sone sone, Sone otherSone) {
127 Post post = preparePost(sone);
128 String otherSoneId = otherSone.getId();
129 when(post.getRecipientId()).thenReturn(of(otherSoneId));
130 when(post.getRecipient()).thenReturn(of(otherSone));
134 private Post preparePost(Sone sone) {
135 Post post = mocks.mockPost(sone, "PostId").create();
136 when(post.getText()).thenReturn("Text of the post.");
137 when(post.getTime()).thenReturn(1000L);
141 private void verifyAnswer(Response response) {
142 assertThat(response, notNullValue());
143 assertThat(response.getReplyParameters(), notNullValue());
144 assertThat(response.getReplyParameters().get("Message"), is("Post"));
147 private void verifyReply(SimpleFieldSet replyParameters, String prefix, PostReply postReply) throws FSParseException {
148 assertThat(replyParameters.get(format("%sID", prefix)), is(postReply.getId()));
149 assertThat(replyParameters.get(format("%sSone", prefix)), is(postReply.getSone().getId()));
150 assertThat(replyParameters.getLong(format("%sTime", prefix)), is(postReply.getTime()));
151 assertThat(replyParameters.get(format("%sText", prefix)), is(postReply.getText()));
154 private void verifyPost(Post post, Response response) throws FSParseException {
155 assertThat(response.getReplyParameters().get("Post.ID"), is(post.getId()));
156 assertThat(response.getReplyParameters().get("Post.Sone"), is(post.getSone().getId()));
157 assertThat(response.getReplyParameters().get("Post.Recipient"), is(post.getRecipientId().orNull()));
158 assertThat(response.getReplyParameters().getLong("Post.Time"), is(post.getTime()));
159 assertThat(response.getReplyParameters().get("Post.Text"), is(post.getText()));