From 6d5dfc4d2627030b549ee2bedafbb83757eb16fa Mon Sep 17 00:00:00 2001 From: nirupa-kumar Date: Tue, 2 Apr 2019 12:21:30 -0700 Subject: [PATCH 1/4] Adding the translate beta samples --- .../snippets/ITTranslateSnippetsbeta.java | 449 ++++++++++++++++++ 1 file changed, 449 insertions(+) create mode 100644 google-cloud-examples/src/test/java/com/google/cloud/examples/translate/snippets/ITTranslateSnippetsbeta.java diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/translate/snippets/ITTranslateSnippetsbeta.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/translate/snippets/ITTranslateSnippetsbeta.java new file mode 100644 index 000000000000..cd59dac55c70 --- /dev/null +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/translate/snippets/ITTranslateSnippetsbeta.java @@ -0,0 +1,449 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.examples.translate.snippets; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import com.google.api.core.ApiFuture; +import com.google.api.gax.paging.Page; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.Storage.BlobListOption; +import com.google.cloud.storage.StorageOptions; +import com.google.cloud.translate.v3beta1.BatchTranslateResponse; +import com.google.cloud.translate.v3beta1.BatchTranslateTextRequest; +import com.google.cloud.translate.v3beta1.CreateGlossaryRequest; +import com.google.cloud.translate.v3beta1.DeleteGlossaryResponse; +import com.google.cloud.translate.v3beta1.DetectLanguageRequest; +import com.google.cloud.translate.v3beta1.DetectLanguageResponse; +import com.google.cloud.translate.v3beta1.GcsDestination; +import com.google.cloud.translate.v3beta1.GcsSource; +import com.google.cloud.translate.v3beta1.GetSupportedLanguagesRequest; +import com.google.cloud.translate.v3beta1.Glossary; +import com.google.cloud.translate.v3beta1.Glossary.LanguageCodesSet; +import com.google.cloud.translate.v3beta1.GlossaryInputConfig; +import com.google.cloud.translate.v3beta1.GlossaryName; +import com.google.cloud.translate.v3beta1.InputConfig; +import com.google.cloud.translate.v3beta1.OutputConfig; +import com.google.cloud.translate.v3beta1.SupportedLanguage; +import com.google.cloud.translate.v3beta1.SupportedLanguages; +import com.google.cloud.translate.v3beta1.TranslateTextGlossaryConfig; +import com.google.cloud.translate.v3beta1.TranslateTextRequest; +import com.google.cloud.translate.v3beta1.TranslateTextResponse; +import com.google.cloud.translate.v3beta1.TranslationServiceClient; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import java.util.concurrent.TimeUnit; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +/** + * After modifying snippets in this file, run {@code ./utilities/snippets + * ./google-cloud-examples/src/test/java/com/google/cloud/examples/translate/snippets/ITTranslateSnippetsv3beta1.java + * ./google-cloud-clients/google-cloud-translate/src/main/java/com/google/cloud/translate/Translate.java} + * to include the snippets in the Javadoc. + */ + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class ITTranslateSnippetsbeta { + + private static final String projectId = System.getenv("PROJECT_ID"); + + private static final String[] LANGUAGES = { + "af", "sq", "ar", "hy", "az", "eu", "be", "bn", "bs", "bg", "ca", "ceb", "ny", "zh-TW", "hr", + "cs", "da", "nl", "en", "eo", "et", "tl", "fi", "fr", "gl", "ka", "de", "el", "gu", "ht", "ha", + "iw", "hi", "hmn", "hu", "is", "ig", "id", "ga", "it", "ja", "jw", "kn", "kk", "km", "ko", "lo", + "la", "lv", "lt", "mk", "mg", "ms", "ml", "mt", "mi", "mr", "mn", "my", "ne", "no", "fa", "pl", + "pt", "ro", "ru", "sr", "st", "si", "sk", "sl", "so", "es", "su", "sw", "sv", "tg", "ta", "te", + "th", "tr", "uk", "ur", "uz", "vi", "cy", "yi", "yo", "zu" + }; + + @Test + public void test1_testListSupportedLanguages() { + // [START translate_list_codes_beta] + // TODO(developer): Uncomment these lines. + // import com.google.cloud.translate.v3beta1.*; + + try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { + + String location = "global"; + + String formattedParent = TranslationServiceClient.formatLocationName(projectId, location); + GetSupportedLanguagesRequest getSupportedLanguagesRequest = + GetSupportedLanguagesRequest.newBuilder().setParent(formattedParent).build(); + ApiFuture future = + translationServiceClient + .getSupportedLanguagesCallable() + .futureCall(getSupportedLanguagesRequest); + + SupportedLanguages response = future.get(); + List languages = response.getLanguagesList(); + System.out.println(projectId); + for (SupportedLanguage language : languages) { + System.out.printf("Code: %s\n", language.getLanguageCode()); + } + // [END translate_list_codes_beta] + Set supportedLanguages = new HashSet<>(); + for (SupportedLanguage language : languages) { + supportedLanguages.add(language.getLanguageCode()); + } + for (String code : LANGUAGES) { + assertTrue(supportedLanguages.contains(code)); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void test1_testListSupportedLanguagesWithTarget() { + // [START translate_list_language_names_beta] + // TODO(developer): Uncomment these lines. + // import com.google.cloud.translate.v3beta1*; + + try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { + + String location = "global"; + + String formattedParent = TranslationServiceClient.formatLocationName(projectId, location); + GetSupportedLanguagesRequest getSupportedLanguagesRequest = + GetSupportedLanguagesRequest.newBuilder() + .setParent(formattedParent) + .setDisplayLanguageCode("en-US") + .build(); + ApiFuture future = + translationServiceClient + .getSupportedLanguagesCallable() + .futureCall(getSupportedLanguagesRequest); + + SupportedLanguages response = future.get(); + List languages = response.getLanguagesList(); + + for (SupportedLanguage language : languages) { + System.out.printf( + "Name: %s, Code: %s\n", language.getDisplayName(), language.getLanguageCode()); + } + // [END translate_list_language_names_beta] + Set supportedLanguages = new HashSet<>(); + for (SupportedLanguage language : languages) { + supportedLanguages.add(language.getLanguageCode()); + assertNotNull(language.getDisplayName()); + } + for (String code : LANGUAGES) { + assertTrue(supportedLanguages.contains(code)); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void test1_testDetectLanguageOfText() { + // [START translate_detect_language_beta] + try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { + + String location = "global"; + String text = "Hæ sæta"; + + String formattedParent = TranslationServiceClient.formatLocationName(projectId, location); + DetectLanguageRequest detectLanguageRequest = + DetectLanguageRequest.newBuilder() + .setParent(formattedParent) + .setMimeType("text/plain") + .setContent(text) + .build(); + DetectLanguageResponse response = + translationServiceClient.detectLanguage(detectLanguageRequest); + // [END translate_detect_language_beta] + assertEquals("is", response.getLanguages(0).getLanguageCode()); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void test1_testTranslateText() { + // [START translate_translate_text_beta] + // TODO(developer): Uncomment these lines. + // import com.google.cloud.translate.*; + + try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { + String location = "global"; + String content = "Hello World!"; + + String formattedParent = TranslationServiceClient.formatLocationName(projectId, location); + + TranslateTextRequest translateTextRequest = + TranslateTextRequest.newBuilder() + .setParent(formattedParent) + .setMimeType("text/plain") + .setSourceLanguageCode("en") + .setTargetLanguageCode("sr") + .addContents(content) + .build(); + + TranslateTextResponse response = translationServiceClient.translateText(translateTextRequest); + // [END translate_translate_text_beta] + assertEquals("Здраво Свете!", response.getTranslationsList().get(0).getTranslatedText()); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void test1_testBatchTranslateText() { + // [START translate_batch_translate_text_beta] + // TODO(developer): Uncomment these lines. + // import com.google.cloud.translate.*; + + try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { + String location = "us-central1"; + + String OUTPUT_PREFIX = "gs://" + projectId + "/BATCH_TRANSLATION_OUTPUT/"; + String formattedParent = TranslationServiceClient.formatLocationName(projectId, location); + GcsSource gcsSource = + GcsSource.newBuilder() + .setInputUri("gs://cloud-samples-data/translation/text.txt") + .build(); + InputConfig inputConfig = + InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build(); + GcsDestination gcsDestination = + GcsDestination.newBuilder().setOutputUriPrefix(OUTPUT_PREFIX).build(); + OutputConfig outputConfig = + OutputConfig.newBuilder().setGcsDestination(gcsDestination).build(); + BatchTranslateTextRequest batchTranslateTextRequest = + BatchTranslateTextRequest.newBuilder() + .setParent(formattedParent) + .setSourceLanguageCode("en") + .addTargetLanguageCodes("sr") + .addInputConfigs(inputConfig) + .setOutputConfig(outputConfig) + .build(); + BatchTranslateResponse response = + translationServiceClient + .batchTranslateTextAsync(batchTranslateTextRequest) + .get(300, TimeUnit.SECONDS); + + System.out.printf("Total Characters: %d\n", response.getTotalCharacters()); + System.out.printf("Translated Characters: %d\n", response.getTranslatedCharacters()); + assertEquals(13, response.getTotalCharacters()); + assertEquals(13, response.getTranslatedCharacters()); + Storage storage = StorageOptions.getDefaultInstance().getService(); + + Page blobs = + storage.list( + projectId, + BlobListOption.currentDirectory(), + BlobListOption.prefix("BATCH_TRANSLATION_OUTPUT/")); + + deleteDirectory(storage, blobs); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void deleteDirectory(Storage storage, Page blobs) { + for (Blob blob : blobs.iterateAll()) { + System.out.println(blob.getBlobId()); + if (!blob.delete()) { + Page subBlobs = + storage.list( + projectId, + BlobListOption.currentDirectory(), + BlobListOption.prefix(blob.getName())); + + deleteDirectory(storage, subBlobs); + } + } + } + + @Test + public void test1_testCreateGlossary() { + // [START translate_create_glossary_beta] + // TODO(developer): Uncomment these lines. + // import com.google.cloud.translate.*; + + try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { + String location = "us-central1"; + String inputUri = "gs://cloud-samples-data/translation/glossary.csv"; + String formattedParent = TranslationServiceClient.formatLocationName(projectId, location); + LanguageCodesSet languageCodesSet = + LanguageCodesSet.newBuilder().addLanguageCodes("en").addLanguageCodes("es").build(); + GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build(); + GlossaryInputConfig glossaryInputConfig = + GlossaryInputConfig.newBuilder().setGcsSource(gcsSource).build(); + GlossaryName glossaryName = + GlossaryName.newBuilder() + .setProject(projectId) + .setLocation(location) + .setGlossary("glossary-testing") + .build(); + Glossary glossary = + Glossary.newBuilder() + .setLanguageCodesSet(languageCodesSet) + .setInputConfig(glossaryInputConfig) + .setName(glossaryName.toString()) + .build(); + CreateGlossaryRequest request = + CreateGlossaryRequest.newBuilder() + .setParent(formattedParent) + .setGlossary(glossary) + .build(); + Glossary response = + translationServiceClient.createGlossaryAsync(request).get(300, TimeUnit.SECONDS); + System.out.format("Created: %s\n", response.getName()); + System.out.format("Input Uri: %s\n", response.getInputConfig().getGcsSource()); + // [END translate_create_glossary_beta] + assertEquals(glossaryName.toString(), response.getName()); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void test2_testListGlossary() { + // [START translate_list_glossary_beta] + // TODO(developer): Uncomment these lines. + // import com.google.cloud.translate.*; + try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { + String location = "us-central1"; + String formattedParent = TranslationServiceClient.formatLocationName(projectId, location); + String filter = ""; + for (Glossary element : + translationServiceClient.listGlossaries(formattedParent, filter).iterateAll()) { + System.out.format("Name: %s\n", element.getName()); + System.out.format("Language Codes Set:\n"); + System.out.format( + "Source Language Code: %s\n", + element.getLanguageCodesSet().getLanguageCodesList().get(0)); + System.out.format( + "Target Language Code: %s\n", + element.getLanguageCodesSet().getLanguageCodesList().get(1)); + System.out.format("Input Uri: %s\n", element.getInputConfig().getGcsSource()); + } + // [END translate_list_glossary_beta] + assertEquals( + "projects/" + projectId + "/locations/us-central1/glossaries/glossary-testing", + translationServiceClient + .listGlossaries(formattedParent, filter) + .iterateAll() + .iterator() + .next() + .getName()); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void test2_testGetGlossary() { + // [START translate_get_glossary_beta] + // TODO(developer): Uncomment these lines. + // import com.google.cloud.translate.*; + + try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { + String location = "us-central1"; + String formattedParent = TranslationServiceClient.formatLocationName(projectId, location); + GlossaryName glossaryName = + GlossaryName.newBuilder() + .setProject(projectId) + .setLocation(location) + .setGlossary("glossary-testing") + .build(); + + Glossary response = translationServiceClient.getGlossary(glossaryName.toString()); + System.out.format("Got: %s\n", response.getName()); + // [END translate_get_glossary_beta] + assertEquals(glossaryName.toString(), response.getName()); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void test3_testDeleteGlossary() { + // [START translate_delete_glossary_beta] + // TODO(developer): Uncomment these lines. + // import com.google.cloud.translate.*; + try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { + String location = "us-central1"; + GlossaryName glossaryName = + GlossaryName.newBuilder() + .setProject(projectId) + .setLocation(location) + .setGlossary("glossary-testing") + .build(); + + DeleteGlossaryResponse response = + translationServiceClient + .deleteGlossaryAsync(glossaryName.toString()) + .get(300, TimeUnit.SECONDS); + + System.out.format("Deleted: %s\n", response.getName()); + // [END translate_delete_glossary_beta] + assertEquals(glossaryName.toString(), response.getName()); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void test2_testTranslateTextWithGlossary() { + // [START translate_translate_text_with_glossary_beta] + // TODO(developer): Uncomment these lines. + // import com.google.cloud.translate.*; + try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { + String location = "us-central1"; + String content = "directions"; + String formattedParent = TranslationServiceClient.formatLocationName(projectId, location); + GlossaryName glossaryName = + GlossaryName.newBuilder() + .setProject(projectId) + .setLocation(location) + .setGlossary("glossary-testing") + .build(); + TranslateTextGlossaryConfig translateTextGlossaryConfig = + TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build(); + TranslateTextRequest translateTextRequest = + TranslateTextRequest.newBuilder() + .setParent(formattedParent) + .setMimeType("text/plain") + .setSourceLanguageCode("en") + .setTargetLanguageCode("es") + .addContents(content) + .setGlossaryConfig(translateTextGlossaryConfig) + .build(); + + TranslateTextResponse response = translationServiceClient.translateText(translateTextRequest); + // [END translate_translate_text_with_glossary_beta] + assertEquals("direcciones", response.getTranslationsList().get(0).getTranslatedText()); + + } catch (Exception e) { + e.printStackTrace(); + } + } + +} From b6c935b9c5cb3e05e2e3173f03aaabf0e121dde2 Mon Sep 17 00:00:00 2001 From: nirupa-kumar Date: Tue, 2 Apr 2019 12:50:06 -0700 Subject: [PATCH 2/4] Fixing lint issues --- .../snippets/ITTranslateSnippetsbeta.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/translate/snippets/ITTranslateSnippetsbeta.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/translate/snippets/ITTranslateSnippetsbeta.java index cd59dac55c70..1bb9f8ee93cd 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/translate/snippets/ITTranslateSnippetsbeta.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/translate/snippets/ITTranslateSnippetsbeta.java @@ -47,11 +47,9 @@ import com.google.cloud.translate.v3beta1.TranslateTextRequest; import com.google.cloud.translate.v3beta1.TranslateTextResponse; import com.google.cloud.translate.v3beta1.TranslationServiceClient; - import java.util.HashSet; import java.util.List; import java.util.Set; - import java.util.concurrent.TimeUnit; import org.junit.FixMethodOrder; import org.junit.Test; @@ -63,19 +61,18 @@ * ./google-cloud-clients/google-cloud-translate/src/main/java/com/google/cloud/translate/Translate.java} * to include the snippets in the Javadoc. */ - @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class ITTranslateSnippetsbeta { private static final String projectId = System.getenv("PROJECT_ID"); private static final String[] LANGUAGES = { - "af", "sq", "ar", "hy", "az", "eu", "be", "bn", "bs", "bg", "ca", "ceb", "ny", "zh-TW", "hr", - "cs", "da", "nl", "en", "eo", "et", "tl", "fi", "fr", "gl", "ka", "de", "el", "gu", "ht", "ha", - "iw", "hi", "hmn", "hu", "is", "ig", "id", "ga", "it", "ja", "jw", "kn", "kk", "km", "ko", "lo", - "la", "lv", "lt", "mk", "mg", "ms", "ml", "mt", "mi", "mr", "mn", "my", "ne", "no", "fa", "pl", - "pt", "ro", "ru", "sr", "st", "si", "sk", "sl", "so", "es", "su", "sw", "sv", "tg", "ta", "te", - "th", "tr", "uk", "ur", "uz", "vi", "cy", "yi", "yo", "zu" + "af", "sq", "ar", "hy", "az", "eu", "be", "bn", "bs", "bg", "ca", "ceb", "ny", "zh-TW", "hr", + "cs", "da", "nl", "en", "eo", "et", "tl", "fi", "fr", "gl", "ka", "de", "el", "gu", "ht", "ha", + "iw", "hi", "hmn", "hu", "is", "ig", "id", "ga", "it", "ja", "jw", "kn", "kk", "km", "ko", "lo", + "la", "lv", "lt", "mk", "mg", "ms", "ml", "mt", "mi", "mr", "mn", "my", "ne", "no", "fa", "pl", + "pt", "ro", "ru", "sr", "st", "si", "sk", "sl", "so", "es", "su", "sw", "sv", "tg", "ta", "te", + "th", "tr", "uk", "ur", "uz", "vi", "cy", "yi", "yo", "zu" }; @Test @@ -445,5 +442,4 @@ public void test2_testTranslateTextWithGlossary() { e.printStackTrace(); } } - } From b23be980bfb74efe816eea2d8efa6a836421bdce Mon Sep 17 00:00:00 2001 From: nirupa-kumar Date: Tue, 2 Apr 2019 16:51:07 -0700 Subject: [PATCH 3/4] Fixing coding format/style --- .../snippets/TranslateSnippetsbeta.java | 448 ++++++++++++++++++ .../snippets/ITTranslateSnippetsbeta.java | 427 ++++------------- 2 files changed, 531 insertions(+), 344 deletions(-) create mode 100644 google-cloud-examples/src/main/java/com/google/cloud/examples/translate/snippets/TranslateSnippetsbeta.java diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/translate/snippets/TranslateSnippetsbeta.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/translate/snippets/TranslateSnippetsbeta.java new file mode 100644 index 000000000000..74b0cea0403c --- /dev/null +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/translate/snippets/TranslateSnippetsbeta.java @@ -0,0 +1,448 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.examples.translate.snippets; + +import com.google.api.core.ApiFuture; +import com.google.cloud.translate.v3beta1.BatchTranslateResponse; +import com.google.cloud.translate.v3beta1.BatchTranslateTextRequest; +import com.google.cloud.translate.v3beta1.CreateGlossaryRequest; +import com.google.cloud.translate.v3beta1.DeleteGlossaryResponse; +import com.google.cloud.translate.v3beta1.DetectLanguageRequest; +import com.google.cloud.translate.v3beta1.DetectLanguageResponse; +import com.google.cloud.translate.v3beta1.GcsDestination; +import com.google.cloud.translate.v3beta1.GcsSource; +import com.google.cloud.translate.v3beta1.GetSupportedLanguagesRequest; +import com.google.cloud.translate.v3beta1.Glossary; +import com.google.cloud.translate.v3beta1.Glossary.LanguageCodesSet; +import com.google.cloud.translate.v3beta1.GlossaryInputConfig; +import com.google.cloud.translate.v3beta1.GlossaryName; +import com.google.cloud.translate.v3beta1.InputConfig; +import com.google.cloud.translate.v3beta1.LocationName; +import com.google.cloud.translate.v3beta1.OutputConfig; +import com.google.cloud.translate.v3beta1.SupportedLanguage; +import com.google.cloud.translate.v3beta1.SupportedLanguages; +import com.google.cloud.translate.v3beta1.TranslateTextGlossaryConfig; +import com.google.cloud.translate.v3beta1.TranslateTextRequest; +import com.google.cloud.translate.v3beta1.TranslateTextResponse; +import com.google.cloud.translate.v3beta1.TranslationServiceClient; +import com.google.cloud.translate.v3beta1.TranslationServiceClient.ListGlossariesPagedResponse; +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** Snippets for the beta features */ +public class TranslateSnippetsbeta { + private TranslateSnippetsbeta() {} + + /** + * Lists all the supported language codes. + * + * @param projectId - Id of the project. + * @param location - location name. + */ + // [START translate_list_codes_beta] + static SupportedLanguages listSupportedLanguages(String projectId, String location) { + try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { + + LocationName locationName = + LocationName.newBuilder().setProject(projectId).setLocation(location).build(); + GetSupportedLanguagesRequest getSupportedLanguagesRequest = + GetSupportedLanguagesRequest.newBuilder().setParent(locationName.toString()).build(); + + // Call the API + ApiFuture future = + translationServiceClient + .getSupportedLanguagesCallable() + .futureCall(getSupportedLanguagesRequest); + + SupportedLanguages response = future.get(); + List languages = response.getLanguagesList(); + for (SupportedLanguage language : languages) { + System.out.printf("Code: %s\n", language.getLanguageCode()); + } + + return response; + + } catch (Exception e) { + throw new RuntimeException("Couldn't create client.", e); + } + } + // [END translate_list_codes_beta] + + /** + * Lists all the supported language names and codes. + * + * @param projectId - Id of the project. + * @param location - location name. + */ + // [START translate_list_language_names_beta] + static SupportedLanguages listSupportedLanguagesWithTarget(String projectId, String location) { + try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { + + LocationName locationName = + LocationName.newBuilder().setProject(projectId).setLocation(location).build(); + GetSupportedLanguagesRequest getSupportedLanguagesRequest = + GetSupportedLanguagesRequest.newBuilder() + .setParent(locationName.toString()) + .setDisplayLanguageCode("en-US") + .build(); + + // Call the API + ApiFuture future = + translationServiceClient + .getSupportedLanguagesCallable() + .futureCall(getSupportedLanguagesRequest); + + SupportedLanguages response = future.get(); + List languages = response.getLanguagesList(); + + for (SupportedLanguage language : languages) { + System.out.printf( + "Name: %s, Code: %s\n", language.getDisplayName(), language.getLanguageCode()); + } + return response; + + } catch (Exception e) { + throw new RuntimeException("Couldn't create client.", e); + } + } + // [END translate_list_language_names_beta] + + /** + * Detects the language of a given text. + * + * @param projectId - Id of the project. + * @param location - location name. + * @param text - Text for detection + */ + // [START translate_detect_language_beta] + static DetectLanguageResponse detectLanguageOfText( + String projectId, String location, String text) { + + try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { + + LocationName locationName = + LocationName.newBuilder().setProject(projectId).setLocation(location).build(); + DetectLanguageRequest detectLanguageRequest = + DetectLanguageRequest.newBuilder() + .setParent(locationName.toString()) + .setMimeType("text/plain") + .setContent(text) + .build(); + + // Call the API + DetectLanguageResponse response = + translationServiceClient.detectLanguage(detectLanguageRequest); + System.out.format("Detected Language Code: %s", response.getLanguages(0).getLanguageCode()); + return response; + + } catch (Exception e) { + throw new RuntimeException("Couldn't create client.", e); + } + } + // [END translate_detect_language_beta] + + // [START translate_translate_text_beta] + + /** + * Translates a given text to a target language. + * + * @param projectId - Id of the project. + * @param location - location name. + * @param text - Text for translation. + * @param sourceLanguageCode - Language code of text. e.g. "en" + * @param targetLanguageCode - Language code for translation. e.g. "sr" + */ + static TranslateTextResponse translateText( + String projectId, + String location, + String text, + String sourceLanguageCode, + String targetLanguageCode) { + try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { + + LocationName locationName = + LocationName.newBuilder().setProject(projectId).setLocation(location).build(); + + TranslateTextRequest translateTextRequest = + TranslateTextRequest.newBuilder() + .setParent(locationName.toString()) + .setMimeType("text/plain") + .setSourceLanguageCode(sourceLanguageCode) + .setTargetLanguageCode(targetLanguageCode) + .addContents(text) + .build(); + + // Call the API + TranslateTextResponse response = translationServiceClient.translateText(translateTextRequest); + System.out.format( + "Translated Text: %s", response.getTranslationsList().get(0).getTranslatedText()); + return response; + + } catch (Exception e) { + throw new RuntimeException("Couldn't create client.", e); + } + } + // [END translate_translate_text_beta] + + /** + * Translates a batch of texts on GCS and stores the result in a GCS location. + * + * @param projectId - Id of the project. + * @param location - location name. + * @param sourceUri - Google Cloud Storage URI. Location where text is stored. + * @param destinationUri - Google Cloud Storage URI where result will be stored. + */ + // [START translate_batch_translate_text_beta] + static BatchTranslateResponse batchTranslateText( + String projectId, String location, String sourceUri, String destinationUri) { + try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { + + LocationName locationName = + LocationName.newBuilder().setProject(projectId).setLocation(location).build(); + GcsSource gcsSource = GcsSource.newBuilder().setInputUri(sourceUri).build(); + InputConfig inputConfig = + InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build(); + GcsDestination gcsDestination = + GcsDestination.newBuilder().setOutputUriPrefix(destinationUri).build(); + OutputConfig outputConfig = + OutputConfig.newBuilder().setGcsDestination(gcsDestination).build(); + BatchTranslateTextRequest batchTranslateTextRequest = + BatchTranslateTextRequest.newBuilder() + .setParent(locationName.toString()) + .setSourceLanguageCode("en") + .addTargetLanguageCodes("sr") + .addInputConfigs(inputConfig) + .setOutputConfig(outputConfig) + .build(); + + // Call the API + BatchTranslateResponse response = + translationServiceClient + .batchTranslateTextAsync(batchTranslateTextRequest) + .get(300, TimeUnit.SECONDS); + + System.out.printf("Total Characters: %d\n", response.getTotalCharacters()); + System.out.printf("Translated Characters: %d\n", response.getTranslatedCharacters()); + return response; + + } catch (Exception e) { + throw new RuntimeException("Couldn't create client.", e); + } + } + // [END translate_batch_translate_text_beta] + + /** + * Creates a glossary. + * + * @param projectId - Id of the project. + * @param location - location name. + * @param name - Glossary name. + * @param gcsUri - Google Cloud Storage URI where glossary is stored in csv format. + */ + // [START translate_create_glossary_beta] + static Glossary createGlossary(String projectId, String location, String name, String gcsUri) { + try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { + + LocationName locationName = + LocationName.newBuilder().setProject(projectId).setLocation(location).build(); + LanguageCodesSet languageCodesSet = + LanguageCodesSet.newBuilder().addLanguageCodes("en").addLanguageCodes("es").build(); + GcsSource gcsSource = GcsSource.newBuilder().setInputUri(gcsUri).build(); + GlossaryInputConfig glossaryInputConfig = + GlossaryInputConfig.newBuilder().setGcsSource(gcsSource).build(); + GlossaryName glossaryName = + GlossaryName.newBuilder() + .setProject(projectId) + .setLocation(location) + .setGlossary(name) + .build(); + Glossary glossary = + Glossary.newBuilder() + .setLanguageCodesSet(languageCodesSet) + .setInputConfig(glossaryInputConfig) + .setName(glossaryName.toString()) + .build(); + CreateGlossaryRequest request = + CreateGlossaryRequest.newBuilder() + .setParent(locationName.toString()) + .setGlossary(glossary) + .build(); + + // Call the API + Glossary response = + translationServiceClient.createGlossaryAsync(request).get(300, TimeUnit.SECONDS); + System.out.format("Created: %s\n", response.getName()); + System.out.format("Input Uri: %s\n", response.getInputConfig().getGcsSource()); + return response; + + } catch (Exception e) { + throw new RuntimeException("Couldn't create client.", e); + } + } + // [END translate_create_glossary_beta] + + /** + * Lists all the glossaries for a given project. + * + * @param projectId - Id of the project. + * @param location - location name. + * @param filter - criteria for listing glossaries. + */ + // [START translate_list_glossary_beta] + static ListGlossariesPagedResponse listGlossary( + String projectId, String location, String filter) { + + try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { + + LocationName locationName = + LocationName.newBuilder().setProject(projectId).setLocation(location).build(); + + ListGlossariesPagedResponse response = + translationServiceClient.listGlossaries(locationName.toString(), filter); + + // Call the API + for (Glossary element : response.iterateAll()) { + System.out.format("Name: %s\n", element.getName()); + System.out.format("Language Codes Set:\n"); + System.out.format( + "Source Language Code: %s\n", + element.getLanguageCodesSet().getLanguageCodesList().get(0)); + System.out.format( + "Target Language Code: %s\n", + element.getLanguageCodesSet().getLanguageCodesList().get(1)); + System.out.format("Input Uri: %s\n", element.getInputConfig().getGcsSource()); + } + return response; + } catch (Exception e) { + throw new RuntimeException("Couldn't create client.", e); + } + } + // [END translate_list_glossary_beta] + + /** + * Retrieves a glossary. + * + * @param projectId - Id of the project. + * @param location - location name. + * @param name - Glossary name. + */ + // [START translate_get_glossary_beta] + static Glossary getGlossary(String projectId, String location, String name) { + try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { + + GlossaryName glossaryName = + GlossaryName.newBuilder() + .setProject(projectId) + .setLocation(location) + .setGlossary(name) + .build(); + + // Call the API + Glossary response = translationServiceClient.getGlossary(glossaryName.toString()); + System.out.format("Got: %s\n", response.getName()); + return response; + + } catch (Exception e) { + throw new RuntimeException("Couldn't create client.", e); + } + } + // [END translate_get_glossary_beta] + + /** + * Translates a given text using a glossary. + * + * @param projectId - Id of the project. + * @param location - location name. + * @param name - Glossary name. + * @param text - Text to be translated. e.g. "Hello World!" + * @param sourceLanguageCode - Language code of text. e.g. "en" + * @param targetLanguageCode - Language code for translation. e.g. "sr" + */ + // [START translate_translate_text_with_glossary_beta] + static TranslateTextResponse translateTextWithGlossary( + String projectId, + String location, + String name, + String text, + String sourceLanguageCode, + String targetLanguageCode) { + try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { + + LocationName locationName = + LocationName.newBuilder().setProject(projectId).setLocation(location).build(); + GlossaryName glossaryName = + GlossaryName.newBuilder() + .setProject(projectId) + .setLocation(location) + .setGlossary(name) + .build(); + TranslateTextGlossaryConfig translateTextGlossaryConfig = + TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build(); + TranslateTextRequest translateTextRequest = + TranslateTextRequest.newBuilder() + .setParent(locationName.toString()) + .setMimeType("text/plain") + .setSourceLanguageCode(sourceLanguageCode) + .setTargetLanguageCode(targetLanguageCode) + .addContents(text) + .setGlossaryConfig(translateTextGlossaryConfig) + .build(); + + // Call the API + TranslateTextResponse response = translationServiceClient.translateText(translateTextRequest); + System.out.format( + "Translated text: %s", response.getTranslationsList().get(0).getTranslatedText()); + return response; + + } catch (Exception e) { + throw new RuntimeException("Couldn't create client.", e); + } + } + // [END translate_translate_text_with_glossary_beta] + + /** + * Deletes a glossary. + * + * @param projectId - Id of the project. + * @param location - location name. + * @param name - Glossary name. + */ + // [START translate_delete_glossary_beta] + static DeleteGlossaryResponse deleteGlossary(String projectId, String location, String name) { + try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { + + GlossaryName glossaryName = + GlossaryName.newBuilder() + .setProject(projectId) + .setLocation(location) + .setGlossary(name) + .build(); + + // Call the API + DeleteGlossaryResponse response = + translationServiceClient + .deleteGlossaryAsync(glossaryName.toString()) + .get(300, TimeUnit.SECONDS); + + System.out.format("Deleted: %s\n", response.getName()); + return response; + } catch (Exception e) { + throw new RuntimeException("Couldn't create client.", e); + } + } + // [END translate_delete_glossary_beta] +} diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/translate/snippets/ITTranslateSnippetsbeta.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/translate/snippets/ITTranslateSnippetsbeta.java index 1bb9f8ee93cd..b73641b8f49a 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/translate/snippets/ITTranslateSnippetsbeta.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/translate/snippets/ITTranslateSnippetsbeta.java @@ -17,246 +17,107 @@ package com.google.cloud.examples.translate.snippets; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import com.google.api.core.ApiFuture; import com.google.api.gax.paging.Page; import com.google.cloud.storage.Blob; import com.google.cloud.storage.Storage; import com.google.cloud.storage.Storage.BlobListOption; import com.google.cloud.storage.StorageOptions; import com.google.cloud.translate.v3beta1.BatchTranslateResponse; -import com.google.cloud.translate.v3beta1.BatchTranslateTextRequest; -import com.google.cloud.translate.v3beta1.CreateGlossaryRequest; import com.google.cloud.translate.v3beta1.DeleteGlossaryResponse; -import com.google.cloud.translate.v3beta1.DetectLanguageRequest; -import com.google.cloud.translate.v3beta1.DetectLanguageResponse; -import com.google.cloud.translate.v3beta1.GcsDestination; -import com.google.cloud.translate.v3beta1.GcsSource; -import com.google.cloud.translate.v3beta1.GetSupportedLanguagesRequest; import com.google.cloud.translate.v3beta1.Glossary; -import com.google.cloud.translate.v3beta1.Glossary.LanguageCodesSet; -import com.google.cloud.translate.v3beta1.GlossaryInputConfig; -import com.google.cloud.translate.v3beta1.GlossaryName; -import com.google.cloud.translate.v3beta1.InputConfig; -import com.google.cloud.translate.v3beta1.OutputConfig; -import com.google.cloud.translate.v3beta1.SupportedLanguage; -import com.google.cloud.translate.v3beta1.SupportedLanguages; -import com.google.cloud.translate.v3beta1.TranslateTextGlossaryConfig; -import com.google.cloud.translate.v3beta1.TranslateTextRequest; import com.google.cloud.translate.v3beta1.TranslateTextResponse; -import com.google.cloud.translate.v3beta1.TranslationServiceClient; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.concurrent.TimeUnit; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; -/** - * After modifying snippets in this file, run {@code ./utilities/snippets - * ./google-cloud-examples/src/test/java/com/google/cloud/examples/translate/snippets/ITTranslateSnippetsv3beta1.java - * ./google-cloud-clients/google-cloud-translate/src/main/java/com/google/cloud/translate/Translate.java} - * to include the snippets in the Javadoc. - */ @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class ITTranslateSnippetsbeta { private static final String projectId = System.getenv("PROJECT_ID"); + private static final String glossaryName = "glossary-testing"; - private static final String[] LANGUAGES = { - "af", "sq", "ar", "hy", "az", "eu", "be", "bn", "bs", "bg", "ca", "ceb", "ny", "zh-TW", "hr", - "cs", "da", "nl", "en", "eo", "et", "tl", "fi", "fr", "gl", "ka", "de", "el", "gu", "ht", "ha", - "iw", "hi", "hmn", "hu", "is", "ig", "id", "ga", "it", "ja", "jw", "kn", "kk", "km", "ko", "lo", - "la", "lv", "lt", "mk", "mg", "ms", "ml", "mt", "mi", "mr", "mn", "my", "ne", "no", "fa", "pl", - "pt", "ro", "ru", "sr", "st", "si", "sk", "sl", "so", "es", "su", "sw", "sv", "tg", "ta", "te", - "th", "tr", "uk", "ur", "uz", "vi", "cy", "yi", "yo", "zu" - }; - - @Test - public void test1_testListSupportedLanguages() { - // [START translate_list_codes_beta] - // TODO(developer): Uncomment these lines. - // import com.google.cloud.translate.v3beta1.*; + private ByteArrayOutputStream bout; + private PrintStream out; - try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } - String location = "global"; + @After + public void tearDown() { + System.setOut(null); + } - String formattedParent = TranslationServiceClient.formatLocationName(projectId, location); - GetSupportedLanguagesRequest getSupportedLanguagesRequest = - GetSupportedLanguagesRequest.newBuilder().setParent(formattedParent).build(); - ApiFuture future = - translationServiceClient - .getSupportedLanguagesCallable() - .futureCall(getSupportedLanguagesRequest); + @Test + public void test1_testListSupportedLanguages() { - SupportedLanguages response = future.get(); - List languages = response.getLanguagesList(); - System.out.println(projectId); - for (SupportedLanguage language : languages) { - System.out.printf("Code: %s\n", language.getLanguageCode()); - } - // [END translate_list_codes_beta] - Set supportedLanguages = new HashSet<>(); - for (SupportedLanguage language : languages) { - supportedLanguages.add(language.getLanguageCode()); - } - for (String code : LANGUAGES) { - assertTrue(supportedLanguages.contains(code)); - } - } catch (Exception e) { - e.printStackTrace(); - } + assertTrue( + 10 + <= TranslateSnippetsbeta.listSupportedLanguages(projectId, "global") + .getLanguagesList() + .size()); } @Test public void test1_testListSupportedLanguagesWithTarget() { - // [START translate_list_language_names_beta] - // TODO(developer): Uncomment these lines. - // import com.google.cloud.translate.v3beta1*; - try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { - - String location = "global"; - - String formattedParent = TranslationServiceClient.formatLocationName(projectId, location); - GetSupportedLanguagesRequest getSupportedLanguagesRequest = - GetSupportedLanguagesRequest.newBuilder() - .setParent(formattedParent) - .setDisplayLanguageCode("en-US") - .build(); - ApiFuture future = - translationServiceClient - .getSupportedLanguagesCallable() - .futureCall(getSupportedLanguagesRequest); - - SupportedLanguages response = future.get(); - List languages = response.getLanguagesList(); - - for (SupportedLanguage language : languages) { - System.out.printf( - "Name: %s, Code: %s\n", language.getDisplayName(), language.getLanguageCode()); - } - // [END translate_list_language_names_beta] - Set supportedLanguages = new HashSet<>(); - for (SupportedLanguage language : languages) { - supportedLanguages.add(language.getLanguageCode()); - assertNotNull(language.getDisplayName()); - } - for (String code : LANGUAGES) { - assertTrue(supportedLanguages.contains(code)); - } - } catch (Exception e) { - e.printStackTrace(); - } + assertTrue( + 0 + < TranslateSnippetsbeta.listSupportedLanguagesWithTarget(projectId, "global") + .getLanguagesList() + .size()); } @Test public void test1_testDetectLanguageOfText() { - // [START translate_detect_language_beta] - try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { - - String location = "global"; - String text = "Hæ sæta"; - String formattedParent = TranslationServiceClient.formatLocationName(projectId, location); - DetectLanguageRequest detectLanguageRequest = - DetectLanguageRequest.newBuilder() - .setParent(formattedParent) - .setMimeType("text/plain") - .setContent(text) - .build(); - DetectLanguageResponse response = - translationServiceClient.detectLanguage(detectLanguageRequest); - // [END translate_detect_language_beta] - assertEquals("is", response.getLanguages(0).getLanguageCode()); - } catch (Exception e) { - e.printStackTrace(); - } + assertEquals( + "is", + TranslateSnippetsbeta.detectLanguageOfText(projectId, "global", "Hæ sæta") + .getLanguages(0) + .getLanguageCode()); } @Test public void test1_testTranslateText() { - // [START translate_translate_text_beta] - // TODO(developer): Uncomment these lines. - // import com.google.cloud.translate.*; - - try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { - String location = "global"; - String content = "Hello World!"; - - String formattedParent = TranslationServiceClient.formatLocationName(projectId, location); - TranslateTextRequest translateTextRequest = - TranslateTextRequest.newBuilder() - .setParent(formattedParent) - .setMimeType("text/plain") - .setSourceLanguageCode("en") - .setTargetLanguageCode("sr") - .addContents(content) - .build(); - - TranslateTextResponse response = translationServiceClient.translateText(translateTextRequest); - // [END translate_translate_text_beta] - assertEquals("Здраво Свете!", response.getTranslationsList().get(0).getTranslatedText()); - } catch (Exception e) { - e.printStackTrace(); - } + assertEquals( + "Здраво Свете!", + TranslateSnippetsbeta.translateText(projectId, "global", "Hello World!", "en", "sr") + .getTranslationsList() + .get(0) + .getTranslatedText()); } @Test public void test1_testBatchTranslateText() { - // [START translate_batch_translate_text_beta] - // TODO(developer): Uncomment these lines. - // import com.google.cloud.translate.*; - - try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { - String location = "us-central1"; - - String OUTPUT_PREFIX = "gs://" + projectId + "/BATCH_TRANSLATION_OUTPUT/"; - String formattedParent = TranslationServiceClient.formatLocationName(projectId, location); - GcsSource gcsSource = - GcsSource.newBuilder() - .setInputUri("gs://cloud-samples-data/translation/text.txt") - .build(); - InputConfig inputConfig = - InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build(); - GcsDestination gcsDestination = - GcsDestination.newBuilder().setOutputUriPrefix(OUTPUT_PREFIX).build(); - OutputConfig outputConfig = - OutputConfig.newBuilder().setGcsDestination(gcsDestination).build(); - BatchTranslateTextRequest batchTranslateTextRequest = - BatchTranslateTextRequest.newBuilder() - .setParent(formattedParent) - .setSourceLanguageCode("en") - .addTargetLanguageCodes("sr") - .addInputConfigs(inputConfig) - .setOutputConfig(outputConfig) - .build(); - BatchTranslateResponse response = - translationServiceClient - .batchTranslateTextAsync(batchTranslateTextRequest) - .get(300, TimeUnit.SECONDS); - - System.out.printf("Total Characters: %d\n", response.getTotalCharacters()); - System.out.printf("Translated Characters: %d\n", response.getTranslatedCharacters()); - assertEquals(13, response.getTotalCharacters()); - assertEquals(13, response.getTranslatedCharacters()); - Storage storage = StorageOptions.getDefaultInstance().getService(); - - Page blobs = - storage.list( - projectId, - BlobListOption.currentDirectory(), - BlobListOption.prefix("BATCH_TRANSLATION_OUTPUT/")); - deleteDirectory(storage, blobs); - } catch (Exception e) { - e.printStackTrace(); - } + BatchTranslateResponse response = + TranslateSnippetsbeta.batchTranslateText( + projectId, + "us-central1", + "gs://cloud-samples-data/translation/text.txt", + "gs://" + projectId + "/BATCH_TRANSLATION_OUTPUT/"); + assertEquals(13, response.getTotalCharacters()); + assertEquals(13, response.getTranslatedCharacters()); + Storage storage = StorageOptions.getDefaultInstance().getService(); + + Page blobs = + storage.list( + projectId, + BlobListOption.currentDirectory(), + BlobListOption.prefix("BATCH_TRANSLATION_OUTPUT/")); + + deleteDirectory(storage, blobs); } private void deleteDirectory(Storage storage, Page blobs) { @@ -276,170 +137,48 @@ private void deleteDirectory(Storage storage, Page blobs) { @Test public void test1_testCreateGlossary() { - // [START translate_create_glossary_beta] - // TODO(developer): Uncomment these lines. - // import com.google.cloud.translate.*; - - try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { - String location = "us-central1"; - String inputUri = "gs://cloud-samples-data/translation/glossary.csv"; - String formattedParent = TranslationServiceClient.formatLocationName(projectId, location); - LanguageCodesSet languageCodesSet = - LanguageCodesSet.newBuilder().addLanguageCodes("en").addLanguageCodes("es").build(); - GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build(); - GlossaryInputConfig glossaryInputConfig = - GlossaryInputConfig.newBuilder().setGcsSource(gcsSource).build(); - GlossaryName glossaryName = - GlossaryName.newBuilder() - .setProject(projectId) - .setLocation(location) - .setGlossary("glossary-testing") - .build(); - Glossary glossary = - Glossary.newBuilder() - .setLanguageCodesSet(languageCodesSet) - .setInputConfig(glossaryInputConfig) - .setName(glossaryName.toString()) - .build(); - CreateGlossaryRequest request = - CreateGlossaryRequest.newBuilder() - .setParent(formattedParent) - .setGlossary(glossary) - .build(); - Glossary response = - translationServiceClient.createGlossaryAsync(request).get(300, TimeUnit.SECONDS); - System.out.format("Created: %s\n", response.getName()); - System.out.format("Input Uri: %s\n", response.getInputConfig().getGcsSource()); - // [END translate_create_glossary_beta] - assertEquals(glossaryName.toString(), response.getName()); - } catch (Exception e) { - e.printStackTrace(); - } + Glossary response = + TranslateSnippetsbeta.createGlossary( + projectId, + "us-central1", + glossaryName, + "gs://cloud-samples-data/translation/glossary.csv"); + assertTrue(response.toString(), response.toString().contains("glossary-testing")); } @Test public void test2_testListGlossary() { - // [START translate_list_glossary_beta] - // TODO(developer): Uncomment these lines. - // import com.google.cloud.translate.*; - try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { - String location = "us-central1"; - String formattedParent = TranslationServiceClient.formatLocationName(projectId, location); - String filter = ""; - for (Glossary element : - translationServiceClient.listGlossaries(formattedParent, filter).iterateAll()) { - System.out.format("Name: %s\n", element.getName()); - System.out.format("Language Codes Set:\n"); - System.out.format( - "Source Language Code: %s\n", - element.getLanguageCodesSet().getLanguageCodesList().get(0)); - System.out.format( - "Target Language Code: %s\n", - element.getLanguageCodesSet().getLanguageCodesList().get(1)); - System.out.format("Input Uri: %s\n", element.getInputConfig().getGcsSource()); - } - // [END translate_list_glossary_beta] - assertEquals( - "projects/" + projectId + "/locations/us-central1/glossaries/glossary-testing", - translationServiceClient - .listGlossaries(formattedParent, filter) - .iterateAll() - .iterator() - .next() - .getName()); - - } catch (Exception e) { - e.printStackTrace(); - } + assertEquals( + "projects/" + projectId + "/locations/us-central1/glossaries/glossary-testing", + TranslateSnippetsbeta.listGlossary(projectId, "us-central1", "") + .iterateAll() + .iterator() + .next() + .getName()); } @Test public void test2_testGetGlossary() { - // [START translate_get_glossary_beta] - // TODO(developer): Uncomment these lines. - // import com.google.cloud.translate.*; - try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { - String location = "us-central1"; - String formattedParent = TranslationServiceClient.formatLocationName(projectId, location); - GlossaryName glossaryName = - GlossaryName.newBuilder() - .setProject(projectId) - .setLocation(location) - .setGlossary("glossary-testing") - .build(); - - Glossary response = translationServiceClient.getGlossary(glossaryName.toString()); - System.out.format("Got: %s\n", response.getName()); - // [END translate_get_glossary_beta] - assertEquals(glossaryName.toString(), response.getName()); - - } catch (Exception e) { - e.printStackTrace(); - } + Glossary response = + TranslateSnippetsbeta.getGlossary(projectId, "us-central1", "glossary-testing"); + assertTrue(response.toString(), response.toString().contains("glossary-testing")); } @Test - public void test3_testDeleteGlossary() { - // [START translate_delete_glossary_beta] - // TODO(developer): Uncomment these lines. - // import com.google.cloud.translate.*; - try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { - String location = "us-central1"; - GlossaryName glossaryName = - GlossaryName.newBuilder() - .setProject(projectId) - .setLocation(location) - .setGlossary("glossary-testing") - .build(); - - DeleteGlossaryResponse response = - translationServiceClient - .deleteGlossaryAsync(glossaryName.toString()) - .get(300, TimeUnit.SECONDS); - - System.out.format("Deleted: %s\n", response.getName()); - // [END translate_delete_glossary_beta] - assertEquals(glossaryName.toString(), response.getName()); + public void test2_testTranslateTextWithGlossary() { - } catch (Exception e) { - e.printStackTrace(); - } + TranslateTextResponse response = + TranslateSnippetsbeta.translateTextWithGlossary( + projectId, "us-central1", "glossary-testing", "directions", "en", "es"); + assertEquals("direcciones", response.getTranslationsList().get(0).getTranslatedText()); } @Test - public void test2_testTranslateTextWithGlossary() { - // [START translate_translate_text_with_glossary_beta] - // TODO(developer): Uncomment these lines. - // import com.google.cloud.translate.*; - try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) { - String location = "us-central1"; - String content = "directions"; - String formattedParent = TranslationServiceClient.formatLocationName(projectId, location); - GlossaryName glossaryName = - GlossaryName.newBuilder() - .setProject(projectId) - .setLocation(location) - .setGlossary("glossary-testing") - .build(); - TranslateTextGlossaryConfig translateTextGlossaryConfig = - TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build(); - TranslateTextRequest translateTextRequest = - TranslateTextRequest.newBuilder() - .setParent(formattedParent) - .setMimeType("text/plain") - .setSourceLanguageCode("en") - .setTargetLanguageCode("es") - .addContents(content) - .setGlossaryConfig(translateTextGlossaryConfig) - .build(); - - TranslateTextResponse response = translationServiceClient.translateText(translateTextRequest); - // [END translate_translate_text_with_glossary_beta] - assertEquals("direcciones", response.getTranslationsList().get(0).getTranslatedText()); + public void test3_testDeleteGlossary() { - } catch (Exception e) { - e.printStackTrace(); - } + DeleteGlossaryResponse response = + TranslateSnippetsbeta.deleteGlossary(projectId, "us-central1", "glossary-testing"); + assertTrue(response.toString(), response.toString().contains("glossary-testing")); } } From 07a84f43d585ead4fe638d9e7dd059d1be94ed1b Mon Sep 17 00:00:00 2001 From: nirupa-kumar Date: Wed, 3 Apr 2019 08:54:11 -0700 Subject: [PATCH 4/4] Fixing coding format/style --- ...tsbeta.java => TranslateSnippetsBeta.java} | 4 +- ...beta.java => ITTranslateSnippetsBeta.java} | 41 +++++-------------- 2 files changed, 13 insertions(+), 32 deletions(-) rename google-cloud-examples/src/main/java/com/google/cloud/examples/translate/snippets/{TranslateSnippetsbeta.java => TranslateSnippetsBeta.java} (99%) rename google-cloud-examples/src/test/java/com/google/cloud/examples/translate/snippets/{ITTranslateSnippetsbeta.java => ITTranslateSnippetsBeta.java} (81%) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/translate/snippets/TranslateSnippetsbeta.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/translate/snippets/TranslateSnippetsBeta.java similarity index 99% rename from google-cloud-examples/src/main/java/com/google/cloud/examples/translate/snippets/TranslateSnippetsbeta.java rename to google-cloud-examples/src/main/java/com/google/cloud/examples/translate/snippets/TranslateSnippetsBeta.java index 74b0cea0403c..9db6225c8efa 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/translate/snippets/TranslateSnippetsbeta.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/translate/snippets/TranslateSnippetsBeta.java @@ -44,8 +44,8 @@ import java.util.concurrent.TimeUnit; /** Snippets for the beta features */ -public class TranslateSnippetsbeta { - private TranslateSnippetsbeta() {} +public class TranslateSnippetsBeta { + private TranslateSnippetsBeta() {} /** * Lists all the supported language codes. diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/translate/snippets/ITTranslateSnippetsbeta.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/translate/snippets/ITTranslateSnippetsBeta.java similarity index 81% rename from google-cloud-examples/src/test/java/com/google/cloud/examples/translate/snippets/ITTranslateSnippetsbeta.java rename to google-cloud-examples/src/test/java/com/google/cloud/examples/translate/snippets/ITTranslateSnippetsBeta.java index b73641b8f49a..efa71e66f3d1 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/translate/snippets/ITTranslateSnippetsbeta.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/translate/snippets/ITTranslateSnippetsBeta.java @@ -28,41 +28,22 @@ import com.google.cloud.translate.v3beta1.DeleteGlossaryResponse; import com.google.cloud.translate.v3beta1.Glossary; import com.google.cloud.translate.v3beta1.TranslateTextResponse; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import org.junit.After; -import org.junit.Before; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; @FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class ITTranslateSnippetsbeta { +public class ITTranslateSnippetsBeta { private static final String projectId = System.getenv("PROJECT_ID"); private static final String glossaryName = "glossary-testing"; - private ByteArrayOutputStream bout; - private PrintStream out; - - @Before - public void setUp() { - bout = new ByteArrayOutputStream(); - out = new PrintStream(bout); - System.setOut(out); - } - - @After - public void tearDown() { - System.setOut(null); - } - @Test public void test1_testListSupportedLanguages() { assertTrue( 10 - <= TranslateSnippetsbeta.listSupportedLanguages(projectId, "global") + <= TranslateSnippetsBeta.listSupportedLanguages(projectId, "global") .getLanguagesList() .size()); } @@ -72,7 +53,7 @@ public void test1_testListSupportedLanguagesWithTarget() { assertTrue( 0 - < TranslateSnippetsbeta.listSupportedLanguagesWithTarget(projectId, "global") + < TranslateSnippetsBeta.listSupportedLanguagesWithTarget(projectId, "global") .getLanguagesList() .size()); } @@ -82,7 +63,7 @@ public void test1_testDetectLanguageOfText() { assertEquals( "is", - TranslateSnippetsbeta.detectLanguageOfText(projectId, "global", "Hæ sæta") + TranslateSnippetsBeta.detectLanguageOfText(projectId, "global", "Hæ sæta") .getLanguages(0) .getLanguageCode()); } @@ -92,7 +73,7 @@ public void test1_testTranslateText() { assertEquals( "Здраво Свете!", - TranslateSnippetsbeta.translateText(projectId, "global", "Hello World!", "en", "sr") + TranslateSnippetsBeta.translateText(projectId, "global", "Hello World!", "en", "sr") .getTranslationsList() .get(0) .getTranslatedText()); @@ -102,7 +83,7 @@ public void test1_testTranslateText() { public void test1_testBatchTranslateText() { BatchTranslateResponse response = - TranslateSnippetsbeta.batchTranslateText( + TranslateSnippetsBeta.batchTranslateText( projectId, "us-central1", "gs://cloud-samples-data/translation/text.txt", @@ -138,7 +119,7 @@ private void deleteDirectory(Storage storage, Page blobs) { @Test public void test1_testCreateGlossary() { Glossary response = - TranslateSnippetsbeta.createGlossary( + TranslateSnippetsBeta.createGlossary( projectId, "us-central1", glossaryName, @@ -150,7 +131,7 @@ public void test1_testCreateGlossary() { public void test2_testListGlossary() { assertEquals( "projects/" + projectId + "/locations/us-central1/glossaries/glossary-testing", - TranslateSnippetsbeta.listGlossary(projectId, "us-central1", "") + TranslateSnippetsBeta.listGlossary(projectId, "us-central1", "") .iterateAll() .iterator() .next() @@ -161,7 +142,7 @@ public void test2_testListGlossary() { public void test2_testGetGlossary() { Glossary response = - TranslateSnippetsbeta.getGlossary(projectId, "us-central1", "glossary-testing"); + TranslateSnippetsBeta.getGlossary(projectId, "us-central1", "glossary-testing"); assertTrue(response.toString(), response.toString().contains("glossary-testing")); } @@ -169,7 +150,7 @@ public void test2_testGetGlossary() { public void test2_testTranslateTextWithGlossary() { TranslateTextResponse response = - TranslateSnippetsbeta.translateTextWithGlossary( + TranslateSnippetsBeta.translateTextWithGlossary( projectId, "us-central1", "glossary-testing", "directions", "en", "es"); assertEquals("direcciones", response.getTranslationsList().get(0).getTranslatedText()); } @@ -178,7 +159,7 @@ public void test2_testTranslateTextWithGlossary() { public void test3_testDeleteGlossary() { DeleteGlossaryResponse response = - TranslateSnippetsbeta.deleteGlossary(projectId, "us-central1", "glossary-testing"); + TranslateSnippetsBeta.deleteGlossary(projectId, "us-central1", "glossary-testing"); assertTrue(response.toString(), response.toString().contains("glossary-testing")); } }