Skip to content

Commit 5da7970

Browse files
committed
Add system tests to Translate sample.
1 parent cf58018 commit 5da7970

File tree

5 files changed

+113
-2
lines changed

5 files changed

+113
-2
lines changed

storage/cloud-client/src/main/java/com/example/storage/QuickstartSample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static void main(String... args) throws Exception {
2929
Storage storage = StorageOptions.defaultInstance().service();
3030

3131
// The name for the new bucket
32-
String bucketName = args[0];
32+
String bucketName = args[0]; // "my-new-bucket";
3333

3434
// Creates the new bucket
3535
Bucket bucket = storage.create(BucketInfo.of(bucketName));

translate/cloud-client/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Getting Started with Google Translate API and the Google Cloud Client libraries
2+
3+
[Google Translate API][translate] provides a simple programmatic interface for translating an
4+
arbitrary string into any supported language.
5+
These sample Java applications demonstrate how to access the Cloud Storage API using
6+
the [Google Cloud Client Library for Java][google-cloud-java].
7+
8+
[translate]: https://cloud.google.com/translate/
9+
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java
10+
11+
## Quickstart
12+
13+
Install [Maven](http://maven.apache.org/).
14+
15+
Build your project with:
16+
17+
mvn clean package -DskipTests
18+
19+
You can then run a given `ClassName` via:
20+
21+
mvn exec:java -Dexec.mainClass=com.example.translate.ClassName \
22+
-DpropertyName=propertyValue \
23+
-Dexec.args="any arguments to the app"
24+
25+
### Translate a string (using the quickstart sample)
26+
27+
mvn exec:java -Dexec.mainClass=com.example.translate.QuickstartSample \
28+
-Dexec.args="YOUR_API_KEY"

translate/cloud-client/pom.xml

+14
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,19 @@
3939
<artifactId>google-cloud-translate</artifactId>
4040
<version>0.4.0</version>
4141
</dependency>
42+
43+
<!-- Test dependencies -->
44+
<dependency>
45+
<groupId>junit</groupId>
46+
<artifactId>junit</artifactId>
47+
<version>4.12</version>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>com.google.truth</groupId>
52+
<artifactId>truth</artifactId>
53+
<version>0.30</version>
54+
<scope>test</scope>
55+
</dependency>
4256
</dependencies>
4357
</project>

translate/cloud-client/src/main/java/com/example/translate/QuickstartSample.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626
public class QuickstartSample {
2727
public static void main(String... args) throws Exception {
2828
// Instantiates a client
29-
Translate translate = TranslateOptions.builder().apiKey("YOUR_API_KEY").build().service();
29+
Translate translate =
30+
TranslateOptions.builder()
31+
.apiKey(args[0]) // .apiKey("YOUR_API_KEY")
32+
.build()
33+
.service();
3034

3135
// The text to translate
3236
String text = "Hello, world!";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Copyright 2016, Google, Inc.
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+
21+
import org.junit.After;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.junit.runners.JUnit4;
26+
27+
import java.io.ByteArrayOutputStream;
28+
import java.io.PrintStream;
29+
30+
/**
31+
* Tests for quickstart sample.
32+
*/
33+
@RunWith(JUnit4.class)
34+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
35+
public class QuickstartSampleIT {
36+
private ByteArrayOutputStream bout;
37+
private PrintStream out;
38+
39+
@Before
40+
public void setUp() {
41+
bout = new ByteArrayOutputStream();
42+
out = new PrintStream(bout);
43+
System.setOut(out);
44+
}
45+
46+
@After
47+
public void tearDown() {
48+
System.setOut(null);
49+
}
50+
51+
@Test
52+
public void testQuickstart() throws Exception {
53+
// Arrange
54+
String apiKey = System.getenv("GOOGLE_API_KEY");
55+
56+
// Act
57+
QuickstartSample.main(apiKey);
58+
59+
// Assert
60+
String got = bout.toString();
61+
assertThat(got).contains("Text: Hello, world!");
62+
assertThat(got).contains("Translation: ");
63+
}
64+
}
65+
// [END datastore_quickstart]

0 commit comments

Comments
 (0)