feat(gift): 선물 상세 조회 상태를 추가한다
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package kr.co.vividnext.sodalive.v2.gift
|
||||
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers
|
||||
import kr.co.vividnext.sodalive.R
|
||||
import kr.co.vividnext.sodalive.base.BaseViewModel
|
||||
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
|
||||
import kr.co.vividnext.sodalive.common.ToastMessage
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftCancelResponse
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftDetailResponse
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftRepository
|
||||
|
||||
sealed interface GiftDetailUiState {
|
||||
data object Loading : GiftDetailUiState
|
||||
data class Content(val detail: GiftDetailResponse) : GiftDetailUiState
|
||||
data class Canceled(val detail: GiftDetailResponse, val cancel: GiftCancelResponse?) : GiftDetailUiState
|
||||
data class Error(val message: String?) : GiftDetailUiState
|
||||
}
|
||||
|
||||
class GiftDetailViewModel(private val repository: GiftRepository) : BaseViewModel() {
|
||||
private val mutableState = MutableLiveData<GiftDetailUiState>()
|
||||
val state: LiveData<GiftDetailUiState> = mutableState
|
||||
private val mutableFinishError = MutableLiveData<ToastMessage?>()
|
||||
val finishErrorLiveData: LiveData<ToastMessage?> = mutableFinishError
|
||||
private val mutableInquiry = MutableLiveData(false)
|
||||
val inquiryLiveData: LiveData<Boolean> = mutableInquiry
|
||||
private var applicationNo: String? = null
|
||||
private var pendingCancellation: GiftCancelResponse? = null
|
||||
|
||||
fun loadInitial(applicationNo: String) {
|
||||
if (this.applicationNo != null) return
|
||||
this.applicationNo = applicationNo
|
||||
if (applicationNo.isBlank()) {
|
||||
mutableFinishError.value = ToastMessage(resId = R.string.gift_send_invalid_access)
|
||||
return
|
||||
}
|
||||
load(applicationNo)
|
||||
}
|
||||
|
||||
fun retry() {
|
||||
val number = applicationNo?.takeIf { it.isNotBlank() } ?: return
|
||||
if (mutableState.value == GiftDetailUiState.Loading) return
|
||||
load(number)
|
||||
}
|
||||
|
||||
fun consumeFinishError(): ToastMessage? = mutableFinishError.value.also { mutableFinishError.value = null }
|
||||
|
||||
fun applyCancellation(cancel: GiftCancelResponse) {
|
||||
if (applicationNo != cancel.applicationNo) return
|
||||
pendingCancellation = cancel
|
||||
val detail = when (val current = mutableState.value) {
|
||||
is GiftDetailUiState.Content -> current.detail
|
||||
is GiftDetailUiState.Canceled -> current.detail
|
||||
else -> return
|
||||
}
|
||||
if (detail.applicationNo == applicationNo) {
|
||||
mutableState.value = GiftDetailUiState.Canceled(detail, cancel)
|
||||
}
|
||||
}
|
||||
|
||||
fun onInquiryClick() {
|
||||
mutableInquiry.value = true
|
||||
}
|
||||
|
||||
fun consumeInquiryIntent(): Intent? {
|
||||
if (mutableInquiry.value != true) return null
|
||||
mutableInquiry.value = false
|
||||
return Intent(Intent.ACTION_VIEW, Uri.parse("http://pf.kakao.com/_lkxgxhG/chat"))
|
||||
}
|
||||
|
||||
private fun load(number: String) {
|
||||
mutableState.value = GiftDetailUiState.Loading
|
||||
compositeDisposable.add(
|
||||
repository.getGiftDetail(number, "Bearer ${SharedPreferenceManager.token}")
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe({ response ->
|
||||
val detail = response.data
|
||||
mutableState.value = if (response.success && detail != null) {
|
||||
val cancel = pendingCancellation?.takeIf { it.applicationNo == applicationNo }
|
||||
if (detail.status == "CANCELED" || cancel != null) {
|
||||
GiftDetailUiState.Canceled(detail, cancel)
|
||||
} else {
|
||||
GiftDetailUiState.Content(detail)
|
||||
}
|
||||
} else {
|
||||
GiftDetailUiState.Error(response.message)
|
||||
}
|
||||
}, { mutableState.value = GiftDetailUiState.Error(null) })
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,269 @@
|
||||
package kr.co.vividnext.sodalive.v2.gift
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Intent
|
||||
import android.content.Context
|
||||
import android.content.res.Configuration
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import io.reactivex.rxjava3.android.plugins.RxAndroidPlugins
|
||||
import io.reactivex.rxjava3.core.Single
|
||||
import io.reactivex.rxjava3.plugins.RxJavaPlugins
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers
|
||||
import io.reactivex.rxjava3.subjects.SingleSubject
|
||||
import kr.co.vividnext.sodalive.R
|
||||
import kr.co.vividnext.sodalive.common.ApiResponse
|
||||
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftApi
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftCancelResponse
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftDeliveryResponse
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftDetailInfoResponse
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftDetailResponse
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftRepository
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import java.util.Locale
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], application = Application::class)
|
||||
class GiftDetailViewModelTest {
|
||||
private lateinit var api: DetailFakeApi
|
||||
private lateinit var viewModel: GiftDetailViewModel
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() }
|
||||
RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }
|
||||
RxAndroidPlugins.setMainThreadSchedulerHandler { Schedulers.trampoline() }
|
||||
SharedPreferenceManager.resetForTest()
|
||||
SharedPreferenceManager.init(ApplicationProvider.getApplicationContext())
|
||||
SharedPreferenceManager.token = "test-token"
|
||||
api = DetailFakeApi()
|
||||
viewModel = GiftDetailViewModel(GiftRepository(api))
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
viewModel.compositeDisposable.clear()
|
||||
RxJavaPlugins.reset()
|
||||
RxAndroidPlugins.reset()
|
||||
SharedPreferenceManager.resetForTest()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `같은 신청의 취소 응답은 재조회 없이 취소 완료와 최신 환불 및 날짜를 적용한다`() {
|
||||
val original = detail().copy(delivery = GiftDeliveryResponse("2025-01-01T00:00:00Z", null, null))
|
||||
val cancel = GiftCancelResponse("test-gift", "CANCELED", "신청 취소", 77, "2026-10-02T12:00:00Z")
|
||||
viewModel.applyCancellation(cancel)
|
||||
assertNull(viewModel.state.value)
|
||||
api.responses.add(Single.just(ApiResponse(true, original, null)))
|
||||
viewModel.loadInitial("test-gift")
|
||||
viewModel.applyCancellation(cancel.copy(applicationNo = "other-gift"))
|
||||
assertEquals(GiftDetailUiState.Content(original), viewModel.state.value)
|
||||
viewModel.applyCancellation(cancel)
|
||||
assertEquals(GiftDetailUiState.Canceled(original, cancel), viewModel.state.value)
|
||||
val model = GiftDetailPresenter.present(original, cancel)
|
||||
assertEquals(77, model.cancellation?.refundCan)
|
||||
assertEquals(GiftDetailPresenter.formatCardDate(cancel.canceledAt), model.cancellation?.date)
|
||||
assertEquals(GiftDetailPresenter.formatCardDate(cancel.canceledAt), model.giftInfo.canceledDate)
|
||||
assertEquals(1, api.requests.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `로딩 중 같은 신청의 취소 응답은 성공 상세에 적용된다`() {
|
||||
val pending = SingleSubject.create<ApiResponse<GiftDetailResponse>>()
|
||||
api.responses.add(pending)
|
||||
val cancel = GiftCancelResponse("test-gift", "CANCELED", "신청 취소", 77, "2026-10-02T00:00:00Z")
|
||||
viewModel.loadInitial("test-gift")
|
||||
viewModel.applyCancellation(cancel)
|
||||
assertEquals(GiftDetailUiState.Loading, viewModel.state.value)
|
||||
val detail = detail()
|
||||
pending.onSuccess(ApiResponse(true, detail, null))
|
||||
assertEquals(GiftDetailUiState.Canceled(detail, cancel), viewModel.state.value)
|
||||
assertEquals(1, api.requests.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `실패 후 재시도해도 같은 신청의 취소 응답은 성공 상세에 적용된다`() {
|
||||
api.responses.add(Single.just(ApiResponse(false, null, "실패")))
|
||||
val cancel = GiftCancelResponse("test-gift", "CANCELED", "신청 취소", 77, "2026-10-02T00:00:00Z")
|
||||
viewModel.loadInitial("test-gift")
|
||||
assertEquals(GiftDetailUiState.Error("실패"), viewModel.state.value)
|
||||
viewModel.applyCancellation(cancel)
|
||||
val pending = SingleSubject.create<ApiResponse<GiftDetailResponse>>()
|
||||
api.responses.add(pending)
|
||||
viewModel.retry()
|
||||
assertEquals(GiftDetailUiState.Loading, viewModel.state.value)
|
||||
val detail = detail()
|
||||
pending.onSuccess(ApiResponse(true, detail, null))
|
||||
assertEquals(GiftDetailUiState.Canceled(detail, cancel), viewModel.state.value)
|
||||
assertEquals(2, api.requests.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `다른 신청의 취소 응답은 로딩 중에도 무시된다`() {
|
||||
val pending = SingleSubject.create<ApiResponse<GiftDetailResponse>>()
|
||||
api.responses.add(pending)
|
||||
viewModel.loadInitial("test-gift")
|
||||
viewModel.applyCancellation(GiftCancelResponse("other-gift", "CANCELED", "신청 취소", 77, "2026-10-02T00:00:00Z"))
|
||||
assertEquals(GiftDetailUiState.Loading, viewModel.state.value)
|
||||
val detail = detail()
|
||||
pending.onSuccess(ApiResponse(true, detail, null))
|
||||
assertEquals(GiftDetailUiState.Content(detail), viewModel.state.value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `공백 신청번호는 조회 없이 종료 오류를 한 번만 소비한다`() {
|
||||
viewModel.loadInitial(" \n ")
|
||||
viewModel.loadInitial(" \n ")
|
||||
viewModel.retry()
|
||||
assertTrue(api.requests.isEmpty())
|
||||
assertEquals(R.string.gift_send_invalid_access, viewModel.consumeFinishError()?.resId)
|
||||
assertNull(viewModel.consumeFinishError())
|
||||
assertFalse(viewModel.finishErrorLiveData.value != null)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `최초 조회는 로딩 후 콘텐츠가 되고 재진입과 로딩 중 재시도는 중복하지 않는다`() {
|
||||
val pending = SingleSubject.create<ApiResponse<GiftDetailResponse>>()
|
||||
api.responses.add(pending)
|
||||
viewModel.loadInitial("test-gift")
|
||||
viewModel.loadInitial("test-gift")
|
||||
viewModel.retry()
|
||||
assertEquals(GiftDetailUiState.Loading, viewModel.state.value)
|
||||
val detail = detail()
|
||||
pending.onSuccess(ApiResponse(true, detail, null))
|
||||
assertEquals(GiftDetailUiState.Content(detail), viewModel.state.value)
|
||||
viewModel.loadInitial("test-gift")
|
||||
assertEquals(listOf("test-gift" to "Bearer test-token"), api.requests)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `실패 응답은 데이터가 있어도 오류 상태다`() {
|
||||
api.responses.add(Single.just(ApiResponse(false, detail(), "조회 실패")))
|
||||
viewModel.loadInitial("test-gift")
|
||||
assertEquals(GiftDetailUiState.Error("조회 실패"), viewModel.state.value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `성공 응답의 null 데이터는 오류 상태다`() {
|
||||
api.responses.add(Single.just(ApiResponse(true, null, null)))
|
||||
viewModel.loadInitial("test-gift")
|
||||
assertEquals(GiftDetailUiState.Error(null), viewModel.state.value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `전송 오류는 내부 예외 내용을 노출하지 않는 오류 상태다`() {
|
||||
api.responses.add(Single.error(IllegalStateException("private transport detail")))
|
||||
viewModel.loadInitial("test-gift")
|
||||
assertEquals(GiftDetailUiState.Error(null), viewModel.state.value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `오류 후 재시도는 같은 번호로 로딩을 거쳐 새로운 콘텐츠를 제공한다`() {
|
||||
api.responses.add(Single.just(ApiResponse(false, null, "실패")))
|
||||
viewModel.loadInitial("test-gift")
|
||||
val pending = SingleSubject.create<ApiResponse<GiftDetailResponse>>()
|
||||
api.responses.add(pending)
|
||||
viewModel.retry()
|
||||
assertEquals(GiftDetailUiState.Loading, viewModel.state.value)
|
||||
val detail = detail("DELIVERED")
|
||||
pending.onSuccess(ApiResponse(true, detail, null))
|
||||
assertEquals(GiftDetailUiState.Content(detail), viewModel.state.value)
|
||||
assertEquals(listOf("test-gift", "test-gift"), api.requests.map { it.first })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `취소 상태 조회는 취소 응답 없이 즉시 취소 완료 상태다`() {
|
||||
val detail = detail("CANCELED")
|
||||
api.responses.add(Single.just(ApiResponse(true, detail, null)))
|
||||
viewModel.loadInitial("test-gift")
|
||||
assertEquals(GiftDetailUiState.Canceled(detail, null), viewModel.state.value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `전달 불가와 알 수 없는 상태도 콘텐츠로 보존한다`() {
|
||||
listOf("UNDELIVERABLE", "FUTURE_STATUS").forEach { status ->
|
||||
val detail = detail(status)
|
||||
api.responses.add(Single.just(ApiResponse(true, detail, null)))
|
||||
val model = GiftDetailViewModel(GiftRepository(api))
|
||||
model.loadInitial("test-gift")
|
||||
assertEquals(GiftDetailUiState.Content(detail), model.state.value)
|
||||
model.compositeDisposable.clear()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `해제된 요청의 늦은 응답은 상태를 갱신하지 않는다`() {
|
||||
val pending = SingleSubject.create<ApiResponse<GiftDetailResponse>>()
|
||||
api.responses.add(pending)
|
||||
viewModel.loadInitial("test-gift")
|
||||
viewModel.compositeDisposable.clear()
|
||||
assertFalse(pending.hasObservers())
|
||||
pending.onSuccess(ApiResponse(true, detail(), null))
|
||||
assertEquals(GiftDetailUiState.Loading, viewModel.state.value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `문의 이벤트는 기존 카카오 ACTION_VIEW 주소로 한 번 소비된다`() {
|
||||
assertNull(viewModel.consumeInquiryIntent())
|
||||
viewModel.onInquiryClick()
|
||||
assertEquals(true, viewModel.inquiryLiveData.value)
|
||||
val intent = viewModel.consumeInquiryIntent()
|
||||
assertEquals(Intent.ACTION_VIEW, intent?.action)
|
||||
assertEquals("http://pf.kakao.com/_lkxgxhG/chat", intent?.data.toString())
|
||||
assertNull(viewModel.consumeInquiryIntent())
|
||||
assertEquals(false, viewModel.inquiryLiveData.value)
|
||||
}
|
||||
|
||||
private fun detail(status: String = "RECEIVED") = GiftDetailResponse(
|
||||
applicationNo = "test-gift", direction = "SENT", status = status, statusName = "상태",
|
||||
giftInfo = GiftDetailInfoResponse("가상 크리에이터", null, "소형", "도서", "test-gift", 100, null, null),
|
||||
senderInfo = null, recipientAddress = null, mailbox = null,
|
||||
trackingRequired = false, recipientAddressRequired = false, recipientAddressDeadlineAt = null,
|
||||
delivery = GiftDeliveryResponse(null, null, null), statusTimeline = emptyList()
|
||||
)
|
||||
|
||||
@Test
|
||||
fun `모든 고정 문구 키는 한국어 영어 일본어로 해석하고 서버 문구는 그대로 유지한다`() {
|
||||
val context = ApplicationProvider.getApplicationContext<Context>()
|
||||
val korean = context.createConfigurationContext(
|
||||
Configuration(context.resources.configuration).apply {
|
||||
setLocale(Locale.KOREAN)
|
||||
}
|
||||
)
|
||||
listOf(Locale.KOREAN, Locale.ENGLISH, Locale.JAPANESE).forEach { locale ->
|
||||
val localized = context.createConfigurationContext(
|
||||
Configuration(context.resources.configuration).apply {
|
||||
setLocale(locale)
|
||||
}
|
||||
)
|
||||
GiftDetailText.Key.entries.forEach { key ->
|
||||
val text = localized.resolveGiftDetailText(key)
|
||||
assertTrue(text.isNotBlank())
|
||||
if (locale != Locale.KOREAN) {
|
||||
assertFalse(text == korean.resolveGiftDetailText(key))
|
||||
assertFalse(text.any { it in '\uac00'..'\ud7a3' })
|
||||
}
|
||||
}
|
||||
assertEquals("서버 원문", localized.resolveGiftDetailText(GiftDetailText.Raw("서버 원문")))
|
||||
}
|
||||
}
|
||||
|
||||
private class DetailFakeApi : GiftApi by InboxFakeApi() {
|
||||
val requests = mutableListOf<Pair<String, String>>()
|
||||
val responses = ArrayDeque<Single<ApiResponse<GiftDetailResponse>>>()
|
||||
|
||||
override fun getGiftDetail(applicationNo: String, authHeader: String): Single<ApiResponse<GiftDetailResponse>> {
|
||||
requests.add(applicationNo to authHeader)
|
||||
return responses.removeFirst()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user