Move verifiers to different package.
[Sone.git] / src / test / java / net / pterodactylus / sone / fcp / GetPostCommandTest.java
1 /*
2  * Sone - GetPostCommandTest.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.verifyPostWithReplies;
23 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
24 import static org.mockito.Mockito.when;
25
26 import net.pterodactylus.sone.data.Mocks;
27 import net.pterodactylus.sone.data.Post;
28 import net.pterodactylus.sone.data.PostReply;
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 GetPostCommand}.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class GetPostCommandTest {
45
46         private final Mocks mocks = new Mocks();
47         private final GetPostCommand getPostCommand = new GetPostCommand(mocks.core);
48
49         @Test
50         public void verifyThatGettingAPostWithoutRepliesAndRecipientWorks() throws FcpException, FSParseException {
51                 Sone sone = mocks.mockSone("SoneId").create();
52                 Post post = mocks.mockPost(sone, "PostId").withTime(1000L).withText("Text of the post.").create();
53                 SimpleFieldSet getPostFieldSet = new SimpleFieldSetBuilder()
54                                 .put("Message", "GetPost")
55                                 .put("Post", "PostId")
56                                 .get();
57                 Response response = getPostCommand.execute(getPostFieldSet, null, DIRECT);
58                 verifyAnswer(response, "Post");
59                 verifyPostWithReplies(response.getReplyParameters(), "Post.", post);
60         }
61
62         @Test
63         public void verifyThatGettingAPostWithoutRepliesAndWithRecipientWorks() throws FcpException, FSParseException {
64                 Sone sone = mocks.mockSone("SoneId").create();
65                 Sone otherSone = mocks.mockSone("OtherSoneId").create();
66                 Post post = mocks.mockPost(sone, "PostId").withRecipient(otherSone.getId()).withTime(1000L).withText("Text of the post.").create();
67                 SimpleFieldSet getPostFieldSet = new SimpleFieldSetBuilder()
68                                 .put("Message", "GetPost")
69                                 .put("Post", "PostId")
70                                 .get();
71                 Response response = getPostCommand.execute(getPostFieldSet, null, DIRECT);
72                 verifyAnswer(response, "Post");
73                 verifyPostWithReplies(response.getReplyParameters(), "Post.", post);
74         }
75
76         @Test
77         public void verifyThatGettingAPostWithRepliesWorks() throws FcpException, FSParseException {
78                 Sone sone = mocks.mockSone("SoneId").create();
79                 Post post = mocks.mockPost(sone, "PostId").withTime(1000L).withText("Text of the post.").create();
80                 PostReply postReply1 = mocks.mockPostReply(sone, "Reply1").create();
81                 when(postReply1.getText()).thenReturn("Reply 1.");
82                 PostReply postReply2 = mocks.mockPostReply(sone, "Reply2").create();
83                 when(postReply2.getText()).thenReturn("Reply 2.");
84                 when(post.getReplies()).thenReturn(asList(postReply1, postReply2));
85                 SimpleFieldSet getPostFieldSet = new SimpleFieldSetBuilder()
86                                 .put("Message", "GetPost")
87                                 .put("Post", "PostId")
88                                 .put("IncludeReplies", "true")
89                                 .get();
90                 Response response = getPostCommand.execute(getPostFieldSet, null, DIRECT);
91                 verifyAnswer(response, "Post");
92                 verifyPostWithReplies(response.getReplyParameters(), "Post.", post);
93         }
94
95         @Test(expected = FcpException.class)
96         public void verifyThatGettingANonExistingPostCausesAnError() throws FcpException {
97                 SimpleFieldSet getPostFieldSet = new SimpleFieldSetBuilder()
98                                 .put("Message", "GetPost")
99                                 .put("Post", "PostId")
100                                 .get();
101                 getPostCommand.execute(getPostFieldSet, null, DIRECT);
102         }
103
104         @Test(expected = FcpException.class)
105         public void verifyThatAMissingPostIdCausesAnError() throws FcpException {
106                 SimpleFieldSet getPostFieldSet = new SimpleFieldSetBuilder()
107                                 .put("Message", "GetPost")
108                                 .get();
109                 getPostCommand.execute(getPostFieldSet, null, DIRECT);
110         }
111
112 }