/
LoggingImplTest.java
2186 lines (2065 loc) · 98.2 KB
/
LoggingImplTest.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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2016 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
*
* http://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.logging;
import static com.google.cloud.logging.SinkInfo.VersionFormat;
import static com.google.protobuf.util.Timestamps.fromMillis;
import static org.easymock.EasyMock.replay;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.api.core.SettableApiFuture;
import com.google.api.gax.paging.AsyncPage;
import com.google.api.gax.paging.Page;
import com.google.cloud.MonitoredResource;
import com.google.cloud.MonitoredResourceDescriptor;
import com.google.cloud.ServiceOptions;
import com.google.cloud.logging.Logging.EntryListOption;
import com.google.cloud.logging.Logging.ListOption;
import com.google.cloud.logging.Logging.SortingField;
import com.google.cloud.logging.Logging.WriteOption;
import com.google.cloud.logging.Payload.StringPayload;
import com.google.cloud.logging.SinkInfo.Destination;
import com.google.cloud.logging.spi.LoggingRpcFactory;
import com.google.cloud.logging.spi.v2.LoggingRpc;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.logging.v2.CreateExclusionRequest;
import com.google.logging.v2.CreateLogMetricRequest;
import com.google.logging.v2.CreateSinkRequest;
import com.google.logging.v2.DeleteExclusionRequest;
import com.google.logging.v2.DeleteLogMetricRequest;
import com.google.logging.v2.DeleteLogRequest;
import com.google.logging.v2.DeleteSinkRequest;
import com.google.logging.v2.GetExclusionRequest;
import com.google.logging.v2.GetLogMetricRequest;
import com.google.logging.v2.GetSinkRequest;
import com.google.logging.v2.ListExclusionsRequest;
import com.google.logging.v2.ListExclusionsResponse;
import com.google.logging.v2.ListLogEntriesRequest;
import com.google.logging.v2.ListLogEntriesResponse;
import com.google.logging.v2.ListLogMetricsRequest;
import com.google.logging.v2.ListLogMetricsResponse;
import com.google.logging.v2.ListLogsRequest;
import com.google.logging.v2.ListLogsResponse;
import com.google.logging.v2.ListMonitoredResourceDescriptorsRequest;
import com.google.logging.v2.ListMonitoredResourceDescriptorsResponse;
import com.google.logging.v2.ListSinksRequest;
import com.google.logging.v2.ListSinksResponse;
import com.google.logging.v2.LogExclusion;
import com.google.logging.v2.LogMetric;
import com.google.logging.v2.LogSink;
import com.google.logging.v2.UpdateExclusionRequest;
import com.google.logging.v2.UpdateLogMetricRequest;
import com.google.logging.v2.UpdateSinkRequest;
import com.google.logging.v2.WriteLogEntriesRequest;
import com.google.logging.v2.WriteLogEntriesResponse;
import com.google.protobuf.Empty;
import com.google.protobuf.Timestamp;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import org.easymock.EasyMock;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class LoggingImplTest {
private static final String PROJECT = "project";
private static final String PROJECT_PB = "projects/" + PROJECT;
private static final String SINK_NAME = "sink";
private static final SinkInfo SINK_INFO =
SinkInfo.newBuilder(SINK_NAME, Destination.BucketDestination.of("bucket"))
.setVersionFormat(VersionFormat.V2)
.build();
private static final String SINK_NAME_PB = "projects/" + PROJECT + "/sinks/" + SINK_NAME;
private static final String METRIC_NAME = "metric";
private static final String METRIC_NAME_PB = "projects/" + PROJECT + "/metrics/" + METRIC_NAME;
private static final String FILTER = "logName=projects/my-projectid/logs/syslog";
private static final String DESCRIPTION = "description";
private static final MetricInfo METRIC_INFO =
MetricInfo.newBuilder(METRIC_NAME, FILTER).setDescription(DESCRIPTION).build();
private static final com.google.api.MonitoredResourceDescriptor DESCRIPTOR_PB =
com.google.api.MonitoredResourceDescriptor.getDefaultInstance();
private static final MonitoredResourceDescriptor DESCRIPTOR =
MonitoredResourceDescriptor.fromPb(DESCRIPTOR_PB);
private static final String LOG_NAME1 = "test-list-log-name-1";
private static final String LOG_NAME2 = "test-list-log-name-2";
private static final String LOG_NAMES_CURSOR = "cursor";
private static final String LOG_NAME = "log";
private static final String LOG_NAME_PB = "projects/" + PROJECT + "/logs/" + LOG_NAME;
private static final MonitoredResource MONITORED_RESOURCE =
MonitoredResource.newBuilder("global").addLabel("project-id", PROJECT).build();
private static final LogEntry LOG_ENTRY1 =
LogEntry.newBuilder(StringPayload.of("entry1"))
.setLogName(LOG_NAME)
.setDestination(LogDestinationName.project(PROJECT))
.setResource(MONITORED_RESOURCE)
.build();
private static final LogEntry LOG_ENTRY2 =
LogEntry.newBuilder(StringPayload.of("entry2"))
.setLogName(LOG_NAME)
.setDestination(LogDestinationName.project(PROJECT))
.setResource(MONITORED_RESOURCE)
.build();
private static final Function<SinkInfo, LogSink> SINK_TO_PB_FUNCTION =
new Function<SinkInfo, LogSink>() {
@Override
public LogSink apply(SinkInfo sinkInfo) {
return sinkInfo.toPb(PROJECT);
}
};
private static final Function<MetricInfo, LogMetric> METRIC_TO_PB_FUNCTION =
new Function<MetricInfo, LogMetric>() {
@Override
public LogMetric apply(MetricInfo metricInfo) {
return metricInfo.toPb();
}
};
private static final Function<
MonitoredResourceDescriptor, com.google.api.MonitoredResourceDescriptor>
DESCRIPTOR_TO_PB_FUNCTION =
new Function<MonitoredResourceDescriptor, com.google.api.MonitoredResourceDescriptor>() {
@Override
public com.google.api.MonitoredResourceDescriptor apply(
MonitoredResourceDescriptor descriptor) {
return descriptor.toPb();
}
};
private static final String EXCLUSION_NAME = "load-balancer-exclusion";
private static final String EXCLUSION_NAME_PB =
"projects/" + PROJECT + "/exclusions/" + EXCLUSION_NAME;
private static final String EXCLUSION_FILTER =
"resource.type=gcs_bucket severity<ERROR sample(insertId, 0.99)";
private static final String CURSOR = "cursor";
private static final String NEXT_CURSOR = "nextCursor";
private static final Boolean DISABLED = Boolean.FALSE;
private static final Timestamp EXCLUSION_CREATED_TIME = fromMillis(System.currentTimeMillis());
private static final Timestamp EXCLUSION_UPDATED_TIME = fromMillis(System.currentTimeMillis());
private static final Exclusion EXCLUSION =
Exclusion.newBuilder(EXCLUSION_NAME, EXCLUSION_FILTER)
.setDisabled(DISABLED)
.setDescription(DESCRIPTION)
.setCreateTime(EXCLUSION_CREATED_TIME)
.build();
private static final Exclusion EXCLUSION1 =
Exclusion.newBuilder(EXCLUSION_NAME, EXCLUSION_FILTER)
.setDisabled(DISABLED)
.setDescription(DESCRIPTION)
.setUpdateTime(EXCLUSION_UPDATED_TIME)
.build();
private LoggingOptions options;
private LoggingRpcFactory rpcFactoryMock;
private LoggingRpc loggingRpcMock;
private Logging logging;
private void configureListLogsTests(List<String> returnedList, String cursor) {
ListLogsRequest request = ListLogsRequest.newBuilder().setParent(PROJECT_PB).build();
ListLogsResponse response =
ListLogsResponse.newBuilder().setNextPageToken(cursor).addAllLogNames(returnedList).build();
ApiFuture<ListLogsResponse> futureResponse = ApiFutures.immediateFuture(response);
EasyMock.expect(loggingRpcMock.listLogs(request)).andReturn(futureResponse);
EasyMock.replay(loggingRpcMock);
}
private void configureListLogsTests(
List<String> page1ReturnedList,
List<String> page2ReturnedList,
String page1Cursor,
String page2Cursor) {
ListLogsRequest request1 = ListLogsRequest.newBuilder().setParent(PROJECT_PB).build();
ListLogsRequest request2 =
ListLogsRequest.newBuilder().setParent(PROJECT_PB).setPageToken(page1Cursor).build();
ListLogsResponse response1 =
ListLogsResponse.newBuilder()
.setNextPageToken(page1Cursor)
.addAllLogNames(page1ReturnedList)
.build();
ListLogsResponse response2 =
ListLogsResponse.newBuilder()
.setNextPageToken(page2Cursor)
.addAllLogNames(page2ReturnedList)
.build();
ApiFuture<ListLogsResponse> futureResponse1 = ApiFutures.immediateFuture(response1);
ApiFuture<ListLogsResponse> futureResponse2 = ApiFutures.immediateFuture(response2);
EasyMock.expect(loggingRpcMock.listLogs(request1)).andReturn(futureResponse1);
EasyMock.expect(loggingRpcMock.listLogs(request2)).andReturn(futureResponse2);
EasyMock.replay(loggingRpcMock);
}
@Before
public void setUp() {
rpcFactoryMock = EasyMock.createStrictMock(LoggingRpcFactory.class);
loggingRpcMock = EasyMock.createStrictMock(LoggingRpc.class);
EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(LoggingOptions.class)))
.andReturn(loggingRpcMock);
options =
LoggingOptions.newBuilder()
.setProjectId(PROJECT)
.setServiceRpcFactory(rpcFactoryMock)
.setRetrySettings(ServiceOptions.getNoRetrySettings())
.build();
// By default when calling ListLogEntries, we append a filter of last 24 hours.
// However when testing, the time when it was called by the test and by the
// method
// implementation might differ by microseconds so we use the same time filter
// implementation
// for test and in "real" method
LoggingImpl.defaultTimestampFilterCreator =
new ITimestampDefaultFilter() {
@Override
public String createDefaultTimestampFilter() {
return "timestamp>=\"2020-10-30T15:39:09Z\"";
}
};
}
@After
public void tearDown() {
EasyMock.verify(rpcFactoryMock, loggingRpcMock);
}
@Test
public void testGetOptions() {
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
assertSame(options, options.getService().getOptions());
}
@Test
public void testCreateSink() {
LogSink sinkPb = SINK_INFO.toPb(PROJECT);
ApiFuture<LogSink> response = ApiFutures.immediateFuture(sinkPb);
CreateSinkRequest request =
CreateSinkRequest.newBuilder().setParent(PROJECT_PB).setSink(sinkPb).build();
EasyMock.expect(loggingRpcMock.create(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
Sink sink = logging.create(SINK_INFO);
assertEquals(new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)), sink);
}
@Test
public void testCreateSinkAsync() throws ExecutionException, InterruptedException {
LogSink sinkPb = SINK_INFO.toPb(PROJECT);
ApiFuture<LogSink> response = ApiFutures.immediateFuture(sinkPb);
CreateSinkRequest request =
CreateSinkRequest.newBuilder().setParent(PROJECT_PB).setSink(sinkPb).build();
EasyMock.expect(loggingRpcMock.create(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
Sink sink = logging.createAsync(SINK_INFO).get();
assertEquals(new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)), sink);
}
@Test
public void testUpdateSink() {
LogSink sinkPb = SINK_INFO.toPb(PROJECT);
ApiFuture<LogSink> response = ApiFutures.immediateFuture(sinkPb);
UpdateSinkRequest request =
UpdateSinkRequest.newBuilder().setSinkName(SINK_NAME_PB).setSink(sinkPb).build();
EasyMock.expect(loggingRpcMock.update(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
Sink sink = logging.update(SINK_INFO);
assertEquals(new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)), sink);
}
@Test
public void testUpdateSinkAsync() throws ExecutionException, InterruptedException {
LogSink sinkPb = SINK_INFO.toPb(PROJECT);
ApiFuture<LogSink> response = ApiFutures.immediateFuture(sinkPb);
UpdateSinkRequest request =
UpdateSinkRequest.newBuilder().setSinkName(SINK_NAME_PB).setSink(sinkPb).build();
EasyMock.expect(loggingRpcMock.update(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
Sink sink = logging.updateAsync(SINK_INFO).get();
assertEquals(new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)), sink);
}
@Test
public void testGetSink() {
LogSink sinkPb = SINK_INFO.toPb(PROJECT);
ApiFuture<LogSink> response = ApiFutures.immediateFuture(sinkPb);
GetSinkRequest request = GetSinkRequest.newBuilder().setSinkName(SINK_NAME_PB).build();
EasyMock.expect(loggingRpcMock.get(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
Sink sink = logging.getSink(SINK_NAME);
assertEquals(new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)), sink);
}
@Test
public void testGetSink_Null() {
ApiFuture<LogSink> response = ApiFutures.immediateFuture(null);
GetSinkRequest request = GetSinkRequest.newBuilder().setSinkName(SINK_NAME_PB).build();
EasyMock.expect(loggingRpcMock.get(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
assertNull(logging.getSink(SINK_NAME));
}
@Test
public void testGetSinkAsync() throws ExecutionException, InterruptedException {
LogSink sinkPb = SINK_INFO.toPb(PROJECT);
ApiFuture<LogSink> response = ApiFutures.immediateFuture(sinkPb);
GetSinkRequest request = GetSinkRequest.newBuilder().setSinkName(SINK_NAME_PB).build();
EasyMock.expect(loggingRpcMock.get(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
Sink sink = logging.getSinkAsync(SINK_NAME).get();
assertEquals(new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)), sink);
}
@Test
public void testGetSinkAsync_Null() throws ExecutionException, InterruptedException {
ApiFuture<LogSink> response = ApiFutures.immediateFuture(null);
GetSinkRequest request = GetSinkRequest.newBuilder().setSinkName(SINK_NAME_PB).build();
EasyMock.expect(loggingRpcMock.get(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
assertNull(logging.getSinkAsync(SINK_NAME).get());
}
@Test
public void testDeleteSink() {
DeleteSinkRequest request = DeleteSinkRequest.newBuilder().setSinkName(SINK_NAME_PB).build();
ApiFuture<Empty> response = ApiFutures.immediateFuture(Empty.getDefaultInstance());
EasyMock.expect(loggingRpcMock.delete(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
assertTrue(logging.deleteSink(SINK_NAME));
}
@Test
public void testDeleteSink_Null() {
DeleteSinkRequest request = DeleteSinkRequest.newBuilder().setSinkName(SINK_NAME_PB).build();
ApiFuture<Empty> response = ApiFutures.immediateFuture(null);
EasyMock.expect(loggingRpcMock.delete(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
assertFalse(logging.deleteSink(SINK_NAME));
}
@Test
public void testDeleteSinkAsync() throws ExecutionException, InterruptedException {
DeleteSinkRequest request = DeleteSinkRequest.newBuilder().setSinkName(SINK_NAME_PB).build();
ApiFuture<Empty> response = ApiFutures.immediateFuture(Empty.getDefaultInstance());
EasyMock.expect(loggingRpcMock.delete(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
assertTrue(logging.deleteSinkAsync(SINK_NAME).get());
}
@Test
public void testDeleteSinkAsync_Null() throws ExecutionException, InterruptedException {
DeleteSinkRequest request = DeleteSinkRequest.newBuilder().setSinkName(SINK_NAME_PB).build();
ApiFuture<Empty> response = ApiFutures.immediateFuture(null);
EasyMock.expect(loggingRpcMock.delete(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
assertFalse(logging.deleteSinkAsync(SINK_NAME).get());
}
@Test
public void testListSinks() {
String cursor = "cursor";
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListSinksRequest request = ListSinksRequest.newBuilder().setParent(PROJECT_PB).build();
List<Sink> sinkList =
ImmutableList.of(
new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)),
new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)));
ListSinksResponse response =
ListSinksResponse.newBuilder()
.setNextPageToken(cursor)
.addAllSinks(Lists.transform(sinkList, SINK_TO_PB_FUNCTION))
.build();
ApiFuture<ListSinksResponse> futureResponse = ApiFutures.immediateFuture(response);
EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse);
EasyMock.replay(loggingRpcMock);
Page<Sink> page = logging.listSinks();
assertEquals(cursor, page.getNextPageToken());
assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Sink.class));
}
@Test
public void testListSinksNextPage() {
String cursor1 = "cursor";
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListSinksRequest request1 = ListSinksRequest.newBuilder().setParent(PROJECT_PB).build();
ListSinksRequest request2 =
ListSinksRequest.newBuilder().setParent(PROJECT_PB).setPageToken(cursor1).build();
List<Sink> sinkList1 =
ImmutableList.of(
new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)),
new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)));
List<Sink> sinkList2 = ImmutableList.of(new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)));
ListSinksResponse response1 =
ListSinksResponse.newBuilder()
.setNextPageToken(cursor1)
.addAllSinks(Lists.transform(sinkList1, SINK_TO_PB_FUNCTION))
.build();
String cursor2 = "nextCursor";
ListSinksResponse response2 =
ListSinksResponse.newBuilder()
.setNextPageToken(cursor2)
.addAllSinks(Lists.transform(sinkList2, SINK_TO_PB_FUNCTION))
.build();
ApiFuture<ListSinksResponse> futureResponse1 = ApiFutures.immediateFuture(response1);
ApiFuture<ListSinksResponse> futureResponse2 = ApiFutures.immediateFuture(response2);
EasyMock.expect(loggingRpcMock.list(request1)).andReturn(futureResponse1);
EasyMock.expect(loggingRpcMock.list(request2)).andReturn(futureResponse2);
EasyMock.replay(loggingRpcMock);
Page<Sink> page = logging.listSinks();
assertEquals(cursor1, page.getNextPageToken());
assertArrayEquals(sinkList1.toArray(), Iterables.toArray(page.getValues(), Sink.class));
page = page.getNextPage();
assertEquals(cursor2, page.getNextPageToken());
assertArrayEquals(sinkList2.toArray(), Iterables.toArray(page.getValues(), Sink.class));
}
@Test
public void testListSinksEmpty() {
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListSinksRequest request = ListSinksRequest.newBuilder().setParent(PROJECT_PB).build();
List<Sink> sinkList = ImmutableList.of();
ListSinksResponse response =
ListSinksResponse.newBuilder()
.setNextPageToken("")
.addAllSinks(Lists.transform(sinkList, SINK_TO_PB_FUNCTION))
.build();
ApiFuture<ListSinksResponse> futureResponse = ApiFutures.immediateFuture(response);
EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse);
EasyMock.replay(loggingRpcMock);
Page<Sink> page = logging.listSinks();
assertNull(page.getNextPageToken());
assertNull(page.getNextPage());
assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Sink.class));
}
@Test
public void testListSinksWithOptions() {
String cursor = "cursor";
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListSinksRequest request =
ListSinksRequest.newBuilder()
.setPageToken(cursor)
.setPageSize(42)
.setParent(PROJECT_PB)
.build();
List<Sink> sinkList =
ImmutableList.of(
new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)),
new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)));
ListSinksResponse response =
ListSinksResponse.newBuilder()
.setNextPageToken(cursor)
.addAllSinks(Lists.transform(sinkList, SINK_TO_PB_FUNCTION))
.build();
ApiFuture<ListSinksResponse> futureResponse = ApiFutures.immediateFuture(response);
EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse);
EasyMock.replay(loggingRpcMock);
Page<Sink> page = logging.listSinks(ListOption.pageSize(42), ListOption.pageToken(cursor));
assertEquals(cursor, page.getNextPageToken());
assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Sink.class));
}
@Test
public void testListSinksAsync() throws ExecutionException, InterruptedException {
String cursor = "cursor";
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListSinksRequest request = ListSinksRequest.newBuilder().setParent(PROJECT_PB).build();
List<Sink> sinkList =
ImmutableList.of(
new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)),
new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)));
ListSinksResponse response =
ListSinksResponse.newBuilder()
.setNextPageToken(cursor)
.addAllSinks(Lists.transform(sinkList, SINK_TO_PB_FUNCTION))
.build();
ApiFuture<ListSinksResponse> futureResponse = ApiFutures.immediateFuture(response);
EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse);
EasyMock.replay(loggingRpcMock);
AsyncPage<Sink> page = logging.listSinksAsync().get();
assertEquals(cursor, page.getNextPageToken());
assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Sink.class));
}
@Test
public void testListSinksAsyncNextPage() throws ExecutionException, InterruptedException {
String cursor1 = "cursor";
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListSinksRequest request1 = ListSinksRequest.newBuilder().setParent(PROJECT_PB).build();
ListSinksRequest request2 =
ListSinksRequest.newBuilder().setParent(PROJECT_PB).setPageToken(cursor1).build();
List<Sink> sinkList1 =
ImmutableList.of(
new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)),
new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)));
List<Sink> sinkList2 = ImmutableList.of(new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)));
ListSinksResponse response1 =
ListSinksResponse.newBuilder()
.setNextPageToken(cursor1)
.addAllSinks(Lists.transform(sinkList1, SINK_TO_PB_FUNCTION))
.build();
String cursor2 = "nextCursor";
ListSinksResponse response2 =
ListSinksResponse.newBuilder()
.setNextPageToken(cursor2)
.addAllSinks(Lists.transform(sinkList2, SINK_TO_PB_FUNCTION))
.build();
ApiFuture<ListSinksResponse> futureResponse1 = ApiFutures.immediateFuture(response1);
ApiFuture<ListSinksResponse> futureResponse2 = ApiFutures.immediateFuture(response2);
EasyMock.expect(loggingRpcMock.list(request1)).andReturn(futureResponse1);
EasyMock.expect(loggingRpcMock.list(request2)).andReturn(futureResponse2);
EasyMock.replay(loggingRpcMock);
AsyncPage<Sink> page = logging.listSinksAsync().get();
assertEquals(cursor1, page.getNextPageToken());
assertArrayEquals(sinkList1.toArray(), Iterables.toArray(page.getValues(), Sink.class));
page = page.getNextPageAsync().get();
assertEquals(cursor2, page.getNextPageToken());
assertArrayEquals(sinkList2.toArray(), Iterables.toArray(page.getValues(), Sink.class));
}
@Test
public void testListSinksAsyncEmpty() throws ExecutionException, InterruptedException {
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListSinksRequest request = ListSinksRequest.newBuilder().setParent(PROJECT_PB).build();
List<Sink> sinkList = ImmutableList.of();
ListSinksResponse response =
ListSinksResponse.newBuilder()
.setNextPageToken("")
.addAllSinks(Lists.transform(sinkList, SINK_TO_PB_FUNCTION))
.build();
ApiFuture<ListSinksResponse> futureResponse = ApiFutures.immediateFuture(response);
EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse);
EasyMock.replay(loggingRpcMock);
AsyncPage<Sink> page = logging.listSinksAsync().get();
assertNull(page.getNextPageToken());
assertNull(page.getNextPage());
assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Sink.class));
}
@Test
public void testListSinksWithOptionsAsync() throws ExecutionException, InterruptedException {
String cursor = "cursor";
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListSinksRequest request =
ListSinksRequest.newBuilder()
.setPageToken(cursor)
.setPageSize(42)
.setParent(PROJECT_PB)
.build();
List<Sink> sinkList =
ImmutableList.of(
new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)),
new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)));
ListSinksResponse response =
ListSinksResponse.newBuilder()
.setNextPageToken(cursor)
.addAllSinks(Lists.transform(sinkList, SINK_TO_PB_FUNCTION))
.build();
ApiFuture<ListSinksResponse> futureResponse = ApiFutures.immediateFuture(response);
EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse);
EasyMock.replay(loggingRpcMock);
AsyncPage<Sink> page =
logging.listSinksAsync(ListOption.pageSize(42), ListOption.pageToken(cursor)).get();
assertEquals(cursor, page.getNextPageToken());
assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Sink.class));
}
@Test
public void testCreateMetric() {
LogMetric metricPb = METRIC_INFO.toPb();
ApiFuture<LogMetric> response = ApiFutures.immediateFuture(metricPb);
CreateLogMetricRequest request =
CreateLogMetricRequest.newBuilder().setParent(PROJECT_PB).setMetric(metricPb).build();
EasyMock.expect(loggingRpcMock.create(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
Metric metric = logging.create(METRIC_INFO);
assertEquals(new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), metric);
}
@Test
public void testCreateMetricAsync() throws ExecutionException, InterruptedException {
LogMetric metricPb = METRIC_INFO.toPb();
ApiFuture<LogMetric> response = ApiFutures.immediateFuture(metricPb);
CreateLogMetricRequest request =
CreateLogMetricRequest.newBuilder().setParent(PROJECT_PB).setMetric(metricPb).build();
EasyMock.expect(loggingRpcMock.create(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
Metric metric = logging.createAsync(METRIC_INFO).get();
assertEquals(new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), metric);
}
@Test
public void testUpdateMetric() {
LogMetric sinkPb = METRIC_INFO.toPb();
ApiFuture<LogMetric> response = ApiFutures.immediateFuture(sinkPb);
UpdateLogMetricRequest request =
UpdateLogMetricRequest.newBuilder().setMetricName(METRIC_NAME_PB).setMetric(sinkPb).build();
EasyMock.expect(loggingRpcMock.update(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
Metric sink = logging.update(METRIC_INFO);
assertEquals(new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), sink);
}
@Test
public void testUpdateMetricAsync() throws ExecutionException, InterruptedException {
LogMetric sinkPb = METRIC_INFO.toPb();
ApiFuture<LogMetric> response = ApiFutures.immediateFuture(sinkPb);
UpdateLogMetricRequest request =
UpdateLogMetricRequest.newBuilder().setMetricName(METRIC_NAME_PB).setMetric(sinkPb).build();
EasyMock.expect(loggingRpcMock.update(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
Metric sink = logging.updateAsync(METRIC_INFO).get();
assertEquals(new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), sink);
}
@Test
public void testGetMetric() {
LogMetric sinkPb = METRIC_INFO.toPb();
ApiFuture<LogMetric> response = ApiFutures.immediateFuture(sinkPb);
GetLogMetricRequest request =
GetLogMetricRequest.newBuilder().setMetricName(METRIC_NAME_PB).build();
EasyMock.expect(loggingRpcMock.get(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
Metric sink = logging.getMetric(METRIC_NAME);
assertEquals(new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), sink);
}
@Test
public void testGetMetric_Null() {
ApiFuture<LogMetric> response = ApiFutures.immediateFuture(null);
GetLogMetricRequest request =
GetLogMetricRequest.newBuilder().setMetricName(METRIC_NAME_PB).build();
EasyMock.expect(loggingRpcMock.get(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
assertNull(logging.getMetric(METRIC_NAME));
}
@Test
public void testGetMetricAsync() throws ExecutionException, InterruptedException {
LogMetric sinkPb = METRIC_INFO.toPb();
ApiFuture<LogMetric> response = ApiFutures.immediateFuture(sinkPb);
GetLogMetricRequest request =
GetLogMetricRequest.newBuilder().setMetricName(METRIC_NAME_PB).build();
EasyMock.expect(loggingRpcMock.get(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
Metric sink = logging.getMetricAsync(METRIC_NAME).get();
assertEquals(new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), sink);
}
@Test
public void testGetMetricAsync_Null() throws ExecutionException, InterruptedException {
ApiFuture<LogMetric> response = ApiFutures.immediateFuture(null);
GetLogMetricRequest request =
GetLogMetricRequest.newBuilder().setMetricName(METRIC_NAME_PB).build();
EasyMock.expect(loggingRpcMock.get(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
assertNull(logging.getMetricAsync(METRIC_NAME).get());
}
@Test
public void testDeleteMetric() {
DeleteLogMetricRequest request =
DeleteLogMetricRequest.newBuilder().setMetricName(METRIC_NAME_PB).build();
ApiFuture<Empty> response = ApiFutures.immediateFuture(Empty.getDefaultInstance());
EasyMock.expect(loggingRpcMock.delete(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
assertTrue(logging.deleteMetric(METRIC_NAME));
}
@Test
public void testDeleteMetric_Null() {
DeleteLogMetricRequest request =
DeleteLogMetricRequest.newBuilder().setMetricName(METRIC_NAME_PB).build();
ApiFuture<Empty> response = ApiFutures.immediateFuture(null);
EasyMock.expect(loggingRpcMock.delete(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
assertFalse(logging.deleteMetric(METRIC_NAME));
}
@Test
public void testDeleteMetricAsync() throws ExecutionException, InterruptedException {
DeleteLogMetricRequest request =
DeleteLogMetricRequest.newBuilder().setMetricName(METRIC_NAME_PB).build();
ApiFuture<Empty> response = ApiFutures.immediateFuture(Empty.getDefaultInstance());
EasyMock.expect(loggingRpcMock.delete(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
assertTrue(logging.deleteMetricAsync(METRIC_NAME).get());
}
@Test
public void testDeleteMetricAsync_Null() throws ExecutionException, InterruptedException {
DeleteLogMetricRequest request =
DeleteLogMetricRequest.newBuilder().setMetricName(METRIC_NAME_PB).build();
ApiFuture<Empty> response = ApiFutures.immediateFuture(null);
EasyMock.expect(loggingRpcMock.delete(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
assertFalse(logging.deleteMetricAsync(METRIC_NAME).get());
}
@Test
public void testListMetrics() {
String cursor = "cursor";
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListLogMetricsRequest request =
ListLogMetricsRequest.newBuilder().setParent(PROJECT_PB).build();
List<Metric> sinkList =
ImmutableList.of(
new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)),
new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)));
ListLogMetricsResponse response =
ListLogMetricsResponse.newBuilder()
.setNextPageToken(cursor)
.addAllMetrics(Lists.transform(sinkList, METRIC_TO_PB_FUNCTION))
.build();
ApiFuture<ListLogMetricsResponse> futureResponse = ApiFutures.immediateFuture(response);
EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse);
EasyMock.replay(loggingRpcMock);
Page<Metric> page = logging.listMetrics();
assertEquals(cursor, page.getNextPageToken());
assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Metric.class));
}
@Test
public void testListMetricsNextPage() {
String cursor1 = "cursor";
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListLogMetricsRequest request1 =
ListLogMetricsRequest.newBuilder().setParent(PROJECT_PB).build();
ListLogMetricsRequest request2 =
ListLogMetricsRequest.newBuilder().setParent(PROJECT_PB).setPageToken(cursor1).build();
List<Metric> sinkList1 =
ImmutableList.of(
new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)),
new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)));
List<Metric> sinkList2 =
ImmutableList.of(new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)));
ListLogMetricsResponse response1 =
ListLogMetricsResponse.newBuilder()
.setNextPageToken(cursor1)
.addAllMetrics(Lists.transform(sinkList1, METRIC_TO_PB_FUNCTION))
.build();
String cursor2 = "nextCursor";
ListLogMetricsResponse response2 =
ListLogMetricsResponse.newBuilder()
.setNextPageToken(cursor2)
.addAllMetrics(Lists.transform(sinkList2, METRIC_TO_PB_FUNCTION))
.build();
ApiFuture<ListLogMetricsResponse> futureResponse1 = ApiFutures.immediateFuture(response1);
ApiFuture<ListLogMetricsResponse> futureResponse2 = ApiFutures.immediateFuture(response2);
EasyMock.expect(loggingRpcMock.list(request1)).andReturn(futureResponse1);
EasyMock.expect(loggingRpcMock.list(request2)).andReturn(futureResponse2);
EasyMock.replay(loggingRpcMock);
Page<Metric> page = logging.listMetrics();
assertEquals(cursor1, page.getNextPageToken());
assertArrayEquals(sinkList1.toArray(), Iterables.toArray(page.getValues(), Metric.class));
page = page.getNextPage();
assertEquals(cursor2, page.getNextPageToken());
assertArrayEquals(sinkList2.toArray(), Iterables.toArray(page.getValues(), Metric.class));
}
@Test
public void testListMetricsEmpty() {
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListLogMetricsRequest request =
ListLogMetricsRequest.newBuilder().setParent(PROJECT_PB).build();
List<Metric> sinkList = ImmutableList.of();
ListLogMetricsResponse response =
ListLogMetricsResponse.newBuilder()
.setNextPageToken("")
.addAllMetrics(Lists.transform(sinkList, METRIC_TO_PB_FUNCTION))
.build();
ApiFuture<ListLogMetricsResponse> futureResponse = ApiFutures.immediateFuture(response);
EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse);
EasyMock.replay(loggingRpcMock);
Page<Metric> page = logging.listMetrics();
assertNull(page.getNextPageToken());
assertNull(page.getNextPage());
assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Metric.class));
}
@Test
public void testListMetricsWithOptions() {
String cursor = "cursor";
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListLogMetricsRequest request =
ListLogMetricsRequest.newBuilder()
.setPageToken(cursor)
.setPageSize(42)
.setParent(PROJECT_PB)
.build();
List<Metric> sinkList =
ImmutableList.of(
new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)),
new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)));
ListLogMetricsResponse response =
ListLogMetricsResponse.newBuilder()
.setNextPageToken(cursor)
.addAllMetrics(Lists.transform(sinkList, METRIC_TO_PB_FUNCTION))
.build();
ApiFuture<ListLogMetricsResponse> futureResponse = ApiFutures.immediateFuture(response);
EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse);
EasyMock.replay(loggingRpcMock);
Page<Metric> page = logging.listMetrics(ListOption.pageSize(42), ListOption.pageToken(cursor));
assertEquals(cursor, page.getNextPageToken());
assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Metric.class));
}
@Test
public void testListMetricsAsync() throws ExecutionException, InterruptedException {
String cursor = "cursor";
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListLogMetricsRequest request =
ListLogMetricsRequest.newBuilder().setParent(PROJECT_PB).build();
List<Metric> sinkList =
ImmutableList.of(
new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)),
new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)));
ListLogMetricsResponse response =
ListLogMetricsResponse.newBuilder()
.setNextPageToken(cursor)
.addAllMetrics(Lists.transform(sinkList, METRIC_TO_PB_FUNCTION))
.build();
ApiFuture<ListLogMetricsResponse> futureResponse = ApiFutures.immediateFuture(response);
EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse);
EasyMock.replay(loggingRpcMock);
AsyncPage<Metric> page = logging.listMetricsAsync().get();
assertEquals(cursor, page.getNextPageToken());
assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Metric.class));
}
@Test
public void testListMetricsAsyncNextPage() throws ExecutionException, InterruptedException {
String cursor1 = "cursor";
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListLogMetricsRequest request1 =
ListLogMetricsRequest.newBuilder().setParent(PROJECT_PB).build();
ListLogMetricsRequest request2 =
ListLogMetricsRequest.newBuilder().setParent(PROJECT_PB).setPageToken(cursor1).build();
List<Metric> sinkList1 =
ImmutableList.of(
new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)),
new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)));
List<Metric> sinkList2 =
ImmutableList.of(new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)));
ListLogMetricsResponse response1 =
ListLogMetricsResponse.newBuilder()
.setNextPageToken(cursor1)
.addAllMetrics(Lists.transform(sinkList1, METRIC_TO_PB_FUNCTION))
.build();
String cursor2 = "nextCursor";
ListLogMetricsResponse response2 =
ListLogMetricsResponse.newBuilder()
.setNextPageToken(cursor2)
.addAllMetrics(Lists.transform(sinkList2, METRIC_TO_PB_FUNCTION))
.build();
ApiFuture<ListLogMetricsResponse> futureResponse1 = ApiFutures.immediateFuture(response1);
ApiFuture<ListLogMetricsResponse> futureResponse2 = ApiFutures.immediateFuture(response2);
EasyMock.expect(loggingRpcMock.list(request1)).andReturn(futureResponse1);
EasyMock.expect(loggingRpcMock.list(request2)).andReturn(futureResponse2);
EasyMock.replay(loggingRpcMock);
AsyncPage<Metric> page = logging.listMetricsAsync().get();
assertEquals(cursor1, page.getNextPageToken());
assertArrayEquals(sinkList1.toArray(), Iterables.toArray(page.getValues(), Metric.class));
page = page.getNextPageAsync().get();
assertEquals(cursor2, page.getNextPageToken());
assertArrayEquals(sinkList2.toArray(), Iterables.toArray(page.getValues(), Metric.class));
}
@Test
public void testListMetricsAsyncEmpty() throws ExecutionException, InterruptedException {
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListLogMetricsRequest request =
ListLogMetricsRequest.newBuilder().setParent(PROJECT_PB).build();
List<Metric> sinkList = ImmutableList.of();
ListLogMetricsResponse response =
ListLogMetricsResponse.newBuilder()
.setNextPageToken("")
.addAllMetrics(Lists.transform(sinkList, METRIC_TO_PB_FUNCTION))
.build();
ApiFuture<ListLogMetricsResponse> futureResponse = ApiFutures.immediateFuture(response);
EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse);
EasyMock.replay(loggingRpcMock);
AsyncPage<Metric> page = logging.listMetricsAsync().get();
assertNull(page.getNextPageToken());
assertNull(page.getNextPage());
assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Metric.class));
}
@Test
public void testListMetricsWithOptionsAsync() throws ExecutionException, InterruptedException {
String cursor = "cursor";
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListLogMetricsRequest request =
ListLogMetricsRequest.newBuilder()
.setPageToken(cursor)
.setPageSize(42)
.setParent(PROJECT_PB)
.build();
List<Metric> sinkList =
ImmutableList.of(
new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)),
new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)));
ListLogMetricsResponse response =
ListLogMetricsResponse.newBuilder()
.setNextPageToken(cursor)
.addAllMetrics(Lists.transform(sinkList, METRIC_TO_PB_FUNCTION))
.build();
ApiFuture<ListLogMetricsResponse> futureResponse = ApiFutures.immediateFuture(response);
EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse);
EasyMock.replay(loggingRpcMock);
AsyncPage<Metric> page =
logging.listMetricsAsync(ListOption.pageSize(42), ListOption.pageToken(cursor)).get();
assertEquals(cursor, page.getNextPageToken());
assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Metric.class));
}
@Test
public void testCreateExclusion() {
LogExclusion exclusionPb = EXCLUSION.toProtobuf();
ApiFuture<LogExclusion> response = ApiFutures.immediateFuture(exclusionPb);
CreateExclusionRequest request =
CreateExclusionRequest.newBuilder().setParent(PROJECT_PB).setExclusion(exclusionPb).build();
EasyMock.expect(loggingRpcMock.create(request)).andReturn(response);
EasyMock.replay(rpcFactoryMock, loggingRpcMock);
logging = options.getService();
Exclusion exclusion = logging.create(EXCLUSION);
assertEquals(EXCLUSION_NAME, exclusion.getName());
assertEquals(DESCRIPTION, exclusion.getDescription());
assertEquals(EXCLUSION_FILTER, exclusion.getFilter());
assertEquals(DISABLED, exclusion.isDisabled());
assertEquals(EXCLUSION_CREATED_TIME, exclusion.getCreateTime());
}
@Test
public void testCreateExclusionAsync() throws ExecutionException, InterruptedException {
LogExclusion exclusionPb = EXCLUSION.toProtobuf();
ApiFuture<LogExclusion> response = ApiFutures.immediateFuture(exclusionPb);
CreateExclusionRequest request =
CreateExclusionRequest.newBuilder().setParent(PROJECT_PB).setExclusion(exclusionPb).build();