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.freenet.fcp.Command.AccessType.DIRECT;
21 import static org.hamcrest.MatcherAssert.assertThat;
22 import static org.hamcrest.Matchers.is;
23 import static org.hamcrest.Matchers.notNullValue;
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 assertThat(response, notNullValue());
61 assertThat(response.getReplyParameters(), notNullValue());
62 assertThat(response.getReplyParameters().get("Message"), is("PostDeleted"));
63 assertThat(deletedPost.getValue(), is(post));
67 public void verifyThatDeletingAPostFromANonLocalSoneCausesAnError() throws FcpException {
68 Sone sone = mocks.mockSone("Sone").create();
69 Post post = mocks.mockPost(sone, "PostId").create();
70 SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder()
71 .put("Message", "DeletePost")
72 .put("Post", "PostId")
74 Response response = deletePostCommand.execute(deletePostFieldSet, null, DIRECT);
75 assertThat(response, notNullValue());
76 assertThat(response.getReplyParameters(), notNullValue());
77 assertThat(response.getReplyParameters().get("Message"), is("Error"));
78 assertThat(response.getReplyParameters().get("ErrorCode"), is("401"));
81 @Test(expected = FcpException.class)
82 public void verifyThatDeletingWithAMissingPostIdCausesAnError() throws FcpException {
83 SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder()
84 .put("Message", "DeletePost")
86 deletePostCommand.execute(deletePostFieldSet, null, DIRECT);
89 @Test(expected = FcpException.class)
90 public void verifyThatDeletingAPostWithAnInvalidPostIdCausesAnError() throws FcpException {
91 SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder()
92 .put("Message", "DeletePost")
93 .put("Post", "OtherPostId")
95 deletePostCommand.execute(deletePostFieldSet, null, DIRECT);