Add unit test for GetSonesCommand.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 5 Nov 2013 06:46:32 +0000 (07:46 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:53 +0000 (22:25 +0100)
src/test/java/net/pterodactylus/sone/fcp/GetSonesCommandTest.java [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/fcp/Verifiers.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 (file)
index 0000000..6650a1a
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class GetSonesCommandTest {
+
+       private final Mocks mocks = new Mocks();
+       private final GetSonesCommand getSonesCommand = new GetSonesCommand(mocks.core);
+       private final List<Sone> 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.<Sone>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<Sone> 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);
+       }
+
+}
index 1578161..ed1a0f3 100644 (file)
@@ -117,4 +117,16 @@ public class Verifiers {
                }
        }
 
+       static void verifySones(SimpleFieldSet simpleFieldSet, String prefix, List<Sone> 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++;
+               }
+       }
+
 }