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