2 * Sone - DeleteReplyCommandTest.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.data.Mocks.mockCore;
21 import static net.pterodactylus.sone.data.Mocks.mockDatabase;
22 import static net.pterodactylus.sone.data.Mocks.mockLocalSone;
23 import static net.pterodactylus.sone.data.Mocks.mockPostReply;
24 import static net.pterodactylus.sone.data.Mocks.mockRemoteSone;
25 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
26 import static org.hamcrest.MatcherAssert.assertThat;
27 import static org.hamcrest.Matchers.is;
28 import static org.hamcrest.Matchers.notNullValue;
29 import static org.mockito.ArgumentCaptor.forClass;
30 import static org.mockito.Mockito.doNothing;
32 import net.pterodactylus.sone.core.Core;
33 import net.pterodactylus.sone.data.PostReply;
34 import net.pterodactylus.sone.data.Sone;
35 import net.pterodactylus.sone.database.Database;
36 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
37 import net.pterodactylus.sone.freenet.fcp.Command.Response;
38 import net.pterodactylus.sone.freenet.fcp.FcpException;
40 import freenet.support.SimpleFieldSet;
42 import org.junit.Test;
43 import org.mockito.ArgumentCaptor;
46 * Unit test for {@link DeleteReplyCommand}.
48 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
50 public class DeleteReplyCommandTest {
52 private final Database database = mockDatabase();
53 private final Core core = mockCore(database);
54 private final DeleteReplyCommand deleteReplyCommand = new DeleteReplyCommand(core);
57 public void verifyThatDeletingAReplyWorks() throws FcpException {
58 Sone sone = mockLocalSone(core, "SoneId");
59 PostReply postReply = mockPostReply(core, sone, "ReplyId");
60 ArgumentCaptor<PostReply> postReplyCaptor = forClass(PostReply.class);
61 doNothing().when(core).deleteReply(postReplyCaptor.capture());
62 SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder()
63 .put("Message", "DeleteReply")
64 .put("Reply", "ReplyId")
66 Response response = deleteReplyCommand.execute(deleteReplyFieldSet, null, DIRECT);
67 assertThat(response, notNullValue());
68 assertThat(postReplyCaptor.getValue(), is(postReply));
69 assertThat(response.getReplyParameters(), notNullValue());
70 assertThat(response.getReplyParameters().get("Message"), is("ReplyDeleted"));
73 @Test(expected = FcpException.class)
74 public void verifyThatDeletingAReplyWithAnInvalidReplyIdCausesAnError() throws FcpException {
75 SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder()
76 .put("Message", "DeleteReply")
77 .put("Reply", "ReplyId")
79 deleteReplyCommand.execute(deleteReplyFieldSet, null, DIRECT);
82 @Test(expected = FcpException.class)
83 public void verifyThatDeletingAReplyWithoutReplyIdCausesAnError() throws FcpException {
84 SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder()
85 .put("Message", "DeleteReply")
87 deleteReplyCommand.execute(deleteReplyFieldSet, null, DIRECT);
91 public void verifyThatDeletingAReplyFromANonLocalSoneCausesAnError() throws FcpException {
92 Sone sone = mockRemoteSone(core, "SoneId");
93 mockPostReply(core, sone, "ReplyId");
94 SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder()
95 .put("Message", "DeleteReply")
96 .put("Reply", "ReplyId")
98 Response response = deleteReplyCommand.execute(deleteReplyFieldSet, null, DIRECT);
99 assertThat(response, notNullValue());
100 assertThat(response.getReplyParameters(), notNullValue());
101 assertThat(response.getReplyParameters().get("Message"), is("Error"));
102 assertThat(response.getReplyParameters().get("ErrorCode"), is("401"));