Repository navigation
Expand file tree
/
Copy pathFirestoreClientTest.java
More file actions
131 lines (114 loc) · 5.04 KB
/
Copy pathFirestoreClientTest.java
File metadata and controls
131 lines (114 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.google.firebase.cloud;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.firestore.DocumentReference;
import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.FirestoreOptions;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.ImplFirebaseTrampolines;
import com.google.firebase.TestOnlyImplFirebaseTrampolines;
import com.google.firebase.auth.MockGoogleCredentials;
import com.google.firebase.testing.ServiceAccount;
import java.io.IOException;
import org.junit.After;
import org.junit.Test;
public class FirestoreClientTest {
private static final FirestoreOptions FIRESTORE_OPTIONS = FirestoreOptions.newBuilder()
// Setting credentials is not required (they get overridden by Admin SDK), but without
// this Firestore logs an ugly warning during tests.
.setCredentials(new MockGoogleCredentials("test-token"))
.build();
@After
public void tearDown() {
TestOnlyImplFirebaseTrampolines.clearInstancesForTest();
}
@Test
public void testExplicitProjectId() throws IOException {
FirebaseApp app = FirebaseApp.initializeApp(FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
.setProjectId("explicit-project-id")
.setFirestoreOptions(FIRESTORE_OPTIONS)
.build());
Firestore firestore = FirestoreClient.getFirestore(app);
assertEquals("explicit-project-id", firestore.getOptions().getProjectId());
firestore = FirestoreClient.getFirestore();
assertEquals("explicit-project-id", firestore.getOptions().getProjectId());
}
@Test
public void testServiceAccountProjectId() throws IOException {
FirebaseApp app = FirebaseApp.initializeApp(FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
.setFirestoreOptions(FIRESTORE_OPTIONS)
.build());
Firestore firestore = FirestoreClient.getFirestore(app);
assertEquals("mock-project-id", firestore.getOptions().getProjectId());
firestore = FirestoreClient.getFirestore();
assertEquals("mock-project-id", firestore.getOptions().getProjectId());
}
@Test
public void testFirestoreOptions() throws IOException {
FirebaseApp app = FirebaseApp.initializeApp(FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
.setProjectId("explicit-project-id")
.setFirestoreOptions(FIRESTORE_OPTIONS)
.build());
Firestore firestore = FirestoreClient.getFirestore(app);
assertEquals("explicit-project-id", firestore.getOptions().getProjectId());
firestore = FirestoreClient.getFirestore();
assertEquals("explicit-project-id", firestore.getOptions().getProjectId());
}
@Test
public void testFirestoreOptionsOverride() throws IOException {
FirebaseApp app = FirebaseApp.initializeApp(FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
.setProjectId("explicit-project-id")
.setFirestoreOptions(FirestoreOptions.newBuilder()
.setProjectId("other-project-id")
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
.build())
.build());
Firestore firestore = FirestoreClient.getFirestore(app);
assertEquals("explicit-project-id", firestore.getOptions().getProjectId());
assertSame(ImplFirebaseTrampolines.getCredentials(app),
firestore.getOptions().getCredentialsProvider().getCredentials());
firestore = FirestoreClient.getFirestore();
assertEquals("explicit-project-id", firestore.getOptions().getProjectId());
assertSame(ImplFirebaseTrampolines.getCredentials(app),
firestore.getOptions().getCredentialsProvider().getCredentials());
}
@Test
public void testAppDelete() throws IOException {
FirebaseApp app = FirebaseApp.initializeApp(FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
.setProjectId("mock-project-id")
.setFirestoreOptions(FIRESTORE_OPTIONS)
.build());
Firestore firestore = FirestoreClient.getFirestore(app);
assertNotNull(firestore);
DocumentReference document = firestore.collection("collection").document("doc");
app.delete();
try {
FirestoreClient.getFirestore(app);
fail("No error thrown for deleted app");
} catch (IllegalStateException expected) {
// ignore
}
try {
document.get();
fail("No error thrown for deleted app");
} catch (IllegalStateException expected) {
// ignore
}
try {
FirestoreClient.getFirestore();
fail("No error thrown for deleted app");
} catch (IllegalStateException expected) {
// ignore
}
}
}