Added more logging and tests
This commit is contained in:
parent
a976b820b6
commit
f07b1a4258
2 changed files with 63 additions and 24 deletions
|
@ -119,12 +119,16 @@ class JokeApi {
|
||||||
urlParams.add("idRange=${idRange.start}")
|
urlParams.add("idRange=${idRange.start}")
|
||||||
} else if (idRange.end > idRange.start) {
|
} else if (idRange.end > idRange.start) {
|
||||||
urlParams.add("idRange=${idRange.start}-${idRange.end}")
|
urlParams.add("idRange=${idRange.start}-${idRange.end}")
|
||||||
|
} else if (logger.isLoggable(Level.WARNING)) {
|
||||||
|
logger.warning("Invalid ID Range: ${idRange.start}, ${idRange.end}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Amount
|
// Amount
|
||||||
if (amount in 2..10) {
|
if (amount in 2..10) {
|
||||||
urlParams.add("amount=${amount}")
|
urlParams.add("amount=${amount}")
|
||||||
|
} else if (amount != 1 && logger.isLoggable(Level.WARNING)) {
|
||||||
|
logger.warning("Invalid Amount: $amount")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Safe
|
// Safe
|
||||||
|
@ -148,7 +152,9 @@ class JokeApi {
|
||||||
|
|
||||||
@Throws(HttpErrorException::class, IOException::class)
|
@Throws(HttpErrorException::class, IOException::class)
|
||||||
internal fun fetchUrl(url: String): String {
|
internal fun fetchUrl(url: String): String {
|
||||||
logger.log(Level.FINE, url)
|
if (logger.isLoggable(Level.FINE)) {
|
||||||
|
logger.fine(url)
|
||||||
|
}
|
||||||
|
|
||||||
val connection = URL(url).openConnection() as HttpURLConnection
|
val connection = URL(url).openConnection() as HttpURLConnection
|
||||||
connection.setRequestProperty(
|
connection.setRequestProperty(
|
||||||
|
@ -158,7 +164,7 @@ class JokeApi {
|
||||||
if (connection.responseCode in 200..399) {
|
if (connection.responseCode in 200..399) {
|
||||||
val body = connection.inputStream.bufferedReader().readText()
|
val body = connection.inputStream.bufferedReader().readText()
|
||||||
if (logger.isLoggable(Level.FINE)) {
|
if (logger.isLoggable(Level.FINE)) {
|
||||||
logger.log(Level.FINE, body)
|
logger.fine(body)
|
||||||
}
|
}
|
||||||
return body
|
return body
|
||||||
} else {
|
} else {
|
||||||
|
@ -282,7 +288,6 @@ class JokeApi {
|
||||||
enabledFlags.add(it)
|
enabledFlags.add(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Joke(
|
return Joke(
|
||||||
error = false,
|
error = false,
|
||||||
category = Category.valueOf(json.getString("category").uppercase()),
|
category = Category.valueOf(json.getString("category").uppercase()),
|
||||||
|
|
|
@ -60,7 +60,7 @@ internal class JokeApiTest {
|
||||||
@Test
|
@Test
|
||||||
fun `Get Joke`() {
|
fun `Get Joke`() {
|
||||||
val joke = getJoke()
|
val joke = getJoke()
|
||||||
logger.log(Level.FINE, joke.toString())
|
logger.fine(joke.toString())
|
||||||
assertAll("no param",
|
assertAll("no param",
|
||||||
{ assertFalse(joke.error) { "error should be false" } },
|
{ assertFalse(joke.error) { "error should be false" } },
|
||||||
{ assertTrue(joke.joke.isNotEmpty()) { "joke should not be empty" } },
|
{ assertTrue(joke.joke.isNotEmpty()) { "joke should not be empty" } },
|
||||||
|
@ -69,11 +69,24 @@ internal class JokeApiTest {
|
||||||
{ assertEquals(Language.EN, joke.language) { "language should be english" } })
|
{ assertEquals(Language.EN, joke.language) { "language should be english" } })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `Get Joke with all Blacklist Flags`() {
|
||||||
|
val joke = getJoke(flags = setOf(Flag.ALL))
|
||||||
|
assertAll("joke with all flags", { assertTrue(joke.flags.isEmpty()) { "flags should be empty." } })
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `Get Joke with no Blacklist Flags`() {
|
||||||
|
val noFlags = Flag.values().filter { it != Flag.ALL }.toSet()
|
||||||
|
val joke = getJoke(flags = noFlags)
|
||||||
|
assertAll("joke with no flags", { assertTrue(joke.flags.isEmpty()) { "flags should be empty." } })
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Get Joke with ID`() {
|
fun `Get Joke with ID`() {
|
||||||
val id = 172
|
val id = 172
|
||||||
val joke = getJoke(idRange = IdRange(id))
|
val joke = getJoke(idRange = IdRange(id))
|
||||||
logger.log(Level.FINE, joke.toString())
|
logger.fine(joke.toString())
|
||||||
assertAll("joke by id",
|
assertAll("joke by id",
|
||||||
{ assertTrue(joke.flags.contains(Flag.NSFW) && joke.flags.contains(Flag.EXPLICIT)) { "nsfw & explicit" } },
|
{ assertTrue(joke.flags.contains(Flag.NSFW) && joke.flags.contains(Flag.EXPLICIT)) { "nsfw & explicit" } },
|
||||||
{ assertEquals(172, joke.id) { "id is $id" } },
|
{ assertEquals(172, joke.id) { "id is $id" } },
|
||||||
|
@ -84,10 +97,26 @@ internal class JokeApiTest {
|
||||||
fun `Get Joke with ID Range`() {
|
fun `Get Joke with ID Range`() {
|
||||||
val idRange = IdRange(1, 100)
|
val idRange = IdRange(1, 100)
|
||||||
val joke = getJoke(idRange = idRange)
|
val joke = getJoke(idRange = idRange)
|
||||||
logger.log(Level.FINE, joke.toString())
|
logger.fine(joke.toString())
|
||||||
assertTrue(joke.id >= idRange.start && joke.id <= idRange.end) { "id should be in range" }
|
assertTrue(joke.id >= idRange.start && joke.id <= idRange.end) { "id should be in range" }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `Get Joke with invalid ID Range`() {
|
||||||
|
val joke = getJoke(idRange = IdRange(100, 1))
|
||||||
|
logger.fine(joke.toString())
|
||||||
|
assertFalse(joke.error) { "should not be an error" }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `Get Joke with max ID Range`() {
|
||||||
|
val e = assertThrows<JokeException> { getJoke(idRange = IdRange(1, 30000)) }
|
||||||
|
assertAll("joke with max ID range",
|
||||||
|
{ assertTrue(e.error) { "should be an error" } },
|
||||||
|
{ assertTrue(e.additionalInfo.contains("ID range")) { "should contain ID range" } }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Get Joke with each Categories`() {
|
fun `Get Joke with each Categories`() {
|
||||||
Category.values().filter { it != Category.ANY }.forEach {
|
Category.values().filter { it != Category.ANY }.forEach {
|
||||||
|
@ -107,16 +136,19 @@ internal class JokeApiTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Get Joke with Newline`() {
|
fun `Get Joke with Newline`() {
|
||||||
val joke =
|
val joke = getJoke(
|
||||||
getJoke(categories = setOf(Category.DARK), type = Type.SINGLE, idRange = IdRange(178), splitNewLine = false)
|
categories = setOf(Category.DARK),
|
||||||
logger.log(Level.FINE, joke.toString())
|
type = Type.SINGLE,
|
||||||
assertEquals(1, joke.joke.size) { "should be a oneliner" }
|
idRange = IdRange(178),
|
||||||
|
splitNewLine = false
|
||||||
|
)
|
||||||
|
assertTrue(joke.joke.toString().contains("\n")) { "should contain newline" }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Get Safe Joke`() {
|
fun `Get Safe Joke`() {
|
||||||
val joke = getJoke(safe = true)
|
val joke = getJoke(safe = true)
|
||||||
logger.log(Level.FINE, joke.toString())
|
logger.fine(joke.toString())
|
||||||
assertAll("safe joke",
|
assertAll("safe joke",
|
||||||
{ assertTrue(joke.safe) { "should be safe" } },
|
{ assertTrue(joke.safe) { "should be safe" } },
|
||||||
{ assertTrue(joke.flags.isEmpty()) { "flags should be empty" } })
|
{ assertTrue(joke.flags.isEmpty()) { "flags should be empty" } })
|
||||||
|
@ -125,14 +157,14 @@ internal class JokeApiTest {
|
||||||
@Test
|
@Test
|
||||||
fun `Get Single Joke`() {
|
fun `Get Single Joke`() {
|
||||||
val joke = getJoke(type = Type.SINGLE)
|
val joke = getJoke(type = Type.SINGLE)
|
||||||
logger.log(Level.FINE, joke.toString())
|
logger.fine(joke.toString())
|
||||||
assertEquals(Type.SINGLE, joke.type) { "type should be single" }
|
assertEquals(Type.SINGLE, joke.type) { "type should be single" }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Get Two-Parts Joke`() {
|
fun `Get Two-Parts Joke`() {
|
||||||
val joke = getJoke(type = Type.TWOPART)
|
val joke = getJoke(type = Type.TWOPART)
|
||||||
logger.log(Level.FINE, joke.toString())
|
logger.fine(joke.toString())
|
||||||
assertAll("two-part joke",
|
assertAll("two-part joke",
|
||||||
{ assertEquals(Type.TWOPART, joke.type) { "type should be two-part" } },
|
{ assertEquals(Type.TWOPART, joke.type) { "type should be two-part" } },
|
||||||
{ assertTrue(joke.joke.size > 1) { "should have multiple lines" } })
|
{ assertTrue(joke.joke.size > 1) { "should have multiple lines" } })
|
||||||
|
@ -142,40 +174,42 @@ internal class JokeApiTest {
|
||||||
fun `Get Joke using Search`() {
|
fun `Get Joke using Search`() {
|
||||||
val id = 1
|
val id = 1
|
||||||
val joke = getJoke(search = "man", categories = setOf(Category.PROGRAMMING), idRange = IdRange(id), safe = true)
|
val joke = getJoke(search = "man", categories = setOf(Category.PROGRAMMING), idRange = IdRange(id), safe = true)
|
||||||
logger.log(Level.FINE, joke.toString())
|
logger.fine(joke.toString())
|
||||||
assertEquals(id, joke.id) { "id should be 1" }
|
assertEquals(id, joke.id) { "id should be $id" }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Get Raw Joke with TXT`() {
|
fun `Get Raw Joke with TXT`() {
|
||||||
val response = getRawJoke(format = Format.TEXT)
|
val response = getRawJoke(format = Format.TEXT)
|
||||||
logger.log(Level.FINE, response)
|
|
||||||
assertAll("plain text",
|
assertAll("plain text",
|
||||||
{ assertTrue(response.isNotEmpty()) { "should be not empty" } },
|
{ assertTrue(response.isNotEmpty()) { "should be not empty" } },
|
||||||
{ assertFalse(response.startsWith("Error ")) { "should not be an error" } })
|
{ assertFalse(response.startsWith("Error ")) { "should not be an error" } })
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Get Raw Joke with invalid ID Range`() {
|
fun `Get Raw Joke with invalid Amount`() {
|
||||||
val response = getRawJoke(format = Format.TXT, idRange = IdRange(0, 30000))
|
val response = getRawJoke(amount = 100)
|
||||||
logger.log(Level.FINE, response)
|
assertFalse(response.contains("\"amount\":")) { "should not have amount" }
|
||||||
assertTrue(response.startsWith("Error ")) { "should be an error" }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Get Raw Joke with XML`() {
|
fun `Get Raw Joke with XML`() {
|
||||||
val response = getRawJoke(format = Format.XML)
|
val response = getRawJoke(format = Format.XML)
|
||||||
logger.log(Level.FINE, response)
|
|
||||||
assertTrue(response.startsWith("<?xml version='1.0'?>\n<data>\n <error>false</error>")) { "should be xml" }
|
assertTrue(response.startsWith("<?xml version='1.0'?>\n<data>\n <error>false</error>")) { "should be xml" }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Get Raw Joke with YAML`() {
|
fun `Get Raw Joke with YAML`() {
|
||||||
val response = getRawJoke(format = Format.YAML)
|
val response = getRawJoke(format = Format.YAML)
|
||||||
logger.log(Level.FINE, response)
|
|
||||||
assertTrue(response.startsWith("error: false")) { "should be yaml" }
|
assertTrue(response.startsWith("error: false")) { "should be yaml" }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `Get Raw Jokes`() {
|
||||||
|
val response = getRawJoke(amount = 2)
|
||||||
|
assertTrue(response.contains("\"amount\": 2")) { "amount should be 2" }
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Fetch Invalid URL`() {
|
fun `Fetch Invalid URL`() {
|
||||||
val statusCode = 999
|
val statusCode = 999
|
||||||
|
@ -193,7 +227,7 @@ internal class JokeApiTest {
|
||||||
val e = assertThrows<JokeException> {
|
val e = assertThrows<JokeException> {
|
||||||
getJoke(categories = setOf(Category.CHRISTMAS), search = "foo")
|
getJoke(categories = setOf(Category.CHRISTMAS), search = "foo")
|
||||||
}
|
}
|
||||||
logger.log(Level.FINE, e.debug())
|
logger.fine(e.debug())
|
||||||
assertAll("JokeException validation",
|
assertAll("JokeException validation",
|
||||||
{ assertEquals(106, e.code) { "code should be valid" } },
|
{ assertEquals(106, e.code) { "code should be valid" } },
|
||||||
{ assertTrue(e.error) { "should be an error" } },
|
{ assertTrue(e.error) { "should be an error" } },
|
||||||
|
@ -211,7 +245,7 @@ internal class JokeApiTest {
|
||||||
val e = assertThrows<HttpErrorException> {
|
val e = assertThrows<HttpErrorException> {
|
||||||
fetchUrl("https://httpstat.us/$input")
|
fetchUrl("https://httpstat.us/$input")
|
||||||
}
|
}
|
||||||
assertAll("JokeException validation",
|
assertAll("HttpErrorException validation",
|
||||||
{ assertEquals(input, e.statusCode) { "status code should be $input" } },
|
{ assertEquals(input, e.statusCode) { "status code should be $input" } },
|
||||||
{ assertTrue(e.message!!.isNotEmpty()) { "message for $input should not be empty" } },
|
{ assertTrue(e.message!!.isNotEmpty()) { "message for $input should not be empty" } },
|
||||||
{ assertTrue(e.cause!!.message!!.isNotEmpty()) { "cause of $input should not be empty" } })
|
{ assertTrue(e.cause!!.message!!.isNotEmpty()) { "cause of $input should not be empty" } })
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue