From 18423dd893c8d4f1ef7d3a82ae229f5544b7616e Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 15 Jan 2017 00:40:14 +0100 Subject: [PATCH] Add unit test for get sones command, fix some problems a) Unify the reply of all sone-related commands. b) Add a prefix for get sones command. --- .../sone/fcp/AbstractSoneCommand.java | 6 +- .../pterodactylus/sone/fcp/GetSonesCommand.java | 4 +- .../sone/fcp/GetLocalSonesCommandTest.kt | 18 ++---- .../pterodactylus/sone/fcp/GetSonesCommandTest.kt | 68 ++++++++++++++++++++++ .../net/pterodactylus/sone/fcp/SoneCommandTest.kt | 3 +- 5 files changed, 80 insertions(+), 19 deletions(-) create mode 100644 src/test/kotlin/net/pterodactylus/sone/fcp/GetSonesCommandTest.kt diff --git a/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java b/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java index 1c4fd2e..98a43ab 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java @@ -237,6 +237,7 @@ public abstract class AbstractSoneCommand extends AbstractCommand { protected static SimpleFieldSet encodeSone(Sone sone, String prefix, Optional 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.absent())); } return soneBuilder.get(); diff --git a/src/main/java/net/pterodactylus/sone/fcp/GetSonesCommand.java b/src/main/java/net/pterodactylus/sone/fcp/GetSonesCommand.java index 10595ff..fa805e1 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/GetSonesCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/GetSonesCommand.java @@ -51,10 +51,10 @@ public class GetSonesCommand extends AbstractSoneCommand { int maxSones = getInt(parameters, "MaxSones", -1); List sones = new ArrayList(getCore().getSones()); if (sones.size() < startSone) { - return new Response("Sones", encodeSones(Collections. emptyList(), "")); + return new Response("Sones", encodeSones(Collections. 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.")); } } diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/GetLocalSonesCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/GetLocalSonesCommandTest.kt index bd7ddac..2a78efc 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/GetLocalSonesCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/GetLocalSonesCommandTest.kt @@ -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 index 0000000..f6ec734 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/GetSonesCommandTest.kt @@ -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")) + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt index ddaecee..e5c2066 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt @@ -99,7 +99,7 @@ abstract class SoneCommandTest { protected operator fun SimpleFieldSet.plusAssign(keyValue: Pair) = 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>().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"] } -- 2.7.4