-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add an API interface to add/remove favorite tools
- Loading branch information
Showing
5 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
library/api/src/main/kotlin/org/cru/godtools/api/UserFavoriteToolsApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.cru.godtools.api | ||
|
||
import org.ccci.gto.android.common.jsonapi.model.JsonApiObject | ||
import org.ccci.gto.android.common.jsonapi.retrofit2.JsonApiParams | ||
import org.ccci.gto.android.common.jsonapi.retrofit2.annotation.JsonApiFields | ||
import org.cru.godtools.model.Tool | ||
import retrofit2.Response | ||
import retrofit2.http.Body | ||
import retrofit2.http.HTTP | ||
import retrofit2.http.POST | ||
import retrofit2.http.QueryMap | ||
|
||
interface UserFavoriteToolsApi { | ||
companion object { | ||
internal const val PATH_FAVORITE_TOOLS = "$PATH_USER/relationships/favorite-tools" | ||
} | ||
|
||
@POST(PATH_FAVORITE_TOOLS) | ||
suspend fun addFavoriteTools( | ||
@QueryMap params: JsonApiParams = JsonApiParams(), | ||
@Body | ||
@JsonApiFields(Tool.JSONAPI_TYPE) | ||
tools: List<Tool>, | ||
): Response<JsonApiObject<Tool>> | ||
|
||
@HTTP(method = "DELETE", path = PATH_FAVORITE_TOOLS, hasBody = true) | ||
suspend fun removeFavoriteTools( | ||
@QueryMap params: JsonApiParams = JsonApiParams(), | ||
@Body | ||
@JsonApiFields(Tool.JSONAPI_TYPE) | ||
tools: List<Tool>, | ||
): Response<JsonApiObject<Tool>> | ||
} |
46 changes: 46 additions & 0 deletions
46
library/api/src/test/kotlin/org/cru/godtools/api/UserFavoriteToolsApiTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.cru.godtools.api | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotNull | ||
import kotlinx.coroutines.test.runTest | ||
import net.javacrumbs.jsonunit.assertj.assertThatJson | ||
import okhttp3.mockwebserver.MockResponse | ||
import okhttp3.mockwebserver.MockWebServer | ||
import org.ccci.gto.android.common.jsonapi.retrofit2.JsonApiConverterFactory | ||
import org.cru.godtools.api.UserFavoriteToolsApi.Companion.PATH_FAVORITE_TOOLS | ||
import org.cru.godtools.model.Tool | ||
import org.junit.Rule | ||
import retrofit2.Retrofit | ||
|
||
private const val JSON_RESPONSE_FAVORITES = "{data:[{id:5,type:\"resource\"}]}" | ||
|
||
class UserFavoriteToolsApiTest { | ||
@get:Rule | ||
val server = MockWebServer() | ||
|
||
private var api = Retrofit.Builder() | ||
.baseUrl(server.url("/")) | ||
.addConverterFactory(JsonApiConverterFactory(Tool::class.java)) | ||
.build() | ||
.create(UserFavoriteToolsApi::class.java) | ||
|
||
@Test | ||
fun `removeFavoriteTools()`() = runTest { | ||
server.enqueue(MockResponse().setBody(JSON_RESPONSE_FAVORITES)) | ||
|
||
val resp = api.removeFavoriteTools(tools = listOf(Tool("en") { id = 1 })).body()!!.data.single() | ||
assertNotNull(resp) { assertEquals(5, it.id) } | ||
|
||
val request = server.takeRequest() | ||
assertEquals("DELETE", request.method) | ||
assertEquals("/$PATH_FAVORITE_TOOLS", request.path) | ||
assertThatJson(request.body.readUtf8()) { | ||
isObject | ||
node("data").isArray.hasSize(1) | ||
node("data[0].id").isEqualTo(1) | ||
node("data[0].attributes").isAbsent() | ||
node("data[0].relationships").isAbsent() | ||
} | ||
} | ||
} |