From 8aa1dcad4bc71dd0eb259552bd5eed4f9ed32d1d Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 5 Nov 2013 07:46:32 +0100 Subject: [PATCH] Add unit test for GetSonesCommand. --- .../sone/fcp/GetSonesCommandTest.java | 111 +++++++++++++++++++++ .../java/net/pterodactylus/sone/fcp/Verifiers.java | 12 +++ 2 files changed, 123 insertions(+) create mode 100644 src/test/java/net/pterodactylus/sone/fcp/GetSonesCommandTest.java diff --git a/src/test/java/net/pterodactylus/sone/fcp/GetSonesCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/GetSonesCommandTest.java new file mode 100644 index 0000000..6650a1a --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/fcp/GetSonesCommandTest.java @@ -0,0 +1,111 @@ +/* + * Sone - GetSonesCommandTest.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 java.util.Arrays.asList; +import static net.pterodactylus.sone.fcp.Verifiers.verifyAnswer; +import static net.pterodactylus.sone.fcp.Verifiers.verifySones; +import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT; + +import java.util.Collections; +import java.util.List; + +import net.pterodactylus.sone.data.Mocks; +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder; +import net.pterodactylus.sone.freenet.fcp.Command.Response; + +import freenet.node.FSParseException; +import freenet.support.SimpleFieldSet; + +import org.junit.Test; + +/** + * Unit test for {@link GetSonesCommand}. + * + * @author David ‘Bombe’ Roden + */ +public class GetSonesCommandTest { + + private final Mocks mocks = new Mocks(); + private final GetSonesCommand getSonesCommand = new GetSonesCommand(mocks.core); + private final List mockedSones = prepareSones(); + + @Test + public void gettingAllSones() throws FSParseException { + SimpleFieldSet getSonesFieldSet = new SimpleFieldSetBuilder() + .put("Message", "GetSones") + .get(); + Response response = getSonesCommand.execute(getSonesFieldSet, null, DIRECT); + verifyAnswer(response, "Sones"); + verifySones(response.getReplyParameters(), "", mockedSones); + } + + @Test + public void skipMoreSonesThanThereAre() throws FSParseException { + SimpleFieldSet getSonesFieldSet = new SimpleFieldSetBuilder() + .put("Message", "GetSones") + .put("StartSone", "5") + .get(); + Response response = getSonesCommand.execute(getSonesFieldSet, null, DIRECT); + verifyAnswer(response, "Sones"); + verifySones(response.getReplyParameters(), "", Collections.emptyList()); + } + + @Test + public void gettingOnlyTwoSones() throws FSParseException { + SimpleFieldSet getSonesFieldSet = new SimpleFieldSetBuilder() + .put("Message", "GetSones") + .put("MaxSones", "2") + .get(); + Response response = getSonesCommand.execute(getSonesFieldSet, null, DIRECT); + verifyAnswer(response, "Sones"); + verifySones(response.getReplyParameters(), "", mockedSones.subList(0, 2)); + } + + @Test + public void gettingAllSonesSkippingTheFirst() throws FSParseException { + SimpleFieldSet getSonesFieldSet = new SimpleFieldSetBuilder() + .put("Message", "GetSones") + .put("StartSone", "1") + .get(); + Response response = getSonesCommand.execute(getSonesFieldSet, null, DIRECT); + verifyAnswer(response, "Sones"); + verifySones(response.getReplyParameters(), "", mockedSones.subList(1, mockedSones.size())); + } + + @Test + public void gettingOnlyOneSonesSkippingTheFirst() throws FSParseException { + SimpleFieldSet getSonesFieldSet = new SimpleFieldSetBuilder() + .put("Message", "GetSones") + .put("MaxSones", "1") + .put("StartSone", "1") + .get(); + Response response = getSonesCommand.execute(getSonesFieldSet, null, DIRECT); + verifyAnswer(response, "Sones"); + verifySones(response.getReplyParameters(), "", mockedSones.subList(1, 2)); + } + + private List prepareSones() { + Sone sone3 = mocks.mockSone("Sone3").withName("Sone3").withProfileName("S.", "O.", "Ne3").withTime(3000L).create(); + Sone sone1 = mocks.mockSone("Sone1").withName("Sone1").withProfileName("S.", "O.", "Ne1").withTime(1000L).create(); + Sone sone2 = mocks.mockSone("Sone2").withName("Sone2").withProfileName("S.", "O.", "Ne2").withTime(2000L).create(); + return asList(sone1, sone2, sone3); + } + +} diff --git a/src/test/java/net/pterodactylus/sone/fcp/Verifiers.java b/src/test/java/net/pterodactylus/sone/fcp/Verifiers.java index 1578161..ed1a0f3 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/Verifiers.java +++ b/src/test/java/net/pterodactylus/sone/fcp/Verifiers.java @@ -117,4 +117,16 @@ public class Verifiers { } } + static void verifySones(SimpleFieldSet simpleFieldSet, String prefix, List sones) throws FSParseException { + assertThat(simpleFieldSet.getInt(prefix + "Count"), is(sones.size())); + int soneIndex = 0; + for (Sone sone : sones) { + assertThat(simpleFieldSet.get(prefix + soneIndex + ".ID"), is(sone.getId())); + assertThat(simpleFieldSet.get(prefix + soneIndex + ".Name"), is(sone.getName())); + assertThat(simpleFieldSet.get(prefix + soneIndex + ".NiceName"), is(getNiceName(sone))); + assertThat(simpleFieldSet.getLong(prefix + soneIndex + ".Time"), is(sone.getTime())); + soneIndex++; + } + } + } -- 2.7.4