/
FirestoreClientTest.java
736 lines (619 loc) · 28.9 KB
/
FirestoreClientTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.firestore.v1;
import static com.google.cloud.firestore.v1.FirestoreClient.ListCollectionIdsPagedResponse;
import static com.google.cloud.firestore.v1.FirestoreClient.ListDocumentsPagedResponse;
import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.grpc.GaxGrpcProperties;
import com.google.api.gax.grpc.testing.LocalChannelProvider;
import com.google.api.gax.grpc.testing.MockGrpcService;
import com.google.api.gax.grpc.testing.MockServiceHelper;
import com.google.api.gax.grpc.testing.MockStreamObserver;
import com.google.api.gax.rpc.ApiClientHeaderProvider;
import com.google.api.gax.rpc.ApiStreamObserver;
import com.google.api.gax.rpc.BidiStreamingCallable;
import com.google.api.gax.rpc.InvalidArgumentException;
import com.google.api.gax.rpc.ServerStreamingCallable;
import com.google.api.gax.rpc.StatusCode;
import com.google.common.collect.Lists;
import com.google.firestore.v1.AnyPathName;
import com.google.firestore.v1.BatchGetDocumentsRequest;
import com.google.firestore.v1.BatchGetDocumentsResponse;
import com.google.firestore.v1.BeginTransactionRequest;
import com.google.firestore.v1.BeginTransactionResponse;
import com.google.firestore.v1.CommitRequest;
import com.google.firestore.v1.CommitResponse;
import com.google.firestore.v1.CreateDocumentRequest;
import com.google.firestore.v1.DatabaseRootName;
import com.google.firestore.v1.DeleteDocumentRequest;
import com.google.firestore.v1.Document;
import com.google.firestore.v1.DocumentMask;
import com.google.firestore.v1.GetDocumentRequest;
import com.google.firestore.v1.ListCollectionIdsRequest;
import com.google.firestore.v1.ListCollectionIdsResponse;
import com.google.firestore.v1.ListDocumentsRequest;
import com.google.firestore.v1.ListDocumentsResponse;
import com.google.firestore.v1.ListenRequest;
import com.google.firestore.v1.ListenResponse;
import com.google.firestore.v1.RollbackRequest;
import com.google.firestore.v1.RunQueryRequest;
import com.google.firestore.v1.RunQueryResponse;
import com.google.firestore.v1.UpdateDocumentRequest;
import com.google.firestore.v1.Write;
import com.google.firestore.v1.WriteRequest;
import com.google.firestore.v1.WriteResponse;
import com.google.protobuf.AbstractMessage;
import com.google.protobuf.ByteString;
import com.google.protobuf.Empty;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
@javax.annotation.Generated("by GAPIC")
public class FirestoreClientTest {
private static MockFirestore mockFirestore;
private static MockServiceHelper serviceHelper;
private FirestoreClient client;
private LocalChannelProvider channelProvider;
@BeforeClass
public static void startStaticServer() {
mockFirestore = new MockFirestore();
serviceHelper =
new MockServiceHelper(
UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockFirestore));
serviceHelper.start();
}
@AfterClass
public static void stopServer() {
serviceHelper.stop();
}
@Before
public void setUp() throws IOException {
serviceHelper.reset();
channelProvider = serviceHelper.createChannelProvider();
FirestoreSettings settings =
FirestoreSettings.newBuilder()
.setTransportChannelProvider(channelProvider)
.setCredentialsProvider(NoCredentialsProvider.create())
.build();
client = FirestoreClient.create(settings);
}
@After
public void tearDown() throws Exception {
client.close();
}
@Test
@SuppressWarnings("all")
public void getDocumentTest() {
String name2 = "name2-1052831874";
Document expectedResponse = Document.newBuilder().setName(name2).build();
mockFirestore.addResponse(expectedResponse);
AnyPathName name = AnyPathName.of("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
GetDocumentRequest request = GetDocumentRequest.newBuilder().setName(name.toString()).build();
Document actualResponse = client.getDocument(request);
Assert.assertEquals(expectedResponse, actualResponse);
List<AbstractMessage> actualRequests = mockFirestore.getRequests();
Assert.assertEquals(1, actualRequests.size());
GetDocumentRequest actualRequest = (GetDocumentRequest) actualRequests.get(0);
Assert.assertEquals(name, AnyPathName.parse(actualRequest.getName()));
Assert.assertTrue(
channelProvider.isHeaderSent(
ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
@Test
@SuppressWarnings("all")
public void getDocumentExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
mockFirestore.addException(exception);
try {
AnyPathName name = AnyPathName.of("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
GetDocumentRequest request = GetDocumentRequest.newBuilder().setName(name.toString()).build();
client.getDocument(request);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception
}
}
@Test
@SuppressWarnings("all")
public void listDocumentsTest() {
String nextPageToken = "";
Document documentsElement = Document.newBuilder().build();
List<Document> documents = Arrays.asList(documentsElement);
ListDocumentsResponse expectedResponse =
ListDocumentsResponse.newBuilder()
.setNextPageToken(nextPageToken)
.addAllDocuments(documents)
.build();
mockFirestore.addResponse(expectedResponse);
String formattedParent =
AnyPathName.format("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
String collectionId = "collectionId-821242276";
ListDocumentsRequest request =
ListDocumentsRequest.newBuilder()
.setParent(formattedParent)
.setCollectionId(collectionId)
.build();
ListDocumentsPagedResponse pagedListResponse = client.listDocuments(request);
List<Document> resources = Lists.newArrayList(pagedListResponse.iterateAll());
Assert.assertEquals(1, resources.size());
Assert.assertEquals(expectedResponse.getDocumentsList().get(0), resources.get(0));
List<AbstractMessage> actualRequests = mockFirestore.getRequests();
Assert.assertEquals(1, actualRequests.size());
ListDocumentsRequest actualRequest = (ListDocumentsRequest) actualRequests.get(0);
Assert.assertEquals(formattedParent, actualRequest.getParent());
Assert.assertEquals(collectionId, actualRequest.getCollectionId());
Assert.assertTrue(
channelProvider.isHeaderSent(
ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
@Test
@SuppressWarnings("all")
public void listDocumentsExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
mockFirestore.addException(exception);
try {
String formattedParent =
AnyPathName.format("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
String collectionId = "collectionId-821242276";
ListDocumentsRequest request =
ListDocumentsRequest.newBuilder()
.setParent(formattedParent)
.setCollectionId(collectionId)
.build();
client.listDocuments(request);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception
}
}
@Test
@SuppressWarnings("all")
public void createDocumentTest() {
String name = "name3373707";
Document expectedResponse = Document.newBuilder().setName(name).build();
mockFirestore.addResponse(expectedResponse);
String formattedParent =
AnyPathName.format("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
String collectionId = "collectionId-821242276";
String documentId = "documentId506676927";
Document document = Document.newBuilder().build();
CreateDocumentRequest request =
CreateDocumentRequest.newBuilder()
.setParent(formattedParent)
.setCollectionId(collectionId)
.setDocumentId(documentId)
.setDocument(document)
.build();
Document actualResponse = client.createDocument(request);
Assert.assertEquals(expectedResponse, actualResponse);
List<AbstractMessage> actualRequests = mockFirestore.getRequests();
Assert.assertEquals(1, actualRequests.size());
CreateDocumentRequest actualRequest = (CreateDocumentRequest) actualRequests.get(0);
Assert.assertEquals(formattedParent, actualRequest.getParent());
Assert.assertEquals(collectionId, actualRequest.getCollectionId());
Assert.assertEquals(documentId, actualRequest.getDocumentId());
Assert.assertEquals(document, actualRequest.getDocument());
Assert.assertTrue(
channelProvider.isHeaderSent(
ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
@Test
@SuppressWarnings("all")
public void createDocumentExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
mockFirestore.addException(exception);
try {
String formattedParent =
AnyPathName.format("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
String collectionId = "collectionId-821242276";
String documentId = "documentId506676927";
Document document = Document.newBuilder().build();
CreateDocumentRequest request =
CreateDocumentRequest.newBuilder()
.setParent(formattedParent)
.setCollectionId(collectionId)
.setDocumentId(documentId)
.setDocument(document)
.build();
client.createDocument(request);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception
}
}
@Test
@SuppressWarnings("all")
public void updateDocumentTest() {
String name = "name3373707";
Document expectedResponse = Document.newBuilder().setName(name).build();
mockFirestore.addResponse(expectedResponse);
Document document = Document.newBuilder().build();
DocumentMask updateMask = DocumentMask.newBuilder().build();
Document actualResponse = client.updateDocument(document, updateMask);
Assert.assertEquals(expectedResponse, actualResponse);
List<AbstractMessage> actualRequests = mockFirestore.getRequests();
Assert.assertEquals(1, actualRequests.size());
UpdateDocumentRequest actualRequest = (UpdateDocumentRequest) actualRequests.get(0);
Assert.assertEquals(document, actualRequest.getDocument());
Assert.assertEquals(updateMask, actualRequest.getUpdateMask());
Assert.assertTrue(
channelProvider.isHeaderSent(
ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
@Test
@SuppressWarnings("all")
public void updateDocumentExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
mockFirestore.addException(exception);
try {
Document document = Document.newBuilder().build();
DocumentMask updateMask = DocumentMask.newBuilder().build();
client.updateDocument(document, updateMask);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception
}
}
@Test
@SuppressWarnings("all")
public void deleteDocumentTest() {
Empty expectedResponse = Empty.newBuilder().build();
mockFirestore.addResponse(expectedResponse);
AnyPathName name = AnyPathName.of("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
client.deleteDocument(name);
List<AbstractMessage> actualRequests = mockFirestore.getRequests();
Assert.assertEquals(1, actualRequests.size());
DeleteDocumentRequest actualRequest = (DeleteDocumentRequest) actualRequests.get(0);
Assert.assertEquals(name, AnyPathName.parse(actualRequest.getName()));
Assert.assertTrue(
channelProvider.isHeaderSent(
ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
@Test
@SuppressWarnings("all")
public void deleteDocumentExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
mockFirestore.addException(exception);
try {
AnyPathName name = AnyPathName.of("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
client.deleteDocument(name);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception
}
}
@Test
@SuppressWarnings("all")
public void batchGetDocumentsTest() throws Exception {
String missing = "missing1069449574";
ByteString transaction = ByteString.copyFromUtf8("-34");
BatchGetDocumentsResponse expectedResponse =
BatchGetDocumentsResponse.newBuilder()
.setMissing(missing)
.setTransaction(transaction)
.build();
mockFirestore.addResponse(expectedResponse);
String formattedDatabase = DatabaseRootName.format("[PROJECT]", "[DATABASE]");
List<String> documents = new ArrayList<>();
BatchGetDocumentsRequest request =
BatchGetDocumentsRequest.newBuilder()
.setDatabase(formattedDatabase)
.addAllDocuments(documents)
.build();
MockStreamObserver<BatchGetDocumentsResponse> responseObserver = new MockStreamObserver<>();
ServerStreamingCallable<BatchGetDocumentsRequest, BatchGetDocumentsResponse> callable =
client.batchGetDocumentsCallable();
callable.serverStreamingCall(request, responseObserver);
List<BatchGetDocumentsResponse> actualResponses = responseObserver.future().get();
Assert.assertEquals(1, actualResponses.size());
Assert.assertEquals(expectedResponse, actualResponses.get(0));
}
@Test
@SuppressWarnings("all")
public void batchGetDocumentsExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
mockFirestore.addException(exception);
String formattedDatabase = DatabaseRootName.format("[PROJECT]", "[DATABASE]");
List<String> documents = new ArrayList<>();
BatchGetDocumentsRequest request =
BatchGetDocumentsRequest.newBuilder()
.setDatabase(formattedDatabase)
.addAllDocuments(documents)
.build();
MockStreamObserver<BatchGetDocumentsResponse> responseObserver = new MockStreamObserver<>();
ServerStreamingCallable<BatchGetDocumentsRequest, BatchGetDocumentsResponse> callable =
client.batchGetDocumentsCallable();
callable.serverStreamingCall(request, responseObserver);
try {
List<BatchGetDocumentsResponse> actualResponses = responseObserver.future().get();
Assert.fail("No exception thrown");
} catch (ExecutionException e) {
Assert.assertTrue(e.getCause() instanceof InvalidArgumentException);
InvalidArgumentException apiException = (InvalidArgumentException) e.getCause();
Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
}
}
@Test
@SuppressWarnings("all")
public void beginTransactionTest() {
ByteString transaction = ByteString.copyFromUtf8("-34");
BeginTransactionResponse expectedResponse =
BeginTransactionResponse.newBuilder().setTransaction(transaction).build();
mockFirestore.addResponse(expectedResponse);
String formattedDatabase = DatabaseRootName.format("[PROJECT]", "[DATABASE]");
BeginTransactionResponse actualResponse = client.beginTransaction(formattedDatabase);
Assert.assertEquals(expectedResponse, actualResponse);
List<AbstractMessage> actualRequests = mockFirestore.getRequests();
Assert.assertEquals(1, actualRequests.size());
BeginTransactionRequest actualRequest = (BeginTransactionRequest) actualRequests.get(0);
Assert.assertEquals(formattedDatabase, actualRequest.getDatabase());
Assert.assertTrue(
channelProvider.isHeaderSent(
ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
@Test
@SuppressWarnings("all")
public void beginTransactionExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
mockFirestore.addException(exception);
try {
String formattedDatabase = DatabaseRootName.format("[PROJECT]", "[DATABASE]");
client.beginTransaction(formattedDatabase);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception
}
}
@Test
@SuppressWarnings("all")
public void commitTest() {
CommitResponse expectedResponse = CommitResponse.newBuilder().build();
mockFirestore.addResponse(expectedResponse);
String formattedDatabase = DatabaseRootName.format("[PROJECT]", "[DATABASE]");
List<Write> writes = new ArrayList<>();
CommitResponse actualResponse = client.commit(formattedDatabase, writes);
Assert.assertEquals(expectedResponse, actualResponse);
List<AbstractMessage> actualRequests = mockFirestore.getRequests();
Assert.assertEquals(1, actualRequests.size());
CommitRequest actualRequest = (CommitRequest) actualRequests.get(0);
Assert.assertEquals(formattedDatabase, actualRequest.getDatabase());
Assert.assertEquals(writes, actualRequest.getWritesList());
Assert.assertTrue(
channelProvider.isHeaderSent(
ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
@Test
@SuppressWarnings("all")
public void commitExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
mockFirestore.addException(exception);
try {
String formattedDatabase = DatabaseRootName.format("[PROJECT]", "[DATABASE]");
List<Write> writes = new ArrayList<>();
client.commit(formattedDatabase, writes);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception
}
}
@Test
@SuppressWarnings("all")
public void rollbackTest() {
Empty expectedResponse = Empty.newBuilder().build();
mockFirestore.addResponse(expectedResponse);
String formattedDatabase = DatabaseRootName.format("[PROJECT]", "[DATABASE]");
ByteString transaction = ByteString.copyFromUtf8("-34");
client.rollback(formattedDatabase, transaction);
List<AbstractMessage> actualRequests = mockFirestore.getRequests();
Assert.assertEquals(1, actualRequests.size());
RollbackRequest actualRequest = (RollbackRequest) actualRequests.get(0);
Assert.assertEquals(formattedDatabase, actualRequest.getDatabase());
Assert.assertEquals(transaction, actualRequest.getTransaction());
Assert.assertTrue(
channelProvider.isHeaderSent(
ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
@Test
@SuppressWarnings("all")
public void rollbackExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
mockFirestore.addException(exception);
try {
String formattedDatabase = DatabaseRootName.format("[PROJECT]", "[DATABASE]");
ByteString transaction = ByteString.copyFromUtf8("-34");
client.rollback(formattedDatabase, transaction);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception
}
}
@Test
@SuppressWarnings("all")
public void runQueryTest() throws Exception {
ByteString transaction = ByteString.copyFromUtf8("-34");
int skippedResults = 880286183;
RunQueryResponse expectedResponse =
RunQueryResponse.newBuilder()
.setTransaction(transaction)
.setSkippedResults(skippedResults)
.build();
mockFirestore.addResponse(expectedResponse);
String formattedParent =
AnyPathName.format("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
RunQueryRequest request = RunQueryRequest.newBuilder().setParent(formattedParent).build();
MockStreamObserver<RunQueryResponse> responseObserver = new MockStreamObserver<>();
ServerStreamingCallable<RunQueryRequest, RunQueryResponse> callable = client.runQueryCallable();
callable.serverStreamingCall(request, responseObserver);
List<RunQueryResponse> actualResponses = responseObserver.future().get();
Assert.assertEquals(1, actualResponses.size());
Assert.assertEquals(expectedResponse, actualResponses.get(0));
}
@Test
@SuppressWarnings("all")
public void runQueryExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
mockFirestore.addException(exception);
String formattedParent =
AnyPathName.format("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
RunQueryRequest request = RunQueryRequest.newBuilder().setParent(formattedParent).build();
MockStreamObserver<RunQueryResponse> responseObserver = new MockStreamObserver<>();
ServerStreamingCallable<RunQueryRequest, RunQueryResponse> callable = client.runQueryCallable();
callable.serverStreamingCall(request, responseObserver);
try {
List<RunQueryResponse> actualResponses = responseObserver.future().get();
Assert.fail("No exception thrown");
} catch (ExecutionException e) {
Assert.assertTrue(e.getCause() instanceof InvalidArgumentException);
InvalidArgumentException apiException = (InvalidArgumentException) e.getCause();
Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
}
}
@Test
@SuppressWarnings("all")
public void writeTest() throws Exception {
String streamId = "streamId-315624902";
ByteString streamToken = ByteString.copyFromUtf8("122");
WriteResponse expectedResponse =
WriteResponse.newBuilder().setStreamId(streamId).setStreamToken(streamToken).build();
mockFirestore.addResponse(expectedResponse);
String formattedDatabase = DatabaseRootName.format("[PROJECT]", "[DATABASE]");
WriteRequest request = WriteRequest.newBuilder().setDatabase(formattedDatabase).build();
MockStreamObserver<WriteResponse> responseObserver = new MockStreamObserver<>();
BidiStreamingCallable<WriteRequest, WriteResponse> callable = client.writeCallable();
ApiStreamObserver<WriteRequest> requestObserver = callable.bidiStreamingCall(responseObserver);
requestObserver.onNext(request);
requestObserver.onCompleted();
List<WriteResponse> actualResponses = responseObserver.future().get();
Assert.assertEquals(1, actualResponses.size());
Assert.assertEquals(expectedResponse, actualResponses.get(0));
}
@Test
@SuppressWarnings("all")
public void writeExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
mockFirestore.addException(exception);
String formattedDatabase = DatabaseRootName.format("[PROJECT]", "[DATABASE]");
WriteRequest request = WriteRequest.newBuilder().setDatabase(formattedDatabase).build();
MockStreamObserver<WriteResponse> responseObserver = new MockStreamObserver<>();
BidiStreamingCallable<WriteRequest, WriteResponse> callable = client.writeCallable();
ApiStreamObserver<WriteRequest> requestObserver = callable.bidiStreamingCall(responseObserver);
requestObserver.onNext(request);
try {
List<WriteResponse> actualResponses = responseObserver.future().get();
Assert.fail("No exception thrown");
} catch (ExecutionException e) {
Assert.assertTrue(e.getCause() instanceof InvalidArgumentException);
InvalidArgumentException apiException = (InvalidArgumentException) e.getCause();
Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
}
}
@Test
@SuppressWarnings("all")
public void listenTest() throws Exception {
ListenResponse expectedResponse = ListenResponse.newBuilder().build();
mockFirestore.addResponse(expectedResponse);
String formattedDatabase = DatabaseRootName.format("[PROJECT]", "[DATABASE]");
ListenRequest request = ListenRequest.newBuilder().setDatabase(formattedDatabase).build();
MockStreamObserver<ListenResponse> responseObserver = new MockStreamObserver<>();
BidiStreamingCallable<ListenRequest, ListenResponse> callable = client.listenCallable();
ApiStreamObserver<ListenRequest> requestObserver = callable.bidiStreamingCall(responseObserver);
requestObserver.onNext(request);
requestObserver.onCompleted();
List<ListenResponse> actualResponses = responseObserver.future().get();
Assert.assertEquals(1, actualResponses.size());
Assert.assertEquals(expectedResponse, actualResponses.get(0));
}
@Test
@SuppressWarnings("all")
public void listenExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
mockFirestore.addException(exception);
String formattedDatabase = DatabaseRootName.format("[PROJECT]", "[DATABASE]");
ListenRequest request = ListenRequest.newBuilder().setDatabase(formattedDatabase).build();
MockStreamObserver<ListenResponse> responseObserver = new MockStreamObserver<>();
BidiStreamingCallable<ListenRequest, ListenResponse> callable = client.listenCallable();
ApiStreamObserver<ListenRequest> requestObserver = callable.bidiStreamingCall(responseObserver);
requestObserver.onNext(request);
try {
List<ListenResponse> actualResponses = responseObserver.future().get();
Assert.fail("No exception thrown");
} catch (ExecutionException e) {
Assert.assertTrue(e.getCause() instanceof InvalidArgumentException);
InvalidArgumentException apiException = (InvalidArgumentException) e.getCause();
Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
}
}
@Test
@SuppressWarnings("all")
public void listCollectionIdsTest() {
String nextPageToken = "";
String collectionIdsElement = "collectionIdsElement1368994900";
List<String> collectionIds = Arrays.asList(collectionIdsElement);
ListCollectionIdsResponse expectedResponse =
ListCollectionIdsResponse.newBuilder()
.setNextPageToken(nextPageToken)
.addAllCollectionIds(collectionIds)
.build();
mockFirestore.addResponse(expectedResponse);
String formattedParent =
AnyPathName.format("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
ListCollectionIdsPagedResponse pagedListResponse = client.listCollectionIds(formattedParent);
List<String> resources = Lists.newArrayList(pagedListResponse.iterateAll());
Assert.assertEquals(1, resources.size());
Assert.assertEquals(expectedResponse.getCollectionIdsList().get(0), resources.get(0));
List<AbstractMessage> actualRequests = mockFirestore.getRequests();
Assert.assertEquals(1, actualRequests.size());
ListCollectionIdsRequest actualRequest = (ListCollectionIdsRequest) actualRequests.get(0);
Assert.assertEquals(formattedParent, actualRequest.getParent());
Assert.assertTrue(
channelProvider.isHeaderSent(
ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
@Test
@SuppressWarnings("all")
public void listCollectionIdsExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
mockFirestore.addException(exception);
try {
String formattedParent =
AnyPathName.format("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
client.listCollectionIds(formattedParent);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception
}
}
}