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;
27 import net.pterodactylus.sone.data.Mocks;
28 import net.pterodactylus.sone.data.Post;
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 GetPostsCommand}.
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class GetPostsCommandTest {
46 private final Mocks mocks = new Mocks();
47 private final GetPostsCommand getPostsCommand = new GetPostsCommand(mocks.core);
50 public void multiplePostsAreReturnedCorrectly() throws FcpException, FSParseException {
51 Sone sone = mocks.mockSone("SoneId").create();
52 Post post1 = mocks.mockPost(sone, "Post1").withTime(1000L).withText("1").create();
53 Post post2 = mocks.mockPost(sone, "Post2").withTime(2000L).withText("1").create();
54 Post post3 = mocks.mockPost(sone, "Post3").withTime(3000L).withText("1").create();
55 Sone otherSone = mocks.mockSone("OtherSone").create();
56 mocks.mockPostReply(otherSone, "Reply1").inReplyTo(post1).withText("R").create();
57 SimpleFieldSet getPostsFieldSet = new SimpleFieldSetBuilder()
58 .put("Message", "GetPosts")
59 .put("Sone", "SoneId")
61 Response response = getPostsCommand.execute(getPostsFieldSet, null, DIRECT);
62 verifyAnswer(response, "Posts");
63 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", asList(post3, post2, post1));
67 public void skippingMorePostsThanThereAreReturnsAnEmptyList() throws FcpException, FSParseException {
68 Sone sone = mocks.mockSone("SoneId").create();
69 Post post1 = mocks.mockPost(sone, "Post1").withTime(1000L).withText("1").create();
70 Post post2 = mocks.mockPost(sone, "Post2").withTime(2000L).withText("1").create();
71 Post post3 = mocks.mockPost(sone, "Post3").withTime(3000L).withText("1").create();
72 Sone otherSone = mocks.mockSone("OtherSone").create();
73 mocks.mockPostReply(otherSone, "Reply1").inReplyTo(post1).withText("R").create();
74 SimpleFieldSet getPostsFieldSet = new SimpleFieldSetBuilder()
75 .put("Message", "GetPosts")
76 .put("Sone", "SoneId")
77 .put("StartPost", "5")
79 Response response = getPostsCommand.execute(getPostsFieldSet, null, DIRECT);
80 verifyAnswer(response, "Posts");
81 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", Collections.<Post>emptyList());
85 public void getOnlyOnePostAndSkipTheFirstPost() throws FcpException, FSParseException {
86 Sone sone = mocks.mockSone("SoneId").create();
87 Post post1 = mocks.mockPost(sone, "Post1").withTime(1000L).withText("1").create();
88 Post post2 = mocks.mockPost(sone, "Post2").withTime(2000L).withText("1").create();
89 Post post3 = mocks.mockPost(sone, "Post3").withTime(3000L).withText("1").create();
90 Sone otherSone = mocks.mockSone("OtherSone").create();
91 mocks.mockPostReply(otherSone, "Reply1").inReplyTo(post1).withText("R").create();
92 SimpleFieldSet getPostsFieldSet = new SimpleFieldSetBuilder()
93 .put("Message", "GetPosts")
94 .put("Sone", "SoneId")
96 .put("StartPost", "1")
98 Response response = getPostsCommand.execute(getPostsFieldSet, null, DIRECT);
99 verifyAnswer(response, "Posts");
100 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", asList(post2));
104 public void getOnlyTheFirstTwoPosts() throws FcpException, FSParseException {
105 Sone sone = mocks.mockSone("SoneId").create();
106 Post post1 = mocks.mockPost(sone, "Post1").withTime(1000L).withText("1").create();
107 Post post2 = mocks.mockPost(sone, "Post2").withTime(2000L).withText("1").create();
108 Post post3 = mocks.mockPost(sone, "Post3").withTime(3000L).withText("1").create();
109 Sone otherSone = mocks.mockSone("OtherSone").create();
110 mocks.mockPostReply(otherSone, "Reply1").inReplyTo(post1).withText("R").create();
111 SimpleFieldSet getPostsFieldSet = new SimpleFieldSetBuilder()
112 .put("Message", "GetPosts")
113 .put("Sone", "SoneId")
114 .put("MaxPosts", "2")
116 Response response = getPostsCommand.execute(getPostsFieldSet, null, DIRECT);
117 verifyAnswer(response, "Posts");
118 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", asList(post3, post2));
122 public void skipTheFirstPost() throws FcpException, FSParseException {
123 Sone sone = mocks.mockSone("SoneId").create();
124 Post post1 = mocks.mockPost(sone, "Post1").withTime(1000L).withText("1").create();
125 Post post2 = mocks.mockPost(sone, "Post2").withTime(2000L).withText("1").create();
126 Post post3 = mocks.mockPost(sone, "Post3").withTime(3000L).withText("1").create();
127 Sone otherSone = mocks.mockSone("OtherSone").create();
128 mocks.mockPostReply(otherSone, "Reply1").inReplyTo(post1).withText("R").create();
129 SimpleFieldSet getPostsFieldSet = new SimpleFieldSetBuilder()
130 .put("Message", "GetPosts")
131 .put("Sone", "SoneId")
132 .put("StartPost", "1")
134 Response response = getPostsCommand.execute(getPostsFieldSet, null, DIRECT);
135 verifyAnswer(response, "Posts");
136 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", asList(post2, post1));
139 @Test(expected = FcpException.class)
140 public void aMissingSoneCausesAnError() throws FcpException {
141 SimpleFieldSet getPostsFieldSet = new SimpleFieldSetBuilder()
142 .put("Message", "GetPosts")
144 getPostsCommand.execute(getPostsFieldSet, null, DIRECT);