()
val url = URL(JOKE_URL)
val json = JSONObject(url.reader().body)
- PublicMessage(json.getString("joke"))
+ if (json.has("joke")) {
+ val joke = json.getString("joke").split("\n")
+ joke.forEach {
+ messages.add(PublicMessage(it, Colors.CYAN))
+ }
+ } else {
+ messages.add(ErrorMessage(json.getString("message"), Colors.RED))
+ }
+ messages
} catch (e: IOException) {
throw ModuleException("randomJoke(): IOE", "An IO error has occurred retrieving a random joke.", e)
} catch (e: JSONException) {
diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/modules/JokeTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/modules/JokeTest.kt
index 56355a8..be27645 100644
--- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/JokeTest.kt
+++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/JokeTest.kt
@@ -31,11 +31,12 @@
*/
package net.thauvin.erik.mobibot.modules
-import assertk.all
import assertk.assertThat
-import assertk.assertions.endsWith
+import assertk.assertions.isGreaterThan
+import assertk.assertions.isInstanceOf
import assertk.assertions.isNotEmpty
import net.thauvin.erik.mobibot.modules.Joke.Companion.randomJoke
+import net.thauvin.erik.mobibot.msg.PublicMessage
import org.testng.annotations.Test
/**
@@ -45,9 +46,11 @@ class JokeTest {
@Test(groups = ["modules"])
@Throws(ModuleException::class)
fun testRandomJoke() {
- assertThat(randomJoke().msg, "randomJoke() > 0").all {
- isNotEmpty()
- endsWith(".")
+ val joke = randomJoke()
+ assertThat(joke.size, "joke is empty").isGreaterThan(0)
+ joke.forEach {
+ assertThat(it, "is not a public message").isInstanceOf(PublicMessage::class.java)
+ assertThat(it.msg, "msg is empty").isNotEmpty()
}
}
}
diff --git a/website/index.html b/website/index.html
index 8637767..774eaff 100644
--- a/website/index.html
+++ b/website/index.html
@@ -112,7 +112,7 @@
Viewing when a nickname was last seen
/msg mobibot seen nickname
- Random jokes from Geek-Jokes
+ Random jokes from JokeAPI
mobibot: joke
Rolling dice or Playing war and rock paper scissors