feat(content): 시리즈 상세의 언어 감지와 번역을 동기로 완료한다
This commit is contained in:
+146
@@ -0,0 +1,146 @@
|
||||
package kr.co.vividnext.sodalive.content.series
|
||||
|
||||
import kr.co.vividnext.sodalive.content.LanguageDetectionCacheService
|
||||
import kr.co.vividnext.sodalive.content.series.translation.SeriesTranslationPayload
|
||||
import kr.co.vividnext.sodalive.content.series.translation.SeriesTranslationRepository
|
||||
import kr.co.vividnext.sodalive.content.series.translation.TranslatedSeries
|
||||
import kr.co.vividnext.sodalive.creator.admin.content.series.Series
|
||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType
|
||||
import kr.co.vividnext.sodalive.i18n.translation.ResourceTranslationJobScheduler
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationLanguages
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationMemoryService
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationReadModelMaterializer
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationSourceField
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.PlatformTransactionManager
|
||||
import org.springframework.transaction.TransactionDefinition
|
||||
import org.springframework.transaction.annotation.Propagation
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import org.springframework.transaction.support.TransactionTemplate
|
||||
import javax.persistence.EntityManager
|
||||
import javax.persistence.LockModeType
|
||||
|
||||
data class ContentSeriesDetailTranslationResult(
|
||||
val languageCode: String?,
|
||||
val translated: TranslatedSeries? = null,
|
||||
val detectionQuery: String? = null
|
||||
)
|
||||
|
||||
@Service
|
||||
class ContentSeriesDetailTranslationService(
|
||||
private val repository: ContentSeriesRepository,
|
||||
private val translations: SeriesTranslationRepository,
|
||||
private val detector: LanguageDetectionCacheService,
|
||||
private val memories: TranslationMemoryService,
|
||||
private val materializer: TranslationReadModelMaterializer,
|
||||
private val scheduler: ResourceTranslationJobScheduler,
|
||||
private val entityManager: EntityManager,
|
||||
transactionManager: PlatformTransactionManager
|
||||
) {
|
||||
private val log = LoggerFactory.getLogger(javaClass)
|
||||
private val transaction = TransactionTemplate(transactionManager).apply {
|
||||
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
fun resolve(seriesId: Long, targetLanguage: String): ContentSeriesDetailTranslationResult {
|
||||
var source = transaction.execute { snapshot(seriesId) } ?: return ContentSeriesDetailTranslationResult(null)
|
||||
if (source.languageCode.isNullOrBlank()) {
|
||||
val beforeDetection = source
|
||||
try {
|
||||
val detected = detector.detectWithCache(source.query)?.detectedLanguage
|
||||
?: return ContentSeriesDetailTranslationResult(source.languageCode, detectionQuery = source.query)
|
||||
source = transaction.execute {
|
||||
val series = entityManager.find(Series::class.java, seriesId, LockModeType.PESSIMISTIC_WRITE)
|
||||
?: return@execute null
|
||||
val current = snapshot(seriesId) ?: return@execute null
|
||||
if (current == beforeDetection) series.languageCode = detected
|
||||
snapshot(seriesId)
|
||||
} ?: return ContentSeriesDetailTranslationResult(null)
|
||||
} catch (ex: Exception) {
|
||||
log.warn("Series detail language detection failed. seriesId={}, error={}", seriesId, ex.message)
|
||||
return ContentSeriesDetailTranslationResult(source.languageCode, detectionQuery = source.query)
|
||||
}
|
||||
}
|
||||
val language = source.languageCode
|
||||
if (language.isNullOrBlank()) return ContentSeriesDetailTranslationResult(language, detectionQuery = source.query)
|
||||
val target = targetLanguage.lowercase()
|
||||
if (
|
||||
!TranslationLanguages.isSupported(language) || !TranslationLanguages.isSupported(target) ||
|
||||
language.lowercase() == target
|
||||
) {
|
||||
return ContentSeriesDetailTranslationResult(language)
|
||||
}
|
||||
|
||||
val existing = transaction.execute { translations.findBySeriesIdAndLocale(seriesId, target)?.renderedPayload }
|
||||
if (existing != null) {
|
||||
maintainRetries(seriesId, target, scheduleMissing = false)
|
||||
return result(language, existing)
|
||||
}
|
||||
|
||||
return try {
|
||||
source.fields.forEach { field ->
|
||||
memories.ensureMemory(LanguageTranslationTargetType.SERIES, field.fieldKey, field.sourceText, language, target)
|
||||
}
|
||||
val payload = transaction.execute {
|
||||
entityManager.find(Series::class.java, seriesId, LockModeType.PESSIMISTIC_WRITE)
|
||||
check(snapshot(seriesId) == source) { "series translation source changed" }
|
||||
check(materializer.materialize(LanguageTranslationTargetType.SERIES, seriesId, target)) {
|
||||
"series translation is incomplete"
|
||||
}
|
||||
requireNotNull(translations.findBySeriesIdAndLocale(seriesId, target)).renderedPayload
|
||||
}
|
||||
maintainRetries(seriesId, target, scheduleMissing = false)
|
||||
result(language, requireNotNull(payload))
|
||||
} catch (ex: Exception) {
|
||||
log.warn("Series detail translation failed. seriesId={}, locale={}, error={}", seriesId, target, ex.message)
|
||||
maintainRetries(seriesId, target, scheduleMissing = true)
|
||||
ContentSeriesDetailTranslationResult(language)
|
||||
}
|
||||
}
|
||||
|
||||
private fun maintainRetries(seriesId: Long, target: String, scheduleMissing: Boolean) {
|
||||
try {
|
||||
transaction.executeWithoutResult {
|
||||
scheduler.restartFailedResourceTranslationsInCurrentTransaction(
|
||||
LanguageTranslationTargetType.SERIES,
|
||||
listOf(seriesId),
|
||||
target
|
||||
)
|
||||
if (scheduleMissing) {
|
||||
scheduler.scheduleResourceTranslation(LanguageTranslationTargetType.SERIES, seriesId, target)
|
||||
}
|
||||
}
|
||||
} catch (ex: Exception) {
|
||||
log.warn(
|
||||
"Series detail translation retry request failed. seriesId={}, locale={}, error={}",
|
||||
seriesId,
|
||||
target,
|
||||
ex.message
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun result(language: String, payload: SeriesTranslationPayload) = ContentSeriesDetailTranslationResult(
|
||||
language,
|
||||
TranslatedSeries(payload.title, payload.introduction, payload.keywords)
|
||||
)
|
||||
|
||||
private fun snapshot(seriesId: Long): DetailSource? {
|
||||
val series = repository.findById(seriesId).orElse(null) ?: return null
|
||||
val keywords = series.keywordList.mapNotNull { it.keyword?.tag }
|
||||
return DetailSource(
|
||||
series.languageCode,
|
||||
listOf(
|
||||
TranslationSourceField("title", series.title),
|
||||
TranslationSourceField("introduction", series.introduction),
|
||||
TranslationSourceField("keywords", keywords.joinToString(", "))
|
||||
),
|
||||
listOf(series.title.trim(), series.introduction.trim(), keywords.joinToString(" ").trim())
|
||||
.filter { it.isNotBlank() }.joinToString(" ")
|
||||
)
|
||||
}
|
||||
|
||||
private data class DetailSource(val languageCode: String?, val fields: List<TranslationSourceField>, val query: String)
|
||||
}
|
||||
@@ -2,13 +2,13 @@ package kr.co.vividnext.sodalive.content.series
|
||||
|
||||
import kr.co.vividnext.sodalive.common.SodaException
|
||||
import kr.co.vividnext.sodalive.content.ContentType
|
||||
import kr.co.vividnext.sodalive.content.LanguageDetectEvent
|
||||
import kr.co.vividnext.sodalive.content.LanguageDetectTargetType
|
||||
import kr.co.vividnext.sodalive.content.order.OrderRepository
|
||||
import kr.co.vividnext.sodalive.content.order.OrderType
|
||||
import kr.co.vividnext.sodalive.content.series.content.ContentSeriesContentRepository
|
||||
import kr.co.vividnext.sodalive.content.series.content.GetSeriesContentListResponse
|
||||
import kr.co.vividnext.sodalive.content.series.translation.SeriesGenreTranslationRepository
|
||||
import kr.co.vividnext.sodalive.content.series.translation.SeriesTranslationRepository
|
||||
import kr.co.vividnext.sodalive.content.series.translation.TranslatedSeries
|
||||
import kr.co.vividnext.sodalive.content.translation.ContentTranslationRepository
|
||||
import kr.co.vividnext.sodalive.creator.admin.content.series.SeriesPublishedDaysOfWeek
|
||||
import kr.co.vividnext.sodalive.creator.admin.content.series.SeriesSortType
|
||||
@@ -21,6 +21,7 @@ import kr.co.vividnext.sodalive.member.Member
|
||||
import kr.co.vividnext.sodalive.member.block.BlockMemberRepository
|
||||
import kr.co.vividnext.sodalive.member.contentpreference.MemberContentPreferenceService
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.context.ApplicationEventPublisher
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import java.time.ZoneId
|
||||
@@ -36,7 +37,8 @@ class ContentSeriesService(
|
||||
|
||||
private val langContext: LangContext,
|
||||
|
||||
private val seriesTranslationRepository: SeriesTranslationRepository,
|
||||
private val detailTranslationService: ContentSeriesDetailTranslationService,
|
||||
private val applicationEventPublisher: ApplicationEventPublisher,
|
||||
private val seriesGenreTranslationRepository: SeriesGenreTranslationRepository,
|
||||
private val contentTranslationRepository: ContentTranslationRepository,
|
||||
private val resourceTranslationJobScheduler: ResourceTranslationJobScheduler,
|
||||
@@ -259,52 +261,16 @@ class ContentSeriesService(
|
||||
limit = 5
|
||||
)
|
||||
|
||||
/**
|
||||
* series.languageCode != null && series.languageCode != languageCode
|
||||
*
|
||||
* 번역 시리즈를 조회한다. - series, locale
|
||||
* 번역 콘텐츠가 있으면
|
||||
* TranslatedSeries로 가공한다
|
||||
*
|
||||
* 번역 콘텐츠가 없으면
|
||||
* 누락 번역 job을 예약하고 원문 응답을 유지한다.
|
||||
*
|
||||
* 번역 대상: title, introduction, keywordList
|
||||
*
|
||||
* 파파고로 번역한 데이터를 TranslatedSeries 가공한다
|
||||
*/
|
||||
|
||||
val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy.MM.dd")
|
||||
|
||||
// 요청된 언어(locale)에 대한 시리즈 번역을 조회하거나, 없으면 동기 번역 후 저장한다.
|
||||
var translated: TranslatedSeries? = null
|
||||
run {
|
||||
val locale = langContext.lang.code
|
||||
val languageCode = series.languageCode
|
||||
// 원본 언어가 존재하고, 요청 언어와 다를 때만 번역 처리
|
||||
if (!languageCode.isNullOrBlank() && languageCode != locale) {
|
||||
resourceTranslationJobScheduler.restartFailedResourceTranslations(
|
||||
LanguageTranslationTargetType.SERIES,
|
||||
listOf(seriesId),
|
||||
locale
|
||||
)
|
||||
val existing = seriesTranslationRepository.findBySeriesIdAndLocale(seriesId = seriesId, locale = locale)
|
||||
if (existing != null) {
|
||||
val payload = existing.renderedPayload
|
||||
val kws = payload.keywords.ifEmpty { keywordList }
|
||||
translated = TranslatedSeries(
|
||||
title = payload.title,
|
||||
introduction = payload.introduction,
|
||||
keywords = kws
|
||||
)
|
||||
} else {
|
||||
resourceTranslationJobScheduler.scheduleResourceTranslation(
|
||||
resourceType = LanguageTranslationTargetType.SERIES,
|
||||
resourceId = seriesId,
|
||||
targetLanguage = locale
|
||||
)
|
||||
}
|
||||
}
|
||||
val translation = detailTranslationService.resolve(seriesId, langContext.lang.code)
|
||||
if (translation.detectionQuery != null) {
|
||||
applicationEventPublisher.publishEvent(
|
||||
LanguageDetectEvent(seriesId, translation.detectionQuery, LanguageDetectTargetType.SERIES)
|
||||
)
|
||||
}
|
||||
val translated = translation.translated?.let {
|
||||
it.copy(keywords = it.keywords.ifEmpty { keywordList })
|
||||
}
|
||||
|
||||
// 장르 번역 조회 (있으면 반환)
|
||||
|
||||
+2
@@ -19,6 +19,7 @@ import kr.co.vividnext.sodalive.content.theme.translation.ContentThemeTranslatio
|
||||
import kr.co.vividnext.sodalive.content.translation.ContentTranslation
|
||||
import kr.co.vividnext.sodalive.content.translation.ContentTranslationPayload
|
||||
import kr.co.vividnext.sodalive.content.translation.ContentTranslationRepository
|
||||
import kr.co.vividnext.sodalive.creator.admin.content.series.Series
|
||||
import kr.co.vividnext.sodalive.explorer.profile.creatorCommunity.CreatorCommunityRepository
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.community.translation.adapter.out.persistence.CreatorCommunityTranslation
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.community.translation.adapter.out.persistence.CreatorCommunityTranslationRepository
|
||||
@@ -141,6 +142,7 @@ class TranslationReadModelMaterializer(
|
||||
.map { it.trim() }
|
||||
.filter { it.isNotBlank() }
|
||||
)
|
||||
entityManager.find(Series::class.java, resourceId, LockModeType.PESSIMISTIC_WRITE)
|
||||
val existing = seriesTranslationRepository.findBySeriesIdAndLocale(resourceId, targetLanguage)
|
||||
if (existing == null) {
|
||||
seriesTranslationRepository.save(SeriesTranslation(resourceId, targetLanguage, payload))
|
||||
|
||||
+12
-2
@@ -3,6 +3,9 @@ package kr.co.vividnext.sodalive.content
|
||||
import kr.co.vividnext.sodalive.aws.cloudfront.AudioContentCloudFront
|
||||
import kr.co.vividnext.sodalive.aws.s3.S3Uploader
|
||||
import kr.co.vividnext.sodalive.configs.QueryDslConfig
|
||||
import kr.co.vividnext.sodalive.content.series.ContentSeriesController
|
||||
import kr.co.vividnext.sodalive.content.series.ContentSeriesDetailTranslationService
|
||||
import kr.co.vividnext.sodalive.content.series.ContentSeriesService
|
||||
import kr.co.vividnext.sodalive.content.theme.AudioContentThemeQueryRepository
|
||||
import kr.co.vividnext.sodalive.explorer.ExplorerQueryRepository
|
||||
import kr.co.vividnext.sodalive.i18n.Lang
|
||||
@@ -66,7 +69,8 @@ import javax.servlet.http.HttpServletResponse
|
||||
TranslationJobScheduler::class, ResourceTranslationJobScheduler::class,
|
||||
TranslationReadModelMaterializer::class, LanguageDetectionCacheService::class,
|
||||
AudioContentDetailTranslationService::class, TranslationMemoryService::class, TranslationJobWorker::class,
|
||||
LanguageDetectListener::class
|
||||
LanguageDetectListener::class, ContentSeriesController::class, ContentSeriesService::class,
|
||||
ContentSeriesDetailTranslationService::class
|
||||
)
|
||||
@MockBean(
|
||||
classes = [
|
||||
@@ -141,7 +145,13 @@ class DetailTranslationProvider : TranslationProvider, LanguageDetectionProvider
|
||||
calls.incrementAndGet()
|
||||
beforeTranslate()
|
||||
check(!fail) { "provider unavailable" }
|
||||
val translations = mapOf("원문 제목" to "English title", "원문 설명" to "English detail", "#힐링" to "#Healing")
|
||||
val translations = mapOf(
|
||||
"원문 제목" to "English title",
|
||||
"원문 설명" to "English detail",
|
||||
"#힐링" to "#Healing",
|
||||
"#힐링, #차분" to "#Healing, #Calm",
|
||||
"수정 제목" to "Edited title"
|
||||
)
|
||||
return TranslateResult(request.texts.map(translations::getValue))
|
||||
}
|
||||
}
|
||||
|
||||
+432
@@ -0,0 +1,432 @@
|
||||
package kr.co.vividnext.sodalive.content
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import kr.co.vividnext.sodalive.admin.content.series.genre.SeriesGenre
|
||||
import kr.co.vividnext.sodalive.content.hashtag.HashTag
|
||||
import kr.co.vividnext.sodalive.content.series.translation.SeriesGenreTranslation
|
||||
import kr.co.vividnext.sodalive.content.series.translation.SeriesGenreTranslationRepository
|
||||
import kr.co.vividnext.sodalive.content.series.translation.SeriesTranslation
|
||||
import kr.co.vividnext.sodalive.content.series.translation.SeriesTranslationPayload
|
||||
import kr.co.vividnext.sodalive.content.series.translation.SeriesTranslationRepository
|
||||
import kr.co.vividnext.sodalive.content.theme.AudioContentTheme
|
||||
import kr.co.vividnext.sodalive.content.translation.ContentTranslation
|
||||
import kr.co.vividnext.sodalive.content.translation.ContentTranslationPayload
|
||||
import kr.co.vividnext.sodalive.content.translation.ContentTranslationRepository
|
||||
import kr.co.vividnext.sodalive.creator.admin.content.series.Series
|
||||
import kr.co.vividnext.sodalive.creator.admin.content.series.SeriesContent
|
||||
import kr.co.vividnext.sodalive.creator.admin.content.series.keyword.SeriesKeyword
|
||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType
|
||||
import kr.co.vividnext.sodalive.i18n.translation.ResourceTranslationJobScheduler
|
||||
import kr.co.vividnext.sodalive.i18n.translation.SourceTextNormalizer
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationJobRepository
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationJobStatus
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationJobWorker
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationMemory
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationMemoryRepository
|
||||
import kr.co.vividnext.sodalive.member.Member
|
||||
import kr.co.vividnext.sodalive.member.MemberRole
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.DisplayName
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.params.ParameterizedTest
|
||||
import org.junit.jupiter.params.provider.NullAndEmptySource
|
||||
import org.junit.jupiter.params.provider.ValueSource
|
||||
import org.mockito.Mockito
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.test.context.SpringBootTest
|
||||
import org.springframework.boot.test.mock.mockito.SpyBean
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate
|
||||
import org.springframework.http.HttpEntity
|
||||
import org.springframework.http.HttpHeaders
|
||||
import org.springframework.http.HttpMethod
|
||||
import org.springframework.jdbc.core.JdbcTemplate
|
||||
import org.springframework.test.context.ActiveProfiles
|
||||
import org.springframework.transaction.PlatformTransactionManager
|
||||
import org.springframework.transaction.TransactionDefinition
|
||||
import org.springframework.transaction.support.TransactionTemplate
|
||||
import java.time.LocalDateTime
|
||||
import java.util.concurrent.Callable
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import java.util.concurrent.Executors
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.persistence.EntityManager
|
||||
|
||||
@SpringBootTest(
|
||||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||
classes = [ContentDetailTranslationTestConfiguration::class],
|
||||
properties = [
|
||||
"spring.cache.type=none", "spring.jpa.properties.hibernate.show_sql=false",
|
||||
"spring.jpa.database-platform=kr.co.vividnext.sodalive.support.H2MySqlFunctionDialect",
|
||||
"cloud.aws.cloud-front.host=https://cdn.test"
|
||||
]
|
||||
)
|
||||
@ActiveProfiles("content-detail-translation-test")
|
||||
class ContentSeriesDetailTranslationHttpIntegrationTest @Autowired constructor(
|
||||
private val http: TestRestTemplate,
|
||||
private val mapper: ObjectMapper,
|
||||
private val provider: DetailTranslationProvider,
|
||||
private val translations: SeriesTranslationRepository,
|
||||
private val genres: SeriesGenreTranslationRepository,
|
||||
private val contentTranslations: ContentTranslationRepository,
|
||||
private val memories: TranslationMemoryRepository,
|
||||
private val jobs: TranslationJobRepository,
|
||||
private val jdbc: JdbcTemplate,
|
||||
private val worker: TranslationJobWorker,
|
||||
private val scheduler: ResourceTranslationJobScheduler,
|
||||
private val entityManager: EntityManager,
|
||||
transactionManager: PlatformTransactionManager
|
||||
) {
|
||||
private val transaction = TransactionTemplate(transactionManager).apply {
|
||||
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW
|
||||
}
|
||||
|
||||
@SpyBean
|
||||
private lateinit var memoryWriter: TranslationMemoryRepository
|
||||
|
||||
@SpyBean
|
||||
private lateinit var jobClaims: TranslationJobRepository
|
||||
|
||||
@BeforeEach
|
||||
fun prepare() {
|
||||
provider.reset()
|
||||
jobs.deleteAllInBatch()
|
||||
translations.deleteAllInBatch()
|
||||
contentTranslations.deleteAllInBatch()
|
||||
genres.deleteAllInBatch()
|
||||
memories.deleteAllInBatch()
|
||||
jdbc.update("delete from language_detection_result")
|
||||
Mockito.doAnswer {
|
||||
jobs.findAll().filter { it.status == TranslationJobStatus.PENDING }.mapNotNull { it.id }
|
||||
}.`when`(jobClaims).findNextPendingGroupJobIdsForUpdate(Mockito.any(LocalDateTime::class.java) ?: LocalDateTime.now())
|
||||
// H2 JSON wraps converter output; preserve the real converter's MySQL text contract.
|
||||
listOf("series_translation", "content_translation").forEach {
|
||||
jdbc.execute("alter table $it alter column rendered_payload varchar(1000000)")
|
||||
}
|
||||
jdbc.execute(
|
||||
"CREATE ALIAS IF NOT EXISTS DATE_FORMAT FOR 'kr.co.vividnext.sodalive.support.H2MysqlDateFunctions.dateFormat'"
|
||||
)
|
||||
jdbc.execute(
|
||||
"CREATE ALIAS IF NOT EXISTS CONVERT_TZ FOR 'kr.co.vividnext.sodalive.support.H2MysqlDateFunctions.convertTz'"
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("시리즈 첫 HTTP 응답의 세 번역 필드는 커밋되고 재조회에 재사용된다")
|
||||
fun shouldCommitTranslationBeforeFirstResponseAndReuseIt() {
|
||||
val fixture = fixture()
|
||||
val response = detail(fixture)
|
||||
|
||||
assertTranslation(response)
|
||||
transaction.executeWithoutResult {
|
||||
val payload = translations.findBySeriesIdAndLocale(fixture.first, "en")!!.renderedPayload
|
||||
assertEquals(SeriesTranslationPayload("English title", "English detail", listOf("#Healing", "#Calm")), payload)
|
||||
}
|
||||
assertEquals(3, provider.calls.get())
|
||||
assertEquals(setOf("en"), memories.findAll().map { it.targetLanguage }.toSet())
|
||||
assertEquals(response.path("translated"), detail(fixture).path("translated"))
|
||||
assertEquals(3, provider.calls.get())
|
||||
assertEquals(0L, jobs.count())
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@NullAndEmptySource
|
||||
@ValueSource(strings = [" \t "])
|
||||
@DisplayName("null 빈 문자열 공백 언어는 같은 HTTP 응답 전에 감지 결과와 번역을 저장한다")
|
||||
fun shouldDetectAndCommitMissingLanguage(language: String?) {
|
||||
val fixture = fixture(language)
|
||||
val response = detail(fixture)
|
||||
|
||||
assertEquals("ko", transaction.execute { entityManager.find(Series::class.java, fixture.first).languageCode })
|
||||
assertTranslation(response)
|
||||
assertEquals(listOf("원문 제목 원문 설명 #힐링 #차분"), provider.detectionQueries)
|
||||
assertEquals(0L, jobs.count())
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("감지 캐시와 번역 메모리만으로 빈 소개와 키워드도 공급자 호출 없이 저장한다")
|
||||
fun shouldUseDetectionAndMemoryCachesWithEmptyFields() {
|
||||
val fixture = fixture(null, " ", false)
|
||||
jdbc.update(
|
||||
"insert into language_detection_result " +
|
||||
"(source_hash, source_text_sample, detected_language, provider, normalization_version) values (?, ?, ?, ?, ?)",
|
||||
SourceTextNormalizer.hash("원문 제목"),
|
||||
"원문 제목",
|
||||
"ko",
|
||||
"aws",
|
||||
SourceTextNormalizer.NORMALIZATION_VERSION
|
||||
)
|
||||
memories.saveAndFlush(TranslationMemory(SourceTextNormalizer.hash("원문 제목"), "원문 제목", "ko", "en", "Cached", "aws", "test"))
|
||||
val response = detail(fixture)
|
||||
|
||||
assertEquals("Cached", response.path("translated").path("title").textValue())
|
||||
assertEquals("", response.path("translated").path("introduction").textValue())
|
||||
assertEquals(emptyList<String>(), response.path("translated").path("keywords").map { it.textValue() })
|
||||
assertEquals("ko", transaction.execute { entityManager.find(Series::class.java, fixture.first).languageCode })
|
||||
assertEquals(1L, translations.count())
|
||||
assertEquals(0, provider.calls.get())
|
||||
assertTrue(provider.detectionQueries.isEmpty())
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = ["en", "fr"])
|
||||
@DisplayName("동일 또는 미지원 언어에는 번역과 job을 만들지 않는다")
|
||||
fun shouldKeepOriginalForSameOrUnsupportedLanguage(language: String) {
|
||||
val response = detail(fixture(language))
|
||||
assertTrue(response.path("translated").isNull)
|
||||
assertEquals("원문 제목", response.path("title").textValue())
|
||||
assertEquals(0, provider.calls.get())
|
||||
assertEquals(0L, translations.count())
|
||||
assertEquals(0L, jobs.count())
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("저장 번역의 빈 키워드는 기존 원문 fallback과 장르 응답을 유지한다")
|
||||
fun shouldKeepOriginalKeywordsWhenStoredTranslationKeywordsAreEmpty() {
|
||||
val fixture = fixture()
|
||||
translations.saveAndFlush(
|
||||
SeriesTranslation(fixture.first, "en", SeriesTranslationPayload("Stored", "Stored intro", emptyList()))
|
||||
)
|
||||
val response = detail(fixture)
|
||||
assertEquals("Stored", response.path("translated").path("title").textValue())
|
||||
assertEquals(listOf("#힐링", "#차분"), response.path("translated").path("keywords").map { it.textValue() })
|
||||
assertEquals("원문 장르", response.path("genre").textValue())
|
||||
assertEquals("Stored genre", response.path("translatedGenre").textValue())
|
||||
assertEquals(0, provider.calls.get())
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("공급자 실패는 원문 200과 복구 job을 남기고 worker가 누락 필드만 채운다")
|
||||
fun shouldRecoverPartialProviderFailureThroughWorker() {
|
||||
val fixture = fixture()
|
||||
provider.beforeTranslate = { if (provider.calls.get() == 2) provider.fail = true }
|
||||
val response = detail(fixture)
|
||||
assertTrue(response.path("translated").isNull)
|
||||
assertEquals("원문 제목", response.path("title").textValue())
|
||||
assertEquals(1L, memories.count())
|
||||
assertEquals(3L, jobs.count())
|
||||
provider.fail = false
|
||||
provider.beforeTranslate = {}
|
||||
assertTrue(worker.processNextGroup())
|
||||
assertTranslation(detail(fixture))
|
||||
assertEquals(4, provider.calls.get())
|
||||
assertEquals(setOf(TranslationJobStatus.COMPLETED), jobs.findAll().map { it.status }.toSet())
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("실제 번역 저장 제약 실패는 별도 트랜잭션에서 rollback되고 worker로 복구된다")
|
||||
fun shouldIsolatePayloadRollbackAndRecoverCommittedMemories() {
|
||||
val fixture = fixture()
|
||||
jdbc.execute("alter table series_translation add constraint reject_series_payload check (series_id <> ${fixture.first})")
|
||||
try {
|
||||
val response = detail(fixture)
|
||||
assertTrue(response.path("translated").isNull)
|
||||
assertEquals("원문 제목", response.path("title").textValue())
|
||||
assertEquals(0L, translations.count())
|
||||
assertEquals(3L, memories.count())
|
||||
assertEquals(3L, jobs.count())
|
||||
} finally {
|
||||
jdbc.execute("alter table series_translation drop constraint reject_series_payload")
|
||||
}
|
||||
assertTrue(worker.processNextGroup())
|
||||
assertTranslation(detail(fixture))
|
||||
assertEquals(3, provider.calls.get())
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("동기 감지 실패 뒤 기존 SERIES after-commit 이벤트가 언어와 복구 작업을 저장한다")
|
||||
fun shouldRetainSeriesDetectionEventRecovery() {
|
||||
val fixture = fixture(null)
|
||||
provider.detection = null
|
||||
provider.beforeDetect = { if (provider.detectionQueries.size == 2) provider.detection = "ko" }
|
||||
val response = detail(fixture)
|
||||
assertTrue(response.path("translated").isNull)
|
||||
assertEquals("ko", transaction.execute { entityManager.find(Series::class.java, fixture.first).languageCode })
|
||||
assertEquals(6L, jobs.count())
|
||||
assertEquals(setOf(LanguageTranslationTargetType.SERIES), jobs.findAll().map { it.resourceType }.toSet())
|
||||
assertEquals(0, provider.calls.get())
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("감지 중 커밋된 원문 언어 변경을 상세의 managed entity가 덮어쓰지 않는다")
|
||||
fun shouldPreserveSourceAndLanguageEditedDuringDetection() {
|
||||
val fixture = fixture(null)
|
||||
provider.beforeDetect = {
|
||||
transaction.executeWithoutResult {
|
||||
entityManager.find(Series::class.java, fixture.first).apply {
|
||||
title = "수정 제목"
|
||||
languageCode = "en"
|
||||
}
|
||||
}
|
||||
}
|
||||
assertTrue(detail(fixture).path("translated").isNull)
|
||||
transaction.executeWithoutResult {
|
||||
val series = entityManager.find(Series::class.java, fixture.first)
|
||||
assertEquals("수정 제목", series.title)
|
||||
assertEquals("en", series.languageCode)
|
||||
}
|
||||
assertEquals(0, provider.calls.get())
|
||||
assertEquals(0L, jobs.count())
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("번역 공급자 호출 중 원문 수정은 오래된 번역을 저장하지 않고 현재 원문 작업으로 복구한다")
|
||||
fun shouldRejectStaleTranslationAndRecoverCurrentSource() {
|
||||
val fixture = fixture()
|
||||
provider.beforeTranslate = {
|
||||
transaction.executeWithoutResult { entityManager.find(Series::class.java, fixture.first).title = "수정 제목" }
|
||||
}
|
||||
assertTrue(detail(fixture).path("translated").isNull)
|
||||
assertEquals(0L, translations.count())
|
||||
assertEquals(3L, jobs.count())
|
||||
assertEquals("수정 제목", jobs.findAll().single { it.fieldKey == "title" }.sourceText)
|
||||
provider.beforeTranslate = {}
|
||||
assertTrue(worker.processNextGroup())
|
||||
assertEquals("Edited title", detail(fixture).path("translated").path("title").textValue())
|
||||
assertEquals("수정 제목", transaction.execute { entityManager.find(Series::class.java, fixture.first).title })
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("동시 상세와 worker는 메모리 및 시리즈 번역을 각각 한 행으로 수렴시킨다")
|
||||
fun shouldConvergeConcurrentDetailsAndWorker() {
|
||||
val fixture = fixture()
|
||||
scheduler.scheduleResourceTranslation(LanguageTranslationTargetType.SERIES, fixture.first, "en")
|
||||
val inserts = CountDownLatch(3)
|
||||
val originalSave = Mockito.mockingDetails(memoryWriter).mockCreationSettings.defaultAnswer
|
||||
Mockito.doAnswer { invocation ->
|
||||
if (invocation.getArgument<TranslationMemory>(0).sourceText == "원문 제목") {
|
||||
inserts.countDown()
|
||||
check(inserts.await(10, TimeUnit.SECONDS)) { "competing series inserts did not arrive" }
|
||||
}
|
||||
originalSave.answer(invocation)
|
||||
}.`when`(memoryWriter).save(Mockito.any(TranslationMemory::class.java) ?: TranslationMemory("", "", "", "", "", "", ""))
|
||||
val pool = Executors.newFixedThreadPool(3)
|
||||
try {
|
||||
val left = pool.submit(Callable { detail(fixture) })
|
||||
val right = pool.submit(Callable { detail(fixture) })
|
||||
val background = pool.submit(Callable { worker.processNextGroup() })
|
||||
assertTranslation(left.get(20, TimeUnit.SECONDS))
|
||||
assertTranslation(right.get(20, TimeUnit.SECONDS))
|
||||
assertTrue(background.get(20, TimeUnit.SECONDS))
|
||||
assertEquals(3L, memories.count())
|
||||
assertEquals(1L, translations.count())
|
||||
assertEquals(setOf(TranslationJobStatus.COMPLETED), jobs.findAll().map { it.status }.toSet())
|
||||
} finally {
|
||||
pool.shutdownNow()
|
||||
check(pool.awaitTermination(5, TimeUnit.SECONDS))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("하위 콘텐츠는 요청 언어의 저장 제목만 사용하고 누락 빈 제목은 원문을 유지한다")
|
||||
fun shouldKeepChildTitlesStoredOnlyForEachLocale() {
|
||||
val fixture = fixture()
|
||||
val children = transaction.execute {
|
||||
val series = entityManager.find(Series::class.java, fixture.first)
|
||||
listOf("원문 child", "빈 child", "누락 child").map { title ->
|
||||
val content = child(series, title)
|
||||
requireNotNull(content.id) to title
|
||||
}
|
||||
}!!
|
||||
contentTranslations.saveAllAndFlush(
|
||||
listOf(
|
||||
ContentTranslation(children[0].first, "en", ContentTranslationPayload("Stored child", "", "")),
|
||||
ContentTranslation(children[0].first, "ja", ContentTranslationPayload("Japanese child", "", "")),
|
||||
ContentTranslation(children[1].first, "en", ContentTranslationPayload(" \t ", "", ""))
|
||||
)
|
||||
)
|
||||
translations.saveAndFlush(SeriesTranslation(fixture.first, "ja", SeriesTranslationPayload("Japanese", "", emptyList())))
|
||||
listOf("en", "ja", "en").forEach { locale ->
|
||||
val response = detail(fixture, locale)
|
||||
val titles = response.path("contentList").associate {
|
||||
it.path("contentId").longValue() to it.path("title").textValue()
|
||||
}
|
||||
assertEquals(if (locale == "en") "Stored child" else "Japanese child", titles[children[0].first])
|
||||
assertEquals("빈 child", titles[children[1].first])
|
||||
assertEquals("누락 child", titles[children[2].first])
|
||||
assertEquals(4, response.path("contentCount").intValue())
|
||||
assertEquals("원문 제목", response.path("title").textValue())
|
||||
assertEquals("원문 설명", response.path("introduction").textValue())
|
||||
assertEquals(3, provider.calls.get())
|
||||
assertEquals(0L, jobs.count())
|
||||
assertEquals(3L, contentTranslations.count())
|
||||
}
|
||||
}
|
||||
|
||||
private fun detail(fixture: Pair<Long, Long>, locale: String = "en"): JsonNode {
|
||||
val response = http.exchange(
|
||||
"/audio-content/series/${fixture.first}",
|
||||
HttpMethod.GET,
|
||||
HttpEntity<Void>(
|
||||
HttpHeaders().apply {
|
||||
set("X-Test-Member", fixture.second.toString())
|
||||
set("Accept-Language", locale)
|
||||
}
|
||||
),
|
||||
String::class.java
|
||||
)
|
||||
assertEquals(200, response.statusCodeValue, response.body)
|
||||
val body = mapper.readTree(response.body)
|
||||
assertTrue(body.path("success").booleanValue(), response.body)
|
||||
return body.path("data")
|
||||
}
|
||||
|
||||
private fun assertTranslation(response: JsonNode) {
|
||||
val translated = response.path("translated")
|
||||
assertEquals("English title", translated.path("title").textValue())
|
||||
assertEquals("English detail", translated.path("introduction").textValue())
|
||||
assertEquals(listOf("#Healing", "#Calm"), translated.path("keywords").map { it.textValue() })
|
||||
}
|
||||
|
||||
private fun fixture(language: String? = "ko", introduction: String = "원문 설명", keywords: Boolean = true): Pair<Long, Long> =
|
||||
transaction.execute {
|
||||
val creator = Member(password = "test", nickname = "series-creator", role = MemberRole.CREATOR)
|
||||
val viewer = Member(password = "test", nickname = "series-viewer")
|
||||
val genre = SeriesGenre("원문 장르")
|
||||
listOf(creator, viewer, genre).forEach(entityManager::persist)
|
||||
val series = Series("원문 제목", introduction, language).apply {
|
||||
member = creator
|
||||
this.genre = genre
|
||||
coverImage = "series.png"
|
||||
}
|
||||
entityManager.persist(series)
|
||||
genres.save(SeriesGenreTranslation(requireNotNull(genre.id), "en", "Stored genre"))
|
||||
if (keywords) {
|
||||
listOf("#힐링", "#차분").forEachIndexed { index, tag ->
|
||||
val keyword = HashTag(tag)
|
||||
entityManager.persist(keyword)
|
||||
entityManager.persist(
|
||||
SeriesKeyword().apply {
|
||||
this.series = series
|
||||
this.keyword = keyword
|
||||
isActive = index == 0
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
child(series, "기본 child")
|
||||
requireNotNull(series.id) to requireNotNull(viewer.id)
|
||||
}!!
|
||||
|
||||
private fun child(series: Series, title: String): AudioContent {
|
||||
val theme = AudioContentTheme("child theme", "theme.png")
|
||||
entityManager.persist(theme)
|
||||
val content = AudioContent(title, "child detail", "ko", releaseDate = LocalDateTime.of(2020, 1, 1, 0, 0)).apply {
|
||||
member = series.member
|
||||
this.theme = theme
|
||||
coverImage = "child.png"
|
||||
duration = "00:01:00"
|
||||
isActive = true
|
||||
}
|
||||
entityManager.persist(content)
|
||||
entityManager.persist(
|
||||
SeriesContent().apply {
|
||||
this.series = series
|
||||
this.content = content
|
||||
}
|
||||
)
|
||||
return content
|
||||
}
|
||||
}
|
||||
+16
-1
@@ -2,7 +2,9 @@ package kr.co.vividnext.sodalive.i18n.translation
|
||||
|
||||
import kr.co.vividnext.sodalive.admin.content.series.genre.SeriesGenre
|
||||
import kr.co.vividnext.sodalive.content.ContentType
|
||||
import kr.co.vividnext.sodalive.content.LanguageDetectionCacheService
|
||||
import kr.co.vividnext.sodalive.content.order.OrderRepository
|
||||
import kr.co.vividnext.sodalive.content.series.ContentSeriesDetailTranslationService
|
||||
import kr.co.vividnext.sodalive.content.series.ContentSeriesRepository
|
||||
import kr.co.vividnext.sodalive.content.series.ContentSeriesService
|
||||
import kr.co.vividnext.sodalive.content.series.GetSeriesGenreListResponse
|
||||
@@ -25,7 +27,9 @@ 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.context.ApplicationEventPublisher
|
||||
import java.time.LocalDateTime
|
||||
import java.util.Optional
|
||||
|
||||
class ContentSeriesReadRetryTest : MetadataReadRetryTestSupport() {
|
||||
@Autowired
|
||||
@@ -55,6 +59,7 @@ class ContentSeriesReadRetryTest : MetadataReadRetryTestSupport() {
|
||||
createdAt = LocalDateTime.of(2026, 1, 1, 0, 0)
|
||||
}
|
||||
Mockito.`when`(repository.getSeriesDetail(10L, false, ContentType.ALL)).thenReturn(series)
|
||||
Mockito.`when`(repository.findById(10L)).thenReturn(Optional.of(series))
|
||||
Mockito.`when`(repository.getSeriesContentMinMaxPrice(10L)).thenReturn(GetSeriesContentMinMaxPriceResponse(0, 0))
|
||||
Mockito.`when`(contents.getContentList(10L, false, ContentType.ALL, "https://cdn.test", SeriesSortType.NEWEST, 0, 5))
|
||||
.thenReturn(emptyList())
|
||||
@@ -91,7 +96,17 @@ class ContentSeriesReadRetryTest : MetadataReadRetryTestSupport() {
|
||||
explorerQueryRepository = Mockito.mock(ExplorerQueryRepository::class.java),
|
||||
seriesContentRepository = contents,
|
||||
langContext = langContext,
|
||||
seriesTranslationRepository = seriesTranslations,
|
||||
detailTranslationService = ContentSeriesDetailTranslationService(
|
||||
repository,
|
||||
seriesTranslations,
|
||||
Mockito.mock(LanguageDetectionCacheService::class.java),
|
||||
Mockito.mock(TranslationMemoryService::class.java),
|
||||
Mockito.mock(TranslationReadModelMaterializer::class.java),
|
||||
scheduler,
|
||||
entityManager,
|
||||
transactionManager
|
||||
),
|
||||
applicationEventPublisher = Mockito.mock(ApplicationEventPublisher::class.java),
|
||||
seriesGenreTranslationRepository = genreTranslations,
|
||||
contentTranslationRepository = Mockito.mock(ContentTranslationRepository::class.java),
|
||||
resourceTranslationJobScheduler = scheduler,
|
||||
|
||||
Reference in New Issue
Block a user