feat(content): 관리자 번역 후보 선택과 필드별 승인 보호를 구현한다
This commit is contained in:
@@ -9,15 +9,17 @@ import kr.co.vividnext.sodalive.aws.cloudfront.AudioContentCloudFront
|
|||||||
import kr.co.vividnext.sodalive.aws.s3.S3Uploader
|
import kr.co.vividnext.sodalive.aws.s3.S3Uploader
|
||||||
import kr.co.vividnext.sodalive.common.SodaException
|
import kr.co.vividnext.sodalive.common.SodaException
|
||||||
import kr.co.vividnext.sodalive.content.main.tab.GetContentMainTabItem
|
import kr.co.vividnext.sodalive.content.main.tab.GetContentMainTabItem
|
||||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationEvent
|
|
||||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType
|
|
||||||
import kr.co.vividnext.sodalive.utils.generateFileName
|
import kr.co.vividnext.sodalive.utils.generateFileName
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationSourceInvalidationService
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.toContentSourceSnapshot
|
||||||
import org.springframework.beans.factory.annotation.Value
|
import org.springframework.beans.factory.annotation.Value
|
||||||
import org.springframework.context.ApplicationEventPublisher
|
|
||||||
import org.springframework.data.domain.Pageable
|
import org.springframework.data.domain.Pageable
|
||||||
import org.springframework.data.repository.findByIdOrNull
|
import org.springframework.data.repository.findByIdOrNull
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
|
import org.springframework.transaction.PlatformTransactionManager
|
||||||
|
import org.springframework.transaction.TransactionDefinition
|
||||||
import org.springframework.transaction.annotation.Transactional
|
import org.springframework.transaction.annotation.Transactional
|
||||||
|
import org.springframework.transaction.support.TransactionTemplate
|
||||||
import org.springframework.web.multipart.MultipartFile
|
import org.springframework.web.multipart.MultipartFile
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@@ -29,10 +31,14 @@ class AdminContentService(
|
|||||||
private val contentMainTabRepository: AdminContentMainTabRepository,
|
private val contentMainTabRepository: AdminContentMainTabRepository,
|
||||||
private val objectMapper: ObjectMapper,
|
private val objectMapper: ObjectMapper,
|
||||||
private val s3Uploader: S3Uploader,
|
private val s3Uploader: S3Uploader,
|
||||||
private val applicationEventPublisher: ApplicationEventPublisher,
|
private val sourceInvalidation: ContentTranslationSourceInvalidationService,
|
||||||
|
transactionManager: PlatformTransactionManager,
|
||||||
@Value("\${cloud.aws.s3.bucket}")
|
@Value("\${cloud.aws.s3.bucket}")
|
||||||
private val bucket: String
|
private val bucket: String
|
||||||
) {
|
) {
|
||||||
|
private val uploadTransaction = TransactionTemplate(transactionManager).apply {
|
||||||
|
propagationBehavior = TransactionDefinition.PROPAGATION_NOT_SUPPORTED
|
||||||
|
}
|
||||||
fun getAudioContentList(status: ContentReleaseStatus, pageable: Pageable): GetAdminContentListResponse {
|
fun getAudioContentList(status: ContentReleaseStatus, pageable: Pageable): GetAdminContentListResponse {
|
||||||
val totalCount = repository.getAudioContentTotalCount(status = status)
|
val totalCount = repository.getAudioContentTotalCount(status = status)
|
||||||
val audioContentAndThemeList = repository.getAudioContentList(
|
val audioContentAndThemeList = repository.getAudioContentList(
|
||||||
@@ -99,24 +105,30 @@ class AdminContentService(
|
|||||||
fun updateAudioContent(coverImage: MultipartFile?, requestString: String) {
|
fun updateAudioContent(coverImage: MultipartFile?, requestString: String) {
|
||||||
val requestNode = objectMapper.readTree(requestString)
|
val requestNode = objectMapper.readTree(requestString)
|
||||||
val request = objectMapper.treeToValue(requestNode, UpdateAdminContentRequest::class.java)
|
val request = objectMapper.treeToValue(requestNode, UpdateAdminContentRequest::class.java)
|
||||||
val audioContent = repository.findByIdOrNull(id = request.id)
|
val audioContent = repository.findByIdOrNull(request.id) ?: throw SodaException(messageKey = "admin.content.not_found")
|
||||||
?: throw SodaException(messageKey = "admin.content.not_found")
|
|
||||||
|
|
||||||
if (coverImage != null) {
|
val uploadedCover = if (coverImage != null) {
|
||||||
val metadata = ObjectMetadata()
|
val metadata = ObjectMetadata()
|
||||||
metadata.contentLength = coverImage.size
|
metadata.contentLength = coverImage.size
|
||||||
|
|
||||||
val fileName = generateFileName(prefix = "${request.id}-cover")
|
val fileName = generateFileName(prefix = "${request.id}-cover")
|
||||||
val imagePath = s3Uploader.upload(
|
uploadTransaction.execute {
|
||||||
|
s3Uploader.upload(
|
||||||
inputStream = coverImage.inputStream,
|
inputStream = coverImage.inputStream,
|
||||||
bucket = bucket,
|
bucket = bucket,
|
||||||
filePath = "audio_content_cover/${request.id}/$fileName",
|
filePath = "audio_content_cover/${request.id}/$fileName",
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
)
|
)
|
||||||
audioContent.coverImage = imagePath
|
|
||||||
} else if (request.isDefaultCoverImage) {
|
|
||||||
audioContent.coverImage = "profile/default_profile.png"
|
|
||||||
}
|
}
|
||||||
|
} else if (request.isDefaultCoverImage) {
|
||||||
|
"profile/default_profile.png"
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceInvalidation.lock(audioContent)
|
||||||
|
val before = audioContent.toContentSourceSnapshot()
|
||||||
|
if (uploadedCover != null) audioContent.coverImage = uploadedCover
|
||||||
|
|
||||||
if (request.isActive != null) {
|
if (request.isActive != null) {
|
||||||
if (!request.isActive) {
|
if (!request.isActive) {
|
||||||
@@ -163,15 +175,7 @@ class AdminContentService(
|
|||||||
audioContent.settlementRatio = request.settlementRatio
|
audioContent.settlementRatio = request.settlementRatio
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.title != null || request.detail != null) {
|
sourceInvalidation.invalidateChanged(audioContent, before)
|
||||||
applicationEventPublisher.publishEvent(
|
|
||||||
LanguageTranslationEvent(
|
|
||||||
id = request.id,
|
|
||||||
targetType = LanguageTranslationTargetType.CONTENT,
|
|
||||||
waitTransactionCommit = true
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getContentMainTabList(): List<GetContentMainTabItem> {
|
fun getContentMainTabList(): List<GetContentMainTabItem> {
|
||||||
|
|||||||
+35
-36
@@ -8,7 +8,11 @@ import kr.co.vividnext.sodalive.i18n.translation.ResourceTranslationJobScheduler
|
|||||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationLanguages
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationLanguages
|
||||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationMemoryService
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationMemoryService
|
||||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationReadModelMaterializer
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationReadModelMaterializer
|
||||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationSourceField
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationLanguageService
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationSourceInvalidationService
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationWriteService
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.toContentSourceSnapshot
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceSnapshot
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
import org.springframework.transaction.PlatformTransactionManager
|
import org.springframework.transaction.PlatformTransactionManager
|
||||||
@@ -29,7 +33,9 @@ data class AudioContentDetailTranslationResult(
|
|||||||
class AudioContentDetailTranslationService(
|
class AudioContentDetailTranslationService(
|
||||||
private val repository: AudioContentRepository,
|
private val repository: AudioContentRepository,
|
||||||
private val translations: ContentTranslationRepository,
|
private val translations: ContentTranslationRepository,
|
||||||
private val detector: LanguageDetectionCacheService,
|
private val languageService: ContentTranslationLanguageService,
|
||||||
|
private val sourceInvalidation: ContentTranslationSourceInvalidationService,
|
||||||
|
private val writer: ContentTranslationWriteService,
|
||||||
private val memories: TranslationMemoryService,
|
private val memories: TranslationMemoryService,
|
||||||
private val materializer: TranslationReadModelMaterializer,
|
private val materializer: TranslationReadModelMaterializer,
|
||||||
private val scheduler: ResourceTranslationJobScheduler,
|
private val scheduler: ResourceTranslationJobScheduler,
|
||||||
@@ -44,34 +50,32 @@ class AudioContentDetailTranslationService(
|
|||||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||||
fun resolve(contentId: Long, targetLanguage: String): AudioContentDetailTranslationResult {
|
fun resolve(contentId: Long, targetLanguage: String): AudioContentDetailTranslationResult {
|
||||||
var source = transaction.execute { snapshot(contentId) } ?: return AudioContentDetailTranslationResult(null)
|
var source = transaction.execute { snapshot(contentId) } ?: return AudioContentDetailTranslationResult(null)
|
||||||
if (source.languageCode.isNullOrBlank()) {
|
if (source.sourceLanguage.isNullOrBlank()) {
|
||||||
val beforeDetection = source
|
|
||||||
try {
|
try {
|
||||||
val detected = detector.detectWithCache(source.query)?.detectedLanguage
|
source = languageService.detectMissing(contentId) ?: return AudioContentDetailTranslationResult(null)
|
||||||
?: return AudioContentDetailTranslationResult(source.languageCode, detectionQuery = source.query)
|
|
||||||
source = transaction.execute {
|
|
||||||
val content = entityManager.find(AudioContent::class.java, contentId, LockModeType.PESSIMISTIC_WRITE)
|
|
||||||
?: return@execute null
|
|
||||||
val current = snapshot(contentId) ?: return@execute null
|
|
||||||
if (current == beforeDetection) content.languageCode = detected
|
|
||||||
snapshot(contentId)
|
|
||||||
} ?: return AudioContentDetailTranslationResult(null)
|
|
||||||
} catch (ex: Exception) {
|
} catch (ex: Exception) {
|
||||||
log.warn("Content detail language detection failed. contentId={}, error={}", contentId, ex.message)
|
log.warn("Content detail language detection failed. contentId={}, error={}", contentId, ex.message)
|
||||||
return AudioContentDetailTranslationResult(source.languageCode, detectionQuery = source.query)
|
return AudioContentDetailTranslationResult(source.sourceLanguage, detectionQuery = source.detectionQuery)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val language = source.languageCode
|
|
||||||
if (language.isNullOrBlank()) return AudioContentDetailTranslationResult(language, detectionQuery = source.query)
|
|
||||||
val target = targetLanguage.lowercase()
|
val target = targetLanguage.lowercase()
|
||||||
|
val existing = transaction.execute {
|
||||||
|
val content = writer.lockContent(contentId) ?: return@execute null
|
||||||
|
sourceInvalidation.invalidateStale(content)
|
||||||
|
source = content.toContentSourceSnapshot()
|
||||||
|
translations.findByContentIdAndLocale(contentId, target)?.let {
|
||||||
|
entityManager.refresh(it, LockModeType.PESSIMISTIC_WRITE)
|
||||||
|
it.renderedPayload
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val language = source.sourceLanguage
|
||||||
|
if (language.isNullOrBlank()) return AudioContentDetailTranslationResult(language, detectionQuery = source.detectionQuery)
|
||||||
if (
|
if (
|
||||||
!TranslationLanguages.isSupported(language) || !TranslationLanguages.isSupported(target) ||
|
!TranslationLanguages.isSupported(language) || !TranslationLanguages.isSupported(target) ||
|
||||||
language.lowercase() == target
|
language.lowercase() == target
|
||||||
) {
|
) {
|
||||||
return AudioContentDetailTranslationResult(language)
|
return AudioContentDetailTranslationResult(language)
|
||||||
}
|
}
|
||||||
|
|
||||||
val existing = transaction.execute { translations.findByContentIdAndLocale(contentId, target)?.renderedPayload }
|
|
||||||
if (existing != null) {
|
if (existing != null) {
|
||||||
maintainRetries(contentId, target, scheduleMissing = false)
|
maintainRetries(contentId, target, scheduleMissing = false)
|
||||||
return result(language, existing)
|
return result(language, existing)
|
||||||
@@ -79,10 +83,19 @@ class AudioContentDetailTranslationService(
|
|||||||
|
|
||||||
return try {
|
return try {
|
||||||
source.fields.forEach { field ->
|
source.fields.forEach { field ->
|
||||||
memories.ensureMemory(LanguageTranslationTargetType.CONTENT, field.fieldKey, field.sourceText, language, target)
|
val approved = transaction.execute { writer.hasCurrentApproval(contentId, target, field.fieldKey) } == true
|
||||||
|
if (!approved) {
|
||||||
|
memories.ensureMemory(
|
||||||
|
LanguageTranslationTargetType.CONTENT,
|
||||||
|
field.fieldKey,
|
||||||
|
field.sourceText,
|
||||||
|
language,
|
||||||
|
target
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
val payload = transaction.execute {
|
val payload = transaction.execute {
|
||||||
entityManager.find(AudioContent::class.java, contentId, LockModeType.PESSIMISTIC_WRITE)
|
writer.lockContent(contentId)
|
||||||
check(snapshot(contentId) == source) { "content translation source changed" }
|
check(snapshot(contentId) == source) { "content translation source changed" }
|
||||||
check(materializer.materialize(LanguageTranslationTargetType.CONTENT, contentId, target)) {
|
check(materializer.materialize(LanguageTranslationTargetType.CONTENT, contentId, target)) {
|
||||||
"content translation is incomplete"
|
"content translation is incomplete"
|
||||||
@@ -125,20 +138,6 @@ class AudioContentDetailTranslationService(
|
|||||||
TranslatedContent(payload.title, payload.detail, payload.tags)
|
TranslatedContent(payload.title, payload.detail, payload.tags)
|
||||||
)
|
)
|
||||||
|
|
||||||
private fun snapshot(contentId: Long): DetailSource? {
|
private fun snapshot(contentId: Long): ContentSourceSnapshot? =
|
||||||
val content = repository.findById(contentId).orElse(null) ?: return null
|
repository.findById(contentId).orElse(null)?.toContentSourceSnapshot()
|
||||||
val tags = content.audioContentHashTags.filter { it.isActive }.mapNotNull { it.hashTag?.tag }
|
|
||||||
return DetailSource(
|
|
||||||
content.languageCode,
|
|
||||||
listOf(
|
|
||||||
TranslationSourceField("title", content.title),
|
|
||||||
TranslationSourceField("detail", content.detail),
|
|
||||||
TranslationSourceField("tags", tags.joinToString(","))
|
|
||||||
),
|
|
||||||
listOf(content.title.trim(), content.detail.trim(), tags.joinToString(" ").trim())
|
|
||||||
.filter { it.isNotBlank() }.joinToString(" ")
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private data class DetailSource(val languageCode: String?, val fields: List<TranslationSourceField>, val query: String)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,6 +38,8 @@ import kr.co.vividnext.sodalive.member.MemberRepository
|
|||||||
import kr.co.vividnext.sodalive.member.block.BlockMemberRepository
|
import kr.co.vividnext.sodalive.member.block.BlockMemberRepository
|
||||||
import kr.co.vividnext.sodalive.member.contentpreference.MemberContentPreferenceService
|
import kr.co.vividnext.sodalive.member.contentpreference.MemberContentPreferenceService
|
||||||
import kr.co.vividnext.sodalive.utils.generateFileName
|
import kr.co.vividnext.sodalive.utils.generateFileName
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationSourceInvalidationService
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.toContentSourceSnapshot
|
||||||
import kr.co.vividnext.sodalive.v2.creatorname.application.CreatorLocalizedNameService
|
import kr.co.vividnext.sodalive.v2.creatorname.application.CreatorLocalizedNameService
|
||||||
import kr.co.vividnext.sodalive.v2.home.following.application.HomeFollowingNewsPublishService
|
import kr.co.vividnext.sodalive.v2.home.following.application.HomeFollowingNewsPublishService
|
||||||
import kr.co.vividnext.sodalive.v2.recommendation.application.CreatorContentViewHistoryService
|
import kr.co.vividnext.sodalive.v2.recommendation.application.CreatorContentViewHistoryService
|
||||||
@@ -97,6 +99,7 @@ class AudioContentService(
|
|||||||
private val homeFollowingNewsPublishService: HomeFollowingNewsPublishService,
|
private val homeFollowingNewsPublishService: HomeFollowingNewsPublishService,
|
||||||
private val memberContentPreferenceService: MemberContentPreferenceService,
|
private val memberContentPreferenceService: MemberContentPreferenceService,
|
||||||
private val subtitleJobService: SubtitleJobService,
|
private val subtitleJobService: SubtitleJobService,
|
||||||
|
private val sourceInvalidation: ContentTranslationSourceInvalidationService,
|
||||||
transactionManager: PlatformTransactionManager,
|
transactionManager: PlatformTransactionManager,
|
||||||
|
|
||||||
@Value("\${cloud.aws.s3.content-bucket}")
|
@Value("\${cloud.aws.s3.content-bucket}")
|
||||||
@@ -112,6 +115,9 @@ class AudioContentService(
|
|||||||
private val registrationTransaction = TransactionTemplate(transactionManager).apply {
|
private val registrationTransaction = TransactionTemplate(transactionManager).apply {
|
||||||
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW
|
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW
|
||||||
}
|
}
|
||||||
|
private val uploadTransaction = TransactionTemplate(transactionManager).apply {
|
||||||
|
propagationBehavior = TransactionDefinition.PROPAGATION_NOT_SUPPORTED
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
fun audioContentLike(request: PutAudioContentLikeRequest, member: Member): PutAudioContentLikeResponse {
|
fun audioContentLike(request: PutAudioContentLikeRequest, member: Member): PutAudioContentLikeResponse {
|
||||||
@@ -145,13 +151,7 @@ class AudioContentService(
|
|||||||
val audioContent = repository.findByIdAndCreatorId(request.contentId, member.id!!)
|
val audioContent = repository.findByIdAndCreatorId(request.contentId, member.id!!)
|
||||||
?: throw SodaException(messageKey = "content.error.invalid_content_retry")
|
?: throw SodaException(messageKey = "content.error.invalid_content_retry")
|
||||||
|
|
||||||
if (request.title != null) audioContent.title = request.title
|
val uploadedCover = if (coverImage != null) {
|
||||||
if (request.detail != null) audioContent.detail = request.detail
|
|
||||||
if (request.isPointAvailable != null) audioContent.isPointAvailable = request.isPointAvailable
|
|
||||||
audioContent.isCommentAvailable = request.isCommentAvailable
|
|
||||||
audioContent.isAdult = request.isAdult
|
|
||||||
|
|
||||||
if (coverImage != null) {
|
|
||||||
val metadata = ObjectMetadata()
|
val metadata = ObjectMetadata()
|
||||||
metadata.contentLength = coverImage.size
|
metadata.contentLength = coverImage.size
|
||||||
|
|
||||||
@@ -159,15 +159,27 @@ class AudioContentService(
|
|||||||
val coverImageFileName = generateFileName(prefix = "${audioContent.id}-cover")
|
val coverImageFileName = generateFileName(prefix = "${audioContent.id}-cover")
|
||||||
|
|
||||||
// 커버 이미지 업로드
|
// 커버 이미지 업로드
|
||||||
val coverImagePath = s3Uploader.upload(
|
uploadTransaction.execute {
|
||||||
|
s3Uploader.upload(
|
||||||
inputStream = coverImage.inputStream,
|
inputStream = coverImage.inputStream,
|
||||||
bucket = coverImageBucket,
|
bucket = coverImageBucket,
|
||||||
filePath = "audio_content_cover/${audioContent.id}/$coverImageFileName",
|
filePath = "audio_content_cover/${audioContent.id}/$coverImageFileName",
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
)
|
)
|
||||||
|
|
||||||
audioContent.coverImage = coverImagePath
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceInvalidation.lock(audioContent)
|
||||||
|
if (audioContent.member?.id != member.id) throw SodaException(messageKey = "content.error.invalid_content_retry")
|
||||||
|
val before = audioContent.toContentSourceSnapshot()
|
||||||
|
if (request.title != null) audioContent.title = request.title
|
||||||
|
if (request.detail != null) audioContent.detail = request.detail
|
||||||
|
if (request.isPointAvailable != null) audioContent.isPointAvailable = request.isPointAvailable
|
||||||
|
audioContent.isCommentAvailable = request.isCommentAvailable
|
||||||
|
audioContent.isAdult = request.isAdult
|
||||||
|
if (uploadedCover != null) audioContent.coverImage = uploadedCover
|
||||||
|
|
||||||
if (!request.tags.isNullOrBlank()) {
|
if (!request.tags.isNullOrBlank()) {
|
||||||
val normalizedTags = request.tags
|
val normalizedTags = request.tags
|
||||||
@@ -201,22 +213,18 @@ class AudioContentService(
|
|||||||
audioContentHashTag
|
audioContentHashTag
|
||||||
}
|
}
|
||||||
|
|
||||||
audioContent.audioContentHashTags.addAll(newAudioContentHashTagList)
|
val added = newAudioContentHashTagList.filter { it !in audioContent.audioContentHashTags }
|
||||||
|
audioContent.audioContentHashTags.addAll(added)
|
||||||
}
|
}
|
||||||
|
sourceInvalidation.invalidateChanged(audioContent, before)
|
||||||
applicationEventPublisher.publishEvent(
|
|
||||||
LanguageTranslationEvent(
|
|
||||||
id = request.contentId,
|
|
||||||
targetType = LanguageTranslationTargetType.CONTENT,
|
|
||||||
waitTransactionCommit = true
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
fun deleteAudioContent(audioContentId: Long, member: Member) {
|
fun deleteAudioContent(audioContentId: Long, member: Member) {
|
||||||
val audioContent = repository.findByIdAndCreatorId(audioContentId, member.id!!)
|
val audioContent = repository.findByIdAndCreatorId(audioContentId, member.id!!)
|
||||||
?: throw SodaException(messageKey = "content.error.invalid_content_retry")
|
?: throw SodaException(messageKey = "content.error.invalid_content_retry")
|
||||||
|
sourceInvalidation.lock(audioContent)
|
||||||
|
if (audioContent.member?.id != member.id) throw SodaException(messageKey = "content.error.invalid_content_retry")
|
||||||
|
|
||||||
val pinContent = pinContentRepository.findByContentIdAndMemberId(
|
val pinContent = pinContentRepository.findByContentIdAndMemberId(
|
||||||
contentId = audioContentId,
|
contentId = audioContentId,
|
||||||
@@ -412,13 +420,8 @@ class AudioContentService(
|
|||||||
registrationTransaction.executeWithoutResult {
|
registrationTransaction.executeWithoutResult {
|
||||||
// 언어 코드가 지정되지 않은 경우, 파파고 언어 감지 API를 통해 비동기로 언어를 식별한다.
|
// 언어 코드가 지정되지 않은 경우, 파파고 언어 감지 API를 통해 비동기로 언어를 식별한다.
|
||||||
if (audioContent.languageCode.isNullOrBlank()) {
|
if (audioContent.languageCode.isNullOrBlank()) {
|
||||||
val papagoQuery = listOf(
|
val papagoQuery = repository.findByIdOrNull(requireNotNull(audioContent.id))
|
||||||
request.title.trim(),
|
?.toContentSourceSnapshot()?.detectionQuery ?: return@executeWithoutResult
|
||||||
request.detail.trim(),
|
|
||||||
request.tags.trim()
|
|
||||||
)
|
|
||||||
.filter { it.isNotBlank() }
|
|
||||||
.joinToString(" ")
|
|
||||||
|
|
||||||
applicationEventPublisher.publishEvent(
|
applicationEventPublisher.publishEvent(
|
||||||
LanguageDetectEvent(
|
LanguageDetectEvent(
|
||||||
@@ -505,6 +508,7 @@ class AudioContentService(
|
|||||||
|
|
||||||
val audioContent = repository.findByIdOrNull(contentId)
|
val audioContent = repository.findByIdOrNull(contentId)
|
||||||
?: throw SodaException(messageKey = "common.error.invalid_request")
|
?: throw SodaException(messageKey = "common.error.invalid_request")
|
||||||
|
sourceInvalidation.lock(audioContent)
|
||||||
|
|
||||||
audioContent.content = content
|
audioContent.content = content
|
||||||
audioContent.duration = duration
|
audioContent.duration = duration
|
||||||
@@ -556,7 +560,14 @@ class AudioContentService(
|
|||||||
fun releaseContent() {
|
fun releaseContent() {
|
||||||
val notReleasedAudioContent = repository.getNotReleaseContent()
|
val notReleasedAudioContent = repository.getNotReleaseContent()
|
||||||
|
|
||||||
for (audioContent in notReleasedAudioContent) {
|
for (audioContent in notReleasedAudioContent.sortedBy { it.id }) {
|
||||||
|
sourceInvalidation.lock(audioContent)
|
||||||
|
if (
|
||||||
|
audioContent.isActive || audioContent.releaseDate == null || audioContent.releaseDate!! > LocalDateTime.now() ||
|
||||||
|
audioContent.duration == null
|
||||||
|
) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
audioContent.isActive = true
|
audioContent.isActive = true
|
||||||
|
|
||||||
applicationEventPublisher.publishEvent(
|
applicationEventPublisher.publishEvent(
|
||||||
|
|||||||
@@ -11,17 +11,21 @@ import kr.co.vividnext.sodalive.explorer.profile.creatorCommunity.CreatorCommuni
|
|||||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType
|
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType
|
||||||
import kr.co.vividnext.sodalive.i18n.translation.ResourceTranslationJobScheduler
|
import kr.co.vividnext.sodalive.i18n.translation.ResourceTranslationJobScheduler
|
||||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationLanguages.getTranslatableLanguageCodes
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationLanguages.getTranslatableLanguageCodes
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationLanguageService
|
||||||
import kr.co.vividnext.sodalive.v2.creator.channel.community.translation.application.CreatorCommunityTranslationService
|
import kr.co.vividnext.sodalive.v2.creator.channel.community.translation.application.CreatorCommunityTranslationService
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import org.springframework.data.repository.findByIdOrNull
|
import org.springframework.data.repository.findByIdOrNull
|
||||||
import org.springframework.scheduling.annotation.Async
|
import org.springframework.scheduling.annotation.Async
|
||||||
import org.springframework.stereotype.Component
|
import org.springframework.stereotype.Component
|
||||||
|
import org.springframework.transaction.PlatformTransactionManager
|
||||||
|
import org.springframework.transaction.TransactionDefinition
|
||||||
import org.springframework.transaction.annotation.Propagation
|
import org.springframework.transaction.annotation.Propagation
|
||||||
import org.springframework.transaction.annotation.Transactional
|
import org.springframework.transaction.annotation.Transactional
|
||||||
import org.springframework.transaction.event.TransactionPhase
|
import org.springframework.transaction.event.TransactionPhase
|
||||||
import org.springframework.transaction.event.TransactionalEventListener
|
import org.springframework.transaction.event.TransactionalEventListener
|
||||||
import org.springframework.transaction.support.TransactionSynchronization
|
import org.springframework.transaction.support.TransactionSynchronization
|
||||||
import org.springframework.transaction.support.TransactionSynchronizationManager
|
import org.springframework.transaction.support.TransactionSynchronizationManager
|
||||||
|
import org.springframework.transaction.support.TransactionTemplate
|
||||||
import javax.persistence.EntityManager
|
import javax.persistence.EntityManager
|
||||||
import javax.persistence.LockModeType
|
import javax.persistence.LockModeType
|
||||||
|
|
||||||
@@ -51,7 +55,6 @@ class LanguageDetectEvent(
|
|||||||
|
|
||||||
@Component
|
@Component
|
||||||
class LanguageDetectListener(
|
class LanguageDetectListener(
|
||||||
private val audioContentRepository: AudioContentRepository,
|
|
||||||
private val audioContentCommentRepository: AudioContentCommentRepository,
|
private val audioContentCommentRepository: AudioContentCommentRepository,
|
||||||
private val chatCharacterRepository: ChatCharacterRepository,
|
private val chatCharacterRepository: ChatCharacterRepository,
|
||||||
private val characterCommentRepository: CharacterCommentRepository,
|
private val characterCommentRepository: CharacterCommentRepository,
|
||||||
@@ -63,22 +66,32 @@ class LanguageDetectListener(
|
|||||||
private val languageDetectionCacheService: LanguageDetectionCacheService,
|
private val languageDetectionCacheService: LanguageDetectionCacheService,
|
||||||
private val creatorCommunityTranslationService: CreatorCommunityTranslationService,
|
private val creatorCommunityTranslationService: CreatorCommunityTranslationService,
|
||||||
private val entityManager: EntityManager,
|
private val entityManager: EntityManager,
|
||||||
private val resourceTranslationJobScheduler: ResourceTranslationJobScheduler
|
private val resourceTranslationJobScheduler: ResourceTranslationJobScheduler,
|
||||||
|
private val contentLanguageService: ContentTranslationLanguageService,
|
||||||
|
transactionManager: PlatformTransactionManager
|
||||||
) {
|
) {
|
||||||
|
|
||||||
private val log = LoggerFactory.getLogger(LanguageDetectListener::class.java)
|
private val log = LoggerFactory.getLogger(LanguageDetectListener::class.java)
|
||||||
|
private val transaction = TransactionTemplate(transactionManager).apply {
|
||||||
|
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW
|
||||||
|
}
|
||||||
|
|
||||||
@Async
|
@Async
|
||||||
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
|
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
|
||||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||||
fun detectLanguage(event: LanguageDetectEvent) {
|
fun detectLanguage(event: LanguageDetectEvent) {
|
||||||
if (event.query.isBlank()) {
|
if (event.query.isBlank()) {
|
||||||
log.debug("[LanguageDetect] query is blank. Skip language detection. targetId={}", event.id)
|
log.debug("[LanguageDetect] query is blank. Skip language detection. targetId={}", event.id)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (event.targetType == LanguageDetectTargetType.CONTENT) {
|
||||||
|
contentLanguageService.detectMissing(event.id, event.query)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
transaction.executeWithoutResult {
|
||||||
when (event.targetType) {
|
when (event.targetType) {
|
||||||
LanguageDetectTargetType.CONTENT -> handleContentLanguageDetect(event)
|
LanguageDetectTargetType.CONTENT -> error("CONTENT detection uses the source guard")
|
||||||
LanguageDetectTargetType.COMMENT -> handleCommentLanguageDetect(event)
|
LanguageDetectTargetType.COMMENT -> handleCommentLanguageDetect(event)
|
||||||
LanguageDetectTargetType.CHARACTER -> handleCharacterLanguageDetect(event)
|
LanguageDetectTargetType.CHARACTER -> handleCharacterLanguageDetect(event)
|
||||||
LanguageDetectTargetType.CHARACTER_COMMENT -> handleCharacterCommentLanguageDetect(event)
|
LanguageDetectTargetType.CHARACTER_COMMENT -> handleCharacterCommentLanguageDetect(event)
|
||||||
@@ -89,6 +102,7 @@ class LanguageDetectListener(
|
|||||||
LanguageDetectTargetType.CREATOR_COMMUNITY -> handleCreatorCommunityLanguageDetect(event)
|
LanguageDetectTargetType.CREATOR_COMMUNITY -> handleCreatorCommunityLanguageDetect(event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun handleCharacterLanguageDetect(event: LanguageDetectEvent) {
|
private fun handleCharacterLanguageDetect(event: LanguageDetectEvent) {
|
||||||
val characterId = event.id
|
val characterId = event.id
|
||||||
@@ -126,44 +140,6 @@ class LanguageDetectListener(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleContentLanguageDetect(event: LanguageDetectEvent) {
|
|
||||||
val contentId = event.id
|
|
||||||
|
|
||||||
val audioContent = audioContentRepository.findById(contentId).orElse(null)
|
|
||||||
if (audioContent == null) {
|
|
||||||
log.warn("[LanguageDetect] AudioContent not found. contentId={}", contentId)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 이미 언어 코드가 설정된 경우 호출하지 않음
|
|
||||||
if (!audioContent.languageCode.isNullOrBlank()) {
|
|
||||||
log.debug(
|
|
||||||
"[LanguageDetect] languageCode already set. Skip language detection. contentId={}, languageCode={}",
|
|
||||||
contentId,
|
|
||||||
audioContent.languageCode
|
|
||||||
)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
val langCode = detectLanguageCode(event) ?: return
|
|
||||||
|
|
||||||
audioContent.languageCode = langCode
|
|
||||||
|
|
||||||
// REQUIRES_NEW 트랜잭션 내에서 변경 내용을 저장한다.
|
|
||||||
audioContentRepository.save(audioContent)
|
|
||||||
|
|
||||||
resourceTranslationJobScheduler.scheduleResourceTranslations(
|
|
||||||
resourceType = LanguageTranslationTargetType.CONTENT,
|
|
||||||
resourceId = contentId
|
|
||||||
)
|
|
||||||
|
|
||||||
log.info(
|
|
||||||
"[LanguageDetect] languageCode updated. contentId={}, langCode={}",
|
|
||||||
contentId,
|
|
||||||
langCode
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun handleCommentLanguageDetect(event: LanguageDetectEvent) {
|
private fun handleCommentLanguageDetect(event: LanguageDetectEvent) {
|
||||||
val commentId = event.id
|
val commentId = event.id
|
||||||
|
|
||||||
|
|||||||
@@ -4,29 +4,48 @@ import kr.co.vividnext.sodalive.i18n.translation.DetectedLanguage
|
|||||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageDetectionProvider
|
import kr.co.vividnext.sodalive.i18n.translation.LanguageDetectionProvider
|
||||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageProviderProperties
|
import kr.co.vividnext.sodalive.i18n.translation.LanguageProviderProperties
|
||||||
import kr.co.vividnext.sodalive.i18n.translation.SourceTextNormalizer
|
import kr.co.vividnext.sodalive.i18n.translation.SourceTextNormalizer
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationCallControl
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.translationCallStop
|
||||||
import org.springframework.stereotype.Service
|
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.annotation.Transactional
|
||||||
|
import org.springframework.transaction.support.TransactionTemplate
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
class LanguageDetectionCacheService(
|
class LanguageDetectionCacheService(
|
||||||
private val languageDetectionResultRepository: LanguageDetectionResultRepository,
|
private val languageDetectionResultRepository: LanguageDetectionResultRepository,
|
||||||
providers: List<LanguageDetectionProvider>,
|
providers: List<LanguageDetectionProvider>,
|
||||||
properties: LanguageProviderProperties
|
properties: LanguageProviderProperties,
|
||||||
|
transactionManager: PlatformTransactionManager
|
||||||
) {
|
) {
|
||||||
private val providers = properties.orderedProviders(properties.detectionOrder, providers) { it.providerId }
|
private val providers = properties.orderedProviders(properties.detectionOrder, providers) { it.providerId }
|
||||||
|
private val transaction = TransactionTemplate(transactionManager).apply {
|
||||||
|
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||||
fun detectWithCache(query: String): DetectedLanguage? {
|
fun detectWithCache(query: String): DetectedLanguage? = detect(query, null)
|
||||||
|
|
||||||
|
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||||
|
fun detectWithCache(query: String, authorizeDispatch: () -> Boolean): DetectedLanguage? {
|
||||||
|
return detect(query, TranslationCallControl(authorizeDispatch))
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun detect(query: String, control: TranslationCallControl?): DetectedLanguage? {
|
||||||
val normalizedQuery = SourceTextNormalizer.normalize(query)
|
val normalizedQuery = SourceTextNormalizer.normalize(query)
|
||||||
if (normalizedQuery.isBlank()) return null
|
if (normalizedQuery.isBlank()) return null
|
||||||
|
|
||||||
val sourceHash = SourceTextNormalizer.hash(normalizedQuery)
|
val sourceHash = SourceTextNormalizer.hash(normalizedQuery)
|
||||||
providers.forEach { provider ->
|
providers.forEach { provider ->
|
||||||
val cached = languageDetectionResultRepository.findBySourceHashAndProviderAndNormalizationVersion(
|
val cached = transaction.execute {
|
||||||
|
languageDetectionResultRepository.findBySourceHashAndProviderAndNormalizationVersion(
|
||||||
sourceHash = sourceHash,
|
sourceHash = sourceHash,
|
||||||
provider = provider.providerId.code,
|
provider = provider.providerId.code,
|
||||||
normalizationVersion = SourceTextNormalizer.NORMALIZATION_VERSION
|
normalizationVersion = SourceTextNormalizer.NORMALIZATION_VERSION
|
||||||
)
|
)
|
||||||
|
}
|
||||||
if (cached != null && cached.detectedLanguage.isNotBlank()) {
|
if (cached != null && cached.detectedLanguage.isNotBlank()) {
|
||||||
return DetectedLanguage(cached.detectedLanguage, provider.providerId)
|
return DetectedLanguage(cached.detectedLanguage, provider.providerId)
|
||||||
}
|
}
|
||||||
@@ -34,10 +53,13 @@ class LanguageDetectionCacheService(
|
|||||||
|
|
||||||
providers.forEach { provider ->
|
providers.forEach { provider ->
|
||||||
val detectedLanguage = try {
|
val detectedLanguage = try {
|
||||||
provider.detect(query)
|
control?.beforeCall()
|
||||||
} catch (_: Exception) {
|
if (control == null) provider.detect(query) else provider.detect(query, control)
|
||||||
|
} catch (error: Exception) {
|
||||||
|
if (error.translationCallStop() != null) return null
|
||||||
null
|
null
|
||||||
}?.takeIf { it.isNotBlank() }?.lowercase() ?: return@forEach
|
}?.takeIf { it.isNotBlank() }?.lowercase() ?: return@forEach
|
||||||
|
val winner = transaction.execute {
|
||||||
languageDetectionResultRepository.insertIfAbsent(
|
languageDetectionResultRepository.insertIfAbsent(
|
||||||
sourceHash = sourceHash,
|
sourceHash = sourceHash,
|
||||||
sourceTextSample = normalizedQuery.take(MAX_SAMPLE_LENGTH),
|
sourceTextSample = normalizedQuery.take(MAX_SAMPLE_LENGTH),
|
||||||
@@ -46,14 +68,15 @@ class LanguageDetectionCacheService(
|
|||||||
confidence = null,
|
confidence = null,
|
||||||
normalizationVersion = SourceTextNormalizer.NORMALIZATION_VERSION
|
normalizationVersion = SourceTextNormalizer.NORMALIZATION_VERSION
|
||||||
)
|
)
|
||||||
val winner = requireNotNull(
|
requireNotNull(
|
||||||
languageDetectionResultRepository.findDetectedLanguageForUpdate(
|
languageDetectionResultRepository.findDetectedLanguageForUpdate(
|
||||||
sourceHash = sourceHash,
|
sourceHash = sourceHash,
|
||||||
provider = provider.providerId.code,
|
provider = provider.providerId.code,
|
||||||
normalizationVersion = SourceTextNormalizer.NORMALIZATION_VERSION
|
normalizationVersion = SourceTextNormalizer.NORMALIZATION_VERSION
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return DetectedLanguage(winner, provider.providerId)
|
}
|
||||||
|
return DetectedLanguage(requireNotNull(winner), provider.providerId)
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|||||||
+3
@@ -9,6 +9,9 @@ import javax.persistence.LockModeType
|
|||||||
interface ContentTranslationRepository : JpaRepository<ContentTranslation, Long> {
|
interface ContentTranslationRepository : JpaRepository<ContentTranslation, Long> {
|
||||||
fun findByContentIdAndLocale(contentId: Long, locale: String): ContentTranslation?
|
fun findByContentIdAndLocale(contentId: Long, locale: String): ContentTranslation?
|
||||||
|
|
||||||
|
@Lock(LockModeType.PESSIMISTIC_WRITE)
|
||||||
|
fun findByContentId(contentId: Long): List<ContentTranslation>
|
||||||
|
|
||||||
@Lock(LockModeType.PESSIMISTIC_WRITE)
|
@Lock(LockModeType.PESSIMISTIC_WRITE)
|
||||||
@Query("select t from ContentTranslation t where t.contentId = :contentId and t.locale = :locale")
|
@Query("select t from ContentTranslation t where t.contentId = :contentId and t.locale = :locale")
|
||||||
fun findByContentIdAndLocaleForUpdate(
|
fun findByContentIdAndLocaleForUpdate(
|
||||||
|
|||||||
+24
-17
@@ -10,15 +10,17 @@ import kr.co.vividnext.sodalive.content.ContentPriceChangeLogRepository
|
|||||||
import kr.co.vividnext.sodalive.content.hashtag.AudioContentHashTag
|
import kr.co.vividnext.sodalive.content.hashtag.AudioContentHashTag
|
||||||
import kr.co.vividnext.sodalive.content.hashtag.HashTag
|
import kr.co.vividnext.sodalive.content.hashtag.HashTag
|
||||||
import kr.co.vividnext.sodalive.content.hashtag.HashTagRepository
|
import kr.co.vividnext.sodalive.content.hashtag.HashTagRepository
|
||||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationEvent
|
|
||||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType
|
|
||||||
import kr.co.vividnext.sodalive.member.Member
|
import kr.co.vividnext.sodalive.member.Member
|
||||||
import kr.co.vividnext.sodalive.utils.generateFileName
|
import kr.co.vividnext.sodalive.utils.generateFileName
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationSourceInvalidationService
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.toContentSourceSnapshot
|
||||||
import org.springframework.beans.factory.annotation.Value
|
import org.springframework.beans.factory.annotation.Value
|
||||||
import org.springframework.context.ApplicationEventPublisher
|
|
||||||
import org.springframework.data.domain.Pageable
|
import org.springframework.data.domain.Pageable
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
|
import org.springframework.transaction.PlatformTransactionManager
|
||||||
|
import org.springframework.transaction.TransactionDefinition
|
||||||
import org.springframework.transaction.annotation.Transactional
|
import org.springframework.transaction.annotation.Transactional
|
||||||
|
import org.springframework.transaction.support.TransactionTemplate
|
||||||
import org.springframework.web.multipart.MultipartFile
|
import org.springframework.web.multipart.MultipartFile
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@@ -30,7 +32,8 @@ class CreatorAdminContentService(
|
|||||||
private val objectMapper: ObjectMapper,
|
private val objectMapper: ObjectMapper,
|
||||||
private val s3Uploader: S3Uploader,
|
private val s3Uploader: S3Uploader,
|
||||||
|
|
||||||
private val applicationEventPublisher: ApplicationEventPublisher,
|
private val sourceInvalidation: ContentTranslationSourceInvalidationService,
|
||||||
|
transactionManager: PlatformTransactionManager,
|
||||||
|
|
||||||
@Value("\${cloud.aws.s3.bucket}")
|
@Value("\${cloud.aws.s3.bucket}")
|
||||||
private val bucket: String,
|
private val bucket: String,
|
||||||
@@ -38,6 +41,9 @@ class CreatorAdminContentService(
|
|||||||
@Value("\${cloud.aws.cloud-front.host}")
|
@Value("\${cloud.aws.cloud-front.host}")
|
||||||
private val coverImageHost: String
|
private val coverImageHost: String
|
||||||
) {
|
) {
|
||||||
|
private val uploadTransaction = TransactionTemplate(transactionManager).apply {
|
||||||
|
propagationBehavior = TransactionDefinition.PROPAGATION_NOT_SUPPORTED
|
||||||
|
}
|
||||||
fun getAudioContentList(pageable: Pageable, member: Member): GetCreatorAdminContentListResponse {
|
fun getAudioContentList(pageable: Pageable, member: Member): GetCreatorAdminContentListResponse {
|
||||||
val totalCount = repository.getAudioContentTotalCount(memberId = member.id!!)
|
val totalCount = repository.getAudioContentTotalCount(memberId = member.id!!)
|
||||||
val audioContentAndThemeList = repository.getAudioContentList(
|
val audioContentAndThemeList = repository.getAudioContentList(
|
||||||
@@ -115,19 +121,27 @@ class CreatorAdminContentService(
|
|||||||
val audioContent = repository.getAudioContent(memberId = member.id!!, audioContentId = request.id)
|
val audioContent = repository.getAudioContent(memberId = member.id!!, audioContentId = request.id)
|
||||||
?: throw SodaException(messageKey = "creator.admin.content.invalid_content")
|
?: throw SodaException(messageKey = "creator.admin.content.invalid_content")
|
||||||
|
|
||||||
if (coverImage != null) {
|
val uploadedCover = if (coverImage != null) {
|
||||||
val metadata = ObjectMetadata()
|
val metadata = ObjectMetadata()
|
||||||
metadata.contentLength = coverImage.size
|
metadata.contentLength = coverImage.size
|
||||||
|
|
||||||
val fileName = generateFileName()
|
val fileName = generateFileName()
|
||||||
val imagePath = s3Uploader.upload(
|
uploadTransaction.execute {
|
||||||
|
s3Uploader.upload(
|
||||||
inputStream = coverImage.inputStream,
|
inputStream = coverImage.inputStream,
|
||||||
bucket = bucket,
|
bucket = bucket,
|
||||||
filePath = "audio_content_cover/${request.id}/$fileName",
|
filePath = "audio_content_cover/${request.id}/$fileName",
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
)
|
)
|
||||||
audioContent.coverImage = imagePath
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceInvalidation.lock(audioContent)
|
||||||
|
if (audioContent.member?.id != member.id) throw SodaException(messageKey = "creator.admin.content.invalid_content")
|
||||||
|
val before = audioContent.toContentSourceSnapshot()
|
||||||
|
if (uploadedCover != null) audioContent.coverImage = uploadedCover
|
||||||
|
|
||||||
if (request.isActive != null) {
|
if (request.isActive != null) {
|
||||||
if (!request.isActive) {
|
if (!request.isActive) {
|
||||||
@@ -198,17 +212,10 @@ class CreatorAdminContentService(
|
|||||||
audioContentHashTag
|
audioContentHashTag
|
||||||
}
|
}
|
||||||
|
|
||||||
audioContent.audioContentHashTags.addAll(newAudioContentHashTagList)
|
val added = newAudioContentHashTagList.filter { it !in audioContent.audioContentHashTags }
|
||||||
|
audioContent.audioContentHashTags.addAll(added)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.title != null || request.detail != null || !request.tags.isNullOrBlank()) {
|
sourceInvalidation.invalidateChanged(audioContent, before)
|
||||||
applicationEventPublisher.publishEvent(
|
|
||||||
LanguageTranslationEvent(
|
|
||||||
id = request.id,
|
|
||||||
targetType = LanguageTranslationTargetType.CONTENT,
|
|
||||||
waitTransactionCommit = true
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+7
@@ -24,6 +24,8 @@ import org.springframework.data.repository.findByIdOrNull
|
|||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
import org.springframework.transaction.annotation.Transactional
|
import org.springframework.transaction.annotation.Transactional
|
||||||
import org.springframework.web.multipart.MultipartFile
|
import org.springframework.web.multipart.MultipartFile
|
||||||
|
import javax.persistence.EntityManager
|
||||||
|
import javax.persistence.LockModeType
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
@@ -32,6 +34,7 @@ class CreatorAdminContentSeriesService(
|
|||||||
private val genreRepository: CreatorAdminContentSeriesGenreRepository,
|
private val genreRepository: CreatorAdminContentSeriesGenreRepository,
|
||||||
private val hashTagRepository: HashTagRepository,
|
private val hashTagRepository: HashTagRepository,
|
||||||
private val audioContentRepository: AudioContentRepository,
|
private val audioContentRepository: AudioContentRepository,
|
||||||
|
private val entityManager: EntityManager,
|
||||||
|
|
||||||
private val s3Uploader: S3Uploader,
|
private val s3Uploader: S3Uploader,
|
||||||
private val objectMapper: ObjectMapper,
|
private val objectMapper: ObjectMapper,
|
||||||
@@ -275,6 +278,8 @@ class CreatorAdminContentSeriesService(
|
|||||||
fun addingContentToTheSeries(request: AddingContentToTheSeriesRequest, memberId: Long) {
|
fun addingContentToTheSeries(request: AddingContentToTheSeriesRequest, memberId: Long) {
|
||||||
val series = repository.findByIdAndCreatorId(id = request.seriesId, creatorId = memberId)
|
val series = repository.findByIdAndCreatorId(id = request.seriesId, creatorId = memberId)
|
||||||
?: throw SodaException(messageKey = "creator.admin.series.invalid_access")
|
?: throw SodaException(messageKey = "creator.admin.series.invalid_access")
|
||||||
|
entityManager.refresh(series, LockModeType.PESSIMISTIC_WRITE)
|
||||||
|
if (series.member?.id != memberId) throw SodaException(messageKey = "creator.admin.series.invalid_access")
|
||||||
|
|
||||||
val seriesContentList = mutableListOf<SeriesContent>()
|
val seriesContentList = mutableListOf<SeriesContent>()
|
||||||
|
|
||||||
@@ -299,6 +304,8 @@ class CreatorAdminContentSeriesService(
|
|||||||
fun removeContentInTheSeries(request: RemoveContentToTheSeriesRequest, memberId: Long) {
|
fun removeContentInTheSeries(request: RemoveContentToTheSeriesRequest, memberId: Long) {
|
||||||
val series = repository.findByIdAndCreatorId(id = request.seriesId, creatorId = memberId)
|
val series = repository.findByIdAndCreatorId(id = request.seriesId, creatorId = memberId)
|
||||||
?: throw SodaException(messageKey = "creator.admin.series.invalid_access")
|
?: throw SodaException(messageKey = "creator.admin.series.invalid_access")
|
||||||
|
entityManager.refresh(series, LockModeType.PESSIMISTIC_WRITE)
|
||||||
|
if (series.member?.id != memberId) throw SodaException(messageKey = "creator.admin.series.invalid_access")
|
||||||
|
|
||||||
series.contentList.removeIf { it.content!!.id == request.contentId }
|
series.contentList.removeIf { it.content!!.id == request.contentId }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,56 @@ class SodaMessageSource {
|
|||||||
Lang.EN to "Invalid request.",
|
Lang.EN to "Invalid request.",
|
||||||
Lang.JA to "無効なリクエストです。"
|
Lang.JA to "無効なリクエストです。"
|
||||||
),
|
),
|
||||||
|
"translation.selection.not_found" to mapOf(
|
||||||
|
Lang.KO to "번역 요청 또는 대상을 찾을 수 없습니다.",
|
||||||
|
Lang.EN to "Translation request or scope was not found.",
|
||||||
|
Lang.JA to "翻訳リクエストまたは対象が見つかりません。"
|
||||||
|
),
|
||||||
|
"translation.selection.invalid_grouping" to mapOf(
|
||||||
|
Lang.KO to "제목 그룹의 구성원과 원문 구간을 확인해 주세요.",
|
||||||
|
Lang.EN to "Check title group members and source ranges.",
|
||||||
|
Lang.JA to "タイトルグループのメンバーと原文の範囲を確認してください。"
|
||||||
|
),
|
||||||
|
"translation.selection.revision_conflict" to mapOf(
|
||||||
|
Lang.KO to "번역 요청이 변경되었습니다. 최신 상태를 조회해 주세요.",
|
||||||
|
Lang.EN to "The translation request changed. Reload its current state.",
|
||||||
|
Lang.JA to "翻訳リクエストが変更されました。最新の状態を取得してください。"
|
||||||
|
),
|
||||||
|
"translation.selection.idempotency_conflict" to mapOf(
|
||||||
|
Lang.KO to "같은 요청 식별자를 다른 내용에 사용할 수 없습니다.",
|
||||||
|
Lang.EN to "The same request identifier cannot be used with a different body.",
|
||||||
|
Lang.JA to "同じリクエスト識別子を異なる内容には使用できません。"
|
||||||
|
),
|
||||||
|
"translation.selection.closed" to mapOf(
|
||||||
|
Lang.KO to "이미 종료된 번역 요청입니다.",
|
||||||
|
Lang.EN to "The translation request is already closed.",
|
||||||
|
Lang.JA to "翻訳リクエストは既に終了しています。"
|
||||||
|
),
|
||||||
|
"translation.selection.not_ready" to mapOf(
|
||||||
|
Lang.KO to "현재 번역 요청 상태에서는 수행할 수 없습니다.",
|
||||||
|
Lang.EN to "This operation is unavailable in the current translation request state.",
|
||||||
|
Lang.JA to "現在の翻訳リクエストの状態では実行できません。"
|
||||||
|
),
|
||||||
|
"translation.selection.candidate_not_selectable" to mapOf(
|
||||||
|
Lang.KO to "선택할 수 없는 번역 후보입니다. 요청과 필드, 언어를 확인해 주세요.",
|
||||||
|
Lang.EN to "This translation candidate cannot be selected. Check its request, field and language.",
|
||||||
|
Lang.JA to "選択できない翻訳候補です。リクエスト、フィールド、言語を確認してください。"
|
||||||
|
),
|
||||||
|
"translation.selection.incomplete_title_group" to mapOf(
|
||||||
|
Lang.KO to "공통 제목과 모든 구성원의 개별 구간 후보를 선택해 주세요.",
|
||||||
|
Lang.EN to "Select the common title and residual choices for every group member.",
|
||||||
|
Lang.JA to "共通タイトルと全メンバーの個別部分の候補を選択してください。"
|
||||||
|
),
|
||||||
|
"translation.selection.source_changed" to mapOf(
|
||||||
|
Lang.KO to "선택한 필드의 원문이 변경되었습니다. 새 번역 요청을 만들어 주세요.",
|
||||||
|
Lang.EN to "The selected source field changed. Create a new translation request.",
|
||||||
|
Lang.JA to "選択したフィールドの原文が変更されました。新しい翻訳リクエストを作成してください。"
|
||||||
|
),
|
||||||
|
"translation.selection.membership_changed" to mapOf(
|
||||||
|
Lang.KO to "시리즈의 활성 구성원이 변경되었습니다. 새 번역 요청을 만들어 주세요.",
|
||||||
|
Lang.EN to "The active series membership changed. Create a new translation request.",
|
||||||
|
Lang.JA to "シリーズの有効なメンバーが変更されました。新しい翻訳リクエストを作成してください。"
|
||||||
|
),
|
||||||
"chat.error.room_not_found" to mapOf(
|
"chat.error.room_not_found" to mapOf(
|
||||||
Lang.KO to "채팅방을 찾을 수 없습니다.",
|
Lang.KO to "채팅방을 찾을 수 없습니다.",
|
||||||
Lang.EN to "Chat room not found.",
|
Lang.EN to "Chat room not found.",
|
||||||
|
|||||||
@@ -21,8 +21,8 @@ enum class TranslationJobStatus {
|
|||||||
name = "translation_job",
|
name = "translation_job",
|
||||||
uniqueConstraints = [
|
uniqueConstraints = [
|
||||||
UniqueConstraint(
|
UniqueConstraint(
|
||||||
name = "uk_translation_job_resource_field_target_hash",
|
name = "uk_translation_job_resource_field_target_hash_language",
|
||||||
columnNames = ["resource_type", "resource_id", "field_key", "target_language", "source_hash"]
|
columnNames = ["resource_type", "resource_id", "field_key", "target_language", "source_hash", "source_language"]
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|||||||
+32
-6
@@ -1,20 +1,44 @@
|
|||||||
package kr.co.vividnext.sodalive.i18n.translation
|
package kr.co.vividnext.sodalive.i18n.translation
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository
|
import org.springframework.data.jpa.repository.JpaRepository
|
||||||
|
import org.springframework.data.jpa.repository.Lock
|
||||||
import org.springframework.data.jpa.repository.Modifying
|
import org.springframework.data.jpa.repository.Modifying
|
||||||
import org.springframework.data.jpa.repository.Query
|
import org.springframework.data.jpa.repository.Query
|
||||||
import org.springframework.data.repository.query.Param
|
import org.springframework.data.repository.query.Param
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
|
import javax.persistence.LockModeType
|
||||||
|
|
||||||
interface TranslationJobRepository : JpaRepository<TranslationJob, Long> {
|
interface TranslationJobRepository : JpaRepository<TranslationJob, Long> {
|
||||||
fun findByResourceTypeAndResourceIdAndFieldKeyAndTargetLanguageAndSourceHash(
|
fun findByResourceTypeAndResourceIdAndFieldKeyAndTargetLanguageAndSourceHashAndSourceLanguage(
|
||||||
resourceType: LanguageTranslationTargetType,
|
resourceType: LanguageTranslationTargetType,
|
||||||
resourceId: Long,
|
resourceId: Long,
|
||||||
fieldKey: String,
|
fieldKey: String,
|
||||||
targetLanguage: String,
|
targetLanguage: String,
|
||||||
sourceHash: String
|
sourceHash: String,
|
||||||
|
sourceLanguage: String
|
||||||
): TranslationJob?
|
): TranslationJob?
|
||||||
|
|
||||||
|
@Lock(LockModeType.PESSIMISTIC_WRITE)
|
||||||
|
@Query(
|
||||||
|
"""
|
||||||
|
select j from TranslationJob j
|
||||||
|
where j.resourceType = kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType.CONTENT
|
||||||
|
and j.resourceId = :contentId
|
||||||
|
and j.fieldKey = :fieldKey
|
||||||
|
and j.targetLanguage = :targetLanguage
|
||||||
|
and j.sourceHash = :sourceHash
|
||||||
|
and j.sourceLanguage = :sourceLanguage
|
||||||
|
order by j.id
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
fun findContentFieldJobsForUpdate(
|
||||||
|
@Param("contentId") contentId: Long,
|
||||||
|
@Param("fieldKey") fieldKey: String,
|
||||||
|
@Param("targetLanguage") targetLanguage: String,
|
||||||
|
@Param("sourceHash") sourceHash: String,
|
||||||
|
@Param("sourceLanguage") sourceLanguage: String
|
||||||
|
): List<TranslationJob>
|
||||||
|
|
||||||
fun findByResourceTypeAndResourceIdInAndTargetLanguageAndStatus(
|
fun findByResourceTypeAndResourceIdInAndTargetLanguageAndStatus(
|
||||||
resourceType: LanguageTranslationTargetType,
|
resourceType: LanguageTranslationTargetType,
|
||||||
resourceIds: Collection<Long>,
|
resourceIds: Collection<Long>,
|
||||||
@@ -61,11 +85,10 @@ interface TranslationJobRepository : JpaRepository<TranslationJob, Long> {
|
|||||||
j.nextRetryAt = :now
|
j.nextRetryAt = :now
|
||||||
where j.id = :jobId
|
where j.id = :jobId
|
||||||
and j.resourceType = kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType.CONTENT
|
and j.resourceType = kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType.CONTENT
|
||||||
and j.fieldKey = 'tags'
|
|
||||||
and j.status = kr.co.vividnext.sodalive.i18n.translation.TranslationJobStatus.COMPLETED
|
and j.status = kr.co.vividnext.sodalive.i18n.translation.TranslationJobStatus.COMPLETED
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
fun restartCompletedContentTagsJob(@Param("jobId") jobId: Long, @Param("now") now: LocalDateTime): Int
|
fun restartCompletedContentFieldJob(@Param("jobId") jobId: Long, @Param("now") now: LocalDateTime): Int
|
||||||
|
|
||||||
@Query(
|
@Query(
|
||||||
"""
|
"""
|
||||||
@@ -75,6 +98,7 @@ interface TranslationJobRepository : JpaRepository<TranslationJob, Long> {
|
|||||||
and j.fieldKey = :fieldKey
|
and j.fieldKey = :fieldKey
|
||||||
and j.targetLanguage = :targetLanguage
|
and j.targetLanguage = :targetLanguage
|
||||||
and j.sourceHash = :sourceHash
|
and j.sourceHash = :sourceHash
|
||||||
|
and j.sourceLanguage = :sourceLanguage
|
||||||
and j.status in (kr.co.vividnext.sodalive.i18n.translation.TranslationJobStatus.PENDING, kr.co.vividnext.sodalive.i18n.translation.TranslationJobStatus.RUNNING)
|
and j.status in (kr.co.vividnext.sodalive.i18n.translation.TranslationJobStatus.PENDING, kr.co.vividnext.sodalive.i18n.translation.TranslationJobStatus.RUNNING)
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
@@ -83,7 +107,8 @@ interface TranslationJobRepository : JpaRepository<TranslationJob, Long> {
|
|||||||
@Param("resourceId") resourceId: Long,
|
@Param("resourceId") resourceId: Long,
|
||||||
@Param("fieldKey") fieldKey: String,
|
@Param("fieldKey") fieldKey: String,
|
||||||
@Param("targetLanguage") targetLanguage: String,
|
@Param("targetLanguage") targetLanguage: String,
|
||||||
@Param("sourceHash") sourceHash: String
|
@Param("sourceHash") sourceHash: String,
|
||||||
|
@Param("sourceLanguage") sourceLanguage: String
|
||||||
): TranslationJob?
|
): TranslationJob?
|
||||||
|
|
||||||
@Query(
|
@Query(
|
||||||
@@ -91,7 +116,7 @@ interface TranslationJobRepository : JpaRepository<TranslationJob, Long> {
|
|||||||
select j.id
|
select j.id
|
||||||
from translation_job j
|
from translation_job j
|
||||||
join (
|
join (
|
||||||
select resource_type, resource_id, target_language
|
select resource_type, resource_id, target_language, source_language
|
||||||
from translation_job
|
from translation_job
|
||||||
where status = 'PENDING'
|
where status = 'PENDING'
|
||||||
and next_retry_at <= :now
|
and next_retry_at <= :now
|
||||||
@@ -100,6 +125,7 @@ interface TranslationJobRepository : JpaRepository<TranslationJob, Long> {
|
|||||||
) g on j.resource_type = g.resource_type
|
) g on j.resource_type = g.resource_type
|
||||||
and j.resource_id = g.resource_id
|
and j.resource_id = g.resource_id
|
||||||
and j.target_language = g.target_language
|
and j.target_language = g.target_language
|
||||||
|
and j.source_language = g.source_language
|
||||||
where j.status = 'PENDING'
|
where j.status = 'PENDING'
|
||||||
and j.next_retry_at <= :now
|
and j.next_retry_at <= :now
|
||||||
order by j.created_at asc
|
order by j.created_at asc
|
||||||
|
|||||||
+33
-7
@@ -4,12 +4,15 @@ import kr.co.vividnext.sodalive.content.translation.ContentTranslationRepository
|
|||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
import org.springframework.transaction.annotation.Transactional
|
import org.springframework.transaction.annotation.Transactional
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
|
import javax.persistence.EntityManager
|
||||||
|
import javax.persistence.LockModeType
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
class TranslationJobScheduler(
|
class TranslationJobScheduler(
|
||||||
private val translationJobRepository: TranslationJobRepository,
|
private val translationJobRepository: TranslationJobRepository,
|
||||||
private val translationCoordinator: TranslationCoordinator,
|
private val translationCoordinator: TranslationCoordinator,
|
||||||
private val contentTranslationRepository: ContentTranslationRepository
|
private val contentTranslationRepository: ContentTranslationRepository,
|
||||||
|
private val entityManager: EntityManager
|
||||||
) {
|
) {
|
||||||
@Transactional
|
@Transactional
|
||||||
fun scheduleMissingTranslation(
|
fun scheduleMissingTranslation(
|
||||||
@@ -18,7 +21,8 @@ class TranslationJobScheduler(
|
|||||||
fieldKey: String,
|
fieldKey: String,
|
||||||
sourceText: String,
|
sourceText: String,
|
||||||
sourceLanguage: String,
|
sourceLanguage: String,
|
||||||
targetLanguage: String
|
targetLanguage: String,
|
||||||
|
refreshCompletedContentField: Boolean = false
|
||||||
) {
|
) {
|
||||||
val normalizedText = SourceTextNormalizer.normalize(sourceText)
|
val normalizedText = SourceTextNormalizer.normalize(sourceText)
|
||||||
if (normalizedText.isBlank()) return
|
if (normalizedText.isBlank()) return
|
||||||
@@ -30,16 +34,38 @@ class TranslationJobScheduler(
|
|||||||
if (normalizedSourceLanguage == normalizedTargetLanguage) return
|
if (normalizedSourceLanguage == normalizedTargetLanguage) return
|
||||||
|
|
||||||
val sourceHash = SourceTextNormalizer.hash(normalizedText)
|
val sourceHash = SourceTextNormalizer.hash(normalizedText)
|
||||||
val existingJob = translationJobRepository.findByResourceTypeAndResourceIdAndFieldKeyAndTargetLanguageAndSourceHash(
|
val forceContentRefresh = resourceType == LanguageTranslationTargetType.CONTENT && refreshCompletedContentField
|
||||||
|
val existingJob = if (forceContentRefresh) {
|
||||||
|
translationJobRepository.findContentFieldJobsForUpdate(
|
||||||
|
resourceId,
|
||||||
|
fieldKey,
|
||||||
|
normalizedTargetLanguage,
|
||||||
|
sourceHash,
|
||||||
|
normalizedSourceLanguage
|
||||||
|
).singleOrNull()?.also { entityManager.refresh(it, LockModeType.PESSIMISTIC_WRITE) }
|
||||||
|
} else {
|
||||||
|
translationJobRepository.findByResourceTypeAndResourceIdAndFieldKeyAndTargetLanguageAndSourceHashAndSourceLanguage(
|
||||||
resourceType = resourceType,
|
resourceType = resourceType,
|
||||||
resourceId = resourceId,
|
resourceId = resourceId,
|
||||||
fieldKey = fieldKey,
|
fieldKey = fieldKey,
|
||||||
targetLanguage = normalizedTargetLanguage,
|
targetLanguage = normalizedTargetLanguage,
|
||||||
sourceHash = sourceHash
|
sourceHash = sourceHash,
|
||||||
|
sourceLanguage = normalizedSourceLanguage
|
||||||
)
|
)
|
||||||
|
}
|
||||||
if (existingJob != null) {
|
if (existingJob != null) {
|
||||||
if (existingJob.sourceLanguage != normalizedSourceLanguage) return
|
if (forceContentRefresh) {
|
||||||
|
when (existingJob.status) {
|
||||||
|
TranslationJobStatus.FAILED, TranslationJobStatus.COMPLETED -> {
|
||||||
|
existingJob.status = TranslationJobStatus.PENDING
|
||||||
|
existingJob.retryCount = 0
|
||||||
|
existingJob.lastErrorMessage = null
|
||||||
|
existingJob.nextRetryAt = LocalDateTime.now()
|
||||||
|
}
|
||||||
|
TranslationJobStatus.PENDING, TranslationJobStatus.RUNNING -> Unit
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
when (existingJob.status) {
|
when (existingJob.status) {
|
||||||
TranslationJobStatus.FAILED ->
|
TranslationJobStatus.FAILED ->
|
||||||
translationJobRepository.restartFailedJob(requireNotNull(existingJob.id), LocalDateTime.now())
|
translationJobRepository.restartFailedJob(requireNotNull(existingJob.id), LocalDateTime.now())
|
||||||
@@ -63,7 +89,7 @@ class TranslationJobScheduler(
|
|||||||
)
|
)
|
||||||
if (cached != null && cached.translatedText == renderedTags) return
|
if (cached != null && cached.translatedText == renderedTags) return
|
||||||
|
|
||||||
translationJobRepository.restartCompletedContentTagsJob(requireNotNull(existingJob.id), LocalDateTime.now())
|
translationJobRepository.restartCompletedContentFieldJob(requireNotNull(existingJob.id), LocalDateTime.now())
|
||||||
}
|
}
|
||||||
TranslationJobStatus.PENDING, TranslationJobStatus.RUNNING -> Unit
|
TranslationJobStatus.PENDING, TranslationJobStatus.RUNNING -> Unit
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ class TranslationJobWorker(
|
|||||||
val failedJobs = mutableListOf<Pair<TranslationJob, Exception>>()
|
val failedJobs = mutableListOf<Pair<TranslationJob, Exception>>()
|
||||||
jobs.forEach { job ->
|
jobs.forEach { job ->
|
||||||
try {
|
try {
|
||||||
|
if (job.resourceType != LanguageTranslationTargetType.CONTENT || !materializer.hasCurrentContentApproval(job)) {
|
||||||
translationMemoryService.ensureMemory(
|
translationMemoryService.ensureMemory(
|
||||||
job.resourceType,
|
job.resourceType,
|
||||||
job.fieldKey,
|
job.fieldKey,
|
||||||
@@ -40,24 +41,39 @@ class TranslationJobWorker(
|
|||||||
job.sourceLanguage,
|
job.sourceLanguage,
|
||||||
job.targetLanguage
|
job.targetLanguage
|
||||||
)
|
)
|
||||||
|
}
|
||||||
succeededJobs.add(job)
|
succeededJobs.add(job)
|
||||||
} catch (ex: Exception) {
|
} catch (ex: Exception) {
|
||||||
failedJobs.add(job to ex)
|
failedJobs.add(job to ex)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (failedJobs.isNotEmpty()) {
|
if (failedJobs.isNotEmpty() && firstJob.resourceType != LanguageTranslationTargetType.CONTENT) {
|
||||||
succeededJobs.forEach { completeJob(it.id!!) }
|
succeededJobs.forEach { completeJob(it.id!!) }
|
||||||
failedJobs.forEach { (job, ex) -> failJob(job.id!!, ex) }
|
failedJobs.forEach { (job, ex) -> failJob(job.id!!, ex) }
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (succeededJobs.isNotEmpty()) {
|
||||||
try {
|
try {
|
||||||
|
transactionTemplate.executeWithoutResult {
|
||||||
|
val materialized = if (firstJob.resourceType == LanguageTranslationTargetType.CONTENT) {
|
||||||
|
materializer.materializeContentFields(
|
||||||
|
firstJob.resourceId,
|
||||||
|
firstJob.targetLanguage,
|
||||||
|
succeededJobs.map { it.fieldKey }.toSet()
|
||||||
|
)
|
||||||
|
} else {
|
||||||
materializer.materialize(firstJob.resourceType, firstJob.resourceId, firstJob.targetLanguage)
|
materializer.materialize(firstJob.resourceType, firstJob.resourceId, firstJob.targetLanguage)
|
||||||
|
}
|
||||||
|
check(materialized) { "translation materialization is incomplete" }
|
||||||
succeededJobs.forEach { completeJob(it.id!!) }
|
succeededJobs.forEach { completeJob(it.id!!) }
|
||||||
|
}
|
||||||
} catch (ex: Exception) {
|
} catch (ex: Exception) {
|
||||||
succeededJobs.forEach { failJob(it.id!!, ex) }
|
succeededJobs.forEach { failJob(it.id!!, ex) }
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
failedJobs.forEach { (job, ex) -> failJob(job.id!!, ex) }
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+22
-26
@@ -6,7 +6,6 @@ import kr.co.vividnext.sodalive.chat.character.translate.AiCharacterTranslationR
|
|||||||
import kr.co.vividnext.sodalive.chat.original.translation.OriginalWorkTranslation
|
import kr.co.vividnext.sodalive.chat.original.translation.OriginalWorkTranslation
|
||||||
import kr.co.vividnext.sodalive.chat.original.translation.OriginalWorkTranslationPayload
|
import kr.co.vividnext.sodalive.chat.original.translation.OriginalWorkTranslationPayload
|
||||||
import kr.co.vividnext.sodalive.chat.original.translation.OriginalWorkTranslationRepository
|
import kr.co.vividnext.sodalive.chat.original.translation.OriginalWorkTranslationRepository
|
||||||
import kr.co.vividnext.sodalive.content.AudioContent
|
|
||||||
import kr.co.vividnext.sodalive.content.category.CategoryTranslation
|
import kr.co.vividnext.sodalive.content.category.CategoryTranslation
|
||||||
import kr.co.vividnext.sodalive.content.category.CategoryTranslationRepository
|
import kr.co.vividnext.sodalive.content.category.CategoryTranslationRepository
|
||||||
import kr.co.vividnext.sodalive.content.series.translation.SeriesGenreTranslation
|
import kr.co.vividnext.sodalive.content.series.translation.SeriesGenreTranslation
|
||||||
@@ -16,11 +15,10 @@ import kr.co.vividnext.sodalive.content.series.translation.SeriesTranslationPayl
|
|||||||
import kr.co.vividnext.sodalive.content.series.translation.SeriesTranslationRepository
|
import kr.co.vividnext.sodalive.content.series.translation.SeriesTranslationRepository
|
||||||
import kr.co.vividnext.sodalive.content.theme.translation.ContentThemeTranslation
|
import kr.co.vividnext.sodalive.content.theme.translation.ContentThemeTranslation
|
||||||
import kr.co.vividnext.sodalive.content.theme.translation.ContentThemeTranslationRepository
|
import kr.co.vividnext.sodalive.content.theme.translation.ContentThemeTranslationRepository
|
||||||
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.Series
|
||||||
import kr.co.vividnext.sodalive.explorer.profile.creatorCommunity.CreatorCommunityRepository
|
import kr.co.vividnext.sodalive.explorer.profile.creatorCommunity.CreatorCommunityRepository
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationWriteService
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||||
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.CreatorCommunityTranslation
|
||||||
import kr.co.vividnext.sodalive.v2.creator.channel.community.translation.adapter.out.persistence.CreatorCommunityTranslationRepository
|
import kr.co.vividnext.sodalive.v2.creator.channel.community.translation.adapter.out.persistence.CreatorCommunityTranslationRepository
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
@@ -32,7 +30,6 @@ import javax.persistence.LockModeType
|
|||||||
class TranslationReadModelMaterializer(
|
class TranslationReadModelMaterializer(
|
||||||
private val sourceExtractor: TranslationSourceExtractor,
|
private val sourceExtractor: TranslationSourceExtractor,
|
||||||
private val translationCoordinator: TranslationCoordinator,
|
private val translationCoordinator: TranslationCoordinator,
|
||||||
private val contentTranslationRepository: ContentTranslationRepository,
|
|
||||||
private val aiCharacterTranslationRepository: AiCharacterTranslationRepository,
|
private val aiCharacterTranslationRepository: AiCharacterTranslationRepository,
|
||||||
private val contentThemeTranslationRepository: ContentThemeTranslationRepository,
|
private val contentThemeTranslationRepository: ContentThemeTranslationRepository,
|
||||||
private val seriesTranslationRepository: SeriesTranslationRepository,
|
private val seriesTranslationRepository: SeriesTranslationRepository,
|
||||||
@@ -41,15 +38,32 @@ class TranslationReadModelMaterializer(
|
|||||||
private val categoryTranslationRepository: CategoryTranslationRepository,
|
private val categoryTranslationRepository: CategoryTranslationRepository,
|
||||||
private val creatorCommunityRepository: CreatorCommunityRepository,
|
private val creatorCommunityRepository: CreatorCommunityRepository,
|
||||||
private val creatorCommunityTranslationRepository: CreatorCommunityTranslationRepository,
|
private val creatorCommunityTranslationRepository: CreatorCommunityTranslationRepository,
|
||||||
private val entityManager: EntityManager
|
private val entityManager: EntityManager,
|
||||||
|
private val contentWriter: ContentTranslationWriteService
|
||||||
) {
|
) {
|
||||||
|
@Transactional
|
||||||
|
fun hasCurrentContentApproval(job: TranslationJob): Boolean = contentWriter.hasCurrentApproval(
|
||||||
|
job.resourceId,
|
||||||
|
job.targetLanguage,
|
||||||
|
job.fieldKey
|
||||||
|
)
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
fun materializeContentFields(resourceId: Long, targetLanguage: String, fieldKeys: Set<String>): Boolean =
|
||||||
|
contentWriter.materialize(
|
||||||
|
resourceId,
|
||||||
|
targetLanguage,
|
||||||
|
fieldKeys.map { key -> ContentSourceFieldKey.values().single { it.key == key } }.toSet()
|
||||||
|
)
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
fun materialize(resourceType: LanguageTranslationTargetType, resourceId: Long, targetLanguage: String): Boolean {
|
fun materialize(resourceType: LanguageTranslationTargetType, resourceId: Long, targetLanguage: String): Boolean {
|
||||||
|
if (resourceType == LanguageTranslationTargetType.CONTENT) return contentWriter.materialize(resourceId, targetLanguage)
|
||||||
val source = sourceExtractor.extract(resourceType, resourceId) ?: return false
|
val source = sourceExtractor.extract(resourceType, resourceId) ?: return false
|
||||||
val translations = resolveTranslatedFields(source, targetLanguage.lowercase()) ?: return false
|
val translations = resolveTranslatedFields(source, targetLanguage.lowercase()) ?: return false
|
||||||
|
|
||||||
when (resourceType) {
|
when (resourceType) {
|
||||||
LanguageTranslationTargetType.CONTENT -> upsertContent(resourceId, targetLanguage, translations)
|
LanguageTranslationTargetType.CONTENT -> error("CONTENT uses the locked field writer")
|
||||||
LanguageTranslationTargetType.CHARACTER -> upsertCharacter(resourceId, targetLanguage, translations)
|
LanguageTranslationTargetType.CHARACTER -> upsertCharacter(resourceId, targetLanguage, translations)
|
||||||
LanguageTranslationTargetType.CONTENT_THEME -> upsertContentTheme(resourceId, targetLanguage, translations)
|
LanguageTranslationTargetType.CONTENT_THEME -> upsertContentTheme(resourceId, targetLanguage, translations)
|
||||||
LanguageTranslationTargetType.SERIES -> upsertSeries(resourceId, targetLanguage, translations)
|
LanguageTranslationTargetType.SERIES -> upsertSeries(resourceId, targetLanguage, translations)
|
||||||
@@ -76,9 +90,7 @@ class TranslationReadModelMaterializer(
|
|||||||
val memory = translationCoordinator.findCached(
|
val memory = translationCoordinator.findCached(
|
||||||
sourceHash = SourceTextNormalizer.hash(normalizedText),
|
sourceHash = SourceTextNormalizer.hash(normalizedText),
|
||||||
sourceLanguage = source.sourceLanguage.lowercase(),
|
sourceLanguage = source.sourceLanguage.lowercase(),
|
||||||
targetLanguage = targetLanguage,
|
targetLanguage = targetLanguage
|
||||||
rejectUnchangedAwsResult = source.resourceType == LanguageTranslationTargetType.CONTENT &&
|
|
||||||
field.fieldKey == "tags"
|
|
||||||
) ?: return null
|
) ?: return null
|
||||||
|
|
||||||
result[field.fieldKey] = memory.translatedText
|
result[field.fieldKey] = memory.translatedText
|
||||||
@@ -86,22 +98,6 @@ class TranslationReadModelMaterializer(
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun upsertContent(resourceId: Long, targetLanguage: String, translations: Map<String, String>) {
|
|
||||||
val payload = ContentTranslationPayload(
|
|
||||||
title = translations["title"].orEmpty(),
|
|
||||||
detail = translations["detail"].orEmpty(),
|
|
||||||
tags = translations["tags"].orEmpty()
|
|
||||||
)
|
|
||||||
entityManager.find(AudioContent::class.java, resourceId, LockModeType.PESSIMISTIC_WRITE)
|
|
||||||
val existing = contentTranslationRepository.findByContentIdAndLocaleForUpdate(resourceId, targetLanguage)
|
|
||||||
if (existing == null) {
|
|
||||||
contentTranslationRepository.save(ContentTranslation(resourceId, targetLanguage, payload))
|
|
||||||
} else {
|
|
||||||
existing.renderedPayload = payload
|
|
||||||
contentTranslationRepository.save(existing)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun upsertCharacter(resourceId: Long, targetLanguage: String, translations: Map<String, String>) {
|
private fun upsertCharacter(resourceId: Long, targetLanguage: String, translations: Map<String, String>) {
|
||||||
val payload = AiCharacterTranslationRenderedPayload(
|
val payload = AiCharacterTranslationRenderedPayload(
|
||||||
name = translations["name"].orEmpty(),
|
name = translations["name"].orEmpty(),
|
||||||
|
|||||||
+4
-10
@@ -8,6 +8,7 @@ import kr.co.vividnext.sodalive.content.AudioContentRepository
|
|||||||
import kr.co.vividnext.sodalive.content.category.CategoryRepository
|
import kr.co.vividnext.sodalive.content.category.CategoryRepository
|
||||||
import kr.co.vividnext.sodalive.content.theme.AudioContentThemeQueryRepository
|
import kr.co.vividnext.sodalive.content.theme.AudioContentThemeQueryRepository
|
||||||
import kr.co.vividnext.sodalive.explorer.profile.creatorCommunity.CreatorCommunityRepository
|
import kr.co.vividnext.sodalive.explorer.profile.creatorCommunity.CreatorCommunityRepository
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.toContentSourceSnapshot
|
||||||
import org.springframework.data.repository.findByIdOrNull
|
import org.springframework.data.repository.findByIdOrNull
|
||||||
import org.springframework.stereotype.Component
|
import org.springframework.stereotype.Component
|
||||||
|
|
||||||
@@ -50,20 +51,13 @@ class TranslationSourceExtractor(
|
|||||||
|
|
||||||
private fun extractContent(resourceId: Long): TranslationSource? {
|
private fun extractContent(resourceId: Long): TranslationSource? {
|
||||||
val content = audioContentRepository.findByIdOrNull(resourceId) ?: return null
|
val content = audioContentRepository.findByIdOrNull(resourceId) ?: return null
|
||||||
val sourceLanguage = content.languageCode?.takeIf { it.isNotBlank() } ?: return null
|
val snapshot = content.toContentSourceSnapshot()
|
||||||
val tags = content.audioContentHashTags
|
val sourceLanguage = snapshot.sourceLanguage?.takeIf { it.isNotBlank() } ?: return null
|
||||||
.filter { it.isActive }
|
|
||||||
.mapNotNull { it.hashTag?.tag }
|
|
||||||
.joinToString(",")
|
|
||||||
return TranslationSource(
|
return TranslationSource(
|
||||||
resourceType = LanguageTranslationTargetType.CONTENT,
|
resourceType = LanguageTranslationTargetType.CONTENT,
|
||||||
resourceId = resourceId,
|
resourceId = resourceId,
|
||||||
sourceLanguage = sourceLanguage,
|
sourceLanguage = sourceLanguage,
|
||||||
fields = listOf(
|
fields = snapshot.fields
|
||||||
TranslationSourceField("title", content.title),
|
|
||||||
TranslationSourceField("detail", content.detail),
|
|
||||||
TranslationSourceField("tags", tags)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+90
@@ -0,0 +1,90 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.`in`.web
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.common.ApiResponse
|
||||||
|
import kr.co.vividnext.sodalive.member.Member
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.TranslationSelectionApplyService
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.TranslationSelectionCandidateQueryService
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.TranslationSelectionContentsService
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.TranslationSelectionGenerationService
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.TranslationSelectionGroupingService
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.TranslationSelectionRequestService
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionError
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.ApplyTranslationSelectionRequest
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.CreateTranslationSelectionRequest
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.GenerateTranslationSelectionRequest
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.ReviewTranslationSelectionGroupingRequest
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.response
|
||||||
|
import org.springframework.security.access.prepost.PreAuthorize
|
||||||
|
import org.springframework.security.core.annotation.AuthenticationPrincipal
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping
|
||||||
|
import org.springframework.web.bind.annotation.PutMapping
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam
|
||||||
|
import org.springframework.web.bind.annotation.RestController
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/admin/audio-content/translation-requests")
|
||||||
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
|
class AdminTranslationSelectionController(
|
||||||
|
private val requests: TranslationSelectionRequestService,
|
||||||
|
private val grouping: TranslationSelectionGroupingService,
|
||||||
|
private val contents: TranslationSelectionContentsService,
|
||||||
|
private val generation: TranslationSelectionGenerationService,
|
||||||
|
private val candidates: TranslationSelectionCandidateQueryService,
|
||||||
|
private val applyService: TranslationSelectionApplyService
|
||||||
|
) {
|
||||||
|
@PostMapping
|
||||||
|
fun create(
|
||||||
|
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?,
|
||||||
|
@RequestBody body: CreateTranslationSelectionRequest
|
||||||
|
) = ApiResponse.ok(
|
||||||
|
requests.create(
|
||||||
|
member?.id ?: throw TranslationSelectionError.INVALID_REQUEST.exception(),
|
||||||
|
body.clientRequestId,
|
||||||
|
body.scope
|
||||||
|
).response()
|
||||||
|
)
|
||||||
|
|
||||||
|
@GetMapping("/{requestId}")
|
||||||
|
fun summary(@PathVariable requestId: Long) = ApiResponse.ok(requests.get(requestId).response())
|
||||||
|
|
||||||
|
@GetMapping("/{requestId}/contents")
|
||||||
|
fun contents(
|
||||||
|
@PathVariable requestId: Long,
|
||||||
|
@RequestParam(required = false) afterContentId: Long?,
|
||||||
|
@RequestParam(defaultValue = "50") size: Int
|
||||||
|
) = ApiResponse.ok(contents.contents(requestId, afterContentId, size))
|
||||||
|
|
||||||
|
@GetMapping("/{requestId}/grouping")
|
||||||
|
fun grouping(@PathVariable requestId: Long) = ApiResponse.ok(grouping.get(requestId))
|
||||||
|
|
||||||
|
@PutMapping("/{requestId}/grouping")
|
||||||
|
fun review(@PathVariable requestId: Long, @RequestBody body: ReviewTranslationSelectionGroupingRequest) =
|
||||||
|
ApiResponse.ok(grouping.review(requestId, body))
|
||||||
|
|
||||||
|
@PostMapping("/{requestId}/cancel")
|
||||||
|
fun cancel(@PathVariable requestId: Long) = ApiResponse.ok(requests.cancel(requestId).response())
|
||||||
|
|
||||||
|
@PostMapping("/{requestId}/generate")
|
||||||
|
fun generate(@PathVariable requestId: Long, @RequestBody body: GenerateTranslationSelectionRequest) =
|
||||||
|
ApiResponse.ok(generation.generate(requestId, body.expectedRevision).response())
|
||||||
|
|
||||||
|
@GetMapping("/{requestId}/candidates")
|
||||||
|
fun candidates(
|
||||||
|
@PathVariable requestId: Long,
|
||||||
|
@RequestParam(required = false) afterCandidateId: Long?,
|
||||||
|
@RequestParam(defaultValue = "50") size: Int
|
||||||
|
) = ApiResponse.ok(candidates.page(requestId, afterCandidateId, size))
|
||||||
|
|
||||||
|
@PostMapping("/{requestId}/apply")
|
||||||
|
fun apply(
|
||||||
|
@PathVariable requestId: Long,
|
||||||
|
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?,
|
||||||
|
@RequestBody body: ApplyTranslationSelectionRequest
|
||||||
|
) = ApiResponse.ok(
|
||||||
|
applyService.apply(requestId, member?.id ?: throw TranslationSelectionError.INVALID_REQUEST.exception(), body)
|
||||||
|
)
|
||||||
|
}
|
||||||
+17
@@ -0,0 +1,17 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.`in`.web
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.common.SodaExceptionHandler
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionError
|
||||||
|
import org.springframework.core.Ordered
|
||||||
|
import org.springframework.core.annotation.Order
|
||||||
|
import org.springframework.http.converter.HttpMessageNotReadableException
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice
|
||||||
|
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException
|
||||||
|
|
||||||
|
@RestControllerAdvice(assignableTypes = [AdminTranslationSelectionController::class])
|
||||||
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||||
|
class TranslationSelectionInputExceptionHandler(private val handler: SodaExceptionHandler) {
|
||||||
|
@ExceptionHandler(HttpMessageNotReadableException::class, MethodArgumentTypeMismatchException::class)
|
||||||
|
fun invalidInput() = handler.handleSodaException(TranslationSelectionError.INVALID_REQUEST.exception())
|
||||||
|
}
|
||||||
+63
@@ -0,0 +1,63 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||||
|
import com.fasterxml.jackson.module.kotlin.readValue
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ApprovalProvenance
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import javax.persistence.AttributeConverter
|
||||||
|
import javax.persistence.Column
|
||||||
|
import javax.persistence.Convert
|
||||||
|
import javax.persistence.Converter
|
||||||
|
import javax.persistence.Entity
|
||||||
|
import javax.persistence.EnumType
|
||||||
|
import javax.persistence.Enumerated
|
||||||
|
import javax.persistence.GeneratedValue
|
||||||
|
import javax.persistence.GenerationType
|
||||||
|
import javax.persistence.Id
|
||||||
|
import javax.persistence.Table
|
||||||
|
import javax.persistence.UniqueConstraint
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(
|
||||||
|
name = "content_translation_override",
|
||||||
|
uniqueConstraints = [
|
||||||
|
UniqueConstraint(name = "uk_content_translation_override_field", columnNames = ["content_id", "locale", "field_key"])
|
||||||
|
]
|
||||||
|
)
|
||||||
|
class ContentTranslationOverrideEntity(
|
||||||
|
@Column(name = "content_id", nullable = false)
|
||||||
|
val contentId: Long,
|
||||||
|
@Column(nullable = false, length = 10)
|
||||||
|
val locale: String,
|
||||||
|
@Enumerated(EnumType.STRING)
|
||||||
|
@Column(name = "field_key", nullable = false, length = 10)
|
||||||
|
val fieldKey: ContentSourceFieldKey,
|
||||||
|
@Column(nullable = false, length = 67)
|
||||||
|
var sourceFingerprint: String,
|
||||||
|
@Column(nullable = false, columnDefinition = "text")
|
||||||
|
var approvedText: String,
|
||||||
|
@Convert(converter = ApprovalProvenanceConverter::class)
|
||||||
|
@Column(nullable = false, columnDefinition = "text")
|
||||||
|
var provenance: ApprovalProvenance,
|
||||||
|
@Column(nullable = false, columnDefinition = "timestamp")
|
||||||
|
var approvedAt: LocalDateTime,
|
||||||
|
@Column(nullable = false, columnDefinition = "timestamp")
|
||||||
|
val createdAt: LocalDateTime = approvedAt,
|
||||||
|
@Column(nullable = false, columnDefinition = "timestamp")
|
||||||
|
var updatedAt: LocalDateTime = approvedAt
|
||||||
|
) {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
var id: Long? = null
|
||||||
|
}
|
||||||
|
|
||||||
|
@Converter
|
||||||
|
class ApprovalProvenanceConverter : AttributeConverter<ApprovalProvenance, String> {
|
||||||
|
override fun convertToDatabaseColumn(attribute: ApprovalProvenance): String = mapper.writeValueAsString(attribute)
|
||||||
|
override fun convertToEntityAttribute(dbData: String): ApprovalProvenance = mapper.readValue(dbData)
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val mapper = jacksonObjectMapper()
|
||||||
|
}
|
||||||
|
}
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository
|
||||||
|
import org.springframework.data.jpa.repository.Lock
|
||||||
|
import javax.persistence.LockModeType
|
||||||
|
|
||||||
|
interface ContentTranslationOverrideRepository : JpaRepository<ContentTranslationOverrideEntity, Long> {
|
||||||
|
@Lock(LockModeType.PESSIMISTIC_WRITE)
|
||||||
|
fun findByContentId(contentId: Long): List<ContentTranslationOverrideEntity>
|
||||||
|
}
|
||||||
+56
@@ -0,0 +1,56 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentFieldApproval
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentTranslationOverride
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.ContentTranslationOverrideStore
|
||||||
|
import org.springframework.stereotype.Component
|
||||||
|
import javax.persistence.EntityManager
|
||||||
|
import javax.persistence.LockModeType
|
||||||
|
|
||||||
|
@Component
|
||||||
|
class JpaContentTranslationOverrideStore(
|
||||||
|
private val repository: ContentTranslationOverrideRepository,
|
||||||
|
private val entityManager: EntityManager
|
||||||
|
) : ContentTranslationOverrideStore {
|
||||||
|
override fun findByContentId(contentId: Long): List<ContentTranslationOverride> = rows(contentId).map {
|
||||||
|
ContentTranslationOverride(
|
||||||
|
it.contentId,
|
||||||
|
it.locale,
|
||||||
|
ContentFieldApproval(it.fieldKey, it.sourceFingerprint, it.approvedText, it.provenance),
|
||||||
|
it.approvedAt
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun save(override: ContentTranslationOverride) {
|
||||||
|
val approval = override.approval
|
||||||
|
val existing = rows(override.contentId).singleOrNull { it.locale == override.locale && it.fieldKey == approval.field }
|
||||||
|
if (existing == null) {
|
||||||
|
repository.save(
|
||||||
|
ContentTranslationOverrideEntity(
|
||||||
|
override.contentId,
|
||||||
|
override.locale,
|
||||||
|
approval.field,
|
||||||
|
approval.sourceFingerprint,
|
||||||
|
approval.translatedText,
|
||||||
|
approval.provenance,
|
||||||
|
override.approvedAt
|
||||||
|
)
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
existing.sourceFingerprint = approval.sourceFingerprint
|
||||||
|
existing.approvedText = approval.translatedText
|
||||||
|
existing.provenance = approval.provenance
|
||||||
|
existing.approvedAt = override.approvedAt
|
||||||
|
existing.updatedAt = override.approvedAt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun deleteFields(contentId: Long, fields: Set<ContentSourceFieldKey>, locale: String?) {
|
||||||
|
rows(contentId).filter { it.fieldKey in fields && (locale == null || it.locale == locale) }.forEach(repository::delete)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun rows(contentId: Long): List<ContentTranslationOverrideEntity> = repository.findByContentId(contentId).onEach {
|
||||||
|
entityManager.refresh(it, LockModeType.PESSIMISTIC_WRITE)
|
||||||
|
}
|
||||||
|
}
|
||||||
+137
@@ -0,0 +1,137 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ApprovalProvider
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.SelectionCandidateProgress
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidate
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateStatus
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationCandidateStore
|
||||||
|
import org.springframework.stereotype.Component
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import javax.persistence.EntityManager
|
||||||
|
import javax.persistence.LockModeType
|
||||||
|
|
||||||
|
@Component
|
||||||
|
class JpaTranslationCandidateStore(private val entityManager: EntityManager) : TranslationCandidateStore {
|
||||||
|
override fun save(candidate: TranslationCandidate): TranslationCandidate {
|
||||||
|
val row = if (candidate.candidateId == 0L) {
|
||||||
|
TranslationCandidateEntity(
|
||||||
|
entityManager.getReference(TranslationSelectionRequestEntity::class.java, candidate.requestId),
|
||||||
|
candidate.unit.key, candidate.unit, candidate.provider.id, candidate.provider.version, candidate.targetLanguage,
|
||||||
|
candidate.status, candidate.translatedValues, candidate.failureCode, candidate.attempt, candidate.leaseUntil,
|
||||||
|
candidate.fence, candidate.createdAt, candidate.updatedAt, candidate.terminalAt, candidate.linkedCandidateId
|
||||||
|
).also { entityManager.persist(it) }
|
||||||
|
} else {
|
||||||
|
entityManager.getReference(TranslationCandidateEntity::class.java, candidate.candidateId).apply {
|
||||||
|
status = candidate.status
|
||||||
|
translatedValues = candidate.translatedValues
|
||||||
|
failureCode = candidate.failureCode
|
||||||
|
attempt = candidate.attempt
|
||||||
|
leaseUntil = candidate.leaseUntil
|
||||||
|
fence = candidate.fence
|
||||||
|
updatedAt = candidate.updatedAt
|
||||||
|
terminalAt = candidate.terminalAt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
entityManager.flush()
|
||||||
|
return row.domain()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun nextRequest(now: LocalDateTime): Long? = entityManager.createQuery(
|
||||||
|
"select c.request.id from TranslationCandidateEntity c where c.request.status = :generating and " +
|
||||||
|
"c.linkedCandidateId is null and (c.status = :pending or (c.status = :running and c.leaseUntil <= :now)) " +
|
||||||
|
"order by c.id",
|
||||||
|
java.lang.Long::class.java
|
||||||
|
).setParameter("generating", TranslationSelectionStatus.GENERATING)
|
||||||
|
.setParameter("pending", TranslationCandidateStatus.PENDING).setParameter("running", TranslationCandidateStatus.RUNNING)
|
||||||
|
.setParameter("now", now)
|
||||||
|
.setMaxResults(1).resultList.firstOrNull()?.toLong()
|
||||||
|
|
||||||
|
override fun lockNext(requestId: Long, now: LocalDateTime): TranslationCandidate? = entityManager.createQuery(
|
||||||
|
"select c from TranslationCandidateEntity c where c.request.id = :requestId and " +
|
||||||
|
"c.linkedCandidateId is null and (c.status = :pending or (c.status = :running and c.leaseUntil <= :now)) " +
|
||||||
|
"order by c.id",
|
||||||
|
TranslationCandidateEntity::class.java
|
||||||
|
).setParameter("requestId", requestId).setParameter("pending", TranslationCandidateStatus.PENDING)
|
||||||
|
.setParameter("running", TranslationCandidateStatus.RUNNING).setParameter("now", now)
|
||||||
|
.setLockMode(LockModeType.PESSIMISTIC_WRITE).setMaxResults(1).resultList.firstOrNull()
|
||||||
|
?.let { lock(requireNotNull(it.id)) }
|
||||||
|
|
||||||
|
override fun find(candidateId: Long): TranslationCandidate? =
|
||||||
|
entityManager.find(TranslationCandidateEntity::class.java, candidateId)?.domain()
|
||||||
|
|
||||||
|
override fun lock(candidateId: Long): TranslationCandidate? {
|
||||||
|
val row = entityManager.find(TranslationCandidateEntity::class.java, candidateId, LockModeType.PESSIMISTIC_WRITE)
|
||||||
|
?: return null
|
||||||
|
entityManager.refresh(row, LockModeType.PESSIMISTIC_WRITE)
|
||||||
|
return row.domain()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun page(requestId: Long, afterId: Long, limit: Int): List<TranslationCandidate> = entityManager.createQuery(
|
||||||
|
"select c from TranslationCandidateEntity c where c.request.id = :requestId and c.id > :afterId order by c.id",
|
||||||
|
TranslationCandidateEntity::class.java
|
||||||
|
).setParameter("requestId", requestId).setParameter("afterId", afterId).setMaxResults(limit).resultList.map { it.domain() }
|
||||||
|
|
||||||
|
override fun progress(requestId: Long): SelectionCandidateProgress {
|
||||||
|
val counts = entityManager.createQuery(
|
||||||
|
"select new kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence.TranslationCandidateCount" +
|
||||||
|
"(c.status, count(c)) from TranslationCandidateEntity c where c.request.id = :requestId group by c.status",
|
||||||
|
TranslationCandidateCount::class.java
|
||||||
|
).setParameter("requestId", requestId).resultList.associate { it.status to it.count.toInt() }
|
||||||
|
val succeeded = counts[TranslationCandidateStatus.SUCCEEDED] ?: 0
|
||||||
|
val failed = counts[TranslationCandidateStatus.FAILED] ?: 0
|
||||||
|
return SelectionCandidateProgress(
|
||||||
|
counts.values.sum(),
|
||||||
|
succeeded + failed + (counts[TranslationCandidateStatus.DISCARDED] ?: 0),
|
||||||
|
succeeded,
|
||||||
|
failed
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun discard(requestId: Long, now: LocalDateTime) {
|
||||||
|
discardUnselected(requestId, emptySet(), now)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun discardUnselected(requestId: Long, selectedIds: Set<Long>, now: LocalDateTime): Int {
|
||||||
|
val rows = entityManager.createQuery(
|
||||||
|
"select c from TranslationCandidateEntity c where c.request.id = :requestId order by c.id",
|
||||||
|
TranslationCandidateEntity::class.java
|
||||||
|
).setParameter("requestId", requestId).setLockMode(LockModeType.PESSIMISTIC_WRITE).resultList
|
||||||
|
val discarded = rows.filter { it.id !in selectedIds }
|
||||||
|
discarded.forEach {
|
||||||
|
it.status = TranslationCandidateStatus.DISCARDED
|
||||||
|
it.translatedValues = null
|
||||||
|
it.failureCode = null
|
||||||
|
it.leaseUntil = null
|
||||||
|
it.fence++
|
||||||
|
it.updatedAt = now
|
||||||
|
it.terminalAt = now
|
||||||
|
}
|
||||||
|
entityManager.flush()
|
||||||
|
return discarded.size
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun completeLinked(source: TranslationCandidate) {
|
||||||
|
val rows = entityManager.createQuery(
|
||||||
|
"select c from TranslationCandidateEntity c where c.request.id = :requestId " +
|
||||||
|
"and c.linkedCandidateId = :sourceId order by c.id",
|
||||||
|
TranslationCandidateEntity::class.java
|
||||||
|
).setParameter("requestId", source.requestId).setParameter("sourceId", source.candidateId)
|
||||||
|
.setLockMode(LockModeType.PESSIMISTIC_WRITE).resultList
|
||||||
|
rows.forEach {
|
||||||
|
it.status = source.status
|
||||||
|
it.translatedValues = source.translatedValues
|
||||||
|
it.failureCode = source.failureCode
|
||||||
|
it.updatedAt = source.updatedAt
|
||||||
|
it.terminalAt = source.terminalAt
|
||||||
|
}
|
||||||
|
entityManager.flush()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun TranslationCandidateEntity.domain() = TranslationCandidate(
|
||||||
|
requireNotNull(id), requireNotNull(request.id), unit, ApprovalProvider(providerId, providerVersion), targetLanguage,
|
||||||
|
status, translatedValues, failureCode, attempt, leaseUntil, fence, createdAt, updatedAt, terminalAt, linkedCandidateId
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
data class TranslationCandidateCount(val status: TranslationCandidateStatus, val count: Long)
|
||||||
+73
@@ -0,0 +1,73 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionRequest
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationCandidateStore
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationSelectionStore
|
||||||
|
import org.springframework.stereotype.Component
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.time.ZoneOffset
|
||||||
|
import java.util.UUID
|
||||||
|
import javax.persistence.EntityManager
|
||||||
|
import javax.persistence.LockModeType
|
||||||
|
|
||||||
|
@Component
|
||||||
|
class JpaTranslationSelectionStore(
|
||||||
|
private val repository: TranslationSelectionRequestRepository,
|
||||||
|
private val entityManager: EntityManager,
|
||||||
|
private val candidates: TranslationCandidateStore
|
||||||
|
) : TranslationSelectionStore {
|
||||||
|
override fun find(requestId: Long): TranslationSelectionRequest? = repository.findById(requestId).orElse(null)?.domain()
|
||||||
|
|
||||||
|
override fun findCreation(adminId: Long, clientRequestId: UUID): TranslationSelectionRequest? =
|
||||||
|
repository.findByCreatedByAndClientRequestId(adminId, clientRequestId.toString())?.domain()
|
||||||
|
|
||||||
|
override fun lock(requestId: Long): TranslationSelectionRequest? {
|
||||||
|
val row = entityManager.find(TranslationSelectionRequestEntity::class.java, requestId, LockModeType.PESSIMISTIC_WRITE)
|
||||||
|
?: return null
|
||||||
|
entityManager.refresh(row, LockModeType.PESSIMISTIC_WRITE)
|
||||||
|
return row.domain()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun nextPreparation(now: LocalDateTime): TranslationSelectionRequest? = entityManager.createQuery(
|
||||||
|
"select r from TranslationSelectionRequestEntity r where r.status = :status " +
|
||||||
|
"and (r.preparationLeaseUntil is null or r.preparationLeaseUntil <= :now) order by r.id",
|
||||||
|
TranslationSelectionRequestEntity::class.java
|
||||||
|
).setParameter("status", TranslationSelectionStatus.PREPARING).setParameter("now", now)
|
||||||
|
.setLockMode(LockModeType.PESSIMISTIC_WRITE).setMaxResults(1).resultList.firstOrNull()
|
||||||
|
?.let { lock(requireNotNull(it.id)) }
|
||||||
|
|
||||||
|
override fun save(request: TranslationSelectionRequest): TranslationSelectionRequest {
|
||||||
|
val row = if (request.requestId == 0L) {
|
||||||
|
TranslationSelectionRequestEntity(
|
||||||
|
request.createdBy, request.clientRequestId.toString(), request.bodyHash, request.scope,
|
||||||
|
request.status, request.revision, request.snapshot, request.preparationAttempt,
|
||||||
|
request.preparationLeaseUntil, request.preparationFence, request.errorCode, request.createdAt, request.updatedAt
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
entityManager.getReference(TranslationSelectionRequestEntity::class.java, request.requestId).apply {
|
||||||
|
status = request.status
|
||||||
|
revision = request.revision
|
||||||
|
snapshot = request.snapshot
|
||||||
|
preparationAttempt = request.preparationAttempt
|
||||||
|
preparationLeaseUntil = request.preparationLeaseUntil
|
||||||
|
preparationFence = request.preparationFence
|
||||||
|
errorCode = request.errorCode
|
||||||
|
updatedAt = request.updatedAt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
row.acceptedGenerateRevision = request.acceptedGenerateRevision
|
||||||
|
row.applyOperationId = request.applyOperationId?.toString()
|
||||||
|
row.applyBodyHash = request.applyBodyHash
|
||||||
|
row.applyReceipt = request.applyReceipt
|
||||||
|
row.appliedAt = request.applyReceipt?.appliedAt?.atOffset(ZoneOffset.UTC)?.toLocalDateTime()
|
||||||
|
return repository.saveAndFlush(row).domain()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun TranslationSelectionRequestEntity.domain() = TranslationSelectionRequest(
|
||||||
|
requireNotNull(id), createdBy, UUID.fromString(clientRequestId), bodyHash, scope, status, revision, snapshot,
|
||||||
|
preparationAttempt, preparationLeaseUntil, preparationFence, errorCode, createdAt, updatedAt,
|
||||||
|
acceptedGenerateRevision, candidates.progress(requireNotNull(id)), applyOperationId?.let(UUID::fromString),
|
||||||
|
applyBodyHash, applyReceipt
|
||||||
|
)
|
||||||
|
}
|
||||||
+85
@@ -0,0 +1,85 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||||
|
import com.fasterxml.jackson.module.kotlin.readValue
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateFailure
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateStatus
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationGenerationUnit
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import javax.persistence.AttributeConverter
|
||||||
|
import javax.persistence.Column
|
||||||
|
import javax.persistence.Convert
|
||||||
|
import javax.persistence.Converter
|
||||||
|
import javax.persistence.Entity
|
||||||
|
import javax.persistence.EnumType
|
||||||
|
import javax.persistence.Enumerated
|
||||||
|
import javax.persistence.FetchType
|
||||||
|
import javax.persistence.GeneratedValue
|
||||||
|
import javax.persistence.GenerationType
|
||||||
|
import javax.persistence.Id
|
||||||
|
import javax.persistence.Index
|
||||||
|
import javax.persistence.JoinColumn
|
||||||
|
import javax.persistence.ManyToOne
|
||||||
|
import javax.persistence.Table
|
||||||
|
import javax.persistence.UniqueConstraint
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(
|
||||||
|
name = "translation_candidate",
|
||||||
|
uniqueConstraints = [
|
||||||
|
UniqueConstraint(
|
||||||
|
name = "uk_translation_candidate_unit",
|
||||||
|
columnNames = ["request_id", "unit_key", "provider_id", "target_language"]
|
||||||
|
)
|
||||||
|
],
|
||||||
|
indexes = [Index(name = "ix_translation_candidate_claim", columnList = "request_id,status,lease_until,id")]
|
||||||
|
)
|
||||||
|
class TranslationCandidateEntity(
|
||||||
|
@ManyToOne(fetch = FetchType.LAZY, optional = false)
|
||||||
|
@JoinColumn(name = "request_id", nullable = false)
|
||||||
|
val request: TranslationSelectionRequestEntity,
|
||||||
|
@Column(name = "unit_key", nullable = false, length = 160) val unitKey: String,
|
||||||
|
@Convert(converter = TranslationGenerationUnitConverter::class)
|
||||||
|
@Column(nullable = false, columnDefinition = "longtext")
|
||||||
|
val unit: TranslationGenerationUnit,
|
||||||
|
@Column(name = "provider_id", nullable = false, length = 20) val providerId: String,
|
||||||
|
@Column(nullable = false, length = 80) val providerVersion: String,
|
||||||
|
@Column(name = "target_language", nullable = false, length = 10) val targetLanguage: String,
|
||||||
|
@Enumerated(EnumType.STRING)
|
||||||
|
@Column(nullable = false, length = 16)
|
||||||
|
var status: TranslationCandidateStatus,
|
||||||
|
@Convert(converter = CandidateValuesConverter::class)
|
||||||
|
@Column(columnDefinition = "longtext")
|
||||||
|
var translatedValues: List<String>?,
|
||||||
|
@Enumerated(EnumType.STRING)
|
||||||
|
@Column(length = 40)
|
||||||
|
var failureCode: TranslationCandidateFailure?,
|
||||||
|
@Column(nullable = false) var attempt: Int,
|
||||||
|
@Column(name = "lease_until", columnDefinition = "timestamp") var leaseUntil: LocalDateTime?,
|
||||||
|
@Column(nullable = false) var fence: Long,
|
||||||
|
@Column(nullable = false, columnDefinition = "timestamp") val createdAt: LocalDateTime,
|
||||||
|
@Column(nullable = false, columnDefinition = "timestamp") var updatedAt: LocalDateTime,
|
||||||
|
@Column(columnDefinition = "timestamp") var terminalAt: LocalDateTime?,
|
||||||
|
val linkedCandidateId: Long?
|
||||||
|
) {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
var id: Long? = null
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TranslationCandidateRepository : JpaRepository<TranslationCandidateEntity, Long>
|
||||||
|
|
||||||
|
@Converter
|
||||||
|
class TranslationGenerationUnitConverter : AttributeConverter<TranslationGenerationUnit, String> {
|
||||||
|
override fun convertToDatabaseColumn(attribute: TranslationGenerationUnit): String = mapper.writeValueAsString(attribute)
|
||||||
|
override fun convertToEntityAttribute(dbData: String): TranslationGenerationUnit = mapper.readValue(dbData)
|
||||||
|
companion object { private val mapper = jacksonObjectMapper() }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Converter
|
||||||
|
class CandidateValuesConverter : AttributeConverter<List<String>?, String?> {
|
||||||
|
override fun convertToDatabaseColumn(attribute: List<String>?): String? = attribute?.let { mapper.writeValueAsString(it) }
|
||||||
|
override fun convertToEntityAttribute(dbData: String?): List<String>? = dbData?.let { mapper.readValue(it) }
|
||||||
|
companion object { private val mapper = jacksonObjectMapper() }
|
||||||
|
}
|
||||||
+98
@@ -0,0 +1,98 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
|
||||||
|
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||||
|
import com.fasterxml.jackson.module.kotlin.readValue
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.SelectionRequestSnapshot
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionError
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionScope
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionApplyReceipt
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import javax.persistence.AttributeConverter
|
||||||
|
import javax.persistence.Column
|
||||||
|
import javax.persistence.Convert
|
||||||
|
import javax.persistence.Converter
|
||||||
|
import javax.persistence.Entity
|
||||||
|
import javax.persistence.EnumType
|
||||||
|
import javax.persistence.Enumerated
|
||||||
|
import javax.persistence.GeneratedValue
|
||||||
|
import javax.persistence.GenerationType
|
||||||
|
import javax.persistence.Id
|
||||||
|
import javax.persistence.Index
|
||||||
|
import javax.persistence.Table
|
||||||
|
import javax.persistence.UniqueConstraint
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(
|
||||||
|
name = "translation_selection_request",
|
||||||
|
uniqueConstraints = [
|
||||||
|
UniqueConstraint(name = "uk_translation_selection_creation", columnNames = ["created_by", "client_request_id"])
|
||||||
|
],
|
||||||
|
indexes = [Index(name = "ix_translation_selection_prepare", columnList = "status,preparation_lease_until,id")]
|
||||||
|
)
|
||||||
|
class TranslationSelectionRequestEntity(
|
||||||
|
@Column(name = "created_by", nullable = false) val createdBy: Long,
|
||||||
|
@Column(name = "client_request_id", nullable = false, length = 36) val clientRequestId: String,
|
||||||
|
@Column(nullable = false, length = 64) val bodyHash: String,
|
||||||
|
@Convert(converter = SelectionScopeConverter::class)
|
||||||
|
@Column(nullable = false, columnDefinition = "text")
|
||||||
|
val scope: TranslationSelectionScope,
|
||||||
|
@Enumerated(EnumType.STRING)
|
||||||
|
@Column(nullable = false, length = 24)
|
||||||
|
var status: TranslationSelectionStatus,
|
||||||
|
@Column(nullable = false) var revision: Long,
|
||||||
|
@Convert(converter = SelectionSnapshotConverter::class)
|
||||||
|
@Column(nullable = false, columnDefinition = "longtext")
|
||||||
|
var snapshot: SelectionRequestSnapshot,
|
||||||
|
@Column(nullable = false) var preparationAttempt: Int,
|
||||||
|
@Column(name = "preparation_lease_until", columnDefinition = "timestamp") var preparationLeaseUntil: LocalDateTime?,
|
||||||
|
@Column(nullable = false) var preparationFence: Long,
|
||||||
|
@Enumerated(EnumType.STRING)
|
||||||
|
@Column(length = 40)
|
||||||
|
var errorCode: TranslationSelectionError?,
|
||||||
|
@Column(nullable = false, columnDefinition = "timestamp") val createdAt: LocalDateTime,
|
||||||
|
@Column(nullable = false, columnDefinition = "timestamp") var updatedAt: LocalDateTime
|
||||||
|
) {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
var id: Long? = null
|
||||||
|
|
||||||
|
var acceptedGenerateRevision: Long? = null
|
||||||
|
|
||||||
|
@Column(length = 36)
|
||||||
|
var applyOperationId: String? = null
|
||||||
|
|
||||||
|
@Column(length = 64)
|
||||||
|
var applyBodyHash: String? = null
|
||||||
|
|
||||||
|
@Column(columnDefinition = "longtext")
|
||||||
|
@Convert(converter = SelectionReceiptConverter::class)
|
||||||
|
var applyReceipt: TranslationSelectionApplyReceipt? = null
|
||||||
|
|
||||||
|
@Column(columnDefinition = "timestamp")
|
||||||
|
var appliedAt: LocalDateTime? = null
|
||||||
|
}
|
||||||
|
|
||||||
|
@Converter
|
||||||
|
class SelectionReceiptConverter : AttributeConverter<TranslationSelectionApplyReceipt?, String?> {
|
||||||
|
override fun convertToDatabaseColumn(attribute: TranslationSelectionApplyReceipt?): String? =
|
||||||
|
attribute?.let { mapper.writeValueAsString(it) }
|
||||||
|
override fun convertToEntityAttribute(dbData: String?): TranslationSelectionApplyReceipt? =
|
||||||
|
dbData?.let { mapper.readValue(it) }
|
||||||
|
companion object { private val mapper = jacksonObjectMapper().registerModule(JavaTimeModule()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Converter
|
||||||
|
class SelectionScopeConverter : AttributeConverter<TranslationSelectionScope, String> {
|
||||||
|
override fun convertToDatabaseColumn(attribute: TranslationSelectionScope): String = mapper.writeValueAsString(attribute)
|
||||||
|
override fun convertToEntityAttribute(dbData: String): TranslationSelectionScope = mapper.readValue(dbData)
|
||||||
|
companion object { private val mapper = jacksonObjectMapper() }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Converter
|
||||||
|
class SelectionSnapshotConverter : AttributeConverter<SelectionRequestSnapshot, String> {
|
||||||
|
override fun convertToDatabaseColumn(attribute: SelectionRequestSnapshot): String = mapper.writeValueAsString(attribute)
|
||||||
|
override fun convertToEntityAttribute(dbData: String): SelectionRequestSnapshot = mapper.readValue(dbData)
|
||||||
|
companion object { private val mapper = jacksonObjectMapper() }
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository
|
||||||
|
|
||||||
|
interface TranslationSelectionRequestRepository : JpaRepository<TranslationSelectionRequestEntity, Long> {
|
||||||
|
fun findByCreatedByAndClientRequestId(createdBy: Long, clientRequestId: String): TranslationSelectionRequestEntity?
|
||||||
|
}
|
||||||
+30
@@ -0,0 +1,30 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.scheduler
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.TranslationSelectionPreparationService
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.TranslationSelectionWorkerService
|
||||||
|
import org.redisson.api.RedissonClient
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled
|
||||||
|
import org.springframework.stereotype.Component
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
|
@Component
|
||||||
|
class TranslationSelectionWorker(
|
||||||
|
private val preparation: TranslationSelectionPreparationService,
|
||||||
|
private val candidates: TranslationSelectionWorkerService,
|
||||||
|
private val redisson: RedissonClient
|
||||||
|
) {
|
||||||
|
@Scheduled(fixedDelayString = "\${sodalive.translation-selection.fixed-delay-ms:1000}")
|
||||||
|
fun runPendingRequests() {
|
||||||
|
val lock = redisson.getLock("lock:translation-selection")
|
||||||
|
if (!lock.tryLock(0, 180, TimeUnit.SECONDS)) return
|
||||||
|
try {
|
||||||
|
try {
|
||||||
|
candidates.runNext()
|
||||||
|
} finally {
|
||||||
|
preparation.prepareNext()
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (lock.isHeldByCurrentThread) lock.unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+44
@@ -0,0 +1,44 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContentRepository
|
||||||
|
import kr.co.vividnext.sodalive.content.LanguageDetectionCacheService
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceSnapshot
|
||||||
|
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
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class ContentTranslationLanguageService(
|
||||||
|
private val repository: AudioContentRepository,
|
||||||
|
private val detector: LanguageDetectionCacheService,
|
||||||
|
private val writer: ContentTranslationWriteService,
|
||||||
|
private val invalidation: ContentTranslationSourceInvalidationService,
|
||||||
|
transactionManager: PlatformTransactionManager
|
||||||
|
) {
|
||||||
|
private val transaction = TransactionTemplate(transactionManager).apply {
|
||||||
|
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||||
|
fun detectMissing(contentId: Long, expectedQuery: String? = null): ContentSourceSnapshot? {
|
||||||
|
val before = transaction.execute {
|
||||||
|
repository.findById(contentId).orElse(null)?.toContentSourceSnapshot()
|
||||||
|
} ?: return null
|
||||||
|
if (!before.sourceLanguage.isNullOrBlank() || (expectedQuery != null && before.detectionQuery != expectedQuery)) {
|
||||||
|
return before
|
||||||
|
}
|
||||||
|
val detected = detector.detectWithCache(before.detectionQuery)?.detectedLanguage ?: return before
|
||||||
|
return transaction.execute {
|
||||||
|
val content = writer.lockContent(contentId) ?: return@execute null
|
||||||
|
val current = content.toContentSourceSnapshot()
|
||||||
|
if (current == before) {
|
||||||
|
content.languageCode = detected
|
||||||
|
invalidation.invalidateChanged(content, current)
|
||||||
|
}
|
||||||
|
content.toContentSourceSnapshot()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+87
@@ -0,0 +1,87 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContent
|
||||||
|
import kr.co.vividnext.sodalive.content.translation.ContentTranslationPayload
|
||||||
|
import kr.co.vividnext.sodalive.content.translation.ContentTranslationRepository
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationJobScheduler
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationLanguages
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceSnapshot
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.ContentTranslationOverrideStore
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
import org.springframework.transaction.annotation.Transactional
|
||||||
|
import javax.persistence.EntityManager
|
||||||
|
import javax.persistence.LockModeType
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class ContentTranslationSourceInvalidationService(
|
||||||
|
private val writer: ContentTranslationWriteService,
|
||||||
|
private val overrides: ContentTranslationOverrideStore,
|
||||||
|
private val translations: ContentTranslationRepository,
|
||||||
|
private val scheduler: TranslationJobScheduler,
|
||||||
|
private val entityManager: EntityManager
|
||||||
|
) {
|
||||||
|
@Transactional
|
||||||
|
fun lock(content: AudioContent) = writer.lockContent(content)
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
fun invalidateChanged(content: AudioContent, before: ContentSourceSnapshot) {
|
||||||
|
val after = content.toContentSourceSnapshot()
|
||||||
|
val changed = ContentSourceFieldKey.values().filter { before.fingerprint(it) != after.fingerprint(it) }.toSet()
|
||||||
|
if (changed.isEmpty()) return
|
||||||
|
val contentId = requireNotNull(content.id)
|
||||||
|
overrides.deleteFields(contentId, changed)
|
||||||
|
fallback(contentId, after, changed)
|
||||||
|
schedule(contentId, after, changed)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
fun invalidateStale(content: AudioContent) {
|
||||||
|
val contentId = requireNotNull(content.id)
|
||||||
|
val source = content.toContentSourceSnapshot()
|
||||||
|
val stale = overrides.findByContentId(contentId).filter {
|
||||||
|
it.approval.sourceFingerprint != source.fingerprint(it.approval.field)
|
||||||
|
}
|
||||||
|
stale.groupBy { it.locale }.forEach { (locale, rows) ->
|
||||||
|
val fields = rows.map { it.approval.field }.toSet()
|
||||||
|
overrides.deleteFields(contentId, fields, locale)
|
||||||
|
fallback(contentId, source, fields, locale)
|
||||||
|
schedule(contentId, source, fields)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun fallback(
|
||||||
|
contentId: Long,
|
||||||
|
source: ContentSourceSnapshot,
|
||||||
|
changed: Set<ContentSourceFieldKey>,
|
||||||
|
locale: String? = null
|
||||||
|
) {
|
||||||
|
translations.findByContentId(contentId).filter { locale == null || it.locale == locale }.forEach { row ->
|
||||||
|
entityManager.refresh(row, LockModeType.PESSIMISTIC_WRITE)
|
||||||
|
val previous = row.renderedPayload
|
||||||
|
row.renderedPayload = ContentTranslationPayload(
|
||||||
|
if (ContentSourceFieldKey.TITLE in changed) source.title else previous.title,
|
||||||
|
if (ContentSourceFieldKey.DETAIL in changed) source.detail else previous.detail,
|
||||||
|
if (ContentSourceFieldKey.TAGS in changed) source.tags.joinToString(",") else previous.tags
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun schedule(contentId: Long, source: ContentSourceSnapshot, changed: Set<ContentSourceFieldKey>) {
|
||||||
|
val language = source.sourceLanguage?.takeIf { TranslationLanguages.isSupported(it) } ?: return
|
||||||
|
TranslationLanguages.getTranslatableLanguageCodes(language).forEach { target ->
|
||||||
|
source.fields.filter { field -> changed.any { it.key == field.fieldKey } }.forEach { field ->
|
||||||
|
scheduler.scheduleMissingTranslation(
|
||||||
|
LanguageTranslationTargetType.CONTENT,
|
||||||
|
contentId,
|
||||||
|
field.fieldKey,
|
||||||
|
field.sourceText,
|
||||||
|
language,
|
||||||
|
target,
|
||||||
|
refreshCompletedContentField = true
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+54
@@ -0,0 +1,54 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContent
|
||||||
|
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.v2.contenttranslation.domain.ContentSourceSnapshot
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionScope
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
import javax.persistence.EntityManager
|
||||||
|
import javax.persistence.LockModeType
|
||||||
|
|
||||||
|
fun AudioContent.toContentSourceSnapshot(): ContentSourceSnapshot = ContentSourceSnapshot(
|
||||||
|
sourceLanguage = languageCode,
|
||||||
|
title = title,
|
||||||
|
detail = detail,
|
||||||
|
tags = audioContentHashTags
|
||||||
|
.filter { it.isActive }
|
||||||
|
.sortedWith(compareBy(nullsLast()) { it.id })
|
||||||
|
.mapNotNull { it.hashTag?.tag }
|
||||||
|
)
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class ContentTranslationSourceService(
|
||||||
|
private val entityManager: EntityManager,
|
||||||
|
private val writer: ContentTranslationWriteService
|
||||||
|
) {
|
||||||
|
// Caller owns the request transaction; membership mutation uses the same Series parent lock.
|
||||||
|
fun lockScope(scope: TranslationSelectionScope): List<AudioContent>? = when (scope) {
|
||||||
|
is TranslationSelectionScope.Content -> writer.lockContent(scope.contentId)?.let { listOf(it) }
|
||||||
|
is TranslationSelectionScope.Series -> {
|
||||||
|
val series = entityManager.find(Series::class.java, scope.seriesId, LockModeType.PESSIMISTIC_WRITE)
|
||||||
|
if (series == null) {
|
||||||
|
null
|
||||||
|
} else {
|
||||||
|
entityManager.refresh(series, LockModeType.PESSIMISTIC_WRITE)
|
||||||
|
if (!series.isActive) {
|
||||||
|
null
|
||||||
|
} else {
|
||||||
|
linkedContentIds(scope.seriesId, true).mapNotNull(writer::lockContent).filter { it.isActive }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun linkedContentIds(seriesId: Long, lock: Boolean = false): List<Long> {
|
||||||
|
val query = entityManager.createQuery(
|
||||||
|
"select s from SeriesContent s where s.series.id = :id order by s.content.id",
|
||||||
|
SeriesContent::class.java
|
||||||
|
).setParameter("id", seriesId)
|
||||||
|
if (lock) query.setLockMode(LockModeType.PESSIMISTIC_WRITE)
|
||||||
|
return query.resultList.onEach { if (lock) entityManager.refresh(it, LockModeType.PESSIMISTIC_WRITE) }
|
||||||
|
.map { requireNotNull(it.content?.id) }.distinct().sorted()
|
||||||
|
}
|
||||||
|
}
|
||||||
+137
@@ -0,0 +1,137 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.common.SodaException
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContent
|
||||||
|
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.i18n.translation.SourceTextNormalizer
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationCoordinator
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentFieldApproval
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceSnapshot
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentTranslationOverride
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.ContentTranslationOverrideStore
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
import org.springframework.transaction.annotation.Transactional
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.time.ZoneOffset
|
||||||
|
import java.time.temporal.ChronoUnit
|
||||||
|
import javax.persistence.EntityManager
|
||||||
|
import javax.persistence.LockModeType
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class ContentTranslationWriteService(
|
||||||
|
private val entityManager: EntityManager,
|
||||||
|
private val translations: ContentTranslationRepository,
|
||||||
|
private val overrides: ContentTranslationOverrideStore,
|
||||||
|
private val coordinator: TranslationCoordinator,
|
||||||
|
private val nowProvider: () -> LocalDateTime = { LocalDateTime.now(ZoneOffset.UTC) }
|
||||||
|
) {
|
||||||
|
@Transactional
|
||||||
|
fun lockContent(contentId: Long): AudioContent? {
|
||||||
|
val content = entityManager.find(AudioContent::class.java, contentId, LockModeType.PESSIMISTIC_WRITE) ?: return null
|
||||||
|
lockContent(content)
|
||||||
|
return content
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
fun lockContent(content: AudioContent) {
|
||||||
|
entityManager.refresh(content, LockModeType.PESSIMISTIC_WRITE)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
fun hasCurrentApproval(contentId: Long, locale: String, fieldKey: String): Boolean {
|
||||||
|
val source = lockContent(contentId)?.toContentSourceSnapshot() ?: return false
|
||||||
|
return overrides.findByContentId(contentId).any {
|
||||||
|
it.locale == locale.lowercase() && it.approval.field.key == fieldKey &&
|
||||||
|
it.approval.sourceFingerprint == source.fingerprint(it.approval.field)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
fun approve(contentId: Long, locale: String, approvals: List<ContentFieldApproval>): List<ContentTranslationOverride> =
|
||||||
|
approve(contentId, locale, approvals, nowProvider())
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
fun approve(
|
||||||
|
contentId: Long,
|
||||||
|
locale: String,
|
||||||
|
approvals: List<ContentFieldApproval>,
|
||||||
|
approvedAt: LocalDateTime
|
||||||
|
): List<ContentTranslationOverride> {
|
||||||
|
val content = lockContent(contentId) ?: throw SodaException(messageKey = "admin.content.not_found")
|
||||||
|
val source = content.toContentSourceSnapshot()
|
||||||
|
if (approvals.any { it.sourceFingerprint != source.fingerprint(it.field) }) {
|
||||||
|
throw SodaException(messageKey = "common.error.invalid_request", errorProperty = "SOURCE_CHANGED")
|
||||||
|
}
|
||||||
|
require(approvals.isNotEmpty() && approvals.map { it.field }.distinct().size == approvals.size)
|
||||||
|
val target = locale.lowercase()
|
||||||
|
val now = approvedAt.truncatedTo(ChronoUnit.SECONDS)
|
||||||
|
val approved = approvals.map { ContentTranslationOverride(contentId, target, it, now) }
|
||||||
|
approved.forEach(overrides::save)
|
||||||
|
merge(contentId, target, source, approvals.associate { it.field to it.translatedText })
|
||||||
|
return approved
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
fun materialize(contentId: Long, locale: String): Boolean =
|
||||||
|
materialize(contentId, locale, ContentSourceFieldKey.values().toSet())
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
fun materialize(contentId: Long, locale: String, fields: Set<ContentSourceFieldKey>): Boolean {
|
||||||
|
val content = lockContent(contentId) ?: return false
|
||||||
|
val source = content.toContentSourceSnapshot()
|
||||||
|
val language = source.sourceLanguage?.takeIf { it.isNotBlank() } ?: return false
|
||||||
|
val target = locale.lowercase()
|
||||||
|
val selected = if (translations.findByContentIdAndLocaleForUpdate(contentId, target) == null) {
|
||||||
|
ContentSourceFieldKey.values().toSet()
|
||||||
|
} else {
|
||||||
|
fields
|
||||||
|
}
|
||||||
|
val approved = overrides.findByContentId(contentId).filter {
|
||||||
|
it.locale == target && it.approval.sourceFingerprint == source.fingerprint(it.approval.field)
|
||||||
|
}.associate { it.approval.field to it.approval.translatedText }
|
||||||
|
val values = mutableMapOf<ContentSourceFieldKey, String>()
|
||||||
|
selected.forEach { field ->
|
||||||
|
val raw = source.fields.single { it.fieldKey == field.key }.sourceText
|
||||||
|
values[field] = approved[field] ?: if (SourceTextNormalizer.normalize(raw).isBlank()) {
|
||||||
|
""
|
||||||
|
} else {
|
||||||
|
coordinator.findCached(
|
||||||
|
SourceTextNormalizer.hash(raw),
|
||||||
|
language.lowercase(),
|
||||||
|
target,
|
||||||
|
rejectUnchangedAwsResult = field == ContentSourceFieldKey.TAGS
|
||||||
|
)?.translatedText ?: return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
merge(contentId, target, source, values)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun merge(
|
||||||
|
contentId: Long,
|
||||||
|
locale: String,
|
||||||
|
source: ContentSourceSnapshot,
|
||||||
|
values: Map<ContentSourceFieldKey, String>
|
||||||
|
) {
|
||||||
|
val existing = translations.findByContentIdAndLocaleForUpdate(contentId, locale)
|
||||||
|
if (existing != null) entityManager.refresh(existing, LockModeType.PESSIMISTIC_WRITE)
|
||||||
|
val previous = existing?.renderedPayload ?: ContentTranslationPayload(
|
||||||
|
source.title,
|
||||||
|
source.detail,
|
||||||
|
source.tags.joinToString(",")
|
||||||
|
)
|
||||||
|
val payload = ContentTranslationPayload(
|
||||||
|
values[ContentSourceFieldKey.TITLE] ?: previous.title,
|
||||||
|
values[ContentSourceFieldKey.DETAIL] ?: previous.detail,
|
||||||
|
values[ContentSourceFieldKey.TAGS] ?: previous.tags
|
||||||
|
)
|
||||||
|
if (existing == null) {
|
||||||
|
translations.save(ContentTranslation(contentId, locale, payload))
|
||||||
|
} else {
|
||||||
|
existing.renderedPayload = payload
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+203
@@ -0,0 +1,203 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationLanguages
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ApprovalProvenance
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentFieldApproval
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceSnapshot
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidate
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateStatus
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationGenerationUnit
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionError
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionRequest
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationUnitKind
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.ApplyTranslationSelectionRequest
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionAppliedField
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionApplyReceipt
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionChoice
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionField
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationCandidateStore
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationSelectionStore
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
import org.springframework.transaction.annotation.Transactional
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.time.ZoneOffset
|
||||||
|
import java.time.temporal.ChronoUnit
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class TranslationSelectionApplyService(
|
||||||
|
private val requests: TranslationSelectionStore,
|
||||||
|
private val candidates: TranslationCandidateStore,
|
||||||
|
private val sources: ContentTranslationSourceService,
|
||||||
|
private val writer: ContentTranslationWriteService,
|
||||||
|
private val nowProvider: () -> LocalDateTime = { LocalDateTime.now(ZoneOffset.UTC) }
|
||||||
|
) {
|
||||||
|
@Transactional
|
||||||
|
fun apply(requestId: Long, adminId: Long, body: ApplyTranslationSelectionRequest): TranslationSelectionApplyReceipt {
|
||||||
|
val request = requests.lock(requestId) ?: throw TranslationSelectionError.RESOURCE_NOT_FOUND.exception()
|
||||||
|
val hash = body.canonicalHash()
|
||||||
|
if (adminId <= 0) throw TranslationSelectionError.INVALID_REQUEST.exception()
|
||||||
|
if (request.applyOperationId == body.operationId) {
|
||||||
|
if (request.applyBodyHash != hash) throw TranslationSelectionError.IDEMPOTENCY_CONFLICT.exception()
|
||||||
|
return requireNotNull(request.applyReceipt)
|
||||||
|
}
|
||||||
|
if (request.status.isTerminal()) throw TranslationSelectionError.REQUEST_CLOSED.exception()
|
||||||
|
if (request.status != TranslationSelectionStatus.READY) throw TranslationSelectionError.REQUEST_NOT_READY.exception()
|
||||||
|
if (request.revision != body.expectedRevision) throw TranslationSelectionError.REVISION_CONFLICT.exception()
|
||||||
|
if (body.selections.isEmpty()) throw TranslationSelectionError.INVALID_REQUEST.exception()
|
||||||
|
val current = sources.lockScope(request.scope).orEmpty()
|
||||||
|
.associate { requireNotNull(it.id) to it.toContentSourceSnapshot() }
|
||||||
|
if (body.selections.any { it is TranslationSelectionChoice.TitleGroup } &&
|
||||||
|
current.keys != request.snapshot.memberContentIds.toSet()
|
||||||
|
) {
|
||||||
|
throw TranslationSelectionError.MEMBERSHIP_CHANGED.exception()
|
||||||
|
}
|
||||||
|
val fields = body.selections.flatMap { choice ->
|
||||||
|
when (choice) {
|
||||||
|
is TranslationSelectionChoice.Field -> listOf(field(request, choice))
|
||||||
|
is TranslationSelectionChoice.TitleGroup -> group(request, choice)
|
||||||
|
}
|
||||||
|
}.sortedWith(compareBy({ it.contentId }, { it.targetLanguage }, { it.field.key }))
|
||||||
|
if (fields.map { Triple(it.contentId, it.field, it.targetLanguage) }.distinct().size != fields.size) {
|
||||||
|
throw TranslationSelectionError.INVALID_REQUEST.exception()
|
||||||
|
}
|
||||||
|
fields.forEach { field -> validateSource(request, field, current[field.contentId]) }
|
||||||
|
val now = nowProvider().truncatedTo(ChronoUnit.SECONDS)
|
||||||
|
fields.groupBy { it.contentId to it.targetLanguage }.forEach { (target, selected) ->
|
||||||
|
writer.approve(
|
||||||
|
target.first,
|
||||||
|
target.second,
|
||||||
|
selected.map {
|
||||||
|
val key = ContentSourceFieldKey.values().single { key -> key.key == it.field.key }
|
||||||
|
ContentFieldApproval(
|
||||||
|
key,
|
||||||
|
current.getValue(it.contentId).fingerprint(key),
|
||||||
|
it.translatedValue,
|
||||||
|
ApprovalProvenance(requestId, it.candidateIds, it.providers, adminId)
|
||||||
|
)
|
||||||
|
},
|
||||||
|
now
|
||||||
|
)
|
||||||
|
}
|
||||||
|
val discarded = candidates.discardUnselected(requestId, fields.flatMap { it.candidateIds }.toSet(), now)
|
||||||
|
val receipt = TranslationSelectionApplyReceipt(
|
||||||
|
requestId,
|
||||||
|
body.operationId,
|
||||||
|
TranslationSelectionStatus.APPLIED,
|
||||||
|
now.toInstant(ZoneOffset.UTC),
|
||||||
|
fields,
|
||||||
|
discarded
|
||||||
|
)
|
||||||
|
requests.save(
|
||||||
|
request.copy(
|
||||||
|
status = TranslationSelectionStatus.APPLIED,
|
||||||
|
revision = request.revision + 1,
|
||||||
|
updatedAt = now,
|
||||||
|
applyOperationId = body.operationId,
|
||||||
|
applyBodyHash = hash,
|
||||||
|
applyReceipt = receipt
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return receipt
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun field(
|
||||||
|
request: TranslationSelectionRequest,
|
||||||
|
choice: TranslationSelectionChoice.Field
|
||||||
|
): TranslationSelectionAppliedField {
|
||||||
|
if (choice.contentId <= 0 || choice.candidateId <= 0 ||
|
||||||
|
(
|
||||||
|
choice.field == TranslationSelectionField.TITLE &&
|
||||||
|
request.snapshot.grouping?.groups.orEmpty().any { choice.contentId in it.memberContentIds }
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
throw TranslationSelectionError.INVALID_REQUEST.exception()
|
||||||
|
}
|
||||||
|
val unit = request.snapshot.generationUnits.singleOrNull {
|
||||||
|
it.kind == TranslationUnitKind.FIELD && it.contentId == choice.contentId && it.field?.key == choice.field.key
|
||||||
|
} ?: throw TranslationSelectionError.CANDIDATE_NOT_SELECTABLE.exception()
|
||||||
|
val selected = candidate(request, choice.candidateId, unit to choice.targetLanguage)
|
||||||
|
return TranslationSelectionAppliedField(
|
||||||
|
choice.contentId,
|
||||||
|
choice.field,
|
||||||
|
choice.targetLanguage,
|
||||||
|
requireNotNull(selected.translatedValues).single(),
|
||||||
|
listOf(selected.candidateId),
|
||||||
|
listOf(selected.provider)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun group(
|
||||||
|
request: TranslationSelectionRequest,
|
||||||
|
choice: TranslationSelectionChoice.TitleGroup
|
||||||
|
): List<TranslationSelectionAppliedField> {
|
||||||
|
val grouping = requireNotNull(request.snapshot.grouping)
|
||||||
|
val group = grouping.groups.singleOrNull { it.groupId == choice.groupId }
|
||||||
|
?: throw TranslationSelectionError.INCOMPLETE_TITLE_GROUP.exception()
|
||||||
|
if (choice.residualChoices.map { it.contentId }.sorted() != group.memberContentIds.sorted()) {
|
||||||
|
throw TranslationSelectionError.INCOMPLETE_TITLE_GROUP.exception()
|
||||||
|
}
|
||||||
|
val commonUnit = request.snapshot.generationUnits.single {
|
||||||
|
it.kind == TranslationUnitKind.GROUP_COMMON && it.groupId == group.groupId
|
||||||
|
}
|
||||||
|
val common = candidate(request, choice.commonCandidateId, commonUnit to choice.targetLanguage)
|
||||||
|
return choice.residualChoices.map { residualChoice ->
|
||||||
|
val template = grouping.templates.single { it.contentId == residualChoice.contentId }.template()
|
||||||
|
val residual = if (template.residualInputs.isEmpty()) {
|
||||||
|
if (residualChoice.candidateId != null) throw TranslationSelectionError.INCOMPLETE_TITLE_GROUP.exception()
|
||||||
|
null
|
||||||
|
} else {
|
||||||
|
val id = residualChoice.candidateId ?: throw TranslationSelectionError.INCOMPLETE_TITLE_GROUP.exception()
|
||||||
|
val residualUnit = request.snapshot.generationUnits.single {
|
||||||
|
it.kind == TranslationUnitKind.TITLE_RESIDUAL && it.groupId == group.groupId &&
|
||||||
|
it.contentId == residualChoice.contentId
|
||||||
|
}
|
||||||
|
candidate(request, id, residualUnit to choice.targetLanguage)
|
||||||
|
}
|
||||||
|
val contributing = listOfNotNull(common, residual)
|
||||||
|
TranslationSelectionAppliedField(
|
||||||
|
residualChoice.contentId,
|
||||||
|
TranslationSelectionField.TITLE,
|
||||||
|
choice.targetLanguage,
|
||||||
|
template.render(requireNotNull(common.translatedValues), residual?.translatedValues.orEmpty()),
|
||||||
|
contributing.map { it.candidateId },
|
||||||
|
contributing.map { it.provider }.distinct()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun candidate(
|
||||||
|
request: TranslationSelectionRequest,
|
||||||
|
candidateId: Long,
|
||||||
|
target: Pair<TranslationGenerationUnit, String>
|
||||||
|
): TranslationCandidate {
|
||||||
|
val (unit, language) = target
|
||||||
|
if (candidateId <= 0 || language !in TranslationLanguages.getTranslatableLanguageCodes(null)) {
|
||||||
|
throw TranslationSelectionError.INVALID_REQUEST.exception()
|
||||||
|
}
|
||||||
|
val candidate = candidates.lock(candidateId) ?: throw TranslationSelectionError.CANDIDATE_NOT_SELECTABLE.exception()
|
||||||
|
if (request.acceptedGenerateRevision == null || candidate.requestId != request.requestId ||
|
||||||
|
candidate.status != TranslationCandidateStatus.SUCCEEDED || candidate.unit != unit ||
|
||||||
|
candidate.provider !in request.snapshot.providerManifest || candidate.targetLanguage != language ||
|
||||||
|
language !in request.snapshot.targetLanguages ||
|
||||||
|
language !in TranslationLanguages.getTranslatableLanguageCodes(unit.sourceLanguage) ||
|
||||||
|
candidate.translatedValues?.size != unit.inputValues.size || candidate.translatedValues.any { it.isBlank() }
|
||||||
|
) {
|
||||||
|
throw TranslationSelectionError.CANDIDATE_NOT_SELECTABLE.exception()
|
||||||
|
}
|
||||||
|
return candidate
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun validateSource(
|
||||||
|
request: TranslationSelectionRequest,
|
||||||
|
field: TranslationSelectionAppliedField,
|
||||||
|
current: ContentSourceSnapshot?
|
||||||
|
) {
|
||||||
|
val frozen = request.snapshot.contents.singleOrNull { it.contentId == field.contentId }
|
||||||
|
val key = ContentSourceFieldKey.values().single { it.key == field.field.key }
|
||||||
|
if (current == null || frozen == null || current.fingerprint(key) != frozen.source().fingerprint(key)) {
|
||||||
|
throw TranslationSelectionError.SOURCE_CHANGED.exception()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+38
@@ -0,0 +1,38 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionError
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationCandidatesResponse
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.response
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationCandidateStore
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationSelectionStore
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
import org.springframework.transaction.annotation.Transactional
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class TranslationSelectionCandidateQueryService(
|
||||||
|
private val requests: TranslationSelectionStore,
|
||||||
|
private val candidates: TranslationCandidateStore
|
||||||
|
) {
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
fun page(requestId: Long, afterCandidateId: Long?, size: Int): TranslationCandidatesResponse {
|
||||||
|
if (size !in 1..100 || (afterCandidateId != null && afterCandidateId <= 0)) {
|
||||||
|
throw TranslationSelectionError.INVALID_REQUEST.exception()
|
||||||
|
}
|
||||||
|
val request = requests.find(requestId) ?: throw TranslationSelectionError.RESOURCE_NOT_FOUND.exception()
|
||||||
|
if (request.status in setOf(
|
||||||
|
TranslationSelectionStatus.PREPARING,
|
||||||
|
TranslationSelectionStatus.REVIEW_REQUIRED,
|
||||||
|
TranslationSelectionStatus.READY_TO_GENERATE
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
throw TranslationSelectionError.REQUEST_NOT_READY.exception()
|
||||||
|
}
|
||||||
|
val page = candidates.page(requestId, afterCandidateId ?: 0, size + 1)
|
||||||
|
val items = page.take(size)
|
||||||
|
return TranslationCandidatesResponse(
|
||||||
|
items.map { it.response() },
|
||||||
|
if (page.size > size) items.last().candidateId else null
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
+120
@@ -0,0 +1,120 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.content.translation.ContentTranslationRepository
|
||||||
|
import kr.co.vividnext.sodalive.creator.admin.content.series.Series
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationLanguages
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.SelectionContentSource
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.SelectionFieldValues
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionError
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionScope
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.selectionFingerprints
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.SelectionApprovalOrigins
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.SelectionApprovalProvenances
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionApprovalOrigin
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionApprovalProvenance
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionContentResponse
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionContentsResponse
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionCurrentTranslation
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionField
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.ContentTranslationOverrideStore
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationSelectionStore
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
import org.springframework.transaction.annotation.Transactional
|
||||||
|
import java.time.ZoneOffset
|
||||||
|
import javax.persistence.EntityManager
|
||||||
|
import javax.persistence.LockModeType
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class TranslationSelectionContentsService(
|
||||||
|
private val store: TranslationSelectionStore,
|
||||||
|
private val sources: ContentTranslationSourceService,
|
||||||
|
private val writer: ContentTranslationWriteService,
|
||||||
|
private val translations: ContentTranslationRepository,
|
||||||
|
private val overrides: ContentTranslationOverrideStore,
|
||||||
|
private val entityManager: EntityManager
|
||||||
|
) {
|
||||||
|
@Transactional
|
||||||
|
fun contents(requestId: Long, afterContentId: Long?, size: Int): TranslationSelectionContentsResponse {
|
||||||
|
if (size !in 1..100 || (afterContentId != null && afterContentId <= 0)) {
|
||||||
|
throw TranslationSelectionError.INVALID_REQUEST.exception()
|
||||||
|
}
|
||||||
|
val request = store.find(requestId) ?: throw TranslationSelectionError.RESOURCE_NOT_FOUND.exception()
|
||||||
|
val page = request.snapshot.contents.filter { it.contentId > (afterContentId ?: 0) }
|
||||||
|
.sortedBy { it.contentId }.take(size + 1)
|
||||||
|
val linked = when (val scope = request.scope) {
|
||||||
|
is TranslationSelectionScope.Content -> null
|
||||||
|
is TranslationSelectionScope.Series -> if (entityManager.find(Series::class.java, scope.seriesId)?.isActive == true) {
|
||||||
|
sources.linkedContentIds(scope.seriesId).toSet()
|
||||||
|
} else {
|
||||||
|
emptySet()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val items = page.take(size).map { source -> item(source, linked) }
|
||||||
|
return TranslationSelectionContentsResponse(items, if (page.size > size) items.last().contentId else null)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun item(source: SelectionContentSource, linked: Set<Long>?): TranslationSelectionContentResponse {
|
||||||
|
val content = writer.lockContent(source.contentId)
|
||||||
|
val current = content?.toContentSourceSnapshot()
|
||||||
|
val active = content?.isActive == true
|
||||||
|
val approved = if (current == null) {
|
||||||
|
emptyList()
|
||||||
|
} else {
|
||||||
|
overrides.findByContentId(source.contentId).filter {
|
||||||
|
it.approval.sourceFingerprint == current.fingerprint(it.approval.field)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val currentTranslations = TranslationLanguages.getTranslatableLanguageCodes(source.sourceLanguage).map { locale ->
|
||||||
|
val row = translations.findByContentIdAndLocaleForUpdate(source.contentId, locale)
|
||||||
|
if (row != null) entityManager.refresh(row, LockModeType.PESSIMISTIC_WRITE)
|
||||||
|
val fallback = current ?: source.source()
|
||||||
|
val fields = row?.renderedPayload?.let { SelectionFieldValues(it.title, it.detail, it.tags) }
|
||||||
|
?: SelectionFieldValues(fallback.title, fallback.detail, fallback.tags.joinToString(","))
|
||||||
|
val provenance = ContentSourceFieldKey.values().associateWith { field ->
|
||||||
|
approved.singleOrNull { it.locale == locale && it.approval.field == field }?.let {
|
||||||
|
val origin = it.approval.provenance
|
||||||
|
TranslationSelectionApprovalProvenance(
|
||||||
|
origin.requestId,
|
||||||
|
origin.candidateIds,
|
||||||
|
origin.providers,
|
||||||
|
origin.approvedBy,
|
||||||
|
it.approvedAt.toInstant(ZoneOffset.UTC)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fun origin(field: ContentSourceFieldKey) = if (provenance[field] == null) {
|
||||||
|
TranslationSelectionApprovalOrigin.UNKNOWN
|
||||||
|
} else {
|
||||||
|
TranslationSelectionApprovalOrigin.ADMIN
|
||||||
|
}
|
||||||
|
TranslationSelectionCurrentTranslation(
|
||||||
|
locale,
|
||||||
|
fields,
|
||||||
|
SelectionApprovalOrigins(
|
||||||
|
origin(ContentSourceFieldKey.TITLE),
|
||||||
|
origin(ContentSourceFieldKey.DETAIL),
|
||||||
|
origin(ContentSourceFieldKey.TAGS)
|
||||||
|
),
|
||||||
|
SelectionApprovalProvenances(
|
||||||
|
provenance[ContentSourceFieldKey.TITLE],
|
||||||
|
provenance[ContentSourceFieldKey.DETAIL],
|
||||||
|
provenance[ContentSourceFieldKey.TAGS]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return TranslationSelectionContentResponse(
|
||||||
|
source.contentId,
|
||||||
|
active,
|
||||||
|
content != null && (linked == null || (active && source.contentId in linked)),
|
||||||
|
requireNotNull(source.sourceLanguage),
|
||||||
|
SelectionFieldValues(source.title, source.detail, source.tags.joinToString(",")),
|
||||||
|
source.sourceFingerprints,
|
||||||
|
current?.selectionFingerprints(),
|
||||||
|
source.source().fields.filter { it.sourceText.isBlank() }.map { field ->
|
||||||
|
TranslationSelectionField.values().single { it.key == field.fieldKey }
|
||||||
|
},
|
||||||
|
currentTranslations
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
+128
@@ -0,0 +1,128 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationLanguages
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationProvider
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ApprovalProvider
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TitleSegmentKind
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidate
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationGenerationUnit
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionError
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionRequest
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationUnitKind
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationCandidateStore
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationSelectionStore
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
import org.springframework.transaction.annotation.Transactional
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.time.ZoneOffset
|
||||||
|
import java.time.temporal.ChronoUnit
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class TranslationSelectionGenerationService(
|
||||||
|
private val requests: TranslationSelectionStore,
|
||||||
|
private val candidates: TranslationCandidateStore,
|
||||||
|
private val providers: List<TranslationProvider>,
|
||||||
|
private val nowProvider: () -> LocalDateTime = { LocalDateTime.now(ZoneOffset.UTC) }
|
||||||
|
) {
|
||||||
|
@Transactional
|
||||||
|
fun generate(requestId: Long, expectedRevision: Long): TranslationSelectionRequest {
|
||||||
|
val request = requests.lock(requestId) ?: throw TranslationSelectionError.RESOURCE_NOT_FOUND.exception()
|
||||||
|
if (request.acceptedGenerateRevision == expectedRevision) return request
|
||||||
|
if (request.status.isTerminal()) throw TranslationSelectionError.REQUEST_CLOSED.exception()
|
||||||
|
if (!request.snapshot.reviewed) throw TranslationSelectionError.REVIEW_REQUIRED.exception()
|
||||||
|
if (request.revision != expectedRevision) throw TranslationSelectionError.REVISION_CONFLICT.exception()
|
||||||
|
if (request.status != TranslationSelectionStatus.READY_TO_GENERATE) {
|
||||||
|
throw TranslationSelectionError.REQUEST_NOT_READY.exception()
|
||||||
|
}
|
||||||
|
val now = nowProvider().truncatedTo(ChronoUnit.SECONDS)
|
||||||
|
val units = units(request)
|
||||||
|
val manifest = providers.map { ApprovalProvider(it.providerId.code, it.providerVersion) }.sortedBy { it.id }
|
||||||
|
val inputs = mutableMapOf<ProviderInput, Long>()
|
||||||
|
units.forEach { unit ->
|
||||||
|
manifest.forEach { provider ->
|
||||||
|
TranslationLanguages.getTranslatableLanguageCodes(unit.sourceLanguage).forEach { language ->
|
||||||
|
val input = ProviderInput(provider, language, unit.sourceLanguage, unit.kind, unit.field, unit.inputValues)
|
||||||
|
val candidate = candidates.save(
|
||||||
|
TranslationCandidate(
|
||||||
|
requestId = requestId,
|
||||||
|
unit = unit,
|
||||||
|
provider = provider,
|
||||||
|
targetLanguage = language,
|
||||||
|
createdAt = now,
|
||||||
|
linkedCandidateId = inputs[input]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
inputs.putIfAbsent(input, candidate.candidateId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return requests.save(
|
||||||
|
request.copy(
|
||||||
|
status = if (candidates.progress(requestId).total == 0) {
|
||||||
|
TranslationSelectionStatus.READY
|
||||||
|
} else {
|
||||||
|
TranslationSelectionStatus.GENERATING
|
||||||
|
},
|
||||||
|
revision = request.revision + 1,
|
||||||
|
acceptedGenerateRevision = expectedRevision,
|
||||||
|
updatedAt = now,
|
||||||
|
snapshot = request.snapshot.copy(generationUnits = units, providerManifest = manifest)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private data class ProviderInput(
|
||||||
|
val provider: ApprovalProvider,
|
||||||
|
val target: String,
|
||||||
|
val source: String,
|
||||||
|
val kind: TranslationUnitKind,
|
||||||
|
val field: ContentSourceFieldKey?,
|
||||||
|
val values: List<String>
|
||||||
|
)
|
||||||
|
|
||||||
|
private fun units(request: TranslationSelectionRequest): List<TranslationGenerationUnit> {
|
||||||
|
val grouping = requireNotNull(request.snapshot.grouping)
|
||||||
|
val grouped = grouping.groups.flatMap { it.memberContentIds }.toSet()
|
||||||
|
val fields = request.snapshot.contents.flatMap { content ->
|
||||||
|
content.source().fields.filter {
|
||||||
|
it.sourceText.isNotBlank() && (it.fieldKey != "title" || content.contentId !in grouped)
|
||||||
|
}.map {
|
||||||
|
TranslationGenerationUnit(
|
||||||
|
"field:${content.contentId}:${it.fieldKey}",
|
||||||
|
TranslationUnitKind.FIELD,
|
||||||
|
requireNotNull(content.sourceLanguage),
|
||||||
|
listOf(it.sourceText),
|
||||||
|
content.contentId,
|
||||||
|
ContentSourceFieldKey.values().single { field -> field.key == it.fieldKey }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val common = grouping.groups.mapIndexed { index, group ->
|
||||||
|
TranslationGenerationUnit(
|
||||||
|
"common:$index",
|
||||||
|
TranslationUnitKind.GROUP_COMMON,
|
||||||
|
group.sourceLanguage,
|
||||||
|
group.sharedParts.map { it.sourceText },
|
||||||
|
groupId = group.groupId
|
||||||
|
)
|
||||||
|
}
|
||||||
|
val residuals = grouping.templates.mapNotNull { template ->
|
||||||
|
val values = template.segments.filter { it.kind == TitleSegmentKind.RESIDUAL }.map { it.sourceText }
|
||||||
|
if (values.isEmpty()) {
|
||||||
|
null
|
||||||
|
} else {
|
||||||
|
TranslationGenerationUnit(
|
||||||
|
"residual:${template.contentId}",
|
||||||
|
TranslationUnitKind.TITLE_RESIDUAL,
|
||||||
|
requireNotNull(request.snapshot.contents.single { it.contentId == template.contentId }.sourceLanguage),
|
||||||
|
values,
|
||||||
|
template.contentId,
|
||||||
|
groupId = template.groupId
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fields + common + residuals
|
||||||
|
}
|
||||||
|
}
|
||||||
+60
@@ -0,0 +1,60 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TitleGroupingReview
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionError
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionRequest
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.groupingInputs
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.selectionSnapshot
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.ReviewTranslationSelectionGroupingRequest
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionGroupingResponse
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationSelectionStore
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
import org.springframework.transaction.annotation.Transactional
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.time.ZoneOffset
|
||||||
|
import java.time.temporal.ChronoUnit
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class TranslationSelectionGroupingService(private val store: TranslationSelectionStore) {
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
fun get(requestId: Long): TranslationSelectionGroupingResponse =
|
||||||
|
response(store.find(requestId) ?: throw TranslationSelectionError.RESOURCE_NOT_FOUND.exception())
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
fun review(requestId: Long, body: ReviewTranslationSelectionGroupingRequest): TranslationSelectionGroupingResponse {
|
||||||
|
val request = store.lock(requestId) ?: throw TranslationSelectionError.RESOURCE_NOT_FOUND.exception()
|
||||||
|
if (request.status.isTerminal()) throw TranslationSelectionError.REQUEST_CLOSED.exception()
|
||||||
|
if (request.status !in setOf(TranslationSelectionStatus.REVIEW_REQUIRED, TranslationSelectionStatus.READY_TO_GENERATE)) {
|
||||||
|
throw TranslationSelectionError.REQUEST_NOT_READY.exception()
|
||||||
|
}
|
||||||
|
if (body.expectedRevision != request.revision) throw TranslationSelectionError.REVISION_CONFLICT.exception()
|
||||||
|
val grouping = try {
|
||||||
|
TitleGroupingReview(body.groups, body.individualContentIds).validate(request.snapshot.contents.groupingInputs())
|
||||||
|
} catch (_: IllegalArgumentException) {
|
||||||
|
throw TranslationSelectionError.INVALID_GROUPING.exception()
|
||||||
|
}
|
||||||
|
return response(
|
||||||
|
store.save(
|
||||||
|
request.copy(
|
||||||
|
status = TranslationSelectionStatus.READY_TO_GENERATE,
|
||||||
|
revision = request.revision + 1,
|
||||||
|
snapshot = request.snapshot.copy(grouping = grouping.selectionSnapshot(), reviewed = true),
|
||||||
|
updatedAt = LocalDateTime.now(ZoneOffset.UTC).truncatedTo(ChronoUnit.SECONDS)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun response(request: TranslationSelectionRequest): TranslationSelectionGroupingResponse {
|
||||||
|
val grouping = request.snapshot.grouping ?: throw TranslationSelectionError.REQUEST_NOT_READY.exception()
|
||||||
|
return TranslationSelectionGroupingResponse(
|
||||||
|
request.requestId,
|
||||||
|
request.revision,
|
||||||
|
request.snapshot.reviewed,
|
||||||
|
grouping.groups,
|
||||||
|
grouping.individualContentIds,
|
||||||
|
grouping.templates
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
+179
@@ -0,0 +1,179 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContent
|
||||||
|
import kr.co.vividnext.sodalive.content.LanguageDetectionCacheService
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationLanguages
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationProvider
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ApprovalProvider
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.SelectionContentSource
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TitleGroupingAnalyzer
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationPreparationClaim
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionError
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionRequest
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionScope
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.groupingInputs
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.selectionFingerprints
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.selectionSnapshot
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationSelectionStore
|
||||||
|
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 java.time.LocalDateTime
|
||||||
|
import java.time.ZoneOffset
|
||||||
|
import java.time.temporal.ChronoUnit
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class TranslationSelectionPreparationService(
|
||||||
|
private val store: TranslationSelectionStore,
|
||||||
|
private val sources: ContentTranslationSourceService,
|
||||||
|
private val detector: LanguageDetectionCacheService,
|
||||||
|
private val providers: List<TranslationProvider>,
|
||||||
|
transactionManager: PlatformTransactionManager,
|
||||||
|
private val nowProvider: () -> LocalDateTime = { LocalDateTime.now(ZoneOffset.UTC) }
|
||||||
|
) {
|
||||||
|
private val transaction = TransactionTemplate(transactionManager).apply {
|
||||||
|
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||||
|
fun prepareNext(): Boolean {
|
||||||
|
val claim = claimNext() ?: return false
|
||||||
|
processPrepare(claim)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
fun claimNext(): TranslationPreparationClaim? = transaction.execute {
|
||||||
|
val now = now()
|
||||||
|
val request = store.nextPreparation(now) ?: return@execute null
|
||||||
|
if (request.preparationAttempt >= 2) {
|
||||||
|
fail(request, TranslationSelectionError.PREPARATION_INTERRUPTED)
|
||||||
|
return@execute null
|
||||||
|
}
|
||||||
|
val current = currentSources(request) ?: return@execute null
|
||||||
|
val baseline = request.snapshot.preparationSources ?: current.map { it.selectionSource() }
|
||||||
|
val claimed = store.save(
|
||||||
|
request.copy(
|
||||||
|
preparationAttempt = request.preparationAttempt + 1,
|
||||||
|
preparationFence = request.preparationFence + 1,
|
||||||
|
preparationLeaseUntil = now.plusSeconds(60),
|
||||||
|
updatedAt = now,
|
||||||
|
snapshot = request.snapshot.copy(preparationSources = baseline)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
TranslationPreparationClaim(claimed.requestId, claimed.preparationFence, baseline)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||||
|
fun processPrepare(claim: TranslationPreparationClaim) {
|
||||||
|
val languages = mutableMapOf<Long, String>()
|
||||||
|
for (source in claim.sources) {
|
||||||
|
if (transaction.execute { checkedCurrent(claim) != null } != true) return
|
||||||
|
val language = source.sourceLanguage?.takeIf { it.isNotBlank() }
|
||||||
|
?: detector.detectWithCache(source.source().detectionQuery) {
|
||||||
|
transaction.execute { checkedCurrent(claim) != null } == true
|
||||||
|
}?.detectedLanguage
|
||||||
|
val accepted = transaction.execute {
|
||||||
|
val (request) = checkedCurrent(claim) ?: return@execute false
|
||||||
|
val error = when {
|
||||||
|
language.isNullOrBlank() -> TranslationSelectionError.LANGUAGE_DETECTION_FAILED
|
||||||
|
!TranslationLanguages.isSupported(language) -> TranslationSelectionError.UNSUPPORTED_SOURCE_LANGUAGE
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
if (error != null) fail(request, error)
|
||||||
|
error == null
|
||||||
|
}
|
||||||
|
if (accepted != true) return
|
||||||
|
languages[source.contentId] = requireNotNull(language)
|
||||||
|
}
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
val (request, current) = checkedCurrent(claim) ?: return@executeWithoutResult
|
||||||
|
current.forEach { content ->
|
||||||
|
if (content.languageCode.isNullOrBlank()) content.languageCode = languages.getValue(requireNotNull(content.id))
|
||||||
|
}
|
||||||
|
val contents = current.map { it.selectionSource() }
|
||||||
|
val proposed = TitleGroupingAnalyzer().analyze(contents.groupingInputs())
|
||||||
|
store.save(
|
||||||
|
request.copy(
|
||||||
|
status = when (request.scope) {
|
||||||
|
is TranslationSelectionScope.Content -> TranslationSelectionStatus.READY_TO_GENERATE
|
||||||
|
is TranslationSelectionScope.Series -> TranslationSelectionStatus.REVIEW_REQUIRED
|
||||||
|
},
|
||||||
|
preparationLeaseUntil = null,
|
||||||
|
updatedAt = now(),
|
||||||
|
snapshot = request.snapshot.copy(
|
||||||
|
contents = contents,
|
||||||
|
grouping = proposed.selectionSnapshot(),
|
||||||
|
reviewed = request.scope is TranslationSelectionScope.Content,
|
||||||
|
providerManifest = providers.map { ApprovalProvider(it.providerId.code, it.providerVersion) }
|
||||||
|
.sortedBy { it.id },
|
||||||
|
targetLanguages = contents.flatMap {
|
||||||
|
TranslationLanguages.getTranslatableLanguageCodes(it.sourceLanguage)
|
||||||
|
}.distinct()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun checkedCurrent(claim: TranslationPreparationClaim): Pair<TranslationSelectionRequest, List<AudioContent>>? {
|
||||||
|
val request = store.lock(claim.requestId) ?: return null
|
||||||
|
val now = now()
|
||||||
|
if (request.status != TranslationSelectionStatus.PREPARING || request.preparationFence != claim.fence ||
|
||||||
|
request.preparationLeaseUntil?.isAfter(now) != true
|
||||||
|
) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
val current = currentSources(request) ?: return null
|
||||||
|
val renewed = store.save(request.copy(preparationLeaseUntil = now.plusSeconds(60), updatedAt = now))
|
||||||
|
return renewed to current
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun currentSources(request: TranslationSelectionRequest): List<AudioContent>? {
|
||||||
|
val current = sources.lockScope(request.scope)
|
||||||
|
val error = when {
|
||||||
|
current == null -> when (request.scope) {
|
||||||
|
is TranslationSelectionScope.Content -> TranslationSelectionError.SOURCE_CHANGED
|
||||||
|
is TranslationSelectionScope.Series -> TranslationSelectionError.MEMBERSHIP_CHANGED
|
||||||
|
}
|
||||||
|
current.map { it.id } != request.snapshot.memberContentIds -> TranslationSelectionError.MEMBERSHIP_CHANGED
|
||||||
|
request.snapshot.preparationSources?.let { it != current.map { content -> content.selectionSource() } } == true ->
|
||||||
|
TranslationSelectionError.SOURCE_CHANGED
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
if (error != null) {
|
||||||
|
fail(request, error)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
return current
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun fail(request: TranslationSelectionRequest, error: TranslationSelectionError) {
|
||||||
|
store.save(
|
||||||
|
request.copy(
|
||||||
|
status = TranslationSelectionStatus.FAILED,
|
||||||
|
errorCode = error,
|
||||||
|
preparationLeaseUntil = null,
|
||||||
|
preparationFence = request.preparationFence + 1,
|
||||||
|
updatedAt = now()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun AudioContent.selectionSource(): SelectionContentSource {
|
||||||
|
val source = toContentSourceSnapshot()
|
||||||
|
return SelectionContentSource(
|
||||||
|
requireNotNull(id),
|
||||||
|
source.sourceLanguage,
|
||||||
|
source.title,
|
||||||
|
source.detail,
|
||||||
|
source.tags,
|
||||||
|
source.selectionFingerprints()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun now(): LocalDateTime = nowProvider().truncatedTo(ChronoUnit.SECONDS)
|
||||||
|
}
|
||||||
+96
@@ -0,0 +1,96 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.SelectionRequestSnapshot
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionError
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionRequest
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionScope
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationCandidateStore
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationSelectionStore
|
||||||
|
import org.springframework.dao.DataIntegrityViolationException
|
||||||
|
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 java.security.MessageDigest
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.time.ZoneOffset
|
||||||
|
import java.time.temporal.ChronoUnit
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class TranslationSelectionRequestService(
|
||||||
|
private val store: TranslationSelectionStore,
|
||||||
|
private val sources: ContentTranslationSourceService,
|
||||||
|
transactionManager: PlatformTransactionManager,
|
||||||
|
private val candidates: TranslationCandidateStore,
|
||||||
|
private val nowProvider: () -> LocalDateTime = { LocalDateTime.now(ZoneOffset.UTC) }
|
||||||
|
) {
|
||||||
|
private val transaction = TransactionTemplate(transactionManager).apply {
|
||||||
|
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||||
|
fun create(adminId: Long, clientRequestId: UUID, scope: TranslationSelectionScope): TranslationSelectionRequest {
|
||||||
|
if (adminId <= 0 || scope.resourceId() <= 0) throw TranslationSelectionError.INVALID_REQUEST.exception()
|
||||||
|
val hash = MessageDigest.getInstance("SHA-256").digest(scope.canonicalBody().toByteArray(Charsets.UTF_8))
|
||||||
|
.joinToString("") { "%02x".format(it) }
|
||||||
|
fun replay(): TranslationSelectionRequest? = transaction.execute { store.findCreation(adminId, clientRequestId) }?.also {
|
||||||
|
if (it.bodyHash != hash) throw TranslationSelectionError.IDEMPOTENCY_CONFLICT.exception()
|
||||||
|
}
|
||||||
|
replay()?.let { return it }
|
||||||
|
try {
|
||||||
|
return requireNotNull(
|
||||||
|
transaction.execute {
|
||||||
|
val members = sources.lockScope(scope) ?: throw TranslationSelectionError.RESOURCE_NOT_FOUND.exception()
|
||||||
|
val now = nowProvider().truncatedTo(ChronoUnit.SECONDS)
|
||||||
|
store.save(
|
||||||
|
TranslationSelectionRequest(
|
||||||
|
createdBy = adminId,
|
||||||
|
clientRequestId = clientRequestId,
|
||||||
|
bodyHash = hash,
|
||||||
|
scope = scope,
|
||||||
|
snapshot = SelectionRequestSnapshot(
|
||||||
|
memberContentIds = members.map { requireNotNull(it.id) }.sorted()
|
||||||
|
),
|
||||||
|
status = if (members.isEmpty()) {
|
||||||
|
TranslationSelectionStatus.FAILED
|
||||||
|
} else {
|
||||||
|
TranslationSelectionStatus.PREPARING
|
||||||
|
},
|
||||||
|
errorCode = if (members.isEmpty()) TranslationSelectionError.EMPTY_SCOPE else null,
|
||||||
|
createdAt = now
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
} catch (collision: DataIntegrityViolationException) {
|
||||||
|
// The failed insert transaction is already rolled back before reading the winning unique row.
|
||||||
|
return replay() ?: throw collision
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
fun get(requestId: Long): TranslationSelectionRequest =
|
||||||
|
store.find(requestId) ?: throw TranslationSelectionError.RESOURCE_NOT_FOUND.exception()
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
fun cancel(requestId: Long): TranslationSelectionRequest {
|
||||||
|
val request = store.lock(requestId) ?: throw TranslationSelectionError.RESOURCE_NOT_FOUND.exception()
|
||||||
|
if (request.status == TranslationSelectionStatus.CANCELLED) return request
|
||||||
|
if (request.status.isTerminal()) throw TranslationSelectionError.REQUEST_CLOSED.exception()
|
||||||
|
val now = nowProvider().truncatedTo(ChronoUnit.SECONDS)
|
||||||
|
candidates.discard(requestId, now)
|
||||||
|
return store.save(
|
||||||
|
request.copy(
|
||||||
|
status = TranslationSelectionStatus.CANCELLED,
|
||||||
|
preparationLeaseUntil = null,
|
||||||
|
preparationFence = request.preparationFence + 1,
|
||||||
|
revision = request.revision + 1,
|
||||||
|
updatedAt = now
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
+169
@@ -0,0 +1,169 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.LanguageProviderId
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.SourceTextNormalizer
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslateRequest
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationCallControl
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationCallStopReason
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationProvider
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.translationCallStop
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidate
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateClaim
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateFailure
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateStatus
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionRequest
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationCandidateStore
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationSelectionStore
|
||||||
|
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 java.time.LocalDateTime
|
||||||
|
import java.time.ZoneOffset
|
||||||
|
import java.time.temporal.ChronoUnit
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class TranslationSelectionWorkerService(
|
||||||
|
private val requests: TranslationSelectionStore,
|
||||||
|
private val candidates: TranslationCandidateStore,
|
||||||
|
private val providers: List<TranslationProvider>,
|
||||||
|
transactionManager: PlatformTransactionManager,
|
||||||
|
private val nowProvider: () -> LocalDateTime = { LocalDateTime.now(ZoneOffset.UTC) }
|
||||||
|
) {
|
||||||
|
private val transaction = TransactionTemplate(transactionManager).apply {
|
||||||
|
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW
|
||||||
|
isolationLevel = TransactionDefinition.ISOLATION_READ_COMMITTED
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||||
|
fun runNext(): Boolean {
|
||||||
|
val claim = claimNext() ?: return false
|
||||||
|
process(claim)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
fun claimNext(): TranslationCandidateClaim? = transaction.execute {
|
||||||
|
val now = now()
|
||||||
|
val requestId = candidates.nextRequest(now) ?: return@execute null
|
||||||
|
val request = requests.lock(requestId) ?: return@execute null
|
||||||
|
if (request.status != TranslationSelectionStatus.GENERATING) return@execute null
|
||||||
|
val candidate = candidates.lockNext(requestId, now) ?: return@execute null
|
||||||
|
if (candidate.attempt >= 2) {
|
||||||
|
val failed = candidates.save(
|
||||||
|
candidate.copy(
|
||||||
|
status = TranslationCandidateStatus.FAILED,
|
||||||
|
failureCode = TranslationCandidateFailure.EXECUTION_INTERRUPTED,
|
||||||
|
fence = candidate.fence + 1,
|
||||||
|
leaseUntil = null,
|
||||||
|
updatedAt = now,
|
||||||
|
terminalAt = now
|
||||||
|
)
|
||||||
|
)
|
||||||
|
candidates.completeLinked(failed)
|
||||||
|
finishIfReady(request)
|
||||||
|
return@execute null
|
||||||
|
}
|
||||||
|
val claimed = candidates.save(
|
||||||
|
candidate.copy(
|
||||||
|
status = TranslationCandidateStatus.RUNNING,
|
||||||
|
attempt = candidate.attempt + 1,
|
||||||
|
fence = candidate.fence + 1,
|
||||||
|
leaseUntil = now.plusSeconds(60),
|
||||||
|
updatedAt = now
|
||||||
|
)
|
||||||
|
)
|
||||||
|
TranslationCandidateClaim(claimed, request.revision)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun authorize(claim: TranslationCandidateClaim): Boolean = transaction.execute {
|
||||||
|
val (_, candidate) = current(claim) ?: return@execute false
|
||||||
|
val now = now()
|
||||||
|
candidates.save(candidate.copy(leaseUntil = now.plusSeconds(60), updatedAt = now))
|
||||||
|
true
|
||||||
|
} == true
|
||||||
|
|
||||||
|
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||||
|
fun process(claim: TranslationCandidateClaim) {
|
||||||
|
val candidate = claim.candidate
|
||||||
|
val provider = providers.singleOrNull {
|
||||||
|
it.providerId.code == candidate.provider.id && it.providerVersion == candidate.provider.version
|
||||||
|
}
|
||||||
|
if (provider == null) {
|
||||||
|
publish(claim, null, TranslationCandidateFailure.PROVIDER_CHANGED)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var values: List<String>? = null
|
||||||
|
val failure = try {
|
||||||
|
val control = TranslationCallControl({ authorize(claim) })
|
||||||
|
control.beforeCall()
|
||||||
|
values = provider.translate(
|
||||||
|
TranslateRequest(
|
||||||
|
candidate.unit.inputValues,
|
||||||
|
candidate.unit.sourceLanguage,
|
||||||
|
candidate.targetLanguage,
|
||||||
|
candidate.unit.field == ContentSourceFieldKey.TAGS,
|
||||||
|
control
|
||||||
|
)
|
||||||
|
).translatedText
|
||||||
|
control.remainingTimeout()
|
||||||
|
when {
|
||||||
|
values.size != candidate.unit.inputValues.size || values.any { it.isBlank() } ->
|
||||||
|
TranslationCandidateFailure.INVALID_PROVIDER_RESULT
|
||||||
|
candidate.unit.field == ContentSourceFieldKey.TAGS && provider.providerId == LanguageProviderId.AWS &&
|
||||||
|
candidate.unit.inputValues.zip(values).any { (source, translated) ->
|
||||||
|
SourceTextNormalizer.hasSameCommaSeparatedText(source, translated)
|
||||||
|
} -> TranslationCandidateFailure.INVALID_PROVIDER_RESULT
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
} catch (error: Exception) {
|
||||||
|
if (error.translationCallStop()?.reason == TranslationCallStopReason.OWNERSHIP_LOST) return
|
||||||
|
// Raw provider exceptions never cross the candidate persistence/API boundary.
|
||||||
|
TranslationCandidateFailure.PROVIDER_FAILED
|
||||||
|
}
|
||||||
|
publish(claim, if (failure == null) values else null, failure)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun publish(claim: TranslationCandidateClaim, values: List<String>?, failure: TranslationCandidateFailure?): Boolean =
|
||||||
|
transaction.execute {
|
||||||
|
val (request, candidate) = current(claim) ?: return@execute false
|
||||||
|
val now = now()
|
||||||
|
val completed = candidates.save(
|
||||||
|
candidate.copy(
|
||||||
|
status = if (failure == null) TranslationCandidateStatus.SUCCEEDED else TranslationCandidateStatus.FAILED,
|
||||||
|
translatedValues = values,
|
||||||
|
failureCode = failure,
|
||||||
|
leaseUntil = null,
|
||||||
|
updatedAt = now,
|
||||||
|
terminalAt = now
|
||||||
|
)
|
||||||
|
)
|
||||||
|
candidates.completeLinked(completed)
|
||||||
|
finishIfReady(request)
|
||||||
|
true
|
||||||
|
} == true
|
||||||
|
|
||||||
|
private fun current(claim: TranslationCandidateClaim): Pair<TranslationSelectionRequest, TranslationCandidate>? {
|
||||||
|
val request = requests.lock(claim.candidate.requestId) ?: return null
|
||||||
|
if (request.status != TranslationSelectionStatus.GENERATING || request.revision != claim.generationRevision) return null
|
||||||
|
val candidate = candidates.lock(claim.candidate.candidateId) ?: return null
|
||||||
|
if (candidate.status != TranslationCandidateStatus.RUNNING || candidate.fence != claim.candidate.fence ||
|
||||||
|
candidate.leaseUntil?.isAfter(now()) != true
|
||||||
|
) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
return request to candidate
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun finishIfReady(request: TranslationSelectionRequest) {
|
||||||
|
val progress = candidates.progress(request.requestId)
|
||||||
|
if (progress.total == progress.terminal) {
|
||||||
|
requests.save(request.copy(status = TranslationSelectionStatus.READY, updatedAt = now()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun now(): LocalDateTime = nowProvider().truncatedTo(ChronoUnit.SECONDS)
|
||||||
|
}
|
||||||
+65
@@ -0,0 +1,65 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.domain
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationSourceField
|
||||||
|
import java.io.ByteArrayOutputStream
|
||||||
|
import java.io.DataOutputStream
|
||||||
|
import java.security.MessageDigest
|
||||||
|
|
||||||
|
enum class ContentSourceFieldKey(val key: String) {
|
||||||
|
TITLE("title"), DETAIL("detail"), TAGS("tags")
|
||||||
|
}
|
||||||
|
|
||||||
|
class ContentSourceSnapshot(
|
||||||
|
val sourceLanguage: String?,
|
||||||
|
val title: String,
|
||||||
|
val detail: String,
|
||||||
|
tags: List<String>
|
||||||
|
) {
|
||||||
|
val tags: List<String> = java.util.List.copyOf(tags)
|
||||||
|
val fields: List<TranslationSourceField> = java.util.List.of(
|
||||||
|
TranslationSourceField(ContentSourceFieldKey.TITLE.key, title),
|
||||||
|
TranslationSourceField(ContentSourceFieldKey.DETAIL.key, detail),
|
||||||
|
TranslationSourceField(ContentSourceFieldKey.TAGS.key, this.tags.joinToString(","))
|
||||||
|
)
|
||||||
|
val detectionQuery: String = listOf(title.trim(), detail.trim(), this.tags.joinToString(" ").trim())
|
||||||
|
.filter { it.isNotBlank() }.joinToString(" ")
|
||||||
|
|
||||||
|
fun fingerprint(field: ContentSourceFieldKey): String {
|
||||||
|
val values = when (field) {
|
||||||
|
ContentSourceFieldKey.TITLE -> listOf(title)
|
||||||
|
ContentSourceFieldKey.DETAIL -> listOf(detail)
|
||||||
|
ContentSourceFieldKey.TAGS -> tags
|
||||||
|
}
|
||||||
|
val bytes = ByteArrayOutputStream()
|
||||||
|
DataOutputStream(bytes).use { output ->
|
||||||
|
output.writeFrame("content-source-v1")
|
||||||
|
output.writeFrame(sourceLanguage)
|
||||||
|
output.writeFrame(field.key)
|
||||||
|
output.writeInt(values.size)
|
||||||
|
values.forEach { output.writeFrame(it) }
|
||||||
|
}
|
||||||
|
val hash = MessageDigest.getInstance("SHA-256").digest(bytes.toByteArray())
|
||||||
|
.joinToString("") { "%02x".format(it) }
|
||||||
|
return "v1:$hash"
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun equals(other: Any?): Boolean = other is ContentSourceSnapshot &&
|
||||||
|
sourceLanguage == other.sourceLanguage && title == other.title && detail == other.detail && tags == other.tags
|
||||||
|
|
||||||
|
override fun hashCode(): Int {
|
||||||
|
var result = sourceLanguage?.hashCode() ?: 0
|
||||||
|
result = 31 * result + title.hashCode()
|
||||||
|
result = 31 * result + detail.hashCode()
|
||||||
|
return 31 * result + tags.hashCode()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun DataOutputStream.writeFrame(value: String?) {
|
||||||
|
if (value == null) {
|
||||||
|
writeInt(-1)
|
||||||
|
} else {
|
||||||
|
val bytes = value.toByteArray(Charsets.UTF_8)
|
||||||
|
writeInt(bytes.size)
|
||||||
|
write(bytes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+26
@@ -0,0 +1,26 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.domain
|
||||||
|
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
|
data class ApprovalProvider(val id: String, val version: String)
|
||||||
|
|
||||||
|
data class ApprovalProvenance(
|
||||||
|
val requestId: Long,
|
||||||
|
val candidateIds: List<Long>,
|
||||||
|
val providers: List<ApprovalProvider>,
|
||||||
|
val approvedBy: Long
|
||||||
|
)
|
||||||
|
|
||||||
|
data class ContentFieldApproval(
|
||||||
|
val field: ContentSourceFieldKey,
|
||||||
|
val sourceFingerprint: String,
|
||||||
|
val translatedText: String,
|
||||||
|
val provenance: ApprovalProvenance
|
||||||
|
)
|
||||||
|
|
||||||
|
data class ContentTranslationOverride(
|
||||||
|
val contentId: Long,
|
||||||
|
val locale: String,
|
||||||
|
val approval: ContentFieldApproval,
|
||||||
|
val approvedAt: LocalDateTime
|
||||||
|
)
|
||||||
+152
@@ -0,0 +1,152 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.domain
|
||||||
|
|
||||||
|
import java.util.regex.Pattern
|
||||||
|
|
||||||
|
class TitleGroupingAnalyzer {
|
||||||
|
fun analyze(inputs: List<TitleGroupingInput>): TitleGrouping {
|
||||||
|
require(inputs.map { it.contentId }.distinct().size == inputs.size) { "Duplicate content ID" }
|
||||||
|
val candidates = inputs.sortedBy { it.contentId }.groupBy { it.sourceLanguage }.values.flatMap { members ->
|
||||||
|
val texts = members.associate { it.contentId to GraphemeTitle(it.title) }
|
||||||
|
val repeated = mutableSetOf<String>()
|
||||||
|
members.forEachIndexed { index, left ->
|
||||||
|
members.drop(index + 1).forEach { right ->
|
||||||
|
scan(texts.getValue(left.contentId), texts.getValue(right.contentId), repeated)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
repeated.mapNotNull { text ->
|
||||||
|
val occurrences = members.mapNotNull { member ->
|
||||||
|
texts.getValue(member.contentId).uniqueOccurrence(member.contentId, text)
|
||||||
|
}
|
||||||
|
if (occurrences.size < 2) null else Candidate(text, occurrences)
|
||||||
|
}
|
||||||
|
}.sortedWith { left, right ->
|
||||||
|
compareValues(right.savings, left.savings).takeIf { it != 0 }
|
||||||
|
?: compareValues(right.length, left.length).takeIf { it != 0 }
|
||||||
|
?: compareCodePoints(left.text, right.text).takeIf { it != 0 }
|
||||||
|
?: compareMemberIds(left.occurrences, right.occurrences)
|
||||||
|
}
|
||||||
|
val assigned = mutableSetOf<Long>()
|
||||||
|
val groups = mutableListOf<ReviewedTitleGroup>()
|
||||||
|
var partNumber = 0
|
||||||
|
candidates.forEach { seed ->
|
||||||
|
val memberIds = seed.occurrences.map { it.contentId }.filter { it !in assigned }
|
||||||
|
if (memberIds.size >= 2) {
|
||||||
|
val parts = mutableListOf<ReviewedSharedPart>()
|
||||||
|
candidates.forEach { candidate ->
|
||||||
|
val occurrences = candidate.occurrences.filter { it.contentId in memberIds }
|
||||||
|
if (occurrences.size == memberIds.size && occurrences.all { occurrence ->
|
||||||
|
parts.flatMap { it.occurrences }.none { existing -> existing.overlaps(occurrence) }
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
parts.add(ReviewedSharedPart("p-${++partNumber}", occurrences))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
groups.add(ReviewedTitleGroup("g-${groups.size + 1}", memberIds, parts))
|
||||||
|
assigned.addAll(memberIds)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return TitleGroupingReview(groups, inputs.map { it.contentId }.filter { it !in assigned }).validate(inputs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scan matching diagonals without retaining a quadratic longest-common-substring matrix.
|
||||||
|
private fun scan(left: GraphemeTitle, right: GraphemeTitle, repeated: MutableSet<String>) {
|
||||||
|
for (diagonal in 1 - right.graphemes.size until left.graphemes.size) {
|
||||||
|
var i = maxOf(0, diagonal)
|
||||||
|
var j = maxOf(0, -diagonal)
|
||||||
|
while (i < left.graphemes.size && j < right.graphemes.size) {
|
||||||
|
if (left.graphemes[i] != right.graphemes[j]) {
|
||||||
|
i++
|
||||||
|
j++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
val initialI = i
|
||||||
|
val initialJ = j
|
||||||
|
while (i < left.graphemes.size && j < right.graphemes.size && left.graphemes[i] == right.graphemes[j]) {
|
||||||
|
i++
|
||||||
|
j++
|
||||||
|
}
|
||||||
|
var start = initialI
|
||||||
|
var end = i
|
||||||
|
while (start < end &&
|
||||||
|
(!left.lexicalBoundary(start) || !right.lexicalBoundary(initialJ + start - initialI))
|
||||||
|
) start++
|
||||||
|
while (end > start && (!left.lexicalBoundary(end) || !right.lexicalBoundary(initialJ + end - initialI))) end--
|
||||||
|
val text = left.substring(start, end)
|
||||||
|
if (text.codePoints().filter { Character.isLetter(it) }.count() >= 2) repeated.add(text)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private data class Candidate(val text: String, val occurrences: List<TitleOccurrence>) {
|
||||||
|
val length: Int = text.codePointCount(0, text.length)
|
||||||
|
val savings: Long = length.toLong() * (occurrences.size - 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun compareCodePoints(left: String, right: String): Int {
|
||||||
|
val a = left.codePoints().iterator()
|
||||||
|
val b = right.codePoints().iterator()
|
||||||
|
while (a.hasNext() && b.hasNext()) {
|
||||||
|
val comparison = a.nextInt().compareTo(b.nextInt())
|
||||||
|
if (comparison != 0) return comparison
|
||||||
|
}
|
||||||
|
return a.hasNext().compareTo(b.hasNext())
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun compareMemberIds(left: List<TitleOccurrence>, right: List<TitleOccurrence>): Int {
|
||||||
|
left.zip(right).forEach { (a, b) ->
|
||||||
|
val comparison = a.contentId.compareTo(b.contentId)
|
||||||
|
if (comparison != 0) return comparison
|
||||||
|
}
|
||||||
|
return left.size.compareTo(right.size)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class GraphemeTitle(private val raw: String) {
|
||||||
|
val graphemes: List<String> = GRAPHEME.matcher(raw).results().map { it.group() }.toList()
|
||||||
|
private val charOffsets = IntArray(graphemes.size + 1)
|
||||||
|
val codePointOffsets = IntArray(graphemes.size + 1)
|
||||||
|
|
||||||
|
init {
|
||||||
|
graphemes.forEachIndexed { index, text ->
|
||||||
|
charOffsets[index + 1] = charOffsets[index] + text.length
|
||||||
|
codePointOffsets[index + 1] = codePointOffsets[index] + text.codePointCount(0, text.length)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun substring(start: Int, end: Int): String = raw.substring(charOffsets[start], charOffsets[end])
|
||||||
|
|
||||||
|
fun slice(occurrence: TitleOccurrence): String {
|
||||||
|
val start = codePointOffsets.binarySearch(occurrence.startCodePoint)
|
||||||
|
val end = codePointOffsets.binarySearch(occurrence.endCodePoint)
|
||||||
|
require(start >= 0 && end > start) { "Range must be nonempty and follow grapheme boundaries" }
|
||||||
|
return substring(start, end)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun lexicalBoundary(index: Int): Boolean = index == 0 || index == graphemes.size ||
|
||||||
|
!isWord(graphemes[index - 1]) || !isWord(graphemes[index])
|
||||||
|
|
||||||
|
fun uniqueOccurrence(contentId: Long, text: String): TitleOccurrence? {
|
||||||
|
var from = 0
|
||||||
|
var found: TitleOccurrence? = null
|
||||||
|
while (from <= raw.length - text.length) {
|
||||||
|
val position = raw.indexOf(text, from)
|
||||||
|
if (position < 0) break
|
||||||
|
val start = charOffsets.binarySearch(position)
|
||||||
|
val end = charOffsets.binarySearch(position + text.length)
|
||||||
|
if (start >= 0 && end >= 0 && lexicalBoundary(start) && lexicalBoundary(end)) {
|
||||||
|
if (found != null) return null
|
||||||
|
found = TitleOccurrence(contentId, codePointOffsets[start], codePointOffsets[end])
|
||||||
|
}
|
||||||
|
from = position + 1
|
||||||
|
}
|
||||||
|
return found
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isWord(text: String): Boolean = text.codePoints().anyMatch {
|
||||||
|
Character.isLetterOrDigit(it) || Character.getType(it) == Character.CONNECTOR_PUNCTUATION.toInt()
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val GRAPHEME = Pattern.compile("\\X")
|
||||||
|
}
|
||||||
|
}
|
||||||
+38
@@ -0,0 +1,38 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.domain
|
||||||
|
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
|
enum class TranslationCandidateStatus { PENDING, RUNNING, SUCCEEDED, FAILED, DISCARDED }
|
||||||
|
enum class TranslationCandidateFailure { PROVIDER_FAILED, PROVIDER_CHANGED, INVALID_PROVIDER_RESULT, EXECUTION_INTERRUPTED }
|
||||||
|
enum class TranslationUnitKind { FIELD, GROUP_COMMON, TITLE_RESIDUAL }
|
||||||
|
|
||||||
|
data class TranslationGenerationUnit(
|
||||||
|
val key: String,
|
||||||
|
val kind: TranslationUnitKind,
|
||||||
|
val sourceLanguage: String,
|
||||||
|
val inputValues: List<String>,
|
||||||
|
val contentId: Long? = null,
|
||||||
|
val field: ContentSourceFieldKey? = null,
|
||||||
|
val groupId: String? = null
|
||||||
|
)
|
||||||
|
|
||||||
|
data class TranslationCandidate(
|
||||||
|
val candidateId: Long = 0,
|
||||||
|
val requestId: Long,
|
||||||
|
val unit: TranslationGenerationUnit,
|
||||||
|
val provider: ApprovalProvider,
|
||||||
|
val targetLanguage: String,
|
||||||
|
val status: TranslationCandidateStatus = TranslationCandidateStatus.PENDING,
|
||||||
|
val translatedValues: List<String>? = null,
|
||||||
|
val failureCode: TranslationCandidateFailure? = null,
|
||||||
|
val attempt: Int = 0,
|
||||||
|
val leaseUntil: LocalDateTime? = null,
|
||||||
|
val fence: Long = 0,
|
||||||
|
val createdAt: LocalDateTime,
|
||||||
|
val updatedAt: LocalDateTime = createdAt,
|
||||||
|
val terminalAt: LocalDateTime? = null,
|
||||||
|
val linkedCandidateId: Long? = null
|
||||||
|
)
|
||||||
|
|
||||||
|
data class TranslationCandidateClaim(val candidate: TranslationCandidate, val generationRevision: Long)
|
||||||
|
data class SelectionCandidateProgress(val total: Int = 0, val terminal: Int = 0, val succeeded: Int = 0, val failed: Int = 0)
|
||||||
+152
@@ -0,0 +1,152 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.domain
|
||||||
|
|
||||||
|
data class TitleGroupingInput(val contentId: Long, val sourceLanguage: String, val title: String)
|
||||||
|
data class TitleOccurrence(val contentId: Long, val startCodePoint: Int, val endCodePoint: Int)
|
||||||
|
data class ReviewedSharedPart(val partId: String, val occurrences: List<TitleOccurrence>)
|
||||||
|
data class ReviewedTitleGroup(val groupId: String, val memberContentIds: List<Long>, val sharedParts: List<ReviewedSharedPart>)
|
||||||
|
data class TitleGroupingReview(val groups: List<ReviewedTitleGroup>, val individualContentIds: List<Long>) {
|
||||||
|
fun validate(inputs: List<TitleGroupingInput>): TitleGrouping {
|
||||||
|
val sources = inputs.associateBy { it.contentId }
|
||||||
|
require(sources.size == inputs.size) { "Duplicate content ID" }
|
||||||
|
val assigned = groups.flatMap { it.memberContentIds } + individualContentIds
|
||||||
|
require(assigned.size == sources.size && assigned.toSet() == sources.keys) { "Scope must be covered exactly once" }
|
||||||
|
require(groups.map { it.groupId }.distinct().size == groups.size && groups.all { it.groupId.isNotBlank() }) {
|
||||||
|
"Group IDs must be nonempty and unique"
|
||||||
|
}
|
||||||
|
val parts = groups.flatMap { it.sharedParts }
|
||||||
|
require(parts.map { it.partId }.distinct().size == parts.size && parts.all { it.partId.isNotBlank() }) {
|
||||||
|
"Part IDs must be nonempty and unique"
|
||||||
|
}
|
||||||
|
val titles = inputs.associate { it.contentId to GraphemeTitle(it.title) }
|
||||||
|
val validated = groups.map { group ->
|
||||||
|
val memberIds = group.memberContentIds.sorted()
|
||||||
|
require(memberIds.size >= 2 && group.sharedParts.isNotEmpty()) { "Group needs two members and a shared part" }
|
||||||
|
val languages = memberIds.map { sources.getValue(it).sourceLanguage }.distinct()
|
||||||
|
require(languages.size == 1) { "Group languages must match" }
|
||||||
|
val shared = group.sharedParts.map { part ->
|
||||||
|
require(part.occurrences.map { it.contentId }.sorted() == memberIds) {
|
||||||
|
"Part must cover every member exactly once"
|
||||||
|
}
|
||||||
|
val occurrences = part.occurrences.sortedBy { it.contentId }
|
||||||
|
val texts = occurrences.map { titles.getValue(it.contentId).slice(it) }
|
||||||
|
require(texts.distinct().size == 1) { "Shared raw text must match exactly" }
|
||||||
|
TitleSharedPart(part.partId, texts.first(), java.util.List.copyOf(occurrences))
|
||||||
|
}
|
||||||
|
memberIds.forEach { contentId ->
|
||||||
|
val ranges = shared.flatMap { it.occurrences }.filter { it.contentId == contentId }.sortedBy { it.startCodePoint }
|
||||||
|
require(ranges.zipWithNext().none { (left, right) -> left.overlaps(right) }) { "Shared parts overlap" }
|
||||||
|
}
|
||||||
|
TitleGroup(group.groupId, languages.single(), java.util.List.copyOf(memberIds), java.util.List.copyOf(shared))
|
||||||
|
}
|
||||||
|
val templates = validated.flatMap { group ->
|
||||||
|
group.memberContentIds.map { contentId -> template(sources.getValue(contentId), group, titles.getValue(contentId)) }
|
||||||
|
}.sortedBy { it.contentId }
|
||||||
|
return TitleGrouping(
|
||||||
|
java.util.List.copyOf(validated),
|
||||||
|
java.util.List.copyOf(individualContentIds.sorted()),
|
||||||
|
java.util.List.copyOf(templates)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun template(input: TitleGroupingInput, group: TitleGroup, title: GraphemeTitle): TitleTemplate {
|
||||||
|
val segments = mutableListOf<TitleSegment>()
|
||||||
|
var residualIndex = 0
|
||||||
|
var cursor = 0
|
||||||
|
val outerDelimiters = Regex("[\\p{javaWhitespace}\\p{Z}\\p{P}<>]+")
|
||||||
|
fun appendGap(endCodePoint: Int) {
|
||||||
|
val start = title.codePointOffsets.binarySearch(cursor)
|
||||||
|
val end = title.codePointOffsets.binarySearch(endCodePoint)
|
||||||
|
val hasLetters = (start until end).any { index ->
|
||||||
|
title.graphemes[index].codePoints().anyMatch { Character.isLetter(it) }
|
||||||
|
}
|
||||||
|
var residualStart = start
|
||||||
|
var residualEnd = end
|
||||||
|
if (hasLetters) {
|
||||||
|
while (residualStart < residualEnd && outerDelimiters.matches(title.graphemes[residualStart])) residualStart++
|
||||||
|
while (residualEnd > residualStart && outerDelimiters.matches(title.graphemes[residualEnd - 1])) residualEnd--
|
||||||
|
}
|
||||||
|
val boundaries = if (hasLetters) listOf(start, residualStart, residualEnd, end) else listOf(start, end)
|
||||||
|
boundaries.zipWithNext().filter { (from, to) -> from < to }.forEach { (from, to) ->
|
||||||
|
val translatable = hasLetters && from == residualStart && to == residualEnd
|
||||||
|
segments.add(
|
||||||
|
TitleSegment(
|
||||||
|
"s-${segments.size + 1}",
|
||||||
|
if (translatable) TitleSegmentKind.RESIDUAL else TitleSegmentKind.LITERAL,
|
||||||
|
title.codePointOffsets[from],
|
||||||
|
title.codePointOffsets[to],
|
||||||
|
title.substring(from, to),
|
||||||
|
null,
|
||||||
|
if (translatable) residualIndex++ else null
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
cursor = endCodePoint
|
||||||
|
}
|
||||||
|
val shared = group.sharedParts.mapIndexed { index, part ->
|
||||||
|
Triple(index, part, part.occurrences.single { it.contentId == input.contentId })
|
||||||
|
}.sortedBy { it.third.startCodePoint }
|
||||||
|
shared.forEach { (valueIndex, part, occurrence) ->
|
||||||
|
appendGap(occurrence.startCodePoint)
|
||||||
|
segments.add(
|
||||||
|
TitleSegment(
|
||||||
|
"s-${segments.size + 1}",
|
||||||
|
TitleSegmentKind.SHARED,
|
||||||
|
occurrence.startCodePoint,
|
||||||
|
occurrence.endCodePoint,
|
||||||
|
part.sourceText,
|
||||||
|
part.partId,
|
||||||
|
valueIndex
|
||||||
|
)
|
||||||
|
)
|
||||||
|
cursor = occurrence.endCodePoint
|
||||||
|
}
|
||||||
|
appendGap(title.codePointOffsets.last())
|
||||||
|
return TitleTemplate(
|
||||||
|
input.contentId,
|
||||||
|
group.groupId,
|
||||||
|
ContentSourceSnapshot(input.sourceLanguage, input.title, "", emptyList()).fingerprint(ContentSourceFieldKey.TITLE),
|
||||||
|
java.util.List.copyOf(segments)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
data class TitleSharedPart(val partId: String, val sourceText: String, val occurrences: List<TitleOccurrence>)
|
||||||
|
data class TitleGroup(
|
||||||
|
val groupId: String,
|
||||||
|
val sourceLanguage: String,
|
||||||
|
val memberContentIds: List<Long>,
|
||||||
|
val sharedParts: List<TitleSharedPart>
|
||||||
|
)
|
||||||
|
data class TitleGrouping(val groups: List<TitleGroup>, val individualContentIds: List<Long>, val templates: List<TitleTemplate>)
|
||||||
|
enum class TitleSegmentKind { LITERAL, SHARED, RESIDUAL }
|
||||||
|
data class TitleSegment(
|
||||||
|
val segmentId: String,
|
||||||
|
val kind: TitleSegmentKind,
|
||||||
|
val startCodePoint: Int,
|
||||||
|
val endCodePoint: Int,
|
||||||
|
val sourceText: String,
|
||||||
|
val partId: String?,
|
||||||
|
val valueIndex: Int?
|
||||||
|
)
|
||||||
|
data class TitleTemplate(
|
||||||
|
val contentId: Long,
|
||||||
|
val groupId: String,
|
||||||
|
val sourceFingerprint: String,
|
||||||
|
val segments: List<TitleSegment>
|
||||||
|
) {
|
||||||
|
val residualInputs: List<String> get() = segments.filter { it.kind == TitleSegmentKind.RESIDUAL }.map { it.sourceText }
|
||||||
|
|
||||||
|
fun render(sharedValues: List<String>, residualValues: List<String>): String {
|
||||||
|
require(sharedValues.size == segments.count { it.kind == TitleSegmentKind.SHARED }) { "Shared value count differs" }
|
||||||
|
require(residualValues.size == residualInputs.size) { "Residual value count differs" }
|
||||||
|
return segments.joinToString("") { segment ->
|
||||||
|
when (segment.kind) {
|
||||||
|
TitleSegmentKind.LITERAL -> segment.sourceText
|
||||||
|
TitleSegmentKind.SHARED -> sharedValues[requireNotNull(segment.valueIndex)]
|
||||||
|
TitleSegmentKind.RESIDUAL -> residualValues[requireNotNull(segment.valueIndex)]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun TitleOccurrence.overlaps(other: TitleOccurrence): Boolean = contentId == other.contentId &&
|
||||||
|
startCodePoint < other.endCodePoint && other.startCodePoint < endCodePoint
|
||||||
+138
@@ -0,0 +1,138 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.domain
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonSubTypes
|
||||||
|
import com.fasterxml.jackson.annotation.JsonTypeInfo
|
||||||
|
import kr.co.vividnext.sodalive.common.SodaException
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionApplyReceipt
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
enum class TranslationSelectionStatus {
|
||||||
|
PREPARING, REVIEW_REQUIRED, READY_TO_GENERATE, GENERATING, READY, APPLIED, CANCELLED, FAILED;
|
||||||
|
|
||||||
|
fun isTerminal(): Boolean = this in setOf(APPLIED, CANCELLED, FAILED)
|
||||||
|
}
|
||||||
|
|
||||||
|
enum class TranslationSelectionError {
|
||||||
|
INVALID_REQUEST, RESOURCE_NOT_FOUND, EMPTY_SCOPE, UNSUPPORTED_SOURCE_LANGUAGE, LANGUAGE_DETECTION_FAILED,
|
||||||
|
PREPARATION_INTERRUPTED, INVALID_GROUPING, REVISION_CONFLICT, REQUEST_NOT_READY, REQUEST_CLOSED, REVIEW_REQUIRED,
|
||||||
|
SOURCE_CHANGED, MEMBERSHIP_CHANGED, IDEMPOTENCY_CONFLICT, CANDIDATE_NOT_SELECTABLE, INCOMPLETE_TITLE_GROUP;
|
||||||
|
|
||||||
|
fun exception(): SodaException = SodaException(
|
||||||
|
messageKey = when (this) {
|
||||||
|
RESOURCE_NOT_FOUND -> "translation.selection.not_found"
|
||||||
|
INVALID_GROUPING -> "translation.selection.invalid_grouping"
|
||||||
|
REVISION_CONFLICT -> "translation.selection.revision_conflict"
|
||||||
|
IDEMPOTENCY_CONFLICT -> "translation.selection.idempotency_conflict"
|
||||||
|
REQUEST_CLOSED -> "translation.selection.closed"
|
||||||
|
REQUEST_NOT_READY -> "translation.selection.not_ready"
|
||||||
|
CANDIDATE_NOT_SELECTABLE -> "translation.selection.candidate_not_selectable"
|
||||||
|
INCOMPLETE_TITLE_GROUP -> "translation.selection.incomplete_title_group"
|
||||||
|
SOURCE_CHANGED -> "translation.selection.source_changed"
|
||||||
|
MEMBERSHIP_CHANGED -> "translation.selection.membership_changed"
|
||||||
|
else -> "common.error.invalid_request"
|
||||||
|
},
|
||||||
|
errorProperty = name
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
|
||||||
|
@JsonSubTypes(
|
||||||
|
JsonSubTypes.Type(value = TranslationSelectionScope.Content::class, name = "CONTENT"),
|
||||||
|
JsonSubTypes.Type(value = TranslationSelectionScope.Series::class, name = "SERIES")
|
||||||
|
)
|
||||||
|
sealed class TranslationSelectionScope {
|
||||||
|
data class Content(val contentId: Long) : TranslationSelectionScope()
|
||||||
|
data class Series(val seriesId: Long) : TranslationSelectionScope()
|
||||||
|
|
||||||
|
fun resourceId(): Long = when (this) {
|
||||||
|
is Content -> contentId
|
||||||
|
is Series -> seriesId
|
||||||
|
}
|
||||||
|
|
||||||
|
fun canonicalBody(): String = when (this) {
|
||||||
|
is Content -> "v1:CONTENT:$contentId"
|
||||||
|
is Series -> "v1:SERIES:$seriesId"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only stored source fields are serialized; shared snapshot's derived getters are never persisted.
|
||||||
|
data class SelectionContentSource(
|
||||||
|
val contentId: Long,
|
||||||
|
val sourceLanguage: String?,
|
||||||
|
val title: String,
|
||||||
|
val detail: String,
|
||||||
|
val tags: List<String>,
|
||||||
|
val sourceFingerprints: SelectionFieldValues
|
||||||
|
) {
|
||||||
|
fun source(): ContentSourceSnapshot = ContentSourceSnapshot(sourceLanguage, title, detail, tags)
|
||||||
|
}
|
||||||
|
|
||||||
|
data class SelectionFieldValues(val title: String, val detail: String, val tags: String)
|
||||||
|
|
||||||
|
fun ContentSourceSnapshot.selectionFingerprints() = SelectionFieldValues(
|
||||||
|
fingerprint(ContentSourceFieldKey.TITLE),
|
||||||
|
fingerprint(ContentSourceFieldKey.DETAIL),
|
||||||
|
fingerprint(ContentSourceFieldKey.TAGS)
|
||||||
|
)
|
||||||
|
|
||||||
|
data class SelectionTitleTemplate(
|
||||||
|
val contentId: Long,
|
||||||
|
val groupId: String,
|
||||||
|
val sourceFingerprint: String,
|
||||||
|
val segments: List<TitleSegment>
|
||||||
|
) {
|
||||||
|
fun template(): TitleTemplate = TitleTemplate(contentId, groupId, sourceFingerprint, segments)
|
||||||
|
}
|
||||||
|
|
||||||
|
data class SelectionGroupingSnapshot(
|
||||||
|
val groups: List<TitleGroup>,
|
||||||
|
val individualContentIds: List<Long>,
|
||||||
|
val templates: List<SelectionTitleTemplate>
|
||||||
|
)
|
||||||
|
|
||||||
|
fun TitleGrouping.selectionSnapshot() = SelectionGroupingSnapshot(
|
||||||
|
groups,
|
||||||
|
individualContentIds,
|
||||||
|
templates.map { SelectionTitleTemplate(it.contentId, it.groupId, it.sourceFingerprint, it.segments) }
|
||||||
|
)
|
||||||
|
|
||||||
|
data class SelectionRequestSnapshot(
|
||||||
|
val version: Int = 1,
|
||||||
|
val memberContentIds: List<Long>,
|
||||||
|
val preparationSources: List<SelectionContentSource>? = null,
|
||||||
|
val contents: List<SelectionContentSource> = emptyList(),
|
||||||
|
val grouping: SelectionGroupingSnapshot? = null,
|
||||||
|
val reviewed: Boolean = false,
|
||||||
|
val providerManifest: List<ApprovalProvider> = emptyList(),
|
||||||
|
val targetLanguages: List<String> = emptyList(),
|
||||||
|
val generationUnits: List<TranslationGenerationUnit> = emptyList()
|
||||||
|
)
|
||||||
|
|
||||||
|
data class TranslationSelectionRequest(
|
||||||
|
val requestId: Long = 0,
|
||||||
|
val createdBy: Long,
|
||||||
|
val clientRequestId: UUID,
|
||||||
|
val bodyHash: String,
|
||||||
|
val scope: TranslationSelectionScope,
|
||||||
|
val status: TranslationSelectionStatus = TranslationSelectionStatus.PREPARING,
|
||||||
|
val revision: Long = 1,
|
||||||
|
val snapshot: SelectionRequestSnapshot,
|
||||||
|
val preparationAttempt: Int = 0,
|
||||||
|
val preparationLeaseUntil: LocalDateTime? = null,
|
||||||
|
val preparationFence: Long = 0,
|
||||||
|
val errorCode: TranslationSelectionError? = null,
|
||||||
|
val createdAt: LocalDateTime,
|
||||||
|
val updatedAt: LocalDateTime = createdAt,
|
||||||
|
val acceptedGenerateRevision: Long? = null,
|
||||||
|
val progress: SelectionCandidateProgress = SelectionCandidateProgress(),
|
||||||
|
val applyOperationId: UUID? = null,
|
||||||
|
val applyBodyHash: String? = null,
|
||||||
|
val applyReceipt: TranslationSelectionApplyReceipt? = null
|
||||||
|
)
|
||||||
|
|
||||||
|
data class TranslationPreparationClaim(val requestId: Long, val fence: Long, val sources: List<SelectionContentSource>)
|
||||||
|
|
||||||
|
fun List<SelectionContentSource>.groupingInputs(): List<TitleGroupingInput> = map {
|
||||||
|
TitleGroupingInput(it.contentId, requireNotNull(it.sourceLanguage), it.title)
|
||||||
|
}
|
||||||
+50
@@ -0,0 +1,50 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.dto
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonSubTypes
|
||||||
|
import com.fasterxml.jackson.annotation.JsonTypeInfo
|
||||||
|
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||||
|
import java.security.MessageDigest
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
data class ApplyTranslationSelectionRequest(
|
||||||
|
val operationId: UUID,
|
||||||
|
val expectedRevision: Long,
|
||||||
|
val selections: List<TranslationSelectionChoice>
|
||||||
|
) {
|
||||||
|
fun canonicalHash(): String {
|
||||||
|
val mapper = jacksonObjectMapper()
|
||||||
|
val choices = selections.map { choice ->
|
||||||
|
when (choice) {
|
||||||
|
is TranslationSelectionChoice.Field -> choice
|
||||||
|
is TranslationSelectionChoice.TitleGroup -> choice.copy(
|
||||||
|
residualChoices = choice.residualChoices.sortedBy { it.contentId }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}.sortedBy { mapper.writeValueAsString(it) }
|
||||||
|
val canonical = mapper.writeValueAsBytes(copy(selections = choices))
|
||||||
|
return MessageDigest.getInstance("SHA-256").digest(canonical).joinToString("") { "%02x".format(it) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "kind")
|
||||||
|
@JsonSubTypes(
|
||||||
|
JsonSubTypes.Type(value = TranslationSelectionChoice.Field::class, name = "FIELD"),
|
||||||
|
JsonSubTypes.Type(value = TranslationSelectionChoice.TitleGroup::class, name = "TITLE_GROUP")
|
||||||
|
)
|
||||||
|
sealed class TranslationSelectionChoice {
|
||||||
|
data class Field(
|
||||||
|
val contentId: Long,
|
||||||
|
val field: TranslationSelectionField,
|
||||||
|
val targetLanguage: String,
|
||||||
|
val candidateId: Long
|
||||||
|
) : TranslationSelectionChoice()
|
||||||
|
|
||||||
|
data class TitleGroup(
|
||||||
|
val groupId: String,
|
||||||
|
val targetLanguage: String,
|
||||||
|
val commonCandidateId: Long,
|
||||||
|
val residualChoices: List<TranslationSelectionResidualChoice>
|
||||||
|
) : TranslationSelectionChoice()
|
||||||
|
}
|
||||||
|
|
||||||
|
data class TranslationSelectionResidualChoice(val contentId: Long, val candidateId: Long?)
|
||||||
+40
@@ -0,0 +1,40 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.dto
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ApprovalProvider
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidate
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateFailure
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateStatus
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationUnitKind
|
||||||
|
|
||||||
|
data class GenerateTranslationSelectionRequest(val expectedRevision: Long)
|
||||||
|
data class TranslationCandidatesResponse(val items: List<TranslationCandidateResponse>, val nextAfterCandidateId: Long?)
|
||||||
|
data class TranslationCandidateResponse(
|
||||||
|
val candidateId: Long,
|
||||||
|
val unit: TranslationCandidateUnitResponse,
|
||||||
|
val provider: ApprovalProvider,
|
||||||
|
val targetLanguage: String,
|
||||||
|
val inputValues: List<String>,
|
||||||
|
val status: TranslationCandidateStatus,
|
||||||
|
val translatedValues: List<String>?,
|
||||||
|
val failureCode: TranslationCandidateFailure?
|
||||||
|
)
|
||||||
|
|
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
|
data class TranslationCandidateUnitResponse(
|
||||||
|
val kind: TranslationUnitKind,
|
||||||
|
val contentId: Long?,
|
||||||
|
val field: String?,
|
||||||
|
val groupId: String?
|
||||||
|
)
|
||||||
|
|
||||||
|
fun TranslationCandidate.response() = TranslationCandidateResponse(
|
||||||
|
candidateId,
|
||||||
|
TranslationCandidateUnitResponse(unit.kind, unit.contentId, unit.field?.key, unit.groupId),
|
||||||
|
provider,
|
||||||
|
targetLanguage,
|
||||||
|
unit.inputValues,
|
||||||
|
status,
|
||||||
|
translatedValues,
|
||||||
|
failureCode
|
||||||
|
)
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.dto
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ReviewedTitleGroup
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionScope
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
data class CreateTranslationSelectionRequest(val clientRequestId: UUID, val scope: TranslationSelectionScope)
|
||||||
|
|
||||||
|
data class ReviewTranslationSelectionGroupingRequest(
|
||||||
|
val expectedRevision: Long,
|
||||||
|
val groups: List<ReviewedTitleGroup>,
|
||||||
|
val individualContentIds: List<Long>
|
||||||
|
)
|
||||||
+112
@@ -0,0 +1,112 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.dto
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude
|
||||||
|
import com.fasterxml.jackson.annotation.JsonValue
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ApprovalProvider
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.SelectionFieldValues
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.SelectionTitleTemplate
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TitleGroup
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionError
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionRequest
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionScope
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||||
|
import java.time.Instant
|
||||||
|
import java.time.ZoneOffset
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
data class TranslationSelectionRequestResponse(
|
||||||
|
val requestId: Long,
|
||||||
|
val scope: TranslationSelectionScope,
|
||||||
|
val status: TranslationSelectionStatus,
|
||||||
|
val revision: Long,
|
||||||
|
val createdAt: Instant,
|
||||||
|
val providerManifest: List<ApprovalProvider>,
|
||||||
|
val targetLanguages: List<String>,
|
||||||
|
val contentCount: Int,
|
||||||
|
val progress: TranslationSelectionProgress,
|
||||||
|
val errorCode: TranslationSelectionError?,
|
||||||
|
val applyReceipt: TranslationSelectionApplyReceipt?
|
||||||
|
)
|
||||||
|
|
||||||
|
data class TranslationSelectionProgress(val total: Int = 0, val terminal: Int = 0, val succeeded: Int = 0, val failed: Int = 0)
|
||||||
|
data class TranslationSelectionApplyReceipt(
|
||||||
|
val requestId: Long,
|
||||||
|
val operationId: UUID,
|
||||||
|
val status: TranslationSelectionStatus,
|
||||||
|
val appliedAt: Instant,
|
||||||
|
val appliedFields: List<TranslationSelectionAppliedField>,
|
||||||
|
val discardedCandidateCount: Int
|
||||||
|
)
|
||||||
|
data class TranslationSelectionAppliedField(
|
||||||
|
val contentId: Long,
|
||||||
|
val field: TranslationSelectionField,
|
||||||
|
val targetLanguage: String,
|
||||||
|
val translatedValue: String,
|
||||||
|
val candidateIds: List<Long>,
|
||||||
|
val providers: List<ApprovalProvider>
|
||||||
|
)
|
||||||
|
|
||||||
|
fun TranslationSelectionRequest.response() = TranslationSelectionRequestResponse(
|
||||||
|
requestId, scope, status, revision, createdAt.toInstant(ZoneOffset.UTC), snapshot.providerManifest,
|
||||||
|
snapshot.targetLanguages, snapshot.contents.size,
|
||||||
|
TranslationSelectionProgress(progress.total, progress.terminal, progress.succeeded, progress.failed), errorCode, applyReceipt
|
||||||
|
)
|
||||||
|
|
||||||
|
enum class TranslationSelectionField(@get:JsonValue val key: String) { TITLE("title"), DETAIL("detail"), TAGS("tags") }
|
||||||
|
enum class TranslationSelectionApprovalOrigin { ADMIN, UNKNOWN }
|
||||||
|
enum class TitleIndexUnit { UNICODE_CODE_POINT }
|
||||||
|
|
||||||
|
data class TranslationSelectionGroupingResponse(
|
||||||
|
val requestId: Long,
|
||||||
|
val revision: Long,
|
||||||
|
val reviewed: Boolean,
|
||||||
|
val groups: List<TitleGroup>,
|
||||||
|
val individualContentIds: List<Long>,
|
||||||
|
val templates: List<SelectionTitleTemplate>,
|
||||||
|
val indexUnit: TitleIndexUnit = TitleIndexUnit.UNICODE_CODE_POINT
|
||||||
|
)
|
||||||
|
|
||||||
|
data class TranslationSelectionContentsResponse(
|
||||||
|
val items: List<TranslationSelectionContentResponse>,
|
||||||
|
val nextAfterContentId: Long?
|
||||||
|
)
|
||||||
|
|
||||||
|
data class TranslationSelectionContentResponse(
|
||||||
|
val contentId: Long,
|
||||||
|
val active: Boolean,
|
||||||
|
val eligible: Boolean,
|
||||||
|
val sourceLanguage: String,
|
||||||
|
val sourceFields: SelectionFieldValues,
|
||||||
|
val sourceFingerprints: SelectionFieldValues,
|
||||||
|
val currentSourceFingerprints: SelectionFieldValues?,
|
||||||
|
val emptyFields: List<TranslationSelectionField>,
|
||||||
|
val currentTranslations: List<TranslationSelectionCurrentTranslation>
|
||||||
|
)
|
||||||
|
|
||||||
|
data class TranslationSelectionCurrentTranslation(
|
||||||
|
val targetLanguage: String,
|
||||||
|
val fields: SelectionFieldValues,
|
||||||
|
val approvalOrigins: SelectionApprovalOrigins,
|
||||||
|
val approvalProvenance: SelectionApprovalProvenances
|
||||||
|
)
|
||||||
|
|
||||||
|
data class SelectionApprovalOrigins(
|
||||||
|
val title: TranslationSelectionApprovalOrigin,
|
||||||
|
val detail: TranslationSelectionApprovalOrigin,
|
||||||
|
val tags: TranslationSelectionApprovalOrigin
|
||||||
|
)
|
||||||
|
|
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
|
data class SelectionApprovalProvenances(
|
||||||
|
val title: TranslationSelectionApprovalProvenance?,
|
||||||
|
val detail: TranslationSelectionApprovalProvenance?,
|
||||||
|
val tags: TranslationSelectionApprovalProvenance?
|
||||||
|
)
|
||||||
|
|
||||||
|
data class TranslationSelectionApprovalProvenance(
|
||||||
|
val requestId: Long,
|
||||||
|
val candidateIds: List<Long>,
|
||||||
|
val providers: List<ApprovalProvider>,
|
||||||
|
val approvedBy: Long,
|
||||||
|
val approvedAt: Instant
|
||||||
|
)
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.port
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentTranslationOverride
|
||||||
|
|
||||||
|
interface ContentTranslationOverrideStore {
|
||||||
|
fun findByContentId(contentId: Long): List<ContentTranslationOverride>
|
||||||
|
fun save(override: ContentTranslationOverride)
|
||||||
|
fun deleteFields(contentId: Long, fields: Set<ContentSourceFieldKey>, locale: String? = null)
|
||||||
|
}
|
||||||
+18
@@ -0,0 +1,18 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.port
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.SelectionCandidateProgress
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidate
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
|
interface TranslationCandidateStore {
|
||||||
|
fun save(candidate: TranslationCandidate): TranslationCandidate
|
||||||
|
fun nextRequest(now: LocalDateTime): Long?
|
||||||
|
fun lockNext(requestId: Long, now: LocalDateTime): TranslationCandidate?
|
||||||
|
fun lock(candidateId: Long): TranslationCandidate?
|
||||||
|
fun find(candidateId: Long): TranslationCandidate?
|
||||||
|
fun page(requestId: Long, afterId: Long, limit: Int): List<TranslationCandidate>
|
||||||
|
fun progress(requestId: Long): SelectionCandidateProgress
|
||||||
|
fun completeLinked(source: TranslationCandidate)
|
||||||
|
fun discard(requestId: Long, now: LocalDateTime)
|
||||||
|
fun discardUnselected(requestId: Long, selectedIds: Set<Long>, now: LocalDateTime): Int
|
||||||
|
}
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.port
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionRequest
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
interface TranslationSelectionStore {
|
||||||
|
fun find(requestId: Long): TranslationSelectionRequest?
|
||||||
|
fun findCreation(adminId: Long, clientRequestId: UUID): TranslationSelectionRequest?
|
||||||
|
fun lock(requestId: Long): TranslationSelectionRequest?
|
||||||
|
fun nextPreparation(now: LocalDateTime): TranslationSelectionRequest?
|
||||||
|
fun save(request: TranslationSelectionRequest): TranslationSelectionRequest
|
||||||
|
}
|
||||||
@@ -15,7 +15,7 @@ import org.junit.jupiter.api.BeforeEach
|
|||||||
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.mockito.Mockito
|
||||||
import org.springframework.context.ApplicationEventPublisher
|
import org.springframework.transaction.PlatformTransactionManager
|
||||||
import java.util.Optional
|
import java.util.Optional
|
||||||
|
|
||||||
class AdminContentServiceTest {
|
class AdminContentServiceTest {
|
||||||
@@ -44,7 +44,10 @@ class AdminContentServiceTest {
|
|||||||
contentMainTabRepository = contentMainTabRepository,
|
contentMainTabRepository = contentMainTabRepository,
|
||||||
objectMapper = jacksonObjectMapper(),
|
objectMapper = jacksonObjectMapper(),
|
||||||
s3Uploader = s3Uploader,
|
s3Uploader = s3Uploader,
|
||||||
applicationEventPublisher = Mockito.mock(ApplicationEventPublisher::class.java),
|
sourceInvalidation = Mockito.mock(
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationSourceInvalidationService::class.java
|
||||||
|
),
|
||||||
|
transactionManager = Mockito.mock(PlatformTransactionManager::class.java),
|
||||||
bucket = "test-bucket"
|
bucket = "test-bucket"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
+42
-1
@@ -20,6 +20,8 @@ import kr.co.vividnext.sodalive.i18n.translation.TranslationJobStatus
|
|||||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationJobWorker
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationJobWorker
|
||||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationMemory
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationMemory
|
||||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationMemoryRepository
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationMemoryRepository
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationSourceExtractor
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationSourceField
|
||||||
import kr.co.vividnext.sodalive.member.Member
|
import kr.co.vividnext.sodalive.member.Member
|
||||||
import kr.co.vividnext.sodalive.member.MemberRole
|
import kr.co.vividnext.sodalive.member.MemberRole
|
||||||
import org.junit.jupiter.api.Assertions.assertEquals
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
@@ -69,6 +71,7 @@ class AudioContentDetailTranslationHttpIntegrationTest @Autowired constructor(
|
|||||||
private val jdbc: JdbcTemplate,
|
private val jdbc: JdbcTemplate,
|
||||||
private val worker: TranslationJobWorker,
|
private val worker: TranslationJobWorker,
|
||||||
private val scheduler: ResourceTranslationJobScheduler,
|
private val scheduler: ResourceTranslationJobScheduler,
|
||||||
|
private val sourceExtractor: TranslationSourceExtractor,
|
||||||
private val entityManager: EntityManager,
|
private val entityManager: EntityManager,
|
||||||
transactionManager: PlatformTransactionManager
|
transactionManager: PlatformTransactionManager
|
||||||
) {
|
) {
|
||||||
@@ -133,7 +136,45 @@ class AudioContentDetailTranslationHttpIntegrationTest @Autowired constructor(
|
|||||||
assertEquals("ko", response.path("languageCode").textValue())
|
assertEquals("ko", response.path("languageCode").textValue())
|
||||||
assertEquals("ko", transaction.execute { entityManager.find(AudioContent::class.java, fixture.first).languageCode })
|
assertEquals("ko", transaction.execute { entityManager.find(AudioContent::class.java, fixture.first).languageCode })
|
||||||
assertEquals(listOf("원문 제목 원문 설명 #힐링"), provider.detectionQueries)
|
assertEquals(listOf("원문 제목 원문 설명 #힐링"), provider.detectionQueries)
|
||||||
assertEquals(0L, jobs.count())
|
assertEquals(6L, jobs.count())
|
||||||
|
assertEquals(setOf("en", "ja"), jobs.findAll().map { it.targetLanguage }.toSet())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("상세와 추출기는 연관 ID 순서의 활성 태그와 같은 원문을 사용하고 메모리 정규화를 유지한다")
|
||||||
|
fun shouldShareOrderedActiveSourceAcrossExtractorAndDetail() {
|
||||||
|
val fixture = fixture(null, detail = "\t원문 설명 \n")
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
val content = entityManager.find(AudioContent::class.java, fixture.first)
|
||||||
|
content.title = " 원문 제목 \t"
|
||||||
|
val tag = HashTag(" #차분")
|
||||||
|
entityManager.persist(tag)
|
||||||
|
entityManager.persist(
|
||||||
|
AudioContentHashTag().apply {
|
||||||
|
audioContent = content
|
||||||
|
hashTag = tag
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
val response = detail(fixture)
|
||||||
|
val extracted = transaction.execute {
|
||||||
|
entityManager.find(AudioContent::class.java, fixture.first).audioContentHashTags.reverse()
|
||||||
|
sourceExtractor.extract(LanguageTranslationTargetType.CONTENT, fixture.first)
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTranslation(response, "English title", "English detail", "#Healing, #Calm")
|
||||||
|
assertEquals(listOf("원문 제목 원문 설명 #힐링 #차분"), provider.detectionQueries)
|
||||||
|
assertEquals(
|
||||||
|
listOf(
|
||||||
|
TranslationSourceField("title", " 원문 제목 \t"),
|
||||||
|
TranslationSourceField("detail", "\t원문 설명 \n"),
|
||||||
|
TranslationSourceField("tags", "#힐링, #차분")
|
||||||
|
),
|
||||||
|
extracted?.fields
|
||||||
|
)
|
||||||
|
assertEquals(setOf("원문 제목", "원문 설명", "#힐링, #차분"), memories.findAll().map { it.sourceText }.toSet())
|
||||||
|
memories.findAll().forEach { assertEquals(SourceTextNormalizer.hash(it.sourceText), it.sourceHash) }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -125,6 +125,9 @@ class AudioContentServiceTest {
|
|||||||
homeFollowingNewsPublishService = homeFollowingNewsPublishService,
|
homeFollowingNewsPublishService = homeFollowingNewsPublishService,
|
||||||
memberContentPreferenceService = memberContentPreferenceService,
|
memberContentPreferenceService = memberContentPreferenceService,
|
||||||
subtitleJobService = subtitleJobService,
|
subtitleJobService = subtitleJobService,
|
||||||
|
sourceInvalidation = Mockito.mock(
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationSourceInvalidationService::class.java
|
||||||
|
),
|
||||||
transactionManager = Mockito.mock(PlatformTransactionManager::class.java),
|
transactionManager = Mockito.mock(PlatformTransactionManager::class.java),
|
||||||
audioContentBucket = "audio-bucket",
|
audioContentBucket = "audio-bucket",
|
||||||
coverImageBucket = "cover-bucket",
|
coverImageBucket = "cover-bucket",
|
||||||
|
|||||||
+7
-1
@@ -29,6 +29,8 @@ import kr.co.vividnext.sodalive.member.MemberAdapter
|
|||||||
import kr.co.vividnext.sodalive.member.MemberRepository
|
import kr.co.vividnext.sodalive.member.MemberRepository
|
||||||
import kr.co.vividnext.sodalive.member.contentpreference.MemberContentPreferenceService
|
import kr.co.vividnext.sodalive.member.contentpreference.MemberContentPreferenceService
|
||||||
import kr.co.vividnext.sodalive.member.contentpreference.ViewerContentPreference
|
import kr.co.vividnext.sodalive.member.contentpreference.ViewerContentPreference
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence.JpaContentTranslationOverrideStore
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationWriteService
|
||||||
import kr.co.vividnext.sodalive.v2.creator.channel.community.translation.application.CreatorCommunityTranslationService
|
import kr.co.vividnext.sodalive.v2.creator.channel.community.translation.application.CreatorCommunityTranslationService
|
||||||
import kr.co.vividnext.sodalive.v2.creatorname.application.CreatorLocalizedNameService
|
import kr.co.vividnext.sodalive.v2.creatorname.application.CreatorLocalizedNameService
|
||||||
import kr.co.vividnext.sodalive.v2.home.following.application.HomeFollowingNewsPublishService
|
import kr.co.vividnext.sodalive.v2.home.following.application.HomeFollowingNewsPublishService
|
||||||
@@ -70,7 +72,11 @@ import javax.servlet.http.HttpServletResponse
|
|||||||
TranslationReadModelMaterializer::class, LanguageDetectionCacheService::class,
|
TranslationReadModelMaterializer::class, LanguageDetectionCacheService::class,
|
||||||
AudioContentDetailTranslationService::class, TranslationMemoryService::class, TranslationJobWorker::class,
|
AudioContentDetailTranslationService::class, TranslationMemoryService::class, TranslationJobWorker::class,
|
||||||
LanguageDetectListener::class, ContentSeriesController::class, ContentSeriesService::class,
|
LanguageDetectListener::class, ContentSeriesController::class, ContentSeriesService::class,
|
||||||
ContentSeriesDetailTranslationService::class
|
ContentSeriesDetailTranslationService::class,
|
||||||
|
ContentTranslationWriteService::class,
|
||||||
|
JpaContentTranslationOverrideStore::class,
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationSourceInvalidationService::class,
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationLanguageService::class
|
||||||
)
|
)
|
||||||
@MockBean(
|
@MockBean(
|
||||||
classes = [
|
classes = [
|
||||||
|
|||||||
+11
-4
@@ -86,9 +86,13 @@ import javax.persistence.EntityManager
|
|||||||
QueryDslConfig::class,
|
QueryDslConfig::class,
|
||||||
AudioContentThemeQueryRepository::class,
|
AudioContentThemeQueryRepository::class,
|
||||||
LanguageDetectListener::class,
|
LanguageDetectListener::class,
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationSourceInvalidationService::class,
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationLanguageService::class,
|
||||||
CreatorCommunityTranslationService::class,
|
CreatorCommunityTranslationService::class,
|
||||||
TranslationSourceExtractor::class,
|
TranslationSourceExtractor::class,
|
||||||
TranslationReadModelMaterializer::class,
|
TranslationReadModelMaterializer::class,
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationWriteService::class,
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence.JpaContentTranslationOverrideStore::class,
|
||||||
TranslationCoordinator::class,
|
TranslationCoordinator::class,
|
||||||
PapagoTranslationService::class,
|
PapagoTranslationService::class,
|
||||||
ResourceTranslationJobScheduler::class,
|
ResourceTranslationJobScheduler::class,
|
||||||
@@ -691,9 +695,10 @@ class CreatorCommunityLanguageDetectTest @Autowired constructor(
|
|||||||
@Bean
|
@Bean
|
||||||
@Primary
|
@Primary
|
||||||
fun languageDetectionCacheService(
|
fun languageDetectionCacheService(
|
||||||
languageDetectionResultRepository: LanguageDetectionResultRepository
|
languageDetectionResultRepository: LanguageDetectionResultRepository,
|
||||||
|
transactionManager: PlatformTransactionManager
|
||||||
): ControlledLanguageDetectionCacheService {
|
): ControlledLanguageDetectionCacheService {
|
||||||
return ControlledLanguageDetectionCacheService(languageDetectionResultRepository)
|
return ControlledLanguageDetectionCacheService(languageDetectionResultRepository, transactionManager)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@@ -732,14 +737,16 @@ class CreatorCommunityLanguageDetectTest @Autowired constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
class ControlledLanguageDetectionCacheService(
|
class ControlledLanguageDetectionCacheService(
|
||||||
languageDetectionResultRepository: LanguageDetectionResultRepository
|
languageDetectionResultRepository: LanguageDetectionResultRepository,
|
||||||
|
transactionManager: PlatformTransactionManager
|
||||||
) : LanguageDetectionCacheService(
|
) : LanguageDetectionCacheService(
|
||||||
languageDetectionResultRepository,
|
languageDetectionResultRepository,
|
||||||
listOf(object : LanguageDetectionProvider {
|
listOf(object : LanguageDetectionProvider {
|
||||||
override val providerId = LanguageProviderId.PAPAGO
|
override val providerId = LanguageProviderId.PAPAGO
|
||||||
override fun detect(query: String): String? = null
|
override fun detect(query: String): String? = null
|
||||||
}),
|
}),
|
||||||
LanguageProviderProperties(listOf(LanguageProviderId.PAPAGO), listOf(LanguageProviderId.PAPAGO))
|
LanguageProviderProperties(listOf(LanguageProviderId.PAPAGO), listOf(LanguageProviderId.PAPAGO)),
|
||||||
|
transactionManager
|
||||||
) {
|
) {
|
||||||
val detections = ConcurrentHashMap<String, String>()
|
val detections = ConcurrentHashMap<String, String>()
|
||||||
val queries = ConcurrentLinkedQueue<String>()
|
val queries = ConcurrentLinkedQueue<String>()
|
||||||
|
|||||||
+4
@@ -79,9 +79,13 @@ import java.util.concurrent.atomic.AtomicInteger
|
|||||||
PapagoTranslationService::class,
|
PapagoTranslationService::class,
|
||||||
TranslationCoordinator::class,
|
TranslationCoordinator::class,
|
||||||
LanguageDetectListener::class,
|
LanguageDetectListener::class,
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationSourceInvalidationService::class,
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationLanguageService::class,
|
||||||
CreatorCommunityTranslationService::class,
|
CreatorCommunityTranslationService::class,
|
||||||
TranslationSourceExtractor::class,
|
TranslationSourceExtractor::class,
|
||||||
TranslationReadModelMaterializer::class,
|
TranslationReadModelMaterializer::class,
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationWriteService::class,
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence.JpaContentTranslationOverrideStore::class,
|
||||||
ResourceTranslationJobScheduler::class,
|
ResourceTranslationJobScheduler::class,
|
||||||
TranslationJobScheduler::class
|
TranslationJobScheduler::class
|
||||||
)
|
)
|
||||||
|
|||||||
+8
-1
@@ -9,6 +9,9 @@ import org.junit.jupiter.api.Assertions.assertEquals
|
|||||||
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.mockito.Mockito
|
||||||
|
import org.springframework.transaction.PlatformTransactionManager
|
||||||
|
import org.springframework.transaction.TransactionDefinition
|
||||||
|
import org.springframework.transaction.support.SimpleTransactionStatus
|
||||||
|
|
||||||
class LanguageDetectionCacheServiceTest {
|
class LanguageDetectionCacheServiceTest {
|
||||||
@Test
|
@Test
|
||||||
@@ -111,7 +114,11 @@ class LanguageDetectionCacheServiceTest {
|
|||||||
return LanguageDetectionCacheService(
|
return LanguageDetectionCacheService(
|
||||||
repository,
|
repository,
|
||||||
listOf(provider),
|
listOf(provider),
|
||||||
LanguageProviderProperties(listOf(LanguageProviderId.PAPAGO), listOf(LanguageProviderId.PAPAGO))
|
LanguageProviderProperties(listOf(LanguageProviderId.PAPAGO), listOf(LanguageProviderId.PAPAGO)),
|
||||||
|
Mockito.mock(PlatformTransactionManager::class.java).also {
|
||||||
|
Mockito.`when`(it.getTransaction(Mockito.any(TransactionDefinition::class.java)))
|
||||||
|
.thenReturn(SimpleTransactionStatus())
|
||||||
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-2
@@ -55,6 +55,10 @@ import javax.persistence.EntityManager
|
|||||||
QueryDslConfig::class,
|
QueryDslConfig::class,
|
||||||
AudioContentThemeQueryRepository::class,
|
AudioContentThemeQueryRepository::class,
|
||||||
LanguageDetectListener::class,
|
LanguageDetectListener::class,
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationWriteService::class,
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence.JpaContentTranslationOverrideStore::class,
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationSourceInvalidationService::class,
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationLanguageService::class,
|
||||||
TranslationSourceExtractor::class,
|
TranslationSourceExtractor::class,
|
||||||
ResourceTranslationJobScheduler::class,
|
ResourceTranslationJobScheduler::class,
|
||||||
TranslationJobScheduler::class,
|
TranslationJobScheduler::class,
|
||||||
@@ -137,7 +141,8 @@ class MetadataLanguageDetectTransactionTest @Autowired constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun prepareDetection(target: MetadataTarget): LanguageDetectEvent {
|
private fun prepareDetection(target: MetadataTarget): LanguageDetectEvent {
|
||||||
Mockito.`when`(detectionCache.detectWithCache("원문 제목"))
|
val query = if (target == MetadataTarget.CONTENT) "원문 제목 원문 설명" else "원문 제목"
|
||||||
|
Mockito.`when`(detectionCache.detectWithCache(query))
|
||||||
.thenReturn(DetectedLanguage("ko", LanguageProviderId.PAPAGO))
|
.thenReturn(DetectedLanguage("ko", LanguageProviderId.PAPAGO))
|
||||||
val id = transaction.execute {
|
val id = transaction.execute {
|
||||||
val owner = persist(Member(password = "password", nickname = "metadata-owner", role = MemberRole.CREATOR))
|
val owner = persist(Member(password = "password", nickname = "metadata-owner", role = MemberRole.CREATOR))
|
||||||
@@ -166,7 +171,7 @@ class MetadataLanguageDetectTransactionTest @Autowired constructor(
|
|||||||
}
|
}
|
||||||
requireNotNull(persist(metadata).id)
|
requireNotNull(persist(metadata).id)
|
||||||
}!!
|
}!!
|
||||||
return LanguageDetectEvent(id, "원문 제목", LanguageDetectTargetType.valueOf(target.name))
|
return LanguageDetectEvent(id, query, LanguageDetectTargetType.valueOf(target.name))
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun <T : BaseEntity> persist(entity: T): T {
|
private fun <T : BaseEntity> persist(entity: T): T {
|
||||||
|
|||||||
+15
-2
@@ -7,7 +7,6 @@ import kr.co.vividnext.sodalive.content.AudioContent
|
|||||||
import kr.co.vividnext.sodalive.content.AudioContentDetailTranslationService
|
import kr.co.vividnext.sodalive.content.AudioContentDetailTranslationService
|
||||||
import kr.co.vividnext.sodalive.content.AudioContentRepository
|
import kr.co.vividnext.sodalive.content.AudioContentRepository
|
||||||
import kr.co.vividnext.sodalive.content.AudioContentService
|
import kr.co.vividnext.sodalive.content.AudioContentService
|
||||||
import kr.co.vividnext.sodalive.content.LanguageDetectionCacheService
|
|
||||||
import kr.co.vividnext.sodalive.content.PlaybackTrackingRepository
|
import kr.co.vividnext.sodalive.content.PlaybackTrackingRepository
|
||||||
import kr.co.vividnext.sodalive.content.comment.AudioContentCommentRepository
|
import kr.co.vividnext.sodalive.content.comment.AudioContentCommentRepository
|
||||||
import kr.co.vividnext.sodalive.content.hashtag.HashTagRepository
|
import kr.co.vividnext.sodalive.content.hashtag.HashTagRepository
|
||||||
@@ -28,6 +27,7 @@ import kr.co.vividnext.sodalive.member.MemberRepository
|
|||||||
import kr.co.vividnext.sodalive.member.block.BlockMemberRepository
|
import kr.co.vividnext.sodalive.member.block.BlockMemberRepository
|
||||||
import kr.co.vividnext.sodalive.member.contentpreference.MemberContentPreferenceService
|
import kr.co.vividnext.sodalive.member.contentpreference.MemberContentPreferenceService
|
||||||
import kr.co.vividnext.sodalive.support.originalLocalizedNameService
|
import kr.co.vividnext.sodalive.support.originalLocalizedNameService
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationSourceInvalidationService
|
||||||
import kr.co.vividnext.sodalive.v2.home.following.application.HomeFollowingNewsPublishService
|
import kr.co.vividnext.sodalive.v2.home.following.application.HomeFollowingNewsPublishService
|
||||||
import kr.co.vividnext.sodalive.v2.recommendation.application.CreatorContentViewHistoryService
|
import kr.co.vividnext.sodalive.v2.recommendation.application.CreatorContentViewHistoryService
|
||||||
import kr.co.vividnext.sodalive.v2.subtitle.application.SubtitleJobService
|
import kr.co.vividnext.sodalive.v2.subtitle.application.SubtitleJobService
|
||||||
@@ -90,7 +90,17 @@ class AudioContentReadRetryTest : MetadataReadRetryTestSupport() {
|
|||||||
detailTranslationService = AudioContentDetailTranslationService(
|
detailTranslationService = AudioContentDetailTranslationService(
|
||||||
repository,
|
repository,
|
||||||
translations,
|
translations,
|
||||||
Mockito.mock(LanguageDetectionCacheService::class.java),
|
Mockito.mock(
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationLanguageService::class.java
|
||||||
|
),
|
||||||
|
Mockito.mock(
|
||||||
|
ContentTranslationSourceInvalidationService::class.java
|
||||||
|
),
|
||||||
|
Mockito.mock(
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationWriteService::class.java
|
||||||
|
).also {
|
||||||
|
Mockito.`when`(it.lockContent(10L)).thenReturn(content)
|
||||||
|
},
|
||||||
Mockito.mock(TranslationMemoryService::class.java),
|
Mockito.mock(TranslationMemoryService::class.java),
|
||||||
Mockito.mock(TranslationReadModelMaterializer::class.java),
|
Mockito.mock(TranslationReadModelMaterializer::class.java),
|
||||||
scheduler,
|
scheduler,
|
||||||
@@ -108,6 +118,9 @@ class AudioContentReadRetryTest : MetadataReadRetryTestSupport() {
|
|||||||
homeFollowingNewsPublishService = Mockito.mock(HomeFollowingNewsPublishService::class.java),
|
homeFollowingNewsPublishService = Mockito.mock(HomeFollowingNewsPublishService::class.java),
|
||||||
memberContentPreferenceService = Mockito.mock(MemberContentPreferenceService::class.java),
|
memberContentPreferenceService = Mockito.mock(MemberContentPreferenceService::class.java),
|
||||||
subtitleJobService = Mockito.mock(SubtitleJobService::class.java),
|
subtitleJobService = Mockito.mock(SubtitleJobService::class.java),
|
||||||
|
sourceInvalidation = Mockito.mock(
|
||||||
|
ContentTranslationSourceInvalidationService::class.java
|
||||||
|
),
|
||||||
transactionManager = transactionManager,
|
transactionManager = transactionManager,
|
||||||
audioContentBucket = "audio-bucket",
|
audioContentBucket = "audio-bucket",
|
||||||
coverImageBucket = "cover-bucket",
|
coverImageBucket = "cover-bucket",
|
||||||
|
|||||||
+12
-5
@@ -142,7 +142,7 @@ class CompletedContentTagRepairTest : MetadataReadRetryTestSupport() {
|
|||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@CsvSource("false,language", "false,hash", "true,language", "true,hash")
|
@CsvSource("false,language", "false,hash", "true,language", "true,hash")
|
||||||
@DisplayName("현재 원문 언어 또는 해시가 다른 완료 태그는 수정과 조회에서 재개하지 않는다")
|
@DisplayName("다른 원문 언어·해시의 완료 태그는 보존하고 수정 요청만 현재 identity 작업을 만든다")
|
||||||
fun shouldPreserveCompletedTagsForDifferentCurrentSource(readRequest: Boolean, mismatch: String) {
|
fun shouldPreserveCompletedTagsForDifferentCurrentSource(readRequest: Boolean, mismatch: String) {
|
||||||
val source = givenSource(LanguageTranslationTargetType.CONTENT, "tags", sourceText)
|
val source = givenSource(LanguageTranslationTargetType.CONTENT, "tags", sourceText)
|
||||||
val previousSource = if (mismatch == "language") {
|
val previousSource = if (mismatch == "language") {
|
||||||
@@ -160,7 +160,13 @@ class CompletedContentTagRepairTest : MetadataReadRetryTestSupport() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
assertPreserved(job)
|
assertPreserved(job)
|
||||||
assertEquals(if (!readRequest && mismatch == "hash") 2L else 1L, jobs.count())
|
assertEquals(if (readRequest) 1L else 2L, jobs.count())
|
||||||
|
if (!readRequest) {
|
||||||
|
val current = jobs.findAll().single { it.id != job.id }
|
||||||
|
assertEquals("ko", current.sourceLanguage)
|
||||||
|
assertEquals(SourceTextNormalizer.hash(sourceText), current.sourceHash)
|
||||||
|
assertEquals(TranslationJobStatus.PENDING, current.status)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@@ -200,16 +206,17 @@ class CompletedContentTagRepairTest : MetadataReadRetryTestSupport() {
|
|||||||
jobs.saveAndFlush(claimed)
|
jobs.saveAndFlush(claimed)
|
||||||
}
|
}
|
||||||
snapshot
|
snapshot
|
||||||
}.`when`(repository).findByResourceTypeAndResourceIdAndFieldKeyAndTargetLanguageAndSourceHash(
|
}.`when`(repository).findByResourceTypeAndResourceIdAndFieldKeyAndTargetLanguageAndSourceHashAndSourceLanguage(
|
||||||
source.resourceType,
|
source.resourceType,
|
||||||
source.resourceId,
|
source.resourceId,
|
||||||
"tags",
|
"tags",
|
||||||
"en",
|
"en",
|
||||||
SourceTextNormalizer.hash(sourceText)
|
SourceTextNormalizer.hash(sourceText),
|
||||||
|
source.sourceLanguage
|
||||||
)
|
)
|
||||||
|
|
||||||
TransactionTemplate(transactionManager).executeWithoutResult {
|
TransactionTemplate(transactionManager).executeWithoutResult {
|
||||||
schedule(source, TranslationJobScheduler(repository, coordinator, translations))
|
schedule(source, TranslationJobScheduler(repository, coordinator, translations, entityManager))
|
||||||
}
|
}
|
||||||
|
|
||||||
val claimed = jobs.findById(requireNotNull(job.id)).orElseThrow()
|
val claimed = jobs.findById(requireNotNull(job.id)).orElseThrow()
|
||||||
|
|||||||
+58
-12
@@ -2,9 +2,14 @@ package kr.co.vividnext.sodalive.i18n.translation
|
|||||||
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode
|
import com.fasterxml.jackson.databind.JsonNode
|
||||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContent
|
||||||
|
import kr.co.vividnext.sodalive.content.hashtag.AudioContentHashTag
|
||||||
|
import kr.co.vividnext.sodalive.content.hashtag.HashTag
|
||||||
|
import kr.co.vividnext.sodalive.content.theme.AudioContentTheme
|
||||||
import kr.co.vividnext.sodalive.content.translation.ContentTranslation
|
import kr.co.vividnext.sodalive.content.translation.ContentTranslation
|
||||||
import kr.co.vividnext.sodalive.content.translation.ContentTranslationPayload
|
import kr.co.vividnext.sodalive.content.translation.ContentTranslationPayload
|
||||||
import kr.co.vividnext.sodalive.content.translation.ContentTranslationRepository
|
import kr.co.vividnext.sodalive.content.translation.ContentTranslationRepository
|
||||||
|
import kr.co.vividnext.sodalive.member.Member
|
||||||
import org.junit.jupiter.api.AfterEach
|
import org.junit.jupiter.api.AfterEach
|
||||||
import org.junit.jupiter.api.Assertions.assertEquals
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
import org.junit.jupiter.api.Assertions.assertFalse
|
import org.junit.jupiter.api.Assertions.assertFalse
|
||||||
@@ -21,11 +26,14 @@ import org.springframework.boot.test.context.TestConfiguration
|
|||||||
import org.springframework.context.annotation.Bean
|
import org.springframework.context.annotation.Bean
|
||||||
import org.springframework.context.annotation.Import
|
import org.springframework.context.annotation.Import
|
||||||
import org.springframework.jdbc.core.JdbcTemplate
|
import org.springframework.jdbc.core.JdbcTemplate
|
||||||
|
import org.springframework.transaction.support.TransactionTemplate
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
@Import(
|
@Import(
|
||||||
TranslationCoordinator::class,
|
TranslationCoordinator::class,
|
||||||
TranslationReadModelMaterializer::class,
|
TranslationReadModelMaterializer::class,
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationWriteService::class,
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence.JpaContentTranslationOverrideStore::class,
|
||||||
ContentTagTranslationIntegrationTest.Providers::class
|
ContentTagTranslationIntegrationTest.Providers::class
|
||||||
)
|
)
|
||||||
class ContentTagTranslationIntegrationTest : MetadataReadRetryTestSupport() {
|
class ContentTagTranslationIntegrationTest : MetadataReadRetryTestSupport() {
|
||||||
@@ -51,6 +59,8 @@ class ContentTagTranslationIntegrationTest : MetadataReadRetryTestSupport() {
|
|||||||
private lateinit var papago: PapagoTagProvider
|
private lateinit var papago: PapagoTagProvider
|
||||||
|
|
||||||
private val sourceText = "#힐링,#수면"
|
private val sourceText = "#힐링,#수면"
|
||||||
|
private var contentId = 0L
|
||||||
|
private lateinit var sourceTag: HashTag
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
fun prepareProviders() {
|
fun prepareProviders() {
|
||||||
@@ -59,12 +69,22 @@ class ContentTagTranslationIntegrationTest : MetadataReadRetryTestSupport() {
|
|||||||
papago.result = "#Healing, #Sleep"
|
papago.result = "#Healing, #Sleep"
|
||||||
papago.calls = 0
|
papago.calls = 0
|
||||||
papago.fail = false
|
papago.fail = false
|
||||||
|
val creator = persist(Member(password = "test", nickname = "tag-creator"))
|
||||||
|
val theme = persist(AudioContentTheme("tag-theme", "tag.png"))
|
||||||
|
sourceTag = HashTag(sourceText)
|
||||||
|
TransactionTemplate(transactionManager).executeWithoutResult { entityManager.persist(sourceTag) }
|
||||||
|
contentId = requireNotNull(persist(AudioContent("", "", "ko").apply { member = creator; this.theme = theme }).id)
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
fun removeWorkerResults() {
|
fun removeWorkerResults() {
|
||||||
translations.deleteAllInBatch()
|
translations.deleteAllInBatch()
|
||||||
memories.deleteAllInBatch()
|
memories.deleteAllInBatch()
|
||||||
|
TransactionTemplate(transactionManager).executeWithoutResult {
|
||||||
|
entityManager.createQuery("delete from AudioContentHashTag t where t.audioContent.id = :id")
|
||||||
|
.setParameter("id", contentId).executeUpdate()
|
||||||
|
entityManager.remove(entityManager.find(HashTag::class.java, sourceTag.id))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@@ -121,12 +141,12 @@ class ContentTagTranslationIntegrationTest : MetadataReadRetryTestSupport() {
|
|||||||
@Test
|
@Test
|
||||||
@DisplayName("AWS 미번역 태그 캐시만 있을 때는 정상 번역 응답으로 조립하지 않는다")
|
@DisplayName("AWS 미번역 태그 캐시만 있을 때는 정상 번역 응답으로 조립하지 않는다")
|
||||||
fun shouldNotMaterializeUnchangedAwsTagCache() {
|
fun shouldNotMaterializeUnchangedAwsTagCache() {
|
||||||
givenSource(LanguageTranslationTargetType.CONTENT, "tags", sourceText)
|
givenContentSource("tags")
|
||||||
memories.saveAndFlush(
|
memories.saveAndFlush(
|
||||||
TranslationMemory(SourceTextNormalizer.hash(sourceText), sourceText, "ko", "en", aws.result, "aws", "test")
|
TranslationMemory(SourceTextNormalizer.hash(sourceText), sourceText, "ko", "en", aws.result, "aws", "test")
|
||||||
)
|
)
|
||||||
|
|
||||||
assertFalse(materializer.materialize(LanguageTranslationTargetType.CONTENT, 10L, "en"))
|
assertFalse(materializer.materialize(LanguageTranslationTargetType.CONTENT, contentId, "en"))
|
||||||
|
|
||||||
assertEquals(0L, translations.count())
|
assertEquals(0L, translations.count())
|
||||||
}
|
}
|
||||||
@@ -168,16 +188,16 @@ class ContentTagTranslationIntegrationTest : MetadataReadRetryTestSupport() {
|
|||||||
memories.saveAndFlush(
|
memories.saveAndFlush(
|
||||||
TranslationMemory(SourceTextNormalizer.hash(sourceText), sourceText, "ko", "en", aws.result, "aws", "test")
|
TranslationMemory(SourceTextNormalizer.hash(sourceText), sourceText, "ko", "en", aws.result, "aws", "test")
|
||||||
)
|
)
|
||||||
translations.saveAndFlush(ContentTranslation(10L, "en", ContentTranslationPayload("", "", aws.result)))
|
translations.saveAndFlush(ContentTranslation(contentId, "en", ContentTranslationPayload("", "", aws.result)))
|
||||||
storeRenderedPayloadAsJsonObject()
|
storeRenderedPayloadAsJsonObject()
|
||||||
papago.result = sourceText
|
papago.result = sourceText
|
||||||
|
|
||||||
scheduler.restartFailedResourceTranslations(LanguageTranslationTargetType.CONTENT, listOf(10L), "en")
|
scheduler.restartFailedResourceTranslations(LanguageTranslationTargetType.CONTENT, listOf(contentId), "en")
|
||||||
assertEquals(TranslationJobStatus.PENDING, jobs.findAll().single().status)
|
assertEquals(TranslationJobStatus.PENDING, jobs.findAll().single().status)
|
||||||
assertTrue(worker().processNextGroup())
|
assertTrue(worker().processNextGroup())
|
||||||
assertEquals(sourceText, renderedTags())
|
assertEquals(sourceText, renderedTags())
|
||||||
storeRenderedPayloadAsJsonObject()
|
storeRenderedPayloadAsJsonObject()
|
||||||
scheduler.restartFailedResourceTranslations(LanguageTranslationTargetType.CONTENT, listOf(10L), "en")
|
scheduler.restartFailedResourceTranslations(LanguageTranslationTargetType.CONTENT, listOf(contentId), "en")
|
||||||
|
|
||||||
val completed = jobs.findAll().single()
|
val completed = jobs.findAll().single()
|
||||||
assertEquals(job.id, completed.id)
|
assertEquals(job.id, completed.id)
|
||||||
@@ -217,8 +237,32 @@ class ContentTagTranslationIntegrationTest : MetadataReadRetryTestSupport() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun prepareJob(fieldKey: String = "tags") {
|
private fun prepareJob(fieldKey: String = "tags") {
|
||||||
givenSource(LanguageTranslationTargetType.CONTENT, fieldKey, sourceText)
|
givenContentSource(fieldKey)
|
||||||
scheduler.scheduleResourceTranslation(LanguageTranslationTargetType.CONTENT, 10L, "en")
|
scheduler.scheduleResourceTranslation(LanguageTranslationTargetType.CONTENT, contentId, "en")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun givenContentSource(fieldKey: String) {
|
||||||
|
val source = TranslationSource(
|
||||||
|
LanguageTranslationTargetType.CONTENT,
|
||||||
|
contentId,
|
||||||
|
"ko",
|
||||||
|
listOf(TranslationSourceField(fieldKey, sourceText))
|
||||||
|
)
|
||||||
|
Mockito.`when`(sourceExtractor.extract(LanguageTranslationTargetType.CONTENT, contentId)).thenReturn(source)
|
||||||
|
if (fieldKey == "title") {
|
||||||
|
TransactionTemplate(transactionManager).executeWithoutResult {
|
||||||
|
entityManager.find(AudioContent::class.java, contentId).title = sourceText
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
TransactionTemplate(transactionManager).executeWithoutResult {
|
||||||
|
entityManager.persist(
|
||||||
|
AudioContentHashTag().apply {
|
||||||
|
audioContent = entityManager.getReference(AudioContent::class.java, contentId)
|
||||||
|
hashTag = sourceTag
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun worker(): TranslationJobWorker {
|
private fun worker(): TranslationJobWorker {
|
||||||
@@ -243,16 +287,18 @@ class ContentTagTranslationIntegrationTest : MetadataReadRetryTestSupport() {
|
|||||||
|
|
||||||
private fun storeRenderedPayloadAsJsonObject() {
|
private fun storeRenderedPayloadAsJsonObject() {
|
||||||
jdbc.update(
|
jdbc.update(
|
||||||
"update content_translation set rendered_payload = ? format json where content_id = 10",
|
"update content_translation set rendered_payload = ? format json where content_id = ?",
|
||||||
renderedPayload().toString()
|
renderedPayload().toString(),
|
||||||
|
contentId
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun renderedPayload(): JsonNode {
|
private fun renderedPayload(): JsonNode {
|
||||||
val raw = jdbc.queryForObject(
|
val raw = jdbc.queryForObject(
|
||||||
"select rendered_payload from content_translation where content_id = 10",
|
"select rendered_payload from content_translation where content_id = ?",
|
||||||
String::class.java
|
String::class.java,
|
||||||
)!!
|
contentId
|
||||||
|
)
|
||||||
val mapper = jacksonObjectMapper()
|
val mapper = jacksonObjectMapper()
|
||||||
val value = mapper.readTree(raw)
|
val value = mapper.readTree(raw)
|
||||||
// H2 JSON은 converter가 저장한 JSON 문자열을 한 겹 감싸 반환한다.
|
// H2 JSON은 converter가 저장한 JSON 문자열을 한 겹 감싸 반환한다.
|
||||||
|
|||||||
+24
-3
@@ -12,11 +12,17 @@ import org.junit.jupiter.api.Assertions.assertThrows
|
|||||||
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.mockito.Mockito
|
||||||
|
import org.springframework.transaction.PlatformTransactionManager
|
||||||
|
import org.springframework.transaction.TransactionDefinition
|
||||||
|
import org.springframework.transaction.support.SimpleTransactionStatus
|
||||||
|
|
||||||
class LanguageDetectionProviderTest {
|
class LanguageDetectionProviderTest {
|
||||||
private val repository = Mockito.mock(LanguageDetectionResultRepository::class.java)
|
private val repository = Mockito.mock(LanguageDetectionResultRepository::class.java)
|
||||||
private val query = "original source"
|
private val query = "original source"
|
||||||
private val hash = SourceTextNormalizer.hash(query)
|
private val hash = SourceTextNormalizer.hash(query)
|
||||||
|
private val transactionManager = Mockito.mock(PlatformTransactionManager::class.java).also {
|
||||||
|
Mockito.`when`(it.getTransaction(Mockito.any(TransactionDefinition::class.java))).thenReturn(SimpleTransactionStatus())
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("기본 감지 순서에서 AWS 예외와 빈 결과는 Papago로 전환한다")
|
@DisplayName("기본 감지 순서에서 AWS 예외와 빈 결과는 Papago로 전환한다")
|
||||||
@@ -26,7 +32,12 @@ class LanguageDetectionProviderTest {
|
|||||||
val aws = RecordingDetectionProvider(AWS, failure)
|
val aws = RecordingDetectionProvider(AWS, failure)
|
||||||
val papago = RecordingDetectionProvider(PAPAGO) { "ko" }
|
val papago = RecordingDetectionProvider(PAPAGO) { "ko" }
|
||||||
Mockito.`when`(repository.findDetectedLanguageForUpdate(hash, "papago", "v1")).thenReturn("ko")
|
Mockito.`when`(repository.findDetectedLanguageForUpdate(hash, "papago", "v1")).thenReturn("ko")
|
||||||
val detector = LanguageDetectionCacheService(repository, listOf(papago, aws), LanguageProviderProperties())
|
val detector = LanguageDetectionCacheService(
|
||||||
|
repository,
|
||||||
|
listOf(papago, aws),
|
||||||
|
LanguageProviderProperties(),
|
||||||
|
transactionManager
|
||||||
|
)
|
||||||
|
|
||||||
assertEquals(DetectedLanguage("ko", PAPAGO), detector.detectWithCache(query))
|
assertEquals(DetectedLanguage("ko", PAPAGO), detector.detectWithCache(query))
|
||||||
assertEquals(listOf(query), aws.queries)
|
assertEquals(listOf(query), aws.queries)
|
||||||
@@ -41,7 +52,12 @@ class LanguageDetectionProviderTest {
|
|||||||
val papago = RecordingDetectionProvider(PAPAGO) { "ko" }
|
val papago = RecordingDetectionProvider(PAPAGO) { "ko" }
|
||||||
Mockito.`when`(repository.findBySourceHashAndProviderAndNormalizationVersion(hash, "papago", "v1"))
|
Mockito.`when`(repository.findBySourceHashAndProviderAndNormalizationVersion(hash, "papago", "v1"))
|
||||||
.thenReturn(LanguageDetectionResult(hash, "sample", "ja", "papago", null, "v1"))
|
.thenReturn(LanguageDetectionResult(hash, "sample", "ja", "papago", null, "v1"))
|
||||||
val detector = LanguageDetectionCacheService(repository, listOf(papago, aws), LanguageProviderProperties())
|
val detector = LanguageDetectionCacheService(
|
||||||
|
repository,
|
||||||
|
listOf(papago, aws),
|
||||||
|
LanguageProviderProperties(),
|
||||||
|
transactionManager
|
||||||
|
)
|
||||||
|
|
||||||
assertEquals(DetectedLanguage("ja", PAPAGO), detector.detectWithCache(query))
|
assertEquals(DetectedLanguage("ja", PAPAGO), detector.detectWithCache(query))
|
||||||
assertEquals(emptyList<String>(), aws.queries + papago.queries)
|
assertEquals(emptyList<String>(), aws.queries + papago.queries)
|
||||||
@@ -125,7 +141,12 @@ class LanguageDetectionProviderTest {
|
|||||||
private fun service(
|
private fun service(
|
||||||
providers: List<LanguageDetectionProvider>,
|
providers: List<LanguageDetectionProvider>,
|
||||||
order: List<LanguageProviderId> = providers.map { it.providerId }
|
order: List<LanguageProviderId> = providers.map { it.providerId }
|
||||||
) = LanguageDetectionCacheService(repository, providers, LanguageProviderProperties(order, listOf(PAPAGO)))
|
) = LanguageDetectionCacheService(
|
||||||
|
repository,
|
||||||
|
providers,
|
||||||
|
LanguageProviderProperties(order, listOf(PAPAGO)),
|
||||||
|
transactionManager
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
class RecordingDetectionProvider(
|
class RecordingDetectionProvider(
|
||||||
|
|||||||
+2
@@ -66,6 +66,8 @@ import java.util.UUID
|
|||||||
TranslationJobWorker::class,
|
TranslationJobWorker::class,
|
||||||
TranslationMemoryService::class,
|
TranslationMemoryService::class,
|
||||||
TranslationReadModelMaterializer::class,
|
TranslationReadModelMaterializer::class,
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationWriteService::class,
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence.JpaContentTranslationOverrideStore::class,
|
||||||
TranslationSourceExtractor::class,
|
TranslationSourceExtractor::class,
|
||||||
TranslationJobScheduler::class,
|
TranslationJobScheduler::class,
|
||||||
ResourceTranslationJobScheduler::class,
|
ResourceTranslationJobScheduler::class,
|
||||||
|
|||||||
+13
-7
@@ -82,6 +82,8 @@ import javax.persistence.EntityManager
|
|||||||
AudioContentDetailTranslationService::class,
|
AudioContentDetailTranslationService::class,
|
||||||
TranslationMemoryService::class,
|
TranslationMemoryService::class,
|
||||||
TranslationReadModelMaterializer::class,
|
TranslationReadModelMaterializer::class,
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationWriteService::class,
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence.JpaContentTranslationOverrideStore::class,
|
||||||
ExplorerQueryRepository::class,
|
ExplorerQueryRepository::class,
|
||||||
SodaMessageSource::class,
|
SodaMessageSource::class,
|
||||||
AudioContentThemeQueryRepository::class,
|
AudioContentThemeQueryRepository::class,
|
||||||
@@ -91,6 +93,8 @@ import javax.persistence.EntityManager
|
|||||||
TranslationJobSchedulerTestConfiguration::class,
|
TranslationJobSchedulerTestConfiguration::class,
|
||||||
LanguageTranslationListener::class,
|
LanguageTranslationListener::class,
|
||||||
LanguageDetectListener::class,
|
LanguageDetectListener::class,
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationSourceInvalidationService::class,
|
||||||
|
kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationLanguageService::class,
|
||||||
MetadataTranslationHttpIntegrationTest.Configuration::class
|
MetadataTranslationHttpIntegrationTest.Configuration::class
|
||||||
)
|
)
|
||||||
@MockBean(
|
@MockBean(
|
||||||
@@ -215,7 +219,7 @@ class MetadataTranslationHttpIntegrationTest @Autowired constructor(
|
|||||||
.andExpect(jsonPath("$.data.translated.detail").value("Full detail"))
|
.andExpect(jsonPath("$.data.translated.detail").value("Full detail"))
|
||||||
.andExpect(jsonPath("$.data.translated.tags").value("#Active"))
|
.andExpect(jsonPath("$.data.translated.tags").value("#Active"))
|
||||||
assertEquals(languageCode, outerContent.languageCode)
|
assertEquals(languageCode, outerContent.languageCode)
|
||||||
assertEquals(0, jobs.findAll().count { it.resourceId == contentId })
|
assertEquals(6, jobs.findAll().count { it.resourceId == contentId })
|
||||||
Mockito.verify(detectionCache).detectWithCache(query)
|
Mockito.verify(detectionCache).detectWithCache(query)
|
||||||
}
|
}
|
||||||
transaction.executeWithoutResult {
|
transaction.executeWithoutResult {
|
||||||
@@ -224,7 +228,9 @@ class MetadataTranslationHttpIntegrationTest @Autowired constructor(
|
|||||||
val stored = jobs.findAll().filter {
|
val stored = jobs.findAll().filter {
|
||||||
it.resourceType == LanguageTranslationTargetType.CONTENT && it.resourceId == contentId
|
it.resourceType == LanguageTranslationTargetType.CONTENT && it.resourceId == contentId
|
||||||
}
|
}
|
||||||
assertEquals(0, stored.size)
|
assertEquals(6, stored.size)
|
||||||
|
assertEquals(setOf("title", "detail", "tags"), stored.map { it.fieldKey }.toSet())
|
||||||
|
assertEquals(setOf("en", "ja"), stored.map { it.targetLanguage }.toSet())
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
SecurityContextHolder.clearContext()
|
SecurityContextHolder.clearContext()
|
||||||
@@ -246,8 +252,8 @@ class MetadataTranslationHttpIntegrationTest @Autowired constructor(
|
|||||||
entityManager.persist(content)
|
entityManager.persist(content)
|
||||||
val failed = jobs.saveAndFlush(
|
val failed = jobs.saveAndFlush(
|
||||||
TranslationJob(
|
TranslationJob(
|
||||||
LanguageTranslationTargetType.CONTENT, content.id!!, "title", SourceTextNormalizer.hash("원문 제목"),
|
LanguageTranslationTargetType.CONTENT, content.id!!, "title", SourceTextNormalizer.hash("수정 제목"),
|
||||||
"원문 제목", "ko", "en", TranslationJobStatus.FAILED, 6, "exhausted", LocalDateTime.of(2040, 1, 1, 0, 0)
|
"수정 제목", "ko", "en", TranslationJobStatus.FAILED, 6, "exhausted", LocalDateTime.of(2040, 1, 1, 0, 0)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
content.id!! to failed.id!!
|
content.id!! to failed.id!!
|
||||||
@@ -261,7 +267,7 @@ class MetadataTranslationHttpIntegrationTest @Autowired constructor(
|
|||||||
"request",
|
"request",
|
||||||
"",
|
"",
|
||||||
"application/json",
|
"application/json",
|
||||||
"""{"id":$contentId,"title":"원문 제목","isDefaultCoverImage":false}""".toByteArray()
|
"""{"id":$contentId,"title":"수정 제목","isDefaultCoverImage":false}""".toByteArray()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.with { request -> request.apply { method = "PUT" } }
|
.with { request -> request.apply { method = "PUT" } }
|
||||||
@@ -276,10 +282,10 @@ class MetadataTranslationHttpIntegrationTest @Autowired constructor(
|
|||||||
assertNull(restarted.lastErrorMessage)
|
assertNull(restarted.lastErrorMessage)
|
||||||
val stored = jobs.findAll().filter { it.resourceId == contentId }
|
val stored = jobs.findAll().filter { it.resourceId == contentId }
|
||||||
assertEquals(
|
assertEquals(
|
||||||
setOf("title" to "en", "title" to "ja", "detail" to "en", "detail" to "ja"),
|
setOf("title" to "en", "title" to "ja"),
|
||||||
stored.map { it.fieldKey to it.targetLanguage }.toSet()
|
stored.map { it.fieldKey to it.targetLanguage }.toSet()
|
||||||
)
|
)
|
||||||
assertEquals(4, stored.size)
|
assertEquals(2, stored.size)
|
||||||
}
|
}
|
||||||
println(
|
println(
|
||||||
"HTTP QA: status=${response.status}, body=${response.contentAsString}, " +
|
"HTTP QA: status=${response.status}, body=${response.contentAsString}, " +
|
||||||
|
|||||||
+17
-7
@@ -92,7 +92,7 @@ class TranslationJobRetryIntegrationTest @Autowired constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("같은 원문 해시라도 원문 언어가 다른 FAILED 행은 재개하지 않는다")
|
@DisplayName("다른 원문 언어의 FAILED 행은 보존하고 같은 hash의 현재 언어 작업을 새로 만든다")
|
||||||
fun shouldNotRestartFailedJobForDifferentSourceLanguage() {
|
fun shouldNotRestartFailedJobForDifferentSourceLanguage() {
|
||||||
val original = persistJob(sourceLanguage = "ja")
|
val original = persistJob(sourceLanguage = "ja")
|
||||||
|
|
||||||
@@ -100,13 +100,20 @@ class TranslationJobRetryIntegrationTest @Autowired constructor(
|
|||||||
|
|
||||||
entityManager.flush()
|
entityManager.flush()
|
||||||
entityManager.clear()
|
entityManager.clear()
|
||||||
val preserved = repository.findAll().single()
|
val jobs = repository.findAll()
|
||||||
|
assertEquals(2, jobs.size)
|
||||||
|
val preserved = jobs.single { it.id == original.id }
|
||||||
assertEquals(original.id, preserved.id)
|
assertEquals(original.id, preserved.id)
|
||||||
assertEquals("ja", preserved.sourceLanguage)
|
assertEquals("ja", preserved.sourceLanguage)
|
||||||
assertEquals(TranslationJobStatus.FAILED, preserved.status)
|
assertEquals(TranslationJobStatus.FAILED, preserved.status)
|
||||||
assertEquals(6, preserved.retryCount)
|
assertEquals(6, preserved.retryCount)
|
||||||
assertEquals("previous failure", preserved.lastErrorMessage)
|
assertEquals("previous failure", preserved.lastErrorMessage)
|
||||||
assertEquals(previousRetryAt, preserved.nextRetryAt)
|
assertEquals(previousRetryAt, preserved.nextRetryAt)
|
||||||
|
val current = jobs.single { it.sourceLanguage == "ko" }
|
||||||
|
assertEquals(original.sourceHash, current.sourceHash)
|
||||||
|
assertEquals(TranslationJobStatus.PENDING, current.status)
|
||||||
|
assertEquals(0, current.retryCount)
|
||||||
|
assertNull(current.lastErrorMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -229,12 +236,13 @@ class TranslationJobRetryIntegrationTest @Autowired constructor(
|
|||||||
AdditionalAnswers.delegatesTo<TranslationJobRepository>(repository)
|
AdditionalAnswers.delegatesTo<TranslationJobRepository>(repository)
|
||||||
)
|
)
|
||||||
Mockito.doAnswer {
|
Mockito.doAnswer {
|
||||||
val snapshot = repository.findByResourceTypeAndResourceIdAndFieldKeyAndTargetLanguageAndSourceHash(
|
val snapshot = repository.findByResourceTypeAndResourceIdAndFieldKeyAndTargetLanguageAndSourceHashAndSourceLanguage(
|
||||||
LanguageTranslationTargetType.CONTENT,
|
LanguageTranslationTargetType.CONTENT,
|
||||||
10L,
|
10L,
|
||||||
"title",
|
"title",
|
||||||
"en",
|
"en",
|
||||||
SourceTextNormalizer.hash("제목")
|
SourceTextNormalizer.hash("제목"),
|
||||||
|
"ko"
|
||||||
)
|
)
|
||||||
requiresNewTransaction.executeWithoutResult {
|
requiresNewTransaction.executeWithoutResult {
|
||||||
val claimed = repository.findById(originalId).orElseThrow()
|
val claimed = repository.findById(originalId).orElseThrow()
|
||||||
@@ -243,16 +251,18 @@ class TranslationJobRetryIntegrationTest @Autowired constructor(
|
|||||||
repository.saveAndFlush(claimed)
|
repository.saveAndFlush(claimed)
|
||||||
}
|
}
|
||||||
snapshot
|
snapshot
|
||||||
}.`when`(interleavingRepository).findByResourceTypeAndResourceIdAndFieldKeyAndTargetLanguageAndSourceHash(
|
}.`when`(interleavingRepository)
|
||||||
|
.findByResourceTypeAndResourceIdAndFieldKeyAndTargetLanguageAndSourceHashAndSourceLanguage(
|
||||||
LanguageTranslationTargetType.CONTENT,
|
LanguageTranslationTargetType.CONTENT,
|
||||||
10L,
|
10L,
|
||||||
"title",
|
"title",
|
||||||
"en",
|
"en",
|
||||||
SourceTextNormalizer.hash("제목")
|
SourceTextNormalizer.hash("제목"),
|
||||||
|
"ko"
|
||||||
)
|
)
|
||||||
try {
|
try {
|
||||||
transaction.executeWithoutResult {
|
transaction.executeWithoutResult {
|
||||||
schedule(TranslationJobScheduler(interleavingRepository, coordinator, contentTranslations))
|
schedule(TranslationJobScheduler(interleavingRepository, coordinator, contentTranslations, entityManager))
|
||||||
}
|
}
|
||||||
|
|
||||||
val claimed = repository.findAll().single()
|
val claimed = repository.findAll().single()
|
||||||
|
|||||||
+10
-6
@@ -6,15 +6,17 @@ import org.junit.jupiter.api.Assertions.assertNotNull
|
|||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import org.mockito.ArgumentCaptor
|
import org.mockito.ArgumentCaptor
|
||||||
import org.mockito.Mockito
|
import org.mockito.Mockito
|
||||||
|
import javax.persistence.EntityManager
|
||||||
|
|
||||||
class TranslationJobSchedulerTest {
|
class TranslationJobSchedulerTest {
|
||||||
private val coordinator = Mockito.mock(TranslationCoordinator::class.java)
|
private val coordinator = Mockito.mock(TranslationCoordinator::class.java)
|
||||||
private val contentTranslations = Mockito.mock(ContentTranslationRepository::class.java)
|
private val contentTranslations = Mockito.mock(ContentTranslationRepository::class.java)
|
||||||
|
private val entityManager = Mockito.mock(EntityManager::class.java)
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun shouldCreateOnePendingJobForMissingNormalizedText() {
|
fun shouldCreateOnePendingJobForMissingNormalizedText() {
|
||||||
val jobRepository = Mockito.mock(TranslationJobRepository::class.java)
|
val jobRepository = Mockito.mock(TranslationJobRepository::class.java)
|
||||||
val scheduler = TranslationJobScheduler(jobRepository, coordinator, contentTranslations)
|
val scheduler = TranslationJobScheduler(jobRepository, coordinator, contentTranslations, entityManager)
|
||||||
|
|
||||||
Mockito.`when`(
|
Mockito.`when`(
|
||||||
jobRepository.findActiveJob(
|
jobRepository.findActiveJob(
|
||||||
@@ -22,7 +24,8 @@ class TranslationJobSchedulerTest {
|
|||||||
resourceId = 10L,
|
resourceId = 10L,
|
||||||
fieldKey = "title",
|
fieldKey = "title",
|
||||||
targetLanguage = "en",
|
targetLanguage = "en",
|
||||||
sourceHash = SourceTextNormalizer.hash("제목")
|
sourceHash = SourceTextNormalizer.hash("제목"),
|
||||||
|
sourceLanguage = "ko"
|
||||||
)
|
)
|
||||||
).thenReturn(null)
|
).thenReturn(null)
|
||||||
|
|
||||||
@@ -53,16 +56,17 @@ class TranslationJobSchedulerTest {
|
|||||||
@Test
|
@Test
|
||||||
fun shouldNotCreateDuplicateJobWhenSameCompletedJobAlreadyExists() {
|
fun shouldNotCreateDuplicateJobWhenSameCompletedJobAlreadyExists() {
|
||||||
val jobRepository = Mockito.mock(TranslationJobRepository::class.java)
|
val jobRepository = Mockito.mock(TranslationJobRepository::class.java)
|
||||||
val scheduler = TranslationJobScheduler(jobRepository, coordinator, contentTranslations)
|
val scheduler = TranslationJobScheduler(jobRepository, coordinator, contentTranslations, entityManager)
|
||||||
val sourceHash = SourceTextNormalizer.hash("제목")
|
val sourceHash = SourceTextNormalizer.hash("제목")
|
||||||
|
|
||||||
Mockito.`when`(
|
Mockito.`when`(
|
||||||
jobRepository.findByResourceTypeAndResourceIdAndFieldKeyAndTargetLanguageAndSourceHash(
|
jobRepository.findByResourceTypeAndResourceIdAndFieldKeyAndTargetLanguageAndSourceHashAndSourceLanguage(
|
||||||
resourceType = LanguageTranslationTargetType.CONTENT,
|
resourceType = LanguageTranslationTargetType.CONTENT,
|
||||||
resourceId = 10L,
|
resourceId = 10L,
|
||||||
fieldKey = "title",
|
fieldKey = "title",
|
||||||
targetLanguage = "en",
|
targetLanguage = "en",
|
||||||
sourceHash = sourceHash
|
sourceHash = sourceHash,
|
||||||
|
sourceLanguage = "ko"
|
||||||
)
|
)
|
||||||
).thenReturn(
|
).thenReturn(
|
||||||
TranslationJob(
|
TranslationJob(
|
||||||
@@ -92,7 +96,7 @@ class TranslationJobSchedulerTest {
|
|||||||
@Test
|
@Test
|
||||||
fun shouldNotCreateJobWhenSourceLanguageIsUnsupported() {
|
fun shouldNotCreateJobWhenSourceLanguageIsUnsupported() {
|
||||||
val jobRepository = Mockito.mock(TranslationJobRepository::class.java)
|
val jobRepository = Mockito.mock(TranslationJobRepository::class.java)
|
||||||
val scheduler = TranslationJobScheduler(jobRepository, coordinator, contentTranslations)
|
val scheduler = TranslationJobScheduler(jobRepository, coordinator, contentTranslations, entityManager)
|
||||||
|
|
||||||
scheduler.scheduleMissingTranslation(
|
scheduler.scheduleMissingTranslation(
|
||||||
resourceType = LanguageTranslationTargetType.CONTENT,
|
resourceType = LanguageTranslationTargetType.CONTENT,
|
||||||
|
|||||||
+5
-2
@@ -71,12 +71,13 @@ class TranslationJobWorkerTest {
|
|||||||
.thenReturn(listOf(100L, 101L))
|
.thenReturn(listOf(100L, 101L))
|
||||||
Mockito.`when`(jobRepository.findById(100L)).thenReturn(Optional.of(titleJob))
|
Mockito.`when`(jobRepository.findById(100L)).thenReturn(Optional.of(titleJob))
|
||||||
Mockito.`when`(jobRepository.findById(101L)).thenReturn(Optional.of(detailJob))
|
Mockito.`when`(jobRepository.findById(101L)).thenReturn(Optional.of(detailJob))
|
||||||
|
Mockito.`when`(materializer.materializeContentFields(10L, "en", setOf("title", "detail"))).thenReturn(true)
|
||||||
|
|
||||||
worker.processNextGroup()
|
worker.processNextGroup()
|
||||||
|
|
||||||
assertEquals(TranslationJobStatus.COMPLETED, titleJob.status)
|
assertEquals(TranslationJobStatus.COMPLETED, titleJob.status)
|
||||||
assertEquals(TranslationJobStatus.COMPLETED, detailJob.status)
|
assertEquals(TranslationJobStatus.COMPLETED, detailJob.status)
|
||||||
Mockito.verify(materializer, Mockito.times(1)).materialize(LanguageTranslationTargetType.CONTENT, 10L, "en")
|
Mockito.verify(materializer, Mockito.times(1)).materializeContentFields(10L, "en", setOf("title", "detail"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -145,7 +146,7 @@ class TranslationJobWorkerTest {
|
|||||||
.thenReturn(listOf(100L, 101L))
|
.thenReturn(listOf(100L, 101L))
|
||||||
Mockito.`when`(jobRepository.findById(100L)).thenReturn(Optional.of(titleJob))
|
Mockito.`when`(jobRepository.findById(100L)).thenReturn(Optional.of(titleJob))
|
||||||
Mockito.`when`(jobRepository.findById(101L)).thenReturn(Optional.of(detailJob))
|
Mockito.`when`(jobRepository.findById(101L)).thenReturn(Optional.of(detailJob))
|
||||||
Mockito.`when`(materializer.materialize(LanguageTranslationTargetType.CONTENT, 10L, "en"))
|
Mockito.`when`(materializer.materializeContentFields(10L, "en", setOf("title", "detail")))
|
||||||
.thenThrow(IllegalStateException("materialize down"))
|
.thenThrow(IllegalStateException("materialize down"))
|
||||||
|
|
||||||
worker.processNextGroup()
|
worker.processNextGroup()
|
||||||
@@ -177,6 +178,7 @@ class TranslationJobWorkerTest {
|
|||||||
.thenReturn(listOf(1L), listOf(2L), listOf(3L), listOf(4L), listOf(5L), listOf(6L))
|
.thenReturn(listOf(1L), listOf(2L), listOf(3L), listOf(4L), listOf(5L), listOf(6L))
|
||||||
jobs.forEach { job ->
|
jobs.forEach { job ->
|
||||||
Mockito.`when`(jobRepository.findById(job.id!!)).thenReturn(Optional.of(job))
|
Mockito.`when`(jobRepository.findById(job.id!!)).thenReturn(Optional.of(job))
|
||||||
|
Mockito.`when`(materializer.materializeContentFields(job.resourceId, "en", setOf("title"))).thenReturn(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
worker.runPendingJobs()
|
worker.runPendingJobs()
|
||||||
@@ -248,6 +250,7 @@ class TranslationJobWorkerTest {
|
|||||||
Mockito.`when`(jobRepository.findNextPendingGroupJobIdsForUpdate(anyLocalDateTime())).thenReturn(listOf(200L))
|
Mockito.`when`(jobRepository.findNextPendingGroupJobIdsForUpdate(anyLocalDateTime())).thenReturn(listOf(200L))
|
||||||
Mockito.`when`(jobRepository.findById(200L)).thenReturn(Optional.of(job))
|
Mockito.`when`(jobRepository.findById(200L)).thenReturn(Optional.of(job))
|
||||||
|
|
||||||
|
Mockito.`when`(materializer.materializeContentFields(10L, "en", setOf("title"))).thenReturn(true)
|
||||||
assertTrue(worker.processNextGroup())
|
assertTrue(worker.processNextGroup())
|
||||||
|
|
||||||
assertEquals(TranslationJobStatus.COMPLETED, job.status)
|
assertEquals(TranslationJobStatus.COMPLETED, job.status)
|
||||||
|
|||||||
-38
@@ -7,7 +7,6 @@ import kr.co.vividnext.sodalive.aws.cloudfront.AudioContentCloudFront
|
|||||||
import kr.co.vividnext.sodalive.chat.character.service.ChatCharacterService
|
import kr.co.vividnext.sodalive.chat.character.service.ChatCharacterService
|
||||||
import kr.co.vividnext.sodalive.content.AudioContent
|
import kr.co.vividnext.sodalive.content.AudioContent
|
||||||
import kr.co.vividnext.sodalive.content.theme.AudioContentTheme
|
import kr.co.vividnext.sodalive.content.theme.AudioContentTheme
|
||||||
import kr.co.vividnext.sodalive.creator.admin.content.CreatorAdminContentService
|
|
||||||
import kr.co.vividnext.sodalive.creator.admin.content.series.Series
|
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.SeriesContent
|
||||||
import kr.co.vividnext.sodalive.member.Member
|
import kr.co.vividnext.sodalive.member.Member
|
||||||
@@ -15,11 +14,8 @@ 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.support.EmbeddedRedisInitializer
|
import kr.co.vividnext.sodalive.support.EmbeddedRedisInitializer
|
||||||
import org.hamcrest.Matchers.nullValue
|
import org.hamcrest.Matchers.nullValue
|
||||||
import org.junit.jupiter.api.AfterEach
|
|
||||||
import org.junit.jupiter.api.Assertions.assertEquals
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
import org.junit.jupiter.api.Assertions.assertSame
|
|
||||||
import org.junit.jupiter.api.Assertions.assertTrue
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
import org.junit.jupiter.api.BeforeEach
|
|
||||||
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.junit.jupiter.params.ParameterizedTest
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
@@ -40,8 +36,6 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio
|
|||||||
import org.springframework.security.core.authority.SimpleGrantedAuthority
|
import org.springframework.security.core.authority.SimpleGrantedAuthority
|
||||||
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication
|
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication
|
||||||
import org.springframework.test.context.ContextConfiguration
|
import org.springframework.test.context.ContextConfiguration
|
||||||
import org.springframework.test.util.AopTestUtils
|
|
||||||
import org.springframework.test.util.ReflectionTestUtils
|
|
||||||
import org.springframework.test.web.servlet.MockMvc
|
import org.springframework.test.web.servlet.MockMvc
|
||||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart
|
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart
|
||||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.header
|
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.header
|
||||||
@@ -60,7 +54,6 @@ import javax.persistence.EntityManager
|
|||||||
class AiCharacterAdminAudioContentUpdateTest @Autowired constructor(
|
class AiCharacterAdminAudioContentUpdateTest @Autowired constructor(
|
||||||
private val mockMvc: MockMvc,
|
private val mockMvc: MockMvc,
|
||||||
private val chatCharacterService: ChatCharacterService,
|
private val chatCharacterService: ChatCharacterService,
|
||||||
private val creatorAdminContentService: CreatorAdminContentService,
|
|
||||||
private val entityManager: EntityManager
|
private val entityManager: EntityManager
|
||||||
) {
|
) {
|
||||||
@MockBean
|
@MockBean
|
||||||
@@ -72,18 +65,6 @@ class AiCharacterAdminAudioContentUpdateTest @Autowired constructor(
|
|||||||
@MockBean
|
@MockBean
|
||||||
private lateinit var applicationEventPublisher: ApplicationEventPublisher
|
private lateinit var applicationEventPublisher: ApplicationEventPublisher
|
||||||
|
|
||||||
private lateinit var originalCreatorAdminContentServicePublisher: ApplicationEventPublisher
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
fun replaceActualServicePublisher() {
|
|
||||||
originalCreatorAdminContentServicePublisher = replaceCreatorAdminContentServicePublisher(applicationEventPublisher)
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterEach
|
|
||||||
fun restoreActualServicePublisher() {
|
|
||||||
replaceCreatorAdminContentServicePublisher(originalCreatorAdminContentServicePublisher)
|
|
||||||
}
|
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@CsvSource(
|
@CsvSource(
|
||||||
value = [
|
value = [
|
||||||
@@ -131,7 +112,6 @@ class AiCharacterAdminAudioContentUpdateTest @Autowired constructor(
|
|||||||
|
|
||||||
entityManager.clear()
|
entityManager.clear()
|
||||||
assertEquals("before update", entityManager.find(AudioContent::class.java, content.id).title)
|
assertEquals("before update", entityManager.find(AudioContent::class.java, content.id).title)
|
||||||
assertActualCreatorAdminContentServicePublisher(applicationEventPublisher)
|
|
||||||
Mockito.verify(amazonS3Client, Mockito.never()).putObject(Mockito.any(PutObjectRequest::class.java))
|
Mockito.verify(amazonS3Client, Mockito.never()).putObject(Mockito.any(PutObjectRequest::class.java))
|
||||||
Mockito.verifyNoInteractions(applicationEventPublisher)
|
Mockito.verifyNoInteractions(applicationEventPublisher)
|
||||||
}
|
}
|
||||||
@@ -175,7 +155,6 @@ class AiCharacterAdminAudioContentUpdateTest @Autowired constructor(
|
|||||||
|
|
||||||
entityManager.clear()
|
entityManager.clear()
|
||||||
assertEquals("undefined-part-before", entityManager.find(AudioContent::class.java, content.id).title)
|
assertEquals("undefined-part-before", entityManager.find(AudioContent::class.java, content.id).title)
|
||||||
assertActualCreatorAdminContentServicePublisher(applicationEventPublisher)
|
|
||||||
Mockito.verify(amazonS3Client, Mockito.never()).putObject(Mockito.any(PutObjectRequest::class.java))
|
Mockito.verify(amazonS3Client, Mockito.never()).putObject(Mockito.any(PutObjectRequest::class.java))
|
||||||
Mockito.verifyNoInteractions(applicationEventPublisher)
|
Mockito.verifyNoInteractions(applicationEventPublisher)
|
||||||
}
|
}
|
||||||
@@ -219,7 +198,6 @@ class AiCharacterAdminAudioContentUpdateTest @Autowired constructor(
|
|||||||
|
|
||||||
entityManager.clear()
|
entityManager.clear()
|
||||||
assertEquals("filename-less-part-before", entityManager.find(AudioContent::class.java, content.id).title)
|
assertEquals("filename-less-part-before", entityManager.find(AudioContent::class.java, content.id).title)
|
||||||
assertActualCreatorAdminContentServicePublisher(applicationEventPublisher)
|
|
||||||
Mockito.verify(amazonS3Client, Mockito.never()).putObject(Mockito.any(PutObjectRequest::class.java))
|
Mockito.verify(amazonS3Client, Mockito.never()).putObject(Mockito.any(PutObjectRequest::class.java))
|
||||||
Mockito.verifyNoInteractions(applicationEventPublisher)
|
Mockito.verifyNoInteractions(applicationEventPublisher)
|
||||||
}
|
}
|
||||||
@@ -397,7 +375,6 @@ class AiCharacterAdminAudioContentUpdateTest @Autowired constructor(
|
|||||||
.andExpect(status().isOk)
|
.andExpect(status().isOk)
|
||||||
|
|
||||||
assertEquals(oldCover, entityManager.find(AudioContent::class.java, content.id).coverImage)
|
assertEquals(oldCover, entityManager.find(AudioContent::class.java, content.id).coverImage)
|
||||||
assertActualCreatorAdminContentServicePublisher(applicationEventPublisher)
|
|
||||||
Mockito.verify(amazonS3Client, Mockito.never()).putObject(Mockito.any(PutObjectRequest::class.java))
|
Mockito.verify(amazonS3Client, Mockito.never()).putObject(Mockito.any(PutObjectRequest::class.java))
|
||||||
Mockito.verifyNoInteractions(applicationEventPublisher)
|
Mockito.verifyNoInteractions(applicationEventPublisher)
|
||||||
}
|
}
|
||||||
@@ -433,7 +410,6 @@ class AiCharacterAdminAudioContentUpdateTest @Autowired constructor(
|
|||||||
.andExpect(jsonPath("$.success").value(false))
|
.andExpect(jsonPath("$.success").value(false))
|
||||||
|
|
||||||
assertEquals(oldTitle, entityManager.find(AudioContent::class.java, content.id).title)
|
assertEquals(oldTitle, entityManager.find(AudioContent::class.java, content.id).title)
|
||||||
assertActualCreatorAdminContentServicePublisher(applicationEventPublisher)
|
|
||||||
Mockito.verify(amazonS3Client, Mockito.never()).putObject(Mockito.any(PutObjectRequest::class.java))
|
Mockito.verify(amazonS3Client, Mockito.never()).putObject(Mockito.any(PutObjectRequest::class.java))
|
||||||
Mockito.verifyNoInteractions(applicationEventPublisher)
|
Mockito.verifyNoInteractions(applicationEventPublisher)
|
||||||
}
|
}
|
||||||
@@ -523,20 +499,6 @@ class AiCharacterAdminAudioContentUpdateTest @Autowired constructor(
|
|||||||
).setParameter("contentId", contentId).resultList
|
).setParameter("contentId", contentId).resultList
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun replaceCreatorAdminContentServicePublisher(
|
|
||||||
publisher: ApplicationEventPublisher
|
|
||||||
): ApplicationEventPublisher {
|
|
||||||
val target = AopTestUtils.getTargetObject<CreatorAdminContentService>(creatorAdminContentService)
|
|
||||||
val original = ReflectionTestUtils.getField(target, "applicationEventPublisher") as ApplicationEventPublisher
|
|
||||||
ReflectionTestUtils.setField(target, "applicationEventPublisher", publisher)
|
|
||||||
return original
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun assertActualCreatorAdminContentServicePublisher(publisher: ApplicationEventPublisher) {
|
|
||||||
val target = AopTestUtils.getTargetObject<CreatorAdminContentService>(creatorAdminContentService)
|
|
||||||
assertSame(publisher, ReflectionTestUtils.getField(target, "applicationEventPublisher"))
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun adminAuthentication() = authentication(
|
private fun adminAuthentication() = authentication(
|
||||||
UsernamePasswordAuthenticationToken(
|
UsernamePasswordAuthenticationToken(
|
||||||
MemberAdapter(
|
MemberAdapter(
|
||||||
|
|||||||
+26
-22
@@ -13,11 +13,13 @@ import kr.co.vividnext.sodalive.content.hashtag.HashTagRepository
|
|||||||
import kr.co.vividnext.sodalive.content.theme.AudioContentTheme
|
import kr.co.vividnext.sodalive.content.theme.AudioContentTheme
|
||||||
import kr.co.vividnext.sodalive.creator.admin.content.CreatorAdminContentRepository
|
import kr.co.vividnext.sodalive.creator.admin.content.CreatorAdminContentRepository
|
||||||
import kr.co.vividnext.sodalive.creator.admin.content.CreatorAdminContentService
|
import kr.co.vividnext.sodalive.creator.admin.content.CreatorAdminContentService
|
||||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationEvent
|
|
||||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType
|
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationJobRepository
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationJobWorker
|
||||||
import kr.co.vividnext.sodalive.member.Member
|
import kr.co.vividnext.sodalive.member.Member
|
||||||
import kr.co.vividnext.sodalive.member.MemberRole
|
import kr.co.vividnext.sodalive.member.MemberRole
|
||||||
import kr.co.vividnext.sodalive.support.EmbeddedRedisInitializer
|
import kr.co.vividnext.sodalive.support.EmbeddedRedisInitializer
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationSourceInvalidationService
|
||||||
import org.junit.jupiter.api.Assertions.assertEquals
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
import org.junit.jupiter.api.Assertions.assertFalse
|
import org.junit.jupiter.api.Assertions.assertFalse
|
||||||
import org.junit.jupiter.api.Assertions.assertNotEquals
|
import org.junit.jupiter.api.Assertions.assertNotEquals
|
||||||
@@ -29,17 +31,13 @@ import org.junit.jupiter.api.DisplayName
|
|||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import org.junit.jupiter.params.ParameterizedTest
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
import org.junit.jupiter.params.provider.CsvSource
|
import org.junit.jupiter.params.provider.CsvSource
|
||||||
import org.mockito.ArgumentCaptor
|
|
||||||
import org.mockito.Mockito
|
import org.mockito.Mockito
|
||||||
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
import org.springframework.boot.test.context.SpringBootTest
|
import org.springframework.boot.test.context.SpringBootTest
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean
|
import org.springframework.boot.test.mock.mockito.MockBean
|
||||||
import org.springframework.context.ApplicationEventPublisher
|
|
||||||
import org.springframework.data.domain.PageRequest
|
import org.springframework.data.domain.PageRequest
|
||||||
import org.springframework.mock.web.MockMultipartFile
|
import org.springframework.mock.web.MockMultipartFile
|
||||||
import org.springframework.test.context.ContextConfiguration
|
import org.springframework.test.context.ContextConfiguration
|
||||||
import org.springframework.test.context.event.ApplicationEvents
|
|
||||||
import org.springframework.test.context.event.RecordApplicationEvents
|
|
||||||
import org.springframework.transaction.annotation.Propagation
|
import org.springframework.transaction.annotation.Propagation
|
||||||
import org.springframework.transaction.annotation.Transactional
|
import org.springframework.transaction.annotation.Transactional
|
||||||
import org.springframework.transaction.support.TransactionTemplate
|
import org.springframework.transaction.support.TransactionTemplate
|
||||||
@@ -50,7 +48,6 @@ import javax.persistence.EntityManager
|
|||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@Transactional
|
@Transactional
|
||||||
@ContextConfiguration(initializers = [EmbeddedRedisInitializer::class])
|
@ContextConfiguration(initializers = [EmbeddedRedisInitializer::class])
|
||||||
@RecordApplicationEvents
|
|
||||||
class LegacyCreatorAdminAudioContentCharacterizationTest @Autowired constructor(
|
class LegacyCreatorAdminAudioContentCharacterizationTest @Autowired constructor(
|
||||||
private val audioContentService: AudioContentService,
|
private val audioContentService: AudioContentService,
|
||||||
private val repository: CreatorAdminContentRepository,
|
private val repository: CreatorAdminContentRepository,
|
||||||
@@ -66,7 +63,13 @@ class LegacyCreatorAdminAudioContentCharacterizationTest @Autowired constructor(
|
|||||||
private lateinit var creatorAdminContentService: CreatorAdminContentService
|
private lateinit var creatorAdminContentService: CreatorAdminContentService
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private lateinit var applicationEvents: ApplicationEvents
|
private lateinit var jobs: TranslationJobRepository
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private lateinit var sourceInvalidation: ContentTranslationSourceInvalidationService
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private lateinit var worker: TranslationJobWorker
|
||||||
|
|
||||||
private lateinit var audioContentCloudFront: AudioContentCloudFront
|
private lateinit var audioContentCloudFront: AudioContentCloudFront
|
||||||
|
|
||||||
@@ -74,7 +77,6 @@ class LegacyCreatorAdminAudioContentCharacterizationTest @Autowired constructor(
|
|||||||
private lateinit var amazonS3Client: AmazonS3Client
|
private lateinit var amazonS3Client: AmazonS3Client
|
||||||
|
|
||||||
private lateinit var s3Uploader: S3Uploader
|
private lateinit var s3Uploader: S3Uploader
|
||||||
private lateinit var applicationEventPublisher: ApplicationEventPublisher
|
|
||||||
private lateinit var service: CreatorAdminContentService
|
private lateinit var service: CreatorAdminContentService
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
@@ -84,7 +86,6 @@ class LegacyCreatorAdminAudioContentCharacterizationTest @Autowired constructor(
|
|||||||
Mockito.`when`(amazonS3Client.getUrl(Mockito.anyString(), Mockito.anyString()))
|
Mockito.`when`(amazonS3Client.getUrl(Mockito.anyString(), Mockito.anyString()))
|
||||||
.thenReturn(URL("https://s3.example.com/test"))
|
.thenReturn(URL("https://s3.example.com/test"))
|
||||||
s3Uploader = S3Uploader(amazonS3Client)
|
s3Uploader = S3Uploader(amazonS3Client)
|
||||||
applicationEventPublisher = Mockito.mock(ApplicationEventPublisher::class.java)
|
|
||||||
service = CreatorAdminContentService(
|
service = CreatorAdminContentService(
|
||||||
repository = repository,
|
repository = repository,
|
||||||
hashTagRepository = hashTagRepository,
|
hashTagRepository = hashTagRepository,
|
||||||
@@ -92,7 +93,8 @@ class LegacyCreatorAdminAudioContentCharacterizationTest @Autowired constructor(
|
|||||||
audioContentCloudFront = audioContentCloudFront,
|
audioContentCloudFront = audioContentCloudFront,
|
||||||
objectMapper = jacksonObjectMapper(),
|
objectMapper = jacksonObjectMapper(),
|
||||||
s3Uploader = s3Uploader,
|
s3Uploader = s3Uploader,
|
||||||
applicationEventPublisher = applicationEventPublisher,
|
sourceInvalidation = sourceInvalidation,
|
||||||
|
transactionManager = requireNotNull(transactionTemplate.transactionManager),
|
||||||
bucket = "test-bucket",
|
bucket = "test-bucket",
|
||||||
coverImageHost = "https://cover.example.com"
|
coverImageHost = "https://cover.example.com"
|
||||||
)
|
)
|
||||||
@@ -175,7 +177,7 @@ class LegacyCreatorAdminAudioContentCharacterizationTest @Autowired constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("기존 크리에이터 콘텐츠 수정은 가격과 플래그를 저장하고 비활성화 시 예약을 해제하며 번역 이벤트를 발행한다")
|
@DisplayName("기존 크리에이터 콘텐츠 수정은 가격과 플래그를 저장하고 비활성화 시 예약 해제와 변경 필드 번역을 저장한다")
|
||||||
fun shouldUpdatePriceFlagsSoftDeleteAndTranslationEvent() {
|
fun shouldUpdatePriceFlagsSoftDeleteAndTranslationEvent() {
|
||||||
val owner = saveMember("legacy-update-owner")
|
val owner = saveMember("legacy-update-owner")
|
||||||
val content = saveAudioContent(owner, "before title", "00:10:00").apply {
|
val content = saveAudioContent(owner, "before title", "00:10:00").apply {
|
||||||
@@ -204,12 +206,12 @@ class LegacyCreatorAdminAudioContentCharacterizationTest @Autowired constructor(
|
|||||||
assertEquals(100, contentPriceChangeLogRepository.findAll().single().prevPrice)
|
assertEquals(100, contentPriceChangeLogRepository.findAll().single().prevPrice)
|
||||||
assertEquals(listOf("#updated"), content.audioContentHashTags.map { it.hashTag!!.tag })
|
assertEquals(listOf("#updated"), content.audioContentHashTags.map { it.hashTag!!.tag })
|
||||||
|
|
||||||
val eventCaptor = ArgumentCaptor.forClass(Any::class.java)
|
val scheduled = jobs.findAll().filter {
|
||||||
Mockito.verify(applicationEventPublisher).publishEvent(eventCaptor.capture())
|
it.resourceId == content.id && it.resourceType == LanguageTranslationTargetType.CONTENT
|
||||||
val event = eventCaptor.value as LanguageTranslationEvent
|
}
|
||||||
assertEquals(content.id, event.id)
|
assertEquals(6, scheduled.size)
|
||||||
assertEquals(LanguageTranslationTargetType.CONTENT, event.targetType)
|
assertEquals(setOf("title", "detail", "tags"), scheduled.map { it.fieldKey }.toSet())
|
||||||
assertTrue(event.waitTransactionCommit)
|
assertEquals(setOf("en", "ja"), scheduled.map { it.targetLanguage }.toSet())
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -250,7 +252,7 @@ class LegacyCreatorAdminAudioContentCharacterizationTest @Autowired constructor(
|
|||||||
"false,title,' '",
|
"false,title,' '",
|
||||||
"false,detail,' '"
|
"false,detail,' '"
|
||||||
)
|
)
|
||||||
@DisplayName("관리자와 크리에이터 관리자의 제목 또는 상세 수정은 태그 없이도 커밋 후 번역을 요청한다")
|
@DisplayName("관리자와 크리에이터 관리자의 제목 또는 상세 수정은 태그 없이도 변경 필드 번역을 저장한다")
|
||||||
fun shouldRequestTranslationAfterCommitWhenMetadataIsUpdatedWithoutTags(asAdmin: Boolean, field: String, tags: String?) {
|
fun shouldRequestTranslationAfterCommitWhenMetadataIsUpdatedWithoutTags(asAdmin: Boolean, field: String, tags: String?) {
|
||||||
val owner = saveMember("metadata-update-owner")
|
val owner = saveMember("metadata-update-owner")
|
||||||
val content = saveAudioContent(owner, "before title", "00:10:00")
|
val content = saveAudioContent(owner, "before title", "00:10:00")
|
||||||
@@ -269,10 +271,12 @@ class LegacyCreatorAdminAudioContentCharacterizationTest @Autowired constructor(
|
|||||||
entityManager.clear()
|
entityManager.clear()
|
||||||
val updated = entityManager.find(AudioContent::class.java, content.id)
|
val updated = entityManager.find(AudioContent::class.java, content.id)
|
||||||
assertEquals("updated text", if (field == "title") updated.title else updated.detail)
|
assertEquals("updated text", if (field == "title") updated.title else updated.detail)
|
||||||
val events = applicationEvents.stream(LanguageTranslationEvent::class.java).filter { it.id == content.id }.toList()
|
val scheduled = jobs.findAll().filter {
|
||||||
assertEquals(1, events.size)
|
it.resourceId == content.id && it.resourceType == LanguageTranslationTargetType.CONTENT
|
||||||
assertEquals(LanguageTranslationTargetType.CONTENT, events.single().targetType)
|
}
|
||||||
assertTrue(events.single().waitTransactionCommit)
|
assertEquals(2, scheduled.size)
|
||||||
|
assertEquals(setOf(field), scheduled.map { it.fieldKey }.toSet())
|
||||||
|
assertEquals(setOf("en", "ja"), scheduled.map { it.targetLanguage }.toSet())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun saveAudioContent(owner: Member, title: String, duration: String): AudioContent {
|
private fun saveAudioContent(owner: Member, title: String, duration: String): AudioContent {
|
||||||
|
|||||||
+1
@@ -77,6 +77,7 @@ class LegacyCreatorAdminSeriesCharacterizationTest @Autowired constructor(
|
|||||||
genreRepository = genreRepository,
|
genreRepository = genreRepository,
|
||||||
hashTagRepository = hashTagRepository,
|
hashTagRepository = hashTagRepository,
|
||||||
audioContentRepository = audioContentRepository,
|
audioContentRepository = audioContentRepository,
|
||||||
|
entityManager = entityManager,
|
||||||
s3Uploader = S3Uploader(amazonS3Client),
|
s3Uploader = S3Uploader(amazonS3Client),
|
||||||
objectMapper = jacksonObjectMapper(),
|
objectMapper = jacksonObjectMapper(),
|
||||||
applicationEventPublisher = eventPublisher,
|
applicationEventPublisher = eventPublisher,
|
||||||
|
|||||||
+138
@@ -0,0 +1,138 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.`in`.web
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContent
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Assertions.assertFalse
|
||||||
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
|
import org.junit.jupiter.api.DisplayName
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource
|
||||||
|
import org.springframework.http.HttpMethod
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
class AdminTranslationSelectionApplyHttpIntegrationTest : TranslationSelectionApplyHttpTestSupport() {
|
||||||
|
@Test
|
||||||
|
@DisplayName("시리즈의 여러 그룹과 개별 제목을 HTTP에서 검토 생성 부분 적용하며 선택 출처와 폐기를 반환한다")
|
||||||
|
fun shouldCompleteSeriesFlowWithMultipleGroupsAndIndividualTitle() {
|
||||||
|
val titles = listOf("공통 원문 제목 끝 1", "공통 원문 제목 끝 2", "다른 제목 3", "다른 제목 4", "원문 제목")
|
||||||
|
val ids = titles.map { title ->
|
||||||
|
val id = fixture()
|
||||||
|
transaction.executeWithoutResult { entityManager.find(AudioContent::class.java, id).title = title }
|
||||||
|
id
|
||||||
|
}
|
||||||
|
val requestId = call(
|
||||||
|
HttpMethod.POST,
|
||||||
|
"",
|
||||||
|
"""{"clientRequestId":"${UUID.randomUUID()}","scope":{"type":"SERIES","seriesId":${series(ids)}}}"""
|
||||||
|
)
|
||||||
|
.path("data").path("requestId").asLong()
|
||||||
|
preparation.prepareNext()
|
||||||
|
val analyzed = call(HttpMethod.GET, "/$requestId/grouping").path("data")
|
||||||
|
assertFalse(analyzed.path("reviewed").asBoolean())
|
||||||
|
val review = """{"expectedRevision":1,"groups":[
|
||||||
|
{"groupId":"g1","memberContentIds":[${ids[0]},${ids[1]}],"sharedParts":[
|
||||||
|
{"partId":"end","occurrences":[{"contentId":${ids[0]},"startCodePoint":9,"endCodePoint":10},
|
||||||
|
{"contentId":${ids[1]},"startCodePoint":9,"endCodePoint":10}]},
|
||||||
|
{"partId":"start","occurrences":[{"contentId":${ids[0]},"startCodePoint":0,"endCodePoint":2},
|
||||||
|
{"contentId":${ids[1]},"startCodePoint":0,"endCodePoint":2}]}]},
|
||||||
|
{"groupId":"g2","memberContentIds":[${ids[2]},${ids[3]}],"sharedParts":[
|
||||||
|
{"partId":"other","occurrences":[{"contentId":${ids[2]},"startCodePoint":0,"endCodePoint":5},
|
||||||
|
{"contentId":${ids[3]},"startCodePoint":0,"endCodePoint":5}]}]}
|
||||||
|
],"individualContentIds":[${ids[4]}]}"""
|
||||||
|
assertTrue(call(HttpMethod.PUT, "/$requestId/grouping", review).path("success").asBoolean())
|
||||||
|
assertTrue(call(HttpMethod.POST, "/$requestId/generate", """{"expectedRevision":2}""").path("success").asBoolean())
|
||||||
|
repeat(requests.get(requestId).progress.total + 1) { selectionWorker.runNext() }
|
||||||
|
val candidates = candidatePage(requestId)
|
||||||
|
fun candidate(kind: String, owner: String, locale: String): Long = candidates.single {
|
||||||
|
val unit = it.path("unit")
|
||||||
|
unit.path("kind").asText() == kind &&
|
||||||
|
(unit.path("groupId").asText() == owner || unit.path("contentId").asText() == owner) &&
|
||||||
|
it.path("targetLanguage").asText() == locale && it.path("provider").path("id").asText() == "papago" &&
|
||||||
|
(kind != "FIELD" || unit.path("field").asText() == "title")
|
||||||
|
}.path("candidateId").asLong()
|
||||||
|
val firstResidual = candidates.single {
|
||||||
|
it.path("unit").path("kind").asText() == "TITLE_RESIDUAL" && it.path("unit").path("contentId").asLong() == ids[0] &&
|
||||||
|
it.path("targetLanguage").asText() == "en" && it.path("provider").path("id").asText() == "aws"
|
||||||
|
}.path("candidateId").asLong()
|
||||||
|
val otherResidual = candidates.single {
|
||||||
|
it.path("unit").path("kind").asText() == "TITLE_RESIDUAL" && it.path("unit").path("contentId").asLong() == ids[1] &&
|
||||||
|
it.path("targetLanguage").asText() == "en" && it.path("provider").path("id").asText() == "papago"
|
||||||
|
}.path("candidateId").asLong()
|
||||||
|
val body = """{"operationId":"${UUID.randomUUID()}","expectedRevision":3,"selections":[
|
||||||
|
{"kind":"TITLE_GROUP","groupId":"g1","targetLanguage":"en","commonCandidateId":${candidate("GROUP_COMMON", "g1", "en")},
|
||||||
|
"residualChoices":[{"contentId":${ids[0]},"candidateId":$firstResidual},{"contentId":${ids[1]},"candidateId":$otherResidual}]},
|
||||||
|
{"kind":"TITLE_GROUP","groupId":"g2","targetLanguage":"ja","commonCandidateId":${candidate("GROUP_COMMON", "g2", "ja")},
|
||||||
|
"residualChoices":[{"contentId":${ids[2]},"candidateId":null},{"contentId":${ids[3]},"candidateId":null}]},
|
||||||
|
{"kind":"FIELD","contentId":${ids[4]},"field":"title","targetLanguage":"en",
|
||||||
|
"candidateId":${candidate("FIELD", ids[4].toString(), "en")}}
|
||||||
|
]}"""
|
||||||
|
|
||||||
|
val incomplete = body.replace(
|
||||||
|
""",{"contentId":${ids[3]},"candidateId":null}""",
|
||||||
|
""
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
"INCOMPLETE_TITLE_GROUP",
|
||||||
|
call(HttpMethod.POST, "/$requestId/apply", incomplete).path("errorProperty").asText()
|
||||||
|
)
|
||||||
|
assertEquals(0L, overrideRepository.count())
|
||||||
|
val applied = call(HttpMethod.POST, "/$requestId/apply", body)
|
||||||
|
|
||||||
|
assertTrue(applied.path("success").asBoolean(), applied.toString())
|
||||||
|
val receipt = applied.path("data")
|
||||||
|
assertEquals(5, receipt.path("appliedFields").size())
|
||||||
|
assertEquals(
|
||||||
|
listOf("result:공통 English title result:끝 1", "result:공통 English title result:끝 2"),
|
||||||
|
ids.take(2).map { payload(it)?.title }
|
||||||
|
)
|
||||||
|
assertEquals("English title", payload(ids[4])?.title)
|
||||||
|
assertEquals(
|
||||||
|
"result:다른 제목 3",
|
||||||
|
transaction.execute { translations.findByContentIdAndLocale(ids[2], "ja")?.renderedPayload?.title }
|
||||||
|
)
|
||||||
|
assertEquals(null, payload(ids[2]))
|
||||||
|
val field = receipt.path("appliedFields").single { it.path("contentId").asLong() == ids[0] }
|
||||||
|
assertEquals(setOf("aws", "papago"), field.path("providers").map { it.path("id").asText() }.toSet())
|
||||||
|
assertEquals(receipt, call(HttpMethod.GET, "/$requestId").path("data").path("applyReceipt"))
|
||||||
|
assertEquals(5L, overrideRepository.count())
|
||||||
|
assertEquals(0L, memories.count())
|
||||||
|
val summary = call(HttpMethod.GET, "/$requestId").path("data")
|
||||||
|
assertEquals(candidates.size, summary.path("progress").path("terminal").asInt())
|
||||||
|
assertEquals(
|
||||||
|
receipt.path("discardedCandidateCount").asInt(),
|
||||||
|
candidatePage(requestId).count { it.path("status").asText() == "DISCARDED" }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = ["uuid", "kind", "field", "empty", "candidate", "source", "closed", "language", "negative"])
|
||||||
|
@DisplayName("apply 입력과 선택 불가 후보 오류는 실제 HTTP에서 안정 코드와 번역 메시지를 반환한다")
|
||||||
|
fun shouldReturnTypedApplyErrors(kind: String) {
|
||||||
|
val id = fixture()
|
||||||
|
val requestId = readyContent(id)
|
||||||
|
val valid = candidatePage(requestId).first { it.path("status").asText() == "SUCCEEDED" }.path("candidateId").asLong()
|
||||||
|
if (kind == "source") transaction.executeWithoutResult { entityManager.find(AudioContent::class.java, id).title += " " }
|
||||||
|
if (kind == "closed") call(HttpMethod.POST, "/$requestId/cancel")
|
||||||
|
val selection = """{"kind":"${if (kind == "kind") "ARBITRARY" else "FIELD"}","contentId":$id,
|
||||||
|
"field":"${if (kind == "field") "other" else "title"}","targetLanguage":"${if (kind == "language") "fr" else "en"}",
|
||||||
|
"candidateId":${when (kind) { "candidate" -> Long.MAX_VALUE; "negative" -> -1; else -> valid }}}"""
|
||||||
|
val body = """{"operationId":"${if (kind == "uuid") "bad" else UUID.randomUUID()}","expectedRevision":2,
|
||||||
|
"selections":[${if (kind == "empty") "" else selection}]}"""
|
||||||
|
|
||||||
|
val error = call(HttpMethod.POST, "/$requestId/apply", body)
|
||||||
|
|
||||||
|
assertFalse(error.path("success").asBoolean())
|
||||||
|
val expected = when (kind) {
|
||||||
|
"candidate" ->
|
||||||
|
"CANDIDATE_NOT_SELECTABLE" to
|
||||||
|
"This translation candidate cannot be selected. Check its request, field and language."
|
||||||
|
"source" -> "SOURCE_CHANGED" to "The selected source field changed. Create a new translation request."
|
||||||
|
"closed" -> "REQUEST_CLOSED" to "The translation request is already closed."
|
||||||
|
else -> "INVALID_REQUEST" to "Invalid request."
|
||||||
|
}
|
||||||
|
assertEquals(expected.first, error.path("errorProperty").asText())
|
||||||
|
assertEquals(expected.second, error.path("message").asText())
|
||||||
|
assertEquals(0L, overrideRepository.count())
|
||||||
|
}
|
||||||
|
}
|
||||||
+111
@@ -0,0 +1,111 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.`in`.web
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.content.translation.ContentTranslation
|
||||||
|
import kr.co.vividnext.sodalive.content.translation.ContentTranslationPayload
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionScope
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Assertions.assertFalse
|
||||||
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
|
import org.junit.jupiter.api.DisplayName
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource
|
||||||
|
import org.springframework.http.HttpMethod
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
class AdminTranslationSelectionContentsHttpIntegrationTest : TranslationSelectionHttpTestSupport() {
|
||||||
|
@Test
|
||||||
|
@DisplayName("콘텐츠는 ID cursor 기본 50개 최대 100개와 마지막 null cursor로 조회한다")
|
||||||
|
fun shouldPageFrozenContents() {
|
||||||
|
val ids = (1..101).map { fixture() }
|
||||||
|
val created = requests.create(91, UUID.randomUUID(), TranslationSelectionScope.Series(series(ids)))
|
||||||
|
preparation.prepareNext()
|
||||||
|
val first = call(HttpMethod.GET, "/${created.requestId}/contents").path("data")
|
||||||
|
assertEquals(ids.take(50), first.path("items").map { it.path("contentId").asLong() })
|
||||||
|
assertEquals(ids[49], first.path("nextAfterContentId").asLong())
|
||||||
|
val next = call(HttpMethod.GET, "/${created.requestId}/contents?afterContentId=${ids[49]}&size=100").path("data")
|
||||||
|
assertEquals(ids.drop(50), next.path("items").map { it.path("contentId").asLong() })
|
||||||
|
assertTrue(next.path("nextAfterContentId").isNull)
|
||||||
|
assertEquals(0, provider.calls.get())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("동결 원문과 현재 필드 fingerprint 활성 연결 자격을 구분하고 출처 불명을 추정하지 않는다")
|
||||||
|
fun shouldReturnSnapshotAndCurrentEligibilityWithApprovalProvenance() {
|
||||||
|
val first = fixture()
|
||||||
|
val second = fixture()
|
||||||
|
val third = fixture()
|
||||||
|
val seriesId = series(listOf(first, second, third))
|
||||||
|
val created = requests.create(91, UUID.randomUUID(), TranslationSelectionScope.Series(seriesId))
|
||||||
|
preparation.prepareNext()
|
||||||
|
translations.saveAndFlush(
|
||||||
|
ContentTranslation(first, "en", ContentTranslationPayload("Legacy", "Legacy detail", "Legacy tags"))
|
||||||
|
)
|
||||||
|
writer.approve(first, "en", listOf(approval(snapshot(first), ContentSourceFieldKey.TITLE, "Chosen title")))
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
writer.lockContent(first)!!.detail = "현재 설명"
|
||||||
|
writer.lockContent(second)!!.isActive = false
|
||||||
|
entityManager.createQuery("delete from SeriesContent s where s.series.id = :series and s.content.id = :content")
|
||||||
|
.setParameter("series", seriesId).setParameter("content", third).executeUpdate()
|
||||||
|
}
|
||||||
|
|
||||||
|
val items = call(HttpMethod.GET, "/${created.requestId}/contents").path("data").path("items")
|
||||||
|
|
||||||
|
val item = items[0]
|
||||||
|
assertEquals("원문 설명", item.path("sourceFields").path("detail").asText())
|
||||||
|
assertTrue(item.path("eligible").asBoolean())
|
||||||
|
assertFalse(item.path("sourceFingerprints").path("detail") == item.path("currentSourceFingerprints").path("detail"))
|
||||||
|
val english = item.path("currentTranslations").first { it.path("targetLanguage").asText() == "en" }
|
||||||
|
assertEquals("Chosen title", english.path("fields").path("title").asText())
|
||||||
|
assertEquals("ADMIN", english.path("approvalOrigins").path("title").asText())
|
||||||
|
assertEquals("UNKNOWN", english.path("approvalOrigins").path("detail").asText())
|
||||||
|
assertEquals(71, english.path("approvalProvenance").path("title").path("requestId").asLong())
|
||||||
|
assertEquals(listOf(81L, 82L), english.path("approvalProvenance").path("title").path("candidateIds").map { it.asLong() })
|
||||||
|
assertFalse(english.path("approvalProvenance").has("detail"))
|
||||||
|
val japanese = item.path("currentTranslations").first { it.path("targetLanguage").asText() == "ja" }
|
||||||
|
assertEquals("현재 설명", japanese.path("fields").path("detail").asText())
|
||||||
|
assertFalse(items[1].path("active").asBoolean())
|
||||||
|
assertFalse(items[1].path("eligible").asBoolean())
|
||||||
|
assertTrue(items[2].path("active").asBoolean())
|
||||||
|
assertFalse(items[2].path("eligible").asBoolean())
|
||||||
|
assertEquals("원문 제목", items[2].path("currentTranslations")[0].path("fields").path("title").asText())
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = ["size=0", "size=101", "size=x", "afterContentId=0"])
|
||||||
|
@DisplayName("페이지 경계와 형식 오류는 안정 INVALID_REQUEST envelope로 반환한다")
|
||||||
|
fun shouldRejectInvalidPage(query: String) {
|
||||||
|
val created = request(fixture())
|
||||||
|
val result = call(HttpMethod.GET, "/${created.requestId}/contents?$query")
|
||||||
|
assertFalse(result.path("success").asBoolean())
|
||||||
|
assertEquals("INVALID_REQUEST", result.path("errorProperty").asText())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("누락 요청과 잘못된 UUID scope ID를 typed business 오류로 반환한다")
|
||||||
|
fun shouldMapInvalidBoundaryAndMissingResource() {
|
||||||
|
assertEquals("RESOURCE_NOT_FOUND", call(HttpMethod.GET, "/9223372036854775807").path("errorProperty").asText())
|
||||||
|
val malformed = """{"clientRequestId":"bad","scope":{"type":"CONTENT","contentId":1}}"""
|
||||||
|
assertEquals("INVALID_REQUEST", call(HttpMethod.POST, "", malformed).path("errorProperty").asText())
|
||||||
|
val invalidId = """{"clientRequestId":"${UUID.randomUUID()}","scope":{"type":"CONTENT","contentId":-1}}"""
|
||||||
|
assertEquals("INVALID_REQUEST", call(HttpMethod.POST, "", invalidId).path("errorProperty").asText())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("삭제된 콘텐츠도 빈 필드와 동결 원문은 남고 현재 fingerprint와 자격은 사라진다")
|
||||||
|
fun shouldPreserveDeletedContentSnapshot() {
|
||||||
|
val id = fixture()
|
||||||
|
transaction.executeWithoutResult { writer.lockContent(id)!!.detail = "" }
|
||||||
|
val created = request(id)
|
||||||
|
preparation.prepareNext()
|
||||||
|
transaction.executeWithoutResult { entityManager.remove(writer.lockContent(id)) }
|
||||||
|
val item = call(HttpMethod.GET, "/${created.requestId}/contents").path("data").path("items")[0]
|
||||||
|
assertEquals(id, item.path("contentId").asLong())
|
||||||
|
assertEquals("원문 제목", item.path("sourceFields").path("title").asText())
|
||||||
|
assertEquals(listOf("detail"), item.path("emptyFields").map { it.asText() })
|
||||||
|
assertTrue(item.path("currentSourceFingerprints").isNull)
|
||||||
|
assertFalse(item.path("eligible").asBoolean())
|
||||||
|
assertFalse(item.path("active").asBoolean())
|
||||||
|
}
|
||||||
|
}
|
||||||
+243
@@ -0,0 +1,243 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.`in`.web
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContent
|
||||||
|
import kr.co.vividnext.sodalive.member.MemberRole
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.TranslationSelectionWorkerService
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionScope
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Assertions.assertFalse
|
||||||
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
|
import org.junit.jupiter.api.DisplayName
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import org.springframework.http.HttpEntity
|
||||||
|
import org.springframework.http.HttpMethod
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
class AdminTranslationSelectionHttpIntegrationTest : TranslationSelectionHttpTestSupport() {
|
||||||
|
@Autowired private lateinit var worker: TranslationSelectionWorkerService
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("실제 HTTP 생성부터 일부 적용까지 진행하고 영수증과 필드 출처를 재조회한다")
|
||||||
|
fun shouldApplyContentSubsetAndReplayReceiptOverHttp() {
|
||||||
|
val id = fixture()
|
||||||
|
val created = call(
|
||||||
|
HttpMethod.POST,
|
||||||
|
"",
|
||||||
|
"""{"clientRequestId":"${UUID.randomUUID()}","scope":{"type":"CONTENT","contentId":$id}}"""
|
||||||
|
).path("data")
|
||||||
|
val requestId = created.path("requestId").asLong()
|
||||||
|
preparation.prepareNext()
|
||||||
|
val generated = call(HttpMethod.POST, "/$requestId/generate", """{"expectedRevision":1}""")
|
||||||
|
assertTrue(generated.path("success").asBoolean())
|
||||||
|
repeat(7) { worker.runNext() }
|
||||||
|
val candidates = call(HttpMethod.GET, "/$requestId/candidates").path("data").path("items")
|
||||||
|
val title = candidates.single {
|
||||||
|
it.path("unit").path("field").asText() == "title" && it.path("targetLanguage").asText() == "en"
|
||||||
|
}
|
||||||
|
val detail = candidates.single {
|
||||||
|
it.path("unit").path("field").asText() == "detail" && it.path("targetLanguage").asText() == "en"
|
||||||
|
}
|
||||||
|
val body = """{"operationId":"${UUID.randomUUID()}","expectedRevision":2,"selections":[
|
||||||
|
{"kind":"FIELD","contentId":$id,"field":"title","targetLanguage":"en","candidateId":${title.path("candidateId")}},
|
||||||
|
{"kind":"FIELD","contentId":$id,"field":"detail","targetLanguage":"en","candidateId":${detail.path("candidateId")}}
|
||||||
|
]}"""
|
||||||
|
|
||||||
|
val applyingAdmin = actor()
|
||||||
|
val callsBeforeApply = provider.calls.get()
|
||||||
|
val applied = callAs(applyingAdmin, HttpMethod.POST, "/$requestId/apply", body)
|
||||||
|
|
||||||
|
assertTrue(applied.path("success").asBoolean(), applied.toString())
|
||||||
|
val receipt = applied.path("data")
|
||||||
|
assertEquals("APPLIED", receipt.path("status").asText())
|
||||||
|
assertEquals(2, receipt.path("appliedFields").size())
|
||||||
|
assertEquals(4, receipt.path("discardedCandidateCount").asInt())
|
||||||
|
assertEquals("English title", payload(id)?.title)
|
||||||
|
assertEquals("English detail", payload(id)?.detail)
|
||||||
|
assertEquals("#힐링", payload(id)?.tags)
|
||||||
|
assertEquals(receipt, call(HttpMethod.GET, "/$requestId").path("data").path("applyReceipt"))
|
||||||
|
assertEquals(applied, call(HttpMethod.POST, "/$requestId/apply", body))
|
||||||
|
val current = call(HttpMethod.GET, "/$requestId/contents").path("data").path("items")[0]
|
||||||
|
.path("currentTranslations").single { it.path("targetLanguage").asText() == "en" }
|
||||||
|
assertEquals("ADMIN", current.path("approvalOrigins").path("title").asText())
|
||||||
|
assertEquals(requestId, current.path("approvalProvenance").path("detail").path("requestId").asLong())
|
||||||
|
assertEquals(applyingAdmin, current.path("approvalProvenance").path("detail").path("approvedBy").asLong())
|
||||||
|
assertEquals("REQUEST_CLOSED", call(HttpMethod.POST, "/$requestId/cancel").path("errorProperty").asText())
|
||||||
|
assertEquals(callsBeforeApply, provider.calls.get())
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = ["CONTENT_MANAGER", "CREATOR", "USER"])
|
||||||
|
@DisplayName("생성 조회 콘텐츠 그룹 검토 취소 모두 ADMIN 이외 역할을 거부한다")
|
||||||
|
fun shouldRequireAdminRole(role: String) {
|
||||||
|
val created = request(fixture())
|
||||||
|
val actor = actor(MemberRole.valueOf(role))
|
||||||
|
val operations = listOf(
|
||||||
|
HttpMethod.POST to "",
|
||||||
|
HttpMethod.GET to "/${created.requestId}",
|
||||||
|
HttpMethod.GET to "/${created.requestId}/contents",
|
||||||
|
HttpMethod.GET to "/${created.requestId}/grouping",
|
||||||
|
HttpMethod.PUT to "/${created.requestId}/grouping",
|
||||||
|
HttpMethod.POST to "/${created.requestId}/cancel",
|
||||||
|
HttpMethod.POST to "/${created.requestId}/generate",
|
||||||
|
HttpMethod.GET to "/${created.requestId}/candidates",
|
||||||
|
HttpMethod.POST to "/${created.requestId}/apply"
|
||||||
|
)
|
||||||
|
operations.forEach { (method, path) ->
|
||||||
|
val body = when (path) {
|
||||||
|
"" -> """{"clientRequestId":"${UUID.randomUUID()}","scope":{"type":"CONTENT","contentId":1}}"""
|
||||||
|
"/${created.requestId}/grouping" -> """{"expectedRevision":1,"groups":[],"individualContentIds":[]}"""
|
||||||
|
"/${created.requestId}/generate" -> """{"expectedRevision":1}"""
|
||||||
|
"/${created.requestId}/apply" -> """{"operationId":"${UUID.randomUUID()}","expectedRevision":1,"selections":[]}"""
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
assertFalse(callAs(actor, method, path, body).path("success").booleanValue())
|
||||||
|
}
|
||||||
|
assertEquals("PREPARING", requests.get(created.requestId).status.name)
|
||||||
|
assertEquals(1L, requestRepository.count())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("상태 조회는 준비를 실행하지 않으며 PREPARING 콘텐츠는 비어 있다")
|
||||||
|
fun shouldReadWithoutHiddenPreparation() {
|
||||||
|
val created = request(fixture(null))
|
||||||
|
assertEquals("PREPARING", call(HttpMethod.GET, "/${created.requestId}").path("data").path("status").asText())
|
||||||
|
assertEquals(0, call(HttpMethod.GET, "/${created.requestId}/contents").path("data").path("items").size())
|
||||||
|
assertEquals("REQUEST_NOT_READY", call(HttpMethod.GET, "/${created.requestId}/grouping").path("errorProperty").asText())
|
||||||
|
assertEquals(emptyList<String>(), provider.detectionQueries)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("분석 결과의 code point template와 valueIndex는 원문 구문 전체를 보존한다")
|
||||||
|
fun shouldReturnAnalyzedGroupingAndAcceptRevision() {
|
||||||
|
val first = fixture()
|
||||||
|
val second = fixture()
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
entityManager.find(AudioContent::class.java, first).title = "공통 제목 - 비 오는 날"
|
||||||
|
entityManager.find(AudioContent::class.java, second).title = "공통 제목 - 새로운 밤"
|
||||||
|
}
|
||||||
|
val created = requests.create(91, UUID.randomUUID(), TranslationSelectionScope.Series(series(listOf(first, second))))
|
||||||
|
preparation.prepareNext()
|
||||||
|
val proposed = call(HttpMethod.GET, "/${created.requestId}/grouping").path("data")
|
||||||
|
assertEquals("UNICODE_CODE_POINT", proposed.path("indexUnit").asText())
|
||||||
|
assertFalse(proposed.path("reviewed").asBoolean())
|
||||||
|
assertEquals(1, proposed.path("groups").size())
|
||||||
|
val segments = proposed.path("templates")[0].path("segments")
|
||||||
|
assertEquals("공통 제목 - 비 오는 날", segments.joinToString("") { it.path("sourceText").asText() })
|
||||||
|
assertEquals(
|
||||||
|
listOf("비 오는 날"),
|
||||||
|
segments.filter { it.path("kind").asText() == "RESIDUAL" }.map { it.path("sourceText").asText() }
|
||||||
|
)
|
||||||
|
assertEquals(0, segments.first { it.path("kind").asText() == "SHARED" }.path("valueIndex").asInt())
|
||||||
|
assertFalse(proposed.path("templates")[0].has("residualInputs"))
|
||||||
|
val review = """{
|
||||||
|
"expectedRevision":1,
|
||||||
|
"groups":[{"groupId":"g","memberContentIds":[$first,$second],"sharedParts":[{"partId":"p","occurrences":[
|
||||||
|
{"contentId":$first,"startCodePoint":0,"endCodePoint":5},
|
||||||
|
{"contentId":$second,"startCodePoint":0,"endCodePoint":5}
|
||||||
|
]}]}],"individualContentIds":[]
|
||||||
|
}"""
|
||||||
|
val accepted = call(HttpMethod.PUT, "/${created.requestId}/grouping", review).path("data")
|
||||||
|
assertEquals(2, accepted.path("revision").asLong())
|
||||||
|
assertTrue(accepted.path("reviewed").asBoolean())
|
||||||
|
assertEquals("공통 제목", accepted.path("groups")[0].path("sharedParts")[0].path("sourceText").asText())
|
||||||
|
assertEquals(
|
||||||
|
"REVISION_CONFLICT",
|
||||||
|
call(HttpMethod.PUT, "/${created.requestId}/grouping", review).path("errorProperty").asText()
|
||||||
|
)
|
||||||
|
assertEquals("READY_TO_GENERATE", requests.get(created.requestId).status.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = ["missing", "duplicate", "different", "grapheme", "language"])
|
||||||
|
@DisplayName("누락 중복 다른 공유 원문 grapheme 분할 언어 혼합 검토는 거부한다")
|
||||||
|
fun shouldRejectInvalidCompleteGrouping(kind: String) {
|
||||||
|
val first = fixture()
|
||||||
|
val second = fixture(if (kind == "language") "en" else "ko")
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
entityManager.find(AudioContent::class.java, first).title = "가\u0301 제목"
|
||||||
|
entityManager.find(AudioContent::class.java, second).title = "나\u0301 제목"
|
||||||
|
}
|
||||||
|
val created = requests.create(91, UUID.randomUUID(), TranslationSelectionScope.Series(series(listOf(first, second))))
|
||||||
|
preparation.prepareNext()
|
||||||
|
val body = when (kind) {
|
||||||
|
"missing" -> """{"expectedRevision":1,"groups":[],"individualContentIds":[$first]}"""
|
||||||
|
"duplicate" -> """{"expectedRevision":1,"groups":[],"individualContentIds":[$first,$first,$second]}"""
|
||||||
|
else -> {
|
||||||
|
val start = if (kind == "language") 3 else 0
|
||||||
|
val end = if (kind == "language") 5 else if (kind == "grapheme") 1 else 2
|
||||||
|
"""{
|
||||||
|
"expectedRevision":1,
|
||||||
|
"groups":[{"groupId":"g","memberContentIds":[$first,$second],"sharedParts":[{"partId":"p","occurrences":[
|
||||||
|
{"contentId":$first,"startCodePoint":$start,"endCodePoint":$end},
|
||||||
|
{"contentId":$second,"startCodePoint":$start,"endCodePoint":$end}
|
||||||
|
]}]}],"individualContentIds":[]
|
||||||
|
}"""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertEquals(
|
||||||
|
"INVALID_GROUPING",
|
||||||
|
call(HttpMethod.PUT, "/${created.requestId}/grouping", body).path("errorProperty").asText()
|
||||||
|
)
|
||||||
|
assertEquals(1, requests.get(created.requestId).revision)
|
||||||
|
assertEquals("REVIEW_REQUIRED", requests.get(created.requestId).status.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("모두 개별인 분석도 관리자 확정과 revision 증가가 필요하며 생성 뒤 편집은 거부한다")
|
||||||
|
fun shouldAcceptAllIndividualReview() {
|
||||||
|
val id = fixture()
|
||||||
|
val created = requests.create(91, UUID.randomUUID(), TranslationSelectionScope.Series(series(listOf(id))))
|
||||||
|
preparation.prepareNext()
|
||||||
|
assertEquals("REVIEW_REQUIRED", requests.get(created.requestId).status.name)
|
||||||
|
val body = """{"expectedRevision":1,"groups":[],"individualContentIds":[$id]}"""
|
||||||
|
assertEquals(2, call(HttpMethod.PUT, "/${created.requestId}/grouping", body).path("data").path("revision").asLong())
|
||||||
|
val edited = body.replace("\"expectedRevision\":1", "\"expectedRevision\":2")
|
||||||
|
assertEquals(3, call(HttpMethod.PUT, "/${created.requestId}/grouping", edited).path("data").path("revision").asLong())
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
requestStore.save(requestStore.lock(created.requestId)!!.copy(status = TranslationSelectionStatus.GENERATING))
|
||||||
|
}
|
||||||
|
assertEquals(
|
||||||
|
"REQUEST_NOT_READY",
|
||||||
|
call(HttpMethod.PUT, "/${created.requestId}/grouping", edited).path("errorProperty").asText()
|
||||||
|
)
|
||||||
|
assertEquals(0, provider.calls.get())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("취소는 revision 없이 즉시 확정하고 같은 취소는 같은 terminal 결과를 반환한다")
|
||||||
|
fun shouldCancelAndReplayWithoutRevision() {
|
||||||
|
val created = request(fixture())
|
||||||
|
val first = call(HttpMethod.POST, "/${created.requestId}/cancel")
|
||||||
|
assertEquals("CANCELLED", first.path("data").path("status").asText())
|
||||||
|
assertEquals(first, call(HttpMethod.POST, "/${created.requestId}/cancel"))
|
||||||
|
val body = """{"expectedRevision":1,"groups":[],"individualContentIds":[]}"""
|
||||||
|
assertEquals(
|
||||||
|
"REQUEST_CLOSED",
|
||||||
|
call(HttpMethod.PUT, "/${created.requestId}/grouping", body).path("errorProperty").asText()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("generate endpoint는 검토 전 시리즈를 REVIEW_REQUIRED로 거부하고 후보를 호출하지 않는다")
|
||||||
|
fun shouldRequireReviewBeforeGeneration() {
|
||||||
|
val id = fixture()
|
||||||
|
val created = requests.create(91, UUID.randomUUID(), TranslationSelectionScope.Series(series(listOf(id))))
|
||||||
|
preparation.prepareNext()
|
||||||
|
val response = http.exchange(
|
||||||
|
"$BASE/${created.requestId}/generate",
|
||||||
|
HttpMethod.POST,
|
||||||
|
HttpEntity("""{"expectedRevision":1}""", headers(actor())),
|
||||||
|
JsonNode::class.java
|
||||||
|
)
|
||||||
|
assertEquals(200, response.statusCodeValue)
|
||||||
|
assertFalse(response.body!!.path("success").asBoolean())
|
||||||
|
assertEquals("REVIEW_REQUIRED", response.body!!.path("errorProperty").asText())
|
||||||
|
assertEquals("REVIEW_REQUIRED", requests.get(created.requestId).status.name)
|
||||||
|
assertEquals(0, provider.calls.get())
|
||||||
|
}
|
||||||
|
}
|
||||||
+71
@@ -0,0 +1,71 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.`in`.web
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContent
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.ContentTranslationSourceService
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.TranslationSelectionApplyService
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.ApplyTranslationSelectionRequest
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionChoice
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionField
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationCandidateStore
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.DisplayName
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import org.springframework.http.HttpMethod
|
||||||
|
import java.time.Instant
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
class AdminTranslationSelectionReceiptHttpIntegrationTest : TranslationSelectionApplyHttpTestSupport() {
|
||||||
|
@Autowired private lateinit var candidates: TranslationCandidateStore
|
||||||
|
|
||||||
|
@Autowired private lateinit var sources: ContentTranslationSourceService
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(ints = [0, 123456789, 999999999])
|
||||||
|
@DisplayName("단일 승인 시각은 나노초 날짜 경계에서 모든 override와 receipt DB 왕복 HTTP에 정확히 일치한다")
|
||||||
|
fun shouldPersistAndReplayExactSecondPrecisionReceipt(nano: Int) {
|
||||||
|
val id = fixture()
|
||||||
|
val requestId = readyContent(id)
|
||||||
|
val candidate = candidatePage(requestId).single {
|
||||||
|
it.path("unit").path("field").asText() == "title" && it.path("provider").path("id").asText() == "papago" &&
|
||||||
|
it.path("targetLanguage").asText() == "en"
|
||||||
|
}.path("candidateId").asLong()
|
||||||
|
val fixed = LocalDateTime.of(2026, 11, 17, 23, 59, 59, nano)
|
||||||
|
val service = TranslationSelectionApplyService(requestStore, candidates, sources, writer) { fixed }
|
||||||
|
val body = ApplyTranslationSelectionRequest(
|
||||||
|
UUID.randomUUID(),
|
||||||
|
2,
|
||||||
|
listOf(TranslationSelectionChoice.Field(id, TranslationSelectionField.TITLE, "en", candidate))
|
||||||
|
)
|
||||||
|
|
||||||
|
val receipt = transaction.execute {
|
||||||
|
val result = service.apply(requestId, 777, body)
|
||||||
|
entityManager.flush()
|
||||||
|
entityManager.clear()
|
||||||
|
assertEquals(result, requestStore.find(requestId)?.applyReceipt)
|
||||||
|
result
|
||||||
|
}!!
|
||||||
|
|
||||||
|
assertEquals(Instant.parse("2026-11-17T23:59:59Z"), receipt.appliedAt)
|
||||||
|
assertEquals(
|
||||||
|
LocalDateTime.of(2026, 11, 17, 23, 59, 59),
|
||||||
|
transaction.execute { overrides.findByContentId(id).single().approvedAt }
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
LocalDateTime.of(2026, 11, 17, 23, 59, 59),
|
||||||
|
transaction.execute { requestRepository.findById(requestId).orElseThrow().appliedAt }
|
||||||
|
)
|
||||||
|
transaction.executeWithoutResult { entityManager.find(AudioContent::class.java, id).title = "원본은 이후 변경" }
|
||||||
|
val summary = call(HttpMethod.GET, "/$requestId").path("data").path("applyReceipt")
|
||||||
|
assertEquals("2026-11-17T23:59:59Z", summary.path("appliedAt").asText())
|
||||||
|
val replay = call(
|
||||||
|
HttpMethod.POST,
|
||||||
|
"/$requestId/apply",
|
||||||
|
"""{"operationId":"${body.operationId}","expectedRevision":2,
|
||||||
|
"selections":[{"kind":"FIELD","contentId":$id,"field":"title","targetLanguage":"en","candidateId":$candidate}]}"""
|
||||||
|
)
|
||||||
|
assertEquals(summary, replay.path("data"))
|
||||||
|
}
|
||||||
|
}
|
||||||
+50
@@ -0,0 +1,50 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.`in`.web
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.member.MemberRole
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.adapter.scheduler.TranslationSelectionWorker
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Assertions.assertFalse
|
||||||
|
import org.junit.jupiter.api.Assertions.assertNull
|
||||||
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
|
import org.junit.jupiter.api.DisplayName
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import org.springframework.context.ApplicationContext
|
||||||
|
import org.springframework.http.HttpMethod
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
class TranslationSelectionActivationHttpIntegrationTest : TranslationSelectionHttpTestSupport() {
|
||||||
|
@Autowired private lateinit var context: ApplicationContext
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("별도 활성화 설정 없이 ADMIN은 실제 HTTP로 생성·조회하고 일반 사용자는 거부한다")
|
||||||
|
fun shouldCreateAndReadAdminRequestWithoutActivationProperty() {
|
||||||
|
val id = fixture(null)
|
||||||
|
val body = """{"clientRequestId":"${UUID.randomUUID()}","scope":{"type":"CONTENT","contentId":$id}}"""
|
||||||
|
val created = call(HttpMethod.POST, "", body)
|
||||||
|
assertTrue(created.path("success").asBoolean())
|
||||||
|
val data = created.path("data")
|
||||||
|
assertEquals("PREPARING", data.path("status").asText())
|
||||||
|
val requestId = data.path("requestId").asLong()
|
||||||
|
assertEquals(data, call(HttpMethod.GET, "/$requestId").path("data"))
|
||||||
|
|
||||||
|
val user = actor(MemberRole.USER)
|
||||||
|
assertFalse(callAs(user, HttpMethod.POST, "", body).path("success").asBoolean())
|
||||||
|
assertFalse(callAs(user, HttpMethod.GET, "/$requestId", null).path("success").asBoolean())
|
||||||
|
assertEquals(1L, requestRepository.count())
|
||||||
|
assertEquals("PREPARING", requests.get(requestId).status.name)
|
||||||
|
assertEquals(emptyList<String>(), provider.detectionQueries)
|
||||||
|
assertEquals(0, provider.calls.get())
|
||||||
|
assertNull(context.environment.getProperty("sodalive.translation-selection.enabled"))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("별도 활성화 설정 없이 실제 후보 worker bean을 등록한다")
|
||||||
|
fun shouldRegisterWorkerWithoutActivationProperty() {
|
||||||
|
assertEquals(
|
||||||
|
1,
|
||||||
|
context.getBeansOfType(TranslationSelectionWorker::class.java).size
|
||||||
|
)
|
||||||
|
assertNull(context.environment.getProperty("sodalive.translation-selection.enabled"))
|
||||||
|
}
|
||||||
|
}
|
||||||
+37
@@ -0,0 +1,37 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.`in`.web
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.SelectionCandidateProviderConfiguration
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.SelectionPapagoProvider
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.TranslationSelectionWorkerService
|
||||||
|
import org.junit.jupiter.api.BeforeEach
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import org.springframework.context.annotation.Import
|
||||||
|
import org.springframework.http.HttpMethod
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
@Import(SelectionCandidateProviderConfiguration::class)
|
||||||
|
abstract class TranslationSelectionApplyHttpTestSupport : TranslationSelectionHttpTestSupport() {
|
||||||
|
@Autowired protected lateinit var selectionWorker: TranslationSelectionWorkerService
|
||||||
|
|
||||||
|
@Autowired protected lateinit var papago: SelectionPapagoProvider
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
fun resetApplyHttpProvider() { papago.reset() }
|
||||||
|
|
||||||
|
protected fun readyContent(id: Long): Long {
|
||||||
|
val result = call(
|
||||||
|
HttpMethod.POST,
|
||||||
|
"",
|
||||||
|
"""{"clientRequestId":"${UUID.randomUUID()}","scope":{"type":"CONTENT","contentId":$id}}"""
|
||||||
|
)
|
||||||
|
val requestId = result.path("data").path("requestId").asLong()
|
||||||
|
preparation.prepareNext()
|
||||||
|
call(HttpMethod.POST, "/$requestId/generate", """{"expectedRevision":1}""")
|
||||||
|
repeat(requests.get(requestId).progress.total + 1) { selectionWorker.runNext() }
|
||||||
|
return requestId
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun candidatePage(requestId: Long): List<JsonNode> =
|
||||||
|
call(HttpMethod.GET, "/$requestId/candidates?size=100").path("data").path("items").toList()
|
||||||
|
}
|
||||||
+46
@@ -0,0 +1,46 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.adapter.`in`.web
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode
|
||||||
|
import kr.co.vividnext.sodalive.member.Member
|
||||||
|
import kr.co.vividnext.sodalive.member.MemberRole
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.TranslationSelectionTestConfiguration
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.application.TranslationSelectionTestSupport
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest
|
||||||
|
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.http.MediaType
|
||||||
|
|
||||||
|
@SpringBootTest(
|
||||||
|
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||||
|
classes = [TranslationSelectionTestConfiguration::class],
|
||||||
|
properties = ["spring.cache.type=none"]
|
||||||
|
)
|
||||||
|
abstract class TranslationSelectionHttpTestSupport : TranslationSelectionTestSupport() {
|
||||||
|
@Autowired protected lateinit var http: TestRestTemplate
|
||||||
|
|
||||||
|
protected fun actor(role: MemberRole = MemberRole.ADMIN): Long = transaction.execute {
|
||||||
|
val member = Member(password = "test", nickname = "selection-http").apply { this.role = role }
|
||||||
|
entityManager.persist(member)
|
||||||
|
requireNotNull(member.id)
|
||||||
|
}!!
|
||||||
|
|
||||||
|
protected fun headers(actorId: Long) = HttpHeaders().apply {
|
||||||
|
contentType = MediaType.APPLICATION_JSON
|
||||||
|
set("X-Test-Member", actorId.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun call(method: HttpMethod, path: String, body: String? = null): JsonNode =
|
||||||
|
callAs(actor(), method, path, body)
|
||||||
|
|
||||||
|
protected fun callAs(actorId: Long, method: HttpMethod, path: String, body: String?): JsonNode {
|
||||||
|
val result = http.exchange(BASE + path, method, HttpEntity(body, headers(actorId)), JsonNode::class.java)
|
||||||
|
assertEquals(200, result.statusCodeValue)
|
||||||
|
return requireNotNull(result.body)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object { const val BASE = "/admin/audio-content/translation-requests" }
|
||||||
|
}
|
||||||
+133
@@ -0,0 +1,133 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.admin.content.AdminContentService
|
||||||
|
import kr.co.vividnext.sodalive.admin.content.series.genre.SeriesGenre
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContent
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContentService
|
||||||
|
import kr.co.vividnext.sodalive.creator.admin.content.CreatorAdminContentService
|
||||||
|
import kr.co.vividnext.sodalive.creator.admin.content.series.CreatorAdminContentSeriesService
|
||||||
|
import kr.co.vividnext.sodalive.creator.admin.content.series.Series
|
||||||
|
import kr.co.vividnext.sodalive.creator.admin.content.series.content.AddingContentToTheSeriesRequest
|
||||||
|
import kr.co.vividnext.sodalive.creator.admin.content.series.content.RemoveContentToTheSeriesRequest
|
||||||
|
import kr.co.vividnext.sodalive.v2.subtitle.application.SubtitleJobService
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
|
import org.junit.jupiter.api.DisplayName
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource
|
||||||
|
import org.mockito.Mockito
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import org.springframework.context.annotation.Import
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.util.concurrent.CountDownLatch
|
||||||
|
import java.util.concurrent.Executors
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
import javax.persistence.LockModeType
|
||||||
|
|
||||||
|
@Import(AdminContentService::class, CreatorAdminContentService::class, CreatorAdminContentSeriesService::class)
|
||||||
|
class ContentTranslationMembershipIntegrationTest : ContentTranslationTestSupport() {
|
||||||
|
@Autowired private lateinit var ownerService: AudioContentService
|
||||||
|
|
||||||
|
@Autowired private lateinit var adminService: AdminContentService
|
||||||
|
|
||||||
|
@Autowired private lateinit var creatorService: CreatorAdminContentService
|
||||||
|
|
||||||
|
@Autowired private lateinit var seriesService: CreatorAdminContentSeriesService
|
||||||
|
|
||||||
|
@Autowired private lateinit var subtitles: SubtitleJobService
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = ["delete", "admin", "creator", "upload", "publish"])
|
||||||
|
@DisplayName("활성 상태 변경 경로는 부모 잠금을 공유하면서 원문이 같은 승인 필드를 유지한다")
|
||||||
|
fun shouldKeepApprovalsAcrossActivityChanges(entry: String) {
|
||||||
|
val id = fixture()
|
||||||
|
approveAll(id)
|
||||||
|
val creator = transaction.execute {
|
||||||
|
val content = entityManager.find(AudioContent::class.java, id)
|
||||||
|
if (entry == "upload" || entry == "publish") content.isActive = false
|
||||||
|
content.releaseDate = LocalDateTime.of(2000, 1, 1, 0, 0)
|
||||||
|
content.member
|
||||||
|
}!!
|
||||||
|
Mockito.`when`(subtitles.matchesOutput(id, "$id.mp3")).thenReturn(true)
|
||||||
|
|
||||||
|
when (entry) {
|
||||||
|
"delete" -> ownerService.deleteAudioContent(id, creator)
|
||||||
|
"admin" -> adminService.updateAudioContent(null, """{"id":$id,"isActive":false,"isDefaultCoverImage":false}""")
|
||||||
|
"creator" -> creatorService.updateAudioContent(null, """{"id":$id,"isActive":false}""", creator)
|
||||||
|
"upload" -> ownerService.uploadComplete(id, "$id.mp3", "00:01:00")
|
||||||
|
"publish" -> ownerService.releaseContent()
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
entry == "upload" || entry == "publish",
|
||||||
|
transaction.execute { entityManager.find(AudioContent::class.java, id).isActive }
|
||||||
|
)
|
||||||
|
assertEquals(3L, overrideRepository.count())
|
||||||
|
assertEquals("Approved title", payload(id)?.title)
|
||||||
|
assertEquals(0L, jobs.count())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("시리즈 잠금 뒤 최신 연관 목록을 다시 읽어 추가와 제거를 직렬화한다")
|
||||||
|
fun shouldRefreshMembershipUnderSeriesParentLock() {
|
||||||
|
val id = fixture()
|
||||||
|
val seriesId = transaction.execute {
|
||||||
|
val content = entityManager.find(AudioContent::class.java, id)
|
||||||
|
val genre = SeriesGenre("approval-genre")
|
||||||
|
entityManager.persist(genre)
|
||||||
|
val series = Series("approval-series", "intro", "ko").apply {
|
||||||
|
member = content.member
|
||||||
|
this.genre = genre
|
||||||
|
}
|
||||||
|
entityManager.persist(series)
|
||||||
|
requireNotNull(series.id)
|
||||||
|
}!!
|
||||||
|
val ownerId = transaction.execute { requireNotNull(entityManager.find(AudioContent::class.java, id).member?.id) }!!
|
||||||
|
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
val stale = entityManager.find(Series::class.java, seriesId)
|
||||||
|
assertEquals(0, stale.contentList.size)
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
seriesService.addingContentToTheSeries(AddingContentToTheSeriesRequest(seriesId, listOf(id)), ownerId)
|
||||||
|
}
|
||||||
|
seriesService.removeContentInTheSeries(RemoveContentToTheSeriesRequest(seriesId, id), ownerId)
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(0, transaction.execute { entityManager.find(Series::class.java, seriesId).contentList.size })
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("명시적으로 잡은 콘텐츠 부모 잠금과 활성 변경이 경쟁해도 변경은 잠금 해제 뒤 반영된다")
|
||||||
|
fun shouldSerializeActivityMutationWithContentParentLock() {
|
||||||
|
val id = fixture()
|
||||||
|
val creator = transaction.execute { entityManager.find(AudioContent::class.java, id).member }!!
|
||||||
|
val locked = CountDownLatch(1)
|
||||||
|
val started = CountDownLatch(1)
|
||||||
|
val release = CountDownLatch(1)
|
||||||
|
val executor = Executors.newFixedThreadPool(2)
|
||||||
|
try {
|
||||||
|
val holder = executor.submit {
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
val content = writer.lockContent(id)!!
|
||||||
|
assertEquals(LockModeType.PESSIMISTIC_WRITE, entityManager.getLockMode(content))
|
||||||
|
locked.countDown()
|
||||||
|
check(release.await(5, TimeUnit.SECONDS))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertTrue(locked.await(5, TimeUnit.SECONDS))
|
||||||
|
val mutation = executor.submit {
|
||||||
|
started.countDown()
|
||||||
|
ownerService.deleteAudioContent(id, creator)
|
||||||
|
}
|
||||||
|
assertTrue(started.await(5, TimeUnit.SECONDS))
|
||||||
|
release.countDown()
|
||||||
|
holder.get(10, TimeUnit.SECONDS)
|
||||||
|
mutation.get(10, TimeUnit.SECONDS)
|
||||||
|
} finally {
|
||||||
|
release.countDown()
|
||||||
|
executor.shutdownNow()
|
||||||
|
}
|
||||||
|
assertEquals(false, transaction.execute { entityManager.find(AudioContent::class.java, id).isActive })
|
||||||
|
}
|
||||||
|
}
|
||||||
+183
@@ -0,0 +1,183 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.common.SodaException
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContent
|
||||||
|
import kr.co.vividnext.sodalive.content.translation.ContentTranslation
|
||||||
|
import kr.co.vividnext.sodalive.content.translation.ContentTranslationPayload
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence.JpaContentTranslationOverrideStore
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Assertions.assertFalse
|
||||||
|
import org.junit.jupiter.api.Assertions.assertNull
|
||||||
|
import org.junit.jupiter.api.Assertions.assertThrows
|
||||||
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
|
import org.junit.jupiter.api.DisplayName
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource
|
||||||
|
import org.mockito.Mockito
|
||||||
|
import org.springframework.boot.test.mock.mockito.SpyBean
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.util.concurrent.CountDownLatch
|
||||||
|
import java.util.concurrent.Executors
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
import javax.persistence.LockModeType
|
||||||
|
|
||||||
|
class ContentTranslationOverrideIntegrationTest : ContentTranslationTestSupport() {
|
||||||
|
@SpyBean private lateinit var overrideReads: JpaContentTranslationOverrideStore
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("잠금 전 영속성 문맥에 남은 원문으로 자동 번역 결과를 쓰지 않는다")
|
||||||
|
fun shouldRejectAutomaticResultWithStaleSource() {
|
||||||
|
val id = fixture()
|
||||||
|
cache(snapshot(id))
|
||||||
|
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
entityManager.find(AudioContent::class.java, id).toContentSourceSnapshot()
|
||||||
|
transaction.executeWithoutResult { entityManager.find(AudioContent::class.java, id).title = "현재 제목" }
|
||||||
|
assertFalse(materializer.materialize(LanguageTranslationTargetType.CONTENT, id, "en"))
|
||||||
|
}
|
||||||
|
|
||||||
|
assertNull(payload(id))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("현재 승인은 같은 원문의 자동 번역 재실행보다 우선한다")
|
||||||
|
fun shouldProtectCurrentApprovalFromAutomaticWrite() {
|
||||||
|
val id = fixture()
|
||||||
|
cache(snapshot(id))
|
||||||
|
writer.approve(id, "en", listOf(approval(snapshot(id), ContentSourceFieldKey.TITLE)))
|
||||||
|
|
||||||
|
assertTrue(materializer.materialize(LanguageTranslationTargetType.CONTENT, id, "en"))
|
||||||
|
|
||||||
|
assertEquals(ContentTranslationPayload("Approved title", "Automatic detail", "Automatic tags"), payload(id))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("전역 메모리가 전혀 없어도 승인된 세 필드를 재료화한다")
|
||||||
|
fun shouldMaterializeApprovedFieldsWithoutSharedMemory() {
|
||||||
|
val id = fixture()
|
||||||
|
approveAll(id)
|
||||||
|
translations.deleteAllInBatch()
|
||||||
|
|
||||||
|
assertTrue(materializer.materialize(LanguageTranslationTargetType.CONTENT, id, "en"))
|
||||||
|
|
||||||
|
assertEquals(ContentTranslationPayload("Approved title", "Approved detail", "Approved tags"), payload(id))
|
||||||
|
assertEquals(0L, memories.count())
|
||||||
|
assertEquals(0, provider.calls.get())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("일부 필드 승인 시 선택하지 않은 기존 문자열의 모든 바이트를 유지한다")
|
||||||
|
fun shouldKeepUnselectedFieldsByteForByte() {
|
||||||
|
val id = fixture()
|
||||||
|
translations.saveAndFlush(ContentTranslation(id, "en", ContentTranslationPayload("Old", " \tDetail\n", "#A, #B ")))
|
||||||
|
|
||||||
|
writer.approve(id, "en", listOf(approval(snapshot(id), ContentSourceFieldKey.TITLE)))
|
||||||
|
|
||||||
|
assertEquals(ContentTranslationPayload("Approved title", " \tDetail\n", "#A, #B "), payload(id))
|
||||||
|
assertEquals(1L, overrideRepository.count())
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(ints = [0, 123456789, 999999999])
|
||||||
|
@DisplayName("승인 출처와 UTC 초 시각은 날짜 경계에서도 flush clear 왕복 후 정확히 같다")
|
||||||
|
fun shouldPersistApprovalProvenance(nano: Int) {
|
||||||
|
val id = fixture()
|
||||||
|
val fixed = LocalDateTime.of(2026, 11, 17, 23, 59, 59, nano)
|
||||||
|
val timedWriter = ContentTranslationWriteService(entityManager, translations, overrides, coordinator) { fixed }
|
||||||
|
val source = snapshot(id)
|
||||||
|
|
||||||
|
val result = transaction.execute {
|
||||||
|
val saved = timedWriter.approve(id, "en", listOf(approval(source, ContentSourceFieldKey.TITLE)))
|
||||||
|
entityManager.flush()
|
||||||
|
entityManager.clear()
|
||||||
|
saved
|
||||||
|
}!!
|
||||||
|
|
||||||
|
val persisted = transaction.execute { overrides.findByContentId(id).single() }!!
|
||||||
|
assertEquals(LocalDateTime.of(2026, 11, 17, 23, 59, 59), result.single().approvedAt)
|
||||||
|
assertEquals(result.single(), persisted)
|
||||||
|
assertEquals(provenance, persisted.approval.provenance)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("선택한 필드가 달라진 승인은 아무 값도 쓰지 않는다")
|
||||||
|
fun shouldRejectApprovalForChangedSelectedSource() {
|
||||||
|
val id = fixture()
|
||||||
|
val old = snapshot(id)
|
||||||
|
transaction.executeWithoutResult { entityManager.find(AudioContent::class.java, id).title = "현재 제목" }
|
||||||
|
|
||||||
|
val error = assertThrows(SodaException::class.java) {
|
||||||
|
writer.approve(id, "en", listOf(approval(old, ContentSourceFieldKey.TITLE)))
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals("SOURCE_CHANGED", error.errorProperty)
|
||||||
|
assertNull(payload(id))
|
||||||
|
assertEquals(0L, overrideRepository.count())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("후속 그룹 적용용 외부 트랜잭션에서 한 승인이 실패하면 앞선 승인도 함께 롤백한다")
|
||||||
|
fun shouldParticipateInOuterTransactionAcrossContents() {
|
||||||
|
val first = fixture()
|
||||||
|
val second = fixture()
|
||||||
|
val source = snapshot(first)
|
||||||
|
|
||||||
|
assertThrows(SodaException::class.java) {
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
writer.approve(first, "en", listOf(approval(source, ContentSourceFieldKey.TITLE)))
|
||||||
|
writer.approve(
|
||||||
|
second,
|
||||||
|
"en",
|
||||||
|
listOf(approval(source, ContentSourceFieldKey.TITLE).copy(sourceFingerprint = "stale"))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assertNull(payload(first))
|
||||||
|
assertEquals(0L, overrideRepository.count())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("부모 잠금 안에서 승인 중인 값은 대기한 자동 writer에 의해 덮어써지지 않는다")
|
||||||
|
fun shouldSerializeApprovalAndAutomaticWriter() {
|
||||||
|
val id = fixture()
|
||||||
|
cache(snapshot(id))
|
||||||
|
Mockito.doAnswer {
|
||||||
|
assertEquals(
|
||||||
|
LockModeType.PESSIMISTIC_WRITE,
|
||||||
|
entityManager.getLockMode(entityManager.find(AudioContent::class.java, id))
|
||||||
|
)
|
||||||
|
it.callRealMethod()
|
||||||
|
}.`when`(overrideReads).findByContentId(id)
|
||||||
|
val locked = CountDownLatch(1)
|
||||||
|
val automaticStarted = CountDownLatch(1)
|
||||||
|
val releaseApproval = CountDownLatch(1)
|
||||||
|
val executor = Executors.newFixedThreadPool(2)
|
||||||
|
try {
|
||||||
|
val approval = executor.submit {
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
writer.lockContent(id)
|
||||||
|
writer.approve(id, "en", listOf(approval(snapshot(id), ContentSourceFieldKey.TITLE)))
|
||||||
|
locked.countDown()
|
||||||
|
check(releaseApproval.await(5, TimeUnit.SECONDS))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertTrue(locked.await(5, TimeUnit.SECONDS))
|
||||||
|
val automatic = executor.submit<Boolean> {
|
||||||
|
automaticStarted.countDown()
|
||||||
|
materializer.materialize(LanguageTranslationTargetType.CONTENT, id, "en")
|
||||||
|
}
|
||||||
|
assertTrue(automaticStarted.await(5, TimeUnit.SECONDS))
|
||||||
|
releaseApproval.countDown()
|
||||||
|
approval.get(10, TimeUnit.SECONDS)
|
||||||
|
assertTrue(automatic.get(10, TimeUnit.SECONDS))
|
||||||
|
} finally {
|
||||||
|
releaseApproval.countDown()
|
||||||
|
executor.shutdownNow()
|
||||||
|
}
|
||||||
|
assertEquals("Approved title", payload(id)?.title)
|
||||||
|
}
|
||||||
|
}
|
||||||
+211
@@ -0,0 +1,211 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.SourceTextNormalizer
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationJobRepository
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationJobScheduler
|
||||||
|
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.TranslationMemoryService
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationReadModelMaterializer
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Assertions.assertFalse
|
||||||
|
import org.junit.jupiter.api.Assertions.assertNull
|
||||||
|
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.mockito.AdditionalAnswers
|
||||||
|
import org.mockito.Mockito
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import org.springframework.boot.test.mock.mockito.SpyBean
|
||||||
|
import org.springframework.data.jpa.repository.Query
|
||||||
|
import org.springframework.transaction.PlatformTransactionManager
|
||||||
|
import org.springframework.transaction.support.TransactionSynchronizationManager
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.util.concurrent.CountDownLatch
|
||||||
|
import java.util.concurrent.Executors
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
|
class ContentTranslationQueueIntegrationTest : ContentTranslationTestSupport() {
|
||||||
|
@Autowired private lateinit var worker: TranslationJobWorker
|
||||||
|
|
||||||
|
@SpyBean private lateinit var claims: TranslationJobRepository
|
||||||
|
|
||||||
|
@Autowired private lateinit var memoryService: TranslationMemoryService
|
||||||
|
|
||||||
|
@Autowired private lateinit var transactionManager: PlatformTransactionManager
|
||||||
|
|
||||||
|
@Autowired private lateinit var invalidation: ContentTranslationSourceInvalidationService
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
fun useH2ClaimSyntax() {
|
||||||
|
val sql = TranslationJobRepository::class.java.getMethod("findNextPendingGroupJobIdsForUpdate", LocalDateTime::class.java)
|
||||||
|
.getAnnotation(Query::class.java).value.replace("skip locked", "")
|
||||||
|
Mockito.doAnswer { call ->
|
||||||
|
entityManager.createNativeQuery(sql).setParameter("now", call.getArgument<LocalDateTime>(0))
|
||||||
|
.resultList.map { (it as Number).toLong() }
|
||||||
|
}.`when`(claims).findNextPendingGroupJobIdsForUpdate(Mockito.any(LocalDateTime::class.java) ?: LocalDateTime.now())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("설정 없이 같은 원문 hash와 필드도 원문 언어마다 별도 작업으로 저장한다")
|
||||||
|
fun shouldDistinguishJobsBySourceLanguage() {
|
||||||
|
enqueue(10L, "ko")
|
||||||
|
enqueue(10L, "ja")
|
||||||
|
|
||||||
|
assertEquals(setOf("ko", "ja"), jobs.findAll().map { it.sourceLanguage }.toSet())
|
||||||
|
assertEquals(2L, jobs.count())
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
val ids = claims.findNextPendingGroupJobIdsForUpdate(LocalDateTime.now())
|
||||||
|
assertEquals(1, ids.size)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("같은 원문 언어의 반복 요청은 기존 작업 하나를 유지한다")
|
||||||
|
fun shouldDeduplicateSameSourceLanguage() {
|
||||||
|
repeat(2) { enqueue(10L, "ko") }
|
||||||
|
|
||||||
|
assertEquals(listOf("ko"), jobs.findAll().map { it.sourceLanguage })
|
||||||
|
assertEquals(1L, jobs.count())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("같은 hash의 다른 언어 작업은 보존하고 exact 언어 작업만 재개한다")
|
||||||
|
fun shouldRestartOnlyExactLanguageJob() {
|
||||||
|
enqueue(10L, "ko")
|
||||||
|
enqueue(10L, "ja")
|
||||||
|
jobs.findAll().forEach { jobs.saveAndFlush(it.apply { status = TranslationJobStatus.FAILED }) }
|
||||||
|
|
||||||
|
enqueue(10L, "ja")
|
||||||
|
|
||||||
|
assertEquals(TranslationJobStatus.FAILED, jobs.findAll().single { it.sourceLanguage == "ko" }.status)
|
||||||
|
assertEquals(TranslationJobStatus.PENDING, jobs.findAll().single { it.sourceLanguage == "ja" }.status)
|
||||||
|
assertEquals(2L, jobs.count())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("materialize false는 완료가 아니라 기존 재시도 정책으로 돌아간다")
|
||||||
|
fun shouldNotCompleteJobWhenMaterializationReturnsFalse() {
|
||||||
|
val id = fixture()
|
||||||
|
memories.saveAndFlush(
|
||||||
|
TranslationMemory(SourceTextNormalizer.hash("원문 제목"), "원문 제목", "ko", "en", "Cached title", "aws", "test")
|
||||||
|
)
|
||||||
|
enqueue(id, "ko")
|
||||||
|
|
||||||
|
assertTrue(worker.processNextGroup())
|
||||||
|
|
||||||
|
val job = jobs.findAll().single()
|
||||||
|
assertEquals(TranslationJobStatus.PENDING, job.status)
|
||||||
|
assertEquals(1, job.retryCount)
|
||||||
|
assertEquals("translation materialization is incomplete", job.lastErrorMessage)
|
||||||
|
assertNull(payload(id))
|
||||||
|
assertEquals(0, provider.calls.get())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("승인된 필드의 자동 작업이 재실행돼도 공급자나 전역 메모리가 필요하지 않다")
|
||||||
|
fun shouldSkipProviderForCurrentApprovedField() {
|
||||||
|
val id = fixture()
|
||||||
|
approveAll(id)
|
||||||
|
provider.fail = true
|
||||||
|
enqueue(id, "ko")
|
||||||
|
|
||||||
|
assertTrue(worker.processNextGroup())
|
||||||
|
|
||||||
|
assertEquals(TranslationJobStatus.COMPLETED, jobs.findAll().single().status)
|
||||||
|
assertEquals("Approved title", payload(id)?.title)
|
||||||
|
assertEquals(0L, memories.count())
|
||||||
|
assertEquals(0, provider.calls.get())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("재료화와 완료 사이 같은 정규화 hash의 원문이 바뀌어도 refresh 작업을 잃지 않는다")
|
||||||
|
fun shouldKeepRefreshWhenRawSourceChangesDuringWorkerCompletion() {
|
||||||
|
val id = fixture()
|
||||||
|
listOf("원문 설명" to "Cached detail", "#힐링" to "#Cached").forEach { (source, translated) ->
|
||||||
|
memories.saveAndFlush(
|
||||||
|
TranslationMemory(SourceTextNormalizer.hash(source), source, "ko", "en", translated, "aws", "test")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
enqueue(id, "ko")
|
||||||
|
val originalJobId = jobs.findAll().single().id
|
||||||
|
val materialized = CountDownLatch(1)
|
||||||
|
val mutationStarted = CountDownLatch(1)
|
||||||
|
val mutationCommitted = CountDownLatch(1)
|
||||||
|
val pausedMaterializer = Mockito.mock(
|
||||||
|
TranslationReadModelMaterializer::class.java,
|
||||||
|
AdditionalAnswers.delegatesTo<TranslationReadModelMaterializer>(materializer)
|
||||||
|
)
|
||||||
|
Mockito.doAnswer {
|
||||||
|
val result = materializer.materializeContentFields(id, "en", setOf("title"))
|
||||||
|
materialized.countDown()
|
||||||
|
if (TransactionSynchronizationManager.isActualTransactionActive()) {
|
||||||
|
check(mutationStarted.await(10, TimeUnit.SECONDS))
|
||||||
|
} else {
|
||||||
|
check(mutationCommitted.await(10, TimeUnit.SECONDS))
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}.`when`(pausedMaterializer).materializeContentFields(id, "en", setOf("title"))
|
||||||
|
val completingWorker = TranslationJobWorker(jobs, memoryService, pausedMaterializer, transactionManager)
|
||||||
|
provider.beforeTranslate = { assertFalse(TransactionSynchronizationManager.isActualTransactionActive()) }
|
||||||
|
val executor = Executors.newFixedThreadPool(2)
|
||||||
|
try {
|
||||||
|
val completion = executor.submit<Boolean> { completingWorker.processNextGroup() }
|
||||||
|
assertTrue(materialized.await(10, TimeUnit.SECONDS))
|
||||||
|
val mutation = executor.submit {
|
||||||
|
mutationStarted.countDown()
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
val content = requireNotNull(writer.lockContent(id))
|
||||||
|
val before = content.toContentSourceSnapshot()
|
||||||
|
content.title = "원문 제목 "
|
||||||
|
invalidation.invalidateChanged(content, before)
|
||||||
|
}
|
||||||
|
mutationCommitted.countDown()
|
||||||
|
}
|
||||||
|
assertTrue(completion.get(15, TimeUnit.SECONDS))
|
||||||
|
mutation.get(15, TimeUnit.SECONDS)
|
||||||
|
} finally {
|
||||||
|
mutationStarted.countDown()
|
||||||
|
mutationCommitted.countDown()
|
||||||
|
executor.shutdownNow()
|
||||||
|
assertTrue(executor.awaitTermination(10, TimeUnit.SECONDS))
|
||||||
|
}
|
||||||
|
|
||||||
|
val refreshed = jobs.findAll().single { it.targetLanguage == "en" }
|
||||||
|
assertEquals(originalJobId, refreshed.id)
|
||||||
|
assertEquals(SourceTextNormalizer.hash("원문 제목"), refreshed.sourceHash)
|
||||||
|
assertEquals(TranslationJobStatus.PENDING, refreshed.status)
|
||||||
|
assertEquals("원문 제목 ", payload(id)?.title)
|
||||||
|
assertTrue(worker.processNextGroup())
|
||||||
|
assertEquals("English title", payload(id)?.title)
|
||||||
|
assertEquals(TranslationJobStatus.COMPLETED, jobs.findById(requireNotNull(originalJobId)).orElseThrow().status)
|
||||||
|
assertEquals(1, provider.calls.get())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("비 CONTENT 리소스도 언어 identity만 바뀌고 완료 작업 정책은 유지한다")
|
||||||
|
fun shouldPreserveOtherResourceCompletedPolicy() {
|
||||||
|
enqueue(10L, "ko", LanguageTranslationTargetType.CHARACTER)
|
||||||
|
jobs.saveAndFlush(jobs.findAll().single().apply { status = TranslationJobStatus.COMPLETED })
|
||||||
|
|
||||||
|
enqueue(10L, "ko", LanguageTranslationTargetType.CHARACTER)
|
||||||
|
enqueue(10L, "ja", LanguageTranslationTargetType.CHARACTER)
|
||||||
|
|
||||||
|
assertEquals(2L, jobs.count())
|
||||||
|
assertEquals(TranslationJobStatus.COMPLETED, jobs.findAll().single { it.sourceLanguage == "ko" }.status)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun enqueue(
|
||||||
|
id: Long,
|
||||||
|
language: String,
|
||||||
|
type: LanguageTranslationTargetType = LanguageTranslationTargetType.CONTENT
|
||||||
|
) {
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
TranslationJobScheduler(jobs, coordinator, translations, entityManager)
|
||||||
|
.scheduleMissingTranslation(type, id, "title", "원문 제목", language, "en")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+156
@@ -0,0 +1,156 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContent
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.SourceTextNormalizer
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationJob
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationJobRepository
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationJobStatus
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Assertions.assertNull
|
||||||
|
import org.junit.jupiter.api.DisplayName
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
|
import org.junit.jupiter.params.provider.EnumSource
|
||||||
|
import org.mockito.Mockito
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import org.springframework.boot.test.mock.mockito.SpyBean
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
|
class ContentTranslationRefreshIntegrationTest : ContentTranslationTestSupport() {
|
||||||
|
@Autowired private lateinit var invalidation: ContentTranslationSourceInvalidationService
|
||||||
|
|
||||||
|
@SpyBean private lateinit var reads: TranslationJobRepository
|
||||||
|
|
||||||
|
private val hash = SourceTextNormalizer.hash("원문 제목")
|
||||||
|
private val previousRetryAt = LocalDateTime.of(2040, 1, 1, 0, 0)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("managed RUNNING 뒤 다른 트랜잭션이 완료한 작업도 현재 상태로 refresh한다")
|
||||||
|
fun shouldRefreshCompletedJobDespiteStaleManagedRunningState() {
|
||||||
|
val contentId = fixture()
|
||||||
|
val jobId = persistJob(contentId, TranslationJobStatus.RUNNING)
|
||||||
|
val otherLanguageJobId = persistJob(contentId, language = "ja")
|
||||||
|
|
||||||
|
val observed = transaction.execute {
|
||||||
|
entityManager.find(AudioContent::class.java, contentId).toContentSourceSnapshot()
|
||||||
|
val stale = jobs.findById(jobId).orElseThrow()
|
||||||
|
transaction.executeWithoutResult { jobs.findById(jobId).orElseThrow().status = TranslationJobStatus.COMPLETED }
|
||||||
|
assertEquals(TranslationJobStatus.RUNNING, stale.status)
|
||||||
|
invalidateTitle(contentId)
|
||||||
|
stale.status
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(TranslationJobStatus.PENDING, jobs.findById(jobId).orElseThrow().status)
|
||||||
|
assertEquals(TranslationJobStatus.PENDING, observed)
|
||||||
|
assertEquals(TranslationJobStatus.COMPLETED, jobs.findById(otherLanguageJobId).orElseThrow().status)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("기존 consistent read가 비어 있어도 이후 commit된 exact 언어 작업을 refresh한다")
|
||||||
|
fun shouldRefreshJobCommittedAfterEmptyConsistentRead() {
|
||||||
|
val contentId = fixture()
|
||||||
|
val jobId = transaction.execute {
|
||||||
|
entityManager.find(AudioContent::class.java, contentId).toContentSourceSnapshot()
|
||||||
|
assertNull(
|
||||||
|
reads.findByResourceTypeAndResourceIdAndFieldKeyAndTargetLanguageAndSourceHashAndSourceLanguage(
|
||||||
|
LanguageTranslationTargetType.CONTENT,
|
||||||
|
contentId,
|
||||||
|
"title",
|
||||||
|
"en",
|
||||||
|
hash,
|
||||||
|
"ko"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
val inserted = persistJob(contentId)
|
||||||
|
Mockito.doReturn(null).`when`(reads)
|
||||||
|
.findByResourceTypeAndResourceIdAndFieldKeyAndTargetLanguageAndSourceHashAndSourceLanguage(
|
||||||
|
LanguageTranslationTargetType.CONTENT,
|
||||||
|
contentId,
|
||||||
|
"title",
|
||||||
|
"en",
|
||||||
|
hash,
|
||||||
|
"ko"
|
||||||
|
)
|
||||||
|
invalidateTitle(contentId)
|
||||||
|
inserted
|
||||||
|
}!!
|
||||||
|
|
||||||
|
assertEquals(TranslationJobStatus.PENDING, jobs.findById(jobId).orElseThrow().status)
|
||||||
|
assertEquals(1, jobs.findAll().count { it.resourceId == contentId && it.targetLanguage == "en" })
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("기존 snapshot 뒤 다른 언어 작업이 commit돼도 현재 원문 언어의 독립 refresh를 생성한다")
|
||||||
|
fun shouldCreateIndependentLanguageRefreshAgainstCurrentRows() {
|
||||||
|
val contentId = fixture()
|
||||||
|
val otherLanguageId = transaction.execute {
|
||||||
|
entityManager.find(AudioContent::class.java, contentId).toContentSourceSnapshot()
|
||||||
|
val inserted = persistJob(contentId, language = "ja")
|
||||||
|
invalidateTitle(contentId)
|
||||||
|
inserted
|
||||||
|
}!!
|
||||||
|
|
||||||
|
val englishJobs = jobs.findAll().filter { it.resourceId == contentId && it.targetLanguage == "en" }
|
||||||
|
assertEquals(setOf("ko", "ja"), englishJobs.map { it.sourceLanguage }.toSet())
|
||||||
|
assertEquals(2, englishJobs.size)
|
||||||
|
val current = englishJobs.single { it.sourceLanguage == "ko" }
|
||||||
|
assertEquals(TranslationJobStatus.PENDING, current.status)
|
||||||
|
assertEquals(hash, current.sourceHash)
|
||||||
|
val other = englishJobs.single { it.id == otherLanguageId }
|
||||||
|
assertEquals("ja", other.sourceLanguage)
|
||||||
|
assertEquals(TranslationJobStatus.COMPLETED, other.status)
|
||||||
|
assertEquals(4, other.retryCount)
|
||||||
|
assertEquals(previousRetryAt, other.nextRetryAt)
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@EnumSource(TranslationJobStatus::class)
|
||||||
|
@DisplayName("강제 refresh는 현재 종료 상태만 재개하고 진행 중 작업의 예산을 유지한다")
|
||||||
|
fun shouldRestartOnlyCurrentTerminalJob(status: TranslationJobStatus) {
|
||||||
|
val contentId = fixture()
|
||||||
|
val jobId = persistJob(contentId, status)
|
||||||
|
|
||||||
|
transaction.executeWithoutResult { invalidateTitle(contentId) }
|
||||||
|
|
||||||
|
val current = jobs.findById(jobId).orElseThrow()
|
||||||
|
val restart = status == TranslationJobStatus.COMPLETED || status == TranslationJobStatus.FAILED
|
||||||
|
assertEquals(if (restart) TranslationJobStatus.PENDING else status, current.status)
|
||||||
|
assertEquals(if (restart) 0 else 4, current.retryCount)
|
||||||
|
assertEquals(if (restart) null else "previous failure", current.lastErrorMessage)
|
||||||
|
if (!restart) assertEquals(previousRetryAt, current.nextRetryAt)
|
||||||
|
assertEquals("ko", current.sourceLanguage)
|
||||||
|
assertEquals(hash, current.sourceHash)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun invalidateTitle(contentId: Long) {
|
||||||
|
val content = requireNotNull(writer.lockContent(contentId))
|
||||||
|
val before = content.toContentSourceSnapshot()
|
||||||
|
content.title = "원문 제목 "
|
||||||
|
invalidation.invalidateChanged(content, before)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun persistJob(
|
||||||
|
contentId: Long,
|
||||||
|
status: TranslationJobStatus = TranslationJobStatus.COMPLETED,
|
||||||
|
language: String = "ko"
|
||||||
|
): Long = transaction.execute {
|
||||||
|
requireNotNull(
|
||||||
|
jobs.saveAndFlush(
|
||||||
|
TranslationJob(
|
||||||
|
LanguageTranslationTargetType.CONTENT,
|
||||||
|
contentId,
|
||||||
|
"title",
|
||||||
|
hash,
|
||||||
|
"원문 제목",
|
||||||
|
language,
|
||||||
|
"en",
|
||||||
|
status,
|
||||||
|
4,
|
||||||
|
"previous failure",
|
||||||
|
previousRetryAt
|
||||||
|
)
|
||||||
|
).id
|
||||||
|
)
|
||||||
|
}!!
|
||||||
|
}
|
||||||
+197
@@ -0,0 +1,197 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import com.amazonaws.services.s3.model.ObjectMetadata
|
||||||
|
import kr.co.vividnext.sodalive.admin.content.AdminContentService
|
||||||
|
import kr.co.vividnext.sodalive.aws.s3.S3Uploader
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContent
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContentDetailTranslationService
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContentService
|
||||||
|
import kr.co.vividnext.sodalive.content.LanguageDetectEvent
|
||||||
|
import kr.co.vividnext.sodalive.content.LanguageDetectListener
|
||||||
|
import kr.co.vividnext.sodalive.creator.admin.content.CreatorAdminContentService
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationJobScheduler
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationJobStatus
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Assertions.assertFalse
|
||||||
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
|
import org.junit.jupiter.api.DisplayName
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
|
import org.junit.jupiter.params.provider.CsvSource
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource
|
||||||
|
import org.mockito.Mockito
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import org.springframework.context.annotation.Import
|
||||||
|
import org.springframework.mock.web.MockMultipartFile
|
||||||
|
import org.springframework.transaction.support.TransactionSynchronizationManager
|
||||||
|
import java.io.ByteArrayInputStream
|
||||||
|
import java.io.InputStream
|
||||||
|
|
||||||
|
@Import(AdminContentService::class, CreatorAdminContentService::class)
|
||||||
|
class ContentTranslationSourceInvalidationIntegrationTest : ContentTranslationTestSupport() {
|
||||||
|
@Autowired private lateinit var ownerService: AudioContentService
|
||||||
|
|
||||||
|
@Autowired private lateinit var adminService: AdminContentService
|
||||||
|
|
||||||
|
@Autowired private lateinit var creatorService: CreatorAdminContentService
|
||||||
|
|
||||||
|
@Autowired private lateinit var detailService: AudioContentDetailTranslationService
|
||||||
|
|
||||||
|
@Autowired private lateinit var languageService: ContentTranslationLanguageService
|
||||||
|
|
||||||
|
@Autowired private lateinit var detectorListener: LanguageDetectListener
|
||||||
|
|
||||||
|
@Autowired private lateinit var uploader: S3Uploader
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = ["owner", "admin", "creator"])
|
||||||
|
@DisplayName("각 원문 수정 진입점은 제목 승인만 해제하고 같은 트랜잭션에 fallback과 refresh를 저장한다")
|
||||||
|
fun shouldInvalidateOnlyChangedField(entry: String) {
|
||||||
|
val id = fixture()
|
||||||
|
approveAll(id)
|
||||||
|
val creator = transaction.execute { entityManager.find(AudioContent::class.java, id).member }!!
|
||||||
|
val image = MockMultipartFile("coverImage", "cover.png", "image/png", byteArrayOf(1))
|
||||||
|
Mockito.doAnswer {
|
||||||
|
assertFalse(TransactionSynchronizationManager.isActualTransactionActive())
|
||||||
|
"saved/cover.png"
|
||||||
|
}.`when`(uploader).upload(
|
||||||
|
Mockito.any(InputStream::class.java) ?: ByteArrayInputStream(byteArrayOf()),
|
||||||
|
Mockito.anyString(),
|
||||||
|
Mockito.anyString(),
|
||||||
|
Mockito.any(ObjectMetadata::class.java)
|
||||||
|
)
|
||||||
|
|
||||||
|
when (entry) {
|
||||||
|
"owner" -> ownerService.modifyAudioContent(
|
||||||
|
image,
|
||||||
|
"""{"contentId":$id,"title":"수정 제목","isAdult":false,"isCommentAvailable":true}""",
|
||||||
|
creator
|
||||||
|
)
|
||||||
|
"admin" -> adminService.updateAudioContent(image, """{"id":$id,"title":"수정 제목","isDefaultCoverImage":false}""")
|
||||||
|
"creator" -> creatorService.updateAudioContent(image, """{"id":$id,"title":"수정 제목"}""", creator)
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals("수정 제목", payload(id)?.title)
|
||||||
|
assertEquals("Approved detail", payload(id)?.detail)
|
||||||
|
assertEquals("Approved tags", payload(id)?.tags)
|
||||||
|
val fields = transaction.execute { overrides.findByContentId(id).map { it.approval.field }.toSet() }
|
||||||
|
assertEquals(setOf(ContentSourceFieldKey.DETAIL, ContentSourceFieldKey.TAGS), fields)
|
||||||
|
assertEquals(setOf("title"), jobs.findAll().map { it.fieldKey }.toSet())
|
||||||
|
assertTrue(jobs.findAll().all { it.sourceText == "수정 제목" })
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@CsvSource("owner,detail", "owner,tags", "admin,detail", "creator,detail", "creator,tags")
|
||||||
|
@DisplayName("상세와 태그도 해당 필드만 무효화하고 pending 태그의 영속화 전후 순서를 유지한다")
|
||||||
|
fun shouldInvalidateChangedDetailOrTags(entry: String, field: String) {
|
||||||
|
val id = fixture()
|
||||||
|
approveAll(id)
|
||||||
|
val creator = transaction.execute { entityManager.find(AudioContent::class.java, id).member }!!
|
||||||
|
val value = if (field == "tags") "#힐링 #새태그" else "수정 상세"
|
||||||
|
|
||||||
|
when (entry) {
|
||||||
|
"owner" -> ownerService.modifyAudioContent(
|
||||||
|
null,
|
||||||
|
"""{"contentId":$id,"$field":"$value","isAdult":false,"isCommentAvailable":true}""",
|
||||||
|
creator
|
||||||
|
)
|
||||||
|
"admin" -> adminService.updateAudioContent(null, """{"id":$id,"$field":"$value","isDefaultCoverImage":false}""")
|
||||||
|
"creator" -> creatorService.updateAudioContent(null, """{"id":$id,"$field":"$value"}""", creator)
|
||||||
|
}
|
||||||
|
|
||||||
|
val current = snapshot(id)
|
||||||
|
assertEquals("Approved title", payload(id)?.title)
|
||||||
|
assertEquals(if (field == "detail") "수정 상세" else "Approved detail", payload(id)?.detail)
|
||||||
|
assertEquals(if (field == "tags") "#힐링,#새태그" else "Approved tags", payload(id)?.tags)
|
||||||
|
assertEquals(setOf(field), jobs.findAll().map { it.fieldKey }.toSet())
|
||||||
|
assertEquals(
|
||||||
|
setOf(if (field == "tags") current.tags.joinToString(",") else current.detail),
|
||||||
|
jobs.findAll().map { it.sourceText }.toSet()
|
||||||
|
)
|
||||||
|
assertEquals(2L, overrideRepository.count())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("A에서 B로 변경한 뒤 A로 돌아와도 이전 승인은 복원하지 않고 완료 작업을 다시 연다")
|
||||||
|
fun shouldNotRestoreApprovalAfterSourceReturns() {
|
||||||
|
val id = fixture()
|
||||||
|
approveAll(id)
|
||||||
|
val old = snapshot(id)
|
||||||
|
val scheduler = TranslationJobScheduler(jobs, coordinator, translations, entityManager)
|
||||||
|
scheduler.scheduleMissingTranslation(LanguageTranslationTargetType.CONTENT, id, "title", old.title, "ko", "en")
|
||||||
|
jobs.saveAndFlush(jobs.findAll().single().apply { status = TranslationJobStatus.COMPLETED })
|
||||||
|
adminService.updateAudioContent(null, """{"id":$id,"title":"B","isDefaultCoverImage":false}""")
|
||||||
|
|
||||||
|
adminService.updateAudioContent(null, """{"id":$id,"title":"원문 제목","isDefaultCoverImage":false}""")
|
||||||
|
|
||||||
|
assertEquals("원문 제목", payload(id)?.title)
|
||||||
|
assertEquals("Approved detail", payload(id)?.detail)
|
||||||
|
assertEquals(2L, overrideRepository.count())
|
||||||
|
assertEquals(
|
||||||
|
TranslationJobStatus.PENDING,
|
||||||
|
jobs.findAll().single { it.sourceText == old.title && it.targetLanguage == "en" }.status
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("누락된 언어 감지는 트랜잭션 밖에서 실행하고 언어 변경은 세 승인을 모두 해제한다")
|
||||||
|
fun shouldInvalidateEveryFieldOnLanguageChange() {
|
||||||
|
val id = fixture(null)
|
||||||
|
approveAll(id)
|
||||||
|
provider.beforeDetect = { assertFalse(TransactionSynchronizationManager.isActualTransactionActive()) }
|
||||||
|
|
||||||
|
languageService.detectMissing(id)
|
||||||
|
|
||||||
|
assertEquals("ko", snapshot(id).sourceLanguage)
|
||||||
|
assertEquals(0L, overrideRepository.count())
|
||||||
|
assertEquals("원문 제목", payload(id)?.title)
|
||||||
|
assertEquals("원문 설명", payload(id)?.detail)
|
||||||
|
assertEquals("#힐링", payload(id)?.tags)
|
||||||
|
assertEquals(6L, jobs.count())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("이전 원문 이벤트는 현재 원문의 언어를 감지하거나 저장하지 않는다")
|
||||||
|
fun shouldIgnoreStaleDetectionEvent() {
|
||||||
|
val id = fixture(null)
|
||||||
|
|
||||||
|
detectorListener.detectLanguage(LanguageDetectEvent(id, "이전 원문"))
|
||||||
|
|
||||||
|
assertEquals(null, snapshot(id).sourceLanguage)
|
||||||
|
assertEquals(emptyList<String>(), provider.detectionQueries)
|
||||||
|
assertEquals(0L, jobs.count())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("감지 중 원문이 바뀌면 늦은 언어 결과를 저장하지 않는다")
|
||||||
|
fun shouldRejectDetectionAfterConcurrentSourceChange() {
|
||||||
|
val id = fixture(null)
|
||||||
|
provider.beforeDetect = {
|
||||||
|
transaction.executeWithoutResult { entityManager.find(AudioContent::class.java, id).title = "다른 제목" }
|
||||||
|
}
|
||||||
|
|
||||||
|
languageService.detectMissing(id)
|
||||||
|
|
||||||
|
assertEquals(null, snapshot(id).sourceLanguage)
|
||||||
|
assertEquals(0L, jobs.count())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("기존 payload shortcut도 오래된 승인 필드를 현재 원문 fallback으로 바꿔 반환한다")
|
||||||
|
fun shouldNotReturnStaleValueFromExistingPayloadShortcut() {
|
||||||
|
val id = fixture()
|
||||||
|
approveAll(id)
|
||||||
|
transaction.executeWithoutResult { entityManager.find(AudioContent::class.java, id).title = "우회 변경" }
|
||||||
|
|
||||||
|
val result = detailService.resolve(id, "en")
|
||||||
|
|
||||||
|
assertEquals("우회 변경", result.translated?.title)
|
||||||
|
assertEquals("Approved detail", result.translated?.detail)
|
||||||
|
assertEquals("Approved tags", result.translated?.tags)
|
||||||
|
assertEquals(2L, overrideRepository.count())
|
||||||
|
assertEquals(setOf("title"), jobs.findAll().map { it.fieldKey }.toSet())
|
||||||
|
assertEquals(0, provider.calls.get())
|
||||||
|
}
|
||||||
|
}
|
||||||
+131
@@ -0,0 +1,131 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContent
|
||||||
|
import kr.co.vividnext.sodalive.content.ContentDetailTranslationTestConfiguration
|
||||||
|
import kr.co.vividnext.sodalive.content.DetailTranslationProvider
|
||||||
|
import kr.co.vividnext.sodalive.content.hashtag.AudioContentHashTag
|
||||||
|
import kr.co.vividnext.sodalive.content.hashtag.HashTag
|
||||||
|
import kr.co.vividnext.sodalive.content.theme.AudioContentTheme
|
||||||
|
import kr.co.vividnext.sodalive.content.translation.ContentTranslationPayload
|
||||||
|
import kr.co.vividnext.sodalive.content.translation.ContentTranslationRepository
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.SourceTextNormalizer
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationCoordinator
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationJobRepository
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationMemory
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationMemoryRepository
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationReadModelMaterializer
|
||||||
|
import kr.co.vividnext.sodalive.member.Member
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence.ContentTranslationOverrideRepository
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ApprovalProvenance
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ApprovalProvider
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentFieldApproval
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceSnapshot
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.ContentTranslationOverrideStore
|
||||||
|
import org.junit.jupiter.api.BeforeEach
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest
|
||||||
|
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 javax.persistence.EntityManager
|
||||||
|
|
||||||
|
@SpringBootTest(
|
||||||
|
classes = [ContentDetailTranslationTestConfiguration::class],
|
||||||
|
properties = ["spring.cache.type=none", "spring.jpa.properties.hibernate.show_sql=false"]
|
||||||
|
)
|
||||||
|
@ActiveProfiles("content-detail-translation-test")
|
||||||
|
abstract class ContentTranslationTestSupport {
|
||||||
|
@Autowired protected lateinit var entityManager: EntityManager
|
||||||
|
|
||||||
|
@Autowired protected lateinit var materializer: TranslationReadModelMaterializer
|
||||||
|
|
||||||
|
@Autowired protected lateinit var writer: ContentTranslationWriteService
|
||||||
|
|
||||||
|
@Autowired protected lateinit var memories: TranslationMemoryRepository
|
||||||
|
|
||||||
|
@Autowired protected lateinit var translations: ContentTranslationRepository
|
||||||
|
|
||||||
|
@Autowired protected lateinit var overrides: ContentTranslationOverrideStore
|
||||||
|
|
||||||
|
@Autowired protected lateinit var overrideRepository: ContentTranslationOverrideRepository
|
||||||
|
|
||||||
|
@Autowired protected lateinit var coordinator: TranslationCoordinator
|
||||||
|
|
||||||
|
@Autowired protected lateinit var provider: DetailTranslationProvider
|
||||||
|
|
||||||
|
@Autowired protected lateinit var jobs: TranslationJobRepository
|
||||||
|
|
||||||
|
@Autowired protected lateinit var jdbc: JdbcTemplate
|
||||||
|
|
||||||
|
@Autowired private lateinit var transactionManager: PlatformTransactionManager
|
||||||
|
|
||||||
|
protected val transaction: TransactionTemplate
|
||||||
|
get() = TransactionTemplate(transactionManager).apply {
|
||||||
|
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW
|
||||||
|
}
|
||||||
|
protected val provenance = ApprovalProvenance(71L, listOf(81L, 82L), listOf(ApprovalProvider("aws", "test")), 91L)
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
fun prepareTranslationStorage() {
|
||||||
|
jobs.deleteAllInBatch()
|
||||||
|
overrideRepository.deleteAllInBatch()
|
||||||
|
translations.deleteAllInBatch()
|
||||||
|
memories.deleteAllInBatch()
|
||||||
|
jdbc.update("delete from language_detection_result")
|
||||||
|
provider.reset()
|
||||||
|
jdbc.execute("alter table content_translation alter column rendered_payload varchar(1000000)")
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun fixture(language: String? = "ko"): Long = transaction.execute {
|
||||||
|
val creator = Member(password = "test", nickname = "approval-creator")
|
||||||
|
val theme = AudioContentTheme("approval-theme", "theme.png")
|
||||||
|
entityManager.persist(creator)
|
||||||
|
entityManager.persist(theme)
|
||||||
|
val content = AudioContent("원문 제목", "원문 설명", language).apply {
|
||||||
|
member = creator
|
||||||
|
this.theme = theme
|
||||||
|
isActive = true
|
||||||
|
duration = "00:01:00"
|
||||||
|
}
|
||||||
|
entityManager.persist(content)
|
||||||
|
val tag = HashTag("#힐링")
|
||||||
|
entityManager.persist(tag)
|
||||||
|
entityManager.persist(AudioContentHashTag().apply { audioContent = content; hashTag = tag })
|
||||||
|
requireNotNull(content.id)
|
||||||
|
}!!
|
||||||
|
|
||||||
|
protected fun snapshot(id: Long): ContentSourceSnapshot = transaction.execute {
|
||||||
|
entityManager.find(AudioContent::class.java, id).toContentSourceSnapshot()
|
||||||
|
}!!
|
||||||
|
|
||||||
|
protected fun approval(source: ContentSourceSnapshot, field: ContentSourceFieldKey, text: String = "Approved ${field.key}") =
|
||||||
|
ContentFieldApproval(field, source.fingerprint(field), text, provenance)
|
||||||
|
|
||||||
|
protected fun approveAll(id: Long) {
|
||||||
|
val source = snapshot(id)
|
||||||
|
writer.approve(id, "en", ContentSourceFieldKey.values().map { approval(source, it) })
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun cache(source: ContentSourceSnapshot) {
|
||||||
|
source.fields.filter { it.sourceText.isNotBlank() }.forEach {
|
||||||
|
memories.saveAndFlush(
|
||||||
|
TranslationMemory(
|
||||||
|
SourceTextNormalizer.hash(it.sourceText),
|
||||||
|
it.sourceText,
|
||||||
|
requireNotNull(source.sourceLanguage),
|
||||||
|
"en",
|
||||||
|
"Automatic ${it.fieldKey}",
|
||||||
|
"aws",
|
||||||
|
"test"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun payload(id: Long): ContentTranslationPayload? = transaction.execute {
|
||||||
|
translations.findByContentIdAndLocale(id, "en")?.renderedPayload
|
||||||
|
}
|
||||||
|
}
|
||||||
+84
@@ -0,0 +1,84 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.LanguageProviderId
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslateRequest
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslateResult
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationProvider
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidate
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationCandidateStore
|
||||||
|
import org.junit.jupiter.api.BeforeEach
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import org.springframework.context.annotation.Bean
|
||||||
|
import org.springframework.context.annotation.Configuration
|
||||||
|
import org.springframework.context.annotation.Import
|
||||||
|
import org.springframework.transaction.PlatformTransactionManager
|
||||||
|
import org.springframework.transaction.support.TransactionSynchronizationManager
|
||||||
|
import java.util.Collections
|
||||||
|
|
||||||
|
@Import(SelectionCandidateProviderConfiguration::class)
|
||||||
|
abstract class TranslationCandidateTestSupport : TranslationSelectionTestSupport() {
|
||||||
|
@Autowired protected lateinit var generation: TranslationSelectionGenerationService
|
||||||
|
|
||||||
|
@Autowired protected lateinit var candidateWorker: TranslationSelectionWorkerService
|
||||||
|
|
||||||
|
@Autowired protected lateinit var candidateStore: TranslationCandidateStore
|
||||||
|
|
||||||
|
@Autowired protected lateinit var papago: SelectionPapagoProvider
|
||||||
|
|
||||||
|
@Autowired protected lateinit var selectionTransactionManager: PlatformTransactionManager
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
fun resetCandidateProvider() { papago.reset() }
|
||||||
|
|
||||||
|
protected fun generated(): Long {
|
||||||
|
val created = request(fixture())
|
||||||
|
preparation.prepareNext()
|
||||||
|
generation.generate(created.requestId, 1)
|
||||||
|
return created.requestId
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun candidates(requestId: Long): List<TranslationCandidate> = transaction.execute {
|
||||||
|
candidateStore.page(requestId, 0, 1000)
|
||||||
|
}!!
|
||||||
|
|
||||||
|
protected fun drain(requestId: Long) {
|
||||||
|
repeat(requests.get(requestId).progress.total + 1) { candidateWorker.runNext() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
class SelectionCandidateProviderConfiguration {
|
||||||
|
@Bean fun selectionPapagoProvider() = SelectionPapagoProvider()
|
||||||
|
}
|
||||||
|
|
||||||
|
class SelectionPapagoProvider : TranslationProvider {
|
||||||
|
override val providerId = LanguageProviderId.PAPAGO
|
||||||
|
override var providerVersion = "nmt-test"
|
||||||
|
val requests: MutableList<TranslateRequest> = Collections.synchronizedList(mutableListOf())
|
||||||
|
var beforeTranslate: () -> Unit = {}
|
||||||
|
var result: (TranslateRequest) -> List<String> = ::outputs
|
||||||
|
|
||||||
|
fun reset() {
|
||||||
|
requests.clear()
|
||||||
|
providerVersion = "nmt-test"
|
||||||
|
beforeTranslate = {}
|
||||||
|
result = ::outputs
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun translate(request: TranslateRequest): TranslateResult {
|
||||||
|
check(!TransactionSynchronizationManager.isActualTransactionActive()) { "network inside transaction" }
|
||||||
|
request.callControl?.beforeCall()
|
||||||
|
requests.add(request)
|
||||||
|
beforeTranslate()
|
||||||
|
return TranslateResult(result(request))
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun outputs(request: TranslateRequest): List<String> = request.texts.map {
|
||||||
|
when (it) {
|
||||||
|
"원문 제목" -> "English title"
|
||||||
|
"원문 설명" -> "English detail"
|
||||||
|
"#힐링" -> "#Healing"
|
||||||
|
else -> "result:$it"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+203
@@ -0,0 +1,203 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.common.SodaException
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContent
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateStatus
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Assertions.assertThrows
|
||||||
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
|
import org.junit.jupiter.api.DisplayName
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource
|
||||||
|
import java.util.concurrent.Callable
|
||||||
|
import java.util.concurrent.CountDownLatch
|
||||||
|
import java.util.concurrent.Executors
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
import javax.persistence.LockModeType
|
||||||
|
|
||||||
|
class TranslationSelectionApplyConcurrencyIntegrationTest : TranslationSelectionApplyTestSupport() {
|
||||||
|
@Test
|
||||||
|
@DisplayName("별도 요청의 후속 관리자 적용이 이기고 경합한 일반 materializer는 유효 승인을 덮어쓰지 않는다")
|
||||||
|
fun shouldSerializeExplicitApplicationsAndAutomaticWriter() {
|
||||||
|
val id = fixture()
|
||||||
|
val first = ready(id)
|
||||||
|
papago.result = { request -> request.texts.map { "Later:$it" } }
|
||||||
|
val second = ready(id)
|
||||||
|
val firstBody = body(first, listOf(field(first, ContentSourceFieldKey.TITLE)))
|
||||||
|
val secondBody = body(second, listOf(field(second, ContentSourceFieldKey.TITLE)))
|
||||||
|
cache(snapshot(id))
|
||||||
|
val locked = CountDownLatch(1)
|
||||||
|
val release = CountDownLatch(1)
|
||||||
|
val started = CountDownLatch(2)
|
||||||
|
val executor = Executors.newFixedThreadPool(3)
|
||||||
|
try {
|
||||||
|
val firstApply = executor.submit(
|
||||||
|
Callable {
|
||||||
|
transaction.execute {
|
||||||
|
val receipt = applyService.apply(first.requestId, 777, firstBody)
|
||||||
|
locked.countDown()
|
||||||
|
check(release.await(10, TimeUnit.SECONDS))
|
||||||
|
receipt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
assertTrue(locked.await(10, TimeUnit.SECONDS))
|
||||||
|
val secondApply = executor.submit(
|
||||||
|
Callable {
|
||||||
|
started.countDown()
|
||||||
|
applyService.apply(second.requestId, 888, secondBody)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
val automatic = executor.submit(
|
||||||
|
Callable {
|
||||||
|
started.countDown()
|
||||||
|
materializer.materialize(LanguageTranslationTargetType.CONTENT, id, "en")
|
||||||
|
}
|
||||||
|
)
|
||||||
|
assertTrue(started.await(10, TimeUnit.SECONDS))
|
||||||
|
release.countDown()
|
||||||
|
assertEquals("English title", firstApply.get(15, TimeUnit.SECONDS)!!.appliedFields.single().translatedValue)
|
||||||
|
assertEquals("Later:원문 제목", secondApply.get(15, TimeUnit.SECONDS).appliedFields.single().translatedValue)
|
||||||
|
assertTrue(automatic.get(15, TimeUnit.SECONDS))
|
||||||
|
} finally {
|
||||||
|
release.countDown()
|
||||||
|
executor.shutdownNow()
|
||||||
|
}
|
||||||
|
assertEquals("Later:원문 제목", payload(id)?.title)
|
||||||
|
assertEquals(888L, transaction.execute { overrides.findByContentId(id).single().approval.provenance.approvedBy })
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(booleans = [true, false])
|
||||||
|
@DisplayName("apply와 cancel은 같은 request 잠금에서 먼저 commit한 terminal 상태만 보존한다")
|
||||||
|
fun shouldLinearizeCancelAndApply(applyFirst: Boolean) {
|
||||||
|
val id = fixture()
|
||||||
|
val request = ready(id)
|
||||||
|
val body = body(request, listOf(field(request, ContentSourceFieldKey.TITLE)))
|
||||||
|
val locked = CountDownLatch(1)
|
||||||
|
val release = CountDownLatch(1)
|
||||||
|
val started = CountDownLatch(1)
|
||||||
|
val executor = Executors.newFixedThreadPool(2)
|
||||||
|
try {
|
||||||
|
val winner = executor.submit {
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
if (applyFirst) applyService.apply(request.requestId, 777, body) else requests.cancel(request.requestId)
|
||||||
|
locked.countDown()
|
||||||
|
check(release.await(10, TimeUnit.SECONDS))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertTrue(locked.await(10, TimeUnit.SECONDS))
|
||||||
|
val loser = executor.submit(
|
||||||
|
Callable {
|
||||||
|
started.countDown()
|
||||||
|
assertThrows(SodaException::class.java) {
|
||||||
|
if (applyFirst) requests.cancel(request.requestId) else applyService.apply(request.requestId, 777, body)
|
||||||
|
}.errorProperty
|
||||||
|
}
|
||||||
|
)
|
||||||
|
assertTrue(started.await(10, TimeUnit.SECONDS))
|
||||||
|
release.countDown()
|
||||||
|
winner.get(15, TimeUnit.SECONDS)
|
||||||
|
assertEquals("REQUEST_CLOSED", loser.get(15, TimeUnit.SECONDS))
|
||||||
|
} finally {
|
||||||
|
release.countDown()
|
||||||
|
executor.shutdownNow()
|
||||||
|
}
|
||||||
|
assertEquals(
|
||||||
|
if (applyFirst) TranslationSelectionStatus.APPLIED else TranslationSelectionStatus.CANCELLED,
|
||||||
|
requests.get(request.requestId).status
|
||||||
|
)
|
||||||
|
assertEquals(if (applyFirst) "English title" else null, payload(id)?.title)
|
||||||
|
assertEquals(if (applyFirst) 1L else 0L, overrideRepository.count())
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = ["title", "tags"])
|
||||||
|
@DisplayName("잠금 전에 로드된 원문과 태그 연관은 current refresh 뒤에만 선택을 판정한다")
|
||||||
|
fun shouldRefreshManagedSourceAssociationsBeforeValidation(kind: String) {
|
||||||
|
val id = fixture()
|
||||||
|
val request = ready(id)
|
||||||
|
val choice = field(request, if (kind == "tags") ContentSourceFieldKey.TAGS else ContentSourceFieldKey.TITLE)
|
||||||
|
val error = assertThrows(SodaException::class.java) {
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
val managed = entityManager.find(AudioContent::class.java, id)
|
||||||
|
managed.toContentSourceSnapshot()
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
val current = writer.lockContent(id)!!
|
||||||
|
if (kind == "title") {
|
||||||
|
current.title += " "
|
||||||
|
} else {
|
||||||
|
current.audioContentHashTags.single().isActive = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
applyService.apply(request.requestId, 777, body(request, listOf(choice)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertEquals("SOURCE_CHANGED", error.errorProperty)
|
||||||
|
assertEquals(0L, translations.count())
|
||||||
|
assertEquals(0L, overrideRepository.count())
|
||||||
|
assertEquals(TranslationSelectionStatus.READY, requests.get(request.requestId).status)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("원본 수정이 먼저 잠금을 보유하면 대기하던 적용은 commit된 최신 원문을 거부한다")
|
||||||
|
fun shouldReadSourceAfterWaitingForMutationCommit() {
|
||||||
|
val id = fixture()
|
||||||
|
val request = ready(id)
|
||||||
|
val body = body(request, listOf(field(request, ContentSourceFieldKey.TITLE)))
|
||||||
|
val locked = CountDownLatch(1)
|
||||||
|
val release = CountDownLatch(1)
|
||||||
|
val started = CountDownLatch(1)
|
||||||
|
val executor = Executors.newFixedThreadPool(2)
|
||||||
|
try {
|
||||||
|
val mutation = executor.submit {
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
writer.lockContent(id)!!.title += " "
|
||||||
|
locked.countDown()
|
||||||
|
check(release.await(10, TimeUnit.SECONDS))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertTrue(locked.await(10, TimeUnit.SECONDS))
|
||||||
|
val applying = executor.submit(
|
||||||
|
Callable {
|
||||||
|
started.countDown()
|
||||||
|
assertThrows(SodaException::class.java) { applyService.apply(request.requestId, 777, body) }.errorProperty
|
||||||
|
}
|
||||||
|
)
|
||||||
|
assertTrue(started.await(10, TimeUnit.SECONDS))
|
||||||
|
release.countDown()
|
||||||
|
mutation.get(15, TimeUnit.SECONDS)
|
||||||
|
assertEquals("SOURCE_CHANGED", applying.get(15, TimeUnit.SECONDS))
|
||||||
|
} finally {
|
||||||
|
release.countDown()
|
||||||
|
executor.shutdownNow()
|
||||||
|
}
|
||||||
|
assertEquals(0L, overrideRepository.count())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("외부 transaction rollback은 payload override 영수증 discard 상태를 함께 되돌린다")
|
||||||
|
fun shouldCommitReceiptDiscardAndPayloadInOneTransaction() {
|
||||||
|
val id = fixture()
|
||||||
|
val request = ready(id)
|
||||||
|
assertThrows(IllegalStateException::class.java) {
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
applyService.apply(request.requestId, 777, body(request, listOf(field(request, ContentSourceFieldKey.TITLE))))
|
||||||
|
assertEquals(
|
||||||
|
LockModeType.PESSIMISTIC_WRITE,
|
||||||
|
entityManager.getLockMode(entityManager.find(AudioContent::class.java, id))
|
||||||
|
)
|
||||||
|
throw IllegalStateException("rollback after all application writes")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertEquals(TranslationSelectionStatus.READY, requests.get(request.requestId).status)
|
||||||
|
assertEquals(null, requests.get(request.requestId).applyReceipt)
|
||||||
|
assertEquals(0L, translations.count())
|
||||||
|
assertEquals(0L, overrideRepository.count())
|
||||||
|
assertEquals(setOf(TranslationCandidateStatus.SUCCEEDED), candidates(request.requestId).map { it.status }.toSet())
|
||||||
|
}
|
||||||
|
}
|
||||||
+169
@@ -0,0 +1,169 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContent
|
||||||
|
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.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateStatus
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionChoice
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionField
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Assertions.assertNotNull
|
||||||
|
import org.junit.jupiter.api.DisplayName
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource
|
||||||
|
|
||||||
|
class TranslationSelectionApplyGroupIntegrationTest : TranslationSelectionApplyTestSupport() {
|
||||||
|
@Test
|
||||||
|
@DisplayName("공통 bundle의 선언 순서와 template 순서가 달라도 전체 제목과 literal 출처를 정확히 적용한다")
|
||||||
|
fun shouldApplyCompleteGroupAndReplayResidualOrdering() {
|
||||||
|
val fixture = readyGroup()
|
||||||
|
val choice = groupChoice(fixture)
|
||||||
|
val body = body(fixture.request, listOf(choice))
|
||||||
|
|
||||||
|
val receipt = applyService.apply(fixture.request.requestId, 777, body)
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
listOf("result:공통 result:잔여 구문 result:끝 1", "result:공통 result:잔여 구문 result:끝 2", "result:공통 result:끝 3"),
|
||||||
|
fixture.ids.take(3).map { payload(it)?.title }
|
||||||
|
)
|
||||||
|
assertEquals(null, payload(fixture.ids.last()))
|
||||||
|
assertEquals(3, receipt.appliedFields.size)
|
||||||
|
assertEquals(
|
||||||
|
listOf(choice.commonCandidateId),
|
||||||
|
receipt.appliedFields.single { it.contentId == fixture.ids[2] }.candidateIds
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
listOf(choice.commonCandidateId, choice.residualChoices[1].candidateId),
|
||||||
|
receipt.appliedFields.single { it.contentId == fixture.ids[1] }.candidateIds
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
receipt,
|
||||||
|
applyService.apply(
|
||||||
|
fixture.request.requestId,
|
||||||
|
777,
|
||||||
|
body.copy(selections = listOf(choice.copy(residualChoices = choice.residualChoices.reversed())))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assertEquals(3L, overrideRepository.count())
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(
|
||||||
|
strings = ["missing", "duplicate", "nullResidual", "literalCandidate", "wrongMember", "wrongCommon", "fieldTitle"]
|
||||||
|
)
|
||||||
|
@DisplayName("누락 중복 literal 및 residual 소유권 불일치와 grouped title FIELD 우회를 원자 거부한다")
|
||||||
|
fun shouldRejectIncompleteOrConflictingGroupWithoutWrites(kind: String) {
|
||||||
|
val fixture = readyGroup()
|
||||||
|
val choice = groupChoice(fixture)
|
||||||
|
val invalid = when (kind) {
|
||||||
|
"missing" -> choice.copy(residualChoices = choice.residualChoices.dropLast(1))
|
||||||
|
"duplicate" -> choice.copy(residualChoices = choice.residualChoices + choice.residualChoices.first())
|
||||||
|
"nullResidual" -> choice.copy(residualChoices = choice.residualChoices.map { it.copy(candidateId = null) })
|
||||||
|
"literalCandidate" -> choice.copy(
|
||||||
|
residualChoices = choice.residualChoices.map {
|
||||||
|
if (it.contentId == fixture.ids[2]) it.copy(candidateId = choice.residualChoices.first().candidateId) else it
|
||||||
|
}
|
||||||
|
)
|
||||||
|
"wrongMember" -> choice.copy(
|
||||||
|
residualChoices = choice.residualChoices.map {
|
||||||
|
if (it.contentId == fixture.ids[1]) it.copy(candidateId = choice.residualChoices.first().candidateId) else it
|
||||||
|
}
|
||||||
|
)
|
||||||
|
"wrongCommon" -> choice.copy(commonCandidateId = requireNotNull(choice.residualChoices.first().candidateId))
|
||||||
|
else -> choice
|
||||||
|
}
|
||||||
|
val choices = if (kind == "fieldTitle") {
|
||||||
|
listOf(
|
||||||
|
choice,
|
||||||
|
TranslationSelectionChoice.Field(
|
||||||
|
fixture.ids.first(),
|
||||||
|
TranslationSelectionField.TITLE,
|
||||||
|
"en",
|
||||||
|
choice.commonCandidateId
|
||||||
|
)
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
listOf(invalid)
|
||||||
|
}
|
||||||
|
val code = when (kind) {
|
||||||
|
"wrongMember", "wrongCommon" -> "CANDIDATE_NOT_SELECTABLE"
|
||||||
|
"fieldTitle" -> "INVALID_REQUEST"
|
||||||
|
else -> "INCOMPLETE_TITLE_GROUP"
|
||||||
|
}
|
||||||
|
|
||||||
|
rejected(fixture.request, body(fixture.request, choices), code)
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = ["title", "language", "active", "removed", "added"])
|
||||||
|
@DisplayName("잠금 후 모든 그룹 제목과 언어 및 전체 활성 membership을 검증한다")
|
||||||
|
fun shouldRejectChangedSelectedGroupSourceOrMembership(kind: String) {
|
||||||
|
val fixture = readyGroup()
|
||||||
|
val additional = if (kind == "added") this.fixture() else null
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
when (kind) {
|
||||||
|
"title" -> entityManager.find(AudioContent::class.java, fixture.ids[1]).title += " "
|
||||||
|
"language" -> entityManager.find(AudioContent::class.java, fixture.ids[1]).languageCode = "en"
|
||||||
|
"active" -> entityManager.find(AudioContent::class.java, fixture.ids.last()).isActive = false
|
||||||
|
"removed" -> entityManager.createQuery(
|
||||||
|
"delete from SeriesContent s where s.series.id = :id and s.content.id = :content"
|
||||||
|
)
|
||||||
|
.setParameter("id", fixture.seriesId).setParameter("content", fixture.ids.last()).executeUpdate()
|
||||||
|
else -> entityManager.persist(
|
||||||
|
SeriesContent().apply {
|
||||||
|
series = entityManager.find(Series::class.java, fixture.seriesId)
|
||||||
|
content = entityManager.find(AudioContent::class.java, additional)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rejected(
|
||||||
|
fixture.request,
|
||||||
|
body(fixture.request, listOf(groupChoice(fixture))),
|
||||||
|
if (kind in listOf("title", "language")) "SOURCE_CHANGED" else "MEMBERSHIP_CHANGED"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("FIELD 선택은 무관한 member 추가와 다른 필드 변경을 허용하고 선택된 alias의 복사 결과를 유지한다")
|
||||||
|
fun shouldAllowUnrelatedMembershipAndKeepSelectedLinkedCandidate() {
|
||||||
|
val fixture = readyGroup()
|
||||||
|
val choice = field(fixture.request, ContentSourceFieldKey.DETAIL, fixture.ids[1])
|
||||||
|
val alias = candidates(fixture.request.requestId).single { it.candidateId == choice.candidateId }
|
||||||
|
assertNotNull(alias.linkedCandidateId)
|
||||||
|
val added = this.fixture()
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
entityManager.persist(
|
||||||
|
SeriesContent().apply {
|
||||||
|
series = entityManager.find(Series::class.java, fixture.seriesId)
|
||||||
|
content = entityManager.find(AudioContent::class.java, added)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
entityManager.find(AudioContent::class.java, fixture.ids[1]).title += " "
|
||||||
|
}
|
||||||
|
|
||||||
|
val receipt = applyService.apply(fixture.request.requestId, 777, body(fixture.request, listOf(choice)))
|
||||||
|
|
||||||
|
assertEquals("English detail", payload(fixture.ids[1])?.detail)
|
||||||
|
assertEquals(listOf(choice.candidateId), receipt.appliedFields.single().candidateIds)
|
||||||
|
val persisted = candidates(fixture.request.requestId)
|
||||||
|
assertEquals(TranslationCandidateStatus.DISCARDED, persisted.single { it.candidateId == alias.linkedCandidateId }.status)
|
||||||
|
assertEquals(listOf("English detail"), persisted.single { it.candidateId == choice.candidateId }.translatedValues)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("SERIES FIELD의 선택 콘텐츠가 비활성이면 다른 source가 같아도 거부한다")
|
||||||
|
fun shouldRejectIneligibleSelectedSeriesField() {
|
||||||
|
val fixture = readyGroup()
|
||||||
|
transaction.executeWithoutResult { entityManager.find(AudioContent::class.java, fixture.ids.first()).isActive = false }
|
||||||
|
|
||||||
|
rejected(
|
||||||
|
fixture.request,
|
||||||
|
body(fixture.request, listOf(field(fixture.request, ContentSourceFieldKey.DETAIL))),
|
||||||
|
"SOURCE_CHANGED"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
+194
@@ -0,0 +1,194 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.common.SodaException
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContent
|
||||||
|
import kr.co.vividnext.sodalive.content.translation.ContentTranslation
|
||||||
|
import kr.co.vividnext.sodalive.content.translation.ContentTranslationPayload
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence.TranslationGenerationUnitConverter
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateStatus
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Assertions.assertThrows
|
||||||
|
import org.junit.jupiter.api.DisplayName
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
class TranslationSelectionApplyIntegrationTest : TranslationSelectionApplyTestSupport() {
|
||||||
|
@Test
|
||||||
|
@DisplayName("한 번의 subset은 같은 locale 필드를 함께 합치고 미선택 바이트와 provider manifest를 보존한다")
|
||||||
|
fun shouldApplySubsetOnceAndDiscardOnlyOmittedCandidates() {
|
||||||
|
val id = fixture()
|
||||||
|
val request = ready(id)
|
||||||
|
translations.saveAndFlush(ContentTranslation(id, "en", ContentTranslationPayload("Old", "Old detail", " \t#A, #B\n")))
|
||||||
|
val title = field(request, ContentSourceFieldKey.TITLE).copy(
|
||||||
|
candidateId = candidates(request.requestId).single {
|
||||||
|
it.unit.field == ContentSourceFieldKey.TITLE && it.provider.id == "aws" && it.targetLanguage == "en"
|
||||||
|
}.candidateId
|
||||||
|
)
|
||||||
|
val detail = field(request, ContentSourceFieldKey.DETAIL)
|
||||||
|
val body = body(request, listOf(detail, title))
|
||||||
|
papago.providerVersion = "changed-after-generation"
|
||||||
|
|
||||||
|
val receipt = applyService.apply(request.requestId, 777, body)
|
||||||
|
|
||||||
|
assertEquals(ContentTranslationPayload("English title", "English detail", " \t#A, #B\n"), payload(id))
|
||||||
|
val persisted = requests.get(request.requestId)
|
||||||
|
assertEquals(TranslationSelectionStatus.APPLIED, persisted.status)
|
||||||
|
assertEquals(request.snapshot, persisted.snapshot)
|
||||||
|
assertEquals(receipt, persisted.applyReceipt)
|
||||||
|
assertEquals(10, receipt.discardedCandidateCount)
|
||||||
|
assertEquals(
|
||||||
|
setOf(title.candidateId, detail.candidateId),
|
||||||
|
candidates(request.requestId).filter { it.status == TranslationCandidateStatus.SUCCEEDED }
|
||||||
|
.map { it.candidateId }.toSet()
|
||||||
|
)
|
||||||
|
val approved = transaction.execute { overrides.findByContentId(id) }!!
|
||||||
|
assertEquals(2, approved.size)
|
||||||
|
assertEquals(setOf(777L), approved.map { it.approval.provenance.approvedBy }.toSet())
|
||||||
|
assertEquals(
|
||||||
|
request.snapshot.providerManifest.toSet(),
|
||||||
|
approved.flatMap { it.approval.provenance.providers }.toSet()
|
||||||
|
)
|
||||||
|
assertEquals(0L, memories.count())
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = ["foreign", "failed", "pending", "provider", "unit", "target", "source", "values"])
|
||||||
|
@DisplayName("성공 여부 요청 unit 언어 frozen provider 원문 소유권이 다른 후보를 모두 거부한다")
|
||||||
|
fun shouldRejectUnselectableCandidateWithoutAnyWrites(kind: String) {
|
||||||
|
val id = fixture()
|
||||||
|
val request = ready(id)
|
||||||
|
val choice = field(request, ContentSourceFieldKey.TITLE)
|
||||||
|
val candidate = candidates(request.requestId).single { it.candidateId == choice.candidateId }
|
||||||
|
val bad = when (kind) {
|
||||||
|
"foreign" -> field(ready(fixture()), ContentSourceFieldKey.TITLE).candidateId
|
||||||
|
"failed" -> { replaceCandidate(candidate.copy(status = TranslationCandidateStatus.FAILED)); candidate.candidateId }
|
||||||
|
"pending" -> { replaceCandidate(candidate.copy(status = TranslationCandidateStatus.PENDING)); candidate.candidateId }
|
||||||
|
"values" -> { replaceCandidate(candidate.copy(translatedValues = listOf(" "))); candidate.candidateId }
|
||||||
|
"provider" -> {
|
||||||
|
jdbc.update("update translation_candidate set provider_version = ? where id = ?", "forged", candidate.candidateId)
|
||||||
|
candidate.candidateId
|
||||||
|
}
|
||||||
|
"target" -> candidates(request.requestId).single {
|
||||||
|
it.unit == candidate.unit && it.provider == candidate.provider && it.targetLanguage == "ja"
|
||||||
|
}.candidateId
|
||||||
|
else -> {
|
||||||
|
val unit = if (kind == "unit") {
|
||||||
|
candidate.unit.copy(contentId = id + 999)
|
||||||
|
} else {
|
||||||
|
candidate.unit.copy(inputValues = listOf("forged"))
|
||||||
|
}
|
||||||
|
jdbc.update(
|
||||||
|
"update translation_candidate set unit = ? where id = ?",
|
||||||
|
TranslationGenerationUnitConverter().convertToDatabaseColumn(unit),
|
||||||
|
candidate.candidateId
|
||||||
|
)
|
||||||
|
candidate.candidateId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rejected(request, body(request, listOf(choice.copy(candidateId = bad))), "CANDIDATE_NOT_SELECTABLE")
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = ["raw", "language", "deleted"])
|
||||||
|
@DisplayName("정규화하면 같아지는 raw 수정과 원본 언어 변경 및 삭제도 선택 필드의 이전 후보를 거부한다")
|
||||||
|
fun shouldRejectChangedSelectedSource(kind: String) {
|
||||||
|
val id = fixture()
|
||||||
|
val request = ready(id)
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
val content = entityManager.find(AudioContent::class.java, id)
|
||||||
|
when (kind) {
|
||||||
|
"raw" -> content.title += " "
|
||||||
|
"language" -> content.languageCode = "ja"
|
||||||
|
else -> {
|
||||||
|
entityManager.createQuery("delete from AudioContentHashTag t where t.audioContent.id = :id")
|
||||||
|
.setParameter("id", id).executeUpdate()
|
||||||
|
entityManager.remove(content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rejected(request, body(request, listOf(field(request, ContentSourceFieldKey.TITLE))), "SOURCE_CHANGED")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("미선택 필드 원문 변경은 허용하고 없는 locale의 나머지 필드는 최신 원문으로 초기화한다")
|
||||||
|
fun shouldAllowUnselectedFieldSourceChange() {
|
||||||
|
val id = fixture()
|
||||||
|
val request = ready(id)
|
||||||
|
transaction.executeWithoutResult { entityManager.find(AudioContent::class.java, id).detail = "현재 상세 " }
|
||||||
|
|
||||||
|
applyService.apply(request.requestId, 777, body(request, listOf(field(request, ContentSourceFieldKey.TITLE))))
|
||||||
|
|
||||||
|
assertEquals(ContentTranslationPayload("English title", "현재 상세 ", "#힐링"), payload(id))
|
||||||
|
assertEquals(1L, overrideRepository.count())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("같은 operation의 순서만 다른 body는 원본 변경 후에도 저장된 영수증을 반환한다")
|
||||||
|
fun shouldReplayCanonicalBodyBeforeClosedAndRevisionValidation() {
|
||||||
|
val id = fixture()
|
||||||
|
val request = ready(id)
|
||||||
|
val body = body(request, listOf(field(request, ContentSourceFieldKey.TITLE), field(request, ContentSourceFieldKey.TAGS)))
|
||||||
|
val receipt = applyService.apply(request.requestId, 777, body)
|
||||||
|
transaction.executeWithoutResult { entityManager.find(AudioContent::class.java, id).title = "변경 이후" }
|
||||||
|
|
||||||
|
assertEquals(receipt, applyService.apply(request.requestId, 888, body.copy(selections = body.selections.reversed())))
|
||||||
|
|
||||||
|
val changed = assertThrows(SodaException::class.java) {
|
||||||
|
applyService.apply(request.requestId, 777, body.copy(selections = body.selections.take(1)))
|
||||||
|
}
|
||||||
|
assertEquals("IDEMPOTENCY_CONFLICT", changed.errorProperty)
|
||||||
|
val closed = assertThrows(SodaException::class.java) {
|
||||||
|
applyService.apply(request.requestId, 777, body.copy(operationId = UUID.randomUUID()))
|
||||||
|
}
|
||||||
|
assertEquals("REQUEST_CLOSED", closed.errorProperty)
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = ["empty", "duplicate", "revision", "generating", "cancelled"])
|
||||||
|
@DisplayName("빈 선택 중복 stale revision과 미완료 취소 요청은 적용할 수 없다")
|
||||||
|
fun shouldValidateNewApplicationStateAndChoices(kind: String) {
|
||||||
|
val request = ready(fixture())
|
||||||
|
val title = field(request, ContentSourceFieldKey.TITLE)
|
||||||
|
val choices = when (kind) {
|
||||||
|
"empty" -> emptyList()
|
||||||
|
"duplicate" -> listOf(title, title)
|
||||||
|
else -> listOf(title)
|
||||||
|
}
|
||||||
|
val body = body(request, choices)
|
||||||
|
if (kind == "cancelled") requests.cancel(request.requestId)
|
||||||
|
if (kind == "generating") {
|
||||||
|
transaction.executeWithoutResult { requestStore.save(request.copy(status = TranslationSelectionStatus.GENERATING)) }
|
||||||
|
}
|
||||||
|
val error = assertThrows(SodaException::class.java) {
|
||||||
|
applyService.apply(request.requestId, 777, if (kind == "revision") body.copy(expectedRevision = 1) else body)
|
||||||
|
}
|
||||||
|
assertEquals(
|
||||||
|
when (kind) {
|
||||||
|
"revision" -> "REVISION_CONFLICT"
|
||||||
|
"generating" -> "REQUEST_NOT_READY"
|
||||||
|
"cancelled" -> "REQUEST_CLOSED"
|
||||||
|
else -> "INVALID_REQUEST"
|
||||||
|
},
|
||||||
|
error.errorProperty
|
||||||
|
)
|
||||||
|
assertEquals(0L, overrideRepository.count())
|
||||||
|
assertEquals(0L, translations.count())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("여러 선택 중 하나가 stale이면 앞선 유효 선택과 영수증 폐기까지 전부 쓰지 않는다")
|
||||||
|
fun shouldRollbackWholeRequestWhenAnySelectionIsInvalid() {
|
||||||
|
val id = fixture()
|
||||||
|
val request = ready(id)
|
||||||
|
transaction.executeWithoutResult { entityManager.find(AudioContent::class.java, id).detail += " " }
|
||||||
|
val choices = listOf(field(request, ContentSourceFieldKey.TITLE), field(request, ContentSourceFieldKey.DETAIL))
|
||||||
|
|
||||||
|
rejected(request, body(request, choices), "SOURCE_CHANGED")
|
||||||
|
|
||||||
|
assertEquals(setOf(TranslationCandidateStatus.SUCCEEDED), candidates(request.requestId).map { it.status }.toSet())
|
||||||
|
}
|
||||||
|
}
|
||||||
+126
@@ -0,0 +1,126 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.common.SodaException
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContent
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ReviewedSharedPart
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ReviewedTitleGroup
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TitleOccurrence
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidate
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionRequest
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionScope
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationUnitKind
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.ApplyTranslationSelectionRequest
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.ReviewTranslationSelectionGroupingRequest
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionChoice
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionField
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.TranslationSelectionResidualChoice
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Assertions.assertThrows
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
abstract class TranslationSelectionApplyTestSupport : TranslationCandidateTestSupport() {
|
||||||
|
@Autowired protected lateinit var applyService: TranslationSelectionApplyService
|
||||||
|
|
||||||
|
@Autowired protected lateinit var grouping: TranslationSelectionGroupingService
|
||||||
|
|
||||||
|
protected fun ready(id: Long): TranslationSelectionRequest {
|
||||||
|
val created = request(id)
|
||||||
|
preparation.prepareNext()
|
||||||
|
generation.generate(created.requestId, 1)
|
||||||
|
drain(created.requestId)
|
||||||
|
return requests.get(created.requestId)
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun field(
|
||||||
|
request: TranslationSelectionRequest,
|
||||||
|
field: ContentSourceFieldKey,
|
||||||
|
contentId: Long = request.snapshot.contents.first().contentId
|
||||||
|
): TranslationSelectionChoice.Field {
|
||||||
|
val candidate = candidates(request.requestId).single {
|
||||||
|
it.unit.kind == TranslationUnitKind.FIELD && it.unit.field == field && it.unit.contentId == contentId &&
|
||||||
|
it.targetLanguage == "en" && it.provider.id == "papago"
|
||||||
|
}
|
||||||
|
return TranslationSelectionChoice.Field(
|
||||||
|
requireNotNull(candidate.unit.contentId),
|
||||||
|
TranslationSelectionField.values().single { it.key == field.key },
|
||||||
|
"en",
|
||||||
|
candidate.candidateId
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun body(request: TranslationSelectionRequest, choices: List<TranslationSelectionChoice>) =
|
||||||
|
ApplyTranslationSelectionRequest(UUID.randomUUID(), request.revision, choices)
|
||||||
|
|
||||||
|
protected fun rejected(request: TranslationSelectionRequest, body: ApplyTranslationSelectionRequest, code: String) {
|
||||||
|
val error = assertThrows(SodaException::class.java) { applyService.apply(request.requestId, 777, body) }
|
||||||
|
assertEquals(code, error.errorProperty)
|
||||||
|
assertEquals(TranslationSelectionStatus.READY, requests.get(request.requestId).status)
|
||||||
|
assertEquals(0L, overrideRepository.count())
|
||||||
|
assertEquals(0L, translations.count())
|
||||||
|
assertEquals(null, requests.get(request.requestId).applyReceipt)
|
||||||
|
}
|
||||||
|
|
||||||
|
protected data class GroupFixture(val request: TranslationSelectionRequest, val ids: List<Long>, val seriesId: Long)
|
||||||
|
|
||||||
|
protected fun readyGroup(): GroupFixture {
|
||||||
|
val titles = listOf("공통 잔여 구문 끝 1", "공통 잔여 구문 끝 2", "공통 끝 3", "개별 제목")
|
||||||
|
val ids = titles.map { title ->
|
||||||
|
val id = fixture()
|
||||||
|
transaction.executeWithoutResult { entityManager.find(AudioContent::class.java, id).title = title }
|
||||||
|
id
|
||||||
|
}
|
||||||
|
val seriesId = series(ids)
|
||||||
|
val request = requests.create(91, UUID.randomUUID(), TranslationSelectionScope.Series(seriesId))
|
||||||
|
preparation.prepareNext()
|
||||||
|
val members = ids.take(3)
|
||||||
|
grouping.review(
|
||||||
|
request.requestId,
|
||||||
|
ReviewTranslationSelectionGroupingRequest(
|
||||||
|
1,
|
||||||
|
listOf(
|
||||||
|
ReviewedTitleGroup(
|
||||||
|
"g",
|
||||||
|
members,
|
||||||
|
listOf(
|
||||||
|
ReviewedSharedPart(
|
||||||
|
"end",
|
||||||
|
members.mapIndexed { index, id ->
|
||||||
|
val start = titles[index].indexOf("끝")
|
||||||
|
TitleOccurrence(id, start, start + 1)
|
||||||
|
}
|
||||||
|
),
|
||||||
|
ReviewedSharedPart("start", members.map { TitleOccurrence(it, 0, 2) })
|
||||||
|
)
|
||||||
|
)
|
||||||
|
),
|
||||||
|
listOf(ids.last())
|
||||||
|
)
|
||||||
|
)
|
||||||
|
generation.generate(request.requestId, 2)
|
||||||
|
drain(request.requestId)
|
||||||
|
return GroupFixture(requests.get(request.requestId), ids, seriesId)
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun groupChoice(fixture: GroupFixture): TranslationSelectionChoice.TitleGroup {
|
||||||
|
val candidates = candidates(fixture.request.requestId).filter { it.targetLanguage == "en" && it.provider.id == "papago" }
|
||||||
|
return TranslationSelectionChoice.TitleGroup(
|
||||||
|
"g",
|
||||||
|
"en",
|
||||||
|
candidates.single { it.unit.kind == TranslationUnitKind.GROUP_COMMON }.candidateId,
|
||||||
|
fixture.ids.take(3).map { id ->
|
||||||
|
TranslationSelectionResidualChoice(
|
||||||
|
id,
|
||||||
|
candidates.singleOrNull { it.unit.kind == TranslationUnitKind.TITLE_RESIDUAL && it.unit.contentId == id }
|
||||||
|
?.candidateId
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun replaceCandidate(candidate: TranslationCandidate) {
|
||||||
|
transaction.executeWithoutResult { candidateStore.save(candidate) }
|
||||||
|
}
|
||||||
|
}
|
||||||
+106
@@ -0,0 +1,106 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import com.sun.net.httpserver.HttpServer
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.AwsTranslationCallInterceptor
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.AwsTranslationService
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.awsTestHttpResponse
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateStatus
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Assertions.assertFalse
|
||||||
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
|
import org.junit.jupiter.api.DisplayName
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials
|
||||||
|
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider
|
||||||
|
import software.amazon.awssdk.regions.Region
|
||||||
|
import software.amazon.awssdk.services.translate.TranslateClient
|
||||||
|
import java.net.InetSocketAddress
|
||||||
|
import java.net.URI
|
||||||
|
import java.util.concurrent.Callable
|
||||||
|
import java.util.concurrent.CountDownLatch
|
||||||
|
import java.util.concurrent.Executors
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger
|
||||||
|
|
||||||
|
class TranslationSelectionCancelIntegrationTest : TranslationCandidateTestSupport() {
|
||||||
|
@Test
|
||||||
|
@DisplayName("취소 DB commit 뒤 실제 AWS SDK retry의 전송 gate와 늦은 게시를 거부한다")
|
||||||
|
fun shouldFenceActualSdkRetryAfterCancellationCommit() {
|
||||||
|
val created = request(fixture())
|
||||||
|
preparation.prepareNext()
|
||||||
|
val calls = AtomicInteger()
|
||||||
|
val server = HttpServer.create(InetSocketAddress("127.0.0.1", 0), 0)
|
||||||
|
server.createContext("/") { exchange ->
|
||||||
|
calls.incrementAndGet()
|
||||||
|
requests.cancel(created.requestId)
|
||||||
|
val body = awsTestHttpResponse("__type" to "InternalServerException", "message" to "retry")
|
||||||
|
exchange.responseHeaders.set("Content-Type", "application/cbor")
|
||||||
|
exchange.sendResponseHeaders(500, body.size.toLong())
|
||||||
|
exchange.responseBody.use { it.write(body) }
|
||||||
|
}
|
||||||
|
server.start()
|
||||||
|
try {
|
||||||
|
TranslateClient.builder().endpointOverride(URI("http://127.0.0.1:${server.address.port}"))
|
||||||
|
.region(Region.AP_NORTHEAST_2)
|
||||||
|
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test")))
|
||||||
|
.overrideConfiguration { it.addExecutionInterceptor(AwsTranslationCallInterceptor()) }
|
||||||
|
.build().use { client ->
|
||||||
|
val provider = AwsTranslationService(client)
|
||||||
|
val generation = TranslationSelectionGenerationService(requestStore, candidateStore, listOf(provider))
|
||||||
|
transaction.execute { generation.generate(created.requestId, 1) }
|
||||||
|
val worker = TranslationSelectionWorkerService(
|
||||||
|
requestStore,
|
||||||
|
candidateStore,
|
||||||
|
listOf(provider),
|
||||||
|
selectionTransactionManager
|
||||||
|
)
|
||||||
|
assertTrue(worker.runNext())
|
||||||
|
}
|
||||||
|
assertEquals(1, calls.get())
|
||||||
|
assertEquals("CANCELLED", requests.get(created.requestId).status.name)
|
||||||
|
assertTrue(candidates(created.requestId).all { it.status == TranslationCandidateStatus.DISCARDED })
|
||||||
|
} finally { server.stop(0) }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("취소는 모든 후보를 폐기하고 claim의 추가 전송과 게시를 거부한다")
|
||||||
|
fun shouldDiscardCandidatesAndRevokeClaimOnCancel() {
|
||||||
|
val requestId = generated()
|
||||||
|
val claim = requireNotNull(candidateWorker.claimNext())
|
||||||
|
assertTrue(candidateWorker.authorize(claim))
|
||||||
|
|
||||||
|
requests.cancel(requestId)
|
||||||
|
|
||||||
|
assertTrue(candidates(requestId).all { it.status == TranslationCandidateStatus.DISCARDED })
|
||||||
|
assertTrue(candidates(requestId).all { it.leaseUntil == null && it.translatedValues == null })
|
||||||
|
assertFalse(candidateWorker.authorize(claim))
|
||||||
|
assertFalse(candidateWorker.publish(claim, listOf("late"), null))
|
||||||
|
assertEquals(null, candidateWorker.claimNext())
|
||||||
|
assertEquals(0, provider.calls.get())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("이미 승인된 외부 호출을 기다리지 않고 취소하며 늦은 성공을 저장하지 않는다")
|
||||||
|
fun shouldRejectLateCompletionAfterCancel() {
|
||||||
|
val requestId = generated()
|
||||||
|
candidateWorker.runNext()
|
||||||
|
candidateWorker.runNext()
|
||||||
|
val entered = CountDownLatch(1)
|
||||||
|
val release = CountDownLatch(1)
|
||||||
|
papago.beforeTranslate = { entered.countDown(); check(release.await(10, TimeUnit.SECONDS)) }
|
||||||
|
val executor = Executors.newSingleThreadExecutor()
|
||||||
|
try {
|
||||||
|
val result = executor.submit(Callable { candidateWorker.runNext() })
|
||||||
|
assertTrue(entered.await(10, TimeUnit.SECONDS))
|
||||||
|
assertEquals("CANCELLED", requests.cancel(requestId).status.name)
|
||||||
|
release.countDown()
|
||||||
|
assertTrue(result.get(10, TimeUnit.SECONDS))
|
||||||
|
assertTrue(candidates(requestId).all { it.status == TranslationCandidateStatus.DISCARDED })
|
||||||
|
assertEquals(1, papago.requests.size)
|
||||||
|
assertEquals(null, payload(requests.get(requestId).snapshot.memberContentIds.single()))
|
||||||
|
} finally {
|
||||||
|
release.countDown()
|
||||||
|
executor.shutdownNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+92
@@ -0,0 +1,92 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateStatus
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Assertions.assertFalse
|
||||||
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
|
import org.junit.jupiter.api.DisplayName
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.util.concurrent.Callable
|
||||||
|
import java.util.concurrent.CyclicBarrier
|
||||||
|
import java.util.concurrent.Executors
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
|
class TranslationSelectionCandidateLeaseIntegrationTest : TranslationCandidateTestSupport() {
|
||||||
|
@Test
|
||||||
|
@DisplayName("동시 generate replay는 같은 후보를 반환하고 worker claim은 서로 다른 후보를 소유한다")
|
||||||
|
fun shouldSerializeConcurrentGenerationAndClaims() {
|
||||||
|
val created = request(fixture())
|
||||||
|
preparation.prepareNext()
|
||||||
|
val executor = Executors.newFixedThreadPool(2)
|
||||||
|
try {
|
||||||
|
val barrier = CyclicBarrier(2)
|
||||||
|
val generated = (1..2).map {
|
||||||
|
executor.submit(Callable { barrier.await(5, TimeUnit.SECONDS); generation.generate(created.requestId, 1) })
|
||||||
|
}.map { it.get(10, TimeUnit.SECONDS) }
|
||||||
|
assertEquals(listOf(2L, 2L), generated.map { it.revision })
|
||||||
|
assertEquals(12, candidates(created.requestId).size)
|
||||||
|
val claims = (1..2).map { executor.submit(Callable { candidateWorker.claimNext() }) }
|
||||||
|
.map { requireNotNull(it.get(10, TimeUnit.SECONDS)) }
|
||||||
|
assertEquals(2, claims.map { it.candidate.candidateId }.distinct().size)
|
||||||
|
assertEquals(0, provider.calls.get())
|
||||||
|
} finally { executor.shutdownNow() }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("만료 claim은 갱신과 게시를 거부하며 두 번째 실행 만료는 실패 종결한다")
|
||||||
|
fun shouldRecoverLeaseAtMostOnceAndFenceExpiredExecutions() {
|
||||||
|
val id = generated()
|
||||||
|
var now = LocalDateTime.of(2026, 11, 17, 23, 59, 59)
|
||||||
|
val worker = TranslationSelectionWorkerService(
|
||||||
|
requestStore,
|
||||||
|
candidateStore,
|
||||||
|
listOf(provider, papago),
|
||||||
|
selectionTransactionManager
|
||||||
|
) { now }
|
||||||
|
val first = requireNotNull(worker.claimNext())
|
||||||
|
now = now.plusSeconds(60)
|
||||||
|
assertFalse(worker.authorize(first))
|
||||||
|
assertFalse(worker.publish(first, listOf("expired"), null))
|
||||||
|
val recovered = requireNotNull(worker.claimNext())
|
||||||
|
assertEquals(first.candidate.candidateId, recovered.candidate.candidateId)
|
||||||
|
assertEquals(2, recovered.candidate.attempt)
|
||||||
|
assertTrue(recovered.candidate.fence > first.candidate.fence)
|
||||||
|
assertFalse(worker.publish(first, listOf("stale"), null))
|
||||||
|
now = now.plusSeconds(60)
|
||||||
|
assertEquals(null, worker.claimNext())
|
||||||
|
val exhausted = candidates(id).first()
|
||||||
|
assertEquals(TranslationCandidateStatus.FAILED, exhausted.status)
|
||||||
|
assertEquals("EXECUTION_INTERRUPTED", exhausted.failureCode?.name)
|
||||||
|
assertEquals(2, exhausted.attempt)
|
||||||
|
assertTrue(worker.runNext())
|
||||||
|
assertEquals(1, provider.calls.get())
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(ints = [0, 123456789, 999999999])
|
||||||
|
@DisplayName("후보 lease와 terminal 시각은 초 정밀도로 저장하고 DB 왕복에도 일치한다")
|
||||||
|
fun shouldPersistSecondPrecisionCandidateTimes(nanos: Int) {
|
||||||
|
val id = generated()
|
||||||
|
val now = LocalDateTime.of(2026, 11, 17, 23, 59, 59, nanos)
|
||||||
|
val worker = TranslationSelectionWorkerService(
|
||||||
|
requestStore,
|
||||||
|
candidateStore,
|
||||||
|
listOf(provider, papago),
|
||||||
|
selectionTransactionManager
|
||||||
|
) { now }
|
||||||
|
val claim = requireNotNull(worker.claimNext())
|
||||||
|
assertEquals(LocalDateTime.of(2026, 11, 18, 0, 0, 59), claim.candidate.leaseUntil)
|
||||||
|
assertTrue(worker.publish(claim, listOf("result"), null))
|
||||||
|
val row = transaction.execute {
|
||||||
|
entityManager.flush()
|
||||||
|
entityManager.clear()
|
||||||
|
candidateStore.find(claim.candidate.candidateId)
|
||||||
|
}!!
|
||||||
|
assertEquals(LocalDateTime.of(2026, 11, 17, 23, 59, 59), row.terminalAt)
|
||||||
|
assertEquals(row.terminalAt, row.updatedAt)
|
||||||
|
assertEquals(1, requests.get(id).progress.succeeded)
|
||||||
|
}
|
||||||
|
}
|
||||||
+167
@@ -0,0 +1,167 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.common.SodaException
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContent
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.LanguageProviderId
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslateRequest
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslateResult
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.TranslationProvider
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationCandidateStatus
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionScope
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Assertions.assertThrows
|
||||||
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
|
import org.junit.jupiter.api.DisplayName
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
class TranslationSelectionCandidateLifecycleIntegrationTest : TranslationCandidateTestSupport() {
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = ["ko", "en", "ja"])
|
||||||
|
@DisplayName("세 원본 언어 각각에서 source 외 두 지원 언어와 세 필드를 모두 동결한다")
|
||||||
|
fun shouldFreezeEveryNonSourceLanguage(source: String) {
|
||||||
|
val created = request(fixture(source))
|
||||||
|
preparation.prepareNext()
|
||||||
|
generation.generate(created.requestId, 1)
|
||||||
|
val rows = candidates(created.requestId)
|
||||||
|
assertEquals(12, rows.size)
|
||||||
|
assertEquals(listOf("ko", "en", "ja").filter { it != source }.toSet(), rows.map { it.targetLanguage }.toSet())
|
||||||
|
assertEquals(setOf("title", "detail", "tags"), rows.map { it.unit.field?.key }.toSet())
|
||||||
|
assertTrue(rows.all { it.unit.sourceLanguage == source })
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("AWS의 unchanged tags는 해당 후보만 실패하고 Papago는 독립적으로 성공한다")
|
||||||
|
fun shouldFailUnchangedAwsTagsWithoutBlockingPapago() {
|
||||||
|
val id = generated()
|
||||||
|
val unchangedAws = object : TranslationProvider {
|
||||||
|
override val providerId = LanguageProviderId.AWS
|
||||||
|
override val providerVersion = "test"
|
||||||
|
override fun translate(request: TranslateRequest) = TranslateResult(
|
||||||
|
if (request.rejectUnchangedAwsResult) request.texts else request.texts.map { "translated" }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
val worker = TranslationSelectionWorkerService(
|
||||||
|
requestStore,
|
||||||
|
candidateStore,
|
||||||
|
listOf(unchangedAws, papago),
|
||||||
|
selectionTransactionManager
|
||||||
|
)
|
||||||
|
repeat(12) { worker.runNext() }
|
||||||
|
assertEquals(2, requests.get(id).progress.failed)
|
||||||
|
assertEquals(10, requests.get(id).progress.succeeded)
|
||||||
|
assertTrue(
|
||||||
|
candidates(id).filter { it.failureCode != null }.all { it.unit.field?.key == "tags" && it.provider.id == "aws" }
|
||||||
|
)
|
||||||
|
assertEquals(6, papago.requests.size)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("수락된 generate revision의 replay는 취소 및 최신 revision 검사보다 먼저 처리한다")
|
||||||
|
fun shouldReplayGenerateBeforeClosedOrRevisionChecks() {
|
||||||
|
val id = generated()
|
||||||
|
val original = candidates(id).map { it.candidateId }
|
||||||
|
requests.cancel(id)
|
||||||
|
|
||||||
|
assertEquals("CANCELLED", generation.generate(id, 1).status.name)
|
||||||
|
assertEquals(original, candidates(id).map { it.candidateId })
|
||||||
|
assertEquals("REQUEST_CLOSED", assertThrows(SodaException::class.java) { generation.generate(id, 2) }.errorProperty)
|
||||||
|
assertEquals(0, provider.calls.get())
|
||||||
|
assertEquals(0, papago.requests.size)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("모두 개별인 시리즈도 검토 전에는 generate를 시작할 수 없다")
|
||||||
|
fun shouldRequireReviewForAllIndividualSeries() {
|
||||||
|
val id = fixture()
|
||||||
|
val created = requests.create(91, UUID.randomUUID(), TranslationSelectionScope.Series(series(listOf(id))))
|
||||||
|
preparation.prepareNext()
|
||||||
|
assertEquals(
|
||||||
|
"REVIEW_REQUIRED",
|
||||||
|
assertThrows(SodaException::class.java) { generation.generate(created.requestId, 1) }.errorProperty
|
||||||
|
)
|
||||||
|
assertEquals(emptyList<Long>(), candidates(created.requestId).map { it.candidateId })
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("한 제공자의 실패는 다른 제공자 성공과 출처를 보존한다")
|
||||||
|
fun shouldIsolateProviderFailureAndKeepEqualResultOrigins() {
|
||||||
|
val id = generated()
|
||||||
|
provider.fail = true
|
||||||
|
drain(id)
|
||||||
|
val rows = candidates(id)
|
||||||
|
assertEquals("READY", requests.get(id).status.name)
|
||||||
|
assertEquals(6, rows.count { it.status == TranslationCandidateStatus.SUCCEEDED && it.provider.id == "papago" })
|
||||||
|
assertEquals(6, rows.count { it.failureCode?.name == "PROVIDER_FAILED" && it.provider.id == "aws" })
|
||||||
|
assertEquals(6, papago.requests.size)
|
||||||
|
assertEquals(0, memories.count())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("모든 제공자가 실패해도 후보가 종결되면 요청은 READY다")
|
||||||
|
fun shouldReachReadyAfterAllProviderFailures() {
|
||||||
|
val id = generated()
|
||||||
|
provider.fail = true
|
||||||
|
papago.result = { throw IllegalStateException("secret provider body") }
|
||||||
|
drain(id)
|
||||||
|
val result = requests.get(id)
|
||||||
|
assertEquals("READY", result.status.name)
|
||||||
|
assertEquals(12, result.progress.failed)
|
||||||
|
assertEquals(12, result.progress.terminal)
|
||||||
|
assertTrue(candidates(id).all { it.translatedValues == null && it.failureCode?.name == "PROVIDER_FAILED" })
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("동일 번역 결과여도 후보 ID와 제공자 출처는 별개로 유지한다")
|
||||||
|
fun shouldKeepBothProvidersForEqualResults() {
|
||||||
|
val id = generated()
|
||||||
|
drain(id)
|
||||||
|
val title = candidates(id).filter { it.unit.field?.key == "title" && it.targetLanguage == "en" }
|
||||||
|
assertEquals(2, title.map { it.candidateId }.distinct().size)
|
||||||
|
assertEquals(setOf("aws", "papago"), title.map { it.provider.id }.toSet())
|
||||||
|
assertEquals(listOf(listOf("English title"), listOf("English title")), title.map { it.translatedValues })
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("동결 이후 버전이 바뀌거나 사라진 제공자는 대체 없이 PROVIDER_CHANGED로 종결한다")
|
||||||
|
fun shouldFailChangedProviderWithoutSubstitution() {
|
||||||
|
val id = generated()
|
||||||
|
papago.providerVersion = "changed"
|
||||||
|
drain(id)
|
||||||
|
assertEquals(6, candidates(id).count { it.failureCode?.name == "PROVIDER_CHANGED" })
|
||||||
|
assertEquals(0, papago.requests.size)
|
||||||
|
assertEquals(6, provider.calls.get())
|
||||||
|
assertEquals("nmt-test", requests.get(id).snapshot.providerManifest.single { it.id == "papago" }.version)
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = ["blank", "count"])
|
||||||
|
@DisplayName("개수 불일치와 공백 결과는 INVALID_PROVIDER_RESULT다")
|
||||||
|
fun shouldRejectInvalidProviderResults(kind: String) {
|
||||||
|
val id = generated()
|
||||||
|
papago.result = { if (kind == "count") emptyList() else listOf(" \t") }
|
||||||
|
drain(id)
|
||||||
|
assertEquals(6, candidates(id).count { it.failureCode?.name == "INVALID_PROVIDER_RESULT" })
|
||||||
|
assertEquals("READY", requests.get(id).status.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("빈 필드는 후보나 외부 호출을 만들지 않고 즉시 READY가 된다")
|
||||||
|
fun shouldReachReadyWithZeroWork() {
|
||||||
|
val contentId = fixture()
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
entityManager.find(AudioContent::class.java, contentId).apply { title = " "; detail = "" }
|
||||||
|
entityManager.createQuery("delete from AudioContentHashTag t where t.audioContent.id = :id")
|
||||||
|
.setParameter("id", contentId).executeUpdate()
|
||||||
|
}
|
||||||
|
val created = request(contentId)
|
||||||
|
preparation.prepareNext()
|
||||||
|
val result = generation.generate(created.requestId, 1)
|
||||||
|
assertEquals("READY", result.status.name)
|
||||||
|
assertEquals(0, result.progress.total)
|
||||||
|
assertEquals(0, provider.calls.get())
|
||||||
|
assertEquals(0, papago.requests.size)
|
||||||
|
}
|
||||||
|
}
|
||||||
+51
@@ -0,0 +1,51 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.content.LanguageDetectionCacheService
|
||||||
|
import kr.co.vividnext.sodalive.content.LanguageDetectionResultRepository
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.LanguageDetectionProvider
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.LanguageProviderId
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.LanguageProviderProperties
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.DisplayName
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import org.springframework.transaction.PlatformTransactionManager
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger
|
||||||
|
|
||||||
|
class TranslationSelectionDetectionDispatchIntegrationTest : TranslationSelectionTestSupport() {
|
||||||
|
@Autowired private lateinit var results: LanguageDetectionResultRepository
|
||||||
|
|
||||||
|
@Autowired private lateinit var sources: ContentTranslationSourceService
|
||||||
|
|
||||||
|
@Autowired private lateinit var manager: PlatformTransactionManager
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("첫 감지 제공자 실행 중 취소하면 cache detector의 다음 제공자 dispatch도 거부한다")
|
||||||
|
fun shouldStopFallbackDetectionAfterCancellation() {
|
||||||
|
val created = request(fixture(null))
|
||||||
|
val fallbackCalls = AtomicInteger()
|
||||||
|
val fallback = object : LanguageDetectionProvider {
|
||||||
|
override val providerId = LanguageProviderId.PAPAGO
|
||||||
|
override fun detect(query: String): String {
|
||||||
|
fallbackCalls.incrementAndGet()
|
||||||
|
return "ko"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val order = listOf(LanguageProviderId.AWS, LanguageProviderId.PAPAGO)
|
||||||
|
val detector = LanguageDetectionCacheService(
|
||||||
|
results,
|
||||||
|
listOf(provider, fallback),
|
||||||
|
LanguageProviderProperties(order, order),
|
||||||
|
manager
|
||||||
|
)
|
||||||
|
val worker = TranslationSelectionPreparationService(requestStore, sources, detector, listOf(provider), manager)
|
||||||
|
provider.detection = null
|
||||||
|
provider.beforeDetect = { requests.cancel(created.requestId) }
|
||||||
|
|
||||||
|
worker.prepareNext()
|
||||||
|
|
||||||
|
assertEquals("CANCELLED", requests.get(created.requestId).status.name)
|
||||||
|
assertEquals(0, fallbackCalls.get())
|
||||||
|
assertEquals(0, requests.get(created.requestId).snapshot.contents.size)
|
||||||
|
}
|
||||||
|
}
|
||||||
+73
@@ -0,0 +1,73 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContent
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ReviewedSharedPart
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.ReviewedTitleGroup
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TitleOccurrence
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionScope
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationUnitKind
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.dto.ReviewTranslationSelectionGroupingRequest
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
|
import org.junit.jupiter.api.DisplayName
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
class TranslationSelectionGroupingGenerationIntegrationTest : TranslationCandidateTestSupport() {
|
||||||
|
@Autowired private lateinit var grouping: TranslationSelectionGroupingService
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("공통 배열 순서와 member 소유권을 보존하며 동일 residual 입력은 요청 안에서만 공유한다")
|
||||||
|
fun shouldTranslateCommonOnceAndDeduplicateResidualWithoutLosingMembers() {
|
||||||
|
val titles = listOf("공통 잔여 구문 끝 1", "공통 잔여 구문 끝 2", "공통 끝 3", "개별 제목")
|
||||||
|
val ids = titles.map { title ->
|
||||||
|
val id = fixture()
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
entityManager.find(AudioContent::class.java, id).apply { this.title = title; detail = "" }
|
||||||
|
entityManager.createQuery("delete from AudioContentHashTag t where t.audioContent.id = :id")
|
||||||
|
.setParameter("id", id).executeUpdate()
|
||||||
|
}
|
||||||
|
id
|
||||||
|
}
|
||||||
|
val created = requests.create(91, UUID.randomUUID(), TranslationSelectionScope.Series(series(ids)))
|
||||||
|
preparation.prepareNext()
|
||||||
|
val members = ids.take(3)
|
||||||
|
val end = ReviewedSharedPart(
|
||||||
|
"end",
|
||||||
|
members.mapIndexed { i, id ->
|
||||||
|
val start = titles[i].indexOf("끝")
|
||||||
|
TitleOccurrence(id, start, start + 1)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
val start = ReviewedSharedPart("start", members.map { TitleOccurrence(it, 0, 2) })
|
||||||
|
grouping.review(
|
||||||
|
created.requestId,
|
||||||
|
ReviewTranslationSelectionGroupingRequest(
|
||||||
|
1,
|
||||||
|
listOf(ReviewedTitleGroup("g", members, listOf(end, start))),
|
||||||
|
listOf(ids.last())
|
||||||
|
)
|
||||||
|
)
|
||||||
|
generation.generate(created.requestId, 2)
|
||||||
|
val frozen = requests.get(created.requestId).snapshot
|
||||||
|
transaction.executeWithoutResult { entityManager.find(AudioContent::class.java, ids.first()).title = "changed" }
|
||||||
|
|
||||||
|
drain(created.requestId)
|
||||||
|
|
||||||
|
assertEquals(frozen, requests.get(created.requestId).snapshot)
|
||||||
|
assertEquals(16, candidates(created.requestId).size)
|
||||||
|
assertEquals(6, papago.requests.size)
|
||||||
|
assertEquals(
|
||||||
|
listOf(listOf("끝", "공통"), listOf("잔여 구문"), listOf("개별 제목")).toSet(),
|
||||||
|
papago.requests.map { it.texts }.toSet()
|
||||||
|
)
|
||||||
|
val residuals = candidates(created.requestId).filter { it.unit.kind == TranslationUnitKind.TITLE_RESIDUAL }
|
||||||
|
assertEquals(members.take(2).toSet(), residuals.map { it.unit.contentId }.toSet())
|
||||||
|
assertTrue(
|
||||||
|
candidates(created.requestId).none { it.unit.kind == TranslationUnitKind.FIELD && it.unit.contentId in members }
|
||||||
|
)
|
||||||
|
assertEquals(emptyList<String>(), papago.requests.flatMap { it.texts }.filter { it.isBlank() })
|
||||||
|
assertEquals(0, memories.count())
|
||||||
|
}
|
||||||
|
}
|
||||||
+32
@@ -0,0 +1,32 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.adapter.scheduler.TranslationSelectionWorker
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.DisplayName
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource
|
||||||
|
import org.mockito.Mockito
|
||||||
|
import org.redisson.api.RLock
|
||||||
|
import org.redisson.api.RedissonClient
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
|
class TranslationSelectionPollingIntegrationTest : TranslationSelectionTestSupport() {
|
||||||
|
@Autowired private lateinit var worker: TranslationSelectionWorker
|
||||||
|
|
||||||
|
@Autowired private lateinit var redisson: RedissonClient
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(booleans = [false, true])
|
||||||
|
@DisplayName("feature poller는 기존 Redisson lock 소유권이 있을 때만 durable 준비를 처리한다")
|
||||||
|
fun shouldPrepareOnlyWhenSchedulerLockIsHeld(held: Boolean) {
|
||||||
|
val created = request(fixture())
|
||||||
|
val lock = Mockito.mock(RLock::class.java)
|
||||||
|
Mockito.`when`(redisson.getLock("lock:translation-selection")).thenReturn(lock)
|
||||||
|
Mockito.`when`(lock.tryLock(0, 180, TimeUnit.SECONDS)).thenReturn(held)
|
||||||
|
Mockito.`when`(lock.isHeldByCurrentThread).thenReturn(held)
|
||||||
|
worker.runPendingRequests()
|
||||||
|
assertEquals(if (held) "READY_TO_GENERATE" else "PREPARING", requests.get(created.requestId).status.name)
|
||||||
|
assertEquals(0, provider.calls.get())
|
||||||
|
}
|
||||||
|
}
|
||||||
+198
@@ -0,0 +1,198 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.content.LanguageDetectionCacheService
|
||||||
|
import kr.co.vividnext.sodalive.content.translation.ContentTranslation
|
||||||
|
import kr.co.vividnext.sodalive.content.translation.ContentTranslationPayload
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionScope
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Assertions.assertFalse
|
||||||
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
|
import org.junit.jupiter.api.DisplayName
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
|
import org.junit.jupiter.params.provider.NullSource
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import org.springframework.transaction.PlatformTransactionManager
|
||||||
|
import org.springframework.transaction.support.TransactionSynchronizationManager
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.util.UUID
|
||||||
|
import java.util.concurrent.CountDownLatch
|
||||||
|
import java.util.concurrent.Executors
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
|
class TranslationSelectionPreparationIntegrationTest : TranslationSelectionTestSupport() {
|
||||||
|
@Autowired private lateinit var sources: ContentTranslationSourceService
|
||||||
|
|
||||||
|
@Autowired private lateinit var detector: LanguageDetectionCacheService
|
||||||
|
|
||||||
|
@Autowired private lateinit var manager: PlatformTransactionManager
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("유효한 기존 언어는 재감지 없이 snapshot과 모든 등록 제공자 버전을 준비한다")
|
||||||
|
fun shouldUseValidSourceLanguageWithoutRedetection() {
|
||||||
|
val created = request(fixture("en"))
|
||||||
|
assertTrue(preparation.prepareNext())
|
||||||
|
val result = requests.get(created.requestId)
|
||||||
|
assertEquals("READY_TO_GENERATE", result.status.name)
|
||||||
|
assertEquals(listOf("ko", "ja"), result.snapshot.targetLanguages)
|
||||||
|
assertEquals(listOf("aws" to "test"), result.snapshot.providerManifest.map { it.id to it.version })
|
||||||
|
assertEquals(emptyList<String>(), provider.detectionQueries)
|
||||||
|
assertEquals(0, provider.calls.get())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("누락 언어 감지는 DB 잠금 밖에서 실행하고 기존 payload와 일반 번역 작업을 변경하지 않는다")
|
||||||
|
fun shouldDetectMissingLanguageWithoutPublishingOrEnqueuing() {
|
||||||
|
val id = fixture(null)
|
||||||
|
val old = ContentTranslationPayload("Published", " Previous detail ", "#old")
|
||||||
|
translations.saveAndFlush(ContentTranslation(id, "en", old))
|
||||||
|
val created = request(id)
|
||||||
|
provider.beforeDetect = { assertFalse(TransactionSynchronizationManager.isActualTransactionActive()) }
|
||||||
|
|
||||||
|
assertTrue(preparation.prepareNext())
|
||||||
|
|
||||||
|
val result = requests.get(created.requestId)
|
||||||
|
assertEquals("READY_TO_GENERATE", result.status.name)
|
||||||
|
assertEquals("ko", result.snapshot.contents.single().sourceLanguage)
|
||||||
|
assertEquals("ko", snapshot(id).sourceLanguage)
|
||||||
|
assertEquals(listOf("원문 제목 원문 설명 #힐링"), provider.detectionQueries)
|
||||||
|
assertEquals(old, payload(id))
|
||||||
|
assertEquals(0L, jobs.count())
|
||||||
|
assertEquals(0L, memories.count())
|
||||||
|
assertEquals(0, provider.calls.get())
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@NullSource
|
||||||
|
@ValueSource(strings = ["fr"])
|
||||||
|
@DisplayName("실패하거나 미지원인 감지는 부분 snapshot을 게시하지 않고 명시적으로 실패한다")
|
||||||
|
fun shouldFailUnsupportedOrUndetectedLanguage(detected: String?) {
|
||||||
|
val id = fixture(null)
|
||||||
|
val created = request(id)
|
||||||
|
provider.detection = detected
|
||||||
|
preparation.prepareNext()
|
||||||
|
val result = requests.get(created.requestId)
|
||||||
|
assertEquals("FAILED", result.status.name)
|
||||||
|
assertEquals(if (detected == null) "LANGUAGE_DETECTION_FAILED" else "UNSUPPORTED_SOURCE_LANGUAGE", result.errorCode?.name)
|
||||||
|
assertEquals(emptyList<Long>(), result.snapshot.contents.map { it.contentId })
|
||||||
|
assertEquals(null, snapshot(id).sourceLanguage)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("저장된 미지원 원본 언어는 재감지로 대체하지 않는다")
|
||||||
|
fun shouldFailExistingUnsupportedLanguage() {
|
||||||
|
val created = request(fixture("fr"))
|
||||||
|
preparation.prepareNext()
|
||||||
|
assertEquals("UNSUPPORTED_SOURCE_LANGUAGE", requests.get(created.requestId).errorCode?.name)
|
||||||
|
assertEquals(emptyList<String>(), provider.detectionQueries)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("감지 전에 취소된 요청은 새 감지를 시작하지 않는다")
|
||||||
|
fun shouldCancelBeforeDetection() {
|
||||||
|
val created = request(fixture(null))
|
||||||
|
requests.cancel(created.requestId)
|
||||||
|
assertFalse(preparation.prepareNext())
|
||||||
|
assertEquals("CANCELLED", requests.get(created.requestId).status.name)
|
||||||
|
assertEquals(emptyList<String>(), provider.detectionQueries)
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = ["cancel", "source", "membership"])
|
||||||
|
@DisplayName("감지 중 취소 또는 원문 구성원 변경은 늦은 snapshot과 후속 감지를 막는다")
|
||||||
|
fun shouldNotPublishPreparedSnapshotAfterCancelOrSourceChange(change: String) {
|
||||||
|
val id = fixture(null)
|
||||||
|
val second = fixture(null)
|
||||||
|
val seriesId = series(listOf(id, second))
|
||||||
|
val created = requests.create(91, UUID.randomUUID(), TranslationSelectionScope.Series(seriesId))
|
||||||
|
provider.beforeDetect = {
|
||||||
|
when (change) {
|
||||||
|
"cancel" -> requests.cancel(created.requestId)
|
||||||
|
"source" -> transaction.executeWithoutResult { writer.lockContent(id)!!.title = "변경" }
|
||||||
|
"membership" -> transaction.executeWithoutResult { writer.lockContent(second)!!.isActive = false }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
preparation.prepareNext()
|
||||||
|
|
||||||
|
val result = requests.get(created.requestId)
|
||||||
|
assertEquals(if (change == "cancel") "CANCELLED" else "FAILED", result.status.name)
|
||||||
|
assertEquals(
|
||||||
|
when (change) { "source" -> "SOURCE_CHANGED"; "membership" -> "MEMBERSHIP_CHANGED"; else -> null },
|
||||||
|
result.errorCode?.name
|
||||||
|
)
|
||||||
|
assertEquals(emptyList<Long>(), result.snapshot.contents.map { it.contentId })
|
||||||
|
assertEquals(null, snapshot(id).sourceLanguage)
|
||||||
|
assertEquals(1, provider.detectionQueries.size)
|
||||||
|
assertEquals(0L, jobs.count())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("진행 중 감지의 종료를 기다리지 않고 취소를 커밋하며 늦은 결과를 폐기한다")
|
||||||
|
fun shouldCancelWhileDetectionIsInFlight() {
|
||||||
|
val id = fixture(null)
|
||||||
|
val created = request(id)
|
||||||
|
val entered = CountDownLatch(1)
|
||||||
|
val release = CountDownLatch(1)
|
||||||
|
val executor = Executors.newSingleThreadExecutor()
|
||||||
|
provider.beforeDetect = { entered.countDown(); check(release.await(10, TimeUnit.SECONDS)) }
|
||||||
|
try {
|
||||||
|
val running = executor.submit<Boolean> { preparation.prepareNext() }
|
||||||
|
assertTrue(entered.await(5, TimeUnit.SECONDS))
|
||||||
|
val cancelled = requests.cancel(created.requestId)
|
||||||
|
assertEquals("CANCELLED", cancelled.status.name)
|
||||||
|
assertEquals("CANCELLED", requests.get(created.requestId).status.name)
|
||||||
|
release.countDown()
|
||||||
|
assertTrue(running.get(10, TimeUnit.SECONDS))
|
||||||
|
assertEquals(emptyList<Long>(), requests.get(created.requestId).snapshot.contents.map { it.contentId })
|
||||||
|
assertEquals(null, snapshot(id).sourceLanguage)
|
||||||
|
} finally {
|
||||||
|
release.countDown()
|
||||||
|
executor.shutdownNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("60초 lease 만료는 한 번만 복구하며 두 번째 만료는 준비 중단으로 실패한다")
|
||||||
|
fun shouldRecoverPreparationOnceThenFail() {
|
||||||
|
var now = LocalDateTime.of(2026, 11, 17, 23, 59, 59, 999999999)
|
||||||
|
val worker = TranslationSelectionPreparationService(requestStore, sources, detector, listOf(provider), manager) { now }
|
||||||
|
val created = request(fixture(null))
|
||||||
|
val first = requireNotNull(worker.claimNext())
|
||||||
|
assertEquals(1, requests.get(created.requestId).preparationAttempt)
|
||||||
|
assertEquals(LocalDateTime.of(2026, 11, 18, 0, 0, 59), requests.get(created.requestId).preparationLeaseUntil)
|
||||||
|
now = now.plusSeconds(60)
|
||||||
|
val recovered = requireNotNull(worker.claimNext())
|
||||||
|
assertTrue(recovered.fence > first.fence)
|
||||||
|
assertEquals(2, requests.get(created.requestId).preparationAttempt)
|
||||||
|
worker.processPrepare(first)
|
||||||
|
assertEquals(emptyList<String>(), provider.detectionQueries)
|
||||||
|
now = now.plusSeconds(60)
|
||||||
|
assertEquals(null, worker.claimNext())
|
||||||
|
val failed = requests.get(created.requestId)
|
||||||
|
assertEquals("FAILED", failed.status.name)
|
||||||
|
assertEquals("PREPARATION_INTERRUPTED", failed.errorCode?.name)
|
||||||
|
worker.processPrepare(recovered)
|
||||||
|
assertEquals(emptyList<Long>(), requests.get(created.requestId).snapshot.contents.map { it.contentId })
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("만료된 실행은 스스로 lease를 갱신하거나 복구 실행의 snapshot을 덮지 못한다")
|
||||||
|
fun shouldRejectExpiredFenceAndPublishRecovery() {
|
||||||
|
var now = LocalDateTime.of(2026, 11, 17, 0, 0)
|
||||||
|
val worker = TranslationSelectionPreparationService(requestStore, sources, detector, listOf(provider), manager) { now }
|
||||||
|
val created = request(fixture(null))
|
||||||
|
val first = requireNotNull(worker.claimNext())
|
||||||
|
now = now.plusSeconds(60)
|
||||||
|
worker.processPrepare(first)
|
||||||
|
assertEquals(emptyList<String>(), provider.detectionQueries)
|
||||||
|
val recovered = requireNotNull(worker.claimNext())
|
||||||
|
worker.processPrepare(recovered)
|
||||||
|
val result = requests.get(created.requestId)
|
||||||
|
worker.processPrepare(first)
|
||||||
|
assertEquals("READY_TO_GENERATE", result.status.name)
|
||||||
|
assertEquals(result, requests.get(created.requestId))
|
||||||
|
assertEquals(1, provider.detectionQueries.size)
|
||||||
|
}
|
||||||
|
}
|
||||||
+143
@@ -0,0 +1,143 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContent
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContentDetailTranslationService
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContentService
|
||||||
|
import kr.co.vividnext.sodalive.content.translation.ContentTranslationPayload
|
||||||
|
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType
|
||||||
|
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.v2.contenttranslation.domain.ContentSourceFieldKey
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Assertions.assertFalse
|
||||||
|
import org.junit.jupiter.api.Assertions.assertNull
|
||||||
|
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.ValueSource
|
||||||
|
import org.mockito.Mockito
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import org.springframework.boot.test.mock.mockito.SpyBean
|
||||||
|
import org.springframework.data.jpa.repository.Query
|
||||||
|
import org.springframework.transaction.support.TransactionSynchronizationManager
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
|
class TranslationSelectionRefreshIntegrationTest : TranslationSelectionApplyTestSupport() {
|
||||||
|
@Autowired private lateinit var ownerService: AudioContentService
|
||||||
|
|
||||||
|
@Autowired private lateinit var ordinaryWorker: TranslationJobWorker
|
||||||
|
|
||||||
|
@Autowired private lateinit var detailService: AudioContentDetailTranslationService
|
||||||
|
|
||||||
|
@SpyBean private lateinit var claims: TranslationJobRepository
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
fun useH2ClaimSyntax() {
|
||||||
|
val sql = TranslationJobRepository::class.java.getMethod("findNextPendingGroupJobIdsForUpdate", LocalDateTime::class.java)
|
||||||
|
.getAnnotation(Query::class.java).value.replace("skip locked", "")
|
||||||
|
Mockito.doAnswer { call ->
|
||||||
|
entityManager.createNativeQuery(sql).setParameter("now", call.getArgument<LocalDateTime>(0))
|
||||||
|
.resultList.map { (it as Number).toLong() }
|
||||||
|
}.`when`(claims).findNextPendingGroupJobIdsForUpdate(Mockito.any(LocalDateTime::class.java) ?: LocalDateTime.now())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("후보 제목 상세만 적용한 뒤 일반 제목 수정과 worker는 태그 메모리 없이 제목을 게시하고 완료한다")
|
||||||
|
fun shouldRefreshChangedTitleAfterPartialApplyWithoutUnselectedMemories() {
|
||||||
|
val id = fixture()
|
||||||
|
applyTitleAndDetail(id)
|
||||||
|
assertEquals(ContentTranslationPayload("English title", "English detail", "#힐링"), payload(id))
|
||||||
|
assertEquals(0L, memories.count())
|
||||||
|
assertEquals(0L, jobs.count())
|
||||||
|
assertFalse(materializer.materialize(LanguageTranslationTargetType.CONTENT, id, "en"))
|
||||||
|
modify(id, "")
|
||||||
|
val detailApproval = transaction.execute { overrides.findByContentId(id).single() }!!
|
||||||
|
assertEquals(ContentSourceFieldKey.DETAIL, detailApproval.approval.field)
|
||||||
|
assertEquals(setOf("title"), jobs.findAll().map { it.fieldKey }.toSet())
|
||||||
|
provider.beforeTranslate = { assertFalse(TransactionSynchronizationManager.isActualTransactionActive()) }
|
||||||
|
|
||||||
|
assertTrue(ordinaryWorker.processNextGroup())
|
||||||
|
|
||||||
|
assertEquals(ContentTranslationPayload("Edited title", "English detail", "#힐링"), payload(id))
|
||||||
|
assertEquals(TranslationJobStatus.COMPLETED, jobs.findAll().single { it.targetLanguage == "en" }.status)
|
||||||
|
assertEquals(detailApproval, transaction.execute { overrides.findByContentId(id).single() })
|
||||||
|
assertEquals(listOf("수정 제목"), memories.findAll().map { it.sourceText })
|
||||||
|
assertEquals(0, memories.findAll().count { it.sourceHash == SourceTextNormalizer.hash("#힐링") })
|
||||||
|
assertEquals("Edited title", detailService.resolve(id, "en").translated?.title)
|
||||||
|
assertEquals(setOf("title"), jobs.findAll().map { it.fieldKey }.toSet())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("변경되지 않은 태그의 메모리가 있어도 worker는 기존 payload의 태그 바이트를 그대로 보존한다")
|
||||||
|
fun shouldPreserveUnselectedPayloadEvenWhenItsMemoryExists() {
|
||||||
|
val id = fixture()
|
||||||
|
applyTitleAndDetail(id)
|
||||||
|
val tags = " \t#Legacy, #Tags\n"
|
||||||
|
transaction.executeWithoutResult {
|
||||||
|
translations.findByContentIdAndLocale(id, "en")!!.renderedPayload =
|
||||||
|
ContentTranslationPayload("English title", "English detail", tags)
|
||||||
|
}
|
||||||
|
memories.saveAndFlush(
|
||||||
|
TranslationMemory(SourceTextNormalizer.hash("#힐링"), "#힐링", "ko", "en", "#Automatic tags", "aws", "test")
|
||||||
|
)
|
||||||
|
modify(id, "")
|
||||||
|
|
||||||
|
assertTrue(ordinaryWorker.processNextGroup())
|
||||||
|
|
||||||
|
assertEquals(ContentTranslationPayload("Edited title", "English detail", tags), payload(id))
|
||||||
|
assertEquals(TranslationJobStatus.COMPLETED, jobs.findAll().single { it.targetLanguage == "en" }.status)
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(booleans = [true, false])
|
||||||
|
@DisplayName("같은 변경 묶음의 태그 번역 실패 시 기존 payload의 성공 제목만 게시하며 새 payload는 완성 전 생성하지 않는다")
|
||||||
|
fun shouldCompleteSuccessfulFieldsOnlyAfterPublishingThem(existingPayload: Boolean) {
|
||||||
|
val id = fixture()
|
||||||
|
if (existingPayload) applyTitleAndDetail(id)
|
||||||
|
modify(id, ",\"tags\":\"#새태그\"")
|
||||||
|
assertEquals(setOf("title", "tags"), jobs.findAll().map { it.fieldKey }.toSet())
|
||||||
|
provider.beforeTranslate = { assertFalse(TransactionSynchronizationManager.isActualTransactionActive()) }
|
||||||
|
|
||||||
|
assertTrue(ordinaryWorker.processNextGroup())
|
||||||
|
|
||||||
|
val englishJobs = jobs.findAll().filter { it.targetLanguage == "en" }.associateBy { it.fieldKey }
|
||||||
|
assertEquals(TranslationJobStatus.PENDING, englishJobs.getValue("tags").status)
|
||||||
|
assertEquals(1, englishJobs.getValue("tags").retryCount)
|
||||||
|
assertEquals(listOf("수정 제목"), memories.findAll().map { it.sourceText })
|
||||||
|
if (existingPayload) {
|
||||||
|
assertEquals(ContentTranslationPayload("Edited title", "English detail", "#새태그"), payload(id))
|
||||||
|
assertEquals(TranslationJobStatus.COMPLETED, englishJobs.getValue("title").status)
|
||||||
|
assertEquals(
|
||||||
|
setOf(ContentSourceFieldKey.DETAIL),
|
||||||
|
transaction.execute { overrides.findByContentId(id).map { it.approval.field }.toSet() }
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
assertNull(payload(id))
|
||||||
|
assertEquals(TranslationJobStatus.PENDING, englishJobs.getValue("title").status)
|
||||||
|
assertEquals(1, englishJobs.getValue("title").retryCount)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun applyTitleAndDetail(id: Long) {
|
||||||
|
val request = ready(id)
|
||||||
|
applyService.apply(
|
||||||
|
request.requestId,
|
||||||
|
777,
|
||||||
|
body(request, listOf(field(request, ContentSourceFieldKey.TITLE), field(request, ContentSourceFieldKey.DETAIL)))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun modify(id: Long, extra: String) {
|
||||||
|
val creator = transaction.execute { entityManager.find(AudioContent::class.java, id).member }!!
|
||||||
|
ownerService.modifyAudioContent(
|
||||||
|
null,
|
||||||
|
"""{"contentId":$id,"title":"수정 제목","isAdult":false,"isCommentAvailable":true$extra}""",
|
||||||
|
creator
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
+175
@@ -0,0 +1,175 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode
|
||||||
|
import kr.co.vividnext.sodalive.common.SodaException
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContent
|
||||||
|
import kr.co.vividnext.sodalive.creator.admin.content.series.Series
|
||||||
|
import kr.co.vividnext.sodalive.member.Member
|
||||||
|
import kr.co.vividnext.sodalive.member.MemberRole
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionScope
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionStatus
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Assertions.assertThrows
|
||||||
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
|
import org.junit.jupiter.api.DisplayName
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest
|
||||||
|
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.http.MediaType
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.util.UUID
|
||||||
|
import java.util.concurrent.Callable
|
||||||
|
import java.util.concurrent.CyclicBarrier
|
||||||
|
import java.util.concurrent.Executors
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
|
@SpringBootTest(
|
||||||
|
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||||
|
classes = [TranslationSelectionTestConfiguration::class],
|
||||||
|
properties = ["spring.cache.type=none"]
|
||||||
|
)
|
||||||
|
class TranslationSelectionRequestIntegrationTest : TranslationSelectionTestSupport() {
|
||||||
|
@Autowired private lateinit var http: TestRestTemplate
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("생성은 외부 호출 전에 PREPARING 요청을 저장하고 같은 본문은 원래 요청을 반환한다")
|
||||||
|
fun shouldReplaySameCreationBody() {
|
||||||
|
val contentId = fixture(null)
|
||||||
|
val adminId = transaction.execute {
|
||||||
|
val admin = Member(password = "test", nickname = "selection-admin").apply { role = MemberRole.ADMIN }
|
||||||
|
entityManager.persist(admin)
|
||||||
|
requireNotNull(admin.id)
|
||||||
|
}!!
|
||||||
|
val headers = HttpHeaders().apply {
|
||||||
|
contentType = MediaType.APPLICATION_JSON
|
||||||
|
set("X-Test-Member", adminId.toString())
|
||||||
|
}
|
||||||
|
val body = """{"clientRequestId":"${UUID.randomUUID()}","scope":{"type":"CONTENT","contentId":$contentId}}"""
|
||||||
|
|
||||||
|
val first = http.postForEntity(BASE, HttpEntity(body, headers), JsonNode::class.java)
|
||||||
|
|
||||||
|
assertEquals(200, first.statusCodeValue)
|
||||||
|
assertTrue(first.body!!.path("success").booleanValue())
|
||||||
|
val data = first.body!!.path("data")
|
||||||
|
assertEquals("PREPARING", data.path("status").textValue())
|
||||||
|
assertEquals(1, jdbc.queryForObject("select count(*) from translation_selection_request", Int::class.java))
|
||||||
|
val replay = http.postForEntity(BASE, HttpEntity(body, headers), JsonNode::class.java)
|
||||||
|
assertEquals(data, replay.body!!.path("data"))
|
||||||
|
assertEquals(emptyList<String>(), provider.detectionQueries)
|
||||||
|
assertEquals(0, provider.calls.get())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("같은 관리자 UUID에 다른 범위를 보내면 원래 요청을 유지하고 충돌한다")
|
||||||
|
fun shouldRejectReusedClientIdWithDifferentBody() {
|
||||||
|
val key = UUID.randomUUID()
|
||||||
|
val first = requests.create(91, key, TranslationSelectionScope.Content(fixture()))
|
||||||
|
val error = assertThrows(SodaException::class.java) {
|
||||||
|
requests.create(91, key, TranslationSelectionScope.Content(fixture()))
|
||||||
|
}
|
||||||
|
assertEquals("IDEMPOTENCY_CONFLICT", error.errorProperty)
|
||||||
|
assertEquals(first, requests.get(first.requestId))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("동시 생성 unique 충돌은 롤백된 삽입과 분리하여 같은 요청으로 반환한다")
|
||||||
|
fun shouldSerializeConcurrentUniqueCreation() {
|
||||||
|
val scope = TranslationSelectionScope.Content(fixture())
|
||||||
|
val key = UUID.randomUUID()
|
||||||
|
val barrier = CyclicBarrier(2)
|
||||||
|
val executor = Executors.newFixedThreadPool(2)
|
||||||
|
try {
|
||||||
|
val results = (1..2).map {
|
||||||
|
executor.submit(Callable { barrier.await(5, TimeUnit.SECONDS); requests.create(91, key, scope) })
|
||||||
|
}.map { it.get(10, TimeUnit.SECONDS) }
|
||||||
|
assertEquals(results[0].requestId, results[1].requestId)
|
||||||
|
assertEquals(1L, requestRepository.count())
|
||||||
|
} finally {
|
||||||
|
executor.shutdownNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("현재 연결된 활성 콘텐츠만 고정하고 시리즈 자체 언어는 감지하지 않는다")
|
||||||
|
fun shouldSnapshotOnlyActiveLinkedMembers() {
|
||||||
|
val active = fixture()
|
||||||
|
val inactive = fixture()
|
||||||
|
fixture()
|
||||||
|
val seriesId = series(listOf(active, inactive))
|
||||||
|
transaction.executeWithoutResult { entityManager.find(AudioContent::class.java, inactive).isActive = false }
|
||||||
|
|
||||||
|
val created = requests.create(91, UUID.randomUUID(), TranslationSelectionScope.Series(seriesId))
|
||||||
|
assertEquals(listOf(active), created.snapshot.memberContentIds)
|
||||||
|
assertTrue(preparation.prepareNext())
|
||||||
|
|
||||||
|
val prepared = requests.get(created.requestId)
|
||||||
|
assertEquals(TranslationSelectionStatus.REVIEW_REQUIRED, prepared.status)
|
||||||
|
assertEquals(listOf(active), prepared.snapshot.contents.map { it.contentId })
|
||||||
|
assertEquals(listOf(active), prepared.snapshot.grouping!!.individualContentIds)
|
||||||
|
assertEquals(null, transaction.execute { entityManager.find(Series::class.java, seriesId).languageCode })
|
||||||
|
assertEquals(emptyList<String>(), provider.detectionQueries)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("활성 콘텐츠가 없는 시리즈는 EMPTY_SCOPE 실패 요청을 저장한다")
|
||||||
|
fun shouldStoreEmptyScopeFailure() {
|
||||||
|
val id = fixture()
|
||||||
|
val seriesId = series(listOf(id))
|
||||||
|
transaction.executeWithoutResult { entityManager.find(AudioContent::class.java, id).isActive = false }
|
||||||
|
val created = requests.create(91, UUID.randomUUID(), TranslationSelectionScope.Series(seriesId))
|
||||||
|
assertEquals(TranslationSelectionStatus.FAILED, requests.get(created.requestId).status)
|
||||||
|
assertEquals("EMPTY_SCOPE", created.errorCode?.name)
|
||||||
|
assertEquals(1L, requestRepository.count())
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(ints = [0, 123456789, 999999999])
|
||||||
|
@DisplayName("생성 UTC 시각은 날짜 경계에서 초 단위로 한 번 정규화하고 DB 왕복에도 동일하다")
|
||||||
|
fun shouldPersistSecondPrecisionCreationTime(nano: Int) {
|
||||||
|
val fixed = LocalDateTime.of(2026, 11, 17, 23, 59, 59, nano)
|
||||||
|
val service = TranslationSelectionRequestService(
|
||||||
|
requestStore,
|
||||||
|
sourceService,
|
||||||
|
transactionManagerForSelection,
|
||||||
|
candidateStore
|
||||||
|
) { fixed }
|
||||||
|
val result = service.create(91, UUID.randomUUID(), TranslationSelectionScope.Content(fixture()))
|
||||||
|
val stored = transaction.execute {
|
||||||
|
entityManager.flush()
|
||||||
|
entityManager.clear()
|
||||||
|
requestStore.find(result.requestId)
|
||||||
|
}!!
|
||||||
|
assertEquals(LocalDateTime.of(2026, 11, 17, 23, 59, 59), result.createdAt)
|
||||||
|
assertEquals(result.createdAt, stored.createdAt)
|
||||||
|
assertEquals(result.updatedAt, stored.updatedAt)
|
||||||
|
val adminId = transaction.execute {
|
||||||
|
val admin = Member(password = "test", nickname = "timestamp-admin").apply { role = MemberRole.ADMIN }
|
||||||
|
entityManager.persist(admin)
|
||||||
|
requireNotNull(admin.id)
|
||||||
|
}!!
|
||||||
|
val headers = HttpHeaders().apply { set("X-Test-Member", adminId.toString()) }
|
||||||
|
val response = http.exchange(
|
||||||
|
"$BASE/${result.requestId}",
|
||||||
|
HttpMethod.GET,
|
||||||
|
HttpEntity<String>(headers),
|
||||||
|
JsonNode::class.java
|
||||||
|
)
|
||||||
|
assertEquals("2026-11-17T23:59:59Z", response.body!!.path("data").path("createdAt").asText())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Autowired private lateinit var sourceService: ContentTranslationSourceService
|
||||||
|
|
||||||
|
@Autowired private lateinit var candidateStore: kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationCandidateStore
|
||||||
|
|
||||||
|
@Autowired private lateinit var transactionManagerForSelection: org.springframework.transaction.PlatformTransactionManager
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val BASE = "/admin/audio-content/translation-requests"
|
||||||
|
}
|
||||||
|
}
|
||||||
+28
@@ -0,0 +1,28 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.common.SodaExceptionHandler
|
||||||
|
import kr.co.vividnext.sodalive.content.ContentDetailTranslationTestConfiguration
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.adapter.`in`.web.AdminTranslationSelectionController
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.adapter.`in`.web.TranslationSelectionInputExceptionHandler
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence.JpaTranslationCandidateStore
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.adapter.out.persistence.JpaTranslationSelectionStore
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.adapter.scheduler.TranslationSelectionWorker
|
||||||
|
import org.redisson.api.RedissonClient
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean
|
||||||
|
import org.springframework.context.annotation.Configuration
|
||||||
|
import org.springframework.context.annotation.Import
|
||||||
|
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableGlobalMethodSecurity(prePostEnabled = true)
|
||||||
|
@Import(
|
||||||
|
ContentDetailTranslationTestConfiguration::class, SodaExceptionHandler::class,
|
||||||
|
JpaTranslationSelectionStore::class, JpaTranslationCandidateStore::class, ContentTranslationSourceService::class,
|
||||||
|
TranslationSelectionGenerationService::class, TranslationSelectionCandidateQueryService::class,
|
||||||
|
TranslationSelectionWorkerService::class, TranslationSelectionApplyService::class,
|
||||||
|
TranslationSelectionRequestService::class, TranslationSelectionPreparationService::class,
|
||||||
|
AdminTranslationSelectionController::class, TranslationSelectionInputExceptionHandler::class,
|
||||||
|
TranslationSelectionGroupingService::class, TranslationSelectionContentsService::class, TranslationSelectionWorker::class
|
||||||
|
)
|
||||||
|
@MockBean(RedissonClient::class)
|
||||||
|
class TranslationSelectionTestConfiguration
|
||||||
+54
@@ -0,0 +1,54 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.contenttranslation.application
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.admin.content.series.genre.SeriesGenre
|
||||||
|
import kr.co.vividnext.sodalive.content.AudioContent
|
||||||
|
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.v2.contenttranslation.adapter.out.persistence.TranslationSelectionRequestRepository
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionRequest
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.domain.TranslationSelectionScope
|
||||||
|
import kr.co.vividnext.sodalive.v2.contenttranslation.port.TranslationSelectionStore
|
||||||
|
import org.junit.jupiter.api.BeforeEach
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
@SpringBootTest(
|
||||||
|
classes = [TranslationSelectionTestConfiguration::class],
|
||||||
|
properties = ["spring.cache.type=none", "spring.jpa.show-sql=false"]
|
||||||
|
)
|
||||||
|
abstract class TranslationSelectionTestSupport : ContentTranslationTestSupport() {
|
||||||
|
@Autowired protected lateinit var requests: TranslationSelectionRequestService
|
||||||
|
|
||||||
|
@Autowired protected lateinit var preparation: TranslationSelectionPreparationService
|
||||||
|
|
||||||
|
@Autowired protected lateinit var requestStore: TranslationSelectionStore
|
||||||
|
|
||||||
|
@Autowired protected lateinit var requestRepository: TranslationSelectionRequestRepository
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
fun prepareSelectionStorage() {
|
||||||
|
jdbc.update("delete from translation_candidate")
|
||||||
|
requestRepository.deleteAllInBatch()
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun request(id: Long): TranslationSelectionRequest =
|
||||||
|
requests.create(91, UUID.randomUUID(), TranslationSelectionScope.Content(id))
|
||||||
|
|
||||||
|
protected fun series(ids: List<Long>): Long = transaction.execute {
|
||||||
|
val creator = entityManager.find(AudioContent::class.java, ids.first()).member
|
||||||
|
val genre = SeriesGenre("selection-genre")
|
||||||
|
entityManager.persist(genre)
|
||||||
|
val series = Series("시리즈 메타데이터", "소개", null).apply { member = creator; this.genre = genre }
|
||||||
|
entityManager.persist(series)
|
||||||
|
ids.forEach { id ->
|
||||||
|
entityManager.persist(
|
||||||
|
SeriesContent().apply {
|
||||||
|
this.series = series
|
||||||
|
content = entityManager.find(AudioContent::class.java, id)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
requireNotNull(series.id)
|
||||||
|
}!!
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user