Cleaned up tests with assertk

This commit is contained in:
Erik C. Thauvin 2022-10-04 13:54:00 -07:00
parent f46aa0e2a5
commit 82d31578c8
90 changed files with 5572 additions and 3926 deletions

View file

@ -254,7 +254,7 @@ open class Bitlinks(private val accessToken: String) {
lastCallResponse = Utils.call(
accessToken, "/bitlinks/${bitlink.removeHttp()}".toEndPoint(), mutableMapOf<String, Any>().apply {
if (references.isNotEmpty()) put("references", references)
if (archived) put("archived", archived)
if (archived) put("archived", true)
if (tags.isNotEmpty()) put("tags", tags)
if (created_at.isNotBlank()) put("created_at", created_at)
if (title.isNotBlank()) put("title", title)

View file

@ -84,6 +84,7 @@ open class Utils private constructor() {
}
}
}
Methods.DELETE -> Request.Builder().url(apiUrl.newBuilder().build()).delete()
else -> { // Methods.GET
val httpUrl = apiUrl.newBuilder().apply {
@ -189,9 +190,11 @@ open class Utils private constructor() {
endPoint.isBlank() -> {
if (logger.isSevereLoggable()) logger.severe("Please specify a valid API endpoint.")
}
accessToken.isBlank() -> {
if (logger.isSevereLoggable()) logger.severe("Please specify a valid API access token.")
}
else -> return true
}
return false

View file

@ -32,6 +32,14 @@
package net.thauvin.erik.bitly
import assertk.all
import assertk.assertThat
import assertk.assertions.contains
import assertk.assertions.isEqualTo
import assertk.assertions.isFalse
import assertk.assertions.isTrue
import assertk.assertions.matches
import assertk.assertions.prop
import net.thauvin.erik.bitly.Utils.Companion.isValidUrl
import net.thauvin.erik.bitly.Utils.Companion.removeHttp
import net.thauvin.erik.bitly.Utils.Companion.toEndPoint
@ -39,7 +47,11 @@ import org.json.JSONObject
import org.junit.Before
import java.io.File
import java.util.logging.Level
import kotlin.test.*
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotEquals
import kotlin.test.assertTrue
class BitlyTest {
private val bitly = with(File("local.properties")) {
@ -89,7 +101,7 @@ class BitlyTest {
@Test
fun `endPoint should be specified`() {
assertFalse(bitly.call("").isSuccessful)
assertThat(bitly.call("")).prop(CallResponse::isSuccessful).isFalse()
}
@Test
@ -105,11 +117,10 @@ class BitlyTest {
@Test
fun `get user`() {
assertTrue(bitly.call("user".toEndPoint(), method = Methods.GET).isSuccessful)
assertTrue(
Utils.call(bitly.accessToken, "/user".toEndPoint(), method = Methods.GET).isSuccessful,
"call(/user)"
)
assertThat(bitly.call("user".toEndPoint(), method = Methods.GET), "call(user)")
.prop(CallResponse::isSuccessful).isTrue()
assertThat(Utils.call(bitly.accessToken, "/user".toEndPoint(), method = Methods.GET), "call(/user)")
.prop(CallResponse::isSuccessful).isTrue()
}
@Test
@ -139,18 +150,18 @@ class BitlyTest {
fun `bitlinks lastCallResponse`() {
val bl = Bitlinks(bitly.accessToken)
bl.shorten(longUrl, domain = "bit.ly")
with(bl.lastCallResponse) {
assertEquals(true, isSuccessful, "is successful")
assertEquals(200, resultCode, "resultCode == 200")
assertTrue(body.contains("\"link\":\"$shortUrl\""), "valid body")
assertThat(bl.lastCallResponse, "shorten(longUrl)").all {
prop(CallResponse::isSuccessful).isTrue()
prop(CallResponse::resultCode).isEqualTo(200)
prop(CallResponse::body).contains("\"link\":\"$shortUrl\"")
}
bl.shorten(shortUrl)
with(bl.lastCallResponse) {
assertEquals(false, isSuccessful, "is not successful")
assertEquals(400, resultCode, "resultCode == 400")
assertTrue(isBadRequest, "is bad request")
assertTrue(body.contains("ALREADY_A_BITLY_LINK"), "already a bitlink")
assertThat(bl.lastCallResponse, "shorten(shortUrl)").all {
prop(CallResponse::isSuccessful).isFalse()
prop(CallResponse::resultCode).isEqualTo(400)
prop(CallResponse::isBadRequest).isTrue()
prop(CallResponse::body).contains("ALREADY_A_BITLY_LINK")
}
}
@ -161,10 +172,8 @@ class BitlyTest {
@Test
fun `create bitlink`() {
assertTrue(
bitly.bitlinks().create(long_url = longUrl).matches("https://\\w+.\\w{2}/\\w{7}".toRegex()),
"just long_url"
)
assertThat(bitly.bitlinks().create(long_url = longUrl), "create(longUrl)")
.matches("https://\\w+.\\w{2}/\\w{7}".toRegex())
assertEquals(
shortUrl,
bitly.bitlinks().create(
@ -181,15 +190,14 @@ class BitlyTest {
val bl = bitly.bitlinks()
assertEquals(
Constants.TRUE,
bl.update(shortUrl, title = "Erik's Weblog", tags = arrayOf("blog", "weblog"))
bl.update(shortUrl, title = "Erik's Weblog", tags = arrayOf("blog", "weblog"), archived = true)
)
assertTrue(
bl.update(shortUrl, tags = emptyArray(), toJson = true).contains("\"tags\":[]"), "update tags toJson"
)
assertThat(bl.update(shortUrl, tags = emptyArray(), toJson = true), "update(tags)")
.contains("\"tags\":[]")
bl.update(shortUrl, link = longUrl)
assertTrue(bl.lastCallResponse.isUnprocessableEntity, "422 Unprocessable")
assertThat(bl.lastCallResponse).prop(CallResponse::isUnprocessableEntity).isTrue()
}
@Test