Repository navigation
Expand file tree
/
Copy pathFirestoreClientTest.java
More file actions
177 lines (144 loc) · 7.62 KB
/
Copy pathFirestoreClientTest.java
File metadata and controls
177 lines (144 loc) · 7.62 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.google.firebase.cloud;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
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"))
.setDatabaseId("differedDefaultDatabaseId")
.build();
@After
public void tearDown() {
TestOnlyImplFirebaseTrampolines.clearInstancesForTest();
}
@Test
public void testExplicitProjectId() throws IOException {
final String databaseId = "databaseIdInTestExplicitProjectId";
FirebaseApp app = FirebaseApp.initializeApp(FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
.setProjectId("explicit-project-id")
.setFirestoreOptions(FIRESTORE_OPTIONS)
.build());
Firestore firestore1 = FirestoreClient.getFirestore(app);
assertEquals("explicit-project-id", firestore1.getOptions().getProjectId());
assertEquals(FIRESTORE_OPTIONS.getDatabaseId(), firestore1.getOptions().getDatabaseId());
assertSame(firestore1, FirestoreClient.getFirestore());
Firestore firestore2 = FirestoreClient.getFirestore(app, databaseId);
assertEquals("explicit-project-id", firestore2.getOptions().getProjectId());
assertEquals(databaseId, firestore2.getOptions().getDatabaseId());
assertSame(firestore2, FirestoreClient.getFirestore(databaseId));
assertNotSame(firestore1, firestore2);
}
@Test
public void testServiceAccountProjectId() throws IOException {
final String databaseId = "databaseIdInTestServiceAccountProjectId";
FirebaseApp app = FirebaseApp.initializeApp(FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
.setFirestoreOptions(FIRESTORE_OPTIONS)
.build());
Firestore firestore1 = FirestoreClient.getFirestore(app);
assertEquals("mock-project-id", firestore1.getOptions().getProjectId());
assertEquals(FIRESTORE_OPTIONS.getDatabaseId(), firestore1.getOptions().getDatabaseId());
assertSame(firestore1, FirestoreClient.getFirestore());
Firestore firestore2 = FirestoreClient.getFirestore(app, databaseId);
assertEquals("mock-project-id", firestore2.getOptions().getProjectId());
assertEquals(databaseId, firestore2.getOptions().getDatabaseId());
assertSame(firestore2, FirestoreClient.getFirestore(databaseId));
assertNotSame(firestore1, firestore2);
}
@Test
public void testAbsentFirestoreOptions() throws Exception {
String projectId = "test-proj";
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
.setProjectId(projectId)
.build();
FirebaseApp.initializeApp(options);
Firestore firestore = FirestoreClient.getFirestore();
assertEquals(projectId, firestore.getOptions().getProjectId());
assertEquals("(default)", firestore.getOptions().getDatabaseId());
}
@Test
public void testFirestoreOptions() throws IOException {
final String databaseId = "databaseIdInTestFirestoreOptions";
FirebaseApp app = FirebaseApp.initializeApp(FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
.setProjectId("explicit-project-id")
.setFirestoreOptions(FIRESTORE_OPTIONS)
.build());
Firestore firestore1 = FirestoreClient.getFirestore(app);
assertEquals("explicit-project-id", firestore1.getOptions().getProjectId());
assertEquals(FIRESTORE_OPTIONS.getDatabaseId(), firestore1.getOptions().getDatabaseId());
assertSame(firestore1, FirestoreClient.getFirestore());
Firestore firestore2 = FirestoreClient.getFirestore(app, databaseId);
assertEquals("explicit-project-id", firestore2.getOptions().getProjectId());
assertEquals(databaseId, firestore2.getOptions().getDatabaseId());
assertSame(firestore2, FirestoreClient.getFirestore(databaseId));
assertNotSame(firestore1, firestore2);
}
@Test
public void testFirestoreOptionsOverride() throws IOException {
final String databaseId = "databaseIdInTestFirestoreOptions";
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 firestore1 = FirestoreClient.getFirestore(app);
assertEquals("explicit-project-id", firestore1.getOptions().getProjectId());
assertSame(ImplFirebaseTrampolines.getCredentials(app),
firestore1.getOptions().getCredentialsProvider().getCredentials());
assertEquals("(default)", firestore1.getOptions().getDatabaseId());
assertSame(firestore1, FirestoreClient.getFirestore());
Firestore firestore2 = FirestoreClient.getFirestore(app, databaseId);
assertEquals("explicit-project-id", firestore2.getOptions().getProjectId());
assertSame(ImplFirebaseTrampolines.getCredentials(app),
firestore2.getOptions().getCredentialsProvider().getCredentials());
assertEquals(databaseId, firestore2.getOptions().getDatabaseId());
assertSame(firestore2, FirestoreClient.getFirestore(databaseId));
assertNotSame(firestore1, firestore2);
}
@Test
public void testAppDelete() throws IOException {
final String databaseId = "databaseIdInTestAppDelete";
FirebaseApp app = FirebaseApp.initializeApp(FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
.setProjectId("mock-project-id")
.setFirestoreOptions(FIRESTORE_OPTIONS)
.build());
Firestore firestore1 = FirestoreClient.getFirestore(app);
assertNotNull(firestore1);
assertSame(firestore1, FirestoreClient.getFirestore());
Firestore firestore2 = FirestoreClient.getFirestore(app, databaseId);
assertNotNull(firestore2);
assertSame(firestore2, FirestoreClient.getFirestore(databaseId));
assertNotSame(firestore1, firestore2);
DocumentReference document = firestore1.collection("collection").document("doc");
app.delete();
assertThrows(IllegalStateException.class, () -> FirestoreClient.getFirestore(app));
assertThrows(IllegalStateException.class, () -> document.get());
assertThrows(IllegalStateException.class, () -> FirestoreClient.getFirestore());
assertThrows(IllegalStateException.class, () -> FirestoreClient.getFirestore(app, databaseId));
assertThrows(IllegalStateException.class, () -> FirestoreClient.getFirestore(databaseId));
}
}