diff --git a/src/test/kotlin/kr/co/vividnext/sodalive/v2/api/content/ranking/adapter/in/web/AudioRankingControllerTest.kt b/src/test/kotlin/kr/co/vividnext/sodalive/v2/api/content/ranking/adapter/in/web/AudioRankingControllerTest.kt index c22def8e..47f50290 100644 --- a/src/test/kotlin/kr/co/vividnext/sodalive/v2/api/content/ranking/adapter/in/web/AudioRankingControllerTest.kt +++ b/src/test/kotlin/kr/co/vividnext/sodalive/v2/api/content/ranking/adapter/in/web/AudioRankingControllerTest.kt @@ -1,114 +1,122 @@ 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.MemberAdapter import kr.co.vividnext.sodalive.member.MemberRole -import kr.co.vividnext.sodalive.v2.api.content.ranking.application.AudioRankingFacade -import kr.co.vividnext.sodalive.v2.api.content.ranking.dto.AudioRankingItemResponse -import kr.co.vividnext.sodalive.v2.api.content.ranking.dto.AudioRankingResponse +import kr.co.vividnext.sodalive.support.EmbeddedRedisInitializer +import kr.co.vividnext.sodalive.v2.content.ranking.adapter.out.persistence.AudioRankingSnapshot import kr.co.vividnext.sodalive.v2.content.ranking.domain.AudioRankingType import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test -import org.mockito.Mockito import org.springframework.beans.factory.annotation.Autowired -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest -import org.springframework.boot.test.mock.mockito.MockBean -import org.springframework.context.annotation.Import +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc +import org.springframework.boot.test.context.SpringBootTest 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.request.MockMvcRequestBuilders.get import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath 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) -@Import(SecurityConfig::class) +@SpringBootTest(properties = ["cloud.aws.cloud-front.host=https://cdn.test"]) +@AutoConfigureMockMvc +@Transactional +@ContextConfiguration(initializers = [EmbeddedRedisInitializer::class]) 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 @DisplayName("오디오 랭킹 조회는 비회원에게 200 OK와 기본 WEEKLY_POPULAR 랭킹을 반환한다") fun shouldReturnWeeklyPopularRankingsForAnonymousByDefault() { - Mockito.doReturn(rankingResponse(AudioRankingType.WEEKLY_POPULAR)) - .`when`(facade).getRankings(AudioRankingType.WEEKLY_POPULAR, null) - mockMvc.perform(get("/api/v2/audio/rankings")) .andExpect(status().isOk) .andExpect(jsonPath("$.success").value(true)) + .andExpect(jsonPath("$.data.showRankChange").value(false)) .andExpect(jsonPath("$.data.type").value("WEEKLY_POPULAR")) .andExpect(jsonPath("$.data.items").isArray) - .andExpect(jsonPath("$.data.items[0].contentId").value(1L)) - - Mockito.verify(facade).getRankings(AudioRankingType.WEEKLY_POPULAR, null) + .andExpect(jsonPath("$.data.items.length()").value(0)) } @Test - @DisplayName("오디오 랭킹 조회는 인증 회원과 요청 type을 facade에 전달한다") - fun shouldPassAuthenticatedMemberAndRequestedTypeToFacade() { + @DisplayName("오디오 랭킹 조회는 인증 회원과 요청 type을 허용한다") + fun shouldAcceptAuthenticatedMemberAndRequestedType() { val member = Member( email = "viewer@test.com", password = "password", nickname = "viewer", role = MemberRole.USER ).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)))) .andExpect(status().isOk) .andExpect(jsonPath("$.success").value(true)) .andExpect(jsonPath("$.data.type").value("RISING")) .andExpect(jsonPath("$.data.items").isArray) - - Mockito.verify(facade).getRankings(eqValue(AudioRankingType.RISING), eqValue(member)) } - private fun rankingResponse(type: AudioRankingType): AudioRankingResponse { - return AudioRankingResponse( - showRankChange = true, - type = type, - items = listOf( - AudioRankingItemResponse( - contentId = 1L, - title = "ranking audio", - creatorNickname = "creator", - rank = 1, - rankChange = 2, - isNew = false, - coverImageUrl = "https://example.com/cover.jpg" - ) + @Test + @DisplayName("오디오 랭킹 조회는 controller, facade, query service를 거쳐 순위 변화 응답 계약을 반환한다") + fun shouldReturnRisingRankingSchemaThroughControllerFacadeAndQueryService() { + saveSnapshot(contentId = 1L, rank = 1, aggregationStartAtUtc = PREVIOUS_START_AT, aggregationEndAtUtc = PREVIOUS_END_AT) + saveSnapshot(contentId = 2L, rank = 2, aggregationStartAtUtc = PREVIOUS_START_AT, aggregationEndAtUtc = PREVIOUS_END_AT) + saveSnapshot(contentId = 2L, rank = 1, aggregationStartAtUtc = LATEST_START_AT, aggregationEndAtUtc = LATEST_END_AT) + saveSnapshot(contentId = 3L, rank = 2, aggregationStartAtUtc = LATEST_START_AT, aggregationEndAtUtc = LATEST_END_AT) + entityManager.flush() + entityManager.clear() + + mockMvc.perform(get("/api/v2/audio/rankings").param("type", "RISING")) + .andExpect(status().isOk) + .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 eqValue(value: T): T { - return Mockito.eq(value) ?: value + companion object { + 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) } }