2 * Sone - GetPostsCommandTest.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.fcp.Verifiers.verifyAnswer;
22 import static net.pterodactylus.sone.fcp.Verifiers.verifyPostsWithReplies;
23 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
25 import java.util.Collections;
26 import java.util.List;
28 import net.pterodactylus.sone.data.Mocks;
29 import net.pterodactylus.sone.data.Post;
30 import net.pterodactylus.sone.data.Sone;
31 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
32 import net.pterodactylus.sone.freenet.fcp.Command.Response;
33 import net.pterodactylus.sone.freenet.fcp.FcpException;
35 import freenet.node.FSParseException;
36 import freenet.support.SimpleFieldSet;
38 import org.junit.Test;
41 * Unit test for {@link GetPostsCommand}.
43 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 public class GetPostsCommandTest {
47 private final Mocks mocks = new Mocks();
48 private final GetPostsCommand getPostsCommand = new GetPostsCommand(mocks.core);
51 public void multiplePostsAreReturnedCorrectly() throws FcpException, FSParseException {
52 PreparedPosts preparedPosts = new PreparedPosts().invoke();
53 SimpleFieldSet getPostsFieldSet = new SimpleFieldSetBuilder()
54 .put("Message", "GetPosts")
55 .put("Sone", "SoneId")
57 Response response = getPostsCommand.execute(getPostsFieldSet, null, DIRECT);
58 verifyAnswer(response, "Posts");
59 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", preparedPosts.getPosts(0, 3));
63 public void skippingMorePostsThanThereAreReturnsAnEmptyList() throws FcpException, FSParseException {
64 new PreparedPosts().invoke();
65 SimpleFieldSet getPostsFieldSet = new SimpleFieldSetBuilder()
66 .put("Message", "GetPosts")
67 .put("Sone", "SoneId")
68 .put("StartPost", "5")
70 Response response = getPostsCommand.execute(getPostsFieldSet, null, DIRECT);
71 verifyAnswer(response, "Posts");
72 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", Collections.<Post>emptyList());
76 public void getOnlyOnePostAndSkipTheFirstPost() throws FcpException, FSParseException {
77 PreparedPosts preparedPosts = new PreparedPosts().invoke();
78 SimpleFieldSet getPostsFieldSet = new SimpleFieldSetBuilder()
79 .put("Message", "GetPosts")
80 .put("Sone", "SoneId")
82 .put("StartPost", "1")
84 Response response = getPostsCommand.execute(getPostsFieldSet, null, DIRECT);
85 verifyAnswer(response, "Posts");
86 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", preparedPosts.getPosts(1, 1));
90 public void getOnlyTheFirstTwoPosts() throws FcpException, FSParseException {
91 PreparedPosts preparedPosts = new PreparedPosts().invoke();
92 SimpleFieldSet getPostsFieldSet = new SimpleFieldSetBuilder()
93 .put("Message", "GetPosts")
94 .put("Sone", "SoneId")
97 Response response = getPostsCommand.execute(getPostsFieldSet, null, DIRECT);
98 verifyAnswer(response, "Posts");
99 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", preparedPosts.getPosts(0, 2));
103 public void skipTheFirstPost() throws FcpException, FSParseException {
104 PreparedPosts preparedPosts = new PreparedPosts().invoke();
105 SimpleFieldSet getPostsFieldSet = new SimpleFieldSetBuilder()
106 .put("Message", "GetPosts")
107 .put("Sone", "SoneId")
108 .put("StartPost", "1")
110 Response response = getPostsCommand.execute(getPostsFieldSet, null, DIRECT);
111 verifyAnswer(response, "Posts");
112 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", preparedPosts.getPosts(1, 2));
115 @Test(expected = FcpException.class)
116 public void aMissingSoneCausesAnError() throws FcpException {
117 SimpleFieldSet getPostsFieldSet = new SimpleFieldSetBuilder()
118 .put("Message", "GetPosts")
120 getPostsCommand.execute(getPostsFieldSet, null, DIRECT);
123 private class PreparedPosts {
129 public List<Post> getPosts(int startPost, int maxPosts) {
130 return asList(post3, post2, post1).subList(startPost, startPost + maxPosts);
133 public PreparedPosts invoke() {
134 Sone sone = mocks.mockSone("SoneId").create();
135 post1 = mocks.mockPost(sone, "Post1").withTime(1000L).withText("1").create();
136 post2 = mocks.mockPost(sone, "Post2").withTime(2000L).withText("1").create();
137 post3 = mocks.mockPost(sone, "Post3").withTime(3000L).withText("1").create();
138 Sone otherSone = mocks.mockSone("OtherSone").create();
139 mocks.mockPostReply(otherSone, "Reply1").inReplyTo(post1).withText("R").create();