Cleanup.
This commit is contained in:
parent
c0fc862bb4
commit
191b09060b
6 changed files with 132 additions and 96 deletions
9
.idea/bitly-shorten.iml
generated
Normal file
9
.idea/bitly-shorten.iml
generated
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/bitly-shorten.iml" filepath="$PROJECT_DIR$/.idea/bitly-shorten.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -50,7 +50,7 @@ import java.util.logging.Logger
|
||||||
open class Utils private constructor() {
|
open class Utils private constructor() {
|
||||||
companion object {
|
companion object {
|
||||||
/** The logger instance. */
|
/** The logger instance. */
|
||||||
val logger: Logger by lazy { Logger.getLogger(Bitly::class.java.name) }
|
val logger: Logger by lazy { Logger.getLogger(Utils::class.java.name) }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes an API call.
|
* Executes an API call.
|
||||||
|
@ -70,8 +70,7 @@ open class Utils private constructor() {
|
||||||
): CallResponse {
|
): CallResponse {
|
||||||
val response = CallResponse()
|
val response = CallResponse()
|
||||||
if (validateCall(accessToken, endPoint)) {
|
if (validateCall(accessToken, endPoint)) {
|
||||||
val apiUrl = endPoint.toHttpUrlOrNull()
|
endPoint.toHttpUrlOrNull()?.let { apiUrl ->
|
||||||
if (apiUrl != null) {
|
|
||||||
val builder = when (method) {
|
val builder = when (method) {
|
||||||
Methods.POST, Methods.PATCH -> {
|
Methods.POST, Methods.PATCH -> {
|
||||||
val formBody = JSONObject(params).toString()
|
val formBody = JSONObject(params).toString()
|
||||||
|
@ -117,8 +116,7 @@ open class Utils private constructor() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun parseBody(endPoint: String, result: Response): String {
|
private fun parseBody(endPoint: String, result: Response): String {
|
||||||
val body = result.body?.string()
|
result.body?.string()?.let { body ->
|
||||||
if (body != null) {
|
|
||||||
if (!result.isSuccessful && body.isNotEmpty()) {
|
if (!result.isSuccessful && body.isNotEmpty()) {
|
||||||
try {
|
try {
|
||||||
with(JSONObject(body)) {
|
with(JSONObject(body)) {
|
||||||
|
|
|
@ -32,16 +32,14 @@
|
||||||
|
|
||||||
package net.thauvin.erik.bitly
|
package net.thauvin.erik.bitly
|
||||||
|
|
||||||
|
import net.thauvin.erik.bitly.Utils.Companion.isValidUrl
|
||||||
import net.thauvin.erik.bitly.Utils.Companion.removeHttp
|
import net.thauvin.erik.bitly.Utils.Companion.removeHttp
|
||||||
import net.thauvin.erik.bitly.Utils.Companion.toEndPoint
|
import net.thauvin.erik.bitly.Utils.Companion.toEndPoint
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import org.junit.Before
|
import org.junit.Before
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.util.logging.Level
|
import java.util.logging.Level
|
||||||
import kotlin.test.Test
|
import kotlin.test.*
|
||||||
import kotlin.test.assertEquals
|
|
||||||
import kotlin.test.assertNotEquals
|
|
||||||
import kotlin.test.assertTrue
|
|
||||||
|
|
||||||
class BitlyTest {
|
class BitlyTest {
|
||||||
private val bitly = with(File("local.properties")) {
|
private val bitly = with(File("local.properties")) {
|
||||||
|
@ -89,6 +87,11 @@ class BitlyTest {
|
||||||
assertEquals(shortUrl, bitly.bitlinks().shorten(shortUrl))
|
assertEquals(shortUrl, bitly.bitlinks().shorten(shortUrl))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `endPoint should be specified`() {
|
||||||
|
assertFalse(bitly.call("").isSuccessful)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `shorten = expand`() {
|
fun `shorten = expand`() {
|
||||||
val shortUrl = bitly.bitlinks().shorten(longUrl, domain = "bit.ly")
|
val shortUrl = bitly.bitlinks().shorten(longUrl, domain = "bit.ly")
|
||||||
|
@ -102,7 +105,11 @@ class BitlyTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `get user`() {
|
fun `get user`() {
|
||||||
assertTrue(bitly.call("/user".toEndPoint(), method = Methods.GET).isSuccessful)
|
assertTrue(bitly.call("user".toEndPoint(), method = Methods.GET).isSuccessful)
|
||||||
|
assertTrue(
|
||||||
|
Utils.call(bitly.accessToken, "/user".toEndPoint(), method = Methods.GET).isSuccessful,
|
||||||
|
"call(/user)"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -154,6 +161,10 @@ class BitlyTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `create bitlink`() {
|
fun `create bitlink`() {
|
||||||
|
assertTrue(
|
||||||
|
bitly.bitlinks().create(long_url = longUrl).matches("https://\\w+.\\w{2}/\\w{7}".toRegex()),
|
||||||
|
"just long_url"
|
||||||
|
)
|
||||||
assertEquals(
|
assertEquals(
|
||||||
shortUrl,
|
shortUrl,
|
||||||
bitly.bitlinks().create(
|
bitly.bitlinks().create(
|
||||||
|
@ -173,7 +184,17 @@ class BitlyTest {
|
||||||
bl.update(shortUrl, title = "Erik's Weblog", tags = arrayOf("blog", "weblog"))
|
bl.update(shortUrl, title = "Erik's Weblog", tags = arrayOf("blog", "weblog"))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
assertTrue(
|
||||||
|
bl.update(shortUrl, tags = emptyArray(), toJson = true).contains("\"tags\":[]"), "update tags toJson"
|
||||||
|
)
|
||||||
|
|
||||||
bl.update(shortUrl, link = longUrl)
|
bl.update(shortUrl, link = longUrl)
|
||||||
assertTrue(bl.lastCallResponse.isUnprocessableEntity, "422 Unprocessable")
|
assertTrue(bl.lastCallResponse.isUnprocessableEntity, "422 Unprocessable")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `validate URL`() {
|
||||||
|
assertTrue("https://www.example.com".isValidUrl(), "valid url")
|
||||||
|
assertFalse("this is a test".isValidUrl(), "invalid url")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue