diff --git a/translate/cloud-client/pom.xml b/translate/cloud-client/pom.xml index 7e689cacae1..0693b19f577 100644 --- a/translate/cloud-client/pom.xml +++ b/translate/cloud-client/pom.xml @@ -43,7 +43,7 @@ See https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/The-Google com.google.cloud libraries-bom - 3.0.0 + 3.3.0 pom import diff --git a/translate/cloud-client/src/main/java/com/example/translate/CreateGlossary.java b/translate/cloud-client/src/main/java/com/example/translate/CreateGlossary.java new file mode 100644 index 00000000000..3dc9d8ff932 --- /dev/null +++ b/translate/cloud-client/src/main/java/com/example/translate/CreateGlossary.java @@ -0,0 +1,97 @@ +/* + * Copyright 2020 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.example.translate; + +// [START translate_v3_create_glossary] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.translate.v3.CreateGlossaryMetadata; +import com.google.cloud.translate.v3.CreateGlossaryRequest; +import com.google.cloud.translate.v3.GcsSource; +import com.google.cloud.translate.v3.Glossary; +import com.google.cloud.translate.v3.GlossaryInputConfig; +import com.google.cloud.translate.v3.GlossaryName; +import com.google.cloud.translate.v3.LocationName; +import com.google.cloud.translate.v3.TranslationServiceClient; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ExecutionException; + +public class CreateGlossary { + + public static void createGlossary() throws InterruptedException, ExecutionException, IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR-PROJECT-ID"; + String glossaryId = "your-glossary-display-name"; + List languageCodes = new ArrayList<>(); + languageCodes.add("your-language-code"); + String inputUri = "gs://your-gcs-bucket/path/to/input/file.txt"; + createGlossary(projectId, glossaryId, languageCodes, inputUri); + } + + // Create a equivalent term sets glossary + // https://cloud.google.com/translate/docs/advanced/glossary#format-glossary + public static void createGlossary( + String projectId, String glossaryId, List languageCodes, String inputUri) + throws IOException, ExecutionException, InterruptedException { + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (TranslationServiceClient client = TranslationServiceClient.create()) { + // Supported Locations: `global`, [glossary location], or [model location] + // Glossaries must be hosted in `us-central1` + // Custom Models must use the same location as your model. (us-central1) + String location = "us-central1"; + LocationName parent = LocationName.of(projectId, location); + GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId); + + // Supported Languages: https://cloud.google.com/translate/docs/languages + Glossary.LanguageCodesSet languageCodesSet = + Glossary.LanguageCodesSet.newBuilder().addAllLanguageCodes(languageCodes).build(); + + GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build(); + GlossaryInputConfig inputConfig = + GlossaryInputConfig.newBuilder().setGcsSource(gcsSource).build(); + + Glossary glossary = + Glossary.newBuilder() + .setName(glossaryName.toString()) + .setLanguageCodesSet(languageCodesSet) + .setInputConfig(inputConfig) + .build(); + + CreateGlossaryRequest request = + CreateGlossaryRequest.newBuilder() + .setParent(parent.toString()) + .setGlossary(glossary) + .build(); + + OperationFuture future = + client.createGlossaryAsync(request); + + System.out.println("Waiting for operation to complete..."); + Glossary response = future.get(); + System.out.println("Created Glossary."); + System.out.printf("Glossary name: %s\n", response.getName()); + System.out.printf("Entry count: %s\n", response.getEntryCount()); + System.out.printf("Input URI: %s\n", response.getInputConfig().getGcsSource().getInputUri()); + } + } +} +// [END translate_v3_create_glossary] diff --git a/translate/cloud-client/src/main/java/com/example/translate/DeleteGlossary.java b/translate/cloud-client/src/main/java/com/example/translate/DeleteGlossary.java new file mode 100644 index 00000000000..aaed8d7c4d5 --- /dev/null +++ b/translate/cloud-client/src/main/java/com/example/translate/DeleteGlossary.java @@ -0,0 +1,63 @@ +/* + * Copyright 2020 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.example.translate; + +// [START translate_v3_delete_glossary] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.translate.v3.DeleteGlossaryMetadata; +import com.google.cloud.translate.v3.DeleteGlossaryRequest; +import com.google.cloud.translate.v3.DeleteGlossaryResponse; +import com.google.cloud.translate.v3.GlossaryName; +import com.google.cloud.translate.v3.TranslationServiceClient; + +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +public class DeleteGlossary { + + public static void deleteGlossary() throws InterruptedException, ExecutionException, IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR-PROJECT-ID"; + String glossaryId = "your-glossary-display-name"; + deleteGlossary(projectId, glossaryId); + } + + // Delete a specific glossary based on the glossary ID + public static void deleteGlossary(String projectId, String glossaryId) + throws InterruptedException, ExecutionException, IOException { + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (TranslationServiceClient client = TranslationServiceClient.create()) { + // Supported Locations: `global`, [glossary location], or [model location] + // Glossaries must be hosted in `us-central1` + // Custom Models must use the same location as your model. (us-central1) + GlossaryName glossaryName = GlossaryName.of(projectId, "us-central1", glossaryId); + DeleteGlossaryRequest request = + DeleteGlossaryRequest.newBuilder().setName(glossaryName.toString()).build(); + + OperationFuture future = + client.deleteGlossaryAsync(request); + + System.out.println("Waiting for operation to complete..."); + DeleteGlossaryResponse response = future.get(); + System.out.format("Deleted Glossary: %s\n", response.getName()); + } + } +} +// [END translate_v3_delete_glossary] diff --git a/translate/cloud-client/src/main/java/com/example/translate/GetGlossary.java b/translate/cloud-client/src/main/java/com/example/translate/GetGlossary.java new file mode 100644 index 00000000000..e400f503ae0 --- /dev/null +++ b/translate/cloud-client/src/main/java/com/example/translate/GetGlossary.java @@ -0,0 +1,57 @@ +/* + * Copyright 2020 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.example.translate; + +// [START translate_v3_get_glossary] +import com.google.cloud.translate.v3.GetGlossaryRequest; +import com.google.cloud.translate.v3.Glossary; +import com.google.cloud.translate.v3.GlossaryName; +import com.google.cloud.translate.v3.TranslationServiceClient; + +import java.io.IOException; + +public class GetGlossary { + + public static void getGlossary() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR-PROJECT-ID"; + String glossaryId = "your-glossary-display-name"; + getGlossary(projectId, glossaryId); + } + + // Get a particular glossary based on the glossary ID + public static void getGlossary(String projectId, String glossaryId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (TranslationServiceClient client = TranslationServiceClient.create()) { + // Supported Locations: `global`, [glossary location], or [model location] + // Glossaries must be hosted in `us-central1` + // Custom Models must use the same location as your model. (us-central1) + GlossaryName glossaryName = GlossaryName.of(projectId, "us-central1", glossaryId); + GetGlossaryRequest request = + GetGlossaryRequest.newBuilder().setName(glossaryName.toString()).build(); + + Glossary response = client.getGlossary(request); + + System.out.printf("Glossary name: %s\n", response.getName()); + System.out.printf("Entry count: %s\n", response.getEntryCount()); + System.out.printf("Input URI: %s\n", response.getInputConfig().getGcsSource().getInputUri()); + } + } +} +// [END translate_v3_get_glossary] diff --git a/translate/cloud-client/src/main/java/com/example/translate/ListGlossaries.java b/translate/cloud-client/src/main/java/com/example/translate/ListGlossaries.java new file mode 100644 index 00000000000..055c2d14058 --- /dev/null +++ b/translate/cloud-client/src/main/java/com/example/translate/ListGlossaries.java @@ -0,0 +1,57 @@ +/* + * Copyright 2020 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.example.translate; + +// [START translate_v3_list_glossary] +import com.google.cloud.translate.v3.Glossary; +import com.google.cloud.translate.v3.ListGlossariesRequest; +import com.google.cloud.translate.v3.LocationName; +import com.google.cloud.translate.v3.TranslationServiceClient; + +import java.io.IOException; + +public class ListGlossaries { + + public static void listGlossaries() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR-PROJECT-ID"; + listGlossaries(projectId); + } + + // List all the glossaries in a specified location + public static void listGlossaries(String projectId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (TranslationServiceClient client = TranslationServiceClient.create()) { + // Supported Locations: `global`, [glossary location], or [model location] + // Glossaries must be hosted in `us-central1` + // Custom Models must use the same location as your model. (us-central1) + LocationName parent = LocationName.of(projectId, "us-central1"); + ListGlossariesRequest request = + ListGlossariesRequest.newBuilder().setParent(parent.toString()).build(); + + for (Glossary responseItem : client.listGlossaries(request).iterateAll()) { + System.out.printf("Glossary name: %s\n", responseItem.getName()); + System.out.printf("Entry count: %s\n", responseItem.getEntryCount()); + System.out.printf( + "Input URI: %s\n", responseItem.getInputConfig().getGcsSource().getInputUri()); + } + } + } +} +// [END translate_v3_list_glossary] diff --git a/translate/cloud-client/src/test/java/com/example/translate/CreateGlossaryTests.java b/translate/cloud-client/src/test/java/com/example/translate/CreateGlossaryTests.java new file mode 100644 index 00000000000..b3ccb01395e --- /dev/null +++ b/translate/cloud-client/src/test/java/com/example/translate/CreateGlossaryTests.java @@ -0,0 +1,89 @@ +/* + * Copyright 2020 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.example.translate; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.ExecutionException; + +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class CreateGlossaryTests { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String GLOSSARY_INPUT_URI = + "gs://cloud-samples-data/translation/glossary_ja.csv"; + private static final String GLOSSARY_ID = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + + private ByteArrayOutputStream bout; + private PrintStream out; + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable '%s' is required to perform these tests.".format(varName), + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() throws InterruptedException, ExecutionException, IOException { + // Delete the created glossary + DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID); + + System.setOut(null); + } + + @Test + public void testCreateGlossary() throws InterruptedException, ExecutionException, IOException { + List languageCodes = new ArrayList<>(); + languageCodes.add("en"); + languageCodes.add("ja"); + CreateGlossary.createGlossary(PROJECT_ID, GLOSSARY_ID, languageCodes, GLOSSARY_INPUT_URI); + + String got = bout.toString(); + assertThat(got).contains("Created"); + assertThat(got).contains(GLOSSARY_ID); + assertThat(got).contains(GLOSSARY_INPUT_URI); + } +} diff --git a/translate/cloud-client/src/test/java/com/example/translate/DeleteGlossaryTests.java b/translate/cloud-client/src/test/java/com/example/translate/DeleteGlossaryTests.java new file mode 100644 index 00000000000..8fe5fd660c4 --- /dev/null +++ b/translate/cloud-client/src/test/java/com/example/translate/DeleteGlossaryTests.java @@ -0,0 +1,91 @@ +/* + * Copyright 2020 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.example.translate; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.ExecutionException; + +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for Create and Delete Glossary samples. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DeleteGlossaryTests { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String GLOSSARY_INPUT_URI = + "gs://cloud-samples-data/translation/glossary_ja.csv"; + private static final String GLOSSARY_ID = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + + private ByteArrayOutputStream bout; + private PrintStream out; + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable '%s' is required to perform these tests.".format(varName), + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws InterruptedException, ExecutionException, IOException { + // Create a glossary to be deleted + PrintStream temp = new PrintStream(new ByteArrayOutputStream()); + System.setOut(temp); + List languageCodes = new ArrayList<>(); + languageCodes.add("en"); + languageCodes.add("ja"); + CreateGlossary.createGlossary(PROJECT_ID, GLOSSARY_ID, languageCodes, GLOSSARY_INPUT_URI); + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testDeleteGlossary() + throws InterruptedException, ExecutionException, IOException { + DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID); + String got = bout.toString(); + assertThat(got).contains("us-central1"); + assertThat(got).contains(GLOSSARY_ID); + } +} diff --git a/translate/cloud-client/src/test/java/com/example/translate/GetGlossaryTests.java b/translate/cloud-client/src/test/java/com/example/translate/GetGlossaryTests.java new file mode 100644 index 00000000000..d082c4fddb0 --- /dev/null +++ b/translate/cloud-client/src/test/java/com/example/translate/GetGlossaryTests.java @@ -0,0 +1,104 @@ +/* + * Copyright 2020 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.example.translate; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.translate.v3.CreateGlossaryMetadata; +import com.google.cloud.translate.v3.CreateGlossaryRequest; +import com.google.cloud.translate.v3.DeleteGlossaryMetadata; +import com.google.cloud.translate.v3.DeleteGlossaryRequest; +import com.google.cloud.translate.v3.DeleteGlossaryResponse; +import com.google.cloud.translate.v3.GcsSource; +import com.google.cloud.translate.v3.Glossary; +import com.google.cloud.translate.v3.GlossaryInputConfig; +import com.google.cloud.translate.v3.GlossaryName; +import com.google.cloud.translate.v3.LocationName; +import com.google.cloud.translate.v3.TranslationServiceClient; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.ExecutionException; + +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for Get Glossary sample. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class GetGlossaryTests { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String GLOSSARY_INPUT_URI = + "gs://cloud-samples-data/translation/glossary_ja.csv"; + private static final String GLOSSARY_ID = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + + private ByteArrayOutputStream bout; + private PrintStream out; + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable '%s' is required to perform these tests.".format(varName), + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws InterruptedException, ExecutionException, IOException { + // Create a glossary that can be used in the test + PrintStream temp = new PrintStream(new ByteArrayOutputStream()); + System.setOut(temp); + List languageCodes = new ArrayList<>(); + languageCodes.add("en"); + languageCodes.add("ja"); + CreateGlossary.createGlossary(PROJECT_ID, GLOSSARY_ID, languageCodes, GLOSSARY_INPUT_URI); + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() throws InterruptedException, ExecutionException, IOException { + // Delete the created glossary + DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID); + System.setOut(null); + } + + @Test + public void testGetGlossary() throws IOException { + GetGlossary.getGlossary(PROJECT_ID, GLOSSARY_ID); + String got = bout.toString(); + assertThat(got).contains(GLOSSARY_ID); + assertThat(got).contains(GLOSSARY_INPUT_URI); + } +} diff --git a/translate/cloud-client/src/test/java/com/example/translate/ListGlossariesTests.java b/translate/cloud-client/src/test/java/com/example/translate/ListGlossariesTests.java new file mode 100644 index 00000000000..ee87cfdf67f --- /dev/null +++ b/translate/cloud-client/src/test/java/com/example/translate/ListGlossariesTests.java @@ -0,0 +1,105 @@ +/* + * Copyright 2020 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.example.translate; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.translate.v3.CreateGlossaryMetadata; +import com.google.cloud.translate.v3.CreateGlossaryRequest; +import com.google.cloud.translate.v3.DeleteGlossaryMetadata; +import com.google.cloud.translate.v3.DeleteGlossaryRequest; +import com.google.cloud.translate.v3.DeleteGlossaryResponse; +import com.google.cloud.translate.v3.GcsSource; +import com.google.cloud.translate.v3.Glossary; +import com.google.cloud.translate.v3.GlossaryInputConfig; +import com.google.cloud.translate.v3.GlossaryName; +import com.google.cloud.translate.v3.LocationName; +import com.google.cloud.translate.v3.TranslationServiceClient; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.ExecutionException; + +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for List Glossaries sample. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class ListGlossariesTests { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String GLOSSARY_INPUT_URI = + "gs://cloud-samples-data/translation/glossary_ja.csv"; + private static final String GLOSSARY_ID = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + + private ByteArrayOutputStream bout; + private PrintStream out; + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable '%s' is required to perform these tests.".format(varName), + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws InterruptedException, ExecutionException, IOException { + // Create a glossary that can be used in the test + PrintStream temp = new PrintStream(new ByteArrayOutputStream()); + System.setOut(temp); + List languageCodes = new ArrayList<>(); + languageCodes.add("en"); + languageCodes.add("ja"); + CreateGlossary.createGlossary(PROJECT_ID, GLOSSARY_ID, languageCodes, GLOSSARY_INPUT_URI); + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() throws InterruptedException, ExecutionException, IOException { + // Delete the created glossary + DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID); + System.setOut(null); + } + + @Test + public void testListGlossaries() throws IOException { + ListGlossaries.listGlossaries(PROJECT_ID); + String got = bout.toString(); + assertThat(got).contains(GLOSSARY_ID); + assertThat(got).contains(GLOSSARY_INPUT_URI); + } +}