feat(ranking): 관리자 스냅샷 job API를 추가한다

This commit is contained in:
2026-06-09 11:50:56 +09:00
parent 4165c54a28
commit 67225fdc1d
3 changed files with 266 additions and 0 deletions

View File

@@ -0,0 +1,172 @@
package kr.co.vividnext.sodalive.v2.admin.ranking.creator
import kr.co.vividnext.sodalive.common.CountryContext
import kr.co.vividnext.sodalive.i18n.LangContext
import kr.co.vividnext.sodalive.i18n.SodaMessageSource
import kr.co.vividnext.sodalive.v2.ranking.port.out.CreatorRankingSnapshotJobRecord
import kr.co.vividnext.sodalive.v2.ranking.port.out.CreatorRankingSnapshotJobStatus
import kr.co.vividnext.sodalive.v2.ranking.port.out.CreatorRankingSnapshotJobTrigger
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.context.TestConfiguration
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Import
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.anonymous
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user
import org.springframework.security.web.SecurityFilterChain
import org.springframework.security.web.authentication.HttpStatusEntryPoint
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import java.time.LocalDateTime
import javax.servlet.http.HttpServletResponse
@WebMvcTest(AdminCreatorRankingSnapshotJobController::class)
@Import(AdminCreatorRankingSnapshotJobControllerTest.TestSecurityConfig::class)
class AdminCreatorRankingSnapshotJobControllerTest @Autowired constructor(
private val mockMvc: MockMvc
) {
@MockBean
private lateinit var service: AdminCreatorRankingSnapshotJobService
@MockBean
private lateinit var countryContext: CountryContext
@MockBean
private lateinit var langContext: LangContext
@MockBean
private lateinit var sodaMessageSource: SodaMessageSource
@TestConfiguration
@EnableGlobalMethodSecurity(prePostEnabled = true)
class TestSecurityConfig {
@Bean
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
return http
.csrf().disable()
.authorizeRequests()
.antMatchers("/admin/rankings/creators/snapshot-jobs/**").hasRole("ADMIN")
.anyRequest().permitAll()
.and()
.exceptionHandling()
.authenticationEntryPoint(HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
.accessDeniedHandler { _, response, _ -> response.sendError(HttpServletResponse.SC_FORBIDDEN) }
.and()
.build()
}
}
@Test
@DisplayName("관리자는 날짜 범위로 크리에이터 랭킹 수동 스냅샷 job을 생성한다")
fun shouldCreateManualSnapshotJobForAdmin() {
val response = AdminCreatorRankingSnapshotJobResponse.from(manualJob(status = CreatorRankingSnapshotJobStatus.PENDING))
Mockito.`when`(
service.createManualJob(
AdminCreatorRankingSnapshotJobRequest(
aggregationStartAtUtc = LocalDateTime.of(2026, 5, 31, 15, 0),
aggregationEndAtUtc = LocalDateTime.of(2026, 6, 7, 15, 0)
)
)
).thenReturn(response)
mockMvc.perform(
post("/admin/rankings/creators/snapshot-jobs")
.with(user("admin").roles("ADMIN"))
.contentType(MediaType.APPLICATION_JSON)
.content(
"""
{
"aggregationStartAtUtc": "2026-05-31T15:00:00",
"aggregationEndAtUtc": "2026-06-07T15:00:00"
}
""".trimIndent()
)
)
.andExpect(status().isOk)
.andExpect(jsonPath("$.success").value(true))
.andExpect(jsonPath("$.data.id").value(1L))
.andExpect(jsonPath("$.data.trigger").value("MANUAL"))
.andExpect(jsonPath("$.data.status").value("PENDING"))
.andExpect(jsonPath("$.data.retryable").value(false))
}
@Test
@DisplayName("관리자는 크리에이터 랭킹 스냅샷 job 목록을 조회한다")
fun shouldListSnapshotJobsForAdmin() {
Mockito.`when`(
service.getJobs(
aggregationStartAtUtc = LocalDateTime.of(2026, 5, 31, 15, 0),
aggregationEndAtUtc = LocalDateTime.of(2026, 6, 7, 15, 0),
statuses = listOf(CreatorRankingSnapshotJobStatus.FAILED)
)
).thenReturn(listOf(AdminCreatorRankingSnapshotJobResponse.from(manualJob(CreatorRankingSnapshotJobStatus.FAILED))))
mockMvc.perform(
get("/admin/rankings/creators/snapshot-jobs")
.param("aggregationStartAtUtc", "2026-05-31T15:00:00")
.param("aggregationEndAtUtc", "2026-06-07T15:00:00")
.param("statuses", "FAILED")
.with(user("admin").roles("ADMIN"))
)
.andExpect(status().isOk)
.andExpect(jsonPath("$.success").value(true))
.andExpect(jsonPath("$.data[0].status").value("FAILED"))
.andExpect(jsonPath("$.data[0].lastError").value("aggregate failed"))
.andExpect(jsonPath("$.data[0].retryable").value(true))
}
@Test
@DisplayName("관리자는 실패한 크리에이터 랭킹 스냅샷 job 재시도를 요청한다")
fun shouldRetryFailedSnapshotJobForAdmin() {
mockMvc.perform(
post("/admin/rankings/creators/snapshot-jobs/1/retry")
.with(user("admin").roles("ADMIN"))
)
.andExpect(status().isOk)
.andExpect(jsonPath("$.success").value(true))
}
@Test
@DisplayName("비관리자는 크리에이터 랭킹 스냅샷 job 관리자 API에 접근할 수 없다")
fun shouldRejectNonAdmin() {
mockMvc.perform(
post("/admin/rankings/creators/snapshot-jobs")
.with(user("user").roles("USER"))
.contentType(MediaType.APPLICATION_JSON)
.content("{}")
)
.andExpect(status().isForbidden)
mockMvc.perform(
post("/admin/rankings/creators/snapshot-jobs")
.with(anonymous())
.contentType(MediaType.APPLICATION_JSON)
.content("{}")
)
.andExpect(status().isUnauthorized)
}
private fun manualJob(status: CreatorRankingSnapshotJobStatus): CreatorRankingSnapshotJobRecord {
return CreatorRankingSnapshotJobRecord(
id = 1L,
aggregationStartAtUtc = LocalDateTime.of(2026, 5, 31, 15, 0),
aggregationEndAtUtc = LocalDateTime.of(2026, 6, 7, 15, 0),
trigger = CreatorRankingSnapshotJobTrigger.MANUAL,
status = status,
lastError = if (status == CreatorRankingSnapshotJobStatus.FAILED) "aggregate failed" else null,
processingStartedAt = null,
processedAt = null
)
}
}