Use verify* methods from Verifiers.
[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 com.google.common.base.Optional.of;
21 import static java.util.Arrays.asList;
22 import static net.pterodactylus.sone.fcp.Verifiers.verifyAnswer;
23 import static net.pterodactylus.sone.fcp.Verifiers.verifyPostWithReplies;
24 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
25 import static org.mockito.Mockito.when;
26
27 import net.pterodactylus.sone.data.Mocks;
28 import net.pterodactylus.sone.data.Post;
29 import net.pterodactylus.sone.data.PostReply;
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 com.google.common.base.Optional;
39 import org.junit.Test;
40
41 /**
42  * Unit test for {@link GetPostCommand}.
43  *
44  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45  */
46 public class GetPostCommandTest {
47
48         private final Mocks mocks = new Mocks();
49         private final GetPostCommand getPostCommand = new GetPostCommand(mocks.core);
50
51         @Test
52         public void verifyThatGettingAPostWithoutRepliesAndRecipientWorks() throws FcpException, FSParseException {
53                 Sone sone = mocks.mockSone("SoneId").create();
54                 Post post = preparePostWithoutRecipient(sone);
55                 SimpleFieldSet getPostFieldSet = new SimpleFieldSetBuilder()
56                                 .put("Message", "GetPost")
57                                 .put("Post", "PostId")
58                                 .get();
59                 Response response = getPostCommand.execute(getPostFieldSet, null, DIRECT);
60                 verifyAnswer(response, "Post");
61                 verifyPostWithReplies(response.getReplyParameters(), "Post.", post);
62         }
63
64         @Test
65         public void verifyThatGettingAPostWithoutRepliesAndWithRecipientWorks() throws FcpException, FSParseException {
66                 Sone sone = mocks.mockSone("SoneId").create();
67                 Sone otherSone = mocks.mockSone("OtherSoneId").create();
68                 Post post = preparePostWithRecipient(sone, otherSone);
69                 SimpleFieldSet getPostFieldSet = new SimpleFieldSetBuilder()
70                                 .put("Message", "GetPost")
71                                 .put("Post", "PostId")
72                                 .get();
73                 Response response = getPostCommand.execute(getPostFieldSet, null, DIRECT);
74                 verifyAnswer(response, "Post");
75                 verifyPostWithReplies(response.getReplyParameters(), "Post.", post);
76         }
77
78         @Test
79         public void verifyThatGettingAPostWithRepliesWorks() throws FcpException, FSParseException {
80                 Sone sone = mocks.mockSone("SoneId").create();
81                 Post post = preparePostWithoutRecipient(sone);
82                 PostReply postReply1 = mocks.mockPostReply(sone, "Reply1").create();
83                 when(postReply1.getText()).thenReturn("Reply 1.");
84                 PostReply postReply2 = mocks.mockPostReply(sone, "Reply2").create();
85                 when(postReply2.getText()).thenReturn("Reply 2.");
86                 when(post.getReplies()).thenReturn(asList(postReply1, postReply2));
87                 SimpleFieldSet getPostFieldSet = new SimpleFieldSetBuilder()
88                                 .put("Message", "GetPost")
89                                 .put("Post", "PostId")
90                                 .put("IncludeReplies", "true")
91                                 .get();
92                 Response response = getPostCommand.execute(getPostFieldSet, null, DIRECT);
93                 verifyAnswer(response, "Post");
94                 verifyPostWithReplies(response.getReplyParameters(), "Post.", post);
95         }
96
97         @Test(expected = FcpException.class)
98         public void verifyThatGettingANonExistingPostCausesAnError() throws FcpException {
99                 SimpleFieldSet getPostFieldSet = new SimpleFieldSetBuilder()
100                                 .put("Message", "GetPost")
101                                 .put("Post", "PostId")
102                                 .get();
103                 getPostCommand.execute(getPostFieldSet, null, DIRECT);
104         }
105
106         @Test(expected = FcpException.class)
107         public void verifyThatAMissingPostIdCausesAnError() throws FcpException {
108                 SimpleFieldSet getPostFieldSet = new SimpleFieldSetBuilder()
109                                 .put("Message", "GetPost")
110                                 .get();
111                 getPostCommand.execute(getPostFieldSet, null, DIRECT);
112         }
113
114         private Post preparePostWithoutRecipient(Sone sone) {
115                 Post post = preparePost(sone);
116                 when(post.getRecipientId()).thenReturn(Optional.<String>absent());
117                 when(post.getRecipient()).thenReturn(Optional.<Sone>absent());
118                 return post;
119         }
120
121         private Post preparePostWithRecipient(Sone sone, Sone otherSone) {
122                 Post post = preparePost(sone);
123                 String otherSoneId = otherSone.getId();
124                 when(post.getRecipientId()).thenReturn(of(otherSoneId));
125                 when(post.getRecipient()).thenReturn(of(otherSone));
126                 return post;
127         }
128
129         private Post preparePost(Sone sone) {
130                 Post post = mocks.mockPost(sone, "PostId").create();
131                 when(post.getText()).thenReturn("Text of the post.");
132                 when(post.getTime()).thenReturn(1000L);
133                 return post;
134         }
135
136 }