Skip to content

Commit e75b417

Browse files
munkhuushmglanguillanneuf
authored andcommitted
chore: removed glossary from setup and teardown, Replaced with predef… (#527)
* chore: removed glossary from setup and teardown, Replaced with predefined glossary * chore: fixed nit
1 parent f841f74 commit e75b417

8 files changed

+23
-99
lines changed

translate/snippets/src/main/java/com/example/translate/BatchTranslateTextWithGlossary.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ public static void batchTranslateTextWithGlossary(
104104
client.batchTranslateTextAsync(request);
105105

106106
System.out.println("Waiting for operation to complete...");
107-
107+
108108
// random number between 300 - 450 (maximum allowed seconds)
109109
long randomNumber = ThreadLocalRandom.current().nextInt(300, 450);
110110
BatchTranslateResponse response = future.get(randomNumber, TimeUnit.SECONDS);
111-
111+
112112
// Display the translation for each input text provided
113113
System.out.printf("Total Characters: %s\n", response.getTotalCharacters());
114114
System.out.printf("Translated Characters: %s\n", response.getTranslatedCharacters());

translate/snippets/src/main/java/com/example/translate/BatchTranslateTextWithGlossaryAndModel.java

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

translate/snippets/src/test/java/com/example/translate/BatchTranslateTextWithGlossaryAndModelTests.java

+4-17
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
import java.io.ByteArrayOutputStream;
2828
import java.io.IOException;
2929
import java.io.PrintStream;
30-
import java.util.ArrayList;
31-
import java.util.List;
3230
import java.util.UUID;
3331
import java.util.concurrent.ExecutionException;
3432
import java.util.concurrent.TimeoutException;
@@ -47,11 +45,8 @@ public class BatchTranslateTextWithGlossaryAndModelTests {
4745
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
4846
private static final String INPUT_URI =
4947
"gs://cloud-samples-data/translation/text_with_custom_model_and_glossary.txt";
50-
private static final String GLOSSARY_ID =
51-
String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
48+
private static final String GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY";
5249
private static final String MODEL_ID = "TRL2188848820815848149";
53-
private static final String GLOSSARY_INPUT_URI =
54-
"gs://cloud-samples-data/translation/glossary_ja.csv";
5550
private static final String PREFIX = "BATCH_TRANSLATION_WITH_MODEL_OUTPUT/";
5651
private static final String OUTPUT_URI =
5752
String.format("gs://%s/%s%s/", PROJECT_ID, PREFIX, UUID.randomUUID());
@@ -100,14 +95,9 @@ public static void checkRequirements() {
10095
private PrintStream originalPrintStream;
10196

10297
@Before
103-
public void setUp() throws InterruptedException, ExecutionException, IOException {
104-
// Create a glossary that can be used in the test
98+
public void setUp() {
10599
PrintStream temp = new PrintStream(new ByteArrayOutputStream());
106100
System.setOut(temp);
107-
List<String> languageCodes = new ArrayList<>();
108-
languageCodes.add("en");
109-
languageCodes.add("ja");
110-
CreateGlossary.createGlossary(PROJECT_ID, GLOSSARY_ID, languageCodes, GLOSSARY_INPUT_URI);
111101

112102
bout = new ByteArrayOutputStream();
113103
out = new PrintStream(bout);
@@ -116,16 +106,13 @@ public void setUp() throws InterruptedException, ExecutionException, IOException
116106
}
117107

118108
@After
119-
public void tearDown() throws InterruptedException, ExecutionException, IOException {
109+
public void tearDown() {
120110
cleanUpBucket();
121-
// Delete the created glossary
122-
DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
123111
System.out.flush();
124112
System.setOut(originalPrintStream);
125113
}
126114

127-
@Rule
128-
public MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3);
115+
@Rule public MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3);
129116

130117
@Test
131118
public void testBatchTranslateTextWithGlossaryAndModel()

translate/snippets/src/test/java/com/example/translate/BatchTranslateTextWithGlossaryTests.java

+4-17
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
import java.io.ByteArrayOutputStream;
2828
import java.io.IOException;
2929
import java.io.PrintStream;
30-
import java.util.ArrayList;
31-
import java.util.List;
3230
import java.util.UUID;
3331
import java.util.concurrent.ExecutionException;
3432
import java.util.concurrent.TimeoutException;
@@ -47,10 +45,7 @@ public class BatchTranslateTextWithGlossaryTests {
4745
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
4846
private static final String INPUT_URI =
4947
"gs://cloud-samples-data/translation/text_with_glossary.txt";
50-
private static final String GLOSSARY_INPUT_URI =
51-
"gs://cloud-samples-data/translation/glossary_ja.csv";
52-
private static final String GLOSSARY_ID =
53-
String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
48+
private static final String GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY";
5449
private static final String PREFIX = "BATCH_TRANSLATION_GLOSSARY_OUTPUT/";
5550
private static final String OUTPUT_URI =
5651
String.format("gs://%s/%s%s/", PROJECT_ID, PREFIX, UUID.randomUUID());
@@ -98,14 +93,9 @@ public static void checkRequirements() {
9893
}
9994

10095
@Before
101-
public void setUp() throws InterruptedException, ExecutionException, IOException {
102-
// Create a glossary that can be used in the test
96+
public void setUp() {
10397
PrintStream temp = new PrintStream(new ByteArrayOutputStream());
10498
System.setOut(temp);
105-
List<String> languageCodes = new ArrayList<>();
106-
languageCodes.add("en");
107-
languageCodes.add("ja");
108-
CreateGlossary.createGlossary(PROJECT_ID, GLOSSARY_ID, languageCodes, GLOSSARY_INPUT_URI);
10999

110100
bout = new ByteArrayOutputStream();
111101
out = new PrintStream(bout);
@@ -114,17 +104,14 @@ public void setUp() throws InterruptedException, ExecutionException, IOException
114104
}
115105

116106
@After
117-
public void tearDown() throws InterruptedException, ExecutionException, IOException {
107+
public void tearDown() {
118108
// Clean up
119109
cleanUpBucket();
120-
// Delete the created glossary
121-
DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
122110
System.out.flush();
123111
System.setOut(originalPrintStream);
124112
}
125113

126-
@Rule
127-
public MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3);
114+
@Rule public MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3);
128115

129116
@Test
130117
public void testBatchTranslateTextWithGlossary()

translate/snippets/src/test/java/com/example/translate/GetGlossaryTests.java

+3-14
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222
import java.io.ByteArrayOutputStream;
2323
import java.io.IOException;
2424
import java.io.PrintStream;
25-
import java.util.ArrayList;
26-
import java.util.List;
27-
import java.util.UUID;
28-
import java.util.concurrent.ExecutionException;
2925
import org.junit.After;
3026
import org.junit.Before;
3127
import org.junit.BeforeClass;
@@ -40,8 +36,7 @@ public class GetGlossaryTests {
4036
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
4137
private static final String GLOSSARY_INPUT_URI =
4238
"gs://cloud-samples-data/translation/glossary_ja.csv";
43-
private static final String GLOSSARY_ID =
44-
String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
39+
private static final String GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY";
4540

4641
private ByteArrayOutputStream bout;
4742
private PrintStream out;
@@ -60,14 +55,10 @@ public static void checkRequirements() {
6055
}
6156

6257
@Before
63-
public void setUp() throws InterruptedException, ExecutionException, IOException {
58+
public void setUp() {
6459
// Create a glossary that can be used in the test
6560
PrintStream temp = new PrintStream(new ByteArrayOutputStream());
6661
System.setOut(temp);
67-
List<String> languageCodes = new ArrayList<>();
68-
languageCodes.add("en");
69-
languageCodes.add("ja");
70-
CreateGlossary.createGlossary(PROJECT_ID, GLOSSARY_ID, languageCodes, GLOSSARY_INPUT_URI);
7162

7263
bout = new ByteArrayOutputStream();
7364
out = new PrintStream(bout);
@@ -76,9 +67,7 @@ public void setUp() throws InterruptedException, ExecutionException, IOException
7667
}
7768

7869
@After
79-
public void tearDown() throws InterruptedException, ExecutionException, IOException {
80-
// Delete the created glossary
81-
DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
70+
public void tearDown() {
8271
// restores print statements in the original method
8372
System.out.flush();
8473
System.setOut(originalPrintStream);

translate/snippets/src/test/java/com/example/translate/ListGlossariesTests.java

+3-16
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222
import java.io.ByteArrayOutputStream;
2323
import java.io.IOException;
2424
import java.io.PrintStream;
25-
import java.util.ArrayList;
26-
import java.util.List;
27-
import java.util.UUID;
28-
import java.util.concurrent.ExecutionException;
2925
import org.junit.After;
3026
import org.junit.Before;
3127
import org.junit.BeforeClass;
@@ -41,8 +37,6 @@ public class ListGlossariesTests {
4137
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
4238
private static final String GLOSSARY_INPUT_URI =
4339
"gs://cloud-samples-data/translation/glossary_ja.csv";
44-
private static final String GLOSSARY_ID =
45-
String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
4640

4741
private ByteArrayOutputStream bout;
4842
private PrintStream out;
@@ -61,14 +55,9 @@ public static void checkRequirements() {
6155
}
6256

6357
@Before
64-
public void setUp() throws InterruptedException, ExecutionException, IOException {
65-
// Create a glossary that can be used in the test
58+
public void setUp() {
6659
PrintStream temp = new PrintStream(new ByteArrayOutputStream());
6760
System.setOut(temp);
68-
List<String> languageCodes = new ArrayList<>();
69-
languageCodes.add("en");
70-
languageCodes.add("ja");
71-
CreateGlossary.createGlossary(PROJECT_ID, GLOSSARY_ID, languageCodes, GLOSSARY_INPUT_URI);
7261

7362
bout = new ByteArrayOutputStream();
7463
out = new PrintStream(bout);
@@ -77,9 +66,7 @@ public void setUp() throws InterruptedException, ExecutionException, IOException
7766
}
7867

7968
@After
80-
public void tearDown() throws InterruptedException, ExecutionException, IOException {
81-
// Delete the created glossary
82-
DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
69+
public void tearDown() {
8370
// restores print statements in the original method
8471
System.out.flush();
8572
System.setOut(originalPrintStream);
@@ -89,7 +76,7 @@ public void tearDown() throws InterruptedException, ExecutionException, IOExcept
8976
public void testListGlossaries() throws IOException {
9077
ListGlossaries.listGlossaries(PROJECT_ID);
9178
String got = bout.toString();
92-
assertThat(got).contains(GLOSSARY_ID);
79+
assertThat(got).contains("Glossary name:");
9380
assertThat(got).contains(GLOSSARY_INPUT_URI);
9481
}
9582
}

translate/snippets/src/test/java/com/example/translate/TranslateTextWithGlossaryAndModelTests.java

+3-16
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222
import java.io.ByteArrayOutputStream;
2323
import java.io.IOException;
2424
import java.io.PrintStream;
25-
import java.util.ArrayList;
26-
import java.util.List;
27-
import java.util.UUID;
28-
import java.util.concurrent.ExecutionException;
2925
import org.junit.After;
3026
import org.junit.Before;
3127
import org.junit.BeforeClass;
@@ -41,8 +37,7 @@ public class TranslateTextWithGlossaryAndModelTests {
4137
private static final String MODEL_ID = "TRL2188848820815848149";
4238
private static final String GLOSSARY_INPUT_URI =
4339
"gs://cloud-samples-data/translation/glossary_ja.csv";
44-
private static final String GLOSSARY_ID =
45-
String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
40+
private static final String GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY";
4641

4742
private ByteArrayOutputStream bout;
4843
private PrintStream out;
@@ -61,14 +56,9 @@ public static void checkRequirements() {
6156
}
6257

6358
@Before
64-
public void setUp() throws InterruptedException, ExecutionException, IOException {
65-
// Create a glossary that can be used in the test
59+
public void setUp() {
6660
PrintStream temp = new PrintStream(new ByteArrayOutputStream());
6761
System.setOut(temp);
68-
List<String> languageCodes = new ArrayList<>();
69-
languageCodes.add("en");
70-
languageCodes.add("ja");
71-
CreateGlossary.createGlossary(PROJECT_ID, GLOSSARY_ID, languageCodes, GLOSSARY_INPUT_URI);
7262

7363
bout = new ByteArrayOutputStream();
7464
out = new PrintStream(bout);
@@ -77,10 +67,7 @@ public void setUp() throws InterruptedException, ExecutionException, IOException
7767
}
7868

7969
@After
80-
public void tearDown() throws InterruptedException, ExecutionException, IOException {
81-
// Clean up
82-
// Delete the created glossary
83-
DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
70+
public void tearDown() {
8471
// restores print statements in the original method
8572
System.out.flush();
8673
System.setOut(originalPrintStream);

translate/snippets/src/test/java/com/example/translate/TranslateTextWithGlossaryTests.java

+3-16
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222
import java.io.ByteArrayOutputStream;
2323
import java.io.IOException;
2424
import java.io.PrintStream;
25-
import java.util.ArrayList;
26-
import java.util.List;
27-
import java.util.UUID;
28-
import java.util.concurrent.ExecutionException;
2925
import org.junit.After;
3026
import org.junit.Before;
3127
import org.junit.BeforeClass;
@@ -40,8 +36,7 @@ public class TranslateTextWithGlossaryTests {
4036
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
4137
private static final String GLOSSARY_INPUT_URI =
4238
"gs://cloud-samples-data/translation/glossary_ja.csv";
43-
private static final String GLOSSARY_ID =
44-
String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
39+
private static final String GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY";
4540

4641
private ByteArrayOutputStream bout;
4742
private PrintStream out;
@@ -60,14 +55,9 @@ public static void checkRequirements() {
6055
}
6156

6257
@Before
63-
public void setUp() throws InterruptedException, ExecutionException, IOException {
64-
// Create a glossary that can be used in the test
58+
public void setUp() {
6559
PrintStream temp = new PrintStream(new ByteArrayOutputStream());
6660
System.setOut(temp);
67-
List<String> languageCodes = new ArrayList<>();
68-
languageCodes.add("en");
69-
languageCodes.add("ja");
70-
CreateGlossary.createGlossary(PROJECT_ID, GLOSSARY_ID, languageCodes, GLOSSARY_INPUT_URI);
7161

7262
bout = new ByteArrayOutputStream();
7363
out = new PrintStream(bout);
@@ -76,10 +66,7 @@ public void setUp() throws InterruptedException, ExecutionException, IOException
7666
}
7767

7868
@After
79-
public void tearDown() throws InterruptedException, ExecutionException, IOException {
80-
// Clean up
81-
// Delete the created glossary
82-
DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
69+
public void tearDown() {
8370
// restores print statements in the original method
8471
System.out.flush();
8572
System.setOut(originalPrintStream);

0 commit comments

Comments
 (0)