4ab8cdf3dc5eaa1657a5e46924daaa6f47e54a0e
[Sone.git] / src / test / java / net / pterodactylus / sone / fcp / DeleteReplyCommandTest.java
1 /*
2  * Sone - DeleteReplyCommandTest.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 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;
31
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;
39
40 import freenet.support.SimpleFieldSet;
41
42 import org.junit.Test;
43 import org.mockito.ArgumentCaptor;
44
45 /**
46  * Unit test for {@link DeleteReplyCommand}.
47  *
48  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49  */
50 public class DeleteReplyCommandTest {
51
52         private final Database database = mockDatabase();
53         private final Core core = mockCore(database);
54         private final DeleteReplyCommand deleteReplyCommand = new DeleteReplyCommand(core);
55
56         @Test
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")
65                                 .get();
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"));
71         }
72
73         @Test(expected = FcpException.class)
74         public void verifyThatDeletingAReplyWithAnInvalidReplyIdCausesAnError() throws FcpException {
75                 SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder()
76                                 .put("Message", "DeleteReply")
77                                 .put("Reply", "ReplyId")
78                                 .get();
79                 deleteReplyCommand.execute(deleteReplyFieldSet, null, DIRECT);
80         }
81
82         @Test(expected = FcpException.class)
83         public void verifyThatDeletingAReplyWithoutReplyIdCausesAnError() throws FcpException {
84                 SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder()
85                                 .put("Message", "DeleteReply")
86                                 .get();
87                 deleteReplyCommand.execute(deleteReplyFieldSet, null, DIRECT);
88         }
89
90         @Test
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")
97                                 .get();
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"));
103         }
104
105 }