Skip to content

Commit 2d3f224

Browse files
committed
Merge pull request GoogleCloudPlatform#16 from GoogleCloudPlatform/cloud-storage-tests
Add tests for cloud storage cmdline sample.
2 parents d918b3c + 6f23c41 commit 2d3f224

File tree

7 files changed

+150
-44
lines changed

7 files changed

+150
-44
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ release.properties
2525
dependency-reduced-pom.xml
2626
buildNumber.properties
2727

28+
service-account.json

.travis.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
language: java
2+
jdk:
3+
- oraclejdk7
4+
env: GOOGLE_APPLICATION_CREDENTIALS=$TRAVIS_BUILD_DIR/service-account.json
5+
before_install:
6+
- openssl aes-256-cbc -K $encrypted_99d8b304f94b_key -iv $encrypted_99d8b304f94b_iv -in service-account.json.enc -out service-account.json -d
27

3-
env:
4-
- TEST_DIR=unittests
5-
6-
script: cd $TEST_DIR && mvn test
8+
script: mvn test
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"projectId": "bigquery-devrel-samples",
3-
"datasetId": "test_dataset",
4-
"currentTableId": "test_table",
5-
"newTableId": "test_table2",
6-
"cloudStorageInputURI": "gs://bigquery-devrel-samples-bucket/data.csv",
7-
"cloudStorageOutputURI": "gs://bigquery-devrel-samples-bucket/output.csv",
2+
"projectId": "cloud-samples-tests",
3+
"datasetId": "test_dataset_java",
4+
"currentTableId": "test_table_java",
5+
"newTableId": "test_table_java_2",
6+
"cloudStorageInputURI": "gs://cloud-samples-tests/data.csv",
7+
"cloudStorageOutputURI": "gs://cloud-samples-tests/output.csv",
88
"query": "SELECT corpus FROM publicdata:samples.shakespeare GROUP BY corpus;"
99
}

cloud-storage/xml-api/cmdline-sample/pom.xml

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<groupId>com.google.apis-samples</groupId>
1414
<artifactId>storage-cmd-line-sample</artifactId>
1515
<version>1</version>
16-
<name>Example for the Google Cloud Storage JSON API using OAuth 2.0.</name>
16+
<name>Example for the Google Cloud Storage XML API using Application Default Credentials.</name>
1717
<build>
1818
<plugins>
1919
<plugin>
@@ -31,6 +31,15 @@
3131
<mainClass>StorageSample</mainClass>
3232
</configuration>
3333
</plugin>
34+
<plugin>
35+
<groupId>org.apache.maven.plugins</groupId>
36+
<artifactId>maven-compiler-plugin</artifactId>
37+
<version>3.2</version>
38+
<configuration>
39+
<source>7</source>
40+
<target>7</target>
41+
</configuration>
42+
</plugin>
3443
</plugins>
3544
<finalName>${project.artifactId}-${project.version}</finalName>
3645
</build>
@@ -50,6 +59,30 @@
5059
<artifactId>guava</artifactId>
5160
<version>18.0</version>
5261
</dependency>
62+
<!-- Test Dependencies -->
63+
<dependency>
64+
<groupId>junit</groupId>
65+
<artifactId>junit</artifactId>
66+
<version>4.10</version>
67+
<scope>test</scope>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.hamcrest</groupId>
71+
<artifactId>hamcrest-core</artifactId>
72+
<version>1.3</version>
73+
<scope>test</scope>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.hamcrest</groupId>
77+
<artifactId>hamcrest-library</artifactId>
78+
<version>1.3</version>
79+
<scope>test</scope>
80+
</dependency>
81+
<dependency>
82+
<groupId>com.jcabi</groupId>
83+
<artifactId>jcabi-matchers</artifactId>
84+
<version>1.3</version>
85+
</dependency>
5386
</dependencies>
5487
<properties>
5588
<project.http.version>1.20.0</project.http.version>

cloud-storage/xml-api/cmdline-sample/src/main/java/StorageSample.java

Lines changed: 66 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@
2626
import java.io.IOException;
2727
import java.io.StringReader;
2828
import java.io.StringWriter;
29-
import java.util.Collections;
3029
import java.net.URLEncoder;
30+
import java.util.Collections;
3131

3232
import javax.xml.transform.OutputKeys;
3333
import javax.xml.transform.Source;
3434
import javax.xml.transform.Transformer;
35+
import javax.xml.transform.TransformerException;
3536
import javax.xml.transform.TransformerFactory;
3637
import javax.xml.transform.stream.StreamResult;
3738
import javax.xml.transform.stream.StreamSource;
39+
import java.security.GeneralSecurityException;
3840

3941
/**
4042
* Sample code used in the Cloud Storage Java documentation.
@@ -49,43 +51,53 @@ private StorageSample() { }
4951
private static final String STORAGE_SCOPE =
5052
"https://www.googleapis.com/auth/devstorage.read_write";
5153

52-
/** Global instance of the HTTP transport. */
53-
private static HttpTransport httpTransport;
54+
/**
55+
* Fetches the listing of the given bucket.
56+
*
57+
* @param bucketName the name of the bucket to list.
58+
*
59+
* @return the raw XML containing the listing of the bucket.
60+
* @throws IOException if there's an error communicating with Cloud Storage.
61+
* @throws GeneralSecurityException for errors creating https connection.
62+
*/
63+
public static String listBucket(final String bucketName)
64+
throws IOException, GeneralSecurityException {
65+
//[START snippet]
66+
// Build an account credential.
67+
GoogleCredential credential = GoogleCredential.getApplicationDefault()
68+
.createScoped(Collections.singleton(STORAGE_SCOPE));
69+
70+
// Set up and execute a Google Cloud Storage request.
71+
String uri = "https://storage.googleapis.com/"
72+
+ URLEncoder.encode(bucketName, "UTF-8");
73+
74+
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
75+
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(
76+
credential);
77+
GenericUrl url = new GenericUrl(uri);
78+
79+
HttpRequest request = requestFactory.buildGetRequest(url);
80+
HttpResponse response = request.execute();
81+
String content = response.parseAsString();
82+
//[END snippet]
83+
84+
return content;
85+
}
5486

5587
/**
56-
* A command-line handler to display the bucket passed in as an argument.
88+
* Prints out the contents of the given xml, in a more readable form.
5789
*
58-
* @param args the array of command-line arguments.
90+
* @param bucketName the name of the bucket you're listing.
91+
* @param content the raw XML string.
5992
*/
60-
public static void main(final String[] args) {
61-
try {
62-
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
63-
// Check for valid setup.
64-
Preconditions.checkArgument(args.length == 1,
65-
"Please pass in the Google Cloud Storage bucket name to display");
66-
String bucketName = args[0];
93+
private static void prettyPrintXml(
94+
final String bucketName, final String content) {
95+
// Instantiate transformer input.
96+
Source xmlInput = new StreamSource(new StringReader(content));
97+
StreamResult xmlOutput = new StreamResult(new StringWriter());
6798

68-
//[START snippet]
69-
// Build an account credential.
70-
GoogleCredential credential = GoogleCredential.getApplicationDefault()
71-
.createScoped(Collections.singleton(STORAGE_SCOPE));
72-
73-
// Set up and execute a Google Cloud Storage request.
74-
String uri = "https://storage.googleapis.com/"
75-
+ URLEncoder.encode(bucketName, "UTF-8");
76-
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(
77-
credential);
78-
GenericUrl url = new GenericUrl(uri);
79-
HttpRequest request = requestFactory.buildGetRequest(url);
80-
HttpResponse response = request.execute();
81-
String content = response.parseAsString();
82-
//[END snippet]
83-
84-
// Instantiate transformer input.
85-
Source xmlInput = new StreamSource(new StringReader(content));
86-
StreamResult xmlOutput = new StreamResult(new StringWriter());
87-
88-
// Configure transformer.
99+
// Configure transformer.
100+
try {
89101
Transformer transformer = TransformerFactory.newInstance()
90102
.newTransformer(); // An identity transformer
91103
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd");
@@ -98,6 +110,27 @@ public static void main(final String[] args) {
98110
// Pretty print the output XML.
99111
System.out.println("\nBucket listing for " + bucketName + ":\n");
100112
System.out.println(xmlOutput.getWriter().toString());
113+
} catch (TransformerException e) {
114+
e.printStackTrace();
115+
}
116+
}
117+
118+
/**
119+
* A command-line handler to display the bucket passed in as an argument.
120+
*
121+
* @param args the array of command-line arguments.
122+
*/
123+
public static void main(final String[] args) {
124+
try {
125+
// Check for valid setup.
126+
Preconditions.checkArgument(
127+
args.length == 1,
128+
"Please pass in the Google Cloud Storage bucket name to display");
129+
String bucketName = args[0];
130+
131+
String content = listBucket(bucketName);
132+
133+
prettyPrintXml(bucketName, content);
101134
System.exit(0);
102135

103136
} catch (IOException e) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright 2015 Google Inc. All Rights Reserved.
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+
// [START StorageSampleTest]
18+
19+
import static com.jcabi.matchers.RegexMatchers.*;
20+
import static org.junit.Assert.assertThat;
21+
22+
import org.junit.Test;
23+
24+
import java.util.regex.Pattern;
25+
26+
public class StorageSampleTest {
27+
@Test
28+
public void testListBucket() throws Exception {
29+
String listing = StorageSample.listBucket("cloud-samples-tests");
30+
assertThat(listing, matchesPattern(
31+
".*<ListBucketResult.*"
32+
+ "<Name>cloud-samples-tests</Name>.*"
33+
+ "</ListBucketResult>.*"));
34+
}
35+
}
36+
37+
// [END StorageSampleTest]

service-account.json.enc

2 KB
Binary file not shown.

0 commit comments

Comments
 (0)