From 053a99758a292c7cec8aacb8f0b9bb5314807025 Mon Sep 17 00:00:00 2001 From: kwakjoohyeong Date: Fri, 19 Apr 2024 18:29:09 +0900 Subject: [PATCH 1/4] =?UTF-8?q?fix:=20server=20=EC=8B=9C=EA=B0=84=EC=9D=84?= =?UTF-8?q?=20=EA=B8=B0=EC=A4=80=EC=9C=BC=EB=A1=9C=20=EB=82=A0=EC=A7=9C=20?= =?UTF-8?q?=EA=B3=84=EC=82=B0=ED=95=98=EA=B2=8C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../withpeace/core/data/mapper/DateMapper.kt | 10 ++++ .../core/data/mapper/PostDetailMapper.kt | 31 +++++----- .../withpeace/core/data/mapper/PostMapper.kt | 3 +- .../core/data/paging/PostPagingSource.kt | 7 ++- .../data/repository/DefaultPostRepository.kt | 7 ++- .../withpeace/core/domain/model/date/Date.kt | 5 +- .../core/domain/model/date/DurationFromNow.kt | 4 +- .../withpeace/core/domain/model/post/Post.kt | 2 + .../core/domain/model/post/PostDetail.kt | 3 + .../withpeace/core/ui/DateUiModel.kt | 56 +++++++++---------- .../core/ui/post/PostDetailUiModel.kt | 4 +- .../withpeace/core/ui/post/PostUiModel.kt | 4 +- .../feature/postdetail/PostDetailScreen.kt | 6 +- .../feature/postdetail/PostDetailViewModel.kt | 1 - .../feature/postlist/PostListScreen.kt | 5 +- 15 files changed, 84 insertions(+), 64 deletions(-) diff --git a/core/data/src/main/kotlin/com/withpeace/withpeace/core/data/mapper/DateMapper.kt b/core/data/src/main/kotlin/com/withpeace/withpeace/core/data/mapper/DateMapper.kt index 4af8f33e..eda1aee8 100644 --- a/core/data/src/main/kotlin/com/withpeace/withpeace/core/data/mapper/DateMapper.kt +++ b/core/data/src/main/kotlin/com/withpeace/withpeace/core/data/mapper/DateMapper.kt @@ -1,11 +1,21 @@ package com.withpeace.withpeace.core.data.mapper import java.time.LocalDateTime +import java.time.ZoneId +import java.time.ZonedDateTime import java.time.format.DateTimeFormatter +import java.util.Locale fun String.toLocalDateTime(): LocalDateTime = LocalDateTime.parse( this, DateTimeFormatter.ofPattern(SERVER_DATE_FORMAT), ) +fun String.toLocalDateTimeForGmt(): LocalDateTime { + val formatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH) + val zonedDateTime = ZonedDateTime.parse(this, formatter) + val seoulZoneId = ZoneId.of("Asia/Seoul") + return zonedDateTime.withZoneSameInstant(seoulZoneId).toLocalDateTime() +} + private const val SERVER_DATE_FORMAT = "yyyy/MM/dd HH:mm:ss" diff --git a/core/data/src/main/kotlin/com/withpeace/withpeace/core/data/mapper/PostDetailMapper.kt b/core/data/src/main/kotlin/com/withpeace/withpeace/core/data/mapper/PostDetailMapper.kt index a157f633..19ee9bee 100644 --- a/core/data/src/main/kotlin/com/withpeace/withpeace/core/data/mapper/PostDetailMapper.kt +++ b/core/data/src/main/kotlin/com/withpeace/withpeace/core/data/mapper/PostDetailMapper.kt @@ -6,18 +6,21 @@ import com.withpeace.withpeace.core.domain.model.post.PostDetail import com.withpeace.withpeace.core.domain.model.post.PostTitle import com.withpeace.withpeace.core.domain.model.post.PostUser import com.withpeace.withpeace.core.network.di.response.post.PostDetailResponse -import java.time.LocalDateTime -fun PostDetailResponse.toDomain() = PostDetail( - user = PostUser( - id = userId, - name = nickname, - profileImageUrl = profileImageUrl, - ), - id = postId, - title = PostTitle(title), - content = PostContent(content), - postTopic = type.toDomain(), - imageUrls = postImageUrls, - createDate = Date(createDate.toLocalDateTime()), -) +fun PostDetailResponse.toDomain(serverCurrentDate: String): PostDetail { + val createTime = createDate.toLocalDateTime() + return PostDetail( + user = PostUser( + id = userId, + name = nickname, + profileImageUrl = profileImageUrl, + ), + id = postId, + title = PostTitle(title), + content = PostContent(content), + postTopic = type.toDomain(), + imageUrls = postImageUrls, + createDate = Date(createTime), + nowDate = serverCurrentDate.toLocalDateTimeForGmt(), + ) +} diff --git a/core/data/src/main/kotlin/com/withpeace/withpeace/core/data/mapper/PostMapper.kt b/core/data/src/main/kotlin/com/withpeace/withpeace/core/data/mapper/PostMapper.kt index db403598..03d0d7e5 100644 --- a/core/data/src/main/kotlin/com/withpeace/withpeace/core/data/mapper/PostMapper.kt +++ b/core/data/src/main/kotlin/com/withpeace/withpeace/core/data/mapper/PostMapper.kt @@ -4,12 +4,13 @@ import com.withpeace.withpeace.core.domain.model.date.Date import com.withpeace.withpeace.core.domain.model.post.Post import com.withpeace.withpeace.core.network.di.response.post.PostResponse -fun PostResponse.toDomain() = +fun PostResponse.toDomain(nowDate: String) = Post( postId = postId, title = title, content = content, postTopic = type.toDomain(), createDate = Date(createDate.toLocalDateTime()), + nowDate = nowDate.toLocalDateTimeForGmt(), postImageUrl = postImageUrl, ) diff --git a/core/data/src/main/kotlin/com/withpeace/withpeace/core/data/paging/PostPagingSource.kt b/core/data/src/main/kotlin/com/withpeace/withpeace/core/data/paging/PostPagingSource.kt index efb08ddc..8d4403c4 100644 --- a/core/data/src/main/kotlin/com/withpeace/withpeace/core/data/paging/PostPagingSource.kt +++ b/core/data/src/main/kotlin/com/withpeace/withpeace/core/data/paging/PostPagingSource.kt @@ -3,6 +3,7 @@ package com.withpeace.withpeace.core.data.paging import androidx.paging.PagingSource import androidx.paging.PagingState import com.skydoves.sandwich.suspendMapSuccess +import com.skydoves.sandwich.suspendOnSuccess import com.withpeace.withpeace.core.data.mapper.toDomain import com.withpeace.withpeace.core.data.util.handleApiFailure import com.withpeace.withpeace.core.domain.model.error.CheonghaError @@ -59,8 +60,10 @@ class PostPagingSource( postService.getPosts( postTopic = postTopic.name, pageIndex = pageIndex, pageSize = pageSize, - ).suspendMapSuccess { - emit(data.map { it.toDomain() }) + ).suspendOnSuccess { + val serverCurrentDate = this.headers["Date"].toString() + val response = this.data + emit(response.data.map { it.toDomain(serverCurrentDate) }) }.handleApiFailure { onErrorWithAuthExpired(it, onError) } diff --git a/core/data/src/main/kotlin/com/withpeace/withpeace/core/data/repository/DefaultPostRepository.kt b/core/data/src/main/kotlin/com/withpeace/withpeace/core/data/repository/DefaultPostRepository.kt index bed30d0b..00786730 100644 --- a/core/data/src/main/kotlin/com/withpeace/withpeace/core/data/repository/DefaultPostRepository.kt +++ b/core/data/src/main/kotlin/com/withpeace/withpeace/core/data/repository/DefaultPostRepository.kt @@ -5,6 +5,7 @@ import androidx.paging.Pager import androidx.paging.PagingConfig import androidx.paging.PagingData import com.skydoves.sandwich.suspendMapSuccess +import com.skydoves.sandwich.suspendOnSuccess import com.withpeace.withpeace.core.data.mapper.toDomain import com.withpeace.withpeace.core.data.paging.PostPagingSource import com.withpeace.withpeace.core.data.util.convertToFile @@ -82,8 +83,10 @@ class DefaultPostRepository @Inject constructor( onError: suspend (CheonghaError) -> Unit, ): Flow = flow { postService.getPost(postId) - .suspendMapSuccess { - emit(data.toDomain()) + .suspendOnSuccess { + val serverCurrentDate = this.headers["Date"].toString() + val response = this.data + emit(response.data.toDomain(serverCurrentDate = serverCurrentDate)) }.handleApiFailure { onErrorWithAuthExpired(it, onError) } diff --git a/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/date/Date.kt b/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/date/Date.kt index 26e36d70..46977c18 100644 --- a/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/date/Date.kt +++ b/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/date/Date.kt @@ -6,6 +6,7 @@ import java.time.LocalDateTime value class Date( val date: LocalDateTime, ) { - val durationFromNow: DurationFromNow - get() = DurationFromNow.from(date) + fun durationFromNow(now: LocalDateTime): DurationFromNow { + return DurationFromNow.from(now, date) + } } diff --git a/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/date/DurationFromNow.kt b/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/date/DurationFromNow.kt index 3aa9270d..ce36f109 100644 --- a/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/date/DurationFromNow.kt +++ b/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/date/DurationFromNow.kt @@ -14,8 +14,8 @@ sealed class DurationFromNow( class OverOneYear(duration: Duration) : DurationFromNow(duration) companion object { - fun from(date: LocalDateTime): DurationFromNow { - val duration = Duration.between(date, LocalDateTime.now()) + fun from(now: LocalDateTime,date: LocalDateTime): DurationFromNow { + val duration = Duration.between(date, now) return when { duration.isOverOneYear() -> OverOneYear(duration) duration.isOverWeekDays() -> SevenDayToOneYear(duration) diff --git a/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/post/Post.kt b/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/post/Post.kt index 24b07a55..342cd2c0 100644 --- a/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/post/Post.kt +++ b/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/post/Post.kt @@ -1,6 +1,7 @@ package com.withpeace.withpeace.core.domain.model.post import com.withpeace.withpeace.core.domain.model.date.Date +import java.time.LocalDateTime data class Post( val postId: Long, @@ -8,5 +9,6 @@ data class Post( val content: String, val postTopic: PostTopic, val createDate: Date, + val nowDate: LocalDateTime, val postImageUrl: String?, ) diff --git a/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/post/PostDetail.kt b/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/post/PostDetail.kt index 7bd80c99..c0378d92 100644 --- a/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/post/PostDetail.kt +++ b/core/domain/src/main/java/com/withpeace/withpeace/core/domain/model/post/PostDetail.kt @@ -1,6 +1,8 @@ package com.withpeace.withpeace.core.domain.model.post import com.withpeace.withpeace.core.domain.model.date.Date +import java.time.Duration +import java.time.LocalDateTime data class PostDetail( val user: PostUser, @@ -10,6 +12,7 @@ data class PostDetail( val postTopic: PostTopic, val imageUrls: List, val createDate: Date, + val nowDate: LocalDateTime, ) data class PostUser( diff --git a/core/ui/src/main/java/com/withpeace/withpeace/core/ui/DateUiModel.kt b/core/ui/src/main/java/com/withpeace/withpeace/core/ui/DateUiModel.kt index 8bf992ac..2409f600 100644 --- a/core/ui/src/main/java/com/withpeace/withpeace/core/ui/DateUiModel.kt +++ b/core/ui/src/main/java/com/withpeace/withpeace/core/ui/DateUiModel.kt @@ -8,64 +8,62 @@ import java.time.LocalDateTime import java.time.format.DateTimeFormatter data class DateUiModel( - val date: LocalDateTime, val durationFromNow: DurationFromNowUiModel, -) { - val duration: Duration - get() = Duration.between(date, LocalDateTime.now()) -} +) -sealed class DurationFromNowUiModel { - data object LessThanOneMinute : DurationFromNowUiModel() - data object OneMinuteToOneHour : DurationFromNowUiModel() - data object OneHourToOneDay : DurationFromNowUiModel() - data object OneDayToSevenDay : DurationFromNowUiModel() - data object SevenDayToOneYear : DurationFromNowUiModel() - data object OverOneYear : DurationFromNowUiModel() +sealed interface DurationFromNowUiModel { + data class LessThanOneMinute(val duration: Duration) : DurationFromNowUiModel + data class OneMinuteToOneHour(val duration: Duration) : DurationFromNowUiModel + data class OneHourToOneDay(val duration: Duration) : DurationFromNowUiModel + data class OneDayToSevenDay(val duration: Duration) : DurationFromNowUiModel + data class SevenDayToOneYear(val date: LocalDateTime) : DurationFromNowUiModel + data class OverOneYear(val duration: Duration) : DurationFromNowUiModel } -fun Date.toUiModel(): DateUiModel = DateUiModel( - date = date, - durationFromNow = when (durationFromNow) { - is DurationFromNow.LessThanOneMinute -> DurationFromNowUiModel.LessThanOneMinute +fun Date.toDurationFromNowUiModel(nowDateTime: LocalDateTime): DateUiModel { + val durationFromNow = durationFromNow(nowDateTime) + return DateUiModel( + durationFromNow = when (durationFromNow) { + is DurationFromNow.LessThanOneMinute -> DurationFromNowUiModel.LessThanOneMinute(durationFromNow.value) - is DurationFromNow.OneDayToSevenDay -> DurationFromNowUiModel.OneDayToSevenDay + is DurationFromNow.OneDayToSevenDay -> DurationFromNowUiModel.OneDayToSevenDay(durationFromNow.value) - is DurationFromNow.OneHourToOneDay -> DurationFromNowUiModel.OneHourToOneDay + is DurationFromNow.OneHourToOneDay -> DurationFromNowUiModel.OneHourToOneDay(durationFromNow.value) - is DurationFromNow.OneMinuteToOneHour -> DurationFromNowUiModel.OneMinuteToOneHour + is DurationFromNow.OneMinuteToOneHour -> DurationFromNowUiModel.OneMinuteToOneHour(durationFromNow.value) - is DurationFromNow.OverOneYear -> DurationFromNowUiModel.OverOneYear + is DurationFromNow.OverOneYear -> DurationFromNowUiModel.OverOneYear(durationFromNow.value) - is DurationFromNow.SevenDayToOneYear -> DurationFromNowUiModel.SevenDayToOneYear - }, -) + is DurationFromNow.SevenDayToOneYear -> DurationFromNowUiModel.SevenDayToOneYear(this.date) + }, + ) +} fun DateUiModel.toRelativeString(context: Context): String { return when (durationFromNow) { is DurationFromNowUiModel.LessThanOneMinute -> { context.getString( R.string.second_format, - duration.seconds, + durationFromNow.duration.seconds, ) } is DurationFromNowUiModel.OneMinuteToOneHour -> context.getString( R.string.minute_format, - duration.toMinutes(), + durationFromNow.duration.toMinutes(), ) is DurationFromNowUiModel.OneHourToOneDay -> context.getString( R.string.hour_format, - duration.toHours(), + durationFromNow.duration.toHours(), ) is DurationFromNowUiModel.OneDayToSevenDay -> context.getString( R.string.day_format, - duration.toDays(), + durationFromNow.duration.toDays(), ) - is DurationFromNowUiModel.SevenDayToOneYear -> date.format( + is DurationFromNowUiModel.SevenDayToOneYear -> durationFromNow.date.format( DateTimeFormatter.ofPattern( DATE_FORMAT, ), @@ -73,7 +71,7 @@ fun DateUiModel.toRelativeString(context: Context): String { is DurationFromNowUiModel.OverOneYear -> context.getString( R.string.years_format, - duration.toDays() / DAYS_FOR_YEAR, + durationFromNow.duration.toDays() / DAYS_FOR_YEAR, ) } } diff --git a/core/ui/src/main/java/com/withpeace/withpeace/core/ui/post/PostDetailUiModel.kt b/core/ui/src/main/java/com/withpeace/withpeace/core/ui/post/PostDetailUiModel.kt index 578c1d8a..d94b1b9a 100644 --- a/core/ui/src/main/java/com/withpeace/withpeace/core/ui/post/PostDetailUiModel.kt +++ b/core/ui/src/main/java/com/withpeace/withpeace/core/ui/post/PostDetailUiModel.kt @@ -2,7 +2,7 @@ package com.withpeace.withpeace.core.ui.post import com.withpeace.withpeace.core.domain.model.post.PostDetail import com.withpeace.withpeace.core.ui.DateUiModel -import com.withpeace.withpeace.core.ui.toUiModel +import com.withpeace.withpeace.core.ui.toDurationFromNowUiModel data class PostDetailUiModel( val postUser: PostUserUiModel, @@ -32,6 +32,6 @@ fun PostDetail.toUiModel(currentUserId: Long): PostDetailUiModel = PostDetailUiM content = content.value, postTopic = postTopic.toUi(), imageUrls = imageUrls, - createDate = createDate.toUiModel(), + createDate = DateUiModel(createDate.toDurationFromNowUiModel(nowDate).durationFromNow), isMyPost = user.id == currentUserId, ) diff --git a/core/ui/src/main/java/com/withpeace/withpeace/core/ui/post/PostUiModel.kt b/core/ui/src/main/java/com/withpeace/withpeace/core/ui/post/PostUiModel.kt index 61a78a37..0ec48d24 100644 --- a/core/ui/src/main/java/com/withpeace/withpeace/core/ui/post/PostUiModel.kt +++ b/core/ui/src/main/java/com/withpeace/withpeace/core/ui/post/PostUiModel.kt @@ -2,7 +2,7 @@ package com.withpeace.withpeace.core.ui.post import com.withpeace.withpeace.core.domain.model.post.Post import com.withpeace.withpeace.core.ui.DateUiModel -import com.withpeace.withpeace.core.ui.toUiModel +import com.withpeace.withpeace.core.ui.toDurationFromNowUiModel data class PostUiModel( val postId: Long, @@ -19,6 +19,6 @@ fun Post.toPostUiModel() = title = title, content = content, postTopic = postTopic.toUi(), - createDate = createDate.toUiModel(), + createDate = DateUiModel(createDate.toDurationFromNowUiModel(nowDate).durationFromNow), postImageUrl = postImageUrl, ) diff --git a/feature/postdetail/src/main/java/com/withpeace/withpeace/feature/postdetail/PostDetailScreen.kt b/feature/postdetail/src/main/java/com/withpeace/withpeace/feature/postdetail/PostDetailScreen.kt index 3fef51c8..2f251d62 100644 --- a/feature/postdetail/src/main/java/com/withpeace/withpeace/feature/postdetail/PostDetailScreen.kt +++ b/feature/postdetail/src/main/java/com/withpeace/withpeace/feature/postdetail/PostDetailScreen.kt @@ -63,7 +63,6 @@ import com.withpeace.withpeace.core.ui.post.RegisterPostUiModel import com.withpeace.withpeace.core.ui.toRelativeString import com.withpeace.withpeace.feature.postdetail.R.drawable import com.withpeace.withpeace.feature.postdetail.R.string -import java.time.Duration import java.time.LocalDateTime @Composable @@ -512,13 +511,12 @@ private fun PostDetailScreenPreview() { postTopic = PostTopicUiModel.FREEDOM, imageUrls = listOf("", "", ""), createDate = DateUiModel( - LocalDateTime.now(), - DurationFromNowUiModel.LessThanOneMinute + DurationFromNowUiModel.SevenDayToOneYear(LocalDateTime.now()), ), isMyPost = false, ), ), - onAuthExpired = {} + onAuthExpired = {}, ) } } diff --git a/feature/postdetail/src/main/java/com/withpeace/withpeace/feature/postdetail/PostDetailViewModel.kt b/feature/postdetail/src/main/java/com/withpeace/withpeace/feature/postdetail/PostDetailViewModel.kt index 110eeb5b..8953a1ac 100644 --- a/feature/postdetail/src/main/java/com/withpeace/withpeace/feature/postdetail/PostDetailViewModel.kt +++ b/feature/postdetail/src/main/java/com/withpeace/withpeace/feature/postdetail/PostDetailViewModel.kt @@ -51,7 +51,6 @@ class PostDetailViewModel @Inject constructor( ClientError.AuthExpired -> _postUiState.update { PostDetailUiState.UnAuthorized } else -> _postUiState.update { PostDetailUiState.FailByNetwork } } - _postUiState.update { PostDetailUiState.FailByNetwork } // TODO: 게시글 NotFound 에러 대응 }, ).onEach { data -> diff --git a/feature/postlist/src/main/java/com/withpeace/withpeace/feature/postlist/PostListScreen.kt b/feature/postlist/src/main/java/com/withpeace/withpeace/feature/postlist/PostListScreen.kt index d56f1a28..7c9537b5 100644 --- a/feature/postlist/src/main/java/com/withpeace/withpeace/feature/postlist/PostListScreen.kt +++ b/feature/postlist/src/main/java/com/withpeace/withpeace/feature/postlist/PostListScreen.kt @@ -50,7 +50,6 @@ import com.withpeace.withpeace.core.ui.post.PostUiModel import com.withpeace.withpeace.core.ui.toRelativeString import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.flowOf -import java.time.Duration import java.time.LocalDateTime @Composable @@ -218,8 +217,8 @@ private fun PostListScreenPreview() { content = "varius", postTopic = PostTopicUiModel.ECONOMY, createDate = DateUiModel( - date = LocalDateTime.now(), - durationFromNow = DurationFromNowUiModel.LessThanOneMinute + durationFromNow = DurationFromNowUiModel.SevenDayToOneYear( + LocalDateTime.now()) ), postImageUrl = null, ) From 023b2704788ea05c38d1e16ff4a60546792e80b9 Mon Sep 17 00:00:00 2001 From: kwakjoohyeong Date: Fri, 19 Apr 2024 20:07:15 +0900 Subject: [PATCH 2/4] =?UTF-8?q?test:=20nowDate=20=EC=BB=AC=EB=9F=BC=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/data/paging/PostPagingSourceTest.kt | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/core/data/src/test/kotlin/com/withpeace/withpeace/core/data/paging/PostPagingSourceTest.kt b/core/data/src/test/kotlin/com/withpeace/withpeace/core/data/paging/PostPagingSourceTest.kt index 091ae7c2..fd8f5867 100644 --- a/core/data/src/test/kotlin/com/withpeace/withpeace/core/data/paging/PostPagingSourceTest.kt +++ b/core/data/src/test/kotlin/com/withpeace/withpeace/core/data/paging/PostPagingSourceTest.kt @@ -1,7 +1,7 @@ package com.withpeace.withpeace.core.data.paging import androidx.paging.PagingConfig -import androidx.paging.PagingSource.LoadResult +import androidx.paging.PagingSource import androidx.paging.testing.TestPager import com.google.common.truth.Truth.assertThat import com.skydoves.sandwich.ApiResponse @@ -67,7 +67,7 @@ class PostPagingSourceTest { ) // when val pager = TestPager(PagingConfig(20), postPagingSource) - val result = pager.refresh() as LoadResult.Page + val result = pager.refresh() as PagingSource.LoadResult.Page // then assertThat(result.data).containsExactlyElementsIn( List(20) { @@ -83,6 +83,10 @@ class PostPagingSourceTest { LocalTime.of(0, 0, 0), ), ), + nowDate = LocalDateTime.of( + LocalDate.of(2024, 4, 12), + LocalTime.of(0, 0, 0), + ), ) }, ).inOrder() @@ -102,7 +106,7 @@ class PostPagingSourceTest { var result = listOf() val pager = TestPager(PagingConfig(20), postPagingSource) result = - result + (pager.refresh() as LoadResult.Page).data + (pager.append() as LoadResult.Page).data + result + (pager.refresh() as PagingSource.LoadResult.Page).data + (pager.append() as PagingSource.LoadResult.Page).data // then assertThat(result).containsExactlyElementsIn( List(40) { @@ -118,6 +122,10 @@ class PostPagingSourceTest { LocalTime.of(0, 0, 0), ), ), + nowDate = LocalDateTime.of( + LocalDate.of(2024, 4, 12), + LocalTime.of(0, 0, 0), + ), ) }, ).inOrder() From fbfc90ff7a5a6b055e2b4493c0680ec3aadff011 Mon Sep 17 00:00:00 2001 From: kwakjoohyeong Date: Fri, 19 Apr 2024 20:54:02 +0900 Subject: [PATCH 3/4] =?UTF-8?q?test:=20api=20header=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/data/paging/PostPagingSourceTest.kt | 54 +++++++++++-------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/core/data/src/test/kotlin/com/withpeace/withpeace/core/data/paging/PostPagingSourceTest.kt b/core/data/src/test/kotlin/com/withpeace/withpeace/core/data/paging/PostPagingSourceTest.kt index fd8f5867..823234c6 100644 --- a/core/data/src/test/kotlin/com/withpeace/withpeace/core/data/paging/PostPagingSourceTest.kt +++ b/core/data/src/test/kotlin/com/withpeace/withpeace/core/data/paging/PostPagingSourceTest.kt @@ -1,7 +1,7 @@ package com.withpeace.withpeace.core.data.paging import androidx.paging.PagingConfig -import androidx.paging.PagingSource +import androidx.paging.PagingSource.LoadResult import androidx.paging.testing.TestPager import com.google.common.truth.Truth.assertThat import com.skydoves.sandwich.ApiResponse @@ -16,6 +16,8 @@ import com.withpeace.withpeace.core.network.di.service.PostService import io.mockk.coEvery import io.mockk.mockk import kotlinx.coroutines.test.runTest +import okhttp3.Headers +import okhttp3.internal.addHeaderLenient import org.junit.Before import org.junit.Test import retrofit2.Response @@ -30,29 +32,35 @@ class PostPagingSourceTest { @Before fun setup() { + val headers = Headers.Builder() + .add("Date", "Thu, 11 Apr 2024 15:00:00 GMT") + .build() + + val response = Response.success( + BaseResponse( + data = List(20) { + PostResponse( + postId = 0, + title = "title", + content = "content", + type = PostTopicResponse.FREEDOM, + postImageUrl = "null", + createDate = "2024/04/12 00:00:00", + ) + }, + error = null, + ), + headers + ) + val apiResponse = ApiResponse.Success>>(response) + coEvery { postService.getPosts( postTopic = any(), pageIndex = any(), pageSize = any(), ) - } returns ApiResponse.Success>>( - response = Response.success( - BaseResponse( - data = List(20) { - PostResponse( - postId = 0, - title = "title", - content = "content", - type = PostTopicResponse.FREEDOM, - postImageUrl = null, - createDate = "2024/04/12 00:00:00", - ) - }, - error = null, - ), - ), - ) + } returns apiResponse } @Test @@ -65,9 +73,11 @@ class PostPagingSourceTest { onError = {}, userRepository = userRepository ) + // when val pager = TestPager(PagingConfig(20), postPagingSource) - val result = pager.refresh() as PagingSource.LoadResult.Page + val result = pager.refresh() as LoadResult.Page + // then assertThat(result.data).containsExactlyElementsIn( List(20) { @@ -76,7 +86,7 @@ class PostPagingSourceTest { title = "title", content = "content", postTopic = PostTopic.FREEDOM, - postImageUrl = null, + postImageUrl = "null", createDate = Date( LocalDateTime.of( LocalDate.of(2024, 4, 12), @@ -106,7 +116,7 @@ class PostPagingSourceTest { var result = listOf() val pager = TestPager(PagingConfig(20), postPagingSource) result = - result + (pager.refresh() as PagingSource.LoadResult.Page).data + (pager.append() as PagingSource.LoadResult.Page).data + result + (pager.refresh() as LoadResult.Page).data + (pager.append() as LoadResult.Page).data // then assertThat(result).containsExactlyElementsIn( List(40) { @@ -115,7 +125,7 @@ class PostPagingSourceTest { title = "title", content = "content", postTopic = PostTopic.FREEDOM, - postImageUrl = null, + postImageUrl = "null", createDate = Date( LocalDateTime.of( LocalDate.of(2024, 4, 12), From fdce4b20046326d8421bd2a036910277cab6e047 Mon Sep 17 00:00:00 2001 From: kwakjoohyeong Date: Fri, 19 Apr 2024 21:02:33 +0900 Subject: [PATCH 4/4] =?UTF-8?q?test:=20PostListViewModelTest=20=EB=B9=A0?= =?UTF-8?q?=EC=A7=84=20=EC=BB=AC=EB=9F=BC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../withpeace/feature/postlist/PostListViewModelTest.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/feature/postlist/src/test/java/com/withpeace/withpeace/feature/postlist/PostListViewModelTest.kt b/feature/postlist/src/test/java/com/withpeace/withpeace/feature/postlist/PostListViewModelTest.kt index eba5a6ed..59189a50 100644 --- a/feature/postlist/src/test/java/com/withpeace/withpeace/feature/postlist/PostListViewModelTest.kt +++ b/feature/postlist/src/test/java/com/withpeace/withpeace/feature/postlist/PostListViewModelTest.kt @@ -57,6 +57,7 @@ class PostListViewModelTest { postTopic = PostTopic.FREEDOM, createDate = Date(date = LocalDateTime.MIN), postImageUrl = null, + nowDate = LocalDateTime.now() ) } coEvery {