Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for cloud storage cmdline sample. #16

Merged
merged 1 commit into from
Jun 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ release.properties
dependency-reduced-pom.xml
buildNumber.properties

service-account.json
10 changes: 6 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
language: java
jdk:
- oraclejdk7
env: GOOGLE_APPLICATION_CREDENTIALS=$TRAVIS_BUILD_DIR/service-account.json
before_install:
- openssl aes-256-cbc -K $encrypted_99d8b304f94b_key -iv $encrypted_99d8b304f94b_iv -in service-account.json.enc -out service-account.json -d

env:
- TEST_DIR=unittests

script: cd $TEST_DIR && mvn test
script: mvn test
12 changes: 6 additions & 6 deletions bigquery/src/main/resources/constants.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"projectId": "bigquery-devrel-samples",
"datasetId": "test_dataset",
"currentTableId": "test_table",
"newTableId": "test_table2",
"cloudStorageInputURI": "gs://bigquery-devrel-samples-bucket/data.csv",
"cloudStorageOutputURI": "gs://bigquery-devrel-samples-bucket/output.csv",
"projectId": "cloud-samples-tests",
"datasetId": "test_dataset_java",
"currentTableId": "test_table_java",
"newTableId": "test_table_java_2",
"cloudStorageInputURI": "gs://cloud-samples-tests/data.csv",
"cloudStorageOutputURI": "gs://cloud-samples-tests/output.csv",
"query": "SELECT corpus FROM publicdata:samples.shakespeare GROUP BY corpus;"
}
35 changes: 34 additions & 1 deletion cloud-storage/xml-api/cmdline-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<groupId>com.google.apis-samples</groupId>
<artifactId>storage-cmd-line-sample</artifactId>
<version>1</version>
<name>Example for the Google Cloud Storage JSON API using OAuth 2.0.</name>
<name>Example for the Google Cloud Storage XML API using Application Default Credentials.</name>
<build>
<plugins>
<plugin>
Expand All @@ -31,6 +31,15 @@
<mainClass>StorageSample</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
<finalName>${project.artifactId}-${project.version}</finalName>
</build>
Expand All @@ -50,6 +59,30 @@
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-matchers</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
<properties>
<project.http.version>1.20.0</project.http.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Collections;
import java.net.URLEncoder;
import java.util.Collections;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.security.GeneralSecurityException;

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

/** Global instance of the HTTP transport. */
private static HttpTransport httpTransport;
/**
* Fetches the listing of the given bucket.
*
* @param bucketName the name of the bucket to list.
*
* @return the raw XML containing the listing of the bucket.
* @throws IOException if there's an error communicating with Cloud Storage.
* @throws GeneralSecurityException for errors creating https connection.
*/
public static String listBucket(final String bucketName)
throws IOException, GeneralSecurityException {
//[START snippet]
// Build an account credential.
GoogleCredential credential = GoogleCredential.getApplicationDefault()
.createScoped(Collections.singleton(STORAGE_SCOPE));

// Set up and execute a Google Cloud Storage request.
String uri = "https://storage.googleapis.com/"
+ URLEncoder.encode(bucketName, "UTF-8");

HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(
credential);
GenericUrl url = new GenericUrl(uri);

HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse response = request.execute();
String content = response.parseAsString();
//[END snippet]

return content;
}

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

//[START snippet]
// Build an account credential.
GoogleCredential credential = GoogleCredential.getApplicationDefault()
.createScoped(Collections.singleton(STORAGE_SCOPE));

// Set up and execute a Google Cloud Storage request.
String uri = "https://storage.googleapis.com/"
+ URLEncoder.encode(bucketName, "UTF-8");
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(
credential);
GenericUrl url = new GenericUrl(uri);
HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse response = request.execute();
String content = response.parseAsString();
//[END snippet]

// Instantiate transformer input.
Source xmlInput = new StreamSource(new StringReader(content));
StreamResult xmlOutput = new StreamResult(new StringWriter());

// Configure transformer.
// Configure transformer.
try {
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(); // An identity transformer
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd");
Expand All @@ -98,6 +110,27 @@ public static void main(final String[] args) {
// Pretty print the output XML.
System.out.println("\nBucket listing for " + bucketName + ":\n");
System.out.println(xmlOutput.getWriter().toString());
} catch (TransformerException e) {
e.printStackTrace();
}
}

/**
* A command-line handler to display the bucket passed in as an argument.
*
* @param args the array of command-line arguments.
*/
public static void main(final String[] args) {
try {
// Check for valid setup.
Preconditions.checkArgument(
args.length == 1,
"Please pass in the Google Cloud Storage bucket name to display");
String bucketName = args[0];

String content = listBucket(bucketName);

prettyPrintXml(bucketName, content);
System.exit(0);

} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// [START StorageSampleTest]

import static com.jcabi.matchers.RegexMatchers.*;
import static org.junit.Assert.assertThat;

import org.junit.Test;

import java.util.regex.Pattern;

public class StorageSampleTest {
@Test
public void testListBucket() throws Exception {
String listing = StorageSample.listBucket("cloud-samples-tests");
assertThat(listing, matchesPattern(
".*<ListBucketResult.*"
+ "<Name>cloud-samples-tests</Name>.*"
+ "</ListBucketResult>.*"));
}
}

// [END StorageSampleTest]
Binary file added service-account.json.enc
Binary file not shown.