Use different method to verify a number of replies.
[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.verifyPostReplies;
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(), "NonExistingSone")).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                 verifyPostReplies(response.getReplyParameters(), "Posts.0.Replies.", asList(friendReplyToLocalPost, remoteReplyToLocalPost));
88                 verifyPost(response.getReplyParameters(), "Posts.1.", friendPost);
89                 verifyPost(response.getReplyParameters(), "Posts.2.", remotePost);
90         }
91
92         @Test
93         public void getFeedForLocalSoneSkippingTheFirstPost() throws FcpException, FSParseException {
94                 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
95                                 .put("Message", "GetPostFeed")
96                                 .put("Sone", "LSone")
97                                 .put("StartPost", "1")
98                                 .get();
99                 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
100                 assertThat(response, notNullValue());
101                 assertThat(response.getReplyParameters(), notNullValue());
102                 assertThat(response.getReplyParameters().get("Message"), is("PostFeed"));
103                 assertThat(response.getReplyParameters().getInt("Posts.Count"), is(2));
104                 verifyPost(response.getReplyParameters(), "Posts.0.", friendPost);
105                 verifyPost(response.getReplyParameters(), "Posts.1.", remotePost);
106         }
107
108         @Test
109         public void getFeedForLocalSoneWithTwoPosts() throws FcpException, FSParseException {
110                 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
111                                 .put("Message", "GetPostFeed")
112                                 .put("Sone", "LSone")
113                                 .put("MaxPosts", "2")
114                                 .get();
115                 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
116                 assertThat(response, notNullValue());
117                 assertThat(response.getReplyParameters(), notNullValue());
118                 assertThat(response.getReplyParameters().get("Message"), is("PostFeed"));
119                 assertThat(response.getReplyParameters().getInt("Posts.Count"), is(2));
120                 verifyPost(response.getReplyParameters(), "Posts.0.", localPost);
121                 verifyPostReplies(response.getReplyParameters(), "Posts.0.Replies.", asList(friendReplyToLocalPost, remoteReplyToLocalPost));
122                 verifyPost(response.getReplyParameters(), "Posts.1.", friendPost);
123         }
124
125         @Test
126         public void getFeedForLocalSoneWithOnePostSkippingTheFirst() throws FcpException, FSParseException {
127                 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
128                                 .put("Message", "GetPostFeed")
129                                 .put("Sone", "LSone")
130                                 .put("MaxPosts", "1")
131                                 .put("StartPost", "1")
132                                 .get();
133                 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
134                 assertThat(response, notNullValue());
135                 assertThat(response.getReplyParameters(), notNullValue());
136                 assertThat(response.getReplyParameters().get("Message"), is("PostFeed"));
137                 assertThat(response.getReplyParameters().getInt("Posts.Count"), is(1));
138                 verifyPost(response.getReplyParameters(), "Posts.0.", friendPost);
139         }
140
141         @Test
142         public void getFeedForLocalSoneSkippingMorePostsThanThereAre() throws FcpException, FSParseException {
143                 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
144                                 .put("Message", "GetPostFeed")
145                                 .put("Sone", "LSone")
146                                 .put("StartPost", "10")
147                                 .get();
148                 Response response = getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
149                 assertThat(response, notNullValue());
150                 assertThat(response.getReplyParameters(), notNullValue());
151                 assertThat(response.getReplyParameters().get("Message"), is("PostFeed"));
152                 assertThat(response.getReplyParameters().getInt("Posts.Count"), is(0));
153         }
154
155         @Test(expected = FcpException.class)
156         public void getFeedWithoutLocalSone() throws FcpException, FSParseException {
157                 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
158                                 .put("Message", "GetPostFeed")
159                                 .get();
160                 getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
161         }
162
163         private long daysBefore(int days) {
164                 return currentTimeMillis() - DAYS.toMillis(days);
165         }
166
167 }