Skip to content

Commit 79e8ddd

Browse files
author
Jerjou Cheng
committed
Prefer integration tests.
1 parent 8c94433 commit 79e8ddd

File tree

6 files changed

+46
-48
lines changed

6 files changed

+46
-48
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ release.properties
2525
dependency-reduced-pom.xml
2626
buildNumber.properties
2727

28+
service-account.json

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
language: java
2+
jdk:
3+
- oraclejdk7
4+
env: GOOGLE_APPLICATION_CREDENTIALS=$TRAVIS_BUILD_DIR/service-account.json
5+
before_install:
6+
- openssl aes-256-cbc -K $encrypted_99d8b304f94b_key -iv $encrypted_99d8b304f94b_iv -in service-account.json.enc -out service-account.json -d
27

38
script: mvn test

cloud-storage/xml-api/cmdline-sample/pom.xml

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@
3131
<mainClass>StorageSample</mainClass>
3232
</configuration>
3333
</plugin>
34+
<plugin>
35+
<groupId>org.apache.maven.plugins</groupId>
36+
<artifactId>maven-compiler-plugin</artifactId>
37+
<version>3.2</version>
38+
<configuration>
39+
<source>7</source>
40+
<target>7</target>
41+
</configuration>
42+
</plugin>
3443
</plugins>
3544
<finalName>${project.artifactId}-${project.version}</finalName>
3645
</build>
@@ -57,12 +66,23 @@
5766
<version>4.10</version>
5867
<scope>test</scope>
5968
</dependency>
60-
<dependency>
61-
<groupId>org.mockito</groupId>
62-
<artifactId>mockito-all</artifactId>
63-
<version>1.8.4</version>
69+
<dependency>
70+
<groupId>org.hamcrest</groupId>
71+
<artifactId>hamcrest-core</artifactId>
72+
<version>1.3</version>
73+
<scope>test</scope>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.hamcrest</groupId>
77+
<artifactId>hamcrest-library</artifactId>
78+
<version>1.3</version>
6479
<scope>test</scope>
6580
</dependency>
81+
<dependency>
82+
<groupId>com.jcabi</groupId>
83+
<artifactId>jcabi-matchers</artifactId>
84+
<version>1.3</version>
85+
</dependency>
6686
</dependencies>
6787
<properties>
6888
<project.http.version>1.20.0</project.http.version>

cloud-storage/xml-api/cmdline-sample/src/main/java/StorageSample.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import javax.xml.transform.TransformerFactory;
3737
import javax.xml.transform.stream.StreamResult;
3838
import javax.xml.transform.stream.StreamSource;
39+
import java.security.GeneralSecurityException;
3940

4041
/**
4142
* Sample code used in the Cloud Storage Java documentation.
@@ -54,14 +55,13 @@ private StorageSample() { }
5455
* Fetches the listing of the given bucket.
5556
*
5657
* @param bucketName the name of the bucket to list.
57-
* @param httpTransport the httpTransport to use (mainly for testing).
5858
*
5959
* @return the raw XML containing the listing of the bucket.
6060
* @throws IOException if there's an error communicating with Cloud Storage.
61+
* @throws GeneralSecurityException for errors creating https connection.
6162
*/
62-
public static String listBucket(
63-
final String bucketName, final HttpTransport httpTransport)
64-
throws IOException {
63+
public static String listBucket(final String bucketName)
64+
throws IOException, GeneralSecurityException {
6565
//[START snippet]
6666
// Build an account credential.
6767
GoogleCredential credential = GoogleCredential.getApplicationDefault()
@@ -70,9 +70,12 @@ public static String listBucket(
7070
// Set up and execute a Google Cloud Storage request.
7171
String uri = "https://storage.googleapis.com/"
7272
+ URLEncoder.encode(bucketName, "UTF-8");
73+
74+
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
7375
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(
7476
credential);
7577
GenericUrl url = new GenericUrl(uri);
78+
7679
HttpRequest request = requestFactory.buildGetRequest(url);
7780
HttpResponse response = request.execute();
7881
String content = response.parseAsString();
@@ -124,10 +127,8 @@ public static void main(final String[] args) {
124127
args.length == 1,
125128
"Please pass in the Google Cloud Storage bucket name to display");
126129
String bucketName = args[0];
127-
HttpTransport httpTransport =
128-
GoogleNetHttpTransport.newTrustedTransport();
129130

130-
String content = listBucket(bucketName, httpTransport);
131+
String content = listBucket(bucketName);
131132

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

cloud-storage/xml-api/cmdline-sample/src/test/java/StorageSampleTest.java

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,21 @@
1616

1717
// [START StorageSampleTest]
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.mockito.Mockito.doReturn;
21-
import static org.mockito.Mockito.spy;
19+
import static com.jcabi.matchers.RegexMatchers.*;
20+
import static org.junit.Assert.assertThat;
2221

23-
import com.google.api.client.testing.http.MockHttpTransport;
24-
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
25-
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
26-
27-
import org.junit.After;
28-
import org.junit.Before;
2922
import org.junit.Test;
30-
import org.mockito.MockitoAnnotations;
3123

32-
import java.io.IOException;
24+
import java.util.regex.Pattern;
3325

3426
public class StorageSampleTest {
35-
private MockHttpTransport transport;
36-
private MockLowLevelHttpRequest request;
37-
38-
private void stubRequest(String url, int statusCode, String responseContent)
39-
throws IOException {
40-
request.setResponse(new MockLowLevelHttpResponse()
41-
.setStatusCode(statusCode)
42-
.setContent(responseContent));
43-
doReturn(request).when(transport).buildRequest("GET", url);
44-
}
45-
46-
@Before
47-
public void setUp() throws Exception {
48-
MockitoAnnotations.initMocks(this);
49-
transport = spy(new MockHttpTransport());
50-
request = spy(new MockLowLevelHttpRequest());
51-
}
52-
53-
@After
54-
public void tearDown() {
55-
}
56-
57-
5827
@Test
5928
public void testListBucket() throws Exception {
60-
stubRequest("https://storage.googleapis.com/test-bucket", 200, "listing");
61-
String listing = StorageSample.listBucket("test-bucket", transport);
62-
assertEquals("listing", listing);
29+
String listing = StorageSample.listBucket("cloud-samples-tests");
30+
assertThat(listing, matchesPattern(
31+
".*<ListBucketResult.*"
32+
+ "<Name>cloud-samples-tests</Name>.*"
33+
+ "</ListBucketResult>.*"));
6334
}
6435
}
6536

service-account.json.enc

2 KB
Binary file not shown.

0 commit comments

Comments
 (0)