1e2ce7ccfe235821b78a1adc333af92884acd85c
[Sone.git] / src / test / java / net / pterodactylus / sone / fcp / DeletePostCommandTest.java
1 /*
2  * Sone - DeletePostCommandTest.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.fcp.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.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;
33
34 import freenet.support.SimpleFieldSet;
35
36 import org.junit.Test;
37 import org.mockito.ArgumentCaptor;
38
39 /**
40  * Unit test for {@link DeletePostCommand}.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class DeletePostCommandTest {
45
46         private final Mocks mocks = new Mocks();
47         private final DeletePostCommand deletePostCommand = new DeletePostCommand(mocks.core);
48
49         @Test
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")
58                                 .get();
59                 Response response = deletePostCommand.execute(deletePostFieldSet, null, DIRECT);
60                 verifyAnswer(response, "PostDeleted");
61                 assertThat(deletedPost.getValue(), is(post));
62         }
63
64         @Test
65         public void verifyThatDeletingAPostFromANonLocalSoneCausesAnError() throws FcpException {
66                 Sone sone = mocks.mockSone("Sone").create();
67                 Post post = mocks.mockPost(sone, "PostId").create();
68                 SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder()
69                                 .put("Message", "DeletePost")
70                                 .put("Post", "PostId")
71                                 .get();
72                 Response response = deletePostCommand.execute(deletePostFieldSet, null, DIRECT);
73                 verifyAnswer(response, "Error");
74                 assertThat(response.getReplyParameters().get("ErrorCode"), is("401"));
75         }
76
77         @Test(expected = FcpException.class)
78         public void verifyThatDeletingWithAMissingPostIdCausesAnError() throws FcpException {
79                 SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder()
80                                 .put("Message", "DeletePost")
81                                 .get();
82                 deletePostCommand.execute(deletePostFieldSet, null, DIRECT);
83         }
84
85         @Test(expected = FcpException.class)
86         public void verifyThatDeletingAPostWithAnInvalidPostIdCausesAnError() throws FcpException {
87                 SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder()
88                                 .put("Message", "DeletePost")
89                                 .put("Post", "OtherPostId")
90                                 .get();
91                 deletePostCommand.execute(deletePostFieldSet, null, DIRECT);
92         }
93
94 }