feat(notice): 최신 공지를 v2 배열 API로 전환한다
This commit is contained in:
@@ -2,6 +2,7 @@ package kr.co.vividnext.sodalive.settings.notice
|
||||
|
||||
import io.reactivex.rxjava3.core.Single
|
||||
import kr.co.vividnext.sodalive.common.ApiResponse
|
||||
import kr.co.vividnext.sodalive.v2.mypage.notice.LatestNoticeResponse
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.Header
|
||||
import retrofit2.http.Query
|
||||
@@ -15,6 +16,6 @@ interface NoticeApi {
|
||||
@Header("Authorization") authHeader: String
|
||||
): Single<ApiResponse<GetNoticeResponse>>
|
||||
|
||||
@GET("/notice/latest")
|
||||
fun getLatestNotice(): Single<ApiResponse<NoticeItem>>
|
||||
@GET("/api/v2/notices/latest")
|
||||
fun getLatestNotices(): Single<ApiResponse<List<LatestNoticeResponse>>>
|
||||
}
|
||||
|
||||
@@ -13,5 +13,5 @@ class NoticeRepository(private val api: NoticeApi) {
|
||||
return api.getNotices(TimeZone.getDefault().id, page, size, authHeader = token)
|
||||
}
|
||||
|
||||
fun getLatestNotice() = api.getLatestNotice()
|
||||
fun getLatestNotices() = api.getLatestNotices()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package kr.co.vividnext.sodalive.v2.mypage.notice
|
||||
|
||||
import androidx.annotation.Keep
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import kr.co.vividnext.sodalive.settings.notice.NoticeItem
|
||||
|
||||
@Keep
|
||||
data class LatestNoticeResponse(
|
||||
@SerializedName("id") val id: Long,
|
||||
@SerializedName("title") val title: String,
|
||||
@SerializedName("content") val content: String,
|
||||
@SerializedName("date") val date: String
|
||||
) {
|
||||
fun toNoticeItem() = NoticeItem(title, content, date)
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package kr.co.vividnext.sodalive.v2.mypage.notice
|
||||
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers
|
||||
import kr.co.vividnext.sodalive.settings.notice.NoticeApi
|
||||
import kr.co.vividnext.sodalive.settings.notice.NoticeRepository
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Protocol
|
||||
import okhttp3.Request
|
||||
import okhttp3.Response
|
||||
import okhttp3.ResponseBody.Companion.toResponseBody
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import retrofit2.http.GET
|
||||
|
||||
class LatestNoticeApiTest {
|
||||
@Test
|
||||
fun `최신 공지는 파라미터 없는 v2 GET을 사용한다`() {
|
||||
val method = NoticeApi::class.java.declaredMethods.single { it.name.startsWith("getLatestNotice") }
|
||||
assertEquals("/api/v2/notices/latest", method.getAnnotation(GET::class.java)!!.value)
|
||||
assertEquals("getLatestNotices", method.name)
|
||||
assertEquals(0, method.parameterCount)
|
||||
assertEquals(listOf(GET::class.java), method.annotations.map { it.annotationClass.java })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `0개 배열을 그대로 전달한다`() = assertArray(0)
|
||||
|
||||
@Test
|
||||
fun `1개 배열을 그대로 전달한다`() = assertArray(1)
|
||||
|
||||
@Test
|
||||
fun `2개 배열은 서버 순서를 유지한다`() = assertArray(2)
|
||||
|
||||
@Test
|
||||
fun `3개 배열은 Long 식별자와 서버 순서를 유지한다`() = assertArray(3)
|
||||
|
||||
@Test
|
||||
fun `상세 payload는 제목 HTML 본문 날짜 원문 전체를 보존한다`() {
|
||||
val notice = LatestNoticeResponse(3_000_000_001L, " 공지 & 제목 ", "<p>원문<br>전체 & 본문</p>", "2026-10-28")
|
||||
val item = notice.toNoticeItem()
|
||||
assertEquals(" 공지 & 제목 ", item.title)
|
||||
assertEquals("<p>원문<br>전체 & 본문</p>", item.content)
|
||||
assertEquals("2026-10-28", item.date)
|
||||
}
|
||||
|
||||
private fun assertArray(count: Int) {
|
||||
val ids = listOf(3_000_000_001L, 2L, 9L).take(count)
|
||||
val data = ids.joinToString(",") {
|
||||
"""{"id":$it,"title":" 공지 & 제목 $it ","content":"<p>원문<br>전체 & 본문</p>","date":"2026-10-28"}"""
|
||||
}
|
||||
listOf("", ",\"message\":null,\"errorProperty\":null").forEach { nullableFields ->
|
||||
var request: Request? = null
|
||||
val client = OkHttpClient.Builder().addInterceptor { chain ->
|
||||
request = chain.request()
|
||||
Response.Builder()
|
||||
.request(chain.request())
|
||||
.protocol(Protocol.HTTP_1_1)
|
||||
.code(200)
|
||||
.message("OK")
|
||||
.body("""{"success":true,"data":[$data]$nullableFields}""".toResponseBody("application/json".toMediaType()))
|
||||
.build()
|
||||
}.build()
|
||||
val api = Retrofit.Builder()
|
||||
.baseUrl("https://example.invalid/")
|
||||
.client(client)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.addCallAdapterFactory(RxJava3CallAdapterFactory.createWithScheduler(Schedulers.trampoline()))
|
||||
.build().create(NoticeApi::class.java)
|
||||
val response = NoticeRepository(api).getLatestNotices().blockingGet()
|
||||
assertTrue(response.success)
|
||||
assertNull(response.message)
|
||||
assertNull(response.errorProperty)
|
||||
assertEquals(ids, response.data!!.map { it.id })
|
||||
response.data.forEach {
|
||||
assertEquals(" 공지 & 제목 ${it.id} ", it.title)
|
||||
assertEquals("<p>원문<br>전체 & 본문</p>", it.content)
|
||||
assertEquals("2026-10-28", it.date)
|
||||
}
|
||||
assertEquals("GET", request!!.method)
|
||||
assertEquals("/api/v2/notices/latest", request!!.url.encodedPath)
|
||||
assertNull(request!!.url.query)
|
||||
assertNull(request!!.body)
|
||||
assertNull(request!!.header("Authorization"))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user