|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.translate; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static junit.framework.TestCase.assertNotNull; |
| 21 | + |
| 22 | +import com.google.api.gax.longrunning.OperationFuture; |
| 23 | +import com.google.api.gax.paging.Page; |
| 24 | +import com.google.cloud.storage.Blob; |
| 25 | +import com.google.cloud.storage.Storage; |
| 26 | +import com.google.cloud.storage.StorageOptions; |
| 27 | +import com.google.cloud.translate.v3.CreateGlossaryMetadata; |
| 28 | +import com.google.cloud.translate.v3.CreateGlossaryRequest; |
| 29 | +import com.google.cloud.translate.v3.DeleteGlossaryMetadata; |
| 30 | +import com.google.cloud.translate.v3.DeleteGlossaryRequest; |
| 31 | +import com.google.cloud.translate.v3.DeleteGlossaryResponse; |
| 32 | +import com.google.cloud.translate.v3.GcsSource; |
| 33 | +import com.google.cloud.translate.v3.Glossary; |
| 34 | +import com.google.cloud.translate.v3.GlossaryInputConfig; |
| 35 | +import com.google.cloud.translate.v3.GlossaryName; |
| 36 | +import com.google.cloud.translate.v3.LocationName; |
| 37 | +import com.google.cloud.translate.v3.TranslationServiceClient; |
| 38 | + |
| 39 | +import java.io.ByteArrayOutputStream; |
| 40 | +import java.io.IOException; |
| 41 | +import java.io.PrintStream; |
| 42 | +import java.util.ArrayList; |
| 43 | +import java.util.List; |
| 44 | +import java.util.UUID; |
| 45 | +import java.util.concurrent.ExecutionException; |
| 46 | + |
| 47 | +import org.junit.After; |
| 48 | +import org.junit.Before; |
| 49 | +import org.junit.BeforeClass; |
| 50 | +import org.junit.Test; |
| 51 | +import org.junit.runner.RunWith; |
| 52 | +import org.junit.runners.JUnit4; |
| 53 | + |
| 54 | +/** Tests for Batch Translate Text With Glossary and Model sample. */ |
| 55 | +@RunWith(JUnit4.class) |
| 56 | +@SuppressWarnings("checkstyle:abbreviationaswordinname") |
| 57 | +public class BatchTranslateTextWithGlossaryTests { |
| 58 | + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); |
| 59 | + private static final String INPUT_URI = |
| 60 | + "gs://cloud-samples-data/translation/text_with_glossary.txt"; |
| 61 | + private static final String GLOSSARY_INPUT_URI = |
| 62 | + "gs://cloud-samples-data/translation/glossary_ja.csv"; |
| 63 | + private static final String GLOSSARY_ID = |
| 64 | + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); |
| 65 | + |
| 66 | + private ByteArrayOutputStream bout; |
| 67 | + private PrintStream out; |
| 68 | + |
| 69 | + private static final void cleanUpBucket() { |
| 70 | + Storage storage = StorageOptions.getDefaultInstance().getService(); |
| 71 | + Page<Blob> blobs = |
| 72 | + storage.list( |
| 73 | + PROJECT_ID, |
| 74 | + Storage.BlobListOption.currentDirectory(), |
| 75 | + Storage.BlobListOption.prefix("BATCH_TRANSLATION_OUTPUT/")); |
| 76 | + |
| 77 | + deleteDirectory(storage, blobs); |
| 78 | + } |
| 79 | + |
| 80 | + private static void deleteDirectory(Storage storage, Page<Blob> blobs) { |
| 81 | + for (Blob blob : blobs.iterateAll()) { |
| 82 | + System.out.println(blob.getBlobId()); |
| 83 | + if (!blob.delete()) { |
| 84 | + Page<Blob> subBlobs = |
| 85 | + storage.list( |
| 86 | + PROJECT_ID, |
| 87 | + Storage.BlobListOption.currentDirectory(), |
| 88 | + Storage.BlobListOption.prefix(blob.getName())); |
| 89 | + |
| 90 | + deleteDirectory(storage, subBlobs); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private static void requireEnvVar(String varName) { |
| 96 | + assertNotNull( |
| 97 | + "Environment variable '%s' is required to perform these tests.".format(varName), |
| 98 | + System.getenv(varName)); |
| 99 | + } |
| 100 | + |
| 101 | + @BeforeClass |
| 102 | + public static void checkRequirements() { |
| 103 | + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); |
| 104 | + requireEnvVar("GOOGLE_CLOUD_PROJECT"); |
| 105 | + } |
| 106 | + |
| 107 | + @Before |
| 108 | + public void setUp() throws InterruptedException, ExecutionException, IOException { |
| 109 | + // Create a glossary that can be used in the test |
| 110 | + PrintStream temp = new PrintStream(new ByteArrayOutputStream()); |
| 111 | + System.setOut(temp); |
| 112 | + List<String> languageCodes = new ArrayList<>(); |
| 113 | + languageCodes.add("en"); |
| 114 | + languageCodes.add("ja"); |
| 115 | + CreateGlossary.createGlossary(PROJECT_ID, GLOSSARY_ID, languageCodes, GLOSSARY_INPUT_URI); |
| 116 | + |
| 117 | + bout = new ByteArrayOutputStream(); |
| 118 | + out = new PrintStream(bout); |
| 119 | + System.setOut(out); |
| 120 | + } |
| 121 | + |
| 122 | + @After |
| 123 | + public void tearDown() throws InterruptedException, ExecutionException, IOException { |
| 124 | + // Clean up |
| 125 | + cleanUpBucket(); |
| 126 | + // Delete the created glossary |
| 127 | + DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID); |
| 128 | + System.setOut(null); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testBatchTranslateTextWithGlossary() |
| 133 | + throws InterruptedException, ExecutionException, IOException { |
| 134 | + BatchTranslateTextWithGlossary.batchTranslateTextWithGlossary( |
| 135 | + PROJECT_ID, |
| 136 | + "en", |
| 137 | + "ja", |
| 138 | + INPUT_URI, |
| 139 | + "gs://" + PROJECT_ID + "/BATCH_TRANSLATION_OUTPUT/", |
| 140 | + GLOSSARY_ID); |
| 141 | + String got = bout.toString(); |
| 142 | + assertThat(got).contains("Total Characters: 9"); |
| 143 | + } |
| 144 | +} |
0 commit comments