Skip to content

cleanup: remove createTranslator methods in BaseTest #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 35 additions & 36 deletions deepl-java/src/test/java/com/deepl/api/GeneralTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ void testInvalidAuthKey() {

@Test
void testExampleTranslation() throws DeepLException, InterruptedException {
Translator translator = createTranslator();
DeepLClient client = createDeepLClient();

for (Map.Entry<String, String> entry : exampleText.entrySet()) {
String inputText = entry.getValue();
String sourceLang = LanguageCode.removeRegionalVariant(entry.getKey());
TextResult result = translator.translateText(inputText, sourceLang, "en-US");
TextResult result = client.translateText(inputText, sourceLang, "en-US");
Assertions.assertTrue(result.getText().toLowerCase(Locale.ENGLISH).contains("proton"));
Assertions.assertEquals(inputText.length(), result.getBilledCharacters());
}
Expand All @@ -56,10 +56,10 @@ void testExampleTranslation() throws DeepLException, InterruptedException {
})
void testModelType(String modelTypeArg, String expectedModelType)
throws DeepLException, InterruptedException {
Translator translator = createTranslator();
DeepLClient client = createDeepLClient();
String sourceLang = "de";
TextResult result =
translator.translateText(
client.translateText(
exampleText.get(sourceLang),
sourceLang,
"en-US",
Expand All @@ -81,7 +81,7 @@ void testInvalidServerUrl() {
@Test
void testMixedDirectionText() throws DeepLException, InterruptedException {
Assumptions.assumeFalse(isMockServer);
Translator translator = createTranslator();
DeepLClient client = createDeepLClient();
TextTranslationOptions options =
new TextTranslationOptions().setTagHandling("xml").setIgnoreTags(Arrays.asList("xml"));
String arIgnorePart = "<ignore>يجب تجاهل هذا الجزء.</ignore>";
Expand All @@ -91,17 +91,16 @@ void testMixedDirectionText() throws DeepLException, InterruptedException {
String arSentenceWithEnIgnorePart =
"<p>هذه <i>جملة</i> <b>قصيرة</b>. " + enIgnorePart + "هذه جملة أخرى.</p>";

TextResult enResult =
translator.translateText(enSentenceWithArIgnorePart, null, "en-US", options);
TextResult enResult = client.translateText(enSentenceWithArIgnorePart, null, "en-US", options);
Assertions.assertTrue(enResult.getText().contains(arIgnorePart));
TextResult arResult = translator.translateText(arSentenceWithEnIgnorePart, null, "ar", options);
TextResult arResult = client.translateText(arSentenceWithEnIgnorePart, null, "ar", options);
Assertions.assertTrue(arResult.getText().contains(enIgnorePart));
}

@Test
void testUsage() throws DeepLException, InterruptedException {
Translator translator = createTranslator();
Usage usage = translator.getUsage();
DeepLClient client = createDeepLClient();
Usage usage = client.getUsage();
Assertions.assertTrue(usage.toString().contains("Usage this billing period"));
}

Expand All @@ -122,9 +121,9 @@ void testUsageLarge() throws DeepLException, InterruptedException {

@Test
void testGetSourceAndTargetLanguages() throws DeepLException, InterruptedException {
Translator translator = createTranslator();
List<Language> sourceLanguages = translator.getSourceLanguages();
List<Language> targetLanguages = translator.getTargetLanguages();
DeepLClient client = createDeepLClient();
List<Language> sourceLanguages = client.getSourceLanguages();
List<Language> targetLanguages = client.getTargetLanguages();

for (Language language : sourceLanguages) {
if (Objects.equals(language.getCode(), "en")) {
Expand All @@ -146,8 +145,8 @@ void testGetSourceAndTargetLanguages() throws DeepLException, InterruptedExcepti

@Test
void testGetGlossaryLanguages() throws DeepLException, InterruptedException {
Translator translator = createTranslator();
List<GlossaryLanguagePair> glossaryLanguagePairs = translator.getGlossaryLanguages();
DeepLClient client = createDeepLClient();
List<GlossaryLanguagePair> glossaryLanguagePairs = client.getGlossaryLanguages();
Assertions.assertTrue(glossaryLanguagePairs.size() > 0);
for (GlossaryLanguagePair glossaryLanguagePair : glossaryLanguagePairs) {
Assertions.assertTrue(glossaryLanguagePair.getSourceLanguage().length() > 0);
Expand Down Expand Up @@ -185,25 +184,25 @@ Proxy.Type.HTTP, new InetSocketAddress(proxyUrl.getHost(), proxyUrl.getPort())))
void testUsageNoResponse() {
Assumptions.assumeTrue(isMockServer);
// Lower the retry count and timeout for this test
Translator translator =
createTranslator(
DeepLClient client =
createDeepLClient(
new SessionOptions().setNoResponse(2),
new TranslatorOptions().setMaxRetries(0).setTimeout(Duration.ofMillis(1)));

Assertions.assertThrows(ConnectionException.class, translator::getUsage);
Assertions.assertThrows(ConnectionException.class, client::getUsage);
}

@Test
void testTranslateTooManyRequests() {
Assumptions.assumeTrue(isMockServer);
// Lower the retry count and timeout for this test
Translator translator =
createTranslator(
DeepLClient client =
createDeepLClient(
new SessionOptions().setRespondWith429(2), new TranslatorOptions().setMaxRetries(0));

Assertions.assertThrows(
TooManyRequestsException.class,
() -> translator.translateText(exampleText.get("en"), null, "DE"));
() -> client.translateText(exampleText.get("en"), null, "DE"));
}

@Test
Expand All @@ -212,15 +211,15 @@ void testUsageOverrun() throws DeepLException, InterruptedException, IOException
int characterLimit = 20;
int documentLimit = 1;
// Lower the retry count and timeout for this test
Translator translator =
createTranslator(
DeepLClient client =
createDeepLClient(
new SessionOptions()
.setInitCharacterLimit(characterLimit)
.setInitDocumentLimit(documentLimit)
.withRandomAuthKey(),
new TranslatorOptions().setMaxRetries(0).setTimeout(Duration.ofMillis(1)));

Usage usage = translator.getUsage();
Usage usage = client.getUsage();
Assertions.assertNotNull(usage.getCharacter());
Assertions.assertNotNull(usage.getDocument());
Assertions.assertNull(usage.getTeamDocument());
Expand All @@ -235,9 +234,9 @@ void testUsageOverrun() throws DeepLException, InterruptedException, IOException
writeToFile(inputFile, repeatString("a", characterLimit));
File outputFile = createOutputFile();

translator.translateDocument(inputFile, outputFile, null, "de");
client.translateDocument(inputFile, outputFile, null, "de");

usage = translator.getUsage();
usage = client.getUsage();
Assertions.assertTrue(usage.anyLimitReached());
Assertions.assertNotNull(usage.getCharacter());
Assertions.assertNotNull(usage.getDocument());
Expand All @@ -247,15 +246,15 @@ void testUsageOverrun() throws DeepLException, InterruptedException, IOException
Assertions.assertThrows(
IOException.class,
() -> {
translator.translateDocument(inputFile, outputFile, null, "de");
client.translateDocument(inputFile, outputFile, null, "de");
});
outputFile.delete();

DocumentTranslationException thrownDeepLException =
Assertions.assertThrows(
DocumentTranslationException.class,
() -> {
translator.translateDocument(inputFile, outputFile, null, "de");
client.translateDocument(inputFile, outputFile, null, "de");
});
Assertions.assertNull(thrownDeepLException.getHandle());
Assertions.assertEquals(
Expand All @@ -264,23 +263,23 @@ void testUsageOverrun() throws DeepLException, InterruptedException, IOException
Assertions.assertThrows(
QuotaExceededException.class,
() -> {
translator.translateText(exampleText.get("en"), null, "de");
client.translateText(exampleText.get("en"), null, "de");
});
}

@Test
void testUsageTeamDocumentLimit() throws Exception {
Assumptions.assumeTrue(isMockServer);
int teamDocumentLimit = 1;
Translator translator =
createTranslator(
DeepLClient client =
createDeepLClient(
new SessionOptions()
.setInitCharacterLimit(0)
.setInitDocumentLimit(0)
.setInitTeamDocumentLimit(teamDocumentLimit)
.withRandomAuthKey());

Usage usage = translator.getUsage();
Usage usage = client.getUsage();
Assertions.assertNull(usage.getCharacter());
Assertions.assertNull(usage.getDocument());
Assertions.assertNotNull(usage.getTeamDocument());
Expand All @@ -292,9 +291,9 @@ void testUsageTeamDocumentLimit() throws Exception {
writeToFile(inputFile, "a");
File outputFile = createOutputFile();

translator.translateDocument(inputFile, outputFile, null, "de");
client.translateDocument(inputFile, outputFile, null, "de");

usage = translator.getUsage();
usage = client.getUsage();
Assertions.assertTrue(usage.anyLimitReached());
Assertions.assertNotNull(usage.getTeamDocument());
Assertions.assertTrue(usage.getTeamDocument().limitReached());
Expand Down Expand Up @@ -326,8 +325,8 @@ void testUserAgent(
(mock, context) -> {
Mockito.when(mock.openConnection()).thenReturn(con);
})) {
Translator translator = createTranslator(sessionOptions, translatorOptions);
Usage usage = translator.getUsage();
DeepLClient client = createDeepLClient(sessionOptions, translatorOptions);
Usage usage = client.getUsage();
String userAgentHeader = headers.get("User-Agent");
for (String s : requiredStrings) {
Assertions.assertTrue(
Expand Down
Loading