Commit e318eae6 authored by HankG's avatar HankG

Resolve "Likes Controller Harness"

parent 907e6ec3
......@@ -9,10 +9,16 @@ version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
jcenter()
mavenCentral()
}
dependencies {
compile 'com.github.kittinunf.fuel:fuel:+' //for JVM
compile 'com.github.kittinunf.fuel:fuel-jackson:+' //for Jackson support
compile 'com.github.kittinunf.fuel:fuel-forge:+' //for Forge support
compile 'com.github.kittinunf.fuel:fuel-coroutines:+' //for Kotlin Coroutines support
compile 'com.github.kittinunf.fuel:fuel-gson:+'
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
......
#Tue Oct 16 20:33:24 EDT 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-all.zip
package hankg.diaspora.api;
import java.io.File
private val defaultAcPath = "/diaspora_ac.txt"
val server = "http://localhost:3000"
val apiBase = "api/v1"
val baseUrl = "$server/$apiBase"
val defaultAccessToken = File(defaultAcPath).readLines().last()
package hankg.diaspora.api
class Library1 {
fun doubleValue(value:Int) = 2*value
fun squareValue(value:Int) = value * value
}
\ No newline at end of file
package hankg.diaspora.api
import com.github.kittinunf.fuel.core.FuelManager
import com.github.kittinunf.fuel.core.Method
import com.github.kittinunf.fuel.util.FuelRouting
sealed class LikesApi(val postGuid: String, val accessToken: String) : FuelRouting {
override val basePath = baseUrl
class showLikes(postGuid: String, accessToken: String): LikesApi(postGuid, accessToken) {}
class addLike(postGuid: String, accessToken: String): LikesApi(postGuid, accessToken) {}
class removeLike(postGuid: String, accessToken: String): LikesApi(postGuid, accessToken) {}
override val method: Method
get() {
when(this) {
is showLikes -> return Method.GET
is addLike -> return Method.POST
is removeLike -> return Method.DELETE
}
}
override val path: String
get() = "posts/${this.postGuid}/likes"
override val params:List<Pair<String,Any?>>?
get() = listOf("access_token" to this.accessToken)
override val headers: Map<String, String>?
get() = null
override val body: String?
get() = null
}
\ No newline at end of file
package hankg.diaspora.api
import com.github.kittinunf.fuel.core.ResponseDeserializable
import com.google.gson.Gson
data class Author(val guid:String = "",
val diaspora_id:String = "",
val name:String = "",
val avatar:String = "") {
class Deserializer : ResponseDeserializable<Author> {
override fun deserialize(content: String): Author = Gson().fromJson(content, Author::class.java)
}
}
data class Like(val guid:String = "",
val author:Author = Author()) {
class Deserializer : ResponseDeserializable<Like> {
override fun deserialize(content: String): Like = Gson().fromJson(content, Like::class.java)
}
class ArrayDeserializer : ResponseDeserializable<List<Like>> {
override fun deserialize(content: String): List<Like> = Gson().fromJson(content, Array<Like>::class.java).toList()
}
}
package hankg.diaspora.api
import com.github.kittinunf.fuel.Fuel
fun main(args : Array<String>) {
println("access_token = $defaultAccessToken")
val postGuid = "2ae87b40b3ae0136d9b1080027f3b408"
val (_, _, result) = Fuel.request(LikesApi.showLikes(postGuid, defaultAccessToken)).responseObject(Like.ArrayDeserializer())
println(result)
println("Done")
}
package hankg.diaspora.api
import junit.framework.TestCase.assertEquals
import org.junit.Test
class TestLibrary1 {
@Test
fun testDoubleValue() {
val x = 2
val expected = 4
val lib1 = Library1()
val actual = lib1.doubleValue(x);
assertEquals(expected, actual)
}
@Test
fun testSquareValue() {
val x = 3
val expected = 9
val lib1 = Library1()
val actual = lib1.squareValue(x);
assertEquals(expected, actual)
}
}
\ No newline at end of file
package hankg.diaspora.api
import com.github.kittinunf.fuel.Fuel
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.fail
import org.junit.Test
class TestLikesApi {
val postGuid = "2ae87b40b3ae0136d9b1080027f3b408"
@Test
fun testShowLikes() {
val (_, response, result) = Fuel.request(LikesApi.showLikes(postGuid, defaultAccessToken)).responseObject(Like.ArrayDeserializer())
assertEquals(response.statusCode, 200)
println(result)
}
@Test
fun testAddLike() {
var (_, response, _) = Fuel.request(LikesApi.addLike(postGuid, defaultAccessToken)).responseObject(Like.ArrayDeserializer())
if(response.statusCode == 422) {
var errorMessage = String(response.data)
if(errorMessage == "Like already exists") {
var (_, removeResponse, _) = Fuel.request(LikesApi.removeLike(postGuid, defaultAccessToken)).responseObject(Like.ArrayDeserializer())
assertEquals(removeResponse.statusCode, 204)
var (_, addAgainResponse, _) = Fuel.request(LikesApi.addLike(postGuid, defaultAccessToken)).responseObject(Like.ArrayDeserializer())
assertEquals(addAgainResponse.statusCode, 204)
} else {
fail()
}
} else {
assertEquals(response.statusCode, 204);
var (_, removeResponse, _) = Fuel.request(LikesApi.removeLike(postGuid, defaultAccessToken)).responseObject(Like.ArrayDeserializer())
assertEquals(removeResponse.statusCode, 204)
}
}
@Test
fun testRemoveLike() {
var (_, response, _) = Fuel.request(LikesApi.removeLike(postGuid, defaultAccessToken)).responseObject(Like.ArrayDeserializer())
assertEquals(response.statusCode, 204);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment