22
33import static org .junit .Assert .assertEquals ;
44import static org .junit .Assert .assertNotNull ;
5+ import static org .junit .Assert .assertNotSame ;
56import 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 ;
88
99import com .google .auth .oauth2 .GoogleCredentials ;
1010import com .google .cloud .firestore .DocumentReference ;
@@ -26,6 +26,7 @@ public class FirestoreClientTest {
2626 // Setting credentials is not required (they get overridden by Admin SDK), but without
2727 // this Firestore logs an ugly warning during tests.
2828 .setCredentials (new MockGoogleCredentials ("test-token" ))
29+ .setDatabaseId ("differedDefaultDatabaseId" )
2930 .build ();
3031
3132 @ After
@@ -35,47 +36,75 @@ public void tearDown() {
3536
3637 @ Test
3738 public void testExplicitProjectId () throws IOException {
39+ final String databaseId = "databaseIdInTestExplicitProjectId" ;
3840 FirebaseApp app = FirebaseApp .initializeApp (FirebaseOptions .builder ()
3941 .setCredentials (GoogleCredentials .fromStream (ServiceAccount .EDITOR .asStream ()))
4042 .setProjectId ("explicit-project-id" )
4143 .setFirestoreOptions (FIRESTORE_OPTIONS )
4244 .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 ());
4548
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 );
4858 }
4959
5060 @ Test
5161 public void testServiceAccountProjectId () throws IOException {
62+ final String databaseId = "databaseIdInTestServiceAccountProjectId" ;
5263 FirebaseApp app = FirebaseApp .initializeApp (FirebaseOptions .builder ()
5364 .setCredentials (GoogleCredentials .fromStream (ServiceAccount .EDITOR .asStream ()))
5465 .setFirestoreOptions (FIRESTORE_OPTIONS )
5566 .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 ());
5872
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 );
6180 }
6281
6382 @ Test
6483 public void testFirestoreOptions () throws IOException {
84+ final String databaseId = "databaseIdInTestFirestoreOptions" ;
6585 FirebaseApp app = FirebaseApp .initializeApp (FirebaseOptions .builder ()
6686 .setCredentials (GoogleCredentials .fromStream (ServiceAccount .EDITOR .asStream ()))
6787 .setProjectId ("explicit-project-id" )
6888 .setFirestoreOptions (FIRESTORE_OPTIONS )
6989 .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 ());
7299
73- firestore = FirestoreClient .getFirestore ();
74- assertEquals ("explicit-project-id" , firestore .getOptions ().getProjectId ());
100+ assertSame (firestore2 , FirestoreClient .getFirestore (databaseId ));
101+
102+ assertNotSame (firestore1 , firestore2 );
75103 }
76104
77105 @ Test
78106 public void testFirestoreOptionsOverride () throws IOException {
107+ final String databaseId = "databaseIdInTestFirestoreOptions" ;
79108 FirebaseApp app = FirebaseApp .initializeApp (FirebaseOptions .builder ()
80109 .setCredentials (GoogleCredentials .fromStream (ServiceAccount .EDITOR .asStream ()))
81110 .setProjectId ("explicit-project-id" )
@@ -84,48 +113,51 @@ public void testFirestoreOptionsOverride() throws IOException {
84113 .setCredentials (GoogleCredentials .fromStream (ServiceAccount .EDITOR .asStream ()))
85114 .build ())
86115 .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 ());
89118 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 ());
91123
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 ());
94126 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 );
96133 }
97134
98135 @ Test
99136 public void testAppDelete () throws IOException {
137+ final String databaseId = "databaseIdInTestAppDelete" ;
100138 FirebaseApp app = FirebaseApp .initializeApp (FirebaseOptions .builder ()
101139 .setCredentials (GoogleCredentials .fromStream (ServiceAccount .EDITOR .asStream ()))
102140 .setProjectId ("mock-project-id" )
103141 .setFirestoreOptions (FIRESTORE_OPTIONS )
104142 .build ());
105143
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" );
109155 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 ));
130162 }
131163}
0 commit comments