a) Unify the reply of all sone-related commands.
b) Add a prefix for get sones command.
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());
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();
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."));
}
}
*/
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
@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))
}
}
--- /dev/null
+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"))
+ }
+
+}
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"]
}
}
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"] }