Add unit test for GetPostFeedCommand.
[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.verifyPost;
24 import static net.pterodactylus.sone.fcp.Verifiers.verifyPostReply;
25 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
26 import static org.hamcrest.MatcherAssert.assertThat;
27 import static org.hamcrest.Matchers.is;
28 import static org.hamcrest.Matchers.notNullValue;
29
30 import net.pterodactylus.sone.data.Mocks;
31 import net.pterodactylus.sone.data.Post;
32 import net.pterodactylus.sone.data.PostReply;
33 import net.pterodactylus.sone.data.Sone;
34 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
35 import net.pterodactylus.sone.freenet.fcp.Command.Response;
36 import net.pterodactylus.sone.freenet.fcp.FcpException;
37
38 import freenet.node.FSParseException;
39 import freenet.support.SimpleFieldSet;
40
41 import org.junit.Before;
42 import org.junit.Test;
43
44 /**
45  * Unit test for {@link  GetPostFeedCommand}.
46  *
47  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
48  */
49 public class GetPostFeedCommandTest {
50
51         private final Mocks mocks = new Mocks();
52         private final GetPostFeedCommand getPostFeedCommand = new GetPostFeedCommand(mocks.core);
53         private Sone remoteSone;
54         private Sone friendSone;
55         private Sone localSone;
56         private Post localPost;
57         private PostReply friendReplyToLocalPost;
58         private PostReply remoteReplyToLocalPost;
59         private Post friendPost;
60         private Post remotePost;
61
62         @Before
63         public void setup() {
64                 remoteSone = mocks.mockSone("RSone").create();
65                 friendSone = mocks.mockSone("FSone").create();
66                 localSone = mocks.mockSone("LSone").local().withFriends(asList(friendSone.getId())).create();
67                 localPost = mocks.mockPost(localSone, "LPost").withTime(daysBefore(11)).withText("My post.").create();
68                 friendReplyToLocalPost = mocks.mockPostReply(friendSone, "FReply").inReplyTo(localPost).withTime(daysBefore(9)).withText("No.").create();
69                 remoteReplyToLocalPost = mocks.mockPostReply(remoteSone, "RReply").inReplyTo(localPost).withTime(daysBefore(7)).withText("Yes.").create();
70                 mocks.mockPostReply(remoteSone, "FutureReply1").inReplyTo(localPost).withTime(daysBefore(-1)).withText("Future!").create();
71                 friendPost = mocks.mockPost(friendSone, "FPost").withTime(daysBefore(12)).withText("The friend's post.").create();
72                 remotePost = mocks.mockPost(remoteSone, "RPost").withTime(daysBefore(13)).withText("Hello, LSone!").withRecipient(localSone.getId()).create();
73         }
74
75         @Test
76         public void getFeedForLocalSone() throws FcpException, FSParseException {
77                 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
78                                 .put("Message", "GetPostFeed")
79                                 .put("Sone", "LSone")
80                                 .get();
81                 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
82                 assertThat(response, notNullValue());
83                 assertThat(response.getReplyParameters(), notNullValue());
84                 assertThat(response.getReplyParameters().get("Message"), is("PostFeed"));
85                 assertThat(response.getReplyParameters().getInt("Posts.Count"), is(3));
86                 verifyPost(response.getReplyParameters(), "Posts.0.", localPost);
87                 assertThat(response.getReplyParameters().getInt("Posts.0.Replies.Count"), is(2));
88                 verifyPostReply(response.getReplyParameters(), "Posts.0.Replies.0.", friendReplyToLocalPost);
89                 verifyPostReply(response.getReplyParameters(), "Posts.0.Replies.1.", remoteReplyToLocalPost);
90                 verifyPost(response.getReplyParameters(), "Posts.1.", friendPost);
91                 verifyPost(response.getReplyParameters(), "Posts.2.", remotePost);
92         }
93
94         @Test
95         public void getFeedForLocalSoneSkippingTheFirstPost() throws FcpException, FSParseException {
96                 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
97                                 .put("Message", "GetPostFeed")
98                                 .put("Sone", "LSone")
99                                 .put("StartPost", "1")
100                                 .get();
101                 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
102                 assertThat(response, notNullValue());
103                 assertThat(response.getReplyParameters(), notNullValue());
104                 assertThat(response.getReplyParameters().get("Message"), is("PostFeed"));
105                 assertThat(response.getReplyParameters().getInt("Posts.Count"), is(2));
106                 verifyPost(response.getReplyParameters(), "Posts.0.", friendPost);
107                 verifyPost(response.getReplyParameters(), "Posts.1.", remotePost);
108         }
109
110         @Test
111         public void getFeedForLocalSoneWithTwoPosts() throws FcpException, FSParseException {
112                 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
113                                 .put("Message", "GetPostFeed")
114                                 .put("Sone", "LSone")
115                                 .put("MaxPosts", "2")
116                                 .get();
117                 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
118                 assertThat(response, notNullValue());
119                 assertThat(response.getReplyParameters(), notNullValue());
120                 assertThat(response.getReplyParameters().get("Message"), is("PostFeed"));
121                 assertThat(response.getReplyParameters().getInt("Posts.Count"), is(2));
122                 verifyPost(response.getReplyParameters(), "Posts.0.", localPost);
123                 assertThat(response.getReplyParameters().getInt("Posts.0.Replies.Count"), is(2));
124                 verifyPostReply(response.getReplyParameters(), "Posts.0.Replies.0.", friendReplyToLocalPost);
125                 verifyPostReply(response.getReplyParameters(), "Posts.0.Replies.1.", remoteReplyToLocalPost);
126                 verifyPost(response.getReplyParameters(), "Posts.1.", friendPost);
127         }
128
129         @Test
130         public void getFeedForLocalSoneWithOnePostSkippingTheFirst() throws FcpException, FSParseException {
131                 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
132                                 .put("Message", "GetPostFeed")
133                                 .put("Sone", "LSone")
134                                 .put("MaxPosts", "1")
135                                 .put("StartPost", "1")
136                                 .get();
137                 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
138                 assertThat(response, notNullValue());
139                 assertThat(response.getReplyParameters(), notNullValue());
140                 assertThat(response.getReplyParameters().get("Message"), is("PostFeed"));
141                 assertThat(response.getReplyParameters().getInt("Posts.Count"), is(1));
142                 verifyPost(response.getReplyParameters(), "Posts.0.", friendPost);
143         }
144
145         @Test
146         public void getFeedForLocalSoneSkippingMorePostsThanThereAre() throws FcpException, FSParseException {
147                 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
148                                 .put("Message", "GetPostFeed")
149                                 .put("Sone", "LSone")
150                                 .put("StartPost", "10")
151                                 .get();
152                 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
153                 assertThat(response, notNullValue());
154                 assertThat(response.getReplyParameters(), notNullValue());
155                 assertThat(response.getReplyParameters().get("Message"), is("PostFeed"));
156                 assertThat(response.getReplyParameters().getInt("Posts.Count"), is(0));
157         }
158
159         @Test(expected = FcpException.class)
160         public void getFeedWithoutLocalSone() throws FcpException, FSParseException {
161                 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
162                                 .put("Message", "GetPostFeed")
163                                 .get();
164                 getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
165         }
166
167         private long daysBefore(int days) {
168                 return currentTimeMillis() - DAYS.toMillis(days);
169         }
170
171 }