Add unit test for DeletePostCommand.
[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.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.mockPost;
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.Post;
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 DeletePostCommand}.
47  *
48  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49  */
50 public class DeletePostCommandTest {
51
52         private final Database database = mockDatabase();
53         private final Core core = mockCore(database);
54         private final DeletePostCommand deletePostCommand = new DeletePostCommand(core);
55
56         @Test
57         public void verifyThatDeletingAPostWorks() throws FcpException {
58                 Sone sone = mockLocalSone(core, "Sone");
59                 Post post = mockPost(core, sone, "PostId");
60                 ArgumentCaptor<Post> deletedPost = forClass(Post.class);
61                 doNothing().when(core).deletePost(deletedPost.capture());
62                 SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder()
63                                 .put("Message", "DeletePost")
64                                 .put("Post", "PostId")
65                                 .get();
66                 Response response = deletePostCommand.execute(deletePostFieldSet, null, DIRECT);
67                 assertThat(response, notNullValue());
68                 assertThat(response.getReplyParameters(), notNullValue());
69                 assertThat(response.getReplyParameters().get("Message"), is("PostDeleted"));
70                 assertThat(deletedPost.getValue(), is(post));
71         }
72
73         @Test
74         public void verifyThatDeletingAPostFromANonLocalSoneCausesAnError() throws FcpException {
75                 Sone sone = mockRemoteSone(core, "Sone");
76                 Post post = mockPost(core, sone, "PostId");
77                 SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder()
78                                 .put("Message", "DeletePost")
79                                 .put("Post", "PostId")
80                                 .get();
81                 Response response = deletePostCommand.execute(deletePostFieldSet, null, DIRECT);
82                 assertThat(response, notNullValue());
83                 assertThat(response.getReplyParameters(), notNullValue());
84                 assertThat(response.getReplyParameters().get("Message"), is("Error"));
85                 assertThat(response.getReplyParameters().get("ErrorCode"), is("401"));
86         }
87
88         @Test(expected = FcpException.class)
89         public void verifyThatDeletingWithAMissingPostIdCausesAnError() throws FcpException {
90                 SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder()
91                                 .put("Message", "DeletePost")
92                                 .get();
93                 deletePostCommand.execute(deletePostFieldSet, null, DIRECT);
94         }
95
96         @Test(expected = FcpException.class)
97         public void verifyThatDeletingAPostWithAnInvalidPostIdCausesAnError() throws FcpException {
98                 Sone sone = mockLocalSone(core, "Sone");
99                 mockPost(core, sone, "PostId");
100                 SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder()
101                                 .put("Message", "DeletePost")
102                                 .put("Post", "OtherPostId")
103                                 .get();
104                 deletePostCommand.execute(deletePostFieldSet, null, DIRECT);
105         }
106
107 }