test(content-ranking): 랭킹 API 통합 계약을 검증한다
This commit is contained in:
@@ -1,114 +1,122 @@
|
|||||||
package kr.co.vividnext.sodalive.v2.api.content.ranking.adapter.`in`.web
|
package kr.co.vividnext.sodalive.v2.api.content.ranking.adapter.`in`.web
|
||||||
|
|
||||||
import kr.co.vividnext.sodalive.common.CountryContext
|
|
||||||
import kr.co.vividnext.sodalive.configs.SecurityConfig
|
|
||||||
import kr.co.vividnext.sodalive.i18n.LangContext
|
|
||||||
import kr.co.vividnext.sodalive.i18n.SodaMessageSource
|
|
||||||
import kr.co.vividnext.sodalive.jwt.JwtAccessDeniedHandler
|
|
||||||
import kr.co.vividnext.sodalive.jwt.JwtAuthenticationEntryPoint
|
|
||||||
import kr.co.vividnext.sodalive.jwt.TokenProvider
|
|
||||||
import kr.co.vividnext.sodalive.member.Member
|
import kr.co.vividnext.sodalive.member.Member
|
||||||
import kr.co.vividnext.sodalive.member.MemberAdapter
|
import kr.co.vividnext.sodalive.member.MemberAdapter
|
||||||
import kr.co.vividnext.sodalive.member.MemberRole
|
import kr.co.vividnext.sodalive.member.MemberRole
|
||||||
import kr.co.vividnext.sodalive.v2.api.content.ranking.application.AudioRankingFacade
|
import kr.co.vividnext.sodalive.support.EmbeddedRedisInitializer
|
||||||
import kr.co.vividnext.sodalive.v2.api.content.ranking.dto.AudioRankingItemResponse
|
import kr.co.vividnext.sodalive.v2.content.ranking.adapter.out.persistence.AudioRankingSnapshot
|
||||||
import kr.co.vividnext.sodalive.v2.api.content.ranking.dto.AudioRankingResponse
|
|
||||||
import kr.co.vividnext.sodalive.v2.content.ranking.domain.AudioRankingType
|
import kr.co.vividnext.sodalive.v2.content.ranking.domain.AudioRankingType
|
||||||
import org.junit.jupiter.api.DisplayName
|
import org.junit.jupiter.api.DisplayName
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import org.mockito.Mockito
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean
|
import org.springframework.boot.test.context.SpringBootTest
|
||||||
import org.springframework.context.annotation.Import
|
|
||||||
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user
|
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user
|
||||||
|
import org.springframework.test.context.ContextConfiguration
|
||||||
import org.springframework.test.web.servlet.MockMvc
|
import org.springframework.test.web.servlet.MockMvc
|
||||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
|
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
|
||||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
|
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
|
||||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
|
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
|
||||||
|
import org.springframework.transaction.annotation.Transactional
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import javax.persistence.EntityManager
|
||||||
|
|
||||||
@WebMvcTest(AudioRankingController::class)
|
@SpringBootTest(properties = ["cloud.aws.cloud-front.host=https://cdn.test"])
|
||||||
@Import(SecurityConfig::class)
|
@AutoConfigureMockMvc
|
||||||
|
@Transactional
|
||||||
|
@ContextConfiguration(initializers = [EmbeddedRedisInitializer::class])
|
||||||
class AudioRankingControllerTest @Autowired constructor(
|
class AudioRankingControllerTest @Autowired constructor(
|
||||||
private val mockMvc: MockMvc
|
private val mockMvc: MockMvc,
|
||||||
|
private val entityManager: EntityManager
|
||||||
) {
|
) {
|
||||||
@MockBean
|
|
||||||
private lateinit var facade: AudioRankingFacade
|
|
||||||
|
|
||||||
@MockBean
|
|
||||||
private lateinit var countryContext: CountryContext
|
|
||||||
|
|
||||||
@MockBean
|
|
||||||
private lateinit var langContext: LangContext
|
|
||||||
|
|
||||||
@MockBean
|
|
||||||
private lateinit var sodaMessageSource: SodaMessageSource
|
|
||||||
|
|
||||||
@MockBean
|
|
||||||
private lateinit var tokenProvider: TokenProvider
|
|
||||||
|
|
||||||
@MockBean
|
|
||||||
private lateinit var accessDeniedHandler: JwtAccessDeniedHandler
|
|
||||||
|
|
||||||
@MockBean
|
|
||||||
private lateinit var authenticationEntryPoint: JwtAuthenticationEntryPoint
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("오디오 랭킹 조회는 비회원에게 200 OK와 기본 WEEKLY_POPULAR 랭킹을 반환한다")
|
@DisplayName("오디오 랭킹 조회는 비회원에게 200 OK와 기본 WEEKLY_POPULAR 랭킹을 반환한다")
|
||||||
fun shouldReturnWeeklyPopularRankingsForAnonymousByDefault() {
|
fun shouldReturnWeeklyPopularRankingsForAnonymousByDefault() {
|
||||||
Mockito.doReturn(rankingResponse(AudioRankingType.WEEKLY_POPULAR))
|
|
||||||
.`when`(facade).getRankings(AudioRankingType.WEEKLY_POPULAR, null)
|
|
||||||
|
|
||||||
mockMvc.perform(get("/api/v2/audio/rankings"))
|
mockMvc.perform(get("/api/v2/audio/rankings"))
|
||||||
.andExpect(status().isOk)
|
.andExpect(status().isOk)
|
||||||
.andExpect(jsonPath("$.success").value(true))
|
.andExpect(jsonPath("$.success").value(true))
|
||||||
|
.andExpect(jsonPath("$.data.showRankChange").value(false))
|
||||||
.andExpect(jsonPath("$.data.type").value("WEEKLY_POPULAR"))
|
.andExpect(jsonPath("$.data.type").value("WEEKLY_POPULAR"))
|
||||||
.andExpect(jsonPath("$.data.items").isArray)
|
.andExpect(jsonPath("$.data.items").isArray)
|
||||||
.andExpect(jsonPath("$.data.items[0].contentId").value(1L))
|
.andExpect(jsonPath("$.data.items.length()").value(0))
|
||||||
|
|
||||||
Mockito.verify(facade).getRankings(AudioRankingType.WEEKLY_POPULAR, null)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("오디오 랭킹 조회는 인증 회원과 요청 type을 facade에 전달한다")
|
@DisplayName("오디오 랭킹 조회는 인증 회원과 요청 type을 허용한다")
|
||||||
fun shouldPassAuthenticatedMemberAndRequestedTypeToFacade() {
|
fun shouldAcceptAuthenticatedMemberAndRequestedType() {
|
||||||
val member = Member(
|
val member = Member(
|
||||||
email = "viewer@test.com",
|
email = "viewer@test.com",
|
||||||
password = "password",
|
password = "password",
|
||||||
nickname = "viewer",
|
nickname = "viewer",
|
||||||
role = MemberRole.USER
|
role = MemberRole.USER
|
||||||
).apply { id = 10L }
|
).apply { id = 10L }
|
||||||
Mockito.doReturn(rankingResponse(AudioRankingType.RISING))
|
|
||||||
.`when`(facade).getRankings(eqValue(AudioRankingType.RISING), eqValue(member))
|
|
||||||
|
|
||||||
mockMvc.perform(get("/api/v2/audio/rankings").param("type", "RISING").with(user(MemberAdapter(member))))
|
mockMvc.perform(get("/api/v2/audio/rankings").param("type", "RISING").with(user(MemberAdapter(member))))
|
||||||
.andExpect(status().isOk)
|
.andExpect(status().isOk)
|
||||||
.andExpect(jsonPath("$.success").value(true))
|
.andExpect(jsonPath("$.success").value(true))
|
||||||
.andExpect(jsonPath("$.data.type").value("RISING"))
|
.andExpect(jsonPath("$.data.type").value("RISING"))
|
||||||
.andExpect(jsonPath("$.data.items").isArray)
|
.andExpect(jsonPath("$.data.items").isArray)
|
||||||
|
|
||||||
Mockito.verify(facade).getRankings(eqValue(AudioRankingType.RISING), eqValue(member))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun rankingResponse(type: AudioRankingType): AudioRankingResponse {
|
@Test
|
||||||
return AudioRankingResponse(
|
@DisplayName("오디오 랭킹 조회는 controller, facade, query service를 거쳐 순위 변화 응답 계약을 반환한다")
|
||||||
showRankChange = true,
|
fun shouldReturnRisingRankingSchemaThroughControllerFacadeAndQueryService() {
|
||||||
type = type,
|
saveSnapshot(contentId = 1L, rank = 1, aggregationStartAtUtc = PREVIOUS_START_AT, aggregationEndAtUtc = PREVIOUS_END_AT)
|
||||||
items = listOf(
|
saveSnapshot(contentId = 2L, rank = 2, aggregationStartAtUtc = PREVIOUS_START_AT, aggregationEndAtUtc = PREVIOUS_END_AT)
|
||||||
AudioRankingItemResponse(
|
saveSnapshot(contentId = 2L, rank = 1, aggregationStartAtUtc = LATEST_START_AT, aggregationEndAtUtc = LATEST_END_AT)
|
||||||
contentId = 1L,
|
saveSnapshot(contentId = 3L, rank = 2, aggregationStartAtUtc = LATEST_START_AT, aggregationEndAtUtc = LATEST_END_AT)
|
||||||
title = "ranking audio",
|
entityManager.flush()
|
||||||
creatorNickname = "creator",
|
entityManager.clear()
|
||||||
rank = 1,
|
|
||||||
rankChange = 2,
|
mockMvc.perform(get("/api/v2/audio/rankings").param("type", "RISING"))
|
||||||
isNew = false,
|
.andExpect(status().isOk)
|
||||||
coverImageUrl = "https://example.com/cover.jpg"
|
.andExpect(jsonPath("$.success").value(true))
|
||||||
)
|
.andExpect(jsonPath("$.data.showRankChange").value(true))
|
||||||
|
.andExpect(jsonPath("$.data.type").value("RISING"))
|
||||||
|
.andExpect(jsonPath("$.data.items[0].contentId").value(2L))
|
||||||
|
.andExpect(jsonPath("$.data.items[0].rank").value(1))
|
||||||
|
.andExpect(jsonPath("$.data.items[0].rankChange").value(1))
|
||||||
|
.andExpect(jsonPath("$.data.items[0].isNew").value(false))
|
||||||
|
.andExpect(jsonPath("$.data.items[1].contentId").value(3L))
|
||||||
|
.andExpect(jsonPath("$.data.items[1].rank").value(2))
|
||||||
|
.andExpect(jsonPath("$.data.items[1].rankChange").doesNotExist())
|
||||||
|
.andExpect(jsonPath("$.data.items[1].isNew").value(true))
|
||||||
|
.andExpect(jsonPath("$.data.items[0].finalScore").doesNotExist())
|
||||||
|
.andExpect(jsonPath("$.data.items[0].aggregationStartAtUtc").doesNotExist())
|
||||||
|
.andExpect(jsonPath("$.data.items[0].aggregationEndAtUtc").doesNotExist())
|
||||||
|
.andExpect(jsonPath("$.data.items[0].visibleFromAtUtc").doesNotExist())
|
||||||
|
.andExpect(jsonPath("$.data.fallback").doesNotExist())
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun saveSnapshot(
|
||||||
|
contentId: Long,
|
||||||
|
rank: Int,
|
||||||
|
aggregationStartAtUtc: LocalDateTime,
|
||||||
|
aggregationEndAtUtc: LocalDateTime
|
||||||
|
) {
|
||||||
|
entityManager.persist(
|
||||||
|
AudioRankingSnapshot(
|
||||||
|
rankingType = AudioRankingType.RISING,
|
||||||
|
aggregationStartAtUtc = aggregationStartAtUtc,
|
||||||
|
aggregationEndAtUtc = aggregationEndAtUtc,
|
||||||
|
visibleFromAtUtc = aggregationEndAtUtc.plusHours(9),
|
||||||
|
contentId = contentId,
|
||||||
|
title = "audio-$contentId",
|
||||||
|
creatorMemberId = 100L + contentId,
|
||||||
|
creatorNickname = "creator-$contentId",
|
||||||
|
coverImageUrl = "cover-$contentId.png",
|
||||||
|
releaseDate = LocalDateTime.of(2026, 6, contentId.toInt(), 0, 0),
|
||||||
|
isAdult = false,
|
||||||
|
rank = rank,
|
||||||
|
finalScore = (100 - rank).toDouble()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun <T> eqValue(value: T): T {
|
companion object {
|
||||||
return Mockito.eq(value) ?: value
|
private val PREVIOUS_START_AT = LocalDateTime.of(2026, 5, 25, 15, 0)
|
||||||
|
private val PREVIOUS_END_AT = LocalDateTime.of(2026, 6, 1, 15, 0)
|
||||||
|
private val LATEST_START_AT = LocalDateTime.of(2026, 6, 1, 15, 0)
|
||||||
|
private val LATEST_END_AT = LocalDateTime.of(2026, 6, 8, 15, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user