77import static org .junit .Assert .fail ;
88
99import com .google .auth .oauth2 .GoogleCredentials ;
10+ import com .google .cloud .firestore .DocumentReference ;
1011import com .google .cloud .firestore .Firestore ;
1112import com .google .cloud .firestore .FirestoreOptions ;
1213import com .google .firebase .FirebaseApp ;
1314import com .google .firebase .FirebaseOptions ;
1415import com .google .firebase .FirebaseOptions .Builder ;
1516import com .google .firebase .ImplFirebaseTrampolines ;
1617import com .google .firebase .TestOnlyImplFirebaseTrampolines ;
18+ import com .google .firebase .auth .MockGoogleCredentials ;
1719import com .google .firebase .testing .ServiceAccount ;
1820import java .io .IOException ;
1921import org .junit .After ;
2022import org .junit .Test ;
2123
2224public class FirestoreClientTest {
2325
26+ public static final FirestoreOptions FIRESTORE_OPTIONS = FirestoreOptions .newBuilder ()
27+ // Setting credentials is not required (they get overridden by Admin SDK), but without
28+ // this Firestore logs an ugly warning during tests.
29+ .setCredentials (new MockGoogleCredentials ("test-token" ))
30+ .setTimestampsInSnapshotsEnabled (true )
31+ .build ();
32+
2433 @ After
2534 public void tearDown () {
2635 TestOnlyImplFirebaseTrampolines .clearInstancesForTest ();
@@ -31,9 +40,7 @@ public void testExplicitProjectId() throws IOException {
3140 FirebaseApp app = FirebaseApp .initializeApp (new FirebaseOptions .Builder ()
3241 .setCredentials (GoogleCredentials .fromStream (ServiceAccount .EDITOR .asStream ()))
3342 .setProjectId ("explicit-project-id" )
34- .setFirestoreOptions (FirestoreOptions .newBuilder ()
35- .setTimestampsInSnapshotsEnabled (true )
36- .build ())
43+ .setFirestoreOptions (FIRESTORE_OPTIONS )
3744 .build ());
3845 Firestore firestore = FirestoreClient .getFirestore (app );
3946 assertEquals ("explicit-project-id" , firestore .getOptions ().getProjectId ());
@@ -46,9 +53,7 @@ public void testExplicitProjectId() throws IOException {
4653 public void testServiceAccountProjectId () throws IOException {
4754 FirebaseApp app = FirebaseApp .initializeApp (new FirebaseOptions .Builder ()
4855 .setCredentials (GoogleCredentials .fromStream (ServiceAccount .EDITOR .asStream ()))
49- .setFirestoreOptions (FirestoreOptions .newBuilder ()
50- .setTimestampsInSnapshotsEnabled (true )
51- .build ())
56+ .setFirestoreOptions (FIRESTORE_OPTIONS )
5257 .build ());
5358 Firestore firestore = FirestoreClient .getFirestore (app );
5459 assertEquals ("mock-project-id" , firestore .getOptions ().getProjectId ());
@@ -62,9 +67,7 @@ public void testFirestoreOptions() throws IOException {
6267 FirebaseApp app = FirebaseApp .initializeApp (new Builder ()
6368 .setCredentials (GoogleCredentials .fromStream (ServiceAccount .EDITOR .asStream ()))
6469 .setProjectId ("explicit-project-id" )
65- .setFirestoreOptions (FirestoreOptions .newBuilder ()
66- .setTimestampsInSnapshotsEnabled (true )
67- .build ())
70+ .setFirestoreOptions (FIRESTORE_OPTIONS )
6871 .build ());
6972 Firestore firestore = FirestoreClient .getFirestore (app );
7073 assertEquals ("explicit-project-id" , firestore .getOptions ().getProjectId ());
@@ -104,12 +107,12 @@ public void testAppDelete() throws IOException {
104107 FirebaseApp app = FirebaseApp .initializeApp (new FirebaseOptions .Builder ()
105108 .setCredentials (GoogleCredentials .fromStream (ServiceAccount .EDITOR .asStream ()))
106109 .setProjectId ("mock-project-id" )
107- .setFirestoreOptions (FirestoreOptions .newBuilder ()
108- .setTimestampsInSnapshotsEnabled (true )
109- .build ())
110+ .setFirestoreOptions (FIRESTORE_OPTIONS )
110111 .build ());
111112
112- assertNotNull (FirestoreClient .getFirestore (app ));
113+ Firestore firestore = FirestoreClient .getFirestore (app );
114+ assertNotNull (firestore );
115+ DocumentReference document = firestore .collection ("collection" ).document ("doc" );
113116 app .delete ();
114117 try {
115118 FirestoreClient .getFirestore (app );
@@ -118,12 +121,18 @@ public void testAppDelete() throws IOException {
118121 // ignore
119122 }
120123
124+ try {
125+ document .get ();
126+ fail ("No error thrown for deleted app" );
127+ } catch (IllegalStateException expected ) {
128+ // ignore
129+ }
130+
121131 try {
122132 FirestoreClient .getFirestore ();
123133 fail ("No error thrown for deleted app" );
124134 } catch (IllegalStateException expected ) {
125135 // ignore
126136 }
127137 }
128-
129138}
0 commit comments