3434import static com .google .cloud .firestore .LocalFirestoreHelper .set ;
3535import static com .google .cloud .firestore .LocalFirestoreHelper .update ;
3636import static org .junit .Assert .assertEquals ;
37+ import static org .junit .Assert .assertNull ;
3738import static org .junit .Assert .assertTrue ;
3839import static org .junit .Assert .fail ;
3940import static org .mockito .Mockito .doAnswer ;
@@ -132,6 +133,33 @@ public String updateCallback(Transaction transaction) {
132133 assertEquals (commit (TRANSACTION_ID ), requests .get (1 ));
133134 }
134135
136+ @ Test
137+ public void returnsValueAsync () throws Exception {
138+ doReturn (beginResponse ())
139+ .doReturn (commitResponse (0 , 0 ))
140+ .when (firestoreMock )
141+ .sendRequest (requestCapture .capture (), Matchers .<UnaryCallable <Message , Message >>any ());
142+
143+ ApiFuture <String > transaction =
144+ firestoreMock .runAsyncTransaction (
145+ new Transaction .AsyncFunction <String >() {
146+ @ Override
147+ public ApiFuture <String > updateCallback (Transaction transaction ) {
148+ Assert .assertEquals ("user_provided" , Thread .currentThread ().getName ());
149+ return ApiFutures .immediateFuture ("foo" );
150+ }
151+ },
152+ options );
153+
154+ assertEquals ("foo" , transaction .get ());
155+
156+ List <Message > requests = requestCapture .getAllValues ();
157+ assertEquals (2 , requests .size ());
158+
159+ assertEquals (begin (), requests .get (0 ));
160+ assertEquals (commit (TRANSACTION_ID ), requests .get (1 ));
161+ }
162+
135163 @ Test
136164 public void canReturnNull () throws Exception {
137165 doReturn (beginResponse ())
@@ -154,6 +182,28 @@ public String updateCallback(Transaction transaction) {
154182 assertEquals (null , transaction .get ());
155183 }
156184
185+ @ Test
186+ public void canReturnNullAsync () throws Exception {
187+ doReturn (beginResponse ())
188+ .doReturn (ApiFutures .immediateFailedFuture (new Exception ()))
189+ .doReturn (beginResponse (ByteString .copyFromUtf8 ("foo2" )))
190+ .doReturn (commitResponse (0 , 0 ))
191+ .when (firestoreMock )
192+ .sendRequest (requestCapture .capture (), Matchers .<UnaryCallable <Message , Message >>any ());
193+
194+ ApiFuture <String > transaction =
195+ firestoreMock .runAsyncTransaction (
196+ new Transaction .AsyncFunction <String >() {
197+ @ Override
198+ public ApiFuture <String > updateCallback (Transaction transaction ) {
199+ return ApiFutures .immediateFuture (null );
200+ }
201+ },
202+ options );
203+
204+ assertNull (transaction .get ());
205+ }
206+
157207 @ Test
158208 public void rollbackOnCallbackError () throws Exception {
159209 doReturn (beginResponse ())
@@ -185,6 +235,37 @@ public String updateCallback(Transaction transaction) throws Exception {
185235 assertEquals (rollback (), requests .get (1 ));
186236 }
187237
238+ @ Test
239+ public void rollbackOnCallbackErrorAsync () throws Exception {
240+ doReturn (beginResponse ())
241+ .doReturn (rollbackResponse ())
242+ .when (firestoreMock )
243+ .sendRequest (requestCapture .capture (), Matchers .<UnaryCallable <Message , Message >>any ());
244+
245+ ApiFuture <String > transaction =
246+ firestoreMock .runAsyncTransaction (
247+ new Transaction .AsyncFunction <String >() {
248+ @ Override
249+ public ApiFuture <String > updateCallback (Transaction transaction ) {
250+ return ApiFutures .immediateFailedFuture (new Exception ("Expected exception" ));
251+ }
252+ },
253+ options );
254+
255+ try {
256+ transaction .get ();
257+ fail ();
258+ } catch (Exception e ) {
259+ assertTrue (e .getMessage ().endsWith ("Expected exception" ));
260+ }
261+
262+ List <Message > requests = requestCapture .getAllValues ();
263+ assertEquals (2 , requests .size ());
264+
265+ assertEquals (begin (), requests .get (0 ));
266+ assertEquals (rollback (), requests .get (1 ));
267+ }
268+
188269 @ Test
189270 public void noRollbackOnBeginFailure () throws Exception {
190271 doReturn (ApiFutures .immediateFailedFuture (new Exception ("Expected exception" )))
@@ -213,6 +294,34 @@ public String updateCallback(Transaction transaction) {
213294 assertEquals (1 , requests .size ());
214295 }
215296
297+ @ Test
298+ public void noRollbackOnBeginFailureAsync () throws Exception {
299+ doReturn (ApiFutures .immediateFailedFuture (new Exception ("Expected exception" )))
300+ .when (firestoreMock )
301+ .sendRequest (requestCapture .capture (), Matchers .<UnaryCallable <Message , Message >>any ());
302+
303+ ApiFuture <String > transaction =
304+ firestoreMock .runAsyncTransaction (
305+ new Transaction .AsyncFunction <String >() {
306+ @ Override
307+ public ApiFuture <String > updateCallback (Transaction transaction ) {
308+ fail ();
309+ return null ;
310+ }
311+ },
312+ options );
313+
314+ try {
315+ transaction .get ();
316+ fail ();
317+ } catch (Exception e ) {
318+ assertTrue (e .getMessage ().endsWith ("Expected exception" ));
319+ }
320+
321+ List <Message > requests = requestCapture .getAllValues ();
322+ assertEquals (1 , requests .size ());
323+ }
324+
216325 @ Test
217326 public void limitsRetriesWithFailure () throws Exception {
218327 doReturn (beginResponse (ByteString .copyFromUtf8 ("foo1" )))
@@ -343,6 +452,40 @@ public DocumentSnapshot updateCallback(Transaction transaction)
343452 assertEquals (commit (TRANSACTION_ID ), requests .get (2 ));
344453 }
345454
455+ @ Test
456+ public void getDocumentAsync () throws Exception {
457+ doReturn (beginResponse ())
458+ .doReturn (commitResponse (0 , 0 ))
459+ .when (firestoreMock )
460+ .sendRequest (requestCapture .capture (), Matchers .<UnaryCallable <Message , Message >>any ());
461+
462+ doAnswer (getAllResponse (SINGLE_FIELD_PROTO ))
463+ .when (firestoreMock )
464+ .streamRequest (
465+ requestCapture .capture (),
466+ streamObserverCapture .capture (),
467+ Matchers .<ServerStreamingCallable <Message , Message >>any ());
468+
469+ ApiFuture <DocumentSnapshot > transaction =
470+ firestoreMock .runAsyncTransaction (
471+ new Transaction .AsyncFunction <DocumentSnapshot >() {
472+ @ Override
473+ public ApiFuture <DocumentSnapshot > updateCallback (Transaction transaction ) {
474+ return transaction .get (documentReference );
475+ }
476+ },
477+ options );
478+
479+ assertEquals ("doc" , transaction .get ().getId ());
480+
481+ List <Message > requests = requestCapture .getAllValues ();
482+ assertEquals (3 , requests .size ());
483+
484+ assertEquals (begin (), requests .get (0 ));
485+ assertEquals (get (TRANSACTION_ID ), requests .get (1 ));
486+ assertEquals (commit (TRANSACTION_ID ), requests .get (2 ));
487+ }
488+
346489 @ Test
347490 public void getMultipleDocuments () throws Exception {
348491 final DocumentReference doc1 = firestoreMock .document ("coll/doc1" );
0 commit comments