Skip to content

Commit

Permalink
Added unit test to verify metric logging
Browse files Browse the repository at this point in the history
  • Loading branch information
maallen committed Nov 18, 2024
1 parent a41399e commit c6b66cd
Show file tree
Hide file tree
Showing 2 changed files with 240 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
240 changes: 240 additions & 0 deletions webapp/src/test/java/com/box/l10n/mojito/service/tm/TMServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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"));
}
}

0 comments on commit c6b66cd

Please sign in to comment.