Skip to content

Commit

Permalink
Checkstyle fixes in Cloud Storage JSON API tests.
Browse files Browse the repository at this point in the history
Getting ready to use shared java-repo-tools configuration.

I also convert the assertions to use
[truth](http://google.github.io/truth/).

Since I find it more readable to get a list of object names first
instead of manually looping through, I also update the sample to use
Java 8 lambdas to extract these object names. The sample is used here:
https://cloud.google.com/storage/docs/json_api/v1/json-api-java-samples
which does not indicate the required Java version.
  • Loading branch information
tswast committed Mar 15, 2016
1 parent 1ee524b commit d1857b0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
12 changes: 12 additions & 0 deletions storage/json-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
<artifactId>storage-json-sample</artifactId>
<version>1</version>
<name>Sample accessing the Google Cloud Storage JSON API using OAuth 2.0.</name>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>

<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -45,5 +51,11 @@
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>0.28</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
38 changes: 13 additions & 25 deletions storage/json-api/src/test/java/StorageSampleTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -16,16 +16,16 @@

// [START all]

import static org.junit.Assert.*;
import static com.google.common.truth.Truth.assertThat;

import com.google.api.services.storage.model.Bucket;
import com.google.api.services.storage.model.StorageObject;

import org.junit.Test;

import java.util.List;
import java.util.regex.Pattern;
import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.stream.Collectors;

public class StorageSampleTest {
private static final String BUCKET = "cloud-samples-tests";
Expand All @@ -34,49 +34,37 @@ public class StorageSampleTest {
@Test
public void testListBucket() throws Exception {
List<StorageObject> listing = StorageSample.listBucket(BUCKET);
assertTrue(listing.size() > 0);
assertThat(listing).isNotEmpty();
}

@Test
public void testGetBucket() throws Exception {
Bucket bucket = StorageSample.getBucket(BUCKET);
assertEquals(bucket.getName(), BUCKET);
assertEquals(bucket.getLocation(), "US-CENTRAL1");
assertThat(bucket.getName()).named("bucket name").isEqualTo(BUCKET);
assertThat(bucket.getLocation()).named("bucket location").isEqualTo("US-CENTRAL1");
}

@Test
public void testUploadDelete() throws Exception {
StorageSample.uploadStream(
TEST_OBJECT, "text/plain",
new ByteArrayInputStream(("This object is uploaded and deleted as part of the "
new ByteArrayInputStream(
("This object is uploaded and deleted as part of the "
+ "StorageSampleTest integration test.").getBytes()),
BUCKET);

try {
// Verify that the object was created
List<StorageObject> listing = StorageSample.listBucket(BUCKET);
boolean found = false;
for (StorageObject so : listing) {
if (TEST_OBJECT.equals(so.getName())) {
found = true;
break;
}
}
assertTrue("Should have uploaded successfully", found);

List<String> names = listing.stream().map(so -> so.getName()).collect(Collectors.toList());
assertThat(names).named("objects found after upload").contains(TEST_OBJECT);
} finally {
StorageSample.deleteObject(TEST_OBJECT, BUCKET);

// Verify that the object no longer exists
List<StorageObject> listing = StorageSample.listBucket(BUCKET);
boolean found = false;
for (StorageObject so : listing) {
if (TEST_OBJECT.equals(so.getName())) {
found = true;
break;
}
}
assertFalse("Object (" + TEST_OBJECT + ") should have been deleted", found);
List<String> names = listing.stream().map(so -> so.getName()).collect(Collectors.toList());
assertThat(names).named("objects found after delete").doesNotContain(TEST_OBJECT);
}
}
}
Expand Down

0 comments on commit d1857b0

Please sign in to comment.