querys = new ArrayList<>();
+ CollectionReference cities = db.collection("cities");
+
+ // [START fs_simple_queries]
+ Query countryQuery = cities.whereEqualTo("state", "CA");
+ Query populationQuery = cities.whereLessThan("population", 1000000L);
+ Query cityQuery = cities.whereGreaterThanOrEqualTo("name", "San Francisco");
+ // [END fs_simple_queries]
+
+ querys.add(countryQuery);
+ querys.add(populationQuery);
+ querys.add(cityQuery);
+ return querys;
+ }
+
+ /**
+ * Creates chained where clauses.
+ *
+ * Note : equality and inequality clauses over multiple fields cannot be chained.
+ *
+ * @return query
+ */
+ Query createChainedQuery() {
+ CollectionReference cities = db.collection("cities");
+ // [START fs_chained_query]
+ Query chainedQuery1 = cities.whereEqualTo("state", "CO")
+ .whereEqualTo("name", "Denver");
+ // [END fs_chained_query]
+ return chainedQuery1;
+ }
+
+ /**
+ * An instance of a currently unsupported chained query: equality with inequality.
+ * NOTE : Requires support for creation of composite indices.
+ *
+ * @return query
+ */
+ Query createCompositeIndexChainedQuery() {
+ CollectionReference cities = db.collection("cities");
+ // [START fs_composite_index_chained_query]
+ Query chainedQuery2 = cities.whereEqualTo("state", "CA")
+ .whereLessThan("population", 1000000L);
+ // [END fs_composite_index_chained_query]
+ return chainedQuery2;
+ }
+
+ /**
+ * An instance of a valid range/inequality query : range operators are limited to a single field.
+ *
+ * @return query
+ */
+ Query createRangeQuery() {
+ CollectionReference cities = db.collection("cities");
+ // [START fs_range_query]
+ Query validQuery1 = cities.whereGreaterThanOrEqualTo("state", "CA")
+ .whereLessThanOrEqualTo("state", "IN");
+ Query validQuery2 = cities.whereEqualTo("state", "CA")
+ .whereGreaterThan("population", 1000000);
+ // [END fs_range_query]
+ return validQuery1;
+ }
+
+ /**
+ * An instance of an invalid range query : range operators are limited to a single field.
+ *
+ * @return query
+ */
+ Query createInvalidRangeQuery() {
+ CollectionReference cities = db.collection("cities");
+ // Violates constraint : range operators are limited to a single field
+ // [START fs_invalid_range_query]
+ Query invalidRangeQuery = cities.whereGreaterThanOrEqualTo("state", "CA")
+ .whereGreaterThan("population", 100000);
+ // [END fs_invalid_range_query]
+ return invalidRangeQuery;
+ }
+
+ /**
+ * Creates a query that combines order by with limit.
+ *
+ * @return query
+ */
+ Query createOrderByNameWithLimitQuery() {
+ CollectionReference cities = db.collection("cities");
+ // [START fs_order_by_name_limit_query]
+ Query query = cities.orderBy("name").limit(3);
+ // [END fs_order_by_name_limit_query]
+ return query;
+ }
+
+ /**
+ * Creates a query that orders by country and population(descending).
+ *
+ * @return query
+ */
+ Query createOrderByCountryAndPopulation() {
+ CollectionReference cities = db.collection("cities");
+ // [START fs_order_by_country_population]
+ Query query = cities.orderBy("state").orderBy("population", Direction.DESCENDING);
+ // [END fs_order_by_country_population]
+ return query;
+ }
+
+ /**
+ * Creates a query that combines order by in descending order with the limit operator.
+ *
+ * @return query
+ */
+ Query createOrderByNameDescWithLimitQuery() {
+ CollectionReference cities = db.collection("cities");
+ // [START fs_order_by_name_desc_limit_query]
+ Query query = cities.orderBy("name", Direction.DESCENDING).limit(3);
+ // [END fs_order_by_name_desc_limit_query]
+ return query;
+ }
+
+ /**
+ * Creates a query that combines where clause with order by and limit operator.
+ *
+ * @return query
+ */
+ Query createWhereWithOrderByAndLimitQuery() {
+ CollectionReference cities = db.collection("cities");
+ // [START fs_where_order_by_limit_query]
+ Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population").limit(2);
+ // [END fs_where_order_by_limit_query]
+ return query;
+ }
+
+ /**
+ * Creates a query using a range where clause with order by. Order by must be based on the same
+ * field as the range clause.
+ *
+ * @return query
+ */
+ Query createRangeWithOrderByQuery() {
+ CollectionReference cities = db.collection("cities");
+ // [START fs_range_order_by_query]
+ Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population");
+ // [END fs_range_order_by_query]
+ return query;
+ }
+
+ /**
+ * Creates an instance of an invalid range combined with order. Violates the constraint that range
+ * and order by are required to be on the same field.
+ *
+ * @return query
+ */
+ Query createInvalidRangeWithOrderByQuery() {
+ CollectionReference cities = db.collection("cities");
+ // Violates the constraint that range and order by are required to be on the same field
+ // [START fs_invalid_range_order_by_query]
+ Query query = cities.whereGreaterThan("population", 2500000L).orderBy("country");
+ // [END fs_invalid_range_order_by_query]
+ return query;
+ }
+
+ /**
+ * Create a query defining the start point of a query.
+ *
+ * @return query
+ */
+ Query createStartAtFieldQueryCursor() {
+ CollectionReference cities = db.collection("cities");
+ // [START fs_start_at_field_query_cursor]
+ Query query = cities.orderBy("population").startAt(4921000L);
+ // [END fs_start_at_field_query_cursor]
+ return query;
+ }
+
+ /**
+ * Create a query defining the start point of a query.
+ *
+ * @return query
+ */
+ Query createEndAtFieldQueryCursor() {
+ CollectionReference cities = db.collection("cities");
+ // [START fs_end_at_field_query_cursor]
+ Query query = cities.orderBy("population").endAt(4921000L);
+ // [END fs_end_at_field_query_cursor]
+ return query;
+ }
+
+ /* Create queries with multiple cursor conditions. */
+ void createMultipleCursorConditionsQuery() {
+ // [START fs_multiple_cursor_conditions]
+ // Will return all Springfields
+ Query query1 = db.collection("cities")
+ .orderBy("name")
+ .orderBy("state")
+ .startAt("Springfield");
+
+ // Will return "Springfield, Missouri" and "Springfield, Wisconsin"
+ Query query2 = db.collection("cities")
+ .orderBy("name")
+ .orderBy("state")
+ .startAt("Springfield", "Missouri");
+ // [END fs_multiple_cursor_conditions]
+ }
+}
diff --git a/firestore/src/main/java/com/example/firestore/snippets/References.java b/firestore/src/main/java/com/example/firestore/snippets/References.java
new file mode 100644
index 00000000000..52c4df728a0
--- /dev/null
+++ b/firestore/src/main/java/com/example/firestore/snippets/References.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2017 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.cloud.firestore.CollectionReference;
+import com.google.cloud.firestore.DocumentReference;
+import com.google.cloud.firestore.Firestore;
+
+/** Examples of references to a collection, document in a collection and subcollection. */
+public class References {
+
+ private final Firestore db;
+
+ public References(Firestore db) {
+ this.db = db;
+ }
+
+ /**
+ * Return a reference to collection.
+ *
+ * @return collection reference
+ */
+ public CollectionReference getACollectionRef() {
+ // [START fs_collection_ref]
+ // Reference to the collection "users"
+ CollectionReference collection = db.collection("users");
+ // [END fs_collection_ref]
+ return collection;
+ }
+
+ /**
+ * Return a reference to a document.
+ *
+ * @return document reference
+ */
+ public DocumentReference getADocumentRef() {
+ // [START fs_document_ref]
+ // Reference to a document with id "alovelace" in the collection "employees"
+ DocumentReference document = db.collection("users").document("alovelace");
+ // [END fs_document_ref]
+ return document;
+ }
+
+ /**
+ * Return a reference to a document using path.
+ *
+ * @return document reference
+ */
+ public DocumentReference getADocumentRefUsingPath() {
+ // [START fs_document_path_ref]
+ // Reference to a document with id "alovelace" in the collection "employees"
+ DocumentReference document = db.document("users/alovelace");
+ // [END fs_document_path_ref]
+ return document;
+ }
+
+ /**
+ * Return a reference to a document in a sub-collection.
+ *
+ * @return document reference in a subcollection
+ */
+ public DocumentReference getASubCollectionDocumentRef() {
+ // [START fs_subcollection_ref]
+ // Reference to a document in subcollection "messages"
+ DocumentReference document =
+ db.collection("rooms").document("roomA").collection("messages").document("message1");
+ // [END fs_subcollection_ref]
+ return document;
+ }
+}
diff --git a/firestore/src/main/java/com/example/firestore/snippets/RetrieveDataSnippets.java b/firestore/src/main/java/com/example/firestore/snippets/RetrieveDataSnippets.java
new file mode 100644
index 00000000000..e3c7515f841
--- /dev/null
+++ b/firestore/src/main/java/com/example/firestore/snippets/RetrieveDataSnippets.java
@@ -0,0 +1,138 @@
+/*
+ * Copyright 2017 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.example.firestore.snippets.model.City;
+
+import com.google.api.core.ApiFuture;
+import com.google.api.core.ApiFutures;
+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.QuerySnapshot;
+import com.google.cloud.firestore.WriteResult;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/** Snippets to demonstrate Firestore data retrieval operations. */
+public class RetrieveDataSnippets {
+
+ private final Firestore db;
+
+ RetrieveDataSnippets(Firestore db) {
+ this.db = db;
+ }
+
+ /** Create cities collection and add sample documents. */
+ void prepareExamples() throws Exception {
+ // [START fs_retrieve_create_examples]
+ CollectionReference cities = db.collection("cities");
+ List> futures = new ArrayList<>();
+ futures.add(cities.document("SF").set(new City("San Francisco", "CA", "USA", false, 860000L)));
+ futures.add(cities.document("LA").set(new City("Los Angeles", "CA", "USA", false, 3900000L)));
+ futures.add(cities.document("DC").set(new City("Washington D.C.", null, "USA", true, 680000L)));
+ futures.add(cities.document("TOK").set(new City("Tokyo", null, "Japan", true, 9000000L)));
+ futures.add(cities.document("BJ").set(new City("Beijing", null, "China", true, 21500000L)));
+ // (optional) block on operation
+ ApiFutures.allAsList(futures).get();
+ // [END fs_retrieve_create_examples]
+ }
+
+ /**
+ * Retrieves document in collection as map.
+ *
+ * @return map (string => object)
+ */
+ public Map getDocumentAsMap() throws Exception {
+ // [START fs_get_doc_as_map]
+ DocumentReference docRef = db.collection("cities").document("SF");
+ // asynchronously retrieve the document
+ ApiFuture future = docRef.get();
+ // ...
+ // future.get() blocks on response
+ DocumentSnapshot document = future.get();
+ if (document.exists()) {
+ System.out.println("Document data: " + document.getData());
+ } else {
+ System.out.println("No such document!");
+ }
+ // [END fs_get_doc_as_map]
+ return (document.exists()) ? document.getData() : null;
+ }
+
+ /**
+ * Retrieves document in collection as a custom object.
+ *
+ * @return document data as City object
+ */
+ public City getDocumentAsEntity() throws Exception {
+ // [START fs_get_doc_as_entity]
+ DocumentReference docRef = db.collection("cities").document("BJ");
+ // asynchronously retrieve the document
+ ApiFuture future = docRef.get();
+ // block on response
+ DocumentSnapshot document = future.get();
+ City city = null;
+ if (document.exists()) {
+ // convert document to POJO
+ city = document.toObject(City.class);
+ System.out.println(city);
+ } else {
+ System.out.println("No such document!");
+ }
+ // [END fs_get_doc_as_entity]
+ return city;
+ }
+
+ /**
+ * Return multiple documents from a collection based on a query.
+ *
+ * @return list of documents of capital cities.
+ */
+ 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();
+ for (DocumentSnapshot document : documents) {
+ System.out.println(document.getId() + " => " + document.toObject(City.class));
+ }
+ // [END fs_get_multiple_docs]
+ return documents;
+ }
+
+ /**
+ * Return all documents in the cities collection.
+ *
+ * @return list of documents
+ */
+ 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) {
+ System.out.println(document.getId() + " => " + document.toObject(City.class));
+ }
+ // [END fs_get_all_docs]
+ return documents;
+ }
+}
diff --git a/firestore/src/main/java/com/example/firestore/snippets/model/City.java b/firestore/src/main/java/com/example/firestore/snippets/model/City.java
new file mode 100644
index 00000000000..c3cc3ebcd5e
--- /dev/null
+++ b/firestore/src/main/java/com/example/firestore/snippets/model/City.java
@@ -0,0 +1,144 @@
+/*
+ * Copyright 2017 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.model;
+
+import java.util.Objects;
+
+/** Represents a city : name, weather, population, country, capital, geo coordinates. */
+public class City {
+
+ private String name;
+ private String state;
+ private String country;
+ private Boolean capital;
+ private Long population;
+
+ // [START fs_class_definition]
+ public City() {
+ // Must have a public no-argument constructor
+ }
+
+ // Initialize all fields of a city
+ public City(String name, String state, String country, Boolean capital, Long population) {
+ this.name = name;
+ this.state = state;
+ this.country = country;
+ this.capital = capital;
+ this.population = population;
+ }
+ // [END fs_class_definition]
+
+ public City(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getState() {
+ return state;
+ }
+
+ public void setState(String state) {
+ this.state = state;
+ }
+
+ public String getCountry() {
+ return country;
+ }
+
+ public void setCountry(String country) {
+ this.country = country;
+ }
+
+ public Boolean getCapital() {
+ return capital;
+ }
+
+ public void setCapital(Boolean capital) {
+ this.capital = capital;
+ }
+
+ public Long getPopulation() {
+ return population;
+ }
+
+ public void setPopulation(Long population) {
+ this.population = population;
+ }
+
+ private String getDefinedValue(String s) {
+ if (s != null) {
+ return s;
+ } else {
+ return "";
+ }
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+ if (name != null) {
+ sb.append(name);
+ }
+ if (state != null) {
+ sb.append(" state : ");
+ sb.append(state);
+ sb.append(",");
+ }
+ if (country != null) {
+ sb.append(", ");
+ sb.append(country);
+ }
+ sb.append(" : [");
+ if (population != null) {
+ sb.append(" population : ");
+ sb.append(population);
+ sb.append(",");
+ }
+ if (capital != null) {
+ sb.append(" capital : ");
+ sb.append(capital);
+ sb.append(",");
+ }
+ //remove trailing comma
+ if (sb.lastIndexOf(",") >= sb.length() - 1) {
+ sb.deleteCharAt(sb.length() - 1);
+ }
+ sb.append(" ]");
+ return sb.toString();
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (!(obj instanceof City)) {
+ return false;
+ }
+ City city = (City) obj;
+ return Objects.equals(name, city.name)
+ && Objects.equals(state, city.state)
+ && Objects.equals(country, city.country)
+ && Objects.equals(population, city.population)
+ && Objects.equals(capital, city.capital);
+
+ }
+}
diff --git a/firestore/src/test/java/com/example/firestore/QuickstartIT.java b/firestore/src/test/java/com/example/firestore/QuickstartIT.java
new file mode 100644
index 00000000000..a84d45ce1ad
--- /dev/null
+++ b/firestore/src/test/java/com/example/firestore/QuickstartIT.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright 2017 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 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;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class QuickstartIT {
+
+ 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);
+ deleteAllDocuments();
+ }
+
+ @Test
+ public void testQuickstart() throws Exception {
+ addData();
+
+ bout.reset();
+ quickstart.runAQuery();
+ String output = bout.toString();
+ // confirm that results do not contain aturing
+ assertTrue(output.contains("alovelace"));
+ assertTrue(output.contains("cbabbage"));
+ assertTrue(!output.contains("aturing"));
+
+ bout.reset();
+ quickstart.retrieveAllDocuments();
+ output = bout.toString();
+ // confirm that all documents are retrieved
+ assertTrue(output.contains("alovelace"));
+ assertTrue(output.contains("aturing"));
+ assertTrue(output.contains("cbabbage"));
+ }
+
+ private void validate(DocumentReference docRef, Map data) throws Exception {
+ DocumentSnapshot documentSnapshot = docRef.get().get();
+ assertTrue(Objects.equals(documentSnapshot.getData(), data));
+ }
+
+ private void addData() throws Exception {
+ Map expectedData = new HashMap<>();
+
+ quickstart.addDocument("alovelace");
+ expectedData.put("first", "Ada");
+ expectedData.put("last", "Lovelace");
+ expectedData.put("born", 1815L);
+ validate(db.document("users/alovelace"), expectedData);
+
+ expectedData.clear();
+ expectedData.put("first", "Alan");
+ expectedData.put("middle", "Mathison");
+ expectedData.put("last", "Turing");
+ expectedData.put("born", 1912L);
+ quickstart.addDocument("aturing");
+ validate(db.document("users/aturing"), expectedData);
+
+ expectedData.clear();
+ expectedData.put("first", "Charles");
+ expectedData.put("last", "Babbage");
+ expectedData.put("born", 1791L);
+ quickstart.addDocument("cbabbage");
+ validate(db.document("users/cbabbage"), expectedData);
+ }
+
+ private void deleteAllDocuments() throws Exception {
+ ApiFuture future = db.collection("users").get();
+ QuerySnapshot querySnapshot = future.get();
+ for (DocumentSnapshot doc : querySnapshot.getDocuments()) {
+ // block on delete operation
+ db.document("users/" + doc.getId()).delete().get();
+ }
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ deleteAllDocuments();
+ }
+}
diff --git a/firestore/src/test/java/com/example/firestore/snippets/ManageDataSnippetsIT.java b/firestore/src/test/java/com/example/firestore/snippets/ManageDataSnippetsIT.java
new file mode 100644
index 00000000000..c0a95fdd651
--- /dev/null
+++ b/firestore/src/test/java/com/example/firestore/snippets/ManageDataSnippetsIT.java
@@ -0,0 +1,213 @@
+/*
+ * Copyright 2017 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 junit.framework.TestCase.assertFalse;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+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;
+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)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class ManageDataSnippetsIT {
+
+ 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();
+ manageDataSnippets = new ManageDataSnippets(db);
+ }
+
+ @Test
+ public void testAddDatasMap() throws Exception {
+ Map expectedData = manageDataSnippets.addSimpleDocumentAsMap();
+ DocumentReference docRef = db.collection("cities").document("LA");
+ assertTrue(Objects.equals(expectedData, getDocumentDataAsMap(docRef)));
+ }
+
+ @Test
+ public void testAddDataWithDifferentDataTypes() throws Exception {
+ Map expectedData = manageDataSnippets.addDocumentWithDifferentDataTypes();
+ DocumentReference docRef = db.collection("data").document("one");
+ assertEquals(expectedData, getDocumentDataAsMap(docRef));
+ }
+
+ @Test
+ public void testAddDataAsEntity() throws Exception {
+ City city = manageDataSnippets.addSimpleDocumentAsEntity();
+ DocumentReference docRef = db.collection("cities").document("LA");
+ assertTrue(Objects.equals(city, getDocumentDataAsCity(docRef)));
+ }
+
+ @Test
+ public void testAddDocWithAutoGenId() throws Exception {
+ String autoId = manageDataSnippets.addDocumentDataWithAutoGeneratedId();
+ City city = new City("Tokyo");
+ city.setCountry("Japan");
+ DocumentReference docRef = db.collection("cities").document(autoId);
+ assertTrue(Objects.equals(city, getDocumentDataAsCity(docRef)));
+ }
+
+ @Test
+ public void testAddDocAfterAutoGenId() throws Exception {
+ String autoId = manageDataSnippets.addDocumentDataAfterAutoGeneratingId();
+ City city = new City();
+ DocumentReference docRef = db.collection("cities").document(autoId);
+ assertTrue(Objects.equals(city, getDocumentDataAsCity(docRef)));
+ }
+
+ @Test
+ public void testUpdateSimpleDocument() throws Exception {
+ manageDataSnippets.updateSimpleDocument();
+ DocumentReference docRef = db.collection("cities").document("DC");
+ City city = new City("Washington D.C.");
+ city.setCapital(true);
+ assertTrue(Objects.equals(city, getDocumentDataAsCity(docRef)));
+ }
+
+ @Test
+ public void testUpdateUsingMap() throws Exception {
+ manageDataSnippets.updateUsingMap();
+ DocumentReference docRef = db.collection("cities").document("DC");
+ City city = new City("Washington D.C.");
+ city.setCapital(true);
+ city.setCountry("USA");
+ assertTrue(Objects.equals(city, getDocumentDataAsCity(docRef)));
+ }
+
+ @Test
+ public void testUpdateAndCreateIfMissing() throws Exception {
+ manageDataSnippets.updateAndCreateIfMissing();
+ DocumentReference docRef = db.collection("cities").document("BJ");
+ assertTrue(getDocumentDataAsCity(docRef).getCapital());
+ }
+
+ @Test
+ public void testUpdateNestedFields() throws Exception {
+ manageDataSnippets.updateNestedFields();
+ DocumentReference docRef = db.collection("users").document("frank");
+
+ DocumentSnapshot snapshot = getDocumentData(docRef);
+ assertEquals((long) snapshot.getLong("age"), 13);
+ assertEquals(snapshot.getString("favorites.color"), "Red");
+ assertEquals(snapshot.getString("favorites.food"), "Pizza");
+ }
+
+ @Test
+ public void testUpdateServerTimestamp() throws Exception {
+ manageDataSnippets.updateServerTimestamp();
+ DocumentReference docRef = db.collection("objects").document("some-id");
+ Map data = getDocumentDataAsMap(docRef);
+ assertTrue(data.get("timestamp") instanceof Date);
+ }
+
+ @Test
+ public void testDeleteFields() throws Exception {
+ manageDataSnippets.deleteFields();
+ DocumentReference docRef = db.collection("cities").document("BJ");
+ Map data = getDocumentDataAsMap(docRef);
+ assertFalse(data.containsKey("capital"));
+ }
+
+ @Test(expected = Exception.class)
+ public void testDeleteDocument() throws Exception {
+ manageDataSnippets.deleteDocument();
+ getDocumentDataAsMap(db.collection("cities").document("DC"));
+ }
+
+ @Test
+ public void testSimpleTransaction() throws Exception {
+ DocumentReference docRef = db.collection("cities").document("SF");
+ ApiFuture future = manageDataSnippets.runSimpleTransaction();
+ future.get();
+ Map data = getDocumentDataAsMap(docRef);
+ assertEquals(data.get("population"), 860000L + 1L);
+ }
+
+ @Test
+ public void testTransactionReturnsInfo() throws Exception {
+ String info = manageDataSnippets.returnInfoFromTransaction(50L);
+ assertEquals(info, "Population increased to 51");
+ try {
+ info = manageDataSnippets.returnInfoFromTransaction(5000001L);
+ assertTrue("Should never get here", false);
+ } catch (Exception e) {
+ assertTrue(e.getMessage().contains("Sorry! Population is too big."));
+ }
+ }
+
+ @Test
+ public void testWriteBatchIsSuccessful() throws Exception {
+ manageDataSnippets.writeBatch();
+ CollectionReference collection = db.collection("cities");
+ ApiFuture document = collection.document("NYC").get();
+ assertTrue(document.get().exists());
+ DocumentReference documentReference = collection.document("SF");
+ Map data = getDocumentDataAsMap(documentReference);
+ assertTrue(data.containsKey("population"));
+ document = collection.document("LA").get();
+ 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);
+ manageDataSnippets.deleteCollection(db.collection("users"), 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
new file mode 100644
index 00000000000..3b93a38bac9
--- /dev/null
+++ b/firestore/src/test/java/com/example/firestore/snippets/QueryDataSnippetsIT.java
@@ -0,0 +1,225 @@
+/*
+ * Copyright 2017 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;
+import static org.junit.Assert.assertTrue;
+
+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.QuerySnapshot;
+import com.google.common.collect.ImmutableMap;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+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)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class QueryDataSnippetsIT {
+
+ 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();
+ }
+
+ @Test
+ public void testCreateAQuery() throws Exception {
+ Query q = queryDataSnippets.createAQuery();
+ Set result = getResultsAsSet(q);
+ Set expectedResults = new HashSet<>(Arrays.asList("DC", "TOK", "BJ"));
+ assertTrue(Objects.equals(result, expectedResults));
+ }
+
+ @Test
+ public void testSimpleQueryReturnsExpectedResults() throws Exception {
+ List> expectedResults = new ArrayList<>();
+
+ expectedResults.add(new HashSet<>(Arrays.asList("SF", "LA")));
+ expectedResults.add(new HashSet<>(Arrays.asList("SF", "DC")));
+ expectedResults.add(new HashSet<>(Arrays.asList("SF", "DC", "TOK")));
+
+ List queries = queryDataSnippets.createSimpleQueries();
+ for (int i = 0; i < queries.size(); i++) {
+ Set results = getResultsAsSet(queries.get(i));
+ assertTrue(Objects.equals(results, expectedResults.get(i)));
+ }
+ }
+
+ @Test
+ public void testChainedQuery() throws Exception {
+ Query q = queryDataSnippets.createChainedQuery();
+ Set result = getResultsAsSet(q);
+ Set expectedResults = new HashSet<>();
+ assertTrue(Objects.equals(result, expectedResults));
+ }
+
+ @Test
+ public void testRangeQuery() throws Exception {
+ Query q = queryDataSnippets.createRangeQuery();
+ Set result = getResultsAsSet(q);
+ Set expectedResults = new HashSet<>(Arrays.asList("SF", "LA"));
+ assertTrue(Objects.equals(result, expectedResults));
+ }
+
+ @Test(expected = Exception.class)
+ public void testInvalidRangeQueryThrowsException() throws Exception {
+ Query q = queryDataSnippets.createInvalidRangeQuery();
+ getResults(q);
+ }
+
+ @Test
+ public void testOrderByNameWithLimitQuery() throws Exception {
+ Query q = queryDataSnippets.createOrderByNameWithLimitQuery();
+ List result = getResults(q);
+ List expectedResults = Arrays.asList("BJ", "LA", "SF");
+ assertEquals(result, expectedResults);
+ }
+
+ @Test
+ public void testOrderByNameDescWithLimitQuery() throws Exception {
+ Query q = queryDataSnippets.createOrderByNameDescWithLimitQuery();
+ List result = getResults(q);
+ List expectedResults = Arrays.asList("DC", "TOK", "SF");
+ assertTrue(Objects.equals(result, expectedResults));
+ }
+
+ @Test
+ public void testWhereWithOrderByAndLimitQuery() throws Exception {
+ Query q = queryDataSnippets.createWhereWithOrderByAndLimitQuery();
+ List result = getResults(q);
+ List expectedResults = Arrays.asList("LA", "TOK");
+ assertEquals(result, expectedResults);
+ }
+
+ @Test
+ public void testRangeWithOrderByQuery() throws Exception {
+ Query q = queryDataSnippets.createRangeWithOrderByQuery();
+ List result = getResults(q);
+ List expectedResults = Arrays.asList("LA", "TOK", "BJ");
+ assertEquals(result, expectedResults);
+ }
+
+ @Test(expected = Exception.class)
+ public void testInvalidRangeWithOrderByQuery() throws Exception {
+ Query q = queryDataSnippets.createInvalidRangeWithOrderByQuery();
+ getResults(q);
+ }
+
+ @Test
+ public void testStartAtFieldQueryCursor() throws Exception {
+ Query q = queryDataSnippets.createStartAtFieldQueryCursor();
+ List expectedResults = Arrays.asList("TOK", "BJ");
+ List result = getResults(q);
+ assertTrue(Objects.equals(result, expectedResults));
+ }
+
+ @Test
+ public void testEndAtFieldQueryCursor() throws Exception {
+ Query q = queryDataSnippets.createEndAtFieldQueryCursor();
+ List expectedResults = Arrays.asList("DC", "SF", "LA");
+ List result = getResults(q);
+ assertEquals(result, expectedResults);
+ }
+
+ @Test
+ public void testMultipleCursorConditions() throws Exception {
+ // populate us_cities collection
+ Map city1 = new ImmutableMap.Builder()
+ .put("name", "Springfield").put("state", "Massachusetts").build();
+ Map city2 = new ImmutableMap.Builder()
+ .put("name", "Springfield").put("state", "Missouri").build();
+ Map city3 = new ImmutableMap.Builder()
+ .put("name", "Springfield").put("state", "Wisconsin").build();
+
+ db.collection("us_cities").document("Massachusetts").set(city1).get();
+ db.collection("us_cities").document("Missouri").set(city2).get();
+ db.collection("us_cities").document("Wisconsin").set(city3).get();
+
+ Query query1 = db.collection("us_cities")
+ .orderBy("name")
+ .orderBy("state")
+ .startAt("Springfield");
+
+ // all documents are retrieved
+ QuerySnapshot querySnapshot = query1.get().get();
+ List docs = querySnapshot.getDocuments();
+ assertEquals(3, docs.size());
+
+
+ // Will return "Springfield, Missouri" and "Springfield, Wisconsin"
+ Query query2 = db.collection("us_cities")
+ .orderBy("name")
+ .orderBy("state")
+ .startAt("Springfield", "Missouri");
+
+ // only Missouri and Wisconsin are retrieved
+ List expectedResults = Arrays.asList("Missouri", "Wisconsin");
+ List result = getResults(query2);
+ assertTrue(Objects.equals(result, expectedResults));
+ }
+
+ private Set getResultsAsSet(Query query) throws Exception {
+ List docIds = getResults(query);
+ return new HashSet<>(docIds);
+ }
+
+ private List getResults(Query query) throws Exception {
+ // asynchronously retrieve query results
+ ApiFuture future = query.get();
+ // block on response
+ QuerySnapshot querySnapshot = future.get();
+ List docIds = new ArrayList<>();
+ for (DocumentSnapshot document : querySnapshot.getDocuments()) {
+ docIds.add(document.getId());
+ }
+ 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();
+ }
+}
diff --git a/firestore/src/test/java/com/example/firestore/snippets/RetrieveDataSnippetsIT.java b/firestore/src/test/java/com/example/firestore/snippets/RetrieveDataSnippetsIT.java
new file mode 100644
index 00000000000..455733d29d3
--- /dev/null
+++ b/firestore/src/test/java/com/example/firestore/snippets/RetrieveDataSnippetsIT.java
@@ -0,0 +1,113 @@
+/*
+ * Copyright 2017 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;
+import static org.junit.Assert.assertTrue;
+
+import com.example.firestore.snippets.model.City;
+
+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.QuerySnapshot;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+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)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class RetrieveDataSnippetsIT {
+ private static Firestore db;
+ 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();
+ }
+
+ @Test
+ public void testRetrievalAsMap() throws Exception {
+ Map data = retrieveDataSnippets.getDocumentAsMap();
+ assertEquals(data.get("name"), "San Francisco");
+ assertEquals(data.get("country"), "USA");
+ assertEquals(data.get("capital"), false);
+ assertEquals(data.get("population"), 860000L);
+ }
+
+ @Test
+ public void testRetrieveAsEntity() throws Exception {
+ City city = retrieveDataSnippets.getDocumentAsEntity();
+ assertEquals(city.getName(), "Beijing");
+ assertEquals(city.getCountry(), "China");
+ assertEquals(city.getCapital(), true);
+ assertEquals((long) city.getPopulation(), 21500000L);
+ }
+
+ @Test
+ public void testRetrieveQueryResults() throws Exception {
+ List docs = retrieveDataSnippets.getQueryResults();
+ assertEquals(docs.size(), 3);
+ Set docIds = new HashSet<>();
+ for (DocumentSnapshot doc : docs) {
+ docIds.add(doc.getId());
+ }
+ assertTrue(docIds.contains("BJ") && docIds.contains("TOK") && docIds.contains("DC"));
+ }
+
+ @Test
+ public void testRetrieveAllDocuments() throws Exception {
+ List docs = retrieveDataSnippets.getAllDocuments();
+ assertEquals(docs.size(), 5);
+ Set docIds = new HashSet<>();
+ for (DocumentSnapshot doc : docs) {
+ docIds.add(doc.getId());
+ }
+ assertTrue(
+ docIds.contains("SF")
+ && docIds.contains("LA")
+ && docIds.contains("DC")
+ && docIds.contains("TOK")
+ && docIds.contains("BJ"));
+ }
+
+ 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 tearDownAfterClass() throws Exception {
+ deleteAllDocuments();
+ }
+}
diff --git a/pom.xml b/pom.xml
index d14b425184c..07e7db7bc59 100644
--- a/pom.xml
+++ b/pom.xml
@@ -56,6 +56,8 @@
errorreporting
+ firestore
+
iap
kms