Use yet another way to verify the posts.
[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.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;
28
29 import java.util.Collections;
30
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;
38
39 import freenet.node.FSParseException;
40 import freenet.support.SimpleFieldSet;
41
42 import org.junit.Before;
43 import org.junit.Test;
44
45 /**
46  * Unit test for {@link  GetPostFeedCommand}.
47  *
48  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49  */
50 public class GetPostFeedCommandTest {
51
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;
62
63         @Before
64         public void setup() {
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();
74         }
75
76         @Test
77         public void getFeedForLocalSone() throws FcpException, FSParseException {
78                 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
79                                 .put("Message", "GetPostFeed")
80                                 .put("Sone", "LSone")
81                                 .get();
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));
87         }
88
89         @Test
90         public void getFeedForLocalSoneSkippingTheFirstPost() throws FcpException, FSParseException {
91                 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
92                                 .put("Message", "GetPostFeed")
93                                 .put("Sone", "LSone")
94                                 .put("StartPost", "1")
95                                 .get();
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));
101         }
102
103         @Test
104         public void getFeedForLocalSoneWithTwoPosts() throws FcpException, FSParseException {
105                 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
106                                 .put("Message", "GetPostFeed")
107                                 .put("Sone", "LSone")
108                                 .put("MaxPosts", "2")
109                                 .get();
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));
115         }
116
117         @Test
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")
124                                 .get();
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));
130         }
131
132         @Test
133         public void getFeedForLocalSoneSkippingMorePostsThanThereAre() throws FcpException, FSParseException {
134                 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
135                                 .put("Message", "GetPostFeed")
136                                 .put("Sone", "LSone")
137                                 .put("StartPost", "10")
138                                 .get();
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());
144         }
145
146         @Test(expected = FcpException.class)
147         public void getFeedWithoutLocalSone() throws FcpException, FSParseException {
148                 SimpleFieldSet getPostFeedFieldSet = new SimpleFieldSetBuilder()
149                                 .put("Message", "GetPostFeed")
150                                 .get();
151                 getPostFeedCommand.execute(getPostFeedFieldSet, null, DIRECT);
152         }
153
154         private long daysBefore(int days) {
155                 return currentTimeMillis() - DAYS.toMillis(days);
156         }
157
158 }