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 java.util.Arrays.asList;
21 import static net.pterodactylus.sone.Verifiers.verifyAnswer;
22 import static net.pterodactylus.sone.Verifiers.verifyPostWithReplies;
23 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
24 import static org.mockito.Mockito.when;
26 import net.pterodactylus.sone.data.Mocks;
27 import net.pterodactylus.sone.data.Post;
28 import net.pterodactylus.sone.data.PostReply;
29 import net.pterodactylus.sone.data.Sone;
30 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
31 import net.pterodactylus.sone.freenet.fcp.Command.Response;
32 import net.pterodactylus.sone.freenet.fcp.FcpException;
34 import freenet.node.FSParseException;
35 import freenet.support.SimpleFieldSet;
37 import org.junit.Test;
40 * Unit test for {@link GetPostCommand}.
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class GetPostCommandTest {
46 private final Mocks mocks = new Mocks();
47 private final GetPostCommand getPostCommand = new GetPostCommand(mocks.core);
50 public void verifyThatGettingAPostWithoutRepliesAndRecipientWorks() throws FcpException, FSParseException {
51 Sone sone = mocks.mockSone("SoneId").create();
52 Post post = mocks.mockPost(sone, "PostId").withTime(1000L).withText("Text of the post.").create();
53 SimpleFieldSet getPostFieldSet = new SimpleFieldSetBuilder()
54 .put("Message", "GetPost")
55 .put("Post", "PostId")
57 Response response = getPostCommand.execute(getPostFieldSet, null, DIRECT);
58 verifyAnswer(response, "Post");
59 verifyPostWithReplies(response.getReplyParameters(), "Post.", post);
63 public void verifyThatGettingAPostWithoutRepliesAndWithRecipientWorks() throws FcpException, FSParseException {
64 Sone sone = mocks.mockSone("SoneId").create();
65 Sone otherSone = mocks.mockSone("OtherSoneId").create();
66 Post post = mocks.mockPost(sone, "PostId").withRecipient(otherSone.getId()).withTime(1000L).withText("Text of the post.").create();
67 SimpleFieldSet getPostFieldSet = new SimpleFieldSetBuilder()
68 .put("Message", "GetPost")
69 .put("Post", "PostId")
71 Response response = getPostCommand.execute(getPostFieldSet, null, DIRECT);
72 verifyAnswer(response, "Post");
73 verifyPostWithReplies(response.getReplyParameters(), "Post.", post);
77 public void verifyThatGettingAPostWithRepliesWorks() throws FcpException, FSParseException {
78 Sone sone = mocks.mockSone("SoneId").create();
79 Post post = mocks.mockPost(sone, "PostId").withTime(1000L).withText("Text of the post.").create();
80 PostReply postReply1 = mocks.mockPostReply(sone, "Reply1").create();
81 when(postReply1.getText()).thenReturn("Reply 1.");
82 PostReply postReply2 = mocks.mockPostReply(sone, "Reply2").create();
83 when(postReply2.getText()).thenReturn("Reply 2.");
84 when(post.getReplies()).thenReturn(asList(postReply1, postReply2));
85 SimpleFieldSet getPostFieldSet = new SimpleFieldSetBuilder()
86 .put("Message", "GetPost")
87 .put("Post", "PostId")
88 .put("IncludeReplies", "true")
90 Response response = getPostCommand.execute(getPostFieldSet, null, DIRECT);
91 verifyAnswer(response, "Post");
92 verifyPostWithReplies(response.getReplyParameters(), "Post.", post);
95 @Test(expected = FcpException.class)
96 public void verifyThatGettingANonExistingPostCausesAnError() throws FcpException {
97 SimpleFieldSet getPostFieldSet = new SimpleFieldSetBuilder()
98 .put("Message", "GetPost")
99 .put("Post", "PostId")
101 getPostCommand.execute(getPostFieldSet, null, DIRECT);
104 @Test(expected = FcpException.class)
105 public void verifyThatAMissingPostIdCausesAnError() throws FcpException {
106 SimpleFieldSet getPostFieldSet = new SimpleFieldSetBuilder()
107 .put("Message", "GetPost")
109 getPostCommand.execute(getPostFieldSet, null, DIRECT);