forked from googleapis/google-cloud-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComputeImplTest.java
More file actions
3206 lines (2984 loc) · 150 KB
/
ComputeImplTest.java
File metadata and controls
3206 lines (2984 loc) · 150 KB
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 Inc. All Rights Reserved.
*
* 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.gcloud.compute;
import static com.google.gcloud.compute.spi.ComputeRpc.Option.FILTER;
import static com.google.gcloud.compute.spi.ComputeRpc.Option.MAX_RESULTS;
import static com.google.gcloud.compute.spi.ComputeRpc.Option.PAGE_TOKEN;
import static org.easymock.EasyMock.capture;
import static org.easymock.EasyMock.eq;
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.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.gcloud.Page;
import com.google.gcloud.RetryParams;
import com.google.gcloud.compute.Compute.AddressAggregatedListOption;
import com.google.gcloud.compute.Compute.AddressFilter;
import com.google.gcloud.compute.Compute.AddressListOption;
import com.google.gcloud.compute.Compute.AddressOption;
import com.google.gcloud.compute.Compute.DiskAggregatedListOption;
import com.google.gcloud.compute.Compute.DiskField;
import com.google.gcloud.compute.Compute.DiskFilter;
import com.google.gcloud.compute.Compute.DiskListOption;
import com.google.gcloud.compute.Compute.DiskOption;
import com.google.gcloud.compute.Compute.DiskTypeAggregatedListOption;
import com.google.gcloud.compute.Compute.DiskTypeFilter;
import com.google.gcloud.compute.Compute.DiskTypeListOption;
import com.google.gcloud.compute.Compute.DiskTypeOption;
import com.google.gcloud.compute.Compute.ImageField;
import com.google.gcloud.compute.Compute.ImageFilter;
import com.google.gcloud.compute.Compute.ImageListOption;
import com.google.gcloud.compute.Compute.ImageOption;
import com.google.gcloud.compute.Compute.LicenseOption;
import com.google.gcloud.compute.Compute.MachineTypeAggregatedListOption;
import com.google.gcloud.compute.Compute.MachineTypeFilter;
import com.google.gcloud.compute.Compute.MachineTypeListOption;
import com.google.gcloud.compute.Compute.MachineTypeOption;
import com.google.gcloud.compute.Compute.NetworkField;
import com.google.gcloud.compute.Compute.NetworkFilter;
import com.google.gcloud.compute.Compute.NetworkListOption;
import com.google.gcloud.compute.Compute.NetworkOption;
import com.google.gcloud.compute.Compute.OperationFilter;
import com.google.gcloud.compute.Compute.OperationListOption;
import com.google.gcloud.compute.Compute.OperationOption;
import com.google.gcloud.compute.Compute.RegionFilter;
import com.google.gcloud.compute.Compute.RegionListOption;
import com.google.gcloud.compute.Compute.RegionOption;
import com.google.gcloud.compute.Compute.SnapshotFilter;
import com.google.gcloud.compute.Compute.SnapshotListOption;
import com.google.gcloud.compute.Compute.SnapshotOption;
import com.google.gcloud.compute.Compute.SubnetworkAggregatedListOption;
import com.google.gcloud.compute.Compute.SubnetworkField;
import com.google.gcloud.compute.Compute.SubnetworkFilter;
import com.google.gcloud.compute.Compute.SubnetworkListOption;
import com.google.gcloud.compute.Compute.SubnetworkOption;
import com.google.gcloud.compute.Compute.ZoneFilter;
import com.google.gcloud.compute.Compute.ZoneListOption;
import com.google.gcloud.compute.Compute.ZoneOption;
import com.google.gcloud.compute.Operation.OperationError;
import com.google.gcloud.compute.Operation.OperationWarning;
import com.google.gcloud.compute.Operation.Status;
import com.google.gcloud.compute.spi.ComputeRpc;
import com.google.gcloud.compute.spi.ComputeRpc.Tuple;
import com.google.gcloud.compute.spi.ComputeRpcFactory;
import org.easymock.Capture;
import org.easymock.EasyMock;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.List;
import java.util.Map;
public class ComputeImplTest {
private static final String PROJECT = "project";
private static final String ID = "42";
private static final Long CREATION_TIMESTAMP = 1453293540000L;
private static final String DESCRIPTION = "description";
private static final String VALID_DISK_SIZE = "10GB-10TB";
private static final Long DEFAULT_DISK_SIZE_GB = 10L;
private static final DiskTypeId DISK_TYPE_ID = DiskTypeId.of("project", "zone", "diskType");
private static final DiskType DISK_TYPE = DiskType.builder()
.id(ID)
.diskTypeId(DISK_TYPE_ID)
.creationTimestamp(CREATION_TIMESTAMP)
.description(DESCRIPTION)
.validDiskSize(VALID_DISK_SIZE)
.defaultDiskSizeGb(DEFAULT_DISK_SIZE_GB)
.build();
private static final MachineTypeId MACHINE_TYPE_ID = MachineTypeId.of("project", "zone", "type");
private static final Integer GUEST_CPUS = 1;
private static final Integer MEMORY_MB = 2;
private static final List<Integer> SCRATCH_DISKS = ImmutableList.of(3);
private static final Integer MAXIMUM_PERSISTENT_DISKS = 4;
private static final Long MAXIMUM_PERSISTENT_DISKS_SIZE_GB = 5L;
private static final MachineType MACHINE_TYPE = MachineType.builder()
.id(ID)
.machineTypeId(MACHINE_TYPE_ID)
.creationTimestamp(CREATION_TIMESTAMP)
.description(DESCRIPTION)
.cpus(GUEST_CPUS)
.memoryMb(MEMORY_MB)
.scratchDisksSizeGb(SCRATCH_DISKS)
.maximumPersistentDisks(MAXIMUM_PERSISTENT_DISKS)
.maximumPersistentDisksSizeGb(MAXIMUM_PERSISTENT_DISKS_SIZE_GB)
.build();
private static final RegionId REGION_ID = RegionId.of("project", "region");
private static final Region.Status REGION_STATUS = Region.Status.DOWN;
private static final ZoneId ZONE_ID1 = ZoneId.of("project", "zone1");
private static final ZoneId ZONE_ID2 = ZoneId.of("project", "zone2");
private static final List<ZoneId> ZONES = ImmutableList.of(ZONE_ID1, ZONE_ID2);
private static final Region.Quota QUOTA1 =
new Region.Quota("METRIC1", 2, 1);
private static final Region.Quota QUOTA2 =
new Region.Quota("METRIC2", 4, 3);
private static final List<Region.Quota> QUOTAS = ImmutableList.of(QUOTA1, QUOTA2);
private static final Region REGION = Region.builder()
.regionId(REGION_ID)
.id(ID)
.creationTimestamp(CREATION_TIMESTAMP)
.description(DESCRIPTION)
.status(REGION_STATUS)
.zones(ZONES)
.quotas(QUOTAS)
.build();
private static final ZoneId ZONE_ID = ZoneId.of("project", "zone");
private static final Zone.Status ZONE_STATUS = Zone.Status.DOWN;
private static final Zone ZONE = Zone.builder()
.zoneId(ZONE_ID)
.id(ID)
.creationTimestamp(CREATION_TIMESTAMP)
.description(DESCRIPTION)
.status(ZONE_STATUS)
.region(REGION_ID)
.build();
private static final LicenseId LICENSE_ID = LicenseId.of("project", "license");
private static final Boolean CHARGES_USE_FEE = true;
private static final License LICENSE = new License(LICENSE_ID, CHARGES_USE_FEE);
private static final OperationError OPERATION_ERROR1 =
new OperationError("code1", "location1", "message1");
private static final OperationError OPERATION_ERROR2 =
new OperationError("code2", "location2", "message2");
private static final OperationWarning OPERATION_WARNING1 =
new OperationWarning("code1", "message1", ImmutableMap.of("k1", "v1"));
private static final OperationWarning OPERATION_WARNING2 =
new OperationWarning("code2", "location2", ImmutableMap.of("k2", "v2"));
private static final String CLIENT_OPERATION_ID = "clientOperationId";
private static final String OPERATION_TYPE = "delete";
private static final String TARGET_LINK = "targetLink";
private static final String TARGET_ID = "42";
private static final Status STATUS = Status.DONE;
private static final String STATUS_MESSAGE = "statusMessage";
private static final String USER = "user";
private static final Integer PROGRESS = 100;
private static final Long INSERT_TIME = 1453293540000L;
private static final Long START_TIME = 1453293420000L;
private static final Long END_TIME = 1453293480000L;
private static final List<OperationError> ERRORS =
ImmutableList.of(OPERATION_ERROR1, OPERATION_ERROR2);
private static final List<OperationWarning> WARNINGS =
ImmutableList.of(OPERATION_WARNING1, OPERATION_WARNING2);
private static final Integer HTTP_ERROR_STATUS_CODE = 404;
private static final String HTTP_ERROR_MESSAGE = "NOT FOUND";
private static final GlobalOperationId GLOBAL_OPERATION_ID =
GlobalOperationId.of("project", "op");
private static final ZoneOperationId ZONE_OPERATION_ID =
ZoneOperationId.of("project", "zone", "op");
private static final RegionOperationId REGION_OPERATION_ID =
RegionOperationId.of("project", "region", "op");
private static final RegionAddressId REGION_ADDRESS_ID =
RegionAddressId.of("project", "region", "address");
private static final GlobalAddressId GLOBAL_ADDRESS_ID =
GlobalAddressId.of("project", "address");
private static final AddressInfo REGION_ADDRESS = AddressInfo.builder(REGION_ADDRESS_ID).build();
private static final AddressInfo GLOBAL_ADDRESS = AddressInfo.builder(GLOBAL_ADDRESS_ID).build();
private static final DiskId DISK_ID = DiskId.of("project", "zone", "disk");
private static final SnapshotId SNAPSHOT_ID = SnapshotId.of("project", "snapshot");
private static final SnapshotInfo SNAPSHOT = SnapshotInfo.of(SNAPSHOT_ID, DISK_ID);
private static final ImageId IMAGE_ID = ImageId.of("project", "image");
private static final ImageInfo IMAGE = ImageInfo.of(IMAGE_ID, DiskImageConfiguration.of(DISK_ID));
private static final DeprecationStatus<ImageId> DEPRECATION_STATUS =
DeprecationStatus.builder(DeprecationStatus.Status.DEPRECATED, IMAGE_ID).build();
private static final DiskInfo DISK =
DiskInfo.of(DISK_ID, StandardDiskConfiguration.of(DISK_TYPE_ID));
private static final NetworkId NETWORK_ID = NetworkId.of("project", "network");
private static final SubnetworkId SUBNETWORK_ID = SubnetworkId.of("project", "region", "network");
private static final SubnetworkInfo SUBNETWORK =
SubnetworkInfo.of(SUBNETWORK_ID, NETWORK_ID, "192.168.0.0/16");
private static final NetworkInfo NETWORK =
NetworkInfo.of(NETWORK_ID, StandardNetworkConfiguration.of("192.168.0.0/16"));
// Empty ComputeRpc options
private static final Map<ComputeRpc.Option, ?> EMPTY_RPC_OPTIONS = ImmutableMap.of();
// DiskType options
private static final DiskTypeOption DISK_TYPE_OPTION_FIELDS =
DiskTypeOption.fields(Compute.DiskTypeField.ID, Compute.DiskTypeField.DESCRIPTION);
// DiskType list options
private static final DiskTypeFilter DISK_TYPE_FILTER =
DiskTypeFilter.equals(Compute.DiskTypeField.DESCRIPTION, "someDescription");
private static final DiskTypeListOption DISK_TYPE_LIST_PAGE_TOKEN =
DiskTypeListOption.pageToken("cursor");
private static final DiskTypeListOption DISK_TYPE_LIST_PAGE_SIZE =
DiskTypeListOption.pageSize(42L);
private static final DiskTypeListOption DISK_TYPE_LIST_FILTER =
DiskTypeListOption.filter(DISK_TYPE_FILTER);
private static final Map<ComputeRpc.Option, ?> DISK_TYPE_LIST_OPTIONS = ImmutableMap.of(
PAGE_TOKEN, "cursor",
MAX_RESULTS, 42L,
FILTER, "description eq someDescription");
// DiskType aggregated list options
private static final DiskTypeAggregatedListOption DISK_TYPE_AGGREGATED_LIST_PAGE_TOKEN =
DiskTypeAggregatedListOption.pageToken("cursor");
private static final DiskTypeAggregatedListOption DISK_TYPE_AGGREGATED_LIST_PAGE_SIZE =
DiskTypeAggregatedListOption.pageSize(42L);
private static final DiskTypeAggregatedListOption DISK_TYPE_AGGREGATED_LIST_FILTER =
DiskTypeAggregatedListOption.filter(DISK_TYPE_FILTER);
// MachineType options
private static final MachineTypeOption MACHINE_TYPE_OPTION_FIELDS =
MachineTypeOption.fields(Compute.MachineTypeField.ID,
Compute.MachineTypeField.DESCRIPTION);
// MachineType list options
private static final MachineTypeFilter MACHINE_TYPE_FILTER =
MachineTypeFilter.notEquals(Compute.MachineTypeField.MAXIMUM_PERSISTENT_DISKS, 42L);
private static final MachineTypeListOption MACHINE_TYPE_LIST_PAGE_TOKEN =
MachineTypeListOption.pageToken("cursor");
private static final MachineTypeListOption MACHINE_TYPE_LIST_PAGE_SIZE =
MachineTypeListOption.pageSize(42L);
private static final MachineTypeListOption MACHINE_TYPE_LIST_FILTER =
MachineTypeListOption.filter(MACHINE_TYPE_FILTER);
private static final Map<ComputeRpc.Option, ?> MACHINE_TYPE_LIST_OPTIONS = ImmutableMap.of(
PAGE_TOKEN, "cursor",
MAX_RESULTS, 42L,
FILTER, "maximumPersistentDisks ne 42");
// MachineType aggregated list options
private static final MachineTypeAggregatedListOption MACHINE_TYPE_AGGREGATED_LIST_PAGE_TOKEN =
MachineTypeAggregatedListOption.pageToken("cursor");
private static final MachineTypeAggregatedListOption MACHINE_TYPE_AGGREGATED_LIST_PAGE_SIZE =
MachineTypeAggregatedListOption.pageSize(42L);
private static final MachineTypeAggregatedListOption MACHINE_TYPE_AGGREGATED_LIST_FILTER =
MachineTypeAggregatedListOption.filter(MACHINE_TYPE_FILTER);
// Region options
private static final RegionOption REGION_OPTION_FIELDS =
RegionOption.fields(Compute.RegionField.ID, Compute.RegionField.DESCRIPTION);
// Region list options
private static final RegionFilter REGION_FILTER =
RegionFilter.equals(Compute.RegionField.ID, "someId");
private static final RegionListOption REGION_LIST_PAGE_TOKEN =
RegionListOption.pageToken("cursor");
private static final RegionListOption REGION_LIST_PAGE_SIZE =
RegionListOption.pageSize(42L);
private static final RegionListOption REGION_LIST_FILTER =
RegionListOption.filter(REGION_FILTER);
private static final Map<ComputeRpc.Option, ?> REGION_LIST_OPTIONS = ImmutableMap.of(
PAGE_TOKEN, "cursor",
MAX_RESULTS, 42L,
FILTER, "id eq someId");
// Zone options
private static final ZoneOption ZONE_OPTION_FIELDS =
ZoneOption.fields(Compute.ZoneField.ID, Compute.ZoneField.DESCRIPTION);
// Zone list options
private static final ZoneFilter ZONE_FILTER =
ZoneFilter.notEquals(Compute.ZoneField.NAME, "someName");
private static final ZoneListOption ZONE_LIST_PAGE_TOKEN =
ZoneListOption.pageToken("cursor");
private static final ZoneListOption ZONE_LIST_PAGE_SIZE = ZoneListOption.pageSize(42L);
private static final ZoneListOption ZONE_LIST_FILTER = ZoneListOption.filter(ZONE_FILTER);
private static final Map<ComputeRpc.Option, ?> ZONE_LIST_OPTIONS = ImmutableMap.of(
PAGE_TOKEN, "cursor",
MAX_RESULTS, 42L,
FILTER, "name ne someName");
// License options
private static final LicenseOption LICENSE_OPTION_FIELDS =
LicenseOption.fields(Compute.LicenseField.CHARGES_USE_FEE);
// Operation options
private static final OperationOption OPERATION_OPTION_FIELDS =
OperationOption.fields(Compute.OperationField.ID, Compute.OperationField.DESCRIPTION);
// Operation list options
private static final OperationFilter OPERATION_FILTER =
OperationFilter.notEquals(Compute.OperationField.PROGRESS, 0);
private static final OperationListOption OPERATION_LIST_PAGE_TOKEN =
OperationListOption.pageToken("cursor");
private static final OperationListOption OPERATION_LIST_PAGE_SIZE =
OperationListOption.pageSize(42L);
private static final OperationListOption OPERATION_LIST_FILTER =
OperationListOption.filter(OPERATION_FILTER);
private static final Map<ComputeRpc.Option, ?> OPERATION_LIST_OPTIONS = ImmutableMap.of(
PAGE_TOKEN, "cursor",
MAX_RESULTS, 42L,
FILTER, "progress ne 0");
// Address options
private static final AddressOption ADDRESS_OPTION_FIELDS =
AddressOption.fields(Compute.AddressField.ID, Compute.AddressField.DESCRIPTION);
// Address list options
private static final AddressFilter ADDRESS_FILTER =
AddressFilter.notEquals(Compute.AddressField.REGION, "someRegion");
private static final AddressListOption ADDRESS_LIST_PAGE_TOKEN =
AddressListOption.pageToken("cursor");
private static final AddressListOption ADDRESS_LIST_PAGE_SIZE = AddressListOption.pageSize(42L);
private static final AddressListOption ADDRESS_LIST_FILTER =
AddressListOption.filter(ADDRESS_FILTER);
private static final Map<ComputeRpc.Option, ?> ADDRESS_LIST_OPTIONS = ImmutableMap.of(
PAGE_TOKEN, "cursor",
MAX_RESULTS, 42L,
FILTER, "region ne someRegion");
// Address aggregated list options
private static final AddressAggregatedListOption ADDRESS_AGGREGATED_LIST_PAGE_TOKEN =
AddressAggregatedListOption.pageToken("cursor");
private static final AddressAggregatedListOption ADDRESS_AGGREGATED_LIST_PAGE_SIZE =
AddressAggregatedListOption.pageSize(42L);
private static final AddressAggregatedListOption ADDRESS_AGGREGATED_LIST_FILTER =
AddressAggregatedListOption.filter(ADDRESS_FILTER);
// Snapshot options
private static final SnapshotOption SNAPSHOT_OPTION_FIELDS =
SnapshotOption.fields(Compute.SnapshotField.ID, Compute.SnapshotField.DESCRIPTION);
// Snapshot list options
private static final SnapshotFilter SNAPSHOT_FILTER =
SnapshotFilter.equals(Compute.SnapshotField.DISK_SIZE_GB, 500L);
private static final SnapshotListOption SNAPSHOT_LIST_PAGE_TOKEN =
SnapshotListOption.pageToken("cursor");
private static final SnapshotListOption SNAPSHOT_LIST_PAGE_SIZE =
SnapshotListOption.pageSize(42L);
private static final SnapshotListOption SNAPSHOT_LIST_FILTER =
SnapshotListOption.filter(SNAPSHOT_FILTER);
private static final Map<ComputeRpc.Option, ?> SNAPSHOT_LIST_OPTIONS = ImmutableMap.of(
PAGE_TOKEN, "cursor",
MAX_RESULTS, 42L,
FILTER, "diskSizeGb eq 500");
// Image options
private static final ImageOption IMAGE_OPTION_FIELDS =
ImageOption.fields(ImageField.ID, ImageField.DESCRIPTION);
// Image list options
private static final ImageFilter IMAGE_FILTER =
ImageFilter.notEquals(ImageField.DISK_SIZE_GB, 500L);
private static final ImageListOption IMAGE_LIST_PAGE_TOKEN = ImageListOption.pageToken("cursor");
private static final ImageListOption IMAGE_LIST_PAGE_SIZE = ImageListOption.pageSize(42L);
private static final ImageListOption IMAGE_LIST_FILTER = ImageListOption.filter(IMAGE_FILTER);
private static final Map<ComputeRpc.Option, ?> IMAGE_LIST_OPTIONS = ImmutableMap.of(
PAGE_TOKEN, "cursor",
MAX_RESULTS, 42L,
FILTER, "diskSizeGb ne 500");
// Disk options
private static final DiskOption DISK_OPTION_FIELDS =
DiskOption.fields(DiskField.ID, DiskField.DESCRIPTION);
// Disk list options
private static final DiskFilter DISK_FILTER = DiskFilter.notEquals(DiskField.SIZE_GB, 500L);
private static final DiskListOption DISK_LIST_PAGE_TOKEN = DiskListOption.pageToken("cursor");
private static final DiskListOption DISK_LIST_PAGE_SIZE = DiskListOption.pageSize(42L);
private static final DiskListOption DISK_LIST_FILTER = DiskListOption.filter(DISK_FILTER);
private static final Map<ComputeRpc.Option, ?> DISK_LIST_OPTIONS = ImmutableMap.of(
PAGE_TOKEN, "cursor",
MAX_RESULTS, 42L,
FILTER, "sizeGb ne 500");
// Disk aggregated list options
private static final DiskAggregatedListOption DISK_AGGREGATED_LIST_PAGE_TOKEN =
DiskAggregatedListOption.pageToken("cursor");
private static final DiskAggregatedListOption DISK_AGGREGATED_LIST_PAGE_SIZE =
DiskAggregatedListOption.pageSize(42L);
private static final DiskAggregatedListOption DISK_AGGREGATED_LIST_FILTER =
DiskAggregatedListOption.filter(DISK_FILTER);
// Subnetwork options
private static final SubnetworkOption SUBNETWORK_OPTION_FIELDS =
SubnetworkOption.fields(SubnetworkField.ID, SubnetworkField.DESCRIPTION);
// Subnetwork list options
private static final SubnetworkFilter SUBNETWORK_FILTER =
SubnetworkFilter.equals(SubnetworkField.IP_CIDR_RANGE, "192.168.0.0/16");
private static final SubnetworkListOption SUBNETWORK_LIST_PAGE_TOKEN =
SubnetworkListOption.pageToken("cursor");
private static final SubnetworkListOption SUBNETWORK_LIST_PAGE_SIZE =
SubnetworkListOption.pageSize(42L);
private static final SubnetworkListOption SUBNETWORK_LIST_FILTER =
SubnetworkListOption.filter(SUBNETWORK_FILTER);
private static final Map<ComputeRpc.Option, ?> SUBNETWORK_LIST_OPTIONS = ImmutableMap.of(
PAGE_TOKEN, "cursor",
MAX_RESULTS, 42L,
FILTER, "ipCidrRange eq 192.168.0.0/16");
// Subnetwork aggregated list options
private static final SubnetworkAggregatedListOption SUBNETWORK_AGGREGATED_LIST_PAGE_TOKEN =
SubnetworkAggregatedListOption.pageToken("cursor");
private static final SubnetworkAggregatedListOption SUBNETWORK_AGGREGATED_LIST_PAGE_SIZE =
SubnetworkAggregatedListOption.pageSize(42L);
private static final SubnetworkAggregatedListOption SUBNETWORK_AGGREGATED_LIST_FILTER =
SubnetworkAggregatedListOption.filter(SUBNETWORK_FILTER);
// Network options
private static final NetworkOption NETWORK_OPTION_FIELDS =
NetworkOption.fields(NetworkField.ID, NetworkField.DESCRIPTION);
// Network list options
private static final NetworkFilter NETWORK_FILTER =
NetworkFilter.equals(NetworkField.IPV4_RANGE, "192.168.0.0/16");
private static final NetworkListOption NETWORK_LIST_PAGE_TOKEN =
NetworkListOption.pageToken("cursor");
private static final NetworkListOption NETWORK_LIST_PAGE_SIZE =
NetworkListOption.pageSize(42L);
private static final NetworkListOption NETWORK_LIST_FILTER =
NetworkListOption.filter(NETWORK_FILTER);
private static final Map<ComputeRpc.Option, ?> NETWORK_LIST_OPTIONS = ImmutableMap.of(
PAGE_TOKEN, "cursor",
MAX_RESULTS, 42L,
FILTER, "IPv4Range eq 192.168.0.0/16");
private static final Function<Operation, com.google.api.services.compute.model.Operation>
OPERATION_TO_PB_FUNCTION = new Function<Operation,
com.google.api.services.compute.model.Operation>() {
@Override
public com.google.api.services.compute.model.Operation apply(Operation operation) {
return operation.toPb();
}
};
private ComputeOptions options;
private ComputeRpcFactory rpcFactoryMock;
private ComputeRpc computeRpcMock;
private Compute compute;
private Operation globalOperation;
private Operation zoneOperation;
private Operation regionOperation;
@Rule
public ExpectedException thrown = ExpectedException.none();
@Before
public void setUp() {
rpcFactoryMock = EasyMock.createMock(ComputeRpcFactory.class);
computeRpcMock = EasyMock.createMock(ComputeRpc.class);
EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(ComputeOptions.class)))
.andReturn(computeRpcMock).times(2);
EasyMock.replay(rpcFactoryMock);
options = ComputeOptions.builder()
.projectId(PROJECT)
.serviceRpcFactory(rpcFactoryMock)
.retryParams(RetryParams.noRetries())
.build();
Compute otherService = options.toBuilder().build().service();
globalOperation = new Operation.Builder(otherService)
.id(ID)
.operationId(GLOBAL_OPERATION_ID)
.clientOperationId(CLIENT_OPERATION_ID)
.operationType(OPERATION_TYPE)
.targetLink(TARGET_LINK)
.targetId(TARGET_ID)
.status(STATUS)
.statusMessage(STATUS_MESSAGE)
.user(USER)
.progress(PROGRESS)
.insertTime(INSERT_TIME)
.startTime(START_TIME)
.endTime(END_TIME)
.errors(ERRORS)
.warnings(WARNINGS)
.httpErrorStatusCode(HTTP_ERROR_STATUS_CODE)
.httpErrorMessage(HTTP_ERROR_MESSAGE)
.description(DESCRIPTION)
.build();
zoneOperation = new Operation.Builder(otherService)
.id(ID)
.operationId(ZONE_OPERATION_ID)
.clientOperationId(CLIENT_OPERATION_ID)
.operationType(OPERATION_TYPE)
.targetLink(TARGET_LINK)
.targetId(TARGET_ID)
.status(STATUS)
.statusMessage(STATUS_MESSAGE)
.user(USER)
.progress(PROGRESS)
.insertTime(INSERT_TIME)
.startTime(START_TIME)
.endTime(END_TIME)
.errors(ERRORS)
.warnings(WARNINGS)
.httpErrorStatusCode(HTTP_ERROR_STATUS_CODE)
.httpErrorMessage(HTTP_ERROR_MESSAGE)
.description(DESCRIPTION)
.build();
regionOperation = new Operation.Builder(otherService)
.id(ID)
.operationId(REGION_OPERATION_ID)
.clientOperationId(CLIENT_OPERATION_ID)
.operationType(OPERATION_TYPE)
.targetLink(TARGET_LINK)
.targetId(TARGET_ID)
.status(STATUS)
.statusMessage(STATUS_MESSAGE)
.user(USER)
.progress(PROGRESS)
.insertTime(INSERT_TIME)
.startTime(START_TIME)
.endTime(END_TIME)
.errors(ERRORS)
.warnings(WARNINGS)
.httpErrorStatusCode(HTTP_ERROR_STATUS_CODE)
.httpErrorMessage(HTTP_ERROR_MESSAGE)
.description(DESCRIPTION)
.build();
}
@After
public void tearDown() {
EasyMock.verify(rpcFactoryMock, computeRpcMock);
}
@Test
public void testGetOptions() {
EasyMock.replay(computeRpcMock);
compute = options.service();
assertSame(options, compute.options());
}
@Test
public void testGetDiskType() {
EasyMock.expect(
computeRpcMock.getDiskType(DISK_TYPE_ID.zone(), DISK_TYPE_ID.type(), EMPTY_RPC_OPTIONS))
.andReturn(DISK_TYPE.toPb());
EasyMock.replay(computeRpcMock);
compute = options.service();
DiskType diskType = compute.getDiskType(DISK_TYPE_ID.zone(), DISK_TYPE_ID.type());
assertEquals(DISK_TYPE, diskType);
}
@Test
public void testGetDiskType_Null() {
EasyMock.expect(
computeRpcMock.getDiskType(DISK_TYPE_ID.zone(), DISK_TYPE_ID.type(), EMPTY_RPC_OPTIONS))
.andReturn(null);
EasyMock.replay(computeRpcMock);
compute = options.service();
assertNull(compute.getDiskType(DISK_TYPE_ID.zone(), DISK_TYPE_ID.type()));
}
@Test
public void testGetDiskTypeFromDiskTypeId() {
EasyMock.expect(
computeRpcMock.getDiskType(DISK_TYPE_ID.zone(), DISK_TYPE_ID.type(), EMPTY_RPC_OPTIONS))
.andReturn(DISK_TYPE.toPb());
EasyMock.replay(computeRpcMock);
compute = options.service();
DiskType diskType = compute.getDiskType(DISK_TYPE_ID);
assertEquals(DISK_TYPE, diskType);
}
@Test
public void testGetDiskTypeWithSelectedFields() {
Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture.newInstance();
EasyMock.expect(
computeRpcMock.getDiskType(
eq(DISK_TYPE_ID.zone()), eq(DISK_TYPE_ID.type()), capture(capturedOptions)))
.andReturn(DISK_TYPE.toPb());
EasyMock.replay(computeRpcMock);
compute = options.service();
DiskType diskType =
compute.getDiskType(DISK_TYPE_ID.zone(), DISK_TYPE_ID.type(), DISK_TYPE_OPTION_FIELDS);
String selector = (String) capturedOptions.getValue().get(DISK_TYPE_OPTION_FIELDS.rpcOption());
assertTrue(selector.contains("selfLink"));
assertTrue(selector.contains("id"));
assertTrue(selector.contains("description"));
assertEquals(23, selector.length());
assertEquals(DISK_TYPE, diskType);
}
@Test
public void testListDiskTypes() {
String cursor = "cursor";
compute = options.service();
ImmutableList<DiskType> diskTypeList = ImmutableList.of(DISK_TYPE, DISK_TYPE);
Tuple<String, Iterable<com.google.api.services.compute.model.DiskType>> result =
Tuple.of(cursor, Iterables.transform(diskTypeList, DiskType.TO_PB_FUNCTION));
EasyMock.expect(computeRpcMock.listDiskTypes(DISK_TYPE_ID.zone(), EMPTY_RPC_OPTIONS))
.andReturn(result);
EasyMock.replay(computeRpcMock);
Page<DiskType> page = compute.listDiskTypes(DISK_TYPE_ID.zone());
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(diskTypeList.toArray(), Iterables.toArray(page.values(), DiskType.class));
}
@Test
public void testListDiskTypesNextPage() {
String cursor = "cursor";
String nextCursor = "nextCursor";
compute = options.service();
ImmutableList<DiskType> diskTypeList = ImmutableList.of(DISK_TYPE, DISK_TYPE);
Tuple<String, Iterable<com.google.api.services.compute.model.DiskType>> result =
Tuple.of(cursor, Iterables.transform(diskTypeList, DiskType.TO_PB_FUNCTION));
ImmutableList<DiskType> nextDiskTypeList = ImmutableList.of(DISK_TYPE);
Tuple<String, Iterable<com.google.api.services.compute.model.DiskType>> nextResult =
Tuple.of(nextCursor, Iterables.transform(nextDiskTypeList, DiskType.TO_PB_FUNCTION));
Map<ComputeRpc.Option, ?> nextOptions = ImmutableMap.of(PAGE_TOKEN, cursor);
EasyMock.expect(computeRpcMock.listDiskTypes(DISK_TYPE_ID.zone(), EMPTY_RPC_OPTIONS))
.andReturn(result);
EasyMock.expect(computeRpcMock.listDiskTypes(DISK_TYPE_ID.zone(), nextOptions))
.andReturn(nextResult);
EasyMock.replay(computeRpcMock);
Page<DiskType> page = compute.listDiskTypes(DISK_TYPE_ID.zone());
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(diskTypeList.toArray(), Iterables.toArray(page.values(), DiskType.class));
page = page.nextPage();
assertEquals(nextCursor, page.nextPageCursor());
assertArrayEquals(nextDiskTypeList.toArray(), Iterables.toArray(page.values(), DiskType.class));
}
@Test
public void testListEmptyDiskTypes() {
ImmutableList<com.google.api.services.compute.model.DiskType> diskTypes = ImmutableList.of();
Tuple<String, Iterable<com.google.api.services.compute.model.DiskType>> result =
Tuple.<String, Iterable<com.google.api.services.compute.model.DiskType>>of(null, diskTypes);
EasyMock.expect(computeRpcMock.listDiskTypes(DISK_TYPE_ID.zone(), EMPTY_RPC_OPTIONS))
.andReturn(result);
EasyMock.replay(computeRpcMock);
compute = options.service();
Page<DiskType> page = compute.listDiskTypes(DISK_TYPE_ID.zone());
assertNull(page.nextPageCursor());
assertArrayEquals(diskTypes.toArray(), Iterables.toArray(page.values(), DiskType.class));
}
@Test
public void testListDiskTypesWithOptions() {
String cursor = "cursor";
compute = options.service();
ImmutableList<DiskType> diskTypeList = ImmutableList.of(DISK_TYPE, DISK_TYPE);
Tuple<String, Iterable<com.google.api.services.compute.model.DiskType>> result =
Tuple.of(cursor, Iterables.transform(diskTypeList, DiskType.TO_PB_FUNCTION));
EasyMock.expect(computeRpcMock.listDiskTypes(DISK_TYPE_ID.zone(), DISK_TYPE_LIST_OPTIONS))
.andReturn(result);
EasyMock.replay(computeRpcMock);
Page<DiskType> page = compute.listDiskTypes(DISK_TYPE_ID.zone(), DISK_TYPE_LIST_PAGE_SIZE,
DISK_TYPE_LIST_PAGE_TOKEN, DISK_TYPE_LIST_FILTER);
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(diskTypeList.toArray(), Iterables.toArray(page.values(), DiskType.class));
}
@Test
public void testAggregatedListDiskTypes() {
String cursor = "cursor";
compute = options.service();
ImmutableList<DiskType> diskTypeList = ImmutableList.of(DISK_TYPE, DISK_TYPE);
Tuple<String, Iterable<com.google.api.services.compute.model.DiskType>> result =
Tuple.of(cursor, Iterables.transform(diskTypeList, DiskType.TO_PB_FUNCTION));
EasyMock.expect(computeRpcMock.listDiskTypes(EMPTY_RPC_OPTIONS)).andReturn(result);
EasyMock.replay(computeRpcMock);
Page<DiskType> page = compute.listDiskTypes();
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(diskTypeList.toArray(), Iterables.toArray(page.values(), DiskType.class));
}
@Test
public void testAggregatedListDiskTypesNextPage() {
String cursor = "cursor";
String nextCursor = "nextCursor";
compute = options.service();
ImmutableList<DiskType> diskTypeList = ImmutableList.of(DISK_TYPE, DISK_TYPE);
Tuple<String, Iterable<com.google.api.services.compute.model.DiskType>> result =
Tuple.of(cursor, Iterables.transform(diskTypeList, DiskType.TO_PB_FUNCTION));
ImmutableList<DiskType> nextDiskTypeList = ImmutableList.of(DISK_TYPE);
Tuple<String, Iterable<com.google.api.services.compute.model.DiskType>> nextResult =
Tuple.of(nextCursor, Iterables.transform(nextDiskTypeList, DiskType.TO_PB_FUNCTION));
Map<ComputeRpc.Option, ?> nextOptions = ImmutableMap.of(PAGE_TOKEN, cursor);
EasyMock.expect(computeRpcMock.listDiskTypes(EMPTY_RPC_OPTIONS)).andReturn(result);
EasyMock.expect(computeRpcMock.listDiskTypes(nextOptions)).andReturn(nextResult);
EasyMock.replay(computeRpcMock);
Page<DiskType> page = compute.listDiskTypes();
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(diskTypeList.toArray(), Iterables.toArray(page.values(), DiskType.class));
page = page.nextPage();
assertEquals(nextCursor, page.nextPageCursor());
assertArrayEquals(nextDiskTypeList.toArray(), Iterables.toArray(page.values(), DiskType.class));
}
@Test
public void testAggregatedListEmptyDiskTypes() {
ImmutableList<com.google.api.services.compute.model.DiskType> diskTypes = ImmutableList.of();
Tuple<String, Iterable<com.google.api.services.compute.model.DiskType>> result =
Tuple.<String, Iterable<com.google.api.services.compute.model.DiskType>>of(null, diskTypes);
EasyMock.expect(computeRpcMock.listDiskTypes(EMPTY_RPC_OPTIONS)).andReturn(result);
EasyMock.replay(computeRpcMock);
compute = options.service();
Page<DiskType> page = compute.listDiskTypes();
assertNull(page.nextPageCursor());
assertArrayEquals(diskTypes.toArray(), Iterables.toArray(page.values(), DiskType.class));
}
@Test
public void testAggregatedListDiskTypesWithOptions() {
String cursor = "cursor";
compute = options.service();
ImmutableList<DiskType> diskTypeList = ImmutableList.of(DISK_TYPE, DISK_TYPE);
Tuple<String, Iterable<com.google.api.services.compute.model.DiskType>> result =
Tuple.of(cursor, Iterables.transform(diskTypeList, DiskType.TO_PB_FUNCTION));
EasyMock.expect(computeRpcMock.listDiskTypes(DISK_TYPE_LIST_OPTIONS)).andReturn(result);
EasyMock.replay(computeRpcMock);
Page<DiskType> page = compute.listDiskTypes(DISK_TYPE_AGGREGATED_LIST_PAGE_SIZE,
DISK_TYPE_AGGREGATED_LIST_PAGE_TOKEN, DISK_TYPE_AGGREGATED_LIST_FILTER);
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(diskTypeList.toArray(), Iterables.toArray(page.values(), DiskType.class));
}
@Test
public void testGetMachineType() {
EasyMock.expect(
computeRpcMock.getMachineType(
MACHINE_TYPE_ID.zone(), MACHINE_TYPE_ID.type(), EMPTY_RPC_OPTIONS))
.andReturn(MACHINE_TYPE.toPb());
EasyMock.replay(computeRpcMock);
compute = options.service();
MachineType machineType =
compute.getMachineType(MACHINE_TYPE_ID.zone(), MACHINE_TYPE_ID.type());
assertEquals(MACHINE_TYPE, machineType);
}
@Test
public void testGetMachineType_Null() {
EasyMock.expect(
computeRpcMock.getMachineType(
MACHINE_TYPE_ID.zone(), MACHINE_TYPE_ID.type(), EMPTY_RPC_OPTIONS))
.andReturn(null);
EasyMock.replay(computeRpcMock);
compute = options.service();
assertNull(compute.getMachineType(MACHINE_TYPE_ID.zone(), MACHINE_TYPE_ID.type()));
}
@Test
public void testGetMachineTypeFromMachineTypeId() {
EasyMock.expect(computeRpcMock.getMachineType(
MACHINE_TYPE_ID.zone(), MACHINE_TYPE_ID.type(), EMPTY_RPC_OPTIONS))
.andReturn(MACHINE_TYPE.toPb());
EasyMock.replay(computeRpcMock);
compute = options.service();
MachineType machineType = compute.getMachineType(MACHINE_TYPE_ID);
assertEquals(MACHINE_TYPE, machineType);
}
@Test
public void testGetMachineTypeWithSelectedFields() {
Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture.newInstance();
EasyMock.expect(
computeRpcMock.getMachineType(eq(MACHINE_TYPE_ID.zone()), eq(MACHINE_TYPE_ID.type()),
capture(capturedOptions)))
.andReturn(MACHINE_TYPE.toPb());
EasyMock.replay(computeRpcMock);
compute = options.service();
MachineType machineType = compute.getMachineType(MACHINE_TYPE_ID.zone(),
MACHINE_TYPE_ID.type(), MACHINE_TYPE_OPTION_FIELDS);
String selector = (String) capturedOptions.getValue().get(DISK_TYPE_OPTION_FIELDS.rpcOption());
assertTrue(selector.contains("selfLink"));
assertTrue(selector.contains("id"));
assertTrue(selector.contains("description"));
assertEquals(23, selector.length());
assertEquals(MACHINE_TYPE, machineType);
}
@Test
public void testListMachineTypes() {
String cursor = "cursor";
compute = options.service();
ImmutableList<MachineType> machineTypeList = ImmutableList.of(MACHINE_TYPE, MACHINE_TYPE);
Tuple<String, Iterable<com.google.api.services.compute.model.MachineType>> result =
Tuple.of(cursor, Iterables.transform(machineTypeList, MachineType.TO_PB_FUNCTION));
EasyMock.expect(computeRpcMock.listMachineTypes(MACHINE_TYPE_ID.zone(), EMPTY_RPC_OPTIONS))
.andReturn(result);
EasyMock.replay(computeRpcMock);
Page<MachineType> page = compute.listMachineTypes(MACHINE_TYPE_ID.zone());
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(machineTypeList.toArray(), Iterables.toArray(page.values(),
MachineType.class));
}
@Test
public void testListMachineTypesNextPage() {
String cursor = "cursor";
String nextCursor = "nextCursor";
compute = options.service();
ImmutableList<MachineType> machineTypeList = ImmutableList.of(MACHINE_TYPE, MACHINE_TYPE);
Tuple<String, Iterable<com.google.api.services.compute.model.MachineType>> result =
Tuple.of(cursor, Iterables.transform(machineTypeList, MachineType.TO_PB_FUNCTION));
ImmutableList<MachineType> nextMachineTypeList = ImmutableList.of(MACHINE_TYPE);
Tuple<String, Iterable<com.google.api.services.compute.model.MachineType>> nextResult =
Tuple.of(nextCursor, Iterables.transform(nextMachineTypeList, MachineType.TO_PB_FUNCTION));
Map<ComputeRpc.Option, ?> nextOptions = ImmutableMap.of(PAGE_TOKEN, cursor);
EasyMock.expect(computeRpcMock.listMachineTypes(MACHINE_TYPE_ID.zone(), EMPTY_RPC_OPTIONS))
.andReturn(result);
EasyMock.expect(computeRpcMock.listMachineTypes(MACHINE_TYPE_ID.zone(), nextOptions))
.andReturn(nextResult);
EasyMock.replay(computeRpcMock);
Page<MachineType> page = compute.listMachineTypes(MACHINE_TYPE_ID.zone());
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(machineTypeList.toArray(),
Iterables.toArray(page.values(), MachineType.class));
page = page.nextPage();
assertEquals(nextCursor, page.nextPageCursor());
assertArrayEquals(nextMachineTypeList.toArray(),
Iterables.toArray(page.values(), MachineType.class));
}
@Test
public void testListEmptyMachineTypes() {
ImmutableList<com.google.api.services.compute.model.MachineType> machineTypes =
ImmutableList.of();
Tuple<String, Iterable<com.google.api.services.compute.model.MachineType>> result =
Tuple.<String, Iterable<com.google.api.services.compute.model.MachineType>>of(null,
machineTypes);
EasyMock.expect(computeRpcMock.listMachineTypes(MACHINE_TYPE_ID.zone(), EMPTY_RPC_OPTIONS))
.andReturn(result);
EasyMock.replay(computeRpcMock);
compute = options.service();
Page<MachineType> page = compute.listMachineTypes(MACHINE_TYPE_ID.zone());
assertNull(page.nextPageCursor());
assertArrayEquals(machineTypes.toArray(), Iterables.toArray(page.values(), MachineType.class));
}
@Test
public void testListMachineTypesWithOptions() {
String cursor = "cursor";
compute = options.service();
ImmutableList<MachineType> machineTypeList = ImmutableList.of(MACHINE_TYPE, MACHINE_TYPE);
Tuple<String, Iterable<com.google.api.services.compute.model.MachineType>> result =
Tuple.of(cursor, Iterables.transform(machineTypeList, MachineType.TO_PB_FUNCTION));
EasyMock.expect(
computeRpcMock.listMachineTypes(MACHINE_TYPE_ID.zone(), MACHINE_TYPE_LIST_OPTIONS))
.andReturn(result);
EasyMock.replay(computeRpcMock);
Page<MachineType> page = compute.listMachineTypes(MACHINE_TYPE_ID.zone(),
MACHINE_TYPE_LIST_PAGE_SIZE, MACHINE_TYPE_LIST_PAGE_TOKEN, MACHINE_TYPE_LIST_FILTER);
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(machineTypeList.toArray(),
Iterables.toArray(page.values(), MachineType.class));
}
@Test
public void testAggregatedListMachineTypes() {
String cursor = "cursor";
compute = options.service();
ImmutableList<MachineType> machineTypeList = ImmutableList.of(MACHINE_TYPE, MACHINE_TYPE);
Tuple<String, Iterable<com.google.api.services.compute.model.MachineType>> result =
Tuple.of(cursor, Iterables.transform(machineTypeList, MachineType.TO_PB_FUNCTION));
EasyMock.expect(computeRpcMock.listMachineTypes(EMPTY_RPC_OPTIONS)).andReturn(result);
EasyMock.replay(computeRpcMock);
Page<MachineType> page = compute.listMachineTypes();
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(machineTypeList.toArray(), Iterables.toArray(page.values(),
MachineType.class));
}
@Test
public void testAggregatedListMachineTypesNextPage() {
String cursor = "cursor";
String nextCursor = "nextCursor";
compute = options.service();
ImmutableList<MachineType> machineTypeList = ImmutableList.of(MACHINE_TYPE, MACHINE_TYPE);
Tuple<String, Iterable<com.google.api.services.compute.model.MachineType>> result =
Tuple.of(cursor, Iterables.transform(machineTypeList, MachineType.TO_PB_FUNCTION));
ImmutableList<MachineType> nextMachineTypeList = ImmutableList.of(MACHINE_TYPE);
Tuple<String, Iterable<com.google.api.services.compute.model.MachineType>> nextResult =
Tuple.of(nextCursor, Iterables.transform(nextMachineTypeList, MachineType.TO_PB_FUNCTION));
Map<ComputeRpc.Option, ?> nextOptions = ImmutableMap.of(PAGE_TOKEN, cursor);
EasyMock.expect(computeRpcMock.listMachineTypes(EMPTY_RPC_OPTIONS)).andReturn(result);
EasyMock.expect(computeRpcMock.listMachineTypes(nextOptions)).andReturn(nextResult);
EasyMock.replay(computeRpcMock);
Page<MachineType> page = compute.listMachineTypes();
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(machineTypeList.toArray(),
Iterables.toArray(page.values(), MachineType.class));
page = page.nextPage();
assertEquals(nextCursor, page.nextPageCursor());
assertArrayEquals(nextMachineTypeList.toArray(),
Iterables.toArray(page.values(), MachineType.class));
}
@Test
public void testAggregatedListEmptyMachineTypes() {
ImmutableList<com.google.api.services.compute.model.MachineType> machineTypes =
ImmutableList.of();
Tuple<String, Iterable<com.google.api.services.compute.model.MachineType>> result =
Tuple.<String, Iterable<com.google.api.services.compute.model.MachineType>>of(null,
machineTypes);
EasyMock.expect(computeRpcMock.listMachineTypes(EMPTY_RPC_OPTIONS)).andReturn(result);
EasyMock.replay(computeRpcMock);
compute = options.service();
Page<MachineType> page = compute.listMachineTypes();
assertNull(page.nextPageCursor());
assertArrayEquals(machineTypes.toArray(), Iterables.toArray(page.values(), MachineType.class));
}
@Test
public void testAggregatedListMachineTypesWithOptions() {
String cursor = "cursor";
compute = options.service();
ImmutableList<MachineType> machineTypeList = ImmutableList.of(MACHINE_TYPE, MACHINE_TYPE);
Tuple<String, Iterable<com.google.api.services.compute.model.MachineType>> result =
Tuple.of(cursor, Iterables.transform(machineTypeList, MachineType.TO_PB_FUNCTION));
EasyMock.expect(computeRpcMock.listMachineTypes(MACHINE_TYPE_LIST_OPTIONS))
.andReturn(result);
EasyMock.replay(computeRpcMock);
Page<MachineType> page = compute.listMachineTypes(MACHINE_TYPE_AGGREGATED_LIST_PAGE_SIZE,
MACHINE_TYPE_AGGREGATED_LIST_PAGE_TOKEN, MACHINE_TYPE_AGGREGATED_LIST_FILTER);
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(machineTypeList.toArray(),
Iterables.toArray(page.values(), MachineType.class));
}
@Test
public void testGetRegion() {
EasyMock.expect(computeRpcMock.getRegion(REGION_ID.region(), EMPTY_RPC_OPTIONS))
.andReturn(REGION.toPb());
EasyMock.replay(computeRpcMock);
compute = options.service();
Region region = compute.getRegion(REGION_ID.region());
assertEquals(REGION, region);
}
@Test
public void testGetRegion_Null() {
EasyMock.expect(computeRpcMock.getRegion(REGION_ID.region(), EMPTY_RPC_OPTIONS))
.andReturn(null);
EasyMock.replay(computeRpcMock);
compute = options.service();
assertNull(compute.getRegion(REGION_ID.region()));
}
@Test
public void testGetRegionWithSelectedFields() {
Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture.newInstance();
EasyMock.expect(computeRpcMock.getRegion(eq(REGION_ID.region()), capture(capturedOptions)))
.andReturn(REGION.toPb());
EasyMock.replay(computeRpcMock);
compute = options.service();
Region region = compute.getRegion(REGION_ID.region(), REGION_OPTION_FIELDS);
String selector = (String) capturedOptions.getValue().get(REGION_OPTION_FIELDS.rpcOption());
assertTrue(selector.contains("selfLink"));
assertTrue(selector.contains("id"));
assertTrue(selector.contains("description"));
assertEquals(23, selector.length());
assertEquals(REGION, region);
}
@Test
public void testListRegions() {
String cursor = "cursor";
compute = options.service();
ImmutableList<Region> regionList = ImmutableList.of(REGION, REGION);
Tuple<String, Iterable<com.google.api.services.compute.model.Region>> result =
Tuple.of(cursor, Iterables.transform(regionList, Region.TO_PB_FUNCTION));
EasyMock.expect(computeRpcMock.listRegions(EMPTY_RPC_OPTIONS)).andReturn(result);
EasyMock.replay(computeRpcMock);
Page<Region> page = compute.listRegions();
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(regionList.toArray(), Iterables.toArray(page.values(), Region.class));
}
@Test
public void testListRegionsNextPage() {