2 * Sone - GetPostFeedCommandTest.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.lang.System.currentTimeMillis;
21 import static java.util.Arrays.asList;
22 import static java.util.concurrent.TimeUnit.DAYS;
23 import static net.pterodactylus.sone.Verifiers.verifyAnswer;
24 import static net.pterodactylus.sone.Verifiers.verifyPostsWithReplies;
25 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
27 import java.util.Collections;
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 org.junit.Before;
41 import org.junit.Test;
44 * Unit test for {@link GetPostFeedCommand}.
46 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
48 public class GetPostFeedCommandTest {
50 private final Mocks mocks = new Mocks();
51 private final GetPostFeedCommand getPostFeedCommand = new GetPostFeedCommand(mocks.core);
52 private Sone remoteSone;
53 private Sone friendSone;
54 private Sone localSone;
55 private Post localPost;
56 private PostReply friendReplyToLocalPost;
57 private PostReply remoteReplyToLocalPost;
58 private Post friendPost;
59 private Post remotePost;
63 remoteSone = mocks.mockSone("RSone").create();
64 friendSone = mocks.mockSone("FSone").create();
65 localSone = mocks.mockSone("LSone").local().withFriends(asList(friendSone.getId(), "NonExistingSone")).create();
66 localPost = mocks.mockPost(localSone, "LPost").withTime(daysBefore(11)).withText("My post.").create();
67 friendReplyToLocalPost = mocks.mockPostReply(friendSone, "FReply").inReplyTo(localPost).withTime(daysBefore(9)).withText("No.").create();
68 remoteReplyToLocalPost = mocks.mockPostReply(remoteSone, "RReply").inReplyTo(localPost).withTime(daysBefore(7)).withText("Yes.").create();
69 mocks.mockPostReply(remoteSone, "FutureReply1").inReplyTo(localPost).withTime(daysBefore(-1)).withText("Future!").create();
70 friendPost = mocks.mockPost(friendSone, "FPost").withTime(daysBefore(12)).withText("The friend's post.").create();
71 remotePost = mocks.mockPost(remoteSone, "RPost").withTime(daysBefore(13)).withText("Hello, LSone!").withRecipient(localSone.getId()).create();
75 public void getFeedForLocalSone() throws FcpException, FSParseException {
76 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
77 .put("Message", "GetPostFeed")
80 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
81 verifyAnswer(response, "PostFeed");
82 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", asList(localPost, friendPost, remotePost));
86 public void getFeedForLocalSoneSkippingTheFirstPost() throws FcpException, FSParseException {
87 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
88 .put("Message", "GetPostFeed")
90 .put("StartPost", "1")
92 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
93 verifyAnswer(response, "PostFeed");
94 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", asList(friendPost, remotePost));
98 public void getFeedForLocalSoneWithTwoPosts() throws FcpException, FSParseException {
99 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
100 .put("Message", "GetPostFeed")
101 .put("Sone", "LSone")
102 .put("MaxPosts", "2")
104 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
105 verifyAnswer(response, "PostFeed");
106 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", asList(localPost, friendPost));
110 public void getFeedForLocalSoneWithOnePostSkippingTheFirst() throws FcpException, FSParseException {
111 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
112 .put("Message", "GetPostFeed")
113 .put("Sone", "LSone")
114 .put("MaxPosts", "1")
115 .put("StartPost", "1")
117 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
118 verifyAnswer(response, "PostFeed");
119 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", asList(friendPost));
123 public void getFeedForLocalSoneSkippingMorePostsThanThereAre() throws FcpException, FSParseException {
124 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
125 .put("Message", "GetPostFeed")
126 .put("Sone", "LSone")
127 .put("StartPost", "10")
129 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
130 verifyAnswer(response, "PostFeed");
131 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", Collections.<Post>emptyList());
134 @Test(expected = FcpException.class)
135 public void getFeedWithoutLocalSone() throws FcpException, FSParseException {
136 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
137 .put("Message", "GetPostFeed")
139 getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
142 private long daysBefore(int days) {
143 return currentTimeMillis() - DAYS.toMillis(days);