diff --git a/app/src/main/java/kr/co/vividnext/sodalive/v2/event/data/EventDetailApi.kt b/app/src/main/java/kr/co/vividnext/sodalive/v2/event/data/EventDetailApi.kt new file mode 100644 index 00000000..0474dc65 --- /dev/null +++ b/app/src/main/java/kr/co/vividnext/sodalive/v2/event/data/EventDetailApi.kt @@ -0,0 +1,15 @@ +package kr.co.vividnext.sodalive.v2.event.data + +import io.reactivex.rxjava3.core.Single +import kr.co.vividnext.sodalive.common.ApiResponse +import retrofit2.http.GET +import retrofit2.http.Header +import retrofit2.http.Path + +interface EventDetailApi { + @GET("/api/v2/events/{id}") + fun getEventDetail( + @Path("id") id: Long, + @Header("Authorization") authHeader: String? + ): Single> +} diff --git a/app/src/main/java/kr/co/vividnext/sodalive/v2/event/data/EventDetailRepository.kt b/app/src/main/java/kr/co/vividnext/sodalive/v2/event/data/EventDetailRepository.kt new file mode 100644 index 00000000..d8f5d4bf --- /dev/null +++ b/app/src/main/java/kr/co/vividnext/sodalive/v2/event/data/EventDetailRepository.kt @@ -0,0 +1,5 @@ +package kr.co.vividnext.sodalive.v2.event.data + +class EventDetailRepository(private val api: EventDetailApi) { + fun getEventDetail(id: Long, authHeader: String?) = api.getEventDetail(id = id, authHeader = authHeader) +} diff --git a/app/src/main/java/kr/co/vividnext/sodalive/v2/event/data/EventDetailResponse.kt b/app/src/main/java/kr/co/vividnext/sodalive/v2/event/data/EventDetailResponse.kt new file mode 100644 index 00000000..5c25f9e8 --- /dev/null +++ b/app/src/main/java/kr/co/vividnext/sodalive/v2/event/data/EventDetailResponse.kt @@ -0,0 +1,13 @@ +package kr.co.vividnext.sodalive.v2.event.data + +import androidx.annotation.Keep +import com.google.gson.annotations.SerializedName + +@Keep +data class EventDetailResponse( + @SerializedName("id") val id: Long?, + @SerializedName("thumbnailImageUrl") val thumbnailImageUrl: String?, + @SerializedName("detailImageUrl") val detailImageUrl: String?, + @SerializedName("popupImageUrl") val popupImageUrl: String?, + @SerializedName("link") val link: String? +) diff --git a/app/src/test/java/kr/co/vividnext/sodalive/v2/event/EventDetailApiTest.kt b/app/src/test/java/kr/co/vividnext/sodalive/v2/event/EventDetailApiTest.kt new file mode 100644 index 00000000..0e0e77f2 --- /dev/null +++ b/app/src/test/java/kr/co/vividnext/sodalive/v2/event/EventDetailApiTest.kt @@ -0,0 +1,196 @@ +package kr.co.vividnext.sodalive.v2.event + +import io.reactivex.rxjava3.schedulers.Schedulers +import kr.co.vividnext.sodalive.v2.event.data.EventDetailApi +import kr.co.vividnext.sodalive.v2.event.data.EventDetailRepository +import okhttp3.MediaType.Companion.toMediaType +import okhttp3.OkHttpClient +import okhttp3.Protocol +import okhttp3.Request +import okhttp3.Response +import okhttp3.ResponseBody.Companion.toResponseBody +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Test +import retrofit2.HttpException +import retrofit2.Retrofit +import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory +import retrofit2.converter.gson.GsonConverterFactory + +class EventDetailApiTest { + @Test + fun `비로그인 상세는 인증 헤더와 query 및 body 없는 정확한 GET이다`() { + val fixture = Fixture("""{"success":true,"data":{"id":7,"thumbnailImageUrl":"thumb"}}""") + + val response = fixture.repository.getEventDetail(7L, null).blockingGet() + + assertTrue(response.success) + assertEquals(7L, response.data?.id) + assertEquals("GET", fixture.request!!.method) + assertEquals("/api/v2/events/7", fixture.request!!.url.encodedPath) + assertNull(fixture.request!!.url.query) + assertNull(fixture.request!!.body) + assertNull(fixture.request!!.header("Authorization")) + } + + @Test + fun `로그인 상세는 Bearer 헤더를 보내고 Int 범위 초과 식별자를 보존한다`() { + val fixture = Fixture("""{"success":true,"data":{"id":3000000001,"thumbnailImageUrl":"thumb"}}""") + + val response = fixture.repository.getEventDetail(3_000_000_001L, "Bearer test-token").blockingGet() + + assertEquals(3_000_000_001L, response.data?.id) + assertEquals("/api/v2/events/3000000001", fixture.request!!.url.encodedPath) + assertEquals("Bearer test-token", fixture.request!!.header("Authorization")) + } + + @Test + fun `Long 최댓값은 요청 경로와 응답에서 손실 없이 유지된다`() { + val fixture = Fixture( + """{"success":true,"data":{"id":9223372036854775807,"thumbnailImageUrl":"thumb"}}""" + ) + + val response = fixture.repository.getEventDetail(Long.MAX_VALUE, null).blockingGet() + + assertEquals(Long.MAX_VALUE, response.data?.id) + assertEquals("/api/v2/events/9223372036854775807", fixture.request!!.url.encodedPath) + } + + @Test + fun `단일 상세의 이미지와 링크 문자열은 가공 없이 보존된다`() { + val fixture = Fixture( + """ + { + "success":true, + "data":{ + "id":7, + "thumbnailImageUrl":" thumb & 원문 ", + "detailImageUrl":"https://example.invalid/detail?a=1&b=2", + "popupImageUrl":" popup ", + "link":" sodalive://event/7 " + }, + "message":null, + "errorProperty":null + } + """ + ) + + val response = fixture.repository.getEventDetail(7L, null).blockingGet() + + assertEquals(" thumb & 원문 ", response.data?.thumbnailImageUrl) + assertEquals("https://example.invalid/detail?a=1&b=2", response.data?.detailImageUrl) + assertEquals(" popup ", response.data?.popupImageUrl) + assertEquals(" sodalive://event/7 ", response.data?.link) + assertNull(response.message) + assertNull(response.errorProperty) + } + + @Test + fun `선택 이미지와 링크의 null 또는 누락 및 빈 필수 문자열은 그대로 전달된다`() { + listOf("", """, "detailImageUrl":null,"popupImageUrl":null,"link":null""").forEach { optionalFields -> + val fixture = Fixture( + """{"success":true,"data":{"id":7,"thumbnailImageUrl":""$optionalFields}}""" + ) + + val detail = fixture.repository.getEventDetail(7L, null).blockingGet().data!! + + assertEquals("", detail.thumbnailImageUrl) + assertNull(detail.detailImageUrl) + assertNull(detail.popupImageUrl) + assertNull(detail.link) + } + } + + @Test + fun `조회 불가 envelope는 메시지와 null 데이터를 그대로 전달한다`() { + val fixture = Fixture( + """{"success":false,"data":null,"message":"잘못된 요청입니다.","errorProperty":null}""" + ) + + val response = fixture.repository.getEventDetail(7L, null).blockingGet() + + assertFalse(response.success) + assertNull(response.data) + assertEquals("잘못된 요청입니다.", response.message) + assertNull(response.errorProperty) + } + + @Test + fun `성공 data 누락과 null은 검증 가능한 null로 전달된다`() { + listOf("""{"success":true}""", """{"success":true,"data":null}""").forEach { json -> + val response = Fixture(json).repository.getEventDetail(7L, null).blockingGet() + + assertTrue(response.success) + assertNull(response.data) + } + } + + @Test + fun `필수 식별자 누락과 null은 기본 숫자로 대체하지 않는다`() { + listOf("", """"id":null,""").forEach { idField -> + val fixture = Fixture("""{"success":true,"data":{$idField"thumbnailImageUrl":"thumb"}}""") + + assertNull(fixture.repository.getEventDetail(7L, null).blockingGet().data!!.id) + } + } + + @Test + fun `필수 썸네일 누락과 null은 빈 문자열로 대체하지 않는다`() { + listOf("", """, "thumbnailImageUrl":null""").forEach { thumbnailField -> + val fixture = Fixture("""{"success":true,"data":{"id":7$thumbnailField}}""") + + assertNull(fixture.repository.getEventDetail(7L, null).blockingGet().data!!.thumbnailImageUrl) + } + } + + @Test + fun `상세 data의 빈 배열과 단일 객체 배열은 단일 객체로 수용하지 않는다`() { + listOf("[]", """[{"id":7,"thumbnailImageUrl":"thumb"}]""").forEach { data -> + Fixture("""{"success":true,"data":$data}""").repository.getEventDetail(7L, null) + .test() + .assertNoValues() + .assertError { it !is HttpException } + } + } + + @Test + fun `정상 HTTP의 비JSON 본문은 역직렬화 오류다`() { + Fixture("invalid response").repository.getEventDetail(7L, null) + .test() + .assertNoValues() + .assertError { it !is HttpException } + } + + @Test + fun `비JSON HTTP 오류는 envelope 파싱 없이 원래 HTTP 오류로 전달된다`() { + listOf(401, 404, 500).forEach { code -> + Fixture("private server error", code).repository.getEventDetail(7L, null) + .test() + .assertNoValues() + .assertError { it is HttpException && it.code() == code } + } + } + + private class Fixture(json: String, code: Int = 200) { + var request: Request? = null + private val client = OkHttpClient.Builder().addInterceptor { chain -> + request = chain.request() + Response.Builder() + .request(chain.request()) + .protocol(Protocol.HTTP_1_1) + .code(code) + .message("test response") + .body(json.toResponseBody("application/json".toMediaType())) + .build() + }.build() + private val api = Retrofit.Builder() + .baseUrl("https://example.invalid/") + .client(client) + .addConverterFactory(GsonConverterFactory.create()) + .addCallAdapterFactory(RxJava3CallAdapterFactory.createWithScheduler(Schedulers.trampoline())) + .build().create(EventDetailApi::class.java) + val repository = EventDetailRepository(api) + } +}