Add unit test for get sones command, fix some problems
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 14 Jan 2017 23:40:14 +0000 (00:40 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 14 Jan 2017 23:40:14 +0000 (00:40 +0100)
a) Unify the reply of all sone-related commands.
b) Add a prefix for get sones command.

src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java
src/main/java/net/pterodactylus/sone/fcp/GetSonesCommand.java
src/test/kotlin/net/pterodactylus/sone/fcp/GetLocalSonesCommandTest.kt
src/test/kotlin/net/pterodactylus/sone/fcp/GetSonesCommandTest.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt

index 1c4fd2e..98a43ab 100644 (file)
@@ -237,6 +237,7 @@ public abstract class AbstractSoneCommand extends AbstractCommand {
        protected static SimpleFieldSet encodeSone(Sone sone, String prefix, Optional<Sone> localSone) {
                SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
 
+               soneBuilder.put(prefix + "ID", sone.getId());
                soneBuilder.put(prefix + "Name", sone.getName());
                soneBuilder.put(prefix + "NiceName", SoneAccessor.getNiceName(sone));
                soneBuilder.put(prefix + "LastUpdated", sone.getTime());
@@ -272,10 +273,7 @@ public abstract class AbstractSoneCommand extends AbstractCommand {
                soneBuilder.put(prefix + "Count", sones.size());
                for (Sone sone : sones) {
                        String sonePrefix = prefix + soneIndex++ + ".";
-                       soneBuilder.put(sonePrefix + "ID", sone.getId());
-                       soneBuilder.put(sonePrefix + "Name", sone.getName());
-                       soneBuilder.put(sonePrefix + "NiceName", SoneAccessor.getNiceName(sone));
-                       soneBuilder.put(sonePrefix + "Time", sone.getTime());
+                       soneBuilder.put(encodeSone(sone, sonePrefix, Optional.<Sone>absent()));
                }
 
                return soneBuilder.get();
index 10595ff..fa805e1 100644 (file)
@@ -51,10 +51,10 @@ public class GetSonesCommand extends AbstractSoneCommand {
                int maxSones = getInt(parameters, "MaxSones", -1);
                List<Sone> sones = new ArrayList<Sone>(getCore().getSones());
                if (sones.size() < startSone) {
-                       return new Response("Sones", encodeSones(Collections.<Sone> emptyList(), ""));
+                       return new Response("Sones", encodeSones(Collections.<Sone> emptyList(), "Sones."));
                }
                Collections.sort(sones, Sone.NICE_NAME_COMPARATOR);
-               return new Response("Sones", encodeSones(sones.subList(startSone, (maxSones == -1) ? sones.size() : Math.min(startSone + maxSones, sones.size())), ""));
+               return new Response("Sones", encodeSones(sones.subList(startSone, (maxSones == -1) ? sones.size() : Math.min(startSone + maxSones, sones.size())), "Sones."));
        }
 
 }
index bd7ddac..2a78efc 100644 (file)
@@ -11,6 +11,9 @@ import org.junit.Test
  */
 class GetLocalSonesCommandTest : SoneCommandTest() {
 
+       private val sone1 = createSone("Id1", "Name1", "First1", "Last1", 1000L)
+       private val sone2 = createSone("Id2", "Name2", "First2", "Last2", 2000L)
+
        override fun createCommand(core: Core) = GetLocalSonesCommand(core)
 
        @Test
@@ -20,23 +23,14 @@ class GetLocalSonesCommandTest : SoneCommandTest() {
 
        @Test
        fun `command returns local sones`() {
-               val localSones = setOf(
-                               createSone("Id1", "Name1", "First1", "Last1", 1000L),
-                               createSone("Id2", "Name2", "First2", "Last2", 2000L)
-               )
+               val localSones = setOf(sone1, sone2)
                whenever(core.localSones).thenReturn(localSones)
                val response = command.execute(null)
                val replyParameters = response.replyParameters
                assertThat(replyParameters["Message"], equalTo("ListLocalSones"))
                assertThat(replyParameters["LocalSones.Count"], equalTo("2"))
-               assertThat(replyParameters["LocalSones.0.ID"], equalTo("Id1"))
-               assertThat(replyParameters["LocalSones.0.Name"], equalTo("Name1"))
-               assertThat(replyParameters["LocalSones.0.NiceName"], equalTo("First1 Last1"))
-               assertThat(replyParameters["LocalSones.0.Time"], equalTo("1000"))
-               assertThat(replyParameters["LocalSones.1.ID"], equalTo("Id2"))
-               assertThat(replyParameters["LocalSones.1.Name"], equalTo("Name2"))
-               assertThat(replyParameters["LocalSones.1.NiceName"], equalTo("First2 Last2"))
-               assertThat(replyParameters["LocalSones.1.Time"], equalTo("2000"))
+               assertThat(replyParameters.parseSone("LocalSones.0."), matchesSone(sone1))
+               assertThat(replyParameters.parseSone("LocalSones.1."), matchesSone(sone2))
        }
 
 }
diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/GetSonesCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/GetSonesCommandTest.kt
new file mode 100644 (file)
index 0000000..f6ec734
--- /dev/null
@@ -0,0 +1,68 @@
+package net.pterodactylus.sone.fcp
+
+import net.pterodactylus.sone.core.Core
+import net.pterodactylus.sone.test.whenever
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.junit.Before
+import org.junit.Test
+
+/**
+ * Unit test for [GetSonesCommand].
+ */
+class GetSonesCommandTest : SoneCommandTest() {
+
+       private val sone1 = createSone("SoneId1", "Sone1", "Sone", "#1", 1000)
+       private val sone2 = createSone("SoneId2", "Sone2", "Sone", "#2", 2000)
+       private val sone3 = createSone("SoneId3", "Sone3", "Sone", "#3", 3000)
+
+       override fun createCommand(core: Core) = GetSonesCommand(core)
+
+       @Before
+       fun setupSones() {
+               whenever(core.sones).thenReturn(setOf(sone2, sone3, sone1))
+       }
+
+       @Test
+       fun `request without parameters lists all sones`() {
+           val replyParameters = command.execute(parameters).replyParameters
+
+               assertThat(replyParameters["Message"], equalTo("Sones"))
+               assertThat(replyParameters["Sones.Count"], equalTo("3"))
+               assertThat(replyParameters.parseSone("Sones.0."), matchesSone(sone1))
+               assertThat(replyParameters.parseSone("Sones.1."), matchesSone(sone2))
+               assertThat(replyParameters.parseSone("Sones.2."), matchesSone(sone3))
+       }
+
+       @Test
+       fun `skipping the first sone lists the last two sones`() {
+               parameters += "StartSone" to "1"
+           val replyParameters = command.execute(parameters).replyParameters
+
+               assertThat(replyParameters["Message"], equalTo("Sones"))
+               assertThat(replyParameters["Sones.Count"], equalTo("2"))
+               assertThat(replyParameters.parseSone("Sones.0."), matchesSone(sone2))
+               assertThat(replyParameters.parseSone("Sones.1."), matchesSone(sone3))
+       }
+
+       @Test
+       fun `requesting only two sones lists the first two sones`() {
+               parameters += "MaxSones" to "2"
+           val replyParameters = command.execute(parameters).replyParameters
+
+               assertThat(replyParameters["Message"], equalTo("Sones"))
+               assertThat(replyParameters["Sones.Count"], equalTo("2"))
+               assertThat(replyParameters.parseSone("Sones.0."), matchesSone(sone1))
+               assertThat(replyParameters.parseSone("Sones.1."), matchesSone(sone2))
+       }
+
+       @Test
+       fun `skipping more sones than there are lists no sones`() {
+               parameters += "StartSone" to "20"
+           val replyParameters = command.execute(parameters).replyParameters
+
+               assertThat(replyParameters["Message"], equalTo("Sones"))
+               assertThat(replyParameters["Sones.Count"], equalTo("0"))
+       }
+
+}
index ddaecee..e5c2066 100644 (file)
@@ -99,7 +99,7 @@ abstract class SoneCommandTest {
        protected operator fun SimpleFieldSet.plusAssign(keyValue: Pair<String, String?>) = putSingle(keyValue.first, keyValue.second)
        protected fun SimpleFieldSet.parsePost(prefix: String) = parseFromSimpleFieldSet(prefix, "ID", "Sone", "Recipient", "Time", "Text")
        protected fun SimpleFieldSet.parseReply(prefix: String) = parseFromSimpleFieldSet(prefix, "ID", "Sone", "Time", "Text")
-       protected fun SimpleFieldSet.parseSone(prefix: String) = parseFromSimpleFieldSet(prefix, "Name", "NiceName", "LastUpdated", "Followed") +
+       protected fun SimpleFieldSet.parseSone(prefix: String) = parseFromSimpleFieldSet(prefix, "ID", "Name", "NiceName", "LastUpdated", "Followed") +
                        (0 until this["${prefix}Field.Count"].toInt()).map {
                                ("Field." + this["${prefix}Field.$it.Name"]) to this["${prefix}Field.$it.Value"]
                        }
@@ -124,6 +124,7 @@ abstract class SoneCommandTest {
        }
 
        protected fun matchesSone(sone: Sone) = OneByOneMatcher<Map<String, String?>>().apply {
+               expect("ID", sone.id) { it["ID"] }
                expect("name", sone.name) { it["Name"] }
                expect("last updated", sone.time.toString()) { it["LastUpdated"] }
                expect("nice name", SoneAccessor.getNiceName(sone)) { it["NiceName"] }