diff --git a/.gitignore b/.gitignore index 7f3c0d3a9ef..86dc131d1b2 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ release.properties dependency-reduced-pom.xml buildNumber.properties +service-account.json diff --git a/.travis.yml b/.travis.yml index 7a63b1470de..8fdf653290f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/bigquery/src/main/resources/constants.json b/bigquery/src/main/resources/constants.json index 1a2eb4b69f2..924d2544f3e 100644 --- a/bigquery/src/main/resources/constants.json +++ b/bigquery/src/main/resources/constants.json @@ -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;" } diff --git a/cloud-storage/xml-api/cmdline-sample/pom.xml b/cloud-storage/xml-api/cmdline-sample/pom.xml index da2a28d8e10..e8d52b3237d 100644 --- a/cloud-storage/xml-api/cmdline-sample/pom.xml +++ b/cloud-storage/xml-api/cmdline-sample/pom.xml @@ -13,7 +13,7 @@ com.google.apis-samples storage-cmd-line-sample 1 - Example for the Google Cloud Storage JSON API using OAuth 2.0. + Example for the Google Cloud Storage XML API using Application Default Credentials. @@ -31,6 +31,15 @@ StorageSample + + org.apache.maven.plugins + maven-compiler-plugin + 3.2 + + 7 + 7 + + ${project.artifactId}-${project.version} @@ -50,6 +59,30 @@ guava 18.0 + + + junit + junit + 4.10 + test + + + org.hamcrest + hamcrest-core + 1.3 + test + + + org.hamcrest + hamcrest-library + 1.3 + test + + + com.jcabi + jcabi-matchers + 1.3 + 1.20.0 diff --git a/cloud-storage/xml-api/cmdline-sample/src/main/java/StorageSample.java b/cloud-storage/xml-api/cmdline-sample/src/main/java/StorageSample.java index 2fbb86dc468..1228197310a 100644 --- a/cloud-storage/xml-api/cmdline-sample/src/main/java/StorageSample.java +++ b/cloud-storage/xml-api/cmdline-sample/src/main/java/StorageSample.java @@ -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. @@ -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"); @@ -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) { diff --git a/cloud-storage/xml-api/cmdline-sample/src/test/java/StorageSampleTest.java b/cloud-storage/xml-api/cmdline-sample/src/test/java/StorageSampleTest.java new file mode 100644 index 00000000000..a1030b6d8e5 --- /dev/null +++ b/cloud-storage/xml-api/cmdline-sample/src/test/java/StorageSampleTest.java @@ -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( + ".*cloud-samples-tests.*" + + ".*")); + } +} + +// [END StorageSampleTest] diff --git a/service-account.json.enc b/service-account.json.enc new file mode 100644 index 00000000000..5a3c4cad9b4 Binary files /dev/null and b/service-account.json.enc differ