Skip to content

Commit

Permalink
Merge pull request #819 from wordpress-mobile/feature/fix_error_in_re…
Browse files Browse the repository at this point in the history
…wind_status_mapping

Fix error in rewind status mapping
  • Loading branch information
0nko committed May 31, 2018
2 parents 0d380ad + 12097e5 commit ba54e56
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 21 deletions.
Expand Up @@ -17,12 +17,16 @@ import org.wordpress.android.fluxc.generated.SiteActionBuilder
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.fluxc.model.activity.ActivityLogModel
import org.wordpress.android.fluxc.model.activity.RewindStatusModel
import org.wordpress.android.fluxc.model.activity.RewindStatusModel.Rewind
import org.wordpress.android.fluxc.model.activity.RewindStatusModel.Rewind.Status.RUNNING
import org.wordpress.android.fluxc.model.activity.RewindStatusModel.State.ACTIVE
import org.wordpress.android.fluxc.store.AccountStore
import org.wordpress.android.fluxc.store.AccountStore.AuthenticatePayload
import org.wordpress.android.fluxc.store.AccountStore.OnAccountChanged
import org.wordpress.android.fluxc.store.AccountStore.OnAuthenticationChanged
import org.wordpress.android.fluxc.store.ActivityLogStore
import org.wordpress.android.fluxc.store.ActivityLogStore.FetchedActivityLogPayload
import org.wordpress.android.fluxc.store.ActivityLogStore.FetchedRewindStatePayload
import org.wordpress.android.fluxc.store.ActivityLogStore.OnActivityLogFetched
import org.wordpress.android.fluxc.store.ActivityLogStore.OnRewindStatusFetched
import org.wordpress.android.fluxc.store.ActivityLogStore.RewindResultPayload
Expand Down Expand Up @@ -170,6 +174,33 @@ class ReleaseStack_ActivityLogTestJetpack : ReleaseStack_Base() {
assertTrue((incomingActions[0].payload as RewindResultPayload).isError)
}

@Test
fun insertAndRetrieveRewindStatus() {
val site = authenticate()
this.mCountDownLatch = CountDownLatch(1)

val rewindId = "rewindId"
val restoreId: Long = 123
val status = RUNNING
val progress = 30
val rewind = Rewind(rewindId, restoreId, status, progress, null)
val model = RewindStatusModel(ACTIVE, null, Date(), null, null, rewind)
val payload = FetchedRewindStatePayload(model, site)

activityLogStore.onAction(ActivityLogActionBuilder.newFetchedRewindStateAction(payload))

val rewindState = activityLogStore.getRewindStatusForSite(site)

assertNotNull(rewindState)
assertNotNull(rewindState?.rewind)
rewindState?.rewind?.apply {
assertEquals(rewindId, this.rewindId)
assertEquals(restoreId, this.restoreId)
assertEquals(status, this.status)
assertEquals(progress, this.progress)
}
}

private fun authenticate(): SiteModel {
authenticateWPComAndFetchSites(BuildConfig.TEST_WPCOM_USERNAME_SINGLE_JETPACK_ONLY,
BuildConfig.TEST_WPCOM_PASSWORD_SINGLE_JETPACK_ONLY)
Expand Down
Expand Up @@ -26,8 +26,8 @@ val ACTIVITY_RESPONSE = ActivityLogRestClient.ActivitiesResponse.ActivityRespons
val ACTIVITY_RESPONSE_PAGE = ActivityLogRestClient.ActivitiesResponse.Page(listOf(ACTIVITY_RESPONSE))
val REWIND_RESPONSE = ActivityLogRestClient.RewindStatusResponse.Rewind(rewind_id = "123",
status = RewindStatusModel.Rewind.Status.RUNNING.value,
progress = 10,
reason = "nit",
progress = null,
reason = null,
site_id = null,
restore_id = 5)
val REWIND_STATUS_RESPONSE = ActivityLogRestClient.RewindStatusResponse(
Expand Down
Expand Up @@ -227,9 +227,6 @@ class ActivityLogRestClientTest {
assertNotNull(this.rewind)
this.rewind?.apply {
assertEquals(this.status.value, REWIND_RESPONSE.status)
assertEquals(this.progress, REWIND_RESPONSE.progress)
assertEquals(this.rewindId, REWIND_RESPONSE.rewind_id)
assertEquals(this.reason, REWIND_RESPONSE.reason)
}
}
}
Expand Down Expand Up @@ -298,8 +295,8 @@ class ActivityLogRestClientTest {

verify(requestQueue).add(any<WPComGsonRequest<RewindResponse>>())

val restoreId = "restoreId"
rewindSuccessMethodCaptor.firstValue.invoke(RewindResponse(restoreId))
val restoreId = 10L
rewindSuccessMethodCaptor.firstValue.invoke(RewindResponse(restoreId, true, null))

verify(dispatcher).dispatch(mRewindActionCaptor.capture())
assertEquals(restoreId, mRewindActionCaptor.firstValue.payload.restoreId)
Expand All @@ -319,6 +316,21 @@ class ActivityLogRestClientTest {
assertTrue(mRewindActionCaptor.firstValue.payload.isError)
}

@Test
fun postRewindApiError() {
initPostRewind()

activityRestClient.rewind(site, "rewindId")

verify(requestQueue).add(any<WPComGsonRequest<RewindResponse>>())

val restoreId = 10L
rewindSuccessMethodCaptor.firstValue.invoke(RewindResponse(restoreId, false, "error"))

verify(dispatcher).dispatch(mRewindActionCaptor.capture())
assertTrue(mRewindActionCaptor.firstValue.payload.isError)
}

private fun assertEmittedActivityError(errorType: ActivityLogErrorType) {
verify(dispatcher).dispatch(activityActionCaptor.capture())
with(activityActionCaptor.firstValue) {
Expand Down
Expand Up @@ -168,7 +168,7 @@ class ActivityLogStoreTest {
@Test
fun emitsRewindResult() {
val rewindId = "rewindId"
val restoreId = "restoreId"
val restoreId = 10L
val payload = ActivityLogStore.RewindResultPayload(rewindId, restoreId, siteModel)
val action = ActivityLogActionBuilder.newRewindResultAction(payload)

Expand Down
Expand Up @@ -37,15 +37,15 @@ data class RewindStatusModel(
val rewindId: String?,
val restoreId: Long,
val status: Status,
val progress: Int,
val progress: Int?,
val reason: String?
) {
enum class Status(val value: String) {
RUNNING("running"), FINISHED("finished"), FAILED("failed");

companion object {
fun fromValue(value: String): Status? {
return Status.values().firstOrNull { it.value == value }
fun fromValue(value: String?): Status? {
return value?.let { Status.values().firstOrNull { it.value == value } }
}
}
}
Expand All @@ -59,7 +59,7 @@ data class RewindStatusModel(
reason: String?
): Rewind? {
val status = stringStatus?.let { Status.fromValue(it) }
if (status != null && progress != null && restoreId != null) {
if (status != null && restoreId != null) {
return Rewind(rewindId, restoreId, status, progress, reason)
}
return null
Expand Down
Expand Up @@ -21,7 +21,10 @@ import org.wordpress.android.fluxc.store.ActivityLogStore.ActivityError
import org.wordpress.android.fluxc.store.ActivityLogStore.ActivityLogErrorType
import org.wordpress.android.fluxc.store.ActivityLogStore.FetchedActivityLogPayload
import org.wordpress.android.fluxc.store.ActivityLogStore.FetchedRewindStatePayload
import org.wordpress.android.fluxc.store.ActivityLogStore.RewindError
import org.wordpress.android.fluxc.store.ActivityLogStore.RewindErrorType
import org.wordpress.android.fluxc.store.ActivityLogStore.RewindErrorType.API_ERROR
import org.wordpress.android.fluxc.store.ActivityLogStore.RewindResultPayload
import org.wordpress.android.fluxc.store.ActivityLogStore.RewindStatusError
import org.wordpress.android.fluxc.store.ActivityLogStore.RewindStatusErrorType
import java.util.Date
Expand Down Expand Up @@ -90,8 +93,13 @@ constructor(
val url = WPCOMREST.activity_log.site(site.siteId).rewind.to.rewind(rewindId).urlV1
val request = wpComGsonRequestBuilder.buildPostRequest(url, mapOf(), RewindResponse::class.java,
{ response ->
val payload = ActivityLogStore.RewindResultPayload(rewindId, response.restore_id, site)
dispatcher.dispatch(ActivityLogActionBuilder.newRewindResultAction(payload))
if (response.ok != true && (response.error != null && response.error.isNotEmpty())) {
val payload = RewindResultPayload(RewindError(API_ERROR, response.error), rewindId, site)
dispatcher.dispatch(ActivityLogActionBuilder.newRewindResultAction(payload))
} else {
val payload = ActivityLogStore.RewindResultPayload(rewindId, response.restore_id, site)
dispatcher.dispatch(ActivityLogActionBuilder.newRewindResultAction(payload))
}
},
{ networkError ->
val error = ActivityLogStore.RewindError(genericToError(networkError,
Expand Down Expand Up @@ -268,14 +276,14 @@ constructor(
)

data class Rewind(
val rewind_id: String?,
val restore_id: Long?,
val site_id: String?,
val status: String,
val progress: Int,
val status: String?,
val restore_id: Long?,
val rewind_id: String?,
val progress: Int?,
val reason: String?
)
}

class RewindResponse(val restore_id: String)
class RewindResponse(val restore_id: Long, val ok: Boolean?, val error: String?)
}
Expand Up @@ -148,6 +148,7 @@ class ActivityLogSqlUtils
reason = this.reason,
canAutoconfigure = this.canAutoconfigure,
rewindId = this.rewind?.rewindId,
restoreId = this.rewind?.restoreId,
rewindStatus = this.rewind?.status?.value,
rewindProgress = this.rewind?.progress,
rewindReason = this.rewind?.reason
Expand Down
Expand Up @@ -137,7 +137,7 @@ class ActivityLogStore

data class OnRewind(
val rewindId: String,
val restoreId: String? = null,
val restoreId: Long? = null,
var causeOfChange: ActivityLogAction
) : Store.OnChanged<RewindError>() {
constructor(rewindId: String, error: RewindError, causeOfChange: ActivityLogAction) :
Expand Down Expand Up @@ -185,7 +185,7 @@ class ActivityLogStore

class RewindResultPayload(
val rewindId: String,
val restoreId: String? = null,
val restoreId: Long? = null,
val site: SiteModel
) : Payload<RewindError>() {
constructor(error: RewindError, rewindId: String, site: SiteModel) : this(rewindId = rewindId, site = site) {
Expand Down Expand Up @@ -219,6 +219,7 @@ class ActivityLogStore

enum class RewindErrorType {
GENERIC_ERROR,
API_ERROR,
AUTHORIZATION_REQUIRED,
INVALID_RESPONSE,
MISSING_STATE
Expand Down

0 comments on commit ba54e56

Please sign in to comment.