From 6f23c4160acbde2294b4c3895d1e6905e9bd5acd Mon Sep 17 00:00:00 2001 From: Jerjou Cheng Date: Mon, 15 Jun 2015 10:51:41 -0700 Subject: [PATCH] Add tests for cloud storage cmdline sample. Also, include all tests in travis run. --- .gitignore | 1 + .travis.yml | 10 +- bigquery/src/main/resources/constants.json | 12 +-- cloud-storage/xml-api/cmdline-sample/pom.xml | 35 ++++++- .../src/main/java/StorageSample.java | 99 ++++++++++++------ .../src/test/java/StorageSampleTest.java | 37 +++++++ service-account.json.enc | Bin 0 -> 2048 bytes 7 files changed, 150 insertions(+), 44 deletions(-) create mode 100644 cloud-storage/xml-api/cmdline-sample/src/test/java/StorageSampleTest.java create mode 100644 service-account.json.enc 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 0000000000000000000000000000000000000000..5a3c4cad9b45b13de4164688ee37d4f01b670ae5 GIT binary patch literal 2048 zcmV+b2>-v|YH6*}c-V<>? zF;*W79mgyI<3Y<@K?68KT4DWMg?H7Qx!bZby8gm-xIZ4a&I)eha(IlA(soyf98aXy zn?eNx*}%jj9^rF(ZGL0Q8Maa3mbrnE#yKhBW&DZP$`J%7Ny$aW<`#$W&%^e#|L$G4 znr5OqgLsmOJ=2CcJ7bB78Nh?5L;E3=j=L~02*n^jL*5S*sQJzo!=_MjYX=&LAv~0n z4z&?Z1xK`?(2nXl?f2|XmcIkK!?_7b>C);|pR8l1xgEYzl(|+<$ALD8Xa}=I=EP!zw)@x+4aO{4Z$hrjvur zlcE^D+018)3l+WmC1GuxYZM=+r^E^q{|c&70Q@>CyKz~&E?DVJLjV974rLia?mJR% z;92Y-o(3+29>HDWBVY&}MB8adlGHkNy)Khbz2on~h<@CfJS{0%6dAoa3->6yIpw63 z(}q5a#-=L)*DpOtP(!wj7S#rWbyIg%Ig3*3iv5H0+TW!gF{MKMjbgjgbj4c1fS_CJ z=q-0*{zlZ|ANE4TJTVL9{*+^A8PRMc!VqN`sBdGQAE-gm8!05WlPCT(%2@mcSV z-2&e;Z&t@s;^1UE(TizdK5z@^De*&%`H1$TmmS&XFrII#Hoj3}DB(IL{lc;KidQdT zU-%E^Oa1VI!>1RkRM}<-hridR4QIo*4*{G!oNnth5AL+JZpEW~@|XkI3gzA-2X(gg z)ufwksJtpe{;%<=Hu;n5-AS3!t(_;|25x-i4$vW5AfxQ#$F2z>Z2jBc{s?u^3iU<; zUVeSs`a{7Xa+V^QlakZ1PQKKr_y2*g!dacYe$^QfdR^uB%WWWa za@0b4=$W@JMGGw|=J9vK_9>Wm_SqupE>gNGw(_dxBk^&L0iB0dcbkGsJ#HH1^q7J& z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