From d1857b0cd4356bdfbec26d34fae8da591aba278a Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Tue, 15 Mar 2016 12:50:16 -0700 Subject: [PATCH] Checkstyle fixes in Cloud Storage JSON API tests. 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. --- storage/json-api/pom.xml | 12 ++++++ .../src/test/java/StorageSampleTest.java | 38 +++++++------------ 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/storage/json-api/pom.xml b/storage/json-api/pom.xml index 7feeca06b11..a23ae40f1f0 100644 --- a/storage/json-api/pom.xml +++ b/storage/json-api/pom.xml @@ -12,6 +12,12 @@ storage-json-sample 1 Sample accessing the Google Cloud Storage JSON API using OAuth 2.0. + + + 1.8 + 1.8 + + @@ -45,5 +51,11 @@ 4.10 test + + com.google.truth + truth + 0.28 + test + diff --git a/storage/json-api/src/test/java/StorageSampleTest.java b/storage/json-api/src/test/java/StorageSampleTest.java index 54a882d1d81..32bc786bbae 100644 --- a/storage/json-api/src/test/java/StorageSampleTest.java +++ b/storage/json-api/src/test/java/StorageSampleTest.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2015 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -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"; @@ -34,49 +34,37 @@ public class StorageSampleTest { @Test public void testListBucket() throws Exception { List 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 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 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 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 names = listing.stream().map(so -> so.getName()).collect(Collectors.toList()); + assertThat(names).named("objects found after delete").doesNotContain(TEST_OBJECT); } } }