2 * Sone - DeletePostCommandTest.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.fcp;
20 import static net.pterodactylus.sone.Verifiers.verifyAnswer;
21 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.is;
24 import static org.mockito.ArgumentCaptor.forClass;
25 import static org.mockito.Mockito.doNothing;
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;
34 import freenet.support.SimpleFieldSet;
36 import org.junit.Test;
37 import org.mockito.ArgumentCaptor;
40 * Unit test for {@link DeletePostCommand}.
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class DeletePostCommandTest {
46 private final Mocks mocks = new Mocks();
47 private final DeletePostCommand deletePostCommand = new DeletePostCommand(mocks.core);
50 public void verifyThatDeletingAPostWorks() throws FcpException {
51 Sone sone = mocks.mockSone("Sone").local().create();
52 Post post = mocks.mockPost(sone, "PostId").create();
53 ArgumentCaptor<Post> deletedPost = forClass(Post.class);
54 doNothing().when(mocks.core).deletePost(deletedPost.capture());
55 SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder()
56 .put("Message", "DeletePost")
57 .put("Post", "PostId")
59 Response response = deletePostCommand.execute(deletePostFieldSet, null, DIRECT);
60 verifyAnswer(response, "PostDeleted");
61 assertThat(deletedPost.getValue(), is(post));
65 public void verifyThatDeletingAPostFromANonLocalSoneCausesAnError() throws FcpException {
66 Sone sone = mocks.mockSone("Sone").create();
67 Post post = mocks.mockPost(sone, "PostId").create();
68 SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder()
69 .put("Message", "DeletePost")
70 .put("Post", "PostId")
72 Response response = deletePostCommand.execute(deletePostFieldSet, null, DIRECT);
73 verifyAnswer(response, "Error");
74 assertThat(response.getReplyParameters().get("ErrorCode"), is("401"));
77 @Test(expected = FcpException.class)
78 public void verifyThatDeletingWithAMissingPostIdCausesAnError() throws FcpException {
79 SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder()
80 .put("Message", "DeletePost")
82 deletePostCommand.execute(deletePostFieldSet, null, DIRECT);
85 @Test(expected = FcpException.class)
86 public void verifyThatDeletingAPostWithAnInvalidPostIdCausesAnError() throws FcpException {
87 SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder()
88 .put("Message", "DeletePost")
89 .put("Post", "OtherPostId")
91 deletePostCommand.execute(deletePostFieldSet, null, DIRECT);