From f48dea1cea6448b4bf128ede662a2b39f3d93671 Mon Sep 17 00:00:00 2001 From: Sam Stern Date: Thu, 11 Jan 2018 15:40:35 -0800 Subject: [PATCH] Add Firestore snippet for getCollections() (#988) * Add snippet for Collections() * Fix test failures --- .../snippets/RetrieveDataSnippets.java | 17 ++++++++++++++ .../snippets/RetrieveDataSnippetsIT.java | 22 +++++++++++++++++++ 2 files changed, 39 insertions(+) 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 bb83df22469..6a996153968 100644 --- a/firestore/src/main/java/com/example/firestore/snippets/RetrieveDataSnippets.java +++ b/firestore/src/main/java/com/example/firestore/snippets/RetrieveDataSnippets.java @@ -137,4 +137,21 @@ public List getAllDocuments() throws Exception { // [END fs_get_all_docs] return documents; } + + /** + * Return all subcollections of the cities/SF document. + * + * @return iterable of collection references. + */ + public Iterable getCollections() throws Exception { + // [START fs_get_collections] + Iterable collections = + db.collection("cities").document("SF").getCollections(); + + for (CollectionReference collRef : collections) { + System.out.println("Found subcollection with id: " + collRef.getId()); + } + // [END fs_get_collections] + return collections; + } } 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 f730329caae..0c06c8853c8 100644 --- a/firestore/src/test/java/com/example/firestore/snippets/RetrieveDataSnippetsIT.java +++ b/firestore/src/test/java/com/example/firestore/snippets/RetrieveDataSnippetsIT.java @@ -22,10 +22,13 @@ 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.QuerySnapshot; +import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -99,6 +102,25 @@ public void testRetrieveAllDocuments() throws Exception { && docIds.contains("BJ")); } + @Test + public void testGetSubcollections() throws Exception { + // Add a landmark subcollection + Map data = new HashMap<>(); + data.put("foo", "bar"); + db.document("cities/SF/landmarks/example").set(data).get(); + + Iterable collections = + retrieveDataSnippets.getCollections(); + + List collectionList = new ArrayList<>(); + for (CollectionReference collRef : collections) { + collectionList.add(collRef); + } + + assertEquals(collectionList.size(), 1); + assertEquals(collectionList.get(0).getId(), "landmarks"); + } + private static void deleteAllDocuments() throws Exception { ApiFuture future = db.collection("cities").get(); QuerySnapshot querySnapshot = future.get();