-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Snippets and tests for Firestore listens #995
Merged
kurtisvg
merged 8 commits into
GoogleCloudPlatform:master
from
samtstern:java-watch-snippets
Jan 19, 2018
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
793f6d7
Snippets and tests for Firestore listens
samtstern fda5dd8
Fix comment block
samtstern 70d62a6
Update to 0.33 SDK
samtstern 3c6f836
Merge branch 'master' into java-watch-snippets
lesv a795dcf
Fix checkstyle issues
samtstern 87f954c
Merge branch 'java-watch-snippets' of github.com:samtstern/java-docs-…
samtstern f181b0a
Suppress name warning
samtstern 6fec327
Merge branch 'master' into java-watch-snippets
samtstern File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
firestore/src/main/java/com/example/firestore/snippets/ListenDataSnippets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Object> listenToDocument() throws Exception { | ||
final SettableApiFuture<Map<String, Object>> future = SettableApiFuture.create(); | ||
|
||
// [START listen_to_document] | ||
DocumentReference docRef = db.collection("cities").document("SF"); | ||
docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() { | ||
@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<String> listenForMultiple() throws Exception { | ||
final SettableApiFuture<List<String>> future = SettableApiFuture.create(); | ||
|
||
// [START listen_to_multiple] | ||
db.collection("cities") | ||
.whereEqualTo("state", "CA") | ||
.addSnapshotListener(new EventListener<QuerySnapshot>() { | ||
@Override | ||
public void onEvent(@Nullable QuerySnapshot snapshots, | ||
@Nullable FirestoreException e) { | ||
if (e != null) { | ||
System.err.println("Listen failed:" + e); | ||
return; | ||
} | ||
|
||
List<String> 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<DocumentChange> listenForChanges() throws Exception { | ||
SettableApiFuture<List<DocumentChange>> future = SettableApiFuture.create(); | ||
|
||
// [START listen_for_changes] | ||
db.collection("cities") | ||
.whereEqualTo("state", "CA") | ||
.addSnapshotListener(new EventListener<QuerySnapshot>() { | ||
@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<QuerySnapshot>() { | ||
// [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<QuerySnapshot>() { | ||
@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] | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
firestore/src/test/java/com/example/firestore/BaseIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Object> 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<QuerySnapshot> 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(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So make sure this isn't listed in the parent pom for testing as it may break things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lesv sorry not sure what you mean by "make sure this isn't listed in the parent pom"? Also this project ID was added by @jabubake I just moved it into a new base class.
Side note: the tests failed but that's expected due to the
SNAPSHOT
build of the SDK. Waiting for the nextgoogle-cloud-java
release.