From c6b66cd7ff905bdbecfa0fce0169b39ddee3fc2e Mon Sep 17 00:00:00 2001 From: Mark Allen <3417310+maallen@users.noreply.github.com> Date: Fri, 15 Nov 2024 17:32:00 +0000 Subject: [PATCH] Added unit test to verify metric logging --- .../box/l10n/mojito/service/tm/TMService.java | 1 - .../l10n/mojito/service/tm/TMServiceTest.java | 240 ++++++++++++++++++ 2 files changed, 240 insertions(+), 1 deletion(-) diff --git a/webapp/src/main/java/com/box/l10n/mojito/service/tm/TMService.java b/webapp/src/main/java/com/box/l10n/mojito/service/tm/TMService.java index 9f20aef549..7b37a6d8e4 100644 --- a/webapp/src/main/java/com/box/l10n/mojito/service/tm/TMService.java +++ b/webapp/src/main/java/com/box/l10n/mojito/service/tm/TMService.java @@ -621,7 +621,6 @@ public AddTMTextUnitCurrentVariantResult addTMTextUnitCurrentVariantWithResult( && currentTmTextUnitVariant.getStatus() == TMTextUnitVariant.Status.OVERRIDDEN; if (currentTmTextUnitVariant.getStatus() == TMTextUnitVariant.Status.MT_REVIEW_NEEDED && status == TMTextUnitVariant.Status.APPROVED) { - logAiReviewMetrics(content, currentTmTextUnitVariant); } boolean updateNeeded = diff --git a/webapp/src/test/java/com/box/l10n/mojito/service/tm/TMServiceTest.java b/webapp/src/test/java/com/box/l10n/mojito/service/tm/TMServiceTest.java index 0bc2813f02..adee8f0e73 100644 --- a/webapp/src/test/java/com/box/l10n/mojito/service/tm/TMServiceTest.java +++ b/webapp/src/test/java/com/box/l10n/mojito/service/tm/TMServiceTest.java @@ -48,6 +48,10 @@ import com.google.common.base.Function; import com.google.common.collect.FluentIterable; import com.google.common.collect.Lists; +import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.Tags; +import io.micrometer.core.instrument.simple.SimpleMeterRegistry; +import jakarta.transaction.Transactional; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -63,6 +67,7 @@ import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; +import org.mockito.Mockito; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -4808,4 +4813,239 @@ public void testAddTMTextUnitWithOverriddenStatus() throws RepositoryNameAlready assertEquals("this is the newest content", textUnitDTOFromSearch.getTarget()); assertEquals(TMTextUnitVariant.Status.APPROVED, textUnitDTOFromSearch.getStatus()); } + + @Test + @Transactional + public void testMTReviewMetricsLoggingTranslationUpdatedMediumSimilarity() + throws RepositoryNameAlreadyUsedException { + MeterRegistry meterRegistry = Mockito.spy(new SimpleMeterRegistry()); + this.tmService.meterRegistry = meterRegistry; + createTestData(); + + Long textUnitId = + addTextUnitAndCheck( + this.tmId, + this.assetId, + "mtReviewMetricsLogging", + "mt translation content", + "some comment", + "3212c3beb09db681379b7a1ed9f37bfe", + "5f3ca19eb49f50b55326065f4185dadd"); + + Locale targetLocale = this.localeService.findByBcp47Tag("fr-FR"); + + TMTextUnitCurrentVariant tmTextUnitCurrentVariant = + this.tmService.addTMTextUnitCurrentVariant( + textUnitId, + targetLocale.getId(), + "mt translation content", + "some comment", + TMTextUnitVariant.Status.MT_REVIEW_NEEDED, + false); + + this.tmService.addTMTextUnitCurrentVariantWithResult( + tmTextUnitCurrentVariant, + this.tmId, + this.assetId, + textUnitId, + tmTextUnitCurrentVariant.getLocale().getId(), + "mt translation content changed", + "some comment", + TMTextUnitVariant.Status.APPROVED, + true, + JSR310Migration.dateTimeNow(), + null, + false); + + Mockito.verify(meterRegistry, Mockito.times(1)) + .counter("AiTranslation.review.similarity.medium", Tags.of("locale", "fr-FR")); + } + + @Test + @Transactional + public void testMTReviewMetricsLoggingTranslationUpdatedHighSimilarity() + throws RepositoryNameAlreadyUsedException { + MeterRegistry meterRegistry = Mockito.spy(new SimpleMeterRegistry()); + this.tmService.meterRegistry = meterRegistry; + createTestData(); + + Long textUnitId = + addTextUnitAndCheck( + this.tmId, + this.assetId, + "mtReviewMetricsLogging", + "mt translation content", + "some comment", + "3212c3beb09db681379b7a1ed9f37bfe", + "5f3ca19eb49f50b55326065f4185dadd"); + + Locale targetLocale = this.localeService.findByBcp47Tag("fr-FR"); + + TMTextUnitCurrentVariant tmTextUnitCurrentVariant = + this.tmService.addTMTextUnitCurrentVariant( + textUnitId, + targetLocale.getId(), + "mt translation content", + "some comment", + TMTextUnitVariant.Status.MT_REVIEW_NEEDED, + false); + + this.tmService.addTMTextUnitCurrentVariantWithResult( + tmTextUnitCurrentVariant, + this.tmId, + this.assetId, + textUnitId, + tmTextUnitCurrentVariant.getLocale().getId(), + "mt translations content", + "some comment", + TMTextUnitVariant.Status.APPROVED, + true, + JSR310Migration.dateTimeNow(), + null, + false); + + Mockito.verify(meterRegistry, Mockito.times(1)) + .counter("AiTranslation.review.similarity.high", Tags.of("locale", "fr-FR")); + } + + @Test + @Transactional + public void testMTReviewMetricsLoggingTranslationUpdatedLowSimilarity() + throws RepositoryNameAlreadyUsedException { + MeterRegistry meterRegistry = Mockito.spy(new SimpleMeterRegistry()); + this.tmService.meterRegistry = meterRegistry; + createTestData(); + + Long textUnitId = + addTextUnitAndCheck( + this.tmId, + this.assetId, + "mtReviewMetricsLogging", + "mt translation content", + "some comment", + "3212c3beb09db681379b7a1ed9f37bfe", + "5f3ca19eb49f50b55326065f4185dadd"); + + Locale targetLocale = this.localeService.findByBcp47Tag("fr-FR"); + + TMTextUnitCurrentVariant tmTextUnitCurrentVariant = + this.tmService.addTMTextUnitCurrentVariant( + textUnitId, + targetLocale.getId(), + "mt translation content", + "some comment", + TMTextUnitVariant.Status.MT_REVIEW_NEEDED, + false); + + this.tmService.addTMTextUnitCurrentVariantWithResult( + tmTextUnitCurrentVariant, + this.tmId, + this.assetId, + textUnitId, + tmTextUnitCurrentVariant.getLocale().getId(), + "completely different", + "some comment", + TMTextUnitVariant.Status.APPROVED, + true, + JSR310Migration.dateTimeNow(), + null, + false); + + Mockito.verify(meterRegistry, Mockito.times(1)) + .counter("AiTranslation.review.similarity.low", Tags.of("locale", "fr-FR")); + } + + @Test + @Transactional + public void testMTReviewMetricsLoggingTranslationMatch() + throws RepositoryNameAlreadyUsedException { + MeterRegistry meterRegistry = Mockito.spy(new SimpleMeterRegistry()); + this.tmService.meterRegistry = meterRegistry; + createTestData(); + + Long textUnitId = + addTextUnitAndCheck( + this.tmId, + this.assetId, + "mtReviewMetricsLogging", + "mt translation content", + "some comment", + "3212c3beb09db681379b7a1ed9f37bfe", + "5f3ca19eb49f50b55326065f4185dadd"); + + Locale targetLocale = this.localeService.findByBcp47Tag("fr-FR"); + + TMTextUnitCurrentVariant tmTextUnitCurrentVariant = + this.tmService.addTMTextUnitCurrentVariant( + textUnitId, + targetLocale.getId(), + "mt translation content", + "some comment", + TMTextUnitVariant.Status.MT_REVIEW_NEEDED, + false); + + this.tmService.addTMTextUnitCurrentVariantWithResult( + tmTextUnitCurrentVariant, + this.tmId, + this.assetId, + textUnitId, + tmTextUnitCurrentVariant.getLocale().getId(), + "mt translation content", + "some comment", + TMTextUnitVariant.Status.APPROVED, + true, + JSR310Migration.dateTimeNow(), + null, + false); + + Mockito.verify(meterRegistry, Mockito.times(1)) + .counter("AiTranslation.review.similarity.match", Tags.of("locale", "fr-FR")); + } + + @Test + @Transactional + public void testMTReviewMetricsLoggingTranslationNotApproved() + throws RepositoryNameAlreadyUsedException { + MeterRegistry meterRegistry = Mockito.spy(new SimpleMeterRegistry()); + this.tmService.meterRegistry = meterRegistry; + createTestData(); + + Long textUnitId = + addTextUnitAndCheck( + this.tmId, + this.assetId, + "mtReviewMetricsLogging", + "mt translation content", + "some comment", + "3212c3beb09db681379b7a1ed9f37bfe", + "5f3ca19eb49f50b55326065f4185dadd"); + + Locale targetLocale = this.localeService.findByBcp47Tag("fr-FR"); + + TMTextUnitCurrentVariant tmTextUnitCurrentVariant = + this.tmService.addTMTextUnitCurrentVariant( + textUnitId, + targetLocale.getId(), + "mt translation content", + "some comment", + TMTextUnitVariant.Status.MT_REVIEW_NEEDED, + false); + + this.tmService.addTMTextUnitCurrentVariantWithResult( + tmTextUnitCurrentVariant, + this.tmId, + this.assetId, + textUnitId, + tmTextUnitCurrentVariant.getLocale().getId(), + "mt translation content", + "some comment", + TMTextUnitVariant.Status.REVIEW_NEEDED, + true, + JSR310Migration.dateTimeNow(), + null, + false); + + Mockito.verify(meterRegistry, Mockito.times(0)) + .counter("AiTranslation.review.similarity.match", Tags.of("locale", "fr-FR")); + } }