Skip to content

Commit

Permalink
Upgraded google-cloud-firestore to the latest version (0.61.0-beta) a…
Browse files Browse the repository at this point in the history
…nd added a FirebaseOptions#setFireStoreTimestampsInSnapshotsEnabled(boolean) method.

The FirestoreOptions#setTimestampsInSnapshotsEnabled(boolean) method is then called in the FirestoreClient constructor
with the value passed to the FirebaseOptions#setFireStoreTimestampsInSnapshotsEnabled(boolean) method.

This allows for firebase-admin-java users to adapt to the Timestamps being returned in DocumentSnapshots as added in
googleapis/google-cloud-java#3317

com.google.api:gax was added as a dependency to the pom.xml file as there were
"java.lang.NoClassDefFoundError: com/google/api/gax/rpc/ClientStream" errors when running "mvn test"
without this dependency.

A couple of tests were also added to the FirestoreClientTest class.
  • Loading branch information
RohanTalip committed Sep 11, 2018
1 parent 7a58ee0 commit 34c4927
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,11 @@
<artifactId>api-common</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.google.api</groupId>
<artifactId>gax</artifactId>
<version>1.30.0</version>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
Expand All @@ -411,7 +416,7 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-firestore</artifactId>
<version>0.45.0-beta</version>
<version>0.61.0-beta</version>
</dependency>

<!-- Utilities -->
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/google/firebase/FirebaseOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public final class FirebaseOptions {
private final int readTimeout;
private final JsonFactory jsonFactory;
private final ThreadManager threadManager;
private final boolean firestoreTimestampsInSnapshotsEnabled;

private FirebaseOptions(@NonNull FirebaseOptions.Builder builder) {
this.credentials = checkNotNull(builder.credentials,
Expand Down Expand Up @@ -94,6 +95,8 @@ private FirebaseOptions(@NonNull FirebaseOptions.Builder builder) {
this.connectTimeout = builder.connectTimeout;
checkArgument(builder.readTimeout >= 0);
this.readTimeout = builder.readTimeout;
this.firestoreTimestampsInSnapshotsEnabled =
builder.firestoreTimestampsInSnapshotsEnabled;
}

/**
Expand Down Expand Up @@ -188,6 +191,14 @@ public int getReadTimeout() {
return readTimeout;
}

/**
* Returns whether or not {@link com.google.cloud.firestore.DocumentSnapshot DocumentSnapshots}
* return timestamp fields as {@link com.google.cloud.Timestamp Timestamps}.
*/
public boolean areFirestoreTimestampsInSnapshotsEnabled() {
return firestoreTimestampsInSnapshotsEnabled;
}

@NonNull
ThreadManager getThreadManager() {
return threadManager;
Expand Down Expand Up @@ -218,6 +229,7 @@ public static final class Builder {
private ThreadManager threadManager = FirebaseThreadManagers.DEFAULT_THREAD_MANAGER;
private int connectTimeout;
private int readTimeout;
private boolean firestoreTimestampsInSnapshotsEnabled;

/** Constructs an empty builder. */
public Builder() {}
Expand All @@ -239,6 +251,7 @@ public Builder(FirebaseOptions options) {
threadManager = options.threadManager;
connectTimeout = options.connectTimeout;
readTimeout = options.readTimeout;
firestoreTimestampsInSnapshotsEnabled = options.firestoreTimestampsInSnapshotsEnabled;
}

/**
Expand Down Expand Up @@ -411,6 +424,20 @@ public Builder setReadTimeout(int readTimeout) {
return this;
}

/**
* Sets whether timestamps are enabled in Firestore
* {@link com.google.cloud.firestore.DocumentSnapshot DocumentSnapshots}.
*
* @param firestoreTimestampsInSnapshotsEnabled If true, timestamps are enabled in Firestore
* DocumentSnapshots.
* @return This <code>Builder</code> instance is returned so subsequent calls can be chained.
*/
public Builder setFirestoreTimestampsInSnapshotsEnabled(
boolean firestoreTimestampsInSnapshotsEnabled) {
this.firestoreTimestampsInSnapshotsEnabled = firestoreTimestampsInSnapshotsEnabled;
return this;
}

/**
* Builds the {@link FirebaseOptions} instance from the previously set options.
*
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/google/firebase/cloud/FirestoreClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ private FirestoreClient(FirebaseApp app) {
+ "set the project ID explicitly via FirebaseOptions. Alternatively you can also "
+ "set the project ID via the GOOGLE_CLOUD_PROJECT environment variable.");
this.firestore = FirestoreOptions.newBuilder()
.setTimestampsInSnapshotsEnabled(
app.getOptions().areFirestoreTimestampsInSnapshotsEnabled())
.setCredentials(ImplFirebaseTrampolines.getCredentials(app))
.setProjectId(projectId)
.build()
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/google/firebase/cloud/FirestoreClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,33 @@ public void testServiceAccountProjectId() throws IOException {
assertEquals("mock-project-id", firestore.getOptions().getProjectId());
}

@Test
public void testFirestoreTimestampsInSnapshotsEnabled_defaultsToFalse() throws IOException {
FirebaseApp app = FirebaseApp.initializeApp(new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
.setProjectId("explicit-project-id")
.build());
Firestore firestore = FirestoreClient.getFirestore(app);
assertEquals(false, firestore.getOptions().areTimestampsInSnapshotsEnabled());

firestore = FirestoreClient.getFirestore();
assertEquals(false, firestore.getOptions().areTimestampsInSnapshotsEnabled());
}

@Test
public void testFirestoreTimestampsInSnapshotsEnabled_setToTrue() throws IOException {
FirebaseApp app = FirebaseApp.initializeApp(new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
.setProjectId("explicit-project-id")
.setFirestoreTimestampsInSnapshotsEnabled(true)
.build());
Firestore firestore = FirestoreClient.getFirestore(app);
assertEquals(true, firestore.getOptions().areTimestampsInSnapshotsEnabled());

firestore = FirestoreClient.getFirestore();
assertEquals(true, firestore.getOptions().areTimestampsInSnapshotsEnabled());
}

@Test
public void testAppDelete() throws IOException {
FirebaseApp app = FirebaseApp.initializeApp(new FirebaseOptions.Builder()
Expand Down

0 comments on commit 34c4927

Please sign in to comment.