21214dbfd133bf978516af0c93cb10d30e0602f5
[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.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.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                 assertThat(response, notNullValue());
61                 assertThat(response.getReplyParameters(), notNullValue());
62                 assertThat(response.getReplyParameters().get("Message"), is("PostDeleted"));
63                 assertThat(deletedPost.getValue(), is(post));
64         }
65
66         @Test
67         public void verifyThatDeletingAPostFromANonLocalSoneCausesAnError() throws FcpException {
68                 Sone sone = mocks.mockSone("Sone").create();
69                 Post post = mocks.mockPost(sone, "PostId").create();
70                 SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder()
71                                 .put("Message", "DeletePost")
72                                 .put("Post", "PostId")
73                                 .get();
74                 Response response = deletePostCommand.execute(deletePostFieldSet, null, DIRECT);
75                 assertThat(response, notNullValue());
76                 assertThat(response.getReplyParameters(), notNullValue());
77                 assertThat(response.getReplyParameters().get("Message"), is("Error"));
78                 assertThat(response.getReplyParameters().get("ErrorCode"), is("401"));
79         }
80
81         @Test(expected = FcpException.class)
82         public void verifyThatDeletingWithAMissingPostIdCausesAnError() throws FcpException {
83                 SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder()
84                                 .put("Message", "DeletePost")
85                                 .get();
86                 deletePostCommand.execute(deletePostFieldSet, null, DIRECT);
87         }
88
89         @Test(expected = FcpException.class)
90         public void verifyThatDeletingAPostWithAnInvalidPostIdCausesAnError() throws FcpException {
91                 SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder()
92                                 .put("Message", "DeletePost")
93                                 .put("Post", "OtherPostId")
94                                 .get();
95                 deletePostCommand.execute(deletePostFieldSet, null, DIRECT);
96         }
97
98 }