Skip to content

Commit

Permalink
Prefer integration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerjou Cheng committed Jun 17, 2015
1 parent 8c94433 commit 79e8ddd
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 48 deletions.
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
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
28 changes: 24 additions & 4 deletions cloud-storage/xml-api/cmdline-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -57,12 +66,23 @@
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.4</version>
<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 @@ -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.
Expand All @@ -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()
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
".*<ListBucketResult.*"
+ "<Name>cloud-samples-tests</Name>.*"
+ "</ListBucketResult>.*"));
}
}

Expand Down
Binary file added service-account.json.enc
Binary file not shown.

0 comments on commit 79e8ddd

Please sign in to comment.