From: David ‘Bombe’ Roden Date: Sun, 27 Oct 2013 21:59:19 +0000 (+0100) Subject: Add unit test for DeletePostCommand. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=1f639c3725a4b9c54d43b549dd693fcde0a125c8;p=Sone.git Add unit test for DeletePostCommand. --- diff --git a/src/test/java/net/pterodactylus/sone/fcp/DeletePostCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/DeletePostCommandTest.java new file mode 100644 index 0000000..1d282ca --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/fcp/DeletePostCommandTest.java @@ -0,0 +1,107 @@ +/* + * Sone - DeletePostCommandTest.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.fcp; + +import static net.pterodactylus.sone.data.Mocks.mockCore; +import static net.pterodactylus.sone.data.Mocks.mockDatabase; +import static net.pterodactylus.sone.data.Mocks.mockLocalSone; +import static net.pterodactylus.sone.data.Mocks.mockPost; +import static net.pterodactylus.sone.data.Mocks.mockRemoteSone; +import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.mockito.ArgumentCaptor.forClass; +import static org.mockito.Mockito.doNothing; + +import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.data.Post; +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.database.Database; +import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder; +import net.pterodactylus.sone.freenet.fcp.Command.Response; +import net.pterodactylus.sone.freenet.fcp.FcpException; + +import freenet.support.SimpleFieldSet; + +import org.junit.Test; +import org.mockito.ArgumentCaptor; + +/** + * Unit test for {@link DeletePostCommand}. + * + * @author David ‘Bombe’ Roden + */ +public class DeletePostCommandTest { + + private final Database database = mockDatabase(); + private final Core core = mockCore(database); + private final DeletePostCommand deletePostCommand = new DeletePostCommand(core); + + @Test + public void verifyThatDeletingAPostWorks() throws FcpException { + Sone sone = mockLocalSone(core, "Sone"); + Post post = mockPost(core, sone, "PostId"); + ArgumentCaptor deletedPost = forClass(Post.class); + doNothing().when(core).deletePost(deletedPost.capture()); + SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder() + .put("Message", "DeletePost") + .put("Post", "PostId") + .get(); + Response response = deletePostCommand.execute(deletePostFieldSet, null, DIRECT); + assertThat(response, notNullValue()); + assertThat(response.getReplyParameters(), notNullValue()); + assertThat(response.getReplyParameters().get("Message"), is("PostDeleted")); + assertThat(deletedPost.getValue(), is(post)); + } + + @Test + public void verifyThatDeletingAPostFromANonLocalSoneCausesAnError() throws FcpException { + Sone sone = mockRemoteSone(core, "Sone"); + Post post = mockPost(core, sone, "PostId"); + SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder() + .put("Message", "DeletePost") + .put("Post", "PostId") + .get(); + Response response = deletePostCommand.execute(deletePostFieldSet, null, DIRECT); + assertThat(response, notNullValue()); + assertThat(response.getReplyParameters(), notNullValue()); + assertThat(response.getReplyParameters().get("Message"), is("Error")); + assertThat(response.getReplyParameters().get("ErrorCode"), is("401")); + } + + @Test(expected = FcpException.class) + public void verifyThatDeletingWithAMissingPostIdCausesAnError() throws FcpException { + SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder() + .put("Message", "DeletePost") + .get(); + deletePostCommand.execute(deletePostFieldSet, null, DIRECT); + } + + @Test(expected = FcpException.class) + public void verifyThatDeletingAPostWithAnInvalidPostIdCausesAnError() throws FcpException { + Sone sone = mockLocalSone(core, "Sone"); + mockPost(core, sone, "PostId"); + SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder() + .put("Message", "DeletePost") + .put("Post", "OtherPostId") + .get(); + deletePostCommand.execute(deletePostFieldSet, null, DIRECT); + } + +}