From 6911fafe0ada1ef90918b5e65a2f1df7b8dc50a9 Mon Sep 17 00:00:00 2001 From: ashiagr Date: Thu, 27 Jan 2022 17:43:38 +0530 Subject: [PATCH 1/8] Feature: Support error field inside response and model --- .../fluxc/model/dashboard/CardModel.kt | 18 ++++++++++------- .../rest/wpcom/dashboard/CardsRestClient.kt | 16 ++++++++------- .../fluxc/store/dashboard/CardsStore.kt | 20 +++++++++++++++++++ 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/model/dashboard/CardModel.kt b/fluxc/src/main/java/org/wordpress/android/fluxc/model/dashboard/CardModel.kt index 7e96029763..a259cf610d 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/model/dashboard/CardModel.kt +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/model/dashboard/CardModel.kt @@ -1,5 +1,7 @@ package org.wordpress.android.fluxc.model.dashboard +import org.wordpress.android.fluxc.store.dashboard.CardsStore.PostCardError +import org.wordpress.android.fluxc.store.dashboard.CardsStore.TodaysStatsCardError import java.util.Date sealed class CardModel( @@ -13,16 +15,18 @@ sealed class CardModel( } data class TodaysStatsCardModel( - val views: Int, - val visitors: Int, - val likes: Int, - val comments: Int + val views: Int = 0, + val visitors: Int = 0, + val likes: Int = 0, + val comments: Int = 0, + val error: TodaysStatsCardError? = null ) : CardModel(Type.TODAYS_STATS) data class PostsCardModel( - val hasPublished: Boolean, - val draft: List, - val scheduled: List + val hasPublished: Boolean = false, + val draft: List = emptyList(), + val scheduled: List = emptyList(), + val error: PostCardError? = null ) : CardModel(Type.POSTS) { data class PostCardModel( val id: Int, diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/network/rest/wpcom/dashboard/CardsRestClient.kt b/fluxc/src/main/java/org/wordpress/android/fluxc/network/rest/wpcom/dashboard/CardsRestClient.kt index b62eb5e7e7..fac3a0a8da 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/network/rest/wpcom/dashboard/CardsRestClient.kt +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/network/rest/wpcom/dashboard/CardsRestClient.kt @@ -62,7 +62,8 @@ class CardsRestClient @Inject constructor( @SerializedName("views") val views: Int? = null, @SerializedName("visitors") val visitors: Int? = null, @SerializedName("likes") val likes: Int? = null, - @SerializedName("comments") val comments: Int? = null + @SerializedName("comments") val comments: Int? = null, + @SerializedName("error") val error: String? = null ) { fun toTodaysStatsCard() = TodaysStatsCardModel( views = views ?: 0, @@ -73,14 +74,15 @@ class CardsRestClient @Inject constructor( } data class PostsResponse( - @SerializedName("has_published") val hasPublished: Boolean, - @SerializedName("draft") val draft: List, - @SerializedName("scheduled") val scheduled: List + @SerializedName("has_published") val hasPublished: Boolean? = null, + @SerializedName("draft") val draft: List? = null, + @SerializedName("scheduled") val scheduled: List? = null, + @SerializedName("error") val error: String? = null ) { fun toPosts() = PostsCardModel( - hasPublished = hasPublished, - draft = draft.map { it.toPost() }, - scheduled = scheduled.map { it.toPost() } + hasPublished = hasPublished ?: false, + draft = draft?.map { it.toPost() } ?: emptyList(), + scheduled = scheduled?.map { it.toPost() } ?: emptyList() ) } diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/store/dashboard/CardsStore.kt b/fluxc/src/main/java/org/wordpress/android/fluxc/store/dashboard/CardsStore.kt index 7fc57818b1..62788c69c1 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/store/dashboard/CardsStore.kt +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/store/dashboard/CardsStore.kt @@ -85,6 +85,26 @@ class CardsStore @Inject constructor( /* ERRORS */ + enum class TodaysStatsCardErrorType { + JETPACK_DISCONNECTED, + JETPACK_DISABLED, + UNAUTHORIZED, + GENERIC_ERROR + } + + class TodaysStatsCardError( + val type: TodaysStatsCardErrorType + ) : OnChangedError + + enum class PostCardErrorType { + UNAUTHORIZED, + GENERIC_ERROR + } + + class PostCardError( + val type: PostCardErrorType + ) : OnChangedError + enum class CardsErrorType { GENERIC_ERROR, AUTHORIZATION_REQUIRED, From f59a1781adef84cad1890a7f420c68a204cc6e83 Mon Sep 17 00:00:00 2001 From: ashiagr Date: Thu, 27 Jan 2022 12:39:58 +0530 Subject: [PATCH 2/8] Tests: Add cards rest client tests for errors inside a card (TDD) Tests will fail at this point --- .../wpcom/dashboard/CardsRestClientTest.kt | 88 +++++++++++++++++++ .../wp/dashboard/cards_with_errors.json | 8 ++ 2 files changed, 96 insertions(+) create mode 100644 example/src/test/resources/wp/dashboard/cards_with_errors.json diff --git a/example/src/test/java/org/wordpress/android/fluxc/network/rest/wpcom/dashboard/CardsRestClientTest.kt b/example/src/test/java/org/wordpress/android/fluxc/network/rest/wpcom/dashboard/CardsRestClientTest.kt index 9ab0a5b07b..c76386f347 100644 --- a/example/src/test/java/org/wordpress/android/fluxc/network/rest/wpcom/dashboard/CardsRestClientTest.kt +++ b/example/src/test/java/org/wordpress/android/fluxc/network/rest/wpcom/dashboard/CardsRestClientTest.kt @@ -20,6 +20,8 @@ import org.mockito.junit.MockitoJUnitRunner import org.wordpress.android.fluxc.Dispatcher import org.wordpress.android.fluxc.UnitTestUtils import org.wordpress.android.fluxc.model.SiteModel +import org.wordpress.android.fluxc.model.dashboard.CardModel.PostsCardModel +import org.wordpress.android.fluxc.model.dashboard.CardModel.TodaysStatsCardModel import org.wordpress.android.fluxc.network.BaseRequest.BaseNetworkError import org.wordpress.android.fluxc.network.BaseRequest.GenericErrorType import org.wordpress.android.fluxc.network.UserAgent @@ -34,12 +36,20 @@ import org.wordpress.android.fluxc.network.rest.wpcom.dashboard.CardsRestClient. import org.wordpress.android.fluxc.network.rest.wpcom.dashboard.CardsRestClient.TodaysStatsResponse import org.wordpress.android.fluxc.store.dashboard.CardsStore.CardsErrorType import org.wordpress.android.fluxc.store.dashboard.CardsStore.CardsPayload +import org.wordpress.android.fluxc.store.dashboard.CardsStore.PostCardError +import org.wordpress.android.fluxc.store.dashboard.CardsStore.PostCardErrorType +import org.wordpress.android.fluxc.store.dashboard.CardsStore.TodaysStatsCardError +import org.wordpress.android.fluxc.store.dashboard.CardsStore.TodaysStatsCardErrorType import org.wordpress.android.fluxc.test /* DATE */ private const val DATE_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss" +/* ERRORS */ +private const val JETPACK_DISABLED = "jetpack_disabled" +private const val UNAUTHORIZED = "unauthorized" + /* RESPONSE */ private val TODAYS_STATS_RESPONSE = TodaysStatsResponse( @@ -181,6 +191,62 @@ class CardsRestClientTest { assertError(CardsErrorType.GENERIC_ERROR, result) } + /* TODAY'S STATS CARD ERRORS */ + @Test + fun `given jetpack disconn, when fetch cards triggered, then returns todays stats jetpack disconn card error`() = + test { + val json = UnitTestUtils.getStringFromResourceFile(javaClass, DASHBOARD_CARDS_WITH_ERRORS_JSON) + initFetchCards(data = getCardsResponseFromJsonString(json)) + + val result = restClient.fetchCards(site) + + assertSuccessWithTodaysStatsError(TodaysStatsCardErrorType.JETPACK_DISCONNECTED, result) + } + + @Test + fun `given jetpack disabled, when fetch cards triggered, then returns todays stats jetpack disabled card error`() = + test { + val json = UnitTestUtils.getStringFromResourceFile(javaClass, DASHBOARD_CARDS_WITH_ERRORS_JSON) + val data = getCardsResponseFromJsonString(json) + .copy(todaysStats = TodaysStatsResponse(error = JETPACK_DISABLED)) + initFetchCards(data = data) + + val result = restClient.fetchCards(site) + + assertSuccessWithTodaysStatsError(TodaysStatsCardErrorType.JETPACK_DISABLED, result) + } + + @Test + fun `given stats unauthorized, when fetch cards triggered, then returns todays stats unauthorized card error`() = + test { + val json = UnitTestUtils.getStringFromResourceFile(javaClass, DASHBOARD_CARDS_WITH_ERRORS_JSON) + val data = getCardsResponseFromJsonString(json) + .copy(todaysStats = TodaysStatsResponse(error = UNAUTHORIZED)) + initFetchCards(data = data) + + val result = restClient.fetchCards(site) + + assertSuccessWithTodaysStatsError(TodaysStatsCardErrorType.UNAUTHORIZED, result) + } + + /* POST CARD ERROR */ + @Test + fun `given posts unauthorized error, when fetch cards triggered, then returns post card card error`() = + test { + val json = UnitTestUtils.getStringFromResourceFile(javaClass, DASHBOARD_CARDS_WITH_ERRORS_JSON) + initFetchCards(data = getCardsResponseFromJsonString(json)) + + val result = restClient.fetchCards(site) + + assertSuccessWithPostCardError(result) + } + + private fun CardsPayload.findTodaysStatsCardError(): TodaysStatsCardError? = + this.response?.toCards()?.filterIsInstance(TodaysStatsCardModel::class.java)?.firstOrNull()?.error + + private fun CardsPayload.findPostCardError(): PostCardError? = + this.response?.toCards()?.filterIsInstance(PostsCardModel::class.java)?.firstOrNull()?.error + private fun getCardsResponseFromJsonString(json: String): CardsResponse { val responseType = object : TypeToken() {}.type return GsonBuilder().setDateFormat(DATE_FORMAT_PATTERN) @@ -232,11 +298,33 @@ class CardsRestClientTest { } } + private fun assertSuccessWithTodaysStatsError( + expected: TodaysStatsCardErrorType, + actual: CardsPayload + ) { + with(actual) { + assertEquals(site, this@CardsRestClientTest.site) + assertFalse(isError) + assertEquals(expected, findTodaysStatsCardError()?.type) + } + } + + private fun assertSuccessWithPostCardError( + actual: CardsPayload + ) { + with(actual) { + assertEquals(site, this@CardsRestClientTest.site) + assertFalse(isError) + assertEquals(PostCardErrorType.UNAUTHORIZED, findPostCardError()?.type) + } + } + companion object { private const val API_BASE_PATH = "https://public-api.wordpress.com/wpcom/v2" private const val API_SITE_PATH = "$API_BASE_PATH/sites" private const val API_DASHBOARD_CARDS_PATH = "dashboard/cards/" private const val DASHBOARD_CARDS_JSON = "wp/dashboard/cards.json" + private const val DASHBOARD_CARDS_WITH_ERRORS_JSON = "wp/dashboard/cards_with_errors.json" } } diff --git a/example/src/test/resources/wp/dashboard/cards_with_errors.json b/example/src/test/resources/wp/dashboard/cards_with_errors.json new file mode 100644 index 0000000000..ffa40898a5 --- /dev/null +++ b/example/src/test/resources/wp/dashboard/cards_with_errors.json @@ -0,0 +1,8 @@ +{ + "todays_stats": { + "error": "jetpack_disconnected" + }, + "posts": { + "error": "unauthorized" + } +} From 521e2a673d6f2300175ed1c16a8e03a32251a7d7 Mon Sep 17 00:00:00 2001 From: ashiagr Date: Thu, 27 Jan 2022 12:40:20 +0530 Subject: [PATCH 3/8] Feature: Map card error strings to appropriate card errors --- .../rest/wpcom/dashboard/CardsRestClient.kt | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/network/rest/wpcom/dashboard/CardsRestClient.kt b/fluxc/src/main/java/org/wordpress/android/fluxc/network/rest/wpcom/dashboard/CardsRestClient.kt index fac3a0a8da..fccf5e36bf 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/network/rest/wpcom/dashboard/CardsRestClient.kt +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/network/rest/wpcom/dashboard/CardsRestClient.kt @@ -21,6 +21,10 @@ import org.wordpress.android.fluxc.network.rest.wpcom.auth.AccessToken import org.wordpress.android.fluxc.store.dashboard.CardsStore.CardsError import org.wordpress.android.fluxc.store.dashboard.CardsStore.CardsErrorType import org.wordpress.android.fluxc.store.dashboard.CardsStore.CardsPayload +import org.wordpress.android.fluxc.store.dashboard.CardsStore.PostCardError +import org.wordpress.android.fluxc.store.dashboard.CardsStore.PostCardErrorType +import org.wordpress.android.fluxc.store.dashboard.CardsStore.TodaysStatsCardError +import org.wordpress.android.fluxc.store.dashboard.CardsStore.TodaysStatsCardErrorType import javax.inject.Inject import javax.inject.Named import javax.inject.Singleton @@ -69,8 +73,19 @@ class CardsRestClient @Inject constructor( views = views ?: 0, visitors = visitors ?: 0, likes = likes ?: 0, - comments = comments ?: 0 + comments = comments ?: 0, + error = error?.let { toTodaysStatsCardsError(it) } ) + + private fun toTodaysStatsCardsError(error: String): TodaysStatsCardError { + val errorType = when (error) { + JETPACK_DISCONNECTED -> TodaysStatsCardErrorType.JETPACK_DISCONNECTED + JETPACK_DISABLED -> TodaysStatsCardErrorType.JETPACK_DISABLED + UNAUTHORIZED -> TodaysStatsCardErrorType.UNAUTHORIZED + else -> TodaysStatsCardErrorType.GENERIC_ERROR + } + return TodaysStatsCardError(errorType) + } } data class PostsResponse( @@ -82,8 +97,17 @@ class CardsRestClient @Inject constructor( fun toPosts() = PostsCardModel( hasPublished = hasPublished ?: false, draft = draft?.map { it.toPost() } ?: emptyList(), - scheduled = scheduled?.map { it.toPost() } ?: emptyList() + scheduled = scheduled?.map { it.toPost() } ?: emptyList(), + error = error?.let { toPostCardError(it) } ) + + private fun toPostCardError(error: String): PostCardError { + val errorType = when (error) { + UNAUTHORIZED -> PostCardErrorType.UNAUTHORIZED + else -> PostCardErrorType.GENERIC_ERROR + } + return PostCardError(errorType) + } } data class PostResponse( @@ -101,6 +125,12 @@ class CardsRestClient @Inject constructor( date = CardsUtils.fromDate(date) ) } + + companion object { + private const val JETPACK_DISCONNECTED = "jetpack_disconnected" + private const val JETPACK_DISABLED = "jetpack_disabled" + private const val UNAUTHORIZED = "unauthorized" + } } fun WPComGsonNetworkError.toCardsError(): CardsError { From 7c5010e077352282c88fcadb635357d4abfd281e Mon Sep 17 00:00:00 2001 From: ashiagr Date: Thu, 27 Jan 2022 17:42:09 +0530 Subject: [PATCH 4/8] Tests: Add cards store tests for errors inside cards --- .../fluxc/store/dashboard/CardsStoreTest.kt | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/example/src/test/java/org/wordpress/android/fluxc/store/dashboard/CardsStoreTest.kt b/example/src/test/java/org/wordpress/android/fluxc/store/dashboard/CardsStoreTest.kt index c01f12286d..8693a0919e 100644 --- a/example/src/test/java/org/wordpress/android/fluxc/store/dashboard/CardsStoreTest.kt +++ b/example/src/test/java/org/wordpress/android/fluxc/store/dashboard/CardsStoreTest.kt @@ -27,6 +27,10 @@ import org.wordpress.android.fluxc.store.dashboard.CardsStore.CardsError import org.wordpress.android.fluxc.store.dashboard.CardsStore.CardsErrorType import org.wordpress.android.fluxc.store.dashboard.CardsStore.CardsPayload import org.wordpress.android.fluxc.store.dashboard.CardsStore.CardsResult +import org.wordpress.android.fluxc.store.dashboard.CardsStore.PostCardError +import org.wordpress.android.fluxc.store.dashboard.CardsStore.PostCardErrorType +import org.wordpress.android.fluxc.store.dashboard.CardsStore.TodaysStatsCardError +import org.wordpress.android.fluxc.store.dashboard.CardsStore.TodaysStatsCardErrorType import org.wordpress.android.fluxc.test import org.wordpress.android.fluxc.tools.initCoroutineEngine import kotlin.test.assertEquals @@ -87,6 +91,10 @@ private val TODAYS_STATS_MODEL = TodaysStatsCardModel( comments = TODAYS_STATS_COMMENTS ) +private val TODAYS_STATS_WITH_ERROR_MODEL = TodaysStatsCardModel( + error = TodaysStatsCardError(TodaysStatsCardErrorType.JETPACK_DISCONNECTED) +) + private val POST_MODEL = PostCardModel( id = POST_ID, title = POST_TITLE, @@ -101,6 +109,10 @@ private val POSTS_MODEL = PostsCardModel( scheduled = listOf(POST_MODEL) ) +private val POSTS_WITH_ERROR_MODEL = PostsCardModel( + error = PostCardError(PostCardErrorType.UNAUTHORIZED) +) + private val CARDS_MODEL = listOf( TODAYS_STATS_MODEL, POSTS_MODEL @@ -114,6 +126,13 @@ private val STATS_ENTITY = CardEntity( json = CardsUtils.GSON.toJson(TODAYS_STATS_MODEL) ) +private val TODAY_STATS_WITH_ERROR_ENTITY = CardEntity( + siteLocalId = SITE_LOCAL_ID, + type = CardModel.Type.TODAYS_STATS.name, + date = CardsUtils.getInsertDate(), + json = CardsUtils.GSON.toJson(TODAYS_STATS_WITH_ERROR_MODEL) +) + private val POSTS_ENTITY = CardEntity( siteLocalId = SITE_LOCAL_ID, type = CardModel.Type.POSTS.name, @@ -121,6 +140,13 @@ private val POSTS_ENTITY = CardEntity( json = CardsUtils.GSON.toJson(POSTS_MODEL) ) +private val POSTS_WITH_ERROR_ENTITY = CardEntity( + siteLocalId = SITE_LOCAL_ID, + type = CardModel.Type.POSTS.name, + date = CardsUtils.getInsertDate(), + json = CardsUtils.GSON.toJson(POSTS_WITH_ERROR_MODEL) +) + private val CARDS_ENTITY = listOf( STATS_ENTITY, POSTS_ENTITY @@ -131,6 +157,7 @@ class CardsStoreTest { @Mock private lateinit var siteModel: SiteModel @Mock private lateinit var restClient: CardsRestClient @Mock private lateinit var dao: CardsDao + @Mock private lateinit var cardsRespone: CardsResponse private lateinit var cardsStore: CardsStore @@ -238,4 +265,81 @@ class CardsStoreTest { assertThat(result).isEqualTo(CardsResult(CARDS_MODEL)) } + + /* TODAYS STATS CARD WITH ERROR */ + + @Test + fun `given todays stats card with error, when fetch cards triggered, then card with error inserted into db`() = + test { + whenever(restClient.fetchCards(siteModel)).thenReturn(CardsPayload(cardsRespone)) + whenever(cardsRespone.toCards()).thenReturn(listOf(TODAYS_STATS_WITH_ERROR_MODEL)) + + cardsStore.fetchCards(siteModel) + + verify(dao).insertWithDate(siteModel.id, listOf(TODAYS_STATS_WITH_ERROR_MODEL)) + } + + @Test + fun `given today's stats jetpack disconn error, when get cards triggered, then error exists in the card`() = test { + whenever(dao.get(SITE_LOCAL_ID)) + .thenReturn( + flowOf(listOf(getTodaysStatsErrorCardEntity(TodaysStatsCardErrorType.JETPACK_DISCONNECTED))) + ) + + val result = cardsStore.getCards(siteModel).single() + + assertThat(result.findTodaysStatsCardError()?.type).isEqualTo(TodaysStatsCardErrorType.JETPACK_DISCONNECTED) + } + + @Test + fun `given today's stats jetpack disabled error, when get cards triggered, then error exists in the card`() = test { + whenever(dao.get(SITE_LOCAL_ID)) + .thenReturn(flowOf(listOf(getTodaysStatsErrorCardEntity(TodaysStatsCardErrorType.JETPACK_DISABLED)))) + + val result = cardsStore.getCards(siteModel).single() + + assertThat(result.findTodaysStatsCardError()?.type).isEqualTo(TodaysStatsCardErrorType.JETPACK_DISABLED) + } + + @Test + fun `given today's stats jetpack unauth error, when get cards triggered, then error exists in the card`() = test { + whenever(dao.get(SITE_LOCAL_ID)) + .thenReturn(flowOf(listOf(getTodaysStatsErrorCardEntity(TodaysStatsCardErrorType.UNAUTHORIZED)))) + + val result = cardsStore.getCards(siteModel).single() + + assertThat(result.findTodaysStatsCardError()?.type).isEqualTo(TodaysStatsCardErrorType.UNAUTHORIZED) + } + + /* POSTS CARD WITH ERROR */ + + @Test + fun `given posts card with error, when fetch cards triggered, then card with error inserted into db`() = test { + whenever(restClient.fetchCards(siteModel)).thenReturn(CardsPayload(cardsRespone)) + whenever(cardsRespone.toCards()).thenReturn(listOf(POSTS_WITH_ERROR_MODEL)) + + cardsStore.fetchCards(siteModel) + + verify(dao).insertWithDate(siteModel.id, listOf(POSTS_WITH_ERROR_MODEL)) + } + + @Test + fun `given posts card unauth error, when get cards triggered, then error exists in the card`() = test { + whenever(dao.get(SITE_LOCAL_ID)).thenReturn(flowOf(listOf(POSTS_WITH_ERROR_ENTITY))) + + val result = cardsStore.getCards(siteModel).single() + + assertThat(result.findPostsCardError()?.type).isEqualTo(PostCardErrorType.UNAUTHORIZED) + } + + private fun CardsResult>.findTodaysStatsCardError(): TodaysStatsCardError? = + model?.filterIsInstance(TodaysStatsCardModel::class.java)?.firstOrNull()?.error + + private fun CardsResult>.findPostsCardError(): PostCardError? = + model?.filterIsInstance(PostsCardModel::class.java)?.firstOrNull()?.error + + private fun getTodaysStatsErrorCardEntity(type: TodaysStatsCardErrorType) = + TODAY_STATS_WITH_ERROR_ENTITY.copy( + json = CardsUtils.GSON.toJson(TodaysStatsCardModel(error = TodaysStatsCardError(type))) + ) } From b7d083f3e52018c1c20e5fc45da036a044ea667f Mon Sep 17 00:00:00 2001 From: ashiagr Date: Thu, 27 Jan 2022 17:47:34 +0530 Subject: [PATCH 5/8] Refactor: Rename stats to todays_stats --- .../wordpress/android/fluxc/store/dashboard/CardsStoreTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example/src/test/java/org/wordpress/android/fluxc/store/dashboard/CardsStoreTest.kt b/example/src/test/java/org/wordpress/android/fluxc/store/dashboard/CardsStoreTest.kt index 8693a0919e..0188ef1861 100644 --- a/example/src/test/java/org/wordpress/android/fluxc/store/dashboard/CardsStoreTest.kt +++ b/example/src/test/java/org/wordpress/android/fluxc/store/dashboard/CardsStoreTest.kt @@ -119,7 +119,7 @@ private val CARDS_MODEL = listOf( ) /* ENTITY */ -private val STATS_ENTITY = CardEntity( +private val TODAYS_STATS_ENTITY = CardEntity( siteLocalId = SITE_LOCAL_ID, type = CardModel.Type.TODAYS_STATS.name, date = CardsUtils.getInsertDate(), @@ -148,7 +148,7 @@ private val POSTS_WITH_ERROR_ENTITY = CardEntity( ) private val CARDS_ENTITY = listOf( - STATS_ENTITY, + TODAYS_STATS_ENTITY, POSTS_ENTITY ) From 60ac8a40d2208aaa0bd570145a78d2e69ebed044 Mon Sep 17 00:00:00 2001 From: ashiagr Date: Thu, 17 Feb 2022 15:01:08 +0530 Subject: [PATCH 6/8] Fix existing tests --- .../android/fluxc/store/dashboard/CardsStoreTest.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/example/src/test/java/org/wordpress/android/fluxc/store/dashboard/CardsStoreTest.kt b/example/src/test/java/org/wordpress/android/fluxc/store/dashboard/CardsStoreTest.kt index 314027ab46..6c40e0337e 100644 --- a/example/src/test/java/org/wordpress/android/fluxc/store/dashboard/CardsStoreTest.kt +++ b/example/src/test/java/org/wordpress/android/fluxc/store/dashboard/CardsStoreTest.kt @@ -275,10 +275,10 @@ class CardsStoreTest { @Test fun `given todays stats card with error, when fetch cards triggered, then card with error inserted into db`() = test { - whenever(restClient.fetchCards(siteModel)).thenReturn(CardsPayload(cardsRespone)) + whenever(restClient.fetchCards(siteModel, CARD_TYPES)).thenReturn(CardsPayload(cardsRespone)) whenever(cardsRespone.toCards()).thenReturn(listOf(TODAYS_STATS_WITH_ERROR_MODEL)) - cardsStore.fetchCards(siteModel) + cardsStore.fetchCards(siteModel, CARD_TYPES) verify(dao).insertWithDate(siteModel.id, listOf(TODAYS_STATS_WITH_ERROR_MODEL)) } @@ -319,10 +319,10 @@ class CardsStoreTest { @Test fun `given posts card with error, when fetch cards triggered, then card with error inserted into db`() = test { - whenever(restClient.fetchCards(siteModel)).thenReturn(CardsPayload(cardsRespone)) + whenever(restClient.fetchCards(siteModel, CARD_TYPES)).thenReturn(CardsPayload(cardsRespone)) whenever(cardsRespone.toCards()).thenReturn(listOf(POSTS_WITH_ERROR_MODEL)) - cardsStore.fetchCards(siteModel) + cardsStore.fetchCards(siteModel, CARD_TYPES) verify(dao).insertWithDate(siteModel.id, listOf(POSTS_WITH_ERROR_MODEL)) } From 44bad81d36ffb178022d0dd8ba868c512c885725 Mon Sep 17 00:00:00 2001 From: ashiagr Date: Thu, 17 Feb 2022 17:32:36 +0530 Subject: [PATCH 7/8] Fix existing tests --- .../fluxc/store/dashboard/CardsStoreTest.kt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/example/src/test/java/org/wordpress/android/fluxc/store/dashboard/CardsStoreTest.kt b/example/src/test/java/org/wordpress/android/fluxc/store/dashboard/CardsStoreTest.kt index b9a360534b..bf8c4bfc7b 100644 --- a/example/src/test/java/org/wordpress/android/fluxc/store/dashboard/CardsStoreTest.kt +++ b/example/src/test/java/org/wordpress/android/fluxc/store/dashboard/CardsStoreTest.kt @@ -325,32 +325,32 @@ class CardsStoreTest { @Test fun `given today's stats jetpack disconn error, when get cards triggered, then error exists in the card`() = test { - whenever(dao.get(SITE_LOCAL_ID)) + whenever(dao.get(SITE_LOCAL_ID, CARD_TYPES)) .thenReturn( flowOf(listOf(getTodaysStatsErrorCardEntity(TodaysStatsCardErrorType.JETPACK_DISCONNECTED))) ) - val result = cardsStore.getCards(siteModel).single() + val result = cardsStore.getCards(siteModel, CARD_TYPES).single() assertThat(result.findTodaysStatsCardError()?.type).isEqualTo(TodaysStatsCardErrorType.JETPACK_DISCONNECTED) } @Test fun `given today's stats jetpack disabled error, when get cards triggered, then error exists in the card`() = test { - whenever(dao.get(SITE_LOCAL_ID)) + whenever(dao.get(SITE_LOCAL_ID, CARD_TYPES)) .thenReturn(flowOf(listOf(getTodaysStatsErrorCardEntity(TodaysStatsCardErrorType.JETPACK_DISABLED)))) - val result = cardsStore.getCards(siteModel).single() + val result = cardsStore.getCards(siteModel, CARD_TYPES).single() assertThat(result.findTodaysStatsCardError()?.type).isEqualTo(TodaysStatsCardErrorType.JETPACK_DISABLED) } @Test fun `given today's stats jetpack unauth error, when get cards triggered, then error exists in the card`() = test { - whenever(dao.get(SITE_LOCAL_ID)) + whenever(dao.get(SITE_LOCAL_ID, CARD_TYPES)) .thenReturn(flowOf(listOf(getTodaysStatsErrorCardEntity(TodaysStatsCardErrorType.UNAUTHORIZED)))) - val result = cardsStore.getCards(siteModel).single() + val result = cardsStore.getCards(siteModel, CARD_TYPES).single() assertThat(result.findTodaysStatsCardError()?.type).isEqualTo(TodaysStatsCardErrorType.UNAUTHORIZED) } @@ -369,9 +369,9 @@ class CardsStoreTest { @Test fun `given posts card unauth error, when get cards triggered, then error exists in the card`() = test { - whenever(dao.get(SITE_LOCAL_ID)).thenReturn(flowOf(listOf(POSTS_WITH_ERROR_ENTITY))) + whenever(dao.get(SITE_LOCAL_ID, CARD_TYPES)).thenReturn(flowOf(listOf(POSTS_WITH_ERROR_ENTITY))) - val result = cardsStore.getCards(siteModel).single() + val result = cardsStore.getCards(siteModel, CARD_TYPES).single() assertThat(result.findPostsCardError()?.type).isEqualTo(PostCardErrorType.UNAUTHORIZED) } From a0cea5224a55e7d289a9f93c3f7063105e9423a2 Mon Sep 17 00:00:00 2001 From: ashiagr Date: Fri, 18 Feb 2022 10:45:04 +0530 Subject: [PATCH 8/8] Pass error string with type --- .../fluxc/network/rest/wpcom/dashboard/CardsRestClient.kt | 4 ++-- .../wordpress/android/fluxc/store/dashboard/CardsStore.kt | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/network/rest/wpcom/dashboard/CardsRestClient.kt b/fluxc/src/main/java/org/wordpress/android/fluxc/network/rest/wpcom/dashboard/CardsRestClient.kt index 75ad64c1c1..2b95949b27 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/network/rest/wpcom/dashboard/CardsRestClient.kt +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/network/rest/wpcom/dashboard/CardsRestClient.kt @@ -88,7 +88,7 @@ class CardsRestClient @Inject constructor( UNAUTHORIZED -> TodaysStatsCardErrorType.UNAUTHORIZED else -> TodaysStatsCardErrorType.GENERIC_ERROR } - return TodaysStatsCardError(errorType) + return TodaysStatsCardError(errorType, error) } } @@ -110,7 +110,7 @@ class CardsRestClient @Inject constructor( UNAUTHORIZED -> PostCardErrorType.UNAUTHORIZED else -> PostCardErrorType.GENERIC_ERROR } - return PostCardError(errorType) + return PostCardError(errorType, error) } } diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/store/dashboard/CardsStore.kt b/fluxc/src/main/java/org/wordpress/android/fluxc/store/dashboard/CardsStore.kt index 9d54658734..0cce72da6d 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/store/dashboard/CardsStore.kt +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/store/dashboard/CardsStore.kt @@ -95,7 +95,8 @@ class CardsStore @Inject constructor( } class TodaysStatsCardError( - val type: TodaysStatsCardErrorType + val type: TodaysStatsCardErrorType, + val message: String? = null ) : OnChangedError enum class PostCardErrorType { @@ -104,7 +105,8 @@ class CardsStore @Inject constructor( } class PostCardError( - val type: PostCardErrorType + val type: PostCardErrorType, + val message: String? = null ) : OnChangedError enum class CardsErrorType {