From 793f6d7e06a44e3498ac845638d3990c4b97fded Mon Sep 17 00:00:00 2001 From: Sam Stern Date: Wed, 17 Jan 2018 13:02:37 -0800 Subject: [PATCH 1/5] Snippets and tests for Firestore listens --- firestore/pom.xml | 2 +- .../com/example/firestore/Quickstart.java | 16 +- .../snippets/ListenDataSnippets.java | 197 ++++++++++++++++++ .../snippets/ManageDataSnippets.java | 5 +- .../snippets/RetrieveDataSnippets.java | 11 +- .../firestore/BaseIntegrationTest.java | 57 +++++ .../com/example/firestore/QuickstartIT.java | 7 +- .../snippets/ListenDataSnippetsIT.java | 64 ++++++ .../snippets/ManageDataSnippetsIT.java | 36 +--- .../snippets/QueryDataSnippetsIT.java | 26 +-- .../snippets/RetrieveDataSnippetsIT.java | 19 +- 11 files changed, 351 insertions(+), 89 deletions(-) create mode 100644 firestore/src/main/java/com/example/firestore/snippets/ListenDataSnippets.java create mode 100644 firestore/src/test/java/com/example/firestore/BaseIntegrationTest.java create mode 100644 firestore/src/test/java/com/example/firestore/snippets/ListenDataSnippetsIT.java diff --git a/firestore/pom.xml b/firestore/pom.xml index e5a0c0576ea..98227981126 100644 --- a/firestore/pom.xml +++ b/firestore/pom.xml @@ -45,7 +45,7 @@ com.google.cloud google-cloud-firestore - 0.32.0-beta + 0.32.1-beta-SNAPSHOT diff --git a/firestore/src/main/java/com/example/firestore/Quickstart.java b/firestore/src/main/java/com/example/firestore/Quickstart.java index 06316a99155..7df3f285e68 100644 --- a/firestore/src/main/java/com/example/firestore/Quickstart.java +++ b/firestore/src/main/java/com/example/firestore/Quickstart.java @@ -18,19 +18,19 @@ import com.google.api.core.ApiFuture; import com.google.cloud.firestore.DocumentReference; -import com.google.cloud.firestore.DocumentSnapshot; -// [START fs_include_dependencies] import com.google.cloud.firestore.Firestore; import com.google.cloud.firestore.FirestoreOptions; -// [END fs_include_dependencies] +import com.google.cloud.firestore.QueryDocumentSnapshot; import com.google.cloud.firestore.QuerySnapshot; import com.google.cloud.firestore.WriteResult; import com.google.common.collect.ImmutableMap; - import java.util.HashMap; import java.util.List; import java.util.Map; +// [START fs_include_dependencies] +// [END fs_include_dependencies] + /** * A simple Quick start application demonstrating how to connect to Firestore * and add and query documents. @@ -126,8 +126,8 @@ void runAQuery() throws Exception { // ... // query.get() blocks on response QuerySnapshot querySnapshot = query.get(); - List documents = querySnapshot.getDocuments(); - for (DocumentSnapshot document : documents) { + List documents = querySnapshot.getDocuments(); + for (QueryDocumentSnapshot document : documents) { System.out.println("User: " + document.getId()); System.out.println("First: " + document.getString("first")); if (document.contains("middle")) { @@ -146,8 +146,8 @@ void retrieveAllDocuments() throws Exception { // ... // query.get() blocks on response QuerySnapshot querySnapshot = query.get(); - List documents = querySnapshot.getDocuments(); - for (DocumentSnapshot document : documents) { + List documents = querySnapshot.getDocuments(); + for (QueryDocumentSnapshot document : documents) { System.out.println("User: " + document.getId()); System.out.println("First: " + document.getString("first")); if (document.contains("middle")) { diff --git a/firestore/src/main/java/com/example/firestore/snippets/ListenDataSnippets.java b/firestore/src/main/java/com/example/firestore/snippets/ListenDataSnippets.java new file mode 100644 index 00000000000..056cf9c5695 --- /dev/null +++ b/firestore/src/main/java/com/example/firestore/snippets/ListenDataSnippets.java @@ -0,0 +1,197 @@ +package com.example.firestore.snippets; + +import com.google.api.core.SettableApiFuture; +import com.google.cloud.firestore.DocumentChange; +import com.google.cloud.firestore.DocumentChange.Type; +import com.google.cloud.firestore.DocumentReference; +import com.google.cloud.firestore.DocumentSnapshot; +import com.google.cloud.firestore.EventListener; +import com.google.cloud.firestore.Firestore; +import com.google.cloud.firestore.FirestoreException; +import com.google.cloud.firestore.ListenerRegistration; +import com.google.cloud.firestore.Query; +import com.google.cloud.firestore.QuerySnapshot; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import javax.annotation.Nullable; + +/** + * Snippets to demonstrate Firestore 'listen' operations. + */ +@SuppressWarnings("Convert2Lambda") +public class ListenDataSnippets { + + private static final long TIMEOUT_SECONDS = 5; + + private final Firestore db; + + ListenDataSnippets(Firestore db) { + this.db = db; + } + + /** + * Listen to a single document, returning data after the first snapshot. + */ + Map listenToDocument() throws Exception { + final SettableApiFuture> future = SettableApiFuture.create(); + + // [START listen_to_document] + DocumentReference docRef = db.collection("cities").document("SF"); + docRef.addSnapshotListener(new EventListener() { + @Override + public void onEvent(@Nullable DocumentSnapshot snapshot, + @Nullable FirestoreException e) { + if (e != null) { + System.err.println("Listen failed: " + e); + return; + } + + if (snapshot != null && snapshot.exists()) { + System.out.println("Current data: " + snapshot.getData()); + } else { + System.out.print("Current data: null"); + } + // [START_EXCLUDE silent] + if (!future.isDone()) { + future.set(snapshot.getData()); + } + // [END_EXCLUDE] + } + }); + // [END listen_to_document] + + return future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); + } + + /** + * Listen to a query, returning the names of all cities in the first snapshot. + */ + List listenForMultiple() throws Exception { + final SettableApiFuture> future = SettableApiFuture.create(); + + // [START listen_to_multiple] + db.collection("cities") + .whereEqualTo("state", "CA") + .addSnapshotListener(new EventListener() { + @Override + public void onEvent(@Nullable QuerySnapshot snapshots, + @Nullable FirestoreException e) { + if (e != null) { + System.err.println("Listen failed:" + e); + return; + } + + List cities = new ArrayList<>(); + for (DocumentSnapshot doc : snapshots) { + if (doc.get("name") != null) { + cities.add(doc.getString("name")); + } + } + System.out.println("Current cites in CA: " + cities); + // [START_EXCLUDE silent] + if (!future.isDone()) { + future.set(cities); + } + // [END_EXCLUDE] + } + }); + // [END listen_to_multiple] + + return future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); + } + + /** + * Listen to a query, returning the list of DocumentChange events in the first snapshot. + */ + List listenForChanges() throws Exception { + SettableApiFuture> future = SettableApiFuture.create(); + + // [START listen_for_changes] + db.collection("cities") + .whereEqualTo("state", "CA") + .addSnapshotListener(new EventListener() { + @Override + public void onEvent(@Nullable QuerySnapshot snapshots, + @Nullable FirestoreException e) { + if (e != null) { + System.err.println("Listen failed: " + e); + return; + } + + for (DocumentChange dc : snapshots.getDocumentChanges()) { + switch (dc.getType()) { + case ADDED: + System.out.println("New city: " + dc.getDocument().getData()); + break; + case MODIFIED: + System.out.println("Modified city: " + dc.getDocument().getData()); + break; + case REMOVED: + System.out.println("Removed city: " + dc.getDocument().getData()); + break; + } + } + // [START_EXCLUDE silent] + if (!future.isDone()) { + future.set(snapshots.getDocumentChanges()); + } + // [END_EXCLUDE] + } + }); + // [END listen_for_changes] + + return future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); + } + + /** + * Demonstrate how to detach an event listener. + */ + void detachListener() { + // [START detach_errors] + Query query = db.collection("cities"); + ListenerRegistration registration = query.addSnapshotListener( + new EventListener() { + // [START_EXCLUDE] + @Override + public void onEvent(@Nullable QuerySnapshot snapshots, + @Nullable FirestoreException e) { + + } + // [END_EXCLUDE] + }); + + // ... + + // Stop listening to changes + registration.remove(); + // [END detach_errors] + } + + /** + * Demonstrate how to handle listening errors. + */ + void listenErrors() { + // [START listen_errors] + db.collection("cities") + .addSnapshotListener(new EventListener() { + @Override + public void onEvent(@Nullable QuerySnapshot snapshots, + @Nullable FirestoreException e) { + if (e != null) { + System.err.println("Listen failed: " + e); + return; + } + + for (DocumentChange dc : snapshots.getDocumentChanges()) { + if (dc.getType() == Type.ADDED) { + System.out.println("New city: " + dc.getDocument().getData()); + } + } + } + }); + // [END listen_errors] + } + +} diff --git a/firestore/src/main/java/com/example/firestore/snippets/ManageDataSnippets.java b/firestore/src/main/java/com/example/firestore/snippets/ManageDataSnippets.java index 45270a7340d..8e397c06a28 100644 --- a/firestore/src/main/java/com/example/firestore/snippets/ManageDataSnippets.java +++ b/firestore/src/main/java/com/example/firestore/snippets/ManageDataSnippets.java @@ -23,6 +23,7 @@ import com.google.cloud.firestore.DocumentSnapshot; import com.google.cloud.firestore.FieldValue; import com.google.cloud.firestore.Firestore; +import com.google.cloud.firestore.QueryDocumentSnapshot; import com.google.cloud.firestore.QuerySnapshot; import com.google.cloud.firestore.SetOptions; import com.google.cloud.firestore.Transaction; @@ -294,8 +295,8 @@ void deleteCollection(CollectionReference collection, int batchSize) { ApiFuture future = collection.limit(batchSize).get(); int deleted = 0; // future.get() blocks on document retrieval - List documents = future.get().getDocuments(); - for (DocumentSnapshot document : documents) { + List documents = future.get().getDocuments(); + for (QueryDocumentSnapshot document : documents) { document.getReference().delete(); ++deleted; } diff --git a/firestore/src/main/java/com/example/firestore/snippets/RetrieveDataSnippets.java b/firestore/src/main/java/com/example/firestore/snippets/RetrieveDataSnippets.java index 6a996153968..5d1de7f1fde 100644 --- a/firestore/src/main/java/com/example/firestore/snippets/RetrieveDataSnippets.java +++ b/firestore/src/main/java/com/example/firestore/snippets/RetrieveDataSnippets.java @@ -24,6 +24,7 @@ import com.google.cloud.firestore.DocumentReference; import com.google.cloud.firestore.DocumentSnapshot; import com.google.cloud.firestore.Firestore; +import com.google.cloud.firestore.QueryDocumentSnapshot; import com.google.cloud.firestore.QuerySnapshot; import com.google.cloud.firestore.WriteResult; @@ -106,13 +107,13 @@ public City getDocumentAsEntity() throws Exception { * * @return list of documents of capital cities. */ - public List getQueryResults() throws Exception { + public List getQueryResults() throws Exception { // [START fs_get_multiple_docs] //asynchronously retrieve multiple documents ApiFuture future = db.collection("cities").whereEqualTo("capital", true).get(); // future.get() blocks on response - List documents = future.get().getDocuments(); + List documents = future.get().getDocuments(); for (DocumentSnapshot document : documents) { System.out.println(document.getId() + " => " + document.toObject(City.class)); } @@ -125,13 +126,13 @@ public List getQueryResults() throws Exception { * * @return list of documents */ - public List getAllDocuments() throws Exception { + public List getAllDocuments() throws Exception { // [START fs_get_all_docs] //asynchronously retrieve all documents ApiFuture future = db.collection("cities").get(); // future.get() blocks on response - List documents = future.get().getDocuments(); - for (DocumentSnapshot document : documents) { + List documents = future.get().getDocuments(); + for (QueryDocumentSnapshot document : documents) { System.out.println(document.getId() + " => " + document.toObject(City.class)); } // [END fs_get_all_docs] diff --git a/firestore/src/test/java/com/example/firestore/BaseIntegrationTest.java b/firestore/src/test/java/com/example/firestore/BaseIntegrationTest.java new file mode 100644 index 00000000000..2165e7e7e71 --- /dev/null +++ b/firestore/src/test/java/com/example/firestore/BaseIntegrationTest.java @@ -0,0 +1,57 @@ +package com.example.firestore; + +import com.example.firestore.snippets.ManageDataSnippetsIT; +import com.example.firestore.snippets.model.City; +import com.google.api.core.ApiFuture; +import com.google.cloud.firestore.DocumentReference; +import com.google.cloud.firestore.DocumentSnapshot; +import com.google.cloud.firestore.Firestore; +import com.google.cloud.firestore.FirestoreOptions; +import com.google.cloud.firestore.QuerySnapshot; +import java.util.Map; +import org.junit.BeforeClass; + +/** + * Base class for tests like {@link ManageDataSnippetsIT}. + */ +public class BaseIntegrationTest { + + protected static String projectId = "java-docs-samples-firestore"; + protected static Firestore db; + + @BeforeClass + public static void baseSetup() throws Exception { + FirestoreOptions firestoreOptions = FirestoreOptions.getDefaultInstance().toBuilder() + .setProjectId(projectId) + .build(); + db = firestoreOptions.getService(); + deleteAllDocuments(db); + } + + protected DocumentSnapshot getDocumentData(DocumentReference docRef) throws Exception { + return docRef.get().get(); + } + + protected Map getDocumentDataAsMap(DocumentReference docRef) throws Exception { + DocumentSnapshot snapshot = docRef.get().get(); + if (!snapshot.exists()) { + throw new RuntimeException("Document does not exist: " + docRef.getPath()); + } + + return snapshot.getData(); + } + + protected City getDocumentDataAsCity(DocumentReference docRef) throws Exception { + return docRef.get().get().toObject(City.class); + } + + protected static void deleteAllDocuments(Firestore db) throws Exception { + ApiFuture future = db.collection("cities").get(); + QuerySnapshot querySnapshot = future.get(); + for (DocumentSnapshot doc : querySnapshot.getDocuments()) { + // block on delete operation + db.collection("cities").document(doc.getId()).delete().get(); + } + } + +} diff --git a/firestore/src/test/java/com/example/firestore/QuickstartIT.java b/firestore/src/test/java/com/example/firestore/QuickstartIT.java index 2c8b00b1eb1..bbfdc1720d4 100644 --- a/firestore/src/test/java/com/example/firestore/QuickstartIT.java +++ b/firestore/src/test/java/com/example/firestore/QuickstartIT.java @@ -19,10 +19,8 @@ import static org.junit.Assert.assertTrue; import com.google.api.core.ApiFuture; - import com.google.cloud.firestore.DocumentReference; import com.google.cloud.firestore.DocumentSnapshot; -import com.google.cloud.firestore.Firestore; import com.google.cloud.firestore.QuerySnapshot; import java.io.ByteArrayOutputStream; import java.io.PrintStream; @@ -37,18 +35,15 @@ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") -public class QuickstartIT { +public class QuickstartIT extends BaseIntegrationTest { private Quickstart quickstart; - private Firestore db; private ByteArrayOutputStream bout; private PrintStream out; - private String projectId = "java-docs-samples-firestore"; @Before public void setUp() throws Exception { quickstart = new Quickstart(projectId); - db = quickstart.getDb(); bout = new ByteArrayOutputStream(); out = new PrintStream(bout); System.setOut(out); diff --git a/firestore/src/test/java/com/example/firestore/snippets/ListenDataSnippetsIT.java b/firestore/src/test/java/com/example/firestore/snippets/ListenDataSnippetsIT.java new file mode 100644 index 00000000000..945b6eadb37 --- /dev/null +++ b/firestore/src/test/java/com/example/firestore/snippets/ListenDataSnippetsIT.java @@ -0,0 +1,64 @@ +package com.example.firestore.snippets; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import com.example.firestore.BaseIntegrationTest; +import com.google.cloud.firestore.DocumentChange; +import com.google.cloud.firestore.DocumentChange.Type; +import com.google.cloud.firestore.DocumentReference; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class ListenDataSnippetsIT extends BaseIntegrationTest { + + private static QueryDataSnippets queryDataSnippets; + private static ListenDataSnippets listenDataSnippets; + + // TODO: tear down + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + queryDataSnippets = new QueryDataSnippets(db); + queryDataSnippets.prepareExamples(); + + listenDataSnippets = new ListenDataSnippets(db); + } + + @Test + public void testListenDocument() throws Exception { + Map expectedData = listenDataSnippets.listenToDocument(); + DocumentReference docRef = db.collection("cities").document("SF"); + assertTrue(Objects.equals(expectedData, getDocumentDataAsMap(docRef))); + } + + @Test + public void testListenForMultiple() throws Exception { + List cities = listenDataSnippets.listenForMultiple(); + + assertEquals("Two cities in CA", cities.size(), 2); + assertTrue("SF in CA", cities.contains("San Francisco")); + assertTrue("LA in CA", cities.contains("Los Angeles")); + } + + @Test + public void testListenForChanges() throws Exception { + List changes = listenDataSnippets.listenForChanges(); + + assertEquals("Two changes in snapshot.", changes.size(), 2); + assertEquals("First change is ADDED", changes.get(0).getType(), Type.ADDED); + assertEquals("Second change is ADDED", changes.get(1).getType(), Type.ADDED); + } + + @AfterClass + public static void tearDown() throws Exception { + deleteAllDocuments(db); + } +} diff --git a/firestore/src/test/java/com/example/firestore/snippets/ManageDataSnippetsIT.java b/firestore/src/test/java/com/example/firestore/snippets/ManageDataSnippetsIT.java index de87a1233c2..293b0bd9542 100644 --- a/firestore/src/test/java/com/example/firestore/snippets/ManageDataSnippetsIT.java +++ b/firestore/src/test/java/com/example/firestore/snippets/ManageDataSnippetsIT.java @@ -20,14 +20,12 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import com.example.firestore.BaseIntegrationTest; import com.example.firestore.snippets.model.City; import com.google.api.core.ApiFuture; import com.google.cloud.firestore.CollectionReference; import com.google.cloud.firestore.DocumentReference; import com.google.cloud.firestore.DocumentSnapshot; -import com.google.cloud.firestore.Firestore; -import com.google.cloud.firestore.FirestoreOptions; -import com.google.cloud.firestore.QuerySnapshot; import java.util.Date; import java.util.Map; import java.util.Objects; @@ -39,19 +37,12 @@ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") -public class ManageDataSnippetsIT { +public class ManageDataSnippetsIT extends BaseIntegrationTest { - private static Firestore db; private static ManageDataSnippets manageDataSnippets; - private static String projectId = "java-docs-samples-firestore"; @BeforeClass - public static void setUpBeforeClass() throws Exception { - FirestoreOptions firestoreOptions = FirestoreOptions.getDefaultInstance().toBuilder() - .setProjectId(projectId) - .build(); - db = firestoreOptions.getService(); - deleteAllDocuments(); + public static void setUpBeforeClass() throws Exception { ; manageDataSnippets = new ManageDataSnippets(db); } @@ -187,27 +178,6 @@ public void testWriteBatchIsSuccessful() throws Exception { assertFalse(document.get().exists()); } - private DocumentSnapshot getDocumentData(DocumentReference docRef) throws Exception { - return docRef.get().get(); - } - - private Map getDocumentDataAsMap(DocumentReference docRef) throws Exception { - return docRef.get().get().getData(); - } - - private City getDocumentDataAsCity(DocumentReference docRef) throws Exception { - return docRef.get().get().toObject(City.class); - } - - private static void deleteAllDocuments() throws Exception { - ApiFuture future = db.collection("cities").get(); - QuerySnapshot querySnapshot = future.get(); - for (DocumentSnapshot doc : querySnapshot.getDocuments()) { - // block on delete operation - db.collection("cities").document(doc.getId()).delete().get(); - } - } - @AfterClass public static void tearDown() throws Exception { manageDataSnippets.deleteCollection(db.collection("cities"), 10); diff --git a/firestore/src/test/java/com/example/firestore/snippets/QueryDataSnippetsIT.java b/firestore/src/test/java/com/example/firestore/snippets/QueryDataSnippetsIT.java index dc709c54f0a..7c18566afea 100644 --- a/firestore/src/test/java/com/example/firestore/snippets/QueryDataSnippetsIT.java +++ b/firestore/src/test/java/com/example/firestore/snippets/QueryDataSnippetsIT.java @@ -19,11 +19,11 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import com.example.firestore.BaseIntegrationTest; import com.google.api.core.ApiFuture; import com.google.cloud.firestore.DocumentSnapshot; -import com.google.cloud.firestore.Firestore; -import com.google.cloud.firestore.FirestoreOptions; import com.google.cloud.firestore.Query; +import com.google.cloud.firestore.QueryDocumentSnapshot; import com.google.cloud.firestore.QuerySnapshot; import com.google.common.collect.ImmutableMap; import java.util.ArrayList; @@ -41,19 +41,12 @@ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") -public class QueryDataSnippetsIT { +public class QueryDataSnippetsIT extends BaseIntegrationTest { - private static Firestore db; private static QueryDataSnippets queryDataSnippets; - private static String projectId = "java-docs-samples-firestore"; @BeforeClass public static void setUpBeforeClass() throws Exception { - FirestoreOptions firestoreOptions = FirestoreOptions.getDefaultInstance().toBuilder() - .setProjectId(projectId) - .build(); - db = firestoreOptions.getService(); - deleteAllDocuments(); queryDataSnippets = new QueryDataSnippets(db); queryDataSnippets.prepareExamples(); } @@ -178,7 +171,7 @@ public void testMultipleCursorConditions() throws Exception { // all documents are retrieved QuerySnapshot querySnapshot = query1.get().get(); - List docs = querySnapshot.getDocuments(); + List docs = querySnapshot.getDocuments(); assertEquals(3, docs.size()); @@ -211,17 +204,8 @@ private List getResults(Query query) throws Exception { return docIds; } - private static void deleteAllDocuments() throws Exception { - ApiFuture future = db.collection("cities").get(); - QuerySnapshot querySnapshot = future.get(); - for (DocumentSnapshot doc : querySnapshot.getDocuments()) { - // block on delete operation - db.collection("cities").document(doc.getId()).delete().get(); - } - } - @AfterClass public static void tearDown() throws Exception { - deleteAllDocuments(); + deleteAllDocuments(db); } } diff --git a/firestore/src/test/java/com/example/firestore/snippets/RetrieveDataSnippetsIT.java b/firestore/src/test/java/com/example/firestore/snippets/RetrieveDataSnippetsIT.java index 0c06c8853c8..76fac8f6d44 100644 --- a/firestore/src/test/java/com/example/firestore/snippets/RetrieveDataSnippetsIT.java +++ b/firestore/src/test/java/com/example/firestore/snippets/RetrieveDataSnippetsIT.java @@ -19,13 +19,12 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import com.example.firestore.BaseIntegrationTest; import com.example.firestore.snippets.model.City; - import com.google.api.core.ApiFuture; import com.google.cloud.firestore.CollectionReference; import com.google.cloud.firestore.DocumentSnapshot; -import com.google.cloud.firestore.Firestore; -import com.google.cloud.firestore.FirestoreOptions; +import com.google.cloud.firestore.QueryDocumentSnapshot; import com.google.cloud.firestore.QuerySnapshot; import java.util.ArrayList; import java.util.HashMap; @@ -41,18 +40,12 @@ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") -public class RetrieveDataSnippetsIT { - private static Firestore db; +public class RetrieveDataSnippetsIT extends BaseIntegrationTest { + private static RetrieveDataSnippets retrieveDataSnippets; - private static String projectId = "java-docs-samples-firestore"; @BeforeClass public static void setUpBeforeClass() throws Exception { - FirestoreOptions firestoreOptions = FirestoreOptions.getDefaultInstance().toBuilder() - .setProjectId(projectId) - .build(); - db = firestoreOptions.getService(); - deleteAllDocuments(); retrieveDataSnippets = new RetrieveDataSnippets(db); retrieveDataSnippets.prepareExamples(); } @@ -77,7 +70,7 @@ public void testRetrieveAsEntity() throws Exception { @Test public void testRetrieveQueryResults() throws Exception { - List docs = retrieveDataSnippets.getQueryResults(); + List docs = retrieveDataSnippets.getQueryResults(); assertEquals(docs.size(), 3); Set docIds = new HashSet<>(); for (DocumentSnapshot doc : docs) { @@ -88,7 +81,7 @@ public void testRetrieveQueryResults() throws Exception { @Test public void testRetrieveAllDocuments() throws Exception { - List docs = retrieveDataSnippets.getAllDocuments(); + List docs = retrieveDataSnippets.getAllDocuments(); assertEquals(docs.size(), 5); Set docIds = new HashSet<>(); for (DocumentSnapshot doc : docs) { From fda5dd87f9db70c8b9137af419e5b05f46b86515 Mon Sep 17 00:00:00 2001 From: Sam Stern Date: Wed, 17 Jan 2018 17:02:10 -0800 Subject: [PATCH 2/5] Fix comment block Change-Id: Iab845387ba4188334abd75e7ae0d84bcc3f2f6f9 --- .../src/main/java/com/example/firestore/Quickstart.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/firestore/src/main/java/com/example/firestore/Quickstart.java b/firestore/src/main/java/com/example/firestore/Quickstart.java index 7df3f285e68..dd7091b0b68 100644 --- a/firestore/src/main/java/com/example/firestore/Quickstart.java +++ b/firestore/src/main/java/com/example/firestore/Quickstart.java @@ -18,8 +18,10 @@ import com.google.api.core.ApiFuture; import com.google.cloud.firestore.DocumentReference; +// [START fs_include_dependencies] import com.google.cloud.firestore.Firestore; import com.google.cloud.firestore.FirestoreOptions; +// [END fs_include_dependencies] import com.google.cloud.firestore.QueryDocumentSnapshot; import com.google.cloud.firestore.QuerySnapshot; import com.google.cloud.firestore.WriteResult; @@ -28,9 +30,6 @@ import java.util.List; import java.util.Map; -// [START fs_include_dependencies] -// [END fs_include_dependencies] - /** * A simple Quick start application demonstrating how to connect to Firestore * and add and query documents. From 70d62a61d1830575b111b6344852bd8bbbfe34ba Mon Sep 17 00:00:00 2001 From: Sam Stern Date: Thu, 18 Jan 2018 12:58:32 -0800 Subject: [PATCH 3/5] Update to 0.33 SDK Change-Id: I9880508d0ac60cc886df940c43b4d720c129ce2f --- firestore/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firestore/pom.xml b/firestore/pom.xml index 98227981126..8eeb73aba0a 100644 --- a/firestore/pom.xml +++ b/firestore/pom.xml @@ -45,7 +45,7 @@ com.google.cloud google-cloud-firestore - 0.32.1-beta-SNAPSHOT + 0.33.0-beta From a795dcf26203d687af3ef934608d36cab61bf1a8 Mon Sep 17 00:00:00 2001 From: Sam Stern Date: Thu, 18 Jan 2018 14:41:09 -0800 Subject: [PATCH 4/5] Fix checkstyle issues Change-Id: Id5074a918a89ba8162b839f01ee213b607dc685b --- .../firestore/snippets/ListenDataSnippets.java | 18 ++++++++++++++++++ .../example/firestore/BaseIntegrationTest.java | 16 ++++++++++++++++ .../snippets/ListenDataSnippetsIT.java | 18 ++++++++++++++++-- .../snippets/ManageDataSnippetsIT.java | 2 +- 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/firestore/src/main/java/com/example/firestore/snippets/ListenDataSnippets.java b/firestore/src/main/java/com/example/firestore/snippets/ListenDataSnippets.java index 056cf9c5695..dc4958737f2 100644 --- a/firestore/src/main/java/com/example/firestore/snippets/ListenDataSnippets.java +++ b/firestore/src/main/java/com/example/firestore/snippets/ListenDataSnippets.java @@ -1,3 +1,19 @@ +/* + * Copyright 2018 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.example.firestore.snippets; import com.google.api.core.SettableApiFuture; @@ -131,6 +147,8 @@ public void onEvent(@Nullable QuerySnapshot snapshots, case REMOVED: System.out.println("Removed city: " + dc.getDocument().getData()); break; + default: + break; } } // [START_EXCLUDE silent] diff --git a/firestore/src/test/java/com/example/firestore/BaseIntegrationTest.java b/firestore/src/test/java/com/example/firestore/BaseIntegrationTest.java index 2165e7e7e71..4c88604d2a3 100644 --- a/firestore/src/test/java/com/example/firestore/BaseIntegrationTest.java +++ b/firestore/src/test/java/com/example/firestore/BaseIntegrationTest.java @@ -1,3 +1,19 @@ +/* + * Copyright 2018 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.example.firestore; import com.example.firestore.snippets.ManageDataSnippetsIT; diff --git a/firestore/src/test/java/com/example/firestore/snippets/ListenDataSnippetsIT.java b/firestore/src/test/java/com/example/firestore/snippets/ListenDataSnippetsIT.java index 945b6eadb37..c414f8ddf55 100644 --- a/firestore/src/test/java/com/example/firestore/snippets/ListenDataSnippetsIT.java +++ b/firestore/src/test/java/com/example/firestore/snippets/ListenDataSnippetsIT.java @@ -1,3 +1,19 @@ +/* + * Copyright 2018 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.example.firestore.snippets; import static org.junit.Assert.assertEquals; @@ -22,8 +38,6 @@ public class ListenDataSnippetsIT extends BaseIntegrationTest { private static QueryDataSnippets queryDataSnippets; private static ListenDataSnippets listenDataSnippets; - // TODO: tear down - @BeforeClass public static void setUpBeforeClass() throws Exception { queryDataSnippets = new QueryDataSnippets(db); diff --git a/firestore/src/test/java/com/example/firestore/snippets/ManageDataSnippetsIT.java b/firestore/src/test/java/com/example/firestore/snippets/ManageDataSnippetsIT.java index 293b0bd9542..1ff9c85de27 100644 --- a/firestore/src/test/java/com/example/firestore/snippets/ManageDataSnippetsIT.java +++ b/firestore/src/test/java/com/example/firestore/snippets/ManageDataSnippetsIT.java @@ -42,7 +42,7 @@ public class ManageDataSnippetsIT extends BaseIntegrationTest { private static ManageDataSnippets manageDataSnippets; @BeforeClass - public static void setUpBeforeClass() throws Exception { ; + public static void setUpBeforeClass() throws Exception { manageDataSnippets = new ManageDataSnippets(db); } From f181b0ad2e5dd2c68c234f8f6e2359d96f4f9f9e Mon Sep 17 00:00:00 2001 From: Sam Stern Date: Fri, 19 Jan 2018 10:44:36 -0800 Subject: [PATCH 5/5] Suppress name warning Change-Id: If24e4daa22b2581e93d50106f65cbc16ad583d0c --- .../com/example/firestore/snippets/ListenDataSnippetsIT.java | 1 + 1 file changed, 1 insertion(+) diff --git a/firestore/src/test/java/com/example/firestore/snippets/ListenDataSnippetsIT.java b/firestore/src/test/java/com/example/firestore/snippets/ListenDataSnippetsIT.java index c414f8ddf55..105280d1ed1 100644 --- a/firestore/src/test/java/com/example/firestore/snippets/ListenDataSnippetsIT.java +++ b/firestore/src/test/java/com/example/firestore/snippets/ListenDataSnippetsIT.java @@ -33,6 +33,7 @@ import org.junit.runners.JUnit4; @RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") public class ListenDataSnippetsIT extends BaseIntegrationTest { private static QueryDataSnippets queryDataSnippets;