Skip to content

Commit 469effa

Browse files
authored
Upgrading Google Cloud Dependencies (#203)
* Upgrading Google Cloud Dependencies * Exposing FirestoreOptions via FirebaseOptions * Updated comment
1 parent 7a58ee0 commit 469effa

File tree

9 files changed

+130
-12
lines changed

9 files changed

+130
-12
lines changed

pom.xml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@
375375
<dependency>
376376
<groupId>com.google.api-client</groupId>
377377
<artifactId>google-api-client</artifactId>
378-
<version>1.23.0</version>
378+
<version>1.25.0</version>
379379
<exclusions>
380380
<exclusion>
381381
<groupId>com.google.guava</groupId>
@@ -386,32 +386,32 @@
386386
<dependency>
387387
<groupId>com.google.api-client</groupId>
388388
<artifactId>google-api-client-gson</artifactId>
389-
<version>1.23.0</version>
389+
<version>1.25.0</version>
390390
</dependency>
391391
<dependency>
392392
<groupId>com.google.http-client</groupId>
393393
<artifactId>google-http-client</artifactId>
394-
<version>1.23.0</version>
394+
<version>1.25.0</version>
395395
</dependency>
396396
<dependency>
397397
<groupId>com.google.api</groupId>
398398
<artifactId>api-common</artifactId>
399-
<version>1.2.0</version>
399+
<version>1.7.0</version>
400400
</dependency>
401401
<dependency>
402402
<groupId>com.google.auth</groupId>
403403
<artifactId>google-auth-library-oauth2-http</artifactId>
404-
<version>0.8.0</version>
404+
<version>0.11.0</version>
405405
</dependency>
406406
<dependency>
407407
<groupId>com.google.cloud</groupId>
408408
<artifactId>google-cloud-storage</artifactId>
409-
<version>1.27.0</version>
409+
<version>1.43.0</version>
410410
</dependency>
411411
<dependency>
412412
<groupId>com.google.cloud</groupId>
413413
<artifactId>google-cloud-firestore</artifactId>
414-
<version>0.45.0-beta</version>
414+
<version>0.61.0-beta</version>
415415
</dependency>
416416

417417
<!-- Utilities -->

src/main/java/com/google/firebase/FirebaseOptions.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.api.client.json.JsonFactory;
2525
import com.google.api.client.util.Key;
2626
import com.google.auth.oauth2.GoogleCredentials;
27+
import com.google.cloud.firestore.FirestoreOptions;
2728
import com.google.common.base.Strings;
2829
import com.google.common.collect.ImmutableList;
2930
import com.google.firebase.internal.FirebaseThreadManagers;
@@ -66,6 +67,7 @@ public final class FirebaseOptions {
6667
private final int readTimeout;
6768
private final JsonFactory jsonFactory;
6869
private final ThreadManager threadManager;
70+
private final FirestoreOptions firestoreOptions;
6971

7072
private FirebaseOptions(@NonNull FirebaseOptions.Builder builder) {
7173
this.credentials = checkNotNull(builder.credentials,
@@ -94,6 +96,7 @@ private FirebaseOptions(@NonNull FirebaseOptions.Builder builder) {
9496
this.connectTimeout = builder.connectTimeout;
9597
checkArgument(builder.readTimeout >= 0);
9698
this.readTimeout = builder.readTimeout;
99+
this.firestoreOptions = builder.firestoreOptions;
97100
}
98101

99102
/**
@@ -193,6 +196,19 @@ ThreadManager getThreadManager() {
193196
return threadManager;
194197
}
195198

199+
FirestoreOptions getFirestoreOptions() {
200+
return firestoreOptions;
201+
}
202+
203+
/**
204+
* Creates an empty builder.
205+
*
206+
* @return A new builder instance.
207+
*/
208+
public static Builder builder() {
209+
return new Builder();
210+
}
211+
196212
/**
197213
* Builder for constructing {@link FirebaseOptions}.
198214
*/
@@ -213,6 +229,7 @@ public static final class Builder {
213229
private String serviceAccountId;
214230

215231
private GoogleCredentials credentials;
232+
private FirestoreOptions firestoreOptions;
216233
private HttpTransport httpTransport = Utils.getDefaultTransport();
217234
private JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
218235
private ThreadManager threadManager = FirebaseThreadManagers.DEFAULT_THREAD_MANAGER;
@@ -239,6 +256,7 @@ public Builder(FirebaseOptions options) {
239256
threadManager = options.threadManager;
240257
connectTimeout = options.connectTimeout;
241258
readTimeout = options.readTimeout;
259+
firestoreOptions = options.firestoreOptions;
242260
}
243261

244262
/**
@@ -384,6 +402,22 @@ public Builder setThreadManager(ThreadManager threadManager) {
384402
return this;
385403
}
386404

405+
/**
406+
* Sets the <code>FirestoreOptions</code> used to initialize Firestore in the
407+
* {@link com.google.firebase.cloud.FirestoreClient} API. This can be used to customize
408+
* low-level transport (GRPC) parameters, and timestamp handling behavior.
409+
*
410+
* <p>If credentials or a project ID is set in <code>FirestoreOptions</code>, they will get
411+
* overwritten by the corresponding parameters in <code>FirebaseOptions</code>.
412+
*
413+
* @param firestoreOptions A <code>FirestoreOptions</code> instance.
414+
* @return This <code>Builder</code> instance is returned so subsequent calls can be chained.
415+
*/
416+
public Builder setFirestoreOptions(FirestoreOptions firestoreOptions) {
417+
this.firestoreOptions = firestoreOptions;
418+
return this;
419+
}
420+
387421
/**
388422
* Sets the connect timeout for outgoing HTTP (REST) connections made by the SDK. This is used
389423
* when opening a communication link to a remote HTTP endpoint. This setting does not

src/main/java/com/google/firebase/ImplFirebaseTrampolines.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.api.core.ApiFuture;
2020
import com.google.auth.oauth2.GoogleCredentials;
21+
import com.google.cloud.firestore.FirestoreOptions;
2122
import com.google.firebase.internal.FirebaseService;
2223
import com.google.firebase.internal.NonNull;
2324

@@ -43,6 +44,10 @@ public static String getProjectId(@NonNull FirebaseApp app) {
4344
return app.getProjectId();
4445
}
4546

47+
public static FirestoreOptions getFirestoreOptions(@NonNull FirebaseApp app) {
48+
return app.getOptions().getFirestoreOptions();
49+
}
50+
4651
public static boolean isDefaultApp(@NonNull FirebaseApp app) {
4752
return app.isDefaultApp();
4853
}

src/main/java/com/google/firebase/cloud/FirestoreClient.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static com.google.common.base.Preconditions.checkArgument;
44
import static com.google.common.base.Preconditions.checkNotNull;
55

6+
import com.google.api.gax.core.FixedCredentialsProvider;
67
import com.google.cloud.firestore.Firestore;
78
import com.google.cloud.firestore.FirestoreOptions;
89
import com.google.common.base.Strings;
@@ -34,8 +35,13 @@ private FirestoreClient(FirebaseApp app) {
3435
"Project ID is required for accessing Firestore. Use a service account credential or "
3536
+ "set the project ID explicitly via FirebaseOptions. Alternatively you can also "
3637
+ "set the project ID via the GOOGLE_CLOUD_PROJECT environment variable.");
37-
this.firestore = FirestoreOptions.newBuilder()
38-
.setCredentials(ImplFirebaseTrampolines.getCredentials(app))
38+
FirestoreOptions userOptions = ImplFirebaseTrampolines.getFirestoreOptions(app);
39+
FirestoreOptions.Builder builder = userOptions != null
40+
? userOptions.toBuilder() : FirestoreOptions.newBuilder();
41+
this.firestore = builder
42+
// CredentialsProvider has highest priority in FirestoreOptions, so we set that.
43+
.setCredentialsProvider(
44+
FixedCredentialsProvider.create(ImplFirebaseTrampolines.getCredentials(app)))
3945
.setProjectId(projectId)
4046
.build()
4147
.getService();

src/test/java/com/google/firebase/FirebaseOptionsTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.google.auth.oauth2.AccessToken;
3131
import com.google.auth.oauth2.GoogleCredentials;
3232
import com.google.auth.oauth2.ServiceAccountCredentials;
33+
import com.google.cloud.firestore.FirestoreOptions;
3334
import com.google.firebase.testing.ServiceAccount;
3435
import com.google.firebase.testing.TestUtils;
3536
import java.io.IOException;
@@ -75,6 +76,9 @@ protected ThreadFactory getThreadFactory() {
7576
public void createOptionsWithAllValuesSet() throws IOException {
7677
GsonFactory jsonFactory = new GsonFactory();
7778
NetHttpTransport httpTransport = new NetHttpTransport();
79+
FirestoreOptions firestoreOptions = FirestoreOptions.newBuilder()
80+
.setTimestampsInSnapshotsEnabled(true)
81+
.build();
7882
FirebaseOptions firebaseOptions =
7983
new FirebaseOptions.Builder()
8084
.setDatabaseUrl(FIREBASE_DB_URL)
@@ -86,6 +90,7 @@ public void createOptionsWithAllValuesSet() throws IOException {
8690
.setThreadManager(MOCK_THREAD_MANAGER)
8791
.setConnectTimeout(30000)
8892
.setReadTimeout(60000)
93+
.setFirestoreOptions(firestoreOptions)
8994
.build();
9095
assertEquals(FIREBASE_DB_URL, firebaseOptions.getDatabaseUrl());
9196
assertEquals(FIREBASE_STORAGE_BUCKET, firebaseOptions.getStorageBucket());
@@ -95,6 +100,7 @@ public void createOptionsWithAllValuesSet() throws IOException {
95100
assertSame(MOCK_THREAD_MANAGER, firebaseOptions.getThreadManager());
96101
assertEquals(30000, firebaseOptions.getConnectTimeout());
97102
assertEquals(60000, firebaseOptions.getReadTimeout());
103+
assertSame(firestoreOptions, firebaseOptions.getFirestoreOptions());
98104

99105
GoogleCredentials credentials = firebaseOptions.getCredentials();
100106
assertNotNull(credentials);
@@ -124,6 +130,7 @@ public void createOptionsWithOnlyMandatoryValuesSet() throws IOException {
124130
assertEquals(
125131
GoogleCredential.fromStream(ServiceAccount.EDITOR.asStream()).getServiceAccountId(),
126132
((ServiceAccountCredentials) credentials).getClientEmail());
133+
assertNull(firebaseOptions.getFirestoreOptions());
127134
}
128135

129136
@Test
@@ -185,6 +192,8 @@ public void checkToBuilderCreatesNewEquivalentInstance() {
185192
assertEquals(ALL_VALUES_OPTIONS.getThreadManager(), allValuesOptionsCopy.getThreadManager());
186193
assertEquals(ALL_VALUES_OPTIONS.getConnectTimeout(), allValuesOptionsCopy.getConnectTimeout());
187194
assertEquals(ALL_VALUES_OPTIONS.getReadTimeout(), allValuesOptionsCopy.getReadTimeout());
195+
assertSame(ALL_VALUES_OPTIONS.getFirestoreOptions(),
196+
allValuesOptionsCopy.getFirestoreOptions());
188197
}
189198

190199
@Test(expected = IllegalArgumentException.class)

src/test/java/com/google/firebase/auth/FirebaseAuthTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.google.auth.oauth2.UserCredentials;
3737
import com.google.common.base.Defaults;
3838
import com.google.common.base.Strings;
39+
import com.google.common.collect.ImmutableList;
3940
import com.google.firebase.FirebaseApp;
4041
import com.google.firebase.FirebaseOptions;
4142
import com.google.firebase.ImplFirebaseTrampolines;
@@ -132,7 +133,8 @@ public HttpTransport create() {
132133
}
133134

134135
private static GoogleCredentials createCertificateCredential() throws IOException {
135-
final MockTokenServerTransport transport = new MockTokenServerTransport();
136+
final MockTokenServerTransport transport = new MockTokenServerTransport(
137+
"https://accounts.google.com/o/oauth2/token");
136138
transport.addServiceAccount(ServiceAccount.EDITOR.getEmail(), ACCESS_TOKEN);
137139
return ServiceAccountCredentials.fromStream(ServiceAccount.EDITOR.asStream(),
138140
new HttpTransportFactory() {

src/test/java/com/google/firebase/cloud/FirestoreClientTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertSame;
6+
import static org.junit.Assert.assertTrue;
57
import static org.junit.Assert.fail;
68

79
import com.google.auth.oauth2.GoogleCredentials;
810
import com.google.cloud.firestore.Firestore;
11+
import com.google.cloud.firestore.FirestoreOptions;
912
import com.google.firebase.FirebaseApp;
1013
import com.google.firebase.FirebaseOptions;
14+
import com.google.firebase.FirebaseOptions.Builder;
15+
import com.google.firebase.ImplFirebaseTrampolines;
1116
import com.google.firebase.TestOnlyImplFirebaseTrampolines;
1217
import com.google.firebase.testing.ServiceAccount;
1318
import java.io.IOException;
@@ -26,6 +31,9 @@ public void testExplicitProjectId() throws IOException {
2631
FirebaseApp app = FirebaseApp.initializeApp(new FirebaseOptions.Builder()
2732
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
2833
.setProjectId("explicit-project-id")
34+
.setFirestoreOptions(FirestoreOptions.newBuilder()
35+
.setTimestampsInSnapshotsEnabled(true)
36+
.build())
2937
.build());
3038
Firestore firestore = FirestoreClient.getFirestore(app);
3139
assertEquals("explicit-project-id", firestore.getOptions().getProjectId());
@@ -38,6 +46,9 @@ public void testExplicitProjectId() throws IOException {
3846
public void testServiceAccountProjectId() throws IOException {
3947
FirebaseApp app = FirebaseApp.initializeApp(new FirebaseOptions.Builder()
4048
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
49+
.setFirestoreOptions(FirestoreOptions.newBuilder()
50+
.setTimestampsInSnapshotsEnabled(true)
51+
.build())
4152
.build());
4253
Firestore firestore = FirestoreClient.getFirestore(app);
4354
assertEquals("mock-project-id", firestore.getOptions().getProjectId());
@@ -46,11 +57,56 @@ public void testServiceAccountProjectId() throws IOException {
4657
assertEquals("mock-project-id", firestore.getOptions().getProjectId());
4758
}
4859

60+
@Test
61+
public void testFirestoreOptions() throws IOException {
62+
FirebaseApp app = FirebaseApp.initializeApp(new Builder()
63+
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
64+
.setProjectId("explicit-project-id")
65+
.setFirestoreOptions(FirestoreOptions.newBuilder()
66+
.setTimestampsInSnapshotsEnabled(true)
67+
.build())
68+
.build());
69+
Firestore firestore = FirestoreClient.getFirestore(app);
70+
assertEquals("explicit-project-id", firestore.getOptions().getProjectId());
71+
assertTrue(firestore.getOptions().areTimestampsInSnapshotsEnabled());
72+
73+
firestore = FirestoreClient.getFirestore();
74+
assertEquals("explicit-project-id", firestore.getOptions().getProjectId());
75+
assertTrue(firestore.getOptions().areTimestampsInSnapshotsEnabled());
76+
}
77+
78+
@Test
79+
public void testFirestoreOptionsOverride() throws IOException {
80+
FirebaseApp app = FirebaseApp.initializeApp(new Builder()
81+
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
82+
.setProjectId("explicit-project-id")
83+
.setFirestoreOptions(FirestoreOptions.newBuilder()
84+
.setTimestampsInSnapshotsEnabled(true)
85+
.setProjectId("other-project-id")
86+
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
87+
.build())
88+
.build());
89+
Firestore firestore = FirestoreClient.getFirestore(app);
90+
assertEquals("explicit-project-id", firestore.getOptions().getProjectId());
91+
assertTrue(firestore.getOptions().areTimestampsInSnapshotsEnabled());
92+
assertSame(ImplFirebaseTrampolines.getCredentials(app),
93+
firestore.getOptions().getCredentialsProvider().getCredentials());
94+
95+
firestore = FirestoreClient.getFirestore();
96+
assertEquals("explicit-project-id", firestore.getOptions().getProjectId());
97+
assertTrue(firestore.getOptions().areTimestampsInSnapshotsEnabled());
98+
assertSame(ImplFirebaseTrampolines.getCredentials(app),
99+
firestore.getOptions().getCredentialsProvider().getCredentials());
100+
}
101+
49102
@Test
50103
public void testAppDelete() throws IOException {
51104
FirebaseApp app = FirebaseApp.initializeApp(new FirebaseOptions.Builder()
52105
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
53106
.setProjectId("mock-project-id")
107+
.setFirestoreOptions(FirestoreOptions.newBuilder()
108+
.setTimestampsInSnapshotsEnabled(true)
109+
.build())
54110
.build());
55111

56112
assertNotNull(FirestoreClient.getFirestore(app));

src/test/java/com/google/firebase/testing/IntegrationTestUtils.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020

2121
import com.google.api.client.googleapis.util.Utils;
2222
import com.google.api.client.json.GenericJson;
23+
import com.google.cloud.firestore.FirestoreOptions;
2324
import com.google.common.collect.ImmutableList;
2425
import com.google.common.io.CharStreams;
2526
import com.google.firebase.FirebaseApp;
2627
import com.google.firebase.FirebaseOptions;
28+
import com.google.firebase.FirebaseOptions.Builder;
2729
import com.google.firebase.TestOnlyImplFirebaseTrampolines;
2830
import com.google.firebase.database.DatabaseReference;
2931
import com.google.firebase.database.FirebaseDatabase;
@@ -105,10 +107,13 @@ public static synchronized String getApiKey() {
105107
public static synchronized FirebaseApp ensureDefaultApp() {
106108
if (masterApp == null) {
107109
FirebaseOptions options =
108-
new FirebaseOptions.Builder()
110+
FirebaseOptions.builder()
109111
.setDatabaseUrl(getDatabaseUrl())
110112
.setStorageBucket(getStorageBucket())
111113
.setCredentials(TestUtils.getCertCredential(getServiceAccountCertificate()))
114+
.setFirestoreOptions(FirestoreOptions.newBuilder()
115+
.setTimestampsInSnapshotsEnabled(true)
116+
.build())
112117
.build();
113118
masterApp = FirebaseApp.initializeApp(options);
114119
}

src/test/java/com/google/firebase/testing/TestUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ public static synchronized GoogleCredentials getApplicationDefaultCredentials()
106106
if (defaultCredentials != null) {
107107
return defaultCredentials;
108108
}
109-
final MockTokenServerTransport transport = new MockTokenServerTransport();
109+
final MockTokenServerTransport transport = new MockTokenServerTransport(
110+
"https://accounts.google.com/o/oauth2/token");
110111
transport.addServiceAccount(ServiceAccount.EDITOR.getEmail(), TEST_ADC_ACCESS_TOKEN);
111112
File serviceAccount = new File("src/test/resources/service_accounts", "editor.json");
112113
Map<String, String> environmentVariables =

0 commit comments

Comments
 (0)