-
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.
Merge pull request #3126 from CruGlobal/syncFavorites
GT-1796 Sync Favorite tools with the API
- Loading branch information
Showing
37 changed files
with
1,545 additions
and
27 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,21 @@ | ||
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.model.JsonApiRetrofitObject | ||
import org.cru.godtools.model.User | ||
import retrofit2.Response | ||
import retrofit2.http.Body | ||
import retrofit2.http.GET | ||
import retrofit2.http.PATCH | ||
import retrofit2.http.QueryMap | ||
|
||
internal const val PATH_USER = "users/me" | ||
|
||
interface UserApi { | ||
@GET(PATH_USER) | ||
suspend fun getUser(): Response<JsonApiObject<User>> | ||
suspend fun getUser(@QueryMap params: JsonApiParams = JsonApiParams()): Response<JsonApiObject<User>> | ||
|
||
@PATCH(PATH_USER) | ||
suspend fun updateUser(@Body user: JsonApiRetrofitObject<User>): Response<JsonApiObject<User>> | ||
} |
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() | ||
} | ||
} | ||
} |
Oops, something went wrong.