Replace Sone mock methods with a mock builder.
[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.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;
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");
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                 assertThat(response, notNullValue());
61                 assertThat(postReplyCaptor.getValue(), is(postReply));
62                 assertThat(response.getReplyParameters(), notNullValue());
63                 assertThat(response.getReplyParameters().get("Message"), is("ReplyDeleted"));
64         }
65
66         @Test(expected = FcpException.class)
67         public void verifyThatDeletingAReplyWithAnInvalidReplyIdCausesAnError() throws FcpException {
68                 SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder()
69                                 .put("Message", "DeleteReply")
70                                 .put("Reply", "ReplyId")
71                                 .get();
72                 deleteReplyCommand.execute(deleteReplyFieldSet, null, DIRECT);
73         }
74
75         @Test(expected = FcpException.class)
76         public void verifyThatDeletingAReplyWithoutReplyIdCausesAnError() throws FcpException {
77                 SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder()
78                                 .put("Message", "DeleteReply")
79                                 .get();
80                 deleteReplyCommand.execute(deleteReplyFieldSet, null, DIRECT);
81         }
82
83         @Test
84         public void verifyThatDeletingAReplyFromANonLocalSoneCausesAnError() throws FcpException {
85                 Sone sone = mocks.mockSone("SoneId").create();
86                 mocks.mockPostReply(sone, "ReplyId");
87                 SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder()
88                                 .put("Message", "DeleteReply")
89                                 .put("Reply", "ReplyId")
90                                 .get();
91                 Response response = deleteReplyCommand.execute(deleteReplyFieldSet, null, DIRECT);
92                 assertThat(response, notNullValue());
93                 assertThat(response.getReplyParameters(), notNullValue());
94                 assertThat(response.getReplyParameters().get("Message"), is("Error"));
95                 assertThat(response.getReplyParameters().get("ErrorCode"), is("401"));
96         }
97
98 }