This commit is contained in:
Erik C. Thauvin 2021-05-08 23:09:50 -07:00
parent aac0d273cf
commit 1a80f6ab89
13 changed files with 154 additions and 21 deletions

View file

@ -32,11 +32,11 @@
package net.thauvin.erik.crypto
@Suppress("EmptySecondaryConstructor")
@Suppress("EmptySecondaryConstructor", "unused")
class CryptoException : Exception {
constructor(message: String, cause: Throwable) : super(message, cause) {}
constructor(message: String) : super(message) {}
constructor(cause: Throwable) : super(cause) {}
constructor(message: String, cause: Throwable) : super(message, cause)
constructor(message: String) : super(message)
constructor(cause: Throwable) : super(cause)
companion object {
private const val serialVersionUID = 1L

View file

@ -36,8 +36,6 @@ import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request
import org.json.JSONObject
import java.io.IOException
import java.net.URL
import java.time.LocalDate
/**
@ -54,9 +52,10 @@ open class CryptoPrice(val base: String, val currency: String, val amount: Doubl
val json = JSONObject(this)
if (json.has("data")) {
with(json.getJSONObject("data")) {
return CryptoPrice(getString("base"), getString("currency"), getString("amount").toDouble()
)
}
return CryptoPrice(
getString("base"), getString("currency"), getString("amount").toDouble()
)
}
} else {
throw CryptoException("Missing JSON data.")
}

View file

@ -2,11 +2,11 @@ package net.thauvin.erik.crypto
import net.thauvin.erik.crypto.CryptoPrice.Companion.marketPrice
import net.thauvin.erik.crypto.CryptoPrice.Companion.toPrice
import java.time.LocalDate
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue
import java.time.LocalDate
/**
* The `CryptoPriceTest` class.
@ -52,24 +52,25 @@ class CryptoPriceTest {
@Throws(CryptoException::class)
fun testMarketPriceExceptions() {
assertFailsWith(
message = "FOO did not fail",
exceptionClass = CryptoException::class,
block = { marketPrice("FOO") }
message = "FOO did not fail",
exceptionClass = CryptoException::class,
block = { marketPrice("FOO") }
)
assertFailsWith(
message = "BAR did not fail",
exceptionClass = CryptoException::class,
block = { marketPrice("BTC", "BAR") }
message = "BAR did not fail",
exceptionClass = CryptoException::class,
block = { marketPrice("BTC", "BAR") }
)
}
@Test
@Throws(CryptoException::class)
fun testToPrice() {
val price = "{\"data\":{\"base\":\"BTC\",\"currency\":\"USD\",\"amount\":\"57515.69\"}}".toPrice()
val d = 57515.69
val price = "{\"data\":{\"base\":\"BTC\",\"currency\":\"USD\",\"amount\":\"$d\"}}".toPrice()
assertEquals(price.base, "BTC", "base is BTC")
assertEquals(price.currency, "USD", "currency is USD")
assertEquals(price.amount, "57515.69".toDouble(), "amount is 57515.69")
assertEquals(price.amount, d, "amount is 57515.69")
}
}