Add unit test for GetPostsCommand.
[Sone.git] / src / test / java / net / pterodactylus / sone / fcp / GetPostsCommandTest.java
1 /*
2  * Sone - GetPostsCommandTest.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.util.Arrays.asList;
21 import static net.pterodactylus.sone.fcp.Verifiers.verifyAnswer;
22 import static net.pterodactylus.sone.fcp.Verifiers.verifyPostsWithReplies;
23 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
24
25 import java.util.Collections;
26
27 import net.pterodactylus.sone.data.Mocks;
28 import net.pterodactylus.sone.data.Post;
29 import net.pterodactylus.sone.data.Sone;
30 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
31 import net.pterodactylus.sone.freenet.fcp.Command.Response;
32 import net.pterodactylus.sone.freenet.fcp.FcpException;
33
34 import freenet.node.FSParseException;
35 import freenet.support.SimpleFieldSet;
36
37 import org.junit.Test;
38
39 /**
40  * Unit test for {@link GetPostsCommand}.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class GetPostsCommandTest {
45
46         private final Mocks mocks = new Mocks();
47         private final GetPostsCommand getPostsCommand = new GetPostsCommand(mocks.core);
48
49         @Test
50         public void multiplePostsAreReturnedCorrectly() throws FcpException, FSParseException {
51                 Sone sone = mocks.mockSone("SoneId").create();
52                 Post post1 = mocks.mockPost(sone, "Post1").withTime(1000L).withText("1").create();
53                 Post post2 = mocks.mockPost(sone, "Post2").withTime(2000L).withText("1").create();
54                 Post post3 = mocks.mockPost(sone, "Post3").withTime(3000L).withText("1").create();
55                 Sone otherSone = mocks.mockSone("OtherSone").create();
56                 mocks.mockPostReply(otherSone, "Reply1").inReplyTo(post1).withText("R").create();
57                 SimpleFieldSet getPostsFieldSet = new SimpleFieldSetBuilder()
58                                 .put("Message", "GetPosts")
59                                 .put("Sone", "SoneId")
60                                 .get();
61                 Response response = getPostsCommand.execute(getPostsFieldSet, null, DIRECT);
62                 verifyAnswer(response, "Posts");
63                 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", asList(post3, post2, post1));
64         }
65
66         @Test
67         public void skippingMorePostsThanThereAreReturnsAnEmptyList() throws FcpException, FSParseException {
68                 Sone sone = mocks.mockSone("SoneId").create();
69                 Post post1 = mocks.mockPost(sone, "Post1").withTime(1000L).withText("1").create();
70                 Post post2 = mocks.mockPost(sone, "Post2").withTime(2000L).withText("1").create();
71                 Post post3 = mocks.mockPost(sone, "Post3").withTime(3000L).withText("1").create();
72                 Sone otherSone = mocks.mockSone("OtherSone").create();
73                 mocks.mockPostReply(otherSone, "Reply1").inReplyTo(post1).withText("R").create();
74                 SimpleFieldSet getPostsFieldSet = new SimpleFieldSetBuilder()
75                                 .put("Message", "GetPosts")
76                                 .put("Sone", "SoneId")
77                                 .put("StartPost", "5")
78                                 .get();
79                 Response response = getPostsCommand.execute(getPostsFieldSet, null, DIRECT);
80                 verifyAnswer(response, "Posts");
81                 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", Collections.<Post>emptyList());
82         }
83
84         @Test
85         public void getOnlyOnePostAndSkipTheFirstPost() throws FcpException, FSParseException {
86                 Sone sone = mocks.mockSone("SoneId").create();
87                 Post post1 = mocks.mockPost(sone, "Post1").withTime(1000L).withText("1").create();
88                 Post post2 = mocks.mockPost(sone, "Post2").withTime(2000L).withText("1").create();
89                 Post post3 = mocks.mockPost(sone, "Post3").withTime(3000L).withText("1").create();
90                 Sone otherSone = mocks.mockSone("OtherSone").create();
91                 mocks.mockPostReply(otherSone, "Reply1").inReplyTo(post1).withText("R").create();
92                 SimpleFieldSet getPostsFieldSet = new SimpleFieldSetBuilder()
93                                 .put("Message", "GetPosts")
94                                 .put("Sone", "SoneId")
95                                 .put("MaxPosts", "1")
96                                 .put("StartPost", "1")
97                                 .get();
98                 Response response = getPostsCommand.execute(getPostsFieldSet, null, DIRECT);
99                 verifyAnswer(response, "Posts");
100                 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", asList(post2));
101         }
102
103         @Test
104         public void getOnlyTheFirstTwoPosts() throws FcpException, FSParseException {
105                 Sone sone = mocks.mockSone("SoneId").create();
106                 Post post1 = mocks.mockPost(sone, "Post1").withTime(1000L).withText("1").create();
107                 Post post2 = mocks.mockPost(sone, "Post2").withTime(2000L).withText("1").create();
108                 Post post3 = mocks.mockPost(sone, "Post3").withTime(3000L).withText("1").create();
109                 Sone otherSone = mocks.mockSone("OtherSone").create();
110                 mocks.mockPostReply(otherSone, "Reply1").inReplyTo(post1).withText("R").create();
111                 SimpleFieldSet getPostsFieldSet = new SimpleFieldSetBuilder()
112                                 .put("Message", "GetPosts")
113                                 .put("Sone", "SoneId")
114                                 .put("MaxPosts", "2")
115                                 .get();
116                 Response response = getPostsCommand.execute(getPostsFieldSet, null, DIRECT);
117                 verifyAnswer(response, "Posts");
118                 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", asList(post3, post2));
119         }
120
121         @Test
122         public void skipTheFirstPost() throws FcpException, FSParseException {
123                 Sone sone = mocks.mockSone("SoneId").create();
124                 Post post1 = mocks.mockPost(sone, "Post1").withTime(1000L).withText("1").create();
125                 Post post2 = mocks.mockPost(sone, "Post2").withTime(2000L).withText("1").create();
126                 Post post3 = mocks.mockPost(sone, "Post3").withTime(3000L).withText("1").create();
127                 Sone otherSone = mocks.mockSone("OtherSone").create();
128                 mocks.mockPostReply(otherSone, "Reply1").inReplyTo(post1).withText("R").create();
129                 SimpleFieldSet getPostsFieldSet = new SimpleFieldSetBuilder()
130                                 .put("Message", "GetPosts")
131                                 .put("Sone", "SoneId")
132                                 .put("StartPost", "1")
133                                 .get();
134                 Response response = getPostsCommand.execute(getPostsFieldSet, null, DIRECT);
135                 verifyAnswer(response, "Posts");
136                 verifyPostsWithReplies(response.getReplyParameters(), "Posts.", asList(post2, post1));
137         }
138
139         @Test(expected = FcpException.class)
140         public void aMissingSoneCausesAnError() throws FcpException {
141                 SimpleFieldSet getPostsFieldSet = new SimpleFieldSetBuilder()
142                                 .put("Message", "GetPosts")
143                                 .get();
144                 getPostsCommand.execute(getPostsFieldSet, null, DIRECT);
145         }
146
147 }