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