Move verifiers to different package.
[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.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;
26
27 import net.pterodactylus.sone.data.Mocks;
28 import net.pterodactylus.sone.data.PostReply;
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;
33
34 import freenet.support.SimpleFieldSet;
35
36 import org.junit.Test;
37 import org.mockito.ArgumentCaptor;
38
39 /**
40  * Unit test for {@link DeleteReplyCommand}.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class DeleteReplyCommandTest {
45
46         private final Mocks mocks = new Mocks();
47         private final DeleteReplyCommand deleteReplyCommand = new DeleteReplyCommand(mocks.core);
48
49         @Test
50         public void verifyThatDeletingAReplyWorks() throws FcpException {
51                 Sone sone = mocks.mockSone("SoneId").local().create();
52                 PostReply postReply = mocks.mockPostReply(sone, "ReplyId").create();
53                 ArgumentCaptor<PostReply> postReplyCaptor = forClass(PostReply.class);
54                 doNothing().when(mocks.core).deleteReply(postReplyCaptor.capture());
55                 SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder()
56                                 .put("Message", "DeleteReply")
57                                 .put("Reply", "ReplyId")
58                                 .get();
59                 Response response = deleteReplyCommand.execute(deleteReplyFieldSet, null, DIRECT);
60                 verifyAnswer(response, "ReplyDeleted");
61                 assertThat(postReplyCaptor.getValue(), is(postReply));
62         }
63
64         @Test(expected = FcpException.class)
65         public void verifyThatDeletingAReplyWithAnInvalidReplyIdCausesAnError() throws FcpException {
66                 SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder()
67                                 .put("Message", "DeleteReply")
68                                 .put("Reply", "ReplyId")
69                                 .get();
70                 deleteReplyCommand.execute(deleteReplyFieldSet, null, DIRECT);
71         }
72
73         @Test(expected = FcpException.class)
74         public void verifyThatDeletingAReplyWithoutReplyIdCausesAnError() throws FcpException {
75                 SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder()
76                                 .put("Message", "DeleteReply")
77                                 .get();
78                 deleteReplyCommand.execute(deleteReplyFieldSet, null, DIRECT);
79         }
80
81         @Test
82         public void verifyThatDeletingAReplyFromANonLocalSoneCausesAnError() throws FcpException {
83                 Sone sone = mocks.mockSone("SoneId").create();
84                 mocks.mockPostReply(sone, "ReplyId").create();
85                 SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder()
86                                 .put("Message", "DeleteReply")
87                                 .put("Reply", "ReplyId")
88                                 .get();
89                 Response response = deleteReplyCommand.execute(deleteReplyFieldSet, null, DIRECT);
90                 verifyAnswer(response, "Error");
91                 assertThat(response.getReplyParameters().get("ErrorCode"), is("401"));
92         }
93
94 }