From 7ddc0f201f6ce4d7479bb0b18804c6f31c6832fe Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 28 Oct 2013 19:21:23 +0100 Subject: [PATCH] Add unit test for DeleteReplyCommand. --- .../sone/fcp/DeleteReplyCommandTest.java | 105 +++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/test/java/net/pterodactylus/sone/fcp/DeleteReplyCommandTest.java diff --git a/src/test/java/net/pterodactylus/sone/fcp/DeleteReplyCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/DeleteReplyCommandTest.java new file mode 100644 index 0000000..4ab8cdf --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/fcp/DeleteReplyCommandTest.java @@ -0,0 +1,105 @@ +/* + * Sone - DeleteReplyCommandTest.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.mockPostReply; +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.PostReply; +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 DeleteReplyCommand}. + * + * @author David ‘Bombe’ Roden + */ +public class DeleteReplyCommandTest { + + private final Database database = mockDatabase(); + private final Core core = mockCore(database); + private final DeleteReplyCommand deleteReplyCommand = new DeleteReplyCommand(core); + + @Test + public void verifyThatDeletingAReplyWorks() throws FcpException { + Sone sone = mockLocalSone(core, "SoneId"); + PostReply postReply = mockPostReply(core, sone, "ReplyId"); + ArgumentCaptor postReplyCaptor = forClass(PostReply.class); + doNothing().when(core).deleteReply(postReplyCaptor.capture()); + SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder() + .put("Message", "DeleteReply") + .put("Reply", "ReplyId") + .get(); + Response response = deleteReplyCommand.execute(deleteReplyFieldSet, null, DIRECT); + assertThat(response, notNullValue()); + assertThat(postReplyCaptor.getValue(), is(postReply)); + assertThat(response.getReplyParameters(), notNullValue()); + assertThat(response.getReplyParameters().get("Message"), is("ReplyDeleted")); + } + + @Test(expected = FcpException.class) + public void verifyThatDeletingAReplyWithAnInvalidReplyIdCausesAnError() throws FcpException { + SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder() + .put("Message", "DeleteReply") + .put("Reply", "ReplyId") + .get(); + deleteReplyCommand.execute(deleteReplyFieldSet, null, DIRECT); + } + + @Test(expected = FcpException.class) + public void verifyThatDeletingAReplyWithoutReplyIdCausesAnError() throws FcpException { + SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder() + .put("Message", "DeleteReply") + .get(); + deleteReplyCommand.execute(deleteReplyFieldSet, null, DIRECT); + } + + @Test + public void verifyThatDeletingAReplyFromANonLocalSoneCausesAnError() throws FcpException { + Sone sone = mockRemoteSone(core, "SoneId"); + mockPostReply(core, sone, "ReplyId"); + SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder() + .put("Message", "DeleteReply") + .put("Reply", "ReplyId") + .get(); + Response response = deleteReplyCommand.execute(deleteReplyFieldSet, null, DIRECT); + assertThat(response, notNullValue()); + assertThat(response.getReplyParameters(), notNullValue()); + assertThat(response.getReplyParameters().get("Message"), is("Error")); + assertThat(response.getReplyParameters().get("ErrorCode"), is("401")); + } + +} -- 2.7.4