From 79e8ddd76e7b0663c75c6b2cf3082949f944d5db Mon Sep 17 00:00:00 2001 From: Jerjou Cheng Date: Wed, 17 Jun 2015 15:42:34 -0700 Subject: [PATCH] Prefer integration tests. --- .gitignore | 1 + .travis.yml | 5 ++ cloud-storage/xml-api/cmdline-sample/pom.xml | 28 +++++++++-- .../src/main/java/StorageSample.java | 15 +++--- .../src/test/java/StorageSampleTest.java | 45 ++++-------------- service-account.json.enc | Bin 0 -> 2048 bytes 6 files changed, 46 insertions(+), 48 deletions(-) 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 9c57f7539a9..8fdf653290f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +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 script: mvn test diff --git a/cloud-storage/xml-api/cmdline-sample/pom.xml b/cloud-storage/xml-api/cmdline-sample/pom.xml index 86c3f2fc983..e8d52b3237d 100644 --- a/cloud-storage/xml-api/cmdline-sample/pom.xml +++ b/cloud-storage/xml-api/cmdline-sample/pom.xml @@ -31,6 +31,15 @@ StorageSample + + org.apache.maven.plugins + maven-compiler-plugin + 3.2 + + 7 + 7 + + ${project.artifactId}-${project.version} @@ -57,12 +66,23 @@ 4.10 test - - org.mockito - mockito-all - 1.8.4 + + 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 1b800f3e706..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 @@ -36,6 +36,7 @@ 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. @@ -54,14 +55,13 @@ private StorageSample() { } * Fetches the listing of the given bucket. * * @param bucketName the name of the bucket to list. - * @param httpTransport the httpTransport to use (mainly for testing). * * @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, final HttpTransport httpTransport) - throws IOException { + public static String listBucket(final String bucketName) + throws IOException, GeneralSecurityException { //[START snippet] // Build an account credential. GoogleCredential credential = GoogleCredential.getApplicationDefault() @@ -70,9 +70,12 @@ public static String listBucket( // 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(); @@ -124,10 +127,8 @@ public static void main(final String[] args) { args.length == 1, "Please pass in the Google Cloud Storage bucket name to display"); String bucketName = args[0]; - HttpTransport httpTransport = - GoogleNetHttpTransport.newTrustedTransport(); - String content = listBucket(bucketName, httpTransport); + String content = listBucket(bucketName); prettyPrintXml(bucketName, content); System.exit(0); 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 index 8922a8f0186..a1030b6d8e5 100644 --- a/cloud-storage/xml-api/cmdline-sample/src/test/java/StorageSampleTest.java +++ b/cloud-storage/xml-api/cmdline-sample/src/test/java/StorageSampleTest.java @@ -16,50 +16,21 @@ // [START StorageSampleTest] -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.spy; +import static com.jcabi.matchers.RegexMatchers.*; +import static org.junit.Assert.assertThat; -import com.google.api.client.testing.http.MockHttpTransport; -import com.google.api.client.testing.http.MockLowLevelHttpRequest; -import com.google.api.client.testing.http.MockLowLevelHttpResponse; - -import org.junit.After; -import org.junit.Before; import org.junit.Test; -import org.mockito.MockitoAnnotations; -import java.io.IOException; +import java.util.regex.Pattern; public class StorageSampleTest { - private MockHttpTransport transport; - private MockLowLevelHttpRequest request; - - private void stubRequest(String url, int statusCode, String responseContent) - throws IOException { - request.setResponse(new MockLowLevelHttpResponse() - .setStatusCode(statusCode) - .setContent(responseContent)); - doReturn(request).when(transport).buildRequest("GET", url); - } - - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - transport = spy(new MockHttpTransport()); - request = spy(new MockLowLevelHttpRequest()); - } - - @After - public void tearDown() { - } - - @Test public void testListBucket() throws Exception { - stubRequest("https://storage.googleapis.com/test-bucket", 200, "listing"); - String listing = StorageSample.listBucket("test-bucket", transport); - assertEquals("listing", listing); + String listing = StorageSample.listBucket("cloud-samples-tests"); + assertThat(listing, matchesPattern( + ".*cloud-samples-tests.*" + + ".*")); } } 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`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