Skip to content

Commit

Permalink
chore: removed glossary from setup and teardown, Replaced with predef… (
Browse files Browse the repository at this point in the history
#527)

* chore: removed glossary from setup and teardown, Replaced with predefined glossary

* chore: fixed nit
  • Loading branch information
munkhuushmgl authored and anguillanneuf committed Dec 5, 2022
1 parent f841f74 commit e75b417
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ public static void batchTranslateTextWithGlossary(
client.batchTranslateTextAsync(request);

System.out.println("Waiting for operation to complete...");

// random number between 300 - 450 (maximum allowed seconds)
long randomNumber = ThreadLocalRandom.current().nextInt(300, 450);
BatchTranslateResponse response = future.get(randomNumber, TimeUnit.SECONDS);

// Display the translation for each input text provided
System.out.printf("Total Characters: %s\n", response.getTotalCharacters());
System.out.printf("Translated Characters: %s\n", response.getTranslatedCharacters());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static void batchTranslateTextWithGlossaryAndModel(
// random number between 300 - 450 (maximum allowed seconds)
long randomNumber = ThreadLocalRandom.current().nextInt(300, 450);
BatchTranslateResponse response = future.get(randomNumber, TimeUnit.SECONDS);

// Display the translation for each input text provided
System.out.printf("Total Characters: %s\n", response.getTotalCharacters());
System.out.printf("Translated Characters: %s\n", response.getTranslatedCharacters());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
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 java.util.concurrent.TimeoutException;
Expand All @@ -47,11 +45,8 @@ public class BatchTranslateTextWithGlossaryAndModelTests {
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private static final String INPUT_URI =
"gs://cloud-samples-data/translation/text_with_custom_model_and_glossary.txt";
private static final String GLOSSARY_ID =
String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
private static final String GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY";
private static final String MODEL_ID = "TRL2188848820815848149";
private static final String GLOSSARY_INPUT_URI =
"gs://cloud-samples-data/translation/glossary_ja.csv";
private static final String PREFIX = "BATCH_TRANSLATION_WITH_MODEL_OUTPUT/";
private static final String OUTPUT_URI =
String.format("gs://%s/%s%s/", PROJECT_ID, PREFIX, UUID.randomUUID());
Expand Down Expand Up @@ -100,14 +95,9 @@ public static void checkRequirements() {
private PrintStream originalPrintStream;

@Before
public void setUp() throws InterruptedException, ExecutionException, IOException {
// Create a glossary that can be used in the test
public void setUp() {
PrintStream temp = new PrintStream(new ByteArrayOutputStream());
System.setOut(temp);
List<String> 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);
Expand All @@ -116,16 +106,13 @@ public void setUp() throws InterruptedException, ExecutionException, IOException
}

@After
public void tearDown() throws InterruptedException, ExecutionException, IOException {
public void tearDown() {
cleanUpBucket();
// Delete the created glossary
DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
System.out.flush();
System.setOut(originalPrintStream);
}

@Rule
public MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3);
@Rule public MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3);

@Test
public void testBatchTranslateTextWithGlossaryAndModel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
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 java.util.concurrent.TimeoutException;
Expand All @@ -47,10 +45,7 @@ public class BatchTranslateTextWithGlossaryTests {
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private static final String INPUT_URI =
"gs://cloud-samples-data/translation/text_with_glossary.txt";
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 static final String GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY";
private static final String PREFIX = "BATCH_TRANSLATION_GLOSSARY_OUTPUT/";
private static final String OUTPUT_URI =
String.format("gs://%s/%s%s/", PROJECT_ID, PREFIX, UUID.randomUUID());
Expand Down Expand Up @@ -98,14 +93,9 @@ public static void checkRequirements() {
}

@Before
public void setUp() throws InterruptedException, ExecutionException, IOException {
// Create a glossary that can be used in the test
public void setUp() {
PrintStream temp = new PrintStream(new ByteArrayOutputStream());
System.setOut(temp);
List<String> 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);
Expand All @@ -114,17 +104,14 @@ public void setUp() throws InterruptedException, ExecutionException, IOException
}

@After
public void tearDown() throws InterruptedException, ExecutionException, IOException {
public void tearDown() {
// Clean up
cleanUpBucket();
// Delete the created glossary
DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
System.out.flush();
System.setOut(originalPrintStream);
}

@Rule
public MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3);
@Rule public MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3);

@Test
public void testBatchTranslateTextWithGlossary()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
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;
Expand All @@ -40,8 +36,7 @@ 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 static final String GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY";

private ByteArrayOutputStream bout;
private PrintStream out;
Expand All @@ -60,14 +55,10 @@ public static void checkRequirements() {
}

@Before
public void setUp() throws InterruptedException, ExecutionException, IOException {
public void setUp() {
// Create a glossary that can be used in the test
PrintStream temp = new PrintStream(new ByteArrayOutputStream());
System.setOut(temp);
List<String> 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);
Expand All @@ -76,9 +67,7 @@ public void setUp() throws InterruptedException, ExecutionException, IOException
}

@After
public void tearDown() throws InterruptedException, ExecutionException, IOException {
// Delete the created glossary
DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
public void tearDown() {
// restores print statements in the original method
System.out.flush();
System.setOut(originalPrintStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
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;
Expand All @@ -41,8 +37,6 @@ 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;
Expand All @@ -61,14 +55,9 @@ public static void checkRequirements() {
}

@Before
public void setUp() throws InterruptedException, ExecutionException, IOException {
// Create a glossary that can be used in the test
public void setUp() {
PrintStream temp = new PrintStream(new ByteArrayOutputStream());
System.setOut(temp);
List<String> 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);
Expand All @@ -77,9 +66,7 @@ public void setUp() throws InterruptedException, ExecutionException, IOException
}

@After
public void tearDown() throws InterruptedException, ExecutionException, IOException {
// Delete the created glossary
DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
public void tearDown() {
// restores print statements in the original method
System.out.flush();
System.setOut(originalPrintStream);
Expand All @@ -89,7 +76,7 @@ public void tearDown() throws InterruptedException, ExecutionException, IOExcept
public void testListGlossaries() throws IOException {
ListGlossaries.listGlossaries(PROJECT_ID);
String got = bout.toString();
assertThat(got).contains(GLOSSARY_ID);
assertThat(got).contains("Glossary name:");
assertThat(got).contains(GLOSSARY_INPUT_URI);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
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;
Expand All @@ -41,8 +37,7 @@ public class TranslateTextWithGlossaryAndModelTests {
private static final String MODEL_ID = "TRL2188848820815848149";
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 static final String GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY";

private ByteArrayOutputStream bout;
private PrintStream out;
Expand All @@ -61,14 +56,9 @@ public static void checkRequirements() {
}

@Before
public void setUp() throws InterruptedException, ExecutionException, IOException {
// Create a glossary that can be used in the test
public void setUp() {
PrintStream temp = new PrintStream(new ByteArrayOutputStream());
System.setOut(temp);
List<String> 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);
Expand All @@ -77,10 +67,7 @@ public void setUp() throws InterruptedException, ExecutionException, IOException
}

@After
public void tearDown() throws InterruptedException, ExecutionException, IOException {
// Clean up
// Delete the created glossary
DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
public void tearDown() {
// restores print statements in the original method
System.out.flush();
System.setOut(originalPrintStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
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;
Expand All @@ -40,8 +36,7 @@ public class TranslateTextWithGlossaryTests {
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 static final String GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY";

private ByteArrayOutputStream bout;
private PrintStream out;
Expand All @@ -60,14 +55,9 @@ public static void checkRequirements() {
}

@Before
public void setUp() throws InterruptedException, ExecutionException, IOException {
// Create a glossary that can be used in the test
public void setUp() {
PrintStream temp = new PrintStream(new ByteArrayOutputStream());
System.setOut(temp);
List<String> 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);
Expand All @@ -76,10 +66,7 @@ public void setUp() throws InterruptedException, ExecutionException, IOException
}

@After
public void tearDown() throws InterruptedException, ExecutionException, IOException {
// Clean up
// Delete the created glossary
DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
public void tearDown() {
// restores print statements in the original method
System.out.flush();
System.setOut(originalPrintStream);
Expand Down

0 comments on commit e75b417

Please sign in to comment.