1
1
package com .google .firebase .cloud ;
2
2
3
- import static org .junit .Assert .assertEquals ;
4
- import static org .junit .Assert .assertNotNull ;
5
- import static org .junit .Assert .assertSame ;
6
- import static org .junit .Assert .assertTrue ;
7
- import static org .junit .Assert .fail ;
8
-
9
3
import com .google .auth .oauth2 .GoogleCredentials ;
10
4
import com .google .cloud .firestore .DocumentReference ;
11
5
import com .google .cloud .firestore .Firestore ;
20
14
import org .junit .After ;
21
15
import org .junit .Test ;
22
16
17
+ import static org .junit .Assert .*;
18
+
23
19
public class FirestoreClientTest {
24
20
25
21
private static final FirestoreOptions FIRESTORE_OPTIONS = FirestoreOptions .newBuilder ()
26
22
// Setting credentials is not required (they get overridden by Admin SDK), but without
27
23
// this Firestore logs an ugly warning during tests.
28
24
.setCredentials (new MockGoogleCredentials ("test-token" ))
25
+ .setDatabaseId ("differedDefaultDatabaseId" )
29
26
.build ();
30
27
31
28
@ After
@@ -35,47 +32,75 @@ public void tearDown() {
35
32
36
33
@ Test
37
34
public void testExplicitProjectId () throws IOException {
35
+ String databaseId = "databaseIdInTestExplicitProjectId" ;
38
36
FirebaseApp app = FirebaseApp .initializeApp (FirebaseOptions .builder ()
39
37
.setCredentials (GoogleCredentials .fromStream (ServiceAccount .EDITOR .asStream ()))
40
38
.setProjectId ("explicit-project-id" )
41
39
.setFirestoreOptions (FIRESTORE_OPTIONS )
42
40
.build ());
43
- Firestore firestore = FirestoreClient .getFirestore (app );
44
- assertEquals ("explicit-project-id" , firestore .getOptions ().getProjectId ());
41
+ Firestore firestore1 = FirestoreClient .getFirestore (app );
42
+ assertEquals ("explicit-project-id" , firestore1 .getOptions ().getProjectId ());
43
+ assertEquals (FIRESTORE_OPTIONS .getDatabaseId (), firestore1 .getOptions ().getDatabaseId ());
44
+
45
+ assertSame (firestore1 , FirestoreClient .getFirestore ());
46
+
47
+ Firestore firestore2 = FirestoreClient .getFirestore (app , databaseId );
48
+ assertEquals ("explicit-project-id" , firestore2 .getOptions ().getProjectId ());
49
+ assertEquals (databaseId , firestore2 .getOptions ().getDatabaseId ());
50
+
51
+ assertSame (firestore2 , FirestoreClient .getFirestore (databaseId ));
45
52
46
- firestore = FirestoreClient .getFirestore ();
47
- assertEquals ("explicit-project-id" , firestore .getOptions ().getProjectId ());
53
+ assertNotSame (firestore1 , firestore2 );
48
54
}
49
55
50
56
@ Test
51
57
public void testServiceAccountProjectId () throws IOException {
58
+ String databaseId = "databaseIdInTestServiceAccountProjectId" ;
52
59
FirebaseApp app = FirebaseApp .initializeApp (FirebaseOptions .builder ()
53
60
.setCredentials (GoogleCredentials .fromStream (ServiceAccount .EDITOR .asStream ()))
54
61
.setFirestoreOptions (FIRESTORE_OPTIONS )
55
62
.build ());
56
- Firestore firestore = FirestoreClient .getFirestore (app );
57
- assertEquals ("mock-project-id" , firestore .getOptions ().getProjectId ());
63
+ Firestore firestore1 = FirestoreClient .getFirestore (app );
64
+ assertEquals ("mock-project-id" , firestore1 .getOptions ().getProjectId ());
65
+ assertEquals (FIRESTORE_OPTIONS .getDatabaseId (), firestore1 .getOptions ().getDatabaseId ());
58
66
59
- firestore = FirestoreClient .getFirestore ();
60
- assertEquals ("mock-project-id" , firestore .getOptions ().getProjectId ());
67
+ assertSame (firestore1 , FirestoreClient .getFirestore ());
68
+
69
+ Firestore firestore2 = FirestoreClient .getFirestore (app , databaseId );
70
+ assertEquals ("mock-project-id" , firestore2 .getOptions ().getProjectId ());
71
+ assertEquals (databaseId , firestore2 .getOptions ().getDatabaseId ());
72
+
73
+ assertSame (firestore2 , FirestoreClient .getFirestore (databaseId ));
74
+
75
+ assertNotSame (firestore1 , firestore2 );
61
76
}
62
77
63
78
@ Test
64
79
public void testFirestoreOptions () throws IOException {
80
+ String databaseId = "databaseIdInTestFirestoreOptions" ;
65
81
FirebaseApp app = FirebaseApp .initializeApp (FirebaseOptions .builder ()
66
82
.setCredentials (GoogleCredentials .fromStream (ServiceAccount .EDITOR .asStream ()))
67
83
.setProjectId ("explicit-project-id" )
68
84
.setFirestoreOptions (FIRESTORE_OPTIONS )
69
85
.build ());
70
- Firestore firestore = FirestoreClient .getFirestore (app );
71
- assertEquals ("explicit-project-id" , firestore .getOptions ().getProjectId ());
86
+ Firestore firestore1 = FirestoreClient .getFirestore (app );
87
+ assertEquals ("explicit-project-id" , firestore1 .getOptions ().getProjectId ());
88
+ assertEquals (FIRESTORE_OPTIONS .getDatabaseId (), firestore1 .getOptions ().getDatabaseId ());
89
+
90
+ assertSame (firestore1 , FirestoreClient .getFirestore ());
72
91
73
- firestore = FirestoreClient .getFirestore ();
74
- assertEquals ("explicit-project-id" , firestore .getOptions ().getProjectId ());
92
+ Firestore firestore2 = FirestoreClient .getFirestore (app , databaseId );
93
+ assertEquals ("explicit-project-id" , firestore2 .getOptions ().getProjectId ());
94
+ assertEquals (databaseId , firestore2 .getOptions ().getDatabaseId ());
95
+
96
+ assertSame (firestore2 , FirestoreClient .getFirestore (databaseId ));
97
+
98
+ assertNotSame (firestore1 , firestore2 );
75
99
}
76
100
77
101
@ Test
78
102
public void testFirestoreOptionsOverride () throws IOException {
103
+ String databaseId = "databaseIdInTestFirestoreOptions" ;
79
104
FirebaseApp app = FirebaseApp .initializeApp (FirebaseOptions .builder ()
80
105
.setCredentials (GoogleCredentials .fromStream (ServiceAccount .EDITOR .asStream ()))
81
106
.setProjectId ("explicit-project-id" )
@@ -84,48 +109,51 @@ public void testFirestoreOptionsOverride() throws IOException {
84
109
.setCredentials (GoogleCredentials .fromStream (ServiceAccount .EDITOR .asStream ()))
85
110
.build ())
86
111
.build ());
87
- Firestore firestore = FirestoreClient .getFirestore (app );
88
- assertEquals ("explicit-project-id" , firestore .getOptions ().getProjectId ());
112
+ Firestore firestore1 = FirestoreClient .getFirestore (app );
113
+ assertEquals ("explicit-project-id" , firestore1 .getOptions ().getProjectId ());
89
114
assertSame (ImplFirebaseTrampolines .getCredentials (app ),
90
- firestore .getOptions ().getCredentialsProvider ().getCredentials ());
115
+ firestore1 .getOptions ().getCredentialsProvider ().getCredentials ());
116
+ assertEquals ("(default)" , firestore1 .getOptions ().getDatabaseId ());
117
+
118
+ assertSame (firestore1 , FirestoreClient .getFirestore ());
91
119
92
- firestore = FirestoreClient .getFirestore ();
93
- assertEquals ("explicit-project-id" , firestore .getOptions ().getProjectId ());
120
+ Firestore firestore2 = FirestoreClient .getFirestore (app , databaseId );
121
+ assertEquals ("explicit-project-id" , firestore2 .getOptions ().getProjectId ());
94
122
assertSame (ImplFirebaseTrampolines .getCredentials (app ),
95
- firestore .getOptions ().getCredentialsProvider ().getCredentials ());
123
+ firestore2 .getOptions ().getCredentialsProvider ().getCredentials ());
124
+ assertEquals (databaseId , firestore2 .getOptions ().getDatabaseId ());
125
+
126
+ assertSame (firestore2 , FirestoreClient .getFirestore (databaseId ));
127
+
128
+ assertNotSame (firestore1 , firestore2 );
96
129
}
97
130
98
131
@ Test
99
132
public void testAppDelete () throws IOException {
133
+ String databaseId = "databaseIdInTestAppDelete" ;
100
134
FirebaseApp app = FirebaseApp .initializeApp (FirebaseOptions .builder ()
101
135
.setCredentials (GoogleCredentials .fromStream (ServiceAccount .EDITOR .asStream ()))
102
136
.setProjectId ("mock-project-id" )
103
137
.setFirestoreOptions (FIRESTORE_OPTIONS )
104
138
.build ());
105
139
106
- Firestore firestore = FirestoreClient .getFirestore (app );
107
- assertNotNull (firestore );
108
- DocumentReference document = firestore .collection ("collection" ).document ("doc" );
140
+ Firestore firestore1 = FirestoreClient .getFirestore (app );
141
+ assertNotNull (firestore1 );
142
+ assertSame (firestore1 , FirestoreClient .getFirestore ());
143
+
144
+ Firestore firestore2 = FirestoreClient .getFirestore (app , databaseId );
145
+ assertNotNull (firestore2 );
146
+ assertSame (firestore2 , FirestoreClient .getFirestore (databaseId ));
147
+
148
+ assertNotSame (firestore1 , firestore2 );
149
+
150
+ DocumentReference document = firestore1 .collection ("collection" ).document ("doc" );
109
151
app .delete ();
110
- try {
111
- FirestoreClient .getFirestore (app );
112
- fail ("No error thrown for deleted app" );
113
- } catch (IllegalStateException expected ) {
114
- // ignore
115
- }
116
-
117
- try {
118
- document .get ();
119
- fail ("No error thrown for deleted app" );
120
- } catch (IllegalStateException expected ) {
121
- // ignore
122
- }
123
-
124
- try {
125
- FirestoreClient .getFirestore ();
126
- fail ("No error thrown for deleted app" );
127
- } catch (IllegalStateException expected ) {
128
- // ignore
129
- }
152
+
153
+ assertThrows (IllegalStateException .class , () -> FirestoreClient .getFirestore (app ));
154
+ assertThrows (IllegalStateException .class , () -> document .get ());
155
+ assertThrows (IllegalStateException .class , () -> FirestoreClient .getFirestore ());
156
+ assertThrows (IllegalStateException .class , () -> FirestoreClient .getFirestore (app , databaseId ));
157
+ assertThrows (IllegalStateException .class , () -> FirestoreClient .getFirestore (databaseId ));
130
158
}
131
159
}
0 commit comments