e27214820c0e3fed5ce249ab2b22bfff44e10d27
[Sone.git] / src / test / java / net / pterodactylus / sone / fcp / GetPostFeedCommandTest.java
1 /*
2  * Sone - GetPostFeedCommandTest.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.fcp;
19
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.verifyAnswer;
24 import static net.pterodactylus.sone.fcp.Verifiers.verifyPostsWithReplies;
25 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
26
27 import java.util.Collections;
28
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;
36
37 import freenet.node.FSParseException;
38 import freenet.support.SimpleFieldSet;
39
40 import org.junit.Before;
41 import org.junit.Test;
42
43 /**
44  * Unit test for {@link  GetPostFeedCommand}.
45  *
46  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47  */
48 public class GetPostFeedCommandTest {
49
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;
60
61         @Before
62         public void setup() {
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();
72         }
73
74         @Test
75         public void getFeedForLocalSone() throws FcpException, FSParseException {
76                 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
77                                 .put("Message", "GetPostFeed")
78                                 .put("Sone", "LSone")
79                                 .get();
80                 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
81                 verifyAnswer(response, "PostFeed");
82                 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", asList(localPost, friendPost, remotePost));
83         }
84
85         @Test
86         public void getFeedForLocalSoneSkippingTheFirstPost() throws FcpException, FSParseException {
87                 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
88                                 .put("Message", "GetPostFeed")
89                                 .put("Sone", "LSone")
90                                 .put("StartPost", "1")
91                                 .get();
92                 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
93                 verifyAnswer(response, "PostFeed");
94                 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", asList(friendPost, remotePost));
95         }
96
97         @Test
98         public void getFeedForLocalSoneWithTwoPosts() throws FcpException, FSParseException {
99                 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
100                                 .put("Message", "GetPostFeed")
101                                 .put("Sone", "LSone")
102                                 .put("MaxPosts", "2")
103                                 .get();
104                 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
105                 verifyAnswer(response, "PostFeed");
106                 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", asList(localPost, friendPost));
107         }
108
109         @Test
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")
116                                 .get();
117                 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
118                 verifyAnswer(response, "PostFeed");
119                 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", asList(friendPost));
120         }
121
122         @Test
123         public void getFeedForLocalSoneSkippingMorePostsThanThereAre() throws FcpException, FSParseException {
124                 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
125                                 .put("Message", "GetPostFeed")
126                                 .put("Sone", "LSone")
127                                 .put("StartPost", "10")
128                                 .get();
129                 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
130                 verifyAnswer(response, "PostFeed");
131                 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", Collections.<Post>emptyList());
132         }
133
134         @Test(expected = FcpException.class)
135         public void getFeedWithoutLocalSone() throws FcpException, FSParseException {
136                 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
137                                 .put("Message", "GetPostFeed")
138                                 .get();
139                 getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
140         }
141
142         private long daysBefore(int days) {
143                 return currentTimeMillis() - DAYS.toMillis(days);
144         }
145
146 }