From 1661d9da5741bd1123a3b7a4a7c8024d8b3e6613 Mon Sep 17 00:00:00 2001 From: Tima Zhum Date: Thu, 13 Feb 2025 23:57:40 +0000 Subject: [PATCH] cleanup: remove createTranslator methods in BaseTest Addressing TODO of removing createTranslator() methods in BaseTest and using createDeepLClient() instead --- .../test/java/com/deepl/api/GeneralTest.java | 71 +++++---- .../test/java/com/deepl/api/GlossaryTest.java | 136 ++++++++---------- .../src/test/java/com/deepl/api/TestBase.java | 35 ----- .../com/deepl/api/TranslateDocumentTest.java | 70 ++++----- .../java/com/deepl/api/TranslateTextTest.java | 101 +++++++------ 5 files changed, 183 insertions(+), 230 deletions(-) diff --git a/deepl-java/src/test/java/com/deepl/api/GeneralTest.java b/deepl-java/src/test/java/com/deepl/api/GeneralTest.java index 904c8b0..0d90ab9 100644 --- a/deepl-java/src/test/java/com/deepl/api/GeneralTest.java +++ b/deepl-java/src/test/java/com/deepl/api/GeneralTest.java @@ -37,12 +37,12 @@ void testInvalidAuthKey() { @Test void testExampleTranslation() throws DeepLException, InterruptedException { - Translator translator = createTranslator(); + DeepLClient client = createDeepLClient(); for (Map.Entry 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()); } @@ -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", @@ -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 = "يجب تجاهل هذا الجزء."; @@ -91,17 +91,16 @@ void testMixedDirectionText() throws DeepLException, InterruptedException { String arSentenceWithEnIgnorePart = "

هذه جملة قصيرة. " + enIgnorePart + "هذه جملة أخرى.

"; - 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")); } @@ -122,9 +121,9 @@ void testUsageLarge() throws DeepLException, InterruptedException { @Test void testGetSourceAndTargetLanguages() throws DeepLException, InterruptedException { - Translator translator = createTranslator(); - List sourceLanguages = translator.getSourceLanguages(); - List targetLanguages = translator.getTargetLanguages(); + DeepLClient client = createDeepLClient(); + List sourceLanguages = client.getSourceLanguages(); + List targetLanguages = client.getTargetLanguages(); for (Language language : sourceLanguages) { if (Objects.equals(language.getCode(), "en")) { @@ -146,8 +145,8 @@ void testGetSourceAndTargetLanguages() throws DeepLException, InterruptedExcepti @Test void testGetGlossaryLanguages() throws DeepLException, InterruptedException { - Translator translator = createTranslator(); - List glossaryLanguagePairs = translator.getGlossaryLanguages(); + DeepLClient client = createDeepLClient(); + List glossaryLanguagePairs = client.getGlossaryLanguages(); Assertions.assertTrue(glossaryLanguagePairs.size() > 0); for (GlossaryLanguagePair glossaryLanguagePair : glossaryLanguagePairs) { Assertions.assertTrue(glossaryLanguagePair.getSourceLanguage().length() > 0); @@ -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 @@ -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()); @@ -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()); @@ -247,7 +246,7 @@ void testUsageOverrun() throws DeepLException, InterruptedException, IOException Assertions.assertThrows( IOException.class, () -> { - translator.translateDocument(inputFile, outputFile, null, "de"); + client.translateDocument(inputFile, outputFile, null, "de"); }); outputFile.delete(); @@ -255,7 +254,7 @@ void testUsageOverrun() throws DeepLException, InterruptedException, IOException Assertions.assertThrows( DocumentTranslationException.class, () -> { - translator.translateDocument(inputFile, outputFile, null, "de"); + client.translateDocument(inputFile, outputFile, null, "de"); }); Assertions.assertNull(thrownDeepLException.getHandle()); Assertions.assertEquals( @@ -264,7 +263,7 @@ void testUsageOverrun() throws DeepLException, InterruptedException, IOException Assertions.assertThrows( QuotaExceededException.class, () -> { - translator.translateText(exampleText.get("en"), null, "de"); + client.translateText(exampleText.get("en"), null, "de"); }); } @@ -272,15 +271,15 @@ void testUsageOverrun() throws DeepLException, InterruptedException, IOException 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()); @@ -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()); @@ -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( diff --git a/deepl-java/src/test/java/com/deepl/api/GlossaryTest.java b/deepl-java/src/test/java/com/deepl/api/GlossaryTest.java index 906399b..9f43618 100644 --- a/deepl-java/src/test/java/com/deepl/api/GlossaryTest.java +++ b/deepl-java/src/test/java/com/deepl/api/GlossaryTest.java @@ -35,23 +35,22 @@ void TestGlossaryEntries() { @Test void testGlossaryCreate() throws Exception { - Translator translator = createTranslator(); - try (GlossaryCleanupUtility cleanup = new GlossaryCleanupUtility(translator)) { + DeepLClient client = createDeepLClient(); + try (GlossaryCleanupUtility cleanup = new GlossaryCleanupUtility(client)) { GlossaryEntries entries = new GlossaryEntries(Collections.singletonMap("Hello", "Hallo")); System.out.println(entries); for (Map.Entry entry : entries.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } String glossaryName = cleanup.getGlossaryName(); - GlossaryInfo glossary = - translator.createGlossary(glossaryName, sourceLang, targetLang, entries); + GlossaryInfo glossary = client.createGlossary(glossaryName, sourceLang, targetLang, entries); Assertions.assertEquals(glossaryName, glossary.getName()); Assertions.assertEquals(sourceLang, glossary.getSourceLang()); Assertions.assertEquals(targetLang, glossary.getTargetLang()); Assertions.assertEquals(1, glossary.getEntryCount()); - GlossaryInfo getResult = translator.getGlossary(glossary.getGlossaryId()); + GlossaryInfo getResult = client.getGlossary(glossary.getGlossaryId()); Assertions.assertEquals(getResult.getName(), glossary.getName()); Assertions.assertEquals(getResult.getSourceLang(), glossary.getSourceLang()); Assertions.assertEquals(getResult.getTargetLang(), glossary.getTargetLang()); @@ -62,8 +61,8 @@ void testGlossaryCreate() throws Exception { @Test void testGlossaryCreateLarge() throws Exception { - Translator translator = createTranslator(); - try (GlossaryCleanupUtility cleanup = new GlossaryCleanupUtility(translator)) { + DeepLClient client = createDeepLClient(); + try (GlossaryCleanupUtility cleanup = new GlossaryCleanupUtility(client)) { String glossaryName = cleanup.getGlossaryName(); Map entryPairs = new HashMap<>(); @@ -72,8 +71,7 @@ void testGlossaryCreateLarge() throws Exception { } GlossaryEntries entries = new GlossaryEntries(entryPairs); Assertions.assertTrue(entries.toTsv().length() > 100000); - GlossaryInfo glossary = - translator.createGlossary(glossaryName, sourceLang, targetLang, entries); + GlossaryInfo glossary = client.createGlossary(glossaryName, sourceLang, targetLang, entries); Assertions.assertEquals(glossaryName, glossary.getName()); Assertions.assertEquals(sourceLang, glossary.getSourceLang()); @@ -84,8 +82,8 @@ void testGlossaryCreateLarge() throws Exception { @Test void testGlossaryCreateCsv() throws Exception { - Translator translator = createTranslator(); - try (GlossaryCleanupUtility cleanup = new GlossaryCleanupUtility(translator)) { + DeepLClient client = createDeepLClient(); + try (GlossaryCleanupUtility cleanup = new GlossaryCleanupUtility(client)) { String glossaryName = cleanup.getGlossaryName(); Map expectedEntries = new HashMap<>(); expectedEntries.put("sourceEntry1", "targetEntry1"); @@ -95,35 +93,34 @@ void testGlossaryCreateCsv() throws Exception { "sourceEntry1,targetEntry1,en,de\n\"source\"\"Entry\",\"target,Entry\",en,de"; GlossaryInfo glossary = - translator.createGlossaryFromCsv(glossaryName, sourceLang, targetLang, csvContent); + client.createGlossaryFromCsv(glossaryName, sourceLang, targetLang, csvContent); - GlossaryEntries entries = translator.getGlossaryEntries(glossary); + GlossaryEntries entries = client.getGlossaryEntries(glossary); Assertions.assertEquals(expectedEntries, entries); } } @Test void testGlossaryCreateInvalid() throws Exception { - Translator translator = createTranslator(); - try (GlossaryCleanupUtility cleanup = new GlossaryCleanupUtility(translator)) { + DeepLClient client = createDeepLClient(); + try (GlossaryCleanupUtility cleanup = new GlossaryCleanupUtility(client)) { String glossaryName = cleanup.getGlossaryName(); Assertions.assertThrows( - Exception.class, - () -> translator.createGlossary("", sourceLang, targetLang, testEntries)); + Exception.class, () -> client.createGlossary("", sourceLang, targetLang, testEntries)); Assertions.assertThrows( - Exception.class, () -> translator.createGlossary(glossaryName, "en", "xx", testEntries)); + Exception.class, () -> client.createGlossary(glossaryName, "en", "xx", testEntries)); } } @Test void testGlossaryGet() throws Exception { - Translator translator = createTranslator(); - try (GlossaryCleanupUtility cleanup = new GlossaryCleanupUtility(translator)) { + DeepLClient client = createDeepLClient(); + try (GlossaryCleanupUtility cleanup = new GlossaryCleanupUtility(client)) { String glossaryName = cleanup.getGlossaryName(); GlossaryInfo createdGlossary = - translator.createGlossary(glossaryName, sourceLang, targetLang, testEntries); + client.createGlossary(glossaryName, sourceLang, targetLang, testEntries); - GlossaryInfo glossary = translator.getGlossary(createdGlossary.getGlossaryId()); + GlossaryInfo glossary = client.getGlossary(createdGlossary.getGlossaryId()); Assertions.assertEquals(createdGlossary.getGlossaryId(), glossary.getGlossaryId()); Assertions.assertEquals(glossaryName, glossary.getName()); Assertions.assertEquals(sourceLang, glossary.getSourceLang()); @@ -131,15 +128,15 @@ void testGlossaryGet() throws Exception { Assertions.assertEquals(createdGlossary.getCreationTime(), glossary.getCreationTime()); Assertions.assertEquals(testEntries.size(), glossary.getEntryCount()); } - Assertions.assertThrows(DeepLException.class, () -> translator.getGlossary(invalidGlossaryId)); + Assertions.assertThrows(DeepLException.class, () -> client.getGlossary(invalidGlossaryId)); Assertions.assertThrows( - GlossaryNotFoundException.class, () -> translator.getGlossary(nonexistentGlossaryId)); + GlossaryNotFoundException.class, () -> client.getGlossary(nonexistentGlossaryId)); } @Test void testGlossaryGetEntries() throws Exception { - Translator translator = createTranslator(); - try (GlossaryCleanupUtility cleanup = new GlossaryCleanupUtility(translator)) { + DeepLClient client = createDeepLClient(); + try (GlossaryCleanupUtility cleanup = new GlossaryCleanupUtility(client)) { String glossaryName = cleanup.getGlossaryName(); GlossaryEntries entries = new GlossaryEntries(); entries.put("Apple", "Apfel"); @@ -149,27 +146,25 @@ void testGlossaryGetEntries() throws Exception { entries.put("\uD83E\uDEA8", "\uD83E\uDEB5"); GlossaryInfo createdGlossary = - translator.createGlossary(glossaryName, sourceLang, targetLang, entries); - Assertions.assertEquals(entries, translator.getGlossaryEntries(createdGlossary)); - Assertions.assertEquals( - entries, translator.getGlossaryEntries(createdGlossary.getGlossaryId())); + client.createGlossary(glossaryName, sourceLang, targetLang, entries); + Assertions.assertEquals(entries, client.getGlossaryEntries(createdGlossary)); + Assertions.assertEquals(entries, client.getGlossaryEntries(createdGlossary.getGlossaryId())); } Assertions.assertThrows( - DeepLException.class, () -> translator.getGlossaryEntries(invalidGlossaryId)); + DeepLException.class, () -> client.getGlossaryEntries(invalidGlossaryId)); Assertions.assertThrows( - GlossaryNotFoundException.class, - () -> translator.getGlossaryEntries(nonexistentGlossaryId)); + GlossaryNotFoundException.class, () -> client.getGlossaryEntries(nonexistentGlossaryId)); } @Test void testGlossaryList() throws Exception { - Translator translator = createTranslator(); - try (GlossaryCleanupUtility cleanup = new GlossaryCleanupUtility(translator)) { + DeepLClient client = createDeepLClient(); + try (GlossaryCleanupUtility cleanup = new GlossaryCleanupUtility(client)) { String glossaryName = cleanup.getGlossaryName(); - translator.createGlossary(glossaryName, sourceLang, targetLang, testEntries); + client.createGlossary(glossaryName, sourceLang, targetLang, testEntries); - List glossaries = translator.listGlossaries(); + List glossaries = client.listGlossaries(); Assertions.assertTrue( glossaries.stream() .anyMatch((glossaryInfo -> Objects.equals(glossaryInfo.getName(), glossaryName)))); @@ -178,27 +173,26 @@ void testGlossaryList() throws Exception { @Test void testGlossaryDelete() throws Exception { - Translator translator = createTranslator(); - try (GlossaryCleanupUtility cleanup = new GlossaryCleanupUtility(translator)) { + DeepLClient client = createDeepLClient(); + try (GlossaryCleanupUtility cleanup = new GlossaryCleanupUtility(client)) { String glossaryName = cleanup.getGlossaryName(); GlossaryInfo glossary = - translator.createGlossary(glossaryName, sourceLang, targetLang, testEntries); + client.createGlossary(glossaryName, sourceLang, targetLang, testEntries); - translator.deleteGlossary(glossary); + client.deleteGlossary(glossary); Assertions.assertThrows( - GlossaryNotFoundException.class, () -> translator.deleteGlossary(glossary)); + GlossaryNotFoundException.class, () -> client.deleteGlossary(glossary)); + Assertions.assertThrows(DeepLException.class, () -> client.deleteGlossary(invalidGlossaryId)); Assertions.assertThrows( - DeepLException.class, () -> translator.deleteGlossary(invalidGlossaryId)); - Assertions.assertThrows( - GlossaryNotFoundException.class, () -> translator.deleteGlossary(nonexistentGlossaryId)); + GlossaryNotFoundException.class, () -> client.deleteGlossary(nonexistentGlossaryId)); } } @Test void testGlossaryTranslateTextSentence() throws Exception { - Translator translator = createTranslator(); - try (GlossaryCleanupUtility cleanup = new GlossaryCleanupUtility(translator)) { + DeepLClient client = createDeepLClient(); + try (GlossaryCleanupUtility cleanup = new GlossaryCleanupUtility(client)) { String glossaryName = cleanup.getGlossaryName(); GlossaryEntries entries = new GlossaryEntries() { @@ -209,11 +203,10 @@ void testGlossaryTranslateTextSentence() throws Exception { }; String inputText = "The artist was awarded a prize."; - GlossaryInfo glossary = - translator.createGlossary(glossaryName, sourceLang, targetLang, entries); + GlossaryInfo glossary = client.createGlossary(glossaryName, sourceLang, targetLang, entries); TextResult result = - translator.translateText( + client.translateText( inputText, sourceLang, targetLang, @@ -225,7 +218,7 @@ void testGlossaryTranslateTextSentence() throws Exception { // It is also possible to specify GlossaryInfo result = - translator.translateText( + client.translateText( inputText, sourceLang, targetLang, @@ -239,9 +232,9 @@ void testGlossaryTranslateTextSentence() throws Exception { @Test void testGlossaryTranslateTextBasic() throws Exception { - Translator translator = createTranslator(); - try (GlossaryCleanupUtility cleanupEnDe = new GlossaryCleanupUtility(translator, "EnDe"); - GlossaryCleanupUtility cleanupDeEn = new GlossaryCleanupUtility(translator, "DeEn")) { + DeepLClient client = createDeepLClient(); + try (GlossaryCleanupUtility cleanupEnDe = new GlossaryCleanupUtility(client, "EnDe"); + GlossaryCleanupUtility cleanupDeEn = new GlossaryCleanupUtility(client, "DeEn")) { String glossaryNameEnDe = cleanupEnDe.getGlossaryName(); String glossaryNameDeEn = cleanupDeEn.getGlossaryName(); List textsEn = @@ -266,18 +259,18 @@ void testGlossaryTranslateTextBasic() throws Exception { } GlossaryInfo glossaryEnDe = - translator.createGlossary(glossaryNameEnDe, "en", "de", glossaryEntriesEnDe); + client.createGlossary(glossaryNameEnDe, "en", "de", glossaryEntriesEnDe); GlossaryInfo glossaryDeEn = - translator.createGlossary(glossaryNameDeEn, "de", "en", glossaryEntriesDeEn); + client.createGlossary(glossaryNameDeEn, "de", "en", glossaryEntriesDeEn); List result = - translator.translateText( + client.translateText( textsEn, "en", "de", new TextTranslationOptions().setGlossary(glossaryEnDe)); Assertions.assertArrayEquals( textsDe.toArray(), result.stream().map(TextResult::getText).toArray()); result = - translator.translateText( + client.translateText( textsDe, "de", "en-US", @@ -289,8 +282,8 @@ void testGlossaryTranslateTextBasic() throws Exception { @Test void testGlossaryTranslateDocument() throws Exception { - Translator translator = createTranslator(); - try (GlossaryCleanupUtility cleanup = new GlossaryCleanupUtility(translator)) { + DeepLClient client = createDeepLClient(); + try (GlossaryCleanupUtility cleanup = new GlossaryCleanupUtility(client)) { String glossaryName = cleanup.getGlossaryName(); File inputFile = createInputFile("artist\nprize"); File outputFile = createOutputFile(); @@ -303,10 +296,9 @@ void testGlossaryTranslateDocument() throws Exception { } }; - GlossaryInfo glossary = - translator.createGlossary(glossaryName, sourceLang, targetLang, entries); + GlossaryInfo glossary = client.createGlossary(glossaryName, sourceLang, targetLang, entries); - translator.translateDocument( + client.translateDocument( inputFile, outputFile, sourceLang, @@ -315,7 +307,7 @@ void testGlossaryTranslateDocument() throws Exception { Assertions.assertEquals(expectedOutput, readFromFile(outputFile)); boolean ignored = outputFile.delete(); - translator.translateDocument( + client.translateDocument( inputFile, outputFile, sourceLang, @@ -327,22 +319,20 @@ void testGlossaryTranslateDocument() throws Exception { @Test void testGlossaryTranslateTextInvalid() throws Exception { - Translator translator = createTranslator(); - try (GlossaryCleanupUtility cleanupEnDe = new GlossaryCleanupUtility(translator, "EnDe"); - GlossaryCleanupUtility cleanupDeEn = new GlossaryCleanupUtility(translator, "DeEn")) { + DeepLClient client = createDeepLClient(); + try (GlossaryCleanupUtility cleanupEnDe = new GlossaryCleanupUtility(client, "EnDe"); + GlossaryCleanupUtility cleanupDeEn = new GlossaryCleanupUtility(client, "DeEn")) { String glossaryNameEnDe = cleanupEnDe.getGlossaryName(); String glossaryNameDeEn = cleanupDeEn.getGlossaryName(); - GlossaryInfo glossaryEnDe = - translator.createGlossary(glossaryNameEnDe, "en", "de", testEntries); - GlossaryInfo glossaryDeEn = - translator.createGlossary(glossaryNameDeEn, "de", "en", testEntries); + GlossaryInfo glossaryEnDe = client.createGlossary(glossaryNameEnDe, "en", "de", testEntries); + GlossaryInfo glossaryDeEn = client.createGlossary(glossaryNameDeEn, "de", "en", testEntries); IllegalArgumentException exception = Assertions.assertThrows( IllegalArgumentException.class, () -> - translator.translateText( + client.translateText( "test", null, "de", new TextTranslationOptions().setGlossary(glossaryEnDe))); Assertions.assertTrue(exception.getMessage().contains("sourceLang is required")); @@ -350,7 +340,7 @@ void testGlossaryTranslateTextInvalid() throws Exception { Assertions.assertThrows( IllegalArgumentException.class, () -> - translator.translateText( + client.translateText( "test", "de", "en", new TextTranslationOptions().setGlossary(glossaryDeEn))); Assertions.assertTrue(exception.getMessage().contains("targetLang=\"en\" is not allowed")); } diff --git a/deepl-java/src/test/java/com/deepl/api/TestBase.java b/deepl-java/src/test/java/com/deepl/api/TestBase.java index 8873335..a130328 100644 --- a/deepl-java/src/test/java/com/deepl/api/TestBase.java +++ b/deepl-java/src/test/java/com/deepl/api/TestBase.java @@ -90,41 +90,6 @@ protected TestBase() { tempDir = createTempDir(); } - // TODO: Delete `createTranslator` methods, replace with `createDeepLClient` - protected Translator createTranslator() { - SessionOptions sessionOptions = new SessionOptions(); - return createTranslator(sessionOptions); - } - - protected Translator createTranslator(SessionOptions sessionOptions) { - TranslatorOptions translatorOptions = new TranslatorOptions(); - return createTranslator(sessionOptions, translatorOptions); - } - - protected Translator createTranslator( - SessionOptions sessionOptions, TranslatorOptions translatorOptions) { - Map headers = sessionOptions.createSessionHeaders(); - - if (translatorOptions.getServerUrl() == null) { - translatorOptions.setServerUrl(serverUrl); - } - - if (translatorOptions.getHeaders() != null) { - headers.putAll(translatorOptions.getHeaders()); - } - translatorOptions.setHeaders(headers); - - String authKey = sessionOptions.randomAuthKey ? UUID.randomUUID().toString() : TestBase.authKey; - - try { - return new Translator(authKey, translatorOptions); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - System.exit(1); - return null; - } - } - protected DeepLClient createDeepLClient() { SessionOptions sessionOptions = new SessionOptions(); return createDeepLClient(sessionOptions); diff --git a/deepl-java/src/test/java/com/deepl/api/TranslateDocumentTest.java b/deepl-java/src/test/java/com/deepl/api/TranslateDocumentTest.java index 54e789c..4e1f166 100644 --- a/deepl-java/src/test/java/com/deepl/api/TranslateDocumentTest.java +++ b/deepl-java/src/test/java/com/deepl/api/TranslateDocumentTest.java @@ -14,25 +14,25 @@ public class TranslateDocumentTest extends TestBase { @Test void testTranslateDocument() throws Exception { - Translator translator = createTranslator(); + DeepLClient client = createDeepLClient(); File inputFile = createInputFile(); File outputFile = createOutputFile(); - translator.translateDocument(inputFile, outputFile, "en", "de"); + client.translateDocument(inputFile, outputFile, "en", "de"); Assertions.assertEquals(exampleOutput, readFromFile(outputFile)); // Test with output path occupied Assertions.assertThrows( IOException.class, () -> { - translator.translateDocument(inputFile, outputFile, "en", "de"); + client.translateDocument(inputFile, outputFile, "en", "de"); }); } @Test void testTranslateDocumentFailsWithOutputOccupied() throws Exception { - Translator translator = createTranslator(); + DeepLClient client = createDeepLClient(); File inputFile = createInputFile(); File outputFile = createOutputFile(); @@ -42,52 +42,52 @@ void testTranslateDocumentFailsWithOutputOccupied() throws Exception { Assertions.assertThrows( IOException.class, () -> { - translator.translateDocument(inputFile, outputFile, "en", "de"); + client.translateDocument(inputFile, outputFile, "en", "de"); }); } @Test void testTranslateDocumentWithRetry() throws Exception { Assumptions.assumeTrue(isMockServer); - Translator translator = - createTranslator( + DeepLClient client = + createDeepLClient( new SessionOptions().setNoResponse(1), new TranslatorOptions().setTimeout(Duration.ofSeconds(1))); File outputFile = createOutputFile(); - translator.translateDocument(createInputFile(), outputFile, "en", "de"); + client.translateDocument(createInputFile(), outputFile, "en", "de"); Assertions.assertEquals(exampleOutput, readFromFile(outputFile)); } @Test void testTranslateDocumentWithWaiting() throws Exception { Assumptions.assumeTrue(isMockServer); - Translator translator = - createTranslator( + DeepLClient client = + createDeepLClient( new SessionOptions() .setDocumentTranslateTime(Duration.ofSeconds(2)) .setDocumentQueueTime(Duration.ofSeconds(2))); File outputFile = createOutputFile(); - translator.translateDocument(createInputFile(), outputFile, "en", "de"); + client.translateDocument(createInputFile(), outputFile, "en", "de"); Assertions.assertEquals(exampleOutput, readFromFile(outputFile)); } @Test void testTranslateLargeDocument() throws Exception { Assumptions.assumeTrue(isMockServer); - Translator translator = createTranslator(); + DeepLClient client = createDeepLClient(); File inputFile = createInputFile(exampleLargeInput); File outputFile = createOutputFile(); - translator.translateDocument(inputFile, outputFile, "en", "de"); + client.translateDocument(inputFile, outputFile, "en", "de"); Assertions.assertEquals(exampleLargeOutput, readFromFile(outputFile)); } @Test void testTranslateDocumentFormality() throws Exception { - Translator translator = createTranslator(); + DeepLClient client = createDeepLClient(); File inputFile = createInputFile("How are you?"); File outputFile = createOutputFile(); - translator.translateDocument( + client.translateDocument( inputFile, outputFile, "en", @@ -99,7 +99,7 @@ void testTranslateDocumentFormality() throws Exception { outputFile.delete(); - translator.translateDocument( + client.translateDocument( inputFile, outputFile, "en", @@ -112,7 +112,7 @@ void testTranslateDocumentFormality() throws Exception { @Test void testTranslateDocumentFailureDuringTranslation() throws Exception { - Translator translator = createTranslator(); + DeepLClient client = createDeepLClient(); // Translating text from DE to DE will trigger error File inputFile = createInputFile(exampleText.get("de")); @@ -122,14 +122,14 @@ void testTranslateDocumentFailureDuringTranslation() throws Exception { Assertions.assertThrows( DocumentTranslationException.class, () -> { - translator.translateDocument(inputFile, outputFile, null, "de"); + client.translateDocument(inputFile, outputFile, null, "de"); }); Assertions.assertTrue(exception.getMessage().contains("Source and target language")); } @Test void testInvalidDocument() throws Exception { - Translator translator = createTranslator(); + DeepLClient client = createDeepLClient(); File inputFile = new File(tempDir + "/document.xyz"); writeToFile(inputFile, exampleText.get("en")); File outputFile = new File(tempDir + "/output_document.xyz"); @@ -139,7 +139,7 @@ void testInvalidDocument() throws Exception { Assertions.assertThrows( DocumentTranslationException.class, () -> { - translator.translateDocument(inputFile, outputFile, "en", "de"); + client.translateDocument(inputFile, outputFile, "en", "de"); }); Assertions.assertNull(exception.getHandle()); } @@ -148,14 +148,14 @@ void testInvalidDocument() throws Exception { void testTranslateDocumentLowLevel() throws Exception { Assumptions.assumeTrue(isMockServer); // Set a small document queue time to attempt downloading a queued document - Translator translator = - createTranslator(new SessionOptions().setDocumentQueueTime(Duration.ofMillis(100))); + DeepLClient client = + createDeepLClient(new SessionOptions().setDocumentQueueTime(Duration.ofMillis(100))); File inputFile = createInputFile(); File outputFile = createOutputFile(); - final DocumentHandle handle = translator.translateDocumentUpload(inputFile, "en", "de"); + final DocumentHandle handle = client.translateDocumentUpload(inputFile, "en", "de"); - DocumentStatus status = translator.translateDocumentStatus(handle); + DocumentStatus status = client.translateDocumentStatus(handle); Assertions.assertEquals(handle.getDocumentId(), status.getDocumentId()); Assertions.assertTrue(status.ok()); Assertions.assertFalse(status.done()); @@ -164,24 +164,24 @@ void testTranslateDocumentLowLevel() throws Exception { String documentId = handle.getDocumentId(); String documentKey = handle.getDocumentKey(); DocumentHandle recreatedHandle = new DocumentHandle(documentId, documentKey); - status = translator.translateDocumentStatus(recreatedHandle); + status = client.translateDocumentStatus(recreatedHandle); Assertions.assertTrue(status.ok()); while (status.ok() && !status.done()) { Thread.sleep(200); - status = translator.translateDocumentStatus(recreatedHandle); + status = client.translateDocumentStatus(recreatedHandle); } Assertions.assertTrue(status.ok() && status.done()); - translator.translateDocumentDownload(recreatedHandle, outputFile); + client.translateDocumentDownload(recreatedHandle, outputFile); Assertions.assertEquals(exampleOutput, readFromFile(outputFile)); } @Test void testTranslateDocumentRequestFields() throws Exception { Assumptions.assumeTrue(isMockServer); - Translator translator = - createTranslator( + DeepLClient client = + createDeepLClient( new SessionOptions() .setDocumentTranslateTime(Duration.ofSeconds(2)) .setDocumentQueueTime(Duration.ofSeconds(2))); @@ -189,13 +189,13 @@ void testTranslateDocumentRequestFields() throws Exception { File outputFile = createOutputFile(); long timeBefore = new Date().getTime(); - DocumentHandle handle = translator.translateDocumentUpload(inputFile, "en", "de"); - DocumentStatus status = translator.translateDocumentStatus(handle); + DocumentHandle handle = client.translateDocumentUpload(inputFile, "en", "de"); + DocumentStatus status = client.translateDocumentStatus(handle); Assertions.assertTrue(status.ok()); Assertions.assertTrue( status.getSecondsRemaining() == null || status.getSecondsRemaining() >= 0); - status = translator.translateDocumentWaitUntilDone(handle); - translator.translateDocumentDownload(handle, outputFile); + status = client.translateDocumentWaitUntilDone(handle); + client.translateDocumentDownload(handle, outputFile); long timeAfter = new Date().getTime(); Assertions.assertEquals(exampleInput.length(), status.getBilledCharacters()); @@ -205,14 +205,14 @@ void testTranslateDocumentRequestFields() throws Exception { @Test void testRecreateDocumentHandleInvalid() { - Translator translator = createTranslator(); + DeepLClient client = createDeepLClient(); String documentId = repeatString("12AB", 8); String documentKey = repeatString("CD34", 16); DocumentHandle handle = new DocumentHandle(documentId, documentKey); Assertions.assertThrows( NotFoundException.class, () -> { - translator.translateDocumentStatus(handle); + client.translateDocumentStatus(handle); }); } } diff --git a/deepl-java/src/test/java/com/deepl/api/TranslateTextTest.java b/deepl-java/src/test/java/com/deepl/api/TranslateTextTest.java index 7b9b222..9d86916 100644 --- a/deepl-java/src/test/java/com/deepl/api/TranslateTextTest.java +++ b/deepl-java/src/test/java/com/deepl/api/TranslateTextTest.java @@ -13,8 +13,8 @@ public class TranslateTextTest extends TestBase { @Test void testSingleText() throws DeepLException, InterruptedException { - Translator translator = createTranslator(); - TextResult result = translator.translateText(exampleText.get("en"), null, LanguageCode.German); + DeepLClient client = createDeepLClient(); + TextResult result = client.translateText(exampleText.get("en"), null, LanguageCode.German); Assertions.assertEquals(exampleText.get("de"), result.getText()); Assertions.assertEquals("en", result.getDetectedSourceLanguage()); Assertions.assertEquals(exampleText.get("en").length(), result.getBilledCharacters()); @@ -22,11 +22,11 @@ void testSingleText() throws DeepLException, InterruptedException { @Test void testTextArray() throws DeepLException, InterruptedException { - Translator translator = createTranslator(); + DeepLClient client = createDeepLClient(); List texts = new ArrayList<>(); texts.add(exampleText.get("fr")); texts.add(exampleText.get("en")); - List result = translator.translateText(texts, null, LanguageCode.German); + List result = client.translateText(texts, null, LanguageCode.German); Assertions.assertEquals(exampleText.get("de"), result.get(0).getText()); Assertions.assertEquals(exampleText.get("de"), result.get(1).getText()); } @@ -39,12 +39,12 @@ void testSourceLang() throws DeepLException, InterruptedException { Assertions.assertEquals("en", result.getDetectedSourceLanguage()); }; - Translator translator = createTranslator(); - checkResult.accept(translator.translateText(exampleText.get("en"), null, "DE")); - checkResult.accept(translator.translateText(exampleText.get("en"), "En", "DE")); - checkResult.accept(translator.translateText(exampleText.get("en"), "en", "DE")); + DeepLClient client = createDeepLClient(); + checkResult.accept(client.translateText(exampleText.get("en"), null, "DE")); + checkResult.accept(client.translateText(exampleText.get("en"), "En", "DE")); + checkResult.accept(client.translateText(exampleText.get("en"), "en", "DE")); - List sourceLanguages = translator.getSourceLanguages(); + List sourceLanguages = client.getSourceLanguages(); Language sourceLanguageEn = sourceLanguages.stream() .filter((language -> Objects.equals(language.getCode(), "en"))) @@ -58,7 +58,7 @@ void testSourceLang() throws DeepLException, InterruptedException { Assertions.assertNotNull(sourceLanguageEn); Assertions.assertNotNull(sourceLanguageDe); checkResult.accept( - translator.translateText(exampleText.get("en"), sourceLanguageEn, sourceLanguageDe)); + client.translateText(exampleText.get("en"), sourceLanguageEn, sourceLanguageDe)); } @Test @@ -69,42 +69,42 @@ void testTargetLang() throws DeepLException, InterruptedException { Assertions.assertEquals("en", result.getDetectedSourceLanguage()); }; - Translator translator = createTranslator(); - checkResult.accept(translator.translateText(exampleText.get("en"), null, "De")); - checkResult.accept(translator.translateText(exampleText.get("en"), null, "de")); - checkResult.accept(translator.translateText(exampleText.get("en"), null, "DE")); + DeepLClient client = createDeepLClient(); + checkResult.accept(client.translateText(exampleText.get("en"), null, "De")); + checkResult.accept(client.translateText(exampleText.get("en"), null, "de")); + checkResult.accept(client.translateText(exampleText.get("en"), null, "DE")); - List targetLanguages = translator.getTargetLanguages(); + List targetLanguages = client.getTargetLanguages(); Language targetLanguageDe = targetLanguages.stream() .filter((language -> Objects.equals(language.getCode(), "de"))) .findFirst() .orElse(null); Assertions.assertNotNull(targetLanguageDe); - checkResult.accept(translator.translateText(exampleText.get("en"), null, targetLanguageDe)); + checkResult.accept(client.translateText(exampleText.get("en"), null, targetLanguageDe)); // Check that en and pt as target languages throw an exception Assertions.assertThrows( IllegalArgumentException.class, () -> { - translator.translateText(exampleText.get("de"), null, "en"); + client.translateText(exampleText.get("de"), null, "en"); }); Assertions.assertThrows( IllegalArgumentException.class, () -> { - translator.translateText(exampleText.get("de"), null, "pt"); + client.translateText(exampleText.get("de"), null, "pt"); }); } @Test void testInvalidLanguage() { - Translator translator = createTranslator(); + DeepLClient client = createDeepLClient(); DeepLException thrown; thrown = Assertions.assertThrows( DeepLException.class, () -> { - translator.translateText(exampleText.get("en"), null, "XX"); + client.translateText(exampleText.get("en"), null, "XX"); }); Assertions.assertTrue(thrown.getMessage().contains("target_lang")); @@ -112,7 +112,7 @@ void testInvalidLanguage() { Assertions.assertThrows( DeepLException.class, () -> { - translator.translateText(exampleText.get("en"), "XX", "de"); + client.translateText(exampleText.get("en"), "XX", "de"); }); Assertions.assertTrue(thrown.getMessage().contains("source_lang")); } @@ -120,13 +120,13 @@ void testInvalidLanguage() { @Test void testTranslateWithRetries() throws DeepLException, InterruptedException { Assumptions.assumeTrue(isMockServer); - Translator translator = createTranslator(new SessionOptions().setRespondWith429(2)); + DeepLClient client = createDeepLClient(new SessionOptions().setRespondWith429(2)); long timeBefore = new Date().getTime(); List texts = new ArrayList<>(); texts.add(exampleText.get("en")); texts.add(exampleText.get("ja")); - List result = translator.translateText(texts, null, "de"); + List result = client.translateText(texts, null, "de"); long timeAfter = new Date().getTime(); Assertions.assertEquals(2, result.size()); @@ -139,18 +139,18 @@ void testTranslateWithRetries() throws DeepLException, InterruptedException { @Test void testFormality() throws DeepLException, InterruptedException { - Translator translator = createTranslator(); + DeepLClient client = createDeepLClient(); TextResult result; result = - translator.translateText( + client.translateText( "How are you?", null, "de", new TextTranslationOptions().setFormality(Formality.Less)); if (!isMockServer) { Assertions.assertEquals("Wie geht es dir?", result.getText()); } result = - translator.translateText( + client.translateText( "How are you?", null, "de", @@ -160,14 +160,14 @@ void testFormality() throws DeepLException, InterruptedException { } result = - translator.translateText( + client.translateText( "How are you?", null, "de", new TextTranslationOptions().setFormality(Formality.More)); if (!isMockServer) { Assertions.assertEquals("Wie geht es Ihnen?", result.getText()); } result = - translator.translateText( + client.translateText( "How are you?", null, "de", @@ -177,7 +177,7 @@ void testFormality() throws DeepLException, InterruptedException { } result = - translator.translateText( + client.translateText( "How are you?", null, "de", @@ -192,13 +192,13 @@ void testContext() throws DeepLException, InterruptedException { // In German, "scharf" can mean: // - spicy/hot when referring to food, or // - sharp when referring to other objects such as a knife (Messer). - Translator translator = createTranslator(); + DeepLClient client = createDeepLClient(); String text = "Das ist scharf!"; - translator.translateText(text, null, "de"); + client.translateText(text, null, "de"); // Result: "That is hot!" - translator.translateText( + client.translateText( text, null, "de", new TextTranslationOptions().setContext("Das ist ein Messer.")); // Result: "That is sharp!" } @@ -207,21 +207,21 @@ void testContext() throws DeepLException, InterruptedException { void testSplitSentences() throws DeepLException, InterruptedException { Assumptions.assumeTrue(isMockServer); - Translator translator = createTranslator(); + DeepLClient client = createDeepLClient(); String text = "If the implementation is hard to explain, it's a bad idea.\nIf the implementation is easy to explain, it may be a good idea."; - translator.translateText( + client.translateText( text, null, "de", new TextTranslationOptions().setSentenceSplittingMode(SentenceSplittingMode.Off)); - translator.translateText( + client.translateText( text, null, "de", new TextTranslationOptions().setSentenceSplittingMode(SentenceSplittingMode.All)); - translator.translateText( + client.translateText( text, null, "de", @@ -232,13 +232,13 @@ void testSplitSentences() throws DeepLException, InterruptedException { void testPreserveFormatting() throws DeepLException, InterruptedException { Assumptions.assumeTrue(isMockServer); - Translator translator = createTranslator(); - translator.translateText( + DeepLClient client = createDeepLClient(); + client.translateText( exampleText.get("en"), null, "de", new TextTranslationOptions().setPreserveFormatting(true)); - translator.translateText( + client.translateText( exampleText.get("en"), null, "de", @@ -247,7 +247,7 @@ void testPreserveFormatting() throws DeepLException, InterruptedException { @Test void testTagHandlingXML() throws DeepLException, InterruptedException { - Translator translator = createTranslator(); + DeepLClient client = createDeepLClient(); String text = "A document's title" + "" @@ -260,7 +260,7 @@ void testTagHandlingXML() throws DeepLException, InterruptedException { + "" + ""; TextResult result = - translator.translateText( + client.translateText( text, null, "de", @@ -279,7 +279,7 @@ void testTagHandlingXML() throws DeepLException, InterruptedException { @Test void testTagHandlingHTML() throws DeepLException, InterruptedException { - Translator translator = createTranslator(); + DeepLClient client = createDeepLClient(); String text = "" + "" @@ -290,8 +290,7 @@ void testTagHandlingHTML() throws DeepLException, InterruptedException { + ""; TextResult result = - translator.translateText( - text, null, "de", new TextTranslationOptions().setTagHandling("html")); + client.translateText(text, null, "de", new TextTranslationOptions().setTagHandling("html")); if (!isMockServer) { Assertions.assertTrue(result.getText().contains("

Meine erste Überschrift

")); Assertions.assertTrue( @@ -301,32 +300,32 @@ void testTagHandlingHTML() throws DeepLException, InterruptedException { @Test void testEmptyText() { - Translator translator = createTranslator(); + DeepLClient client = createDeepLClient(); Assertions.assertThrows( IllegalArgumentException.class, () -> { - translator.translateText("", null, "de"); + client.translateText("", null, "de"); }); } @Test void testMixedCaseLanguages() throws DeepLException, InterruptedException { - Translator translator = createTranslator(); + DeepLClient client = createDeepLClient(); TextResult result; - result = translator.translateText(exampleText.get("de"), null, "en-us"); + result = client.translateText(exampleText.get("de"), null, "en-us"); Assertions.assertEquals(exampleText.get("en-US"), result.getText().toLowerCase(Locale.ENGLISH)); Assertions.assertEquals("de", result.getDetectedSourceLanguage()); - result = translator.translateText(exampleText.get("de"), null, "EN-us"); + result = client.translateText(exampleText.get("de"), null, "EN-us"); Assertions.assertEquals(exampleText.get("en-US"), result.getText().toLowerCase(Locale.ENGLISH)); Assertions.assertEquals("de", result.getDetectedSourceLanguage()); - result = translator.translateText(exampleText.get("de"), "de", "EN-US"); + result = client.translateText(exampleText.get("de"), "de", "EN-US"); Assertions.assertEquals(exampleText.get("en-US"), result.getText().toLowerCase(Locale.ENGLISH)); Assertions.assertEquals("de", result.getDetectedSourceLanguage()); - result = translator.translateText(exampleText.get("de"), "dE", "EN-US"); + result = client.translateText(exampleText.get("de"), "dE", "EN-US"); Assertions.assertEquals(exampleText.get("en-US"), result.getText().toLowerCase(Locale.ENGLISH)); Assertions.assertEquals("de", result.getDetectedSourceLanguage()); }