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.fcp.Verifiers.verifyPostsWithReplies;
24 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.hamcrest.Matchers.is;
27 import static org.hamcrest.Matchers.notNullValue;
29 import java.util.Collections;
31 import net.pterodactylus.sone.data.Mocks;
32 import net.pterodactylus.sone.data.Post;
33 import net.pterodactylus.sone.data.PostReply;
34 import net.pterodactylus.sone.data.Sone;
35 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
36 import net.pterodactylus.sone.freenet.fcp.Command.Response;
37 import net.pterodactylus.sone.freenet.fcp.FcpException;
39 import freenet.node.FSParseException;
40 import freenet.support.SimpleFieldSet;
42 import org.junit.Before;
43 import org.junit.Test;
46 * Unit test for {@link GetPostFeedCommand}.
48 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
50 public class GetPostFeedCommandTest {
52 private final Mocks mocks = new Mocks();
53 private final GetPostFeedCommand getPostFeedCommand = new GetPostFeedCommand(mocks.core);
54 private Sone remoteSone;
55 private Sone friendSone;
56 private Sone localSone;
57 private Post localPost;
58 private PostReply friendReplyToLocalPost;
59 private PostReply remoteReplyToLocalPost;
60 private Post friendPost;
61 private Post remotePost;
65 remoteSone = mocks.mockSone("RSone").create();
66 friendSone = mocks.mockSone("FSone").create();
67 localSone = mocks.mockSone("LSone").local().withFriends(asList(friendSone.getId(), "NonExistingSone")).create();
68 localPost = mocks.mockPost(localSone, "LPost").withTime(daysBefore(11)).withText("My post.").create();
69 friendReplyToLocalPost = mocks.mockPostReply(friendSone, "FReply").inReplyTo(localPost).withTime(daysBefore(9)).withText("No.").create();
70 remoteReplyToLocalPost = mocks.mockPostReply(remoteSone, "RReply").inReplyTo(localPost).withTime(daysBefore(7)).withText("Yes.").create();
71 mocks.mockPostReply(remoteSone, "FutureReply1").inReplyTo(localPost).withTime(daysBefore(-1)).withText("Future!").create();
72 friendPost = mocks.mockPost(friendSone, "FPost").withTime(daysBefore(12)).withText("The friend's post.").create();
73 remotePost = mocks.mockPost(remoteSone, "RPost").withTime(daysBefore(13)).withText("Hello, LSone!").withRecipient(localSone.getId()).create();
77 public void getFeedForLocalSone() throws FcpException, FSParseException {
78 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
79 .put("Message", "GetPostFeed")
82 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
83 assertThat(response, notNullValue());
84 assertThat(response.getReplyParameters(), notNullValue());
85 assertThat(response.getReplyParameters().get("Message"), is("PostFeed"));
86 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", asList(localPost, friendPost, remotePost));
90 public void getFeedForLocalSoneSkippingTheFirstPost() throws FcpException, FSParseException {
91 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
92 .put("Message", "GetPostFeed")
94 .put("StartPost", "1")
96 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
97 assertThat(response, notNullValue());
98 assertThat(response.getReplyParameters(), notNullValue());
99 assertThat(response.getReplyParameters().get("Message"), is("PostFeed"));
100 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", asList(friendPost, remotePost));
104 public void getFeedForLocalSoneWithTwoPosts() throws FcpException, FSParseException {
105 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
106 .put("Message", "GetPostFeed")
107 .put("Sone", "LSone")
108 .put("MaxPosts", "2")
110 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
111 assertThat(response, notNullValue());
112 assertThat(response.getReplyParameters(), notNullValue());
113 assertThat(response.getReplyParameters().get("Message"), is("PostFeed"));
114 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", asList(localPost, friendPost));
118 public void getFeedForLocalSoneWithOnePostSkippingTheFirst() throws FcpException, FSParseException {
119 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
120 .put("Message", "GetPostFeed")
121 .put("Sone", "LSone")
122 .put("MaxPosts", "1")
123 .put("StartPost", "1")
125 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
126 assertThat(response, notNullValue());
127 assertThat(response.getReplyParameters(), notNullValue());
128 assertThat(response.getReplyParameters().get("Message"), is("PostFeed"));
129 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", asList(friendPost));
133 public void getFeedForLocalSoneSkippingMorePostsThanThereAre() throws FcpException, FSParseException {
134 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
135 .put("Message", "GetPostFeed")
136 .put("Sone", "LSone")
137 .put("StartPost", "10")
139 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
140 assertThat(response, notNullValue());
141 assertThat(response.getReplyParameters(), notNullValue());
142 assertThat(response.getReplyParameters().get("Message"), is("PostFeed"));
143 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", Collections.<Post>emptyList());
146 @Test(expected = FcpException.class)
147 public void getFeedWithoutLocalSone() throws FcpException, FSParseException {
148 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
149 .put("Message", "GetPostFeed")
151 getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
154 private long daysBefore(int days) {
155 return currentTimeMillis() - DAYS.toMillis(days);