@Override public void run(Compute compute, RegionId region) { Page<Address> addressPage; if (region != null) { addressPage = compute.listRegionAddresses(region.getRegion()); } else { addressPage = compute.listAddresses(); } for (Address address : addressPage.iterateAll()) { System.out.println(address); } } }
@Test public void testAggregatedListEmptyAddresses() { ImmutableList<com.google.api.services.compute.model.Address> addresses = ImmutableList.of(); Tuple<String, Iterable<com.google.api.services.compute.model.Address>> result = Tuple.<String, Iterable<com.google.api.services.compute.model.Address>>of(null, addresses); EasyMock.expect(computeRpcMock.listAddresses(EMPTY_RPC_OPTIONS)).andReturn(result); EasyMock.replay(computeRpcMock); compute = options.getService(); Page<Address> page = compute.listAddresses(); assertNull(page.getNextPageToken()); assertArrayEquals(addresses.toArray(), Iterables.toArray(page.getValues(), Address.class)); }
@Test public void testAggregatedListAddresses() { String cursor = "cursor"; compute = options.getService(); ImmutableList<Address> addressList = ImmutableList.of( new Address(compute, new AddressInfo.BuilderImpl(REGION_ADDRESS)), new Address(compute, new AddressInfo.BuilderImpl(REGION_ADDRESS))); Tuple<String, Iterable<com.google.api.services.compute.model.Address>> result = Tuple.of(cursor, Iterables.transform(addressList, AddressInfo.TO_PB_FUNCTION)); EasyMock.expect(computeRpcMock.listAddresses(EMPTY_RPC_OPTIONS)).andReturn(result); EasyMock.replay(computeRpcMock); Page<Address> page = compute.listAddresses(); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(addressList.toArray(), Iterables.toArray(page.getValues(), Address.class)); }
@Test public void testAggregatedListAddressesWithOptions() { String cursor = "cursor"; compute = options.getService(); ImmutableList<Address> addressList = ImmutableList.of( new Address(compute, new AddressInfo.BuilderImpl(REGION_ADDRESS)), new Address(compute, new AddressInfo.BuilderImpl(REGION_ADDRESS))); Tuple<String, Iterable<com.google.api.services.compute.model.Address>> result = Tuple.of(cursor, Iterables.transform(addressList, AddressInfo.TO_PB_FUNCTION)); EasyMock.expect(computeRpcMock.listAddresses(ADDRESS_LIST_OPTIONS)).andReturn(result); EasyMock.replay(computeRpcMock); Page<Address> page = compute.listAddresses( ADDRESS_AGGREGATED_LIST_PAGE_SIZE, ADDRESS_AGGREGATED_LIST_PAGE_TOKEN, ADDRESS_AGGREGATED_LIST_FILTER); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(addressList.toArray(), Iterables.toArray(page.getValues(), Address.class)); }
@Test public void testAggregatedListAddressesNextPage() { String cursor = "cursor"; String nextCursor = "nextCursor"; compute = options.getService(); ImmutableList<Address> addressList = ImmutableList.of( new Address(compute, new AddressInfo.BuilderImpl(REGION_ADDRESS)), new Address(compute, new AddressInfo.BuilderImpl(REGION_ADDRESS))); ImmutableList<Address> nextAddressList = ImmutableList.of(new Address(compute, new AddressInfo.BuilderImpl(REGION_ADDRESS))); Tuple<String, Iterable<com.google.api.services.compute.model.Address>> result = Tuple.of(cursor, Iterables.transform(addressList, AddressInfo.TO_PB_FUNCTION)); Tuple<String, Iterable<com.google.api.services.compute.model.Address>> nextResult = Tuple.of(nextCursor, Iterables.transform(nextAddressList, AddressInfo.TO_PB_FUNCTION)); Map<ComputeRpc.Option, ?> nextOptions = ImmutableMap.of(ComputeRpc.Option.PAGE_TOKEN, cursor); EasyMock.expect(computeRpcMock.listAddresses(EMPTY_RPC_OPTIONS)).andReturn(result); EasyMock.expect(computeRpcMock.listAddresses(nextOptions)).andReturn(nextResult); EasyMock.replay(computeRpcMock); Page<Address> page = compute.listAddresses(); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(addressList.toArray(), Iterables.toArray(page.getValues(), Address.class)); page = page.getNextPage(); assertEquals(nextCursor, page.getNextPageToken()); assertArrayEquals( nextAddressList.toArray(), Iterables.toArray(page.getValues(), Address.class)); }
@Test public void testAggregatedListAddresses() throws InterruptedException, TimeoutException { String prefix = BASE_RESOURCE_NAME + "aggregated-list-address"; String[] addressNames = {prefix + "1", prefix + "2"}; AddressId firstAddressId = RegionAddressId.of(REGION, addressNames[0]); AddressId secondAddressId = GlobalAddressId.of(REGION, addressNames[1]); Operation firstOperation = compute.create(AddressInfo.of(firstAddressId)); Operation secondOperation = compute.create(AddressInfo.of(secondAddressId)); firstOperation.waitFor(); resourceCleaner.add(firstAddressId); secondOperation.waitFor(); resourceCleaner.add(secondAddressId); Set<String> addressSet = ImmutableSet.copyOf(addressNames); Compute.AddressFilter filter = Compute.AddressFilter.equals(Compute.AddressField.NAME, prefix + "\\d"); Page<Address> addressPage = compute.listAddresses(Compute.AddressAggregatedListOption.filter(filter)); Iterator<Address> addressIterator = addressPage.iterateAll().iterator(); int count = 0; while (addressIterator.hasNext()) { Address address = addressIterator.next(); assertNotNull(address.getAddressId()); assertTrue(addressSet.contains(address.getAddressId().getAddress())); assertNotNull(address.getAddress()); assertNotNull(address.getCreationTimestamp()); assertNotNull(address.getGeneratedId()); count++; } assertEquals(2, count); }