Refine search
@Override public Address build() { return new Address(compute, infoBuilder); } }
@Override public Address apply(com.google.api.services.compute.model.Address address) { return Address.fromPb(serviceOptions.getService(), address); } };
private void compareAddress(Address expected, Address value) { assertEquals(expected, value); assertEquals(expected.getCompute().getOptions(), value.getCompute().getOptions()); assertEquals(expected.getAddress(), value.getAddress()); assertEquals(expected.getCreationTimestamp(), value.getCreationTimestamp()); assertEquals(expected.getDescription(), value.getDescription()); assertEquals(expected.getGeneratedId(), value.getGeneratedId()); assertEquals(expected.getAddressId(), value.getAddressId()); assertEquals(expected.getUsage(), value.getUsage()); assertEquals(expected.getStatus(), value.getStatus()); assertEquals(expected.hashCode(), value.hashCode()); } }
@Test public void testReload() throws Exception { initializeExpectedAddress(5); expect(compute.getOptions()).andReturn(mockOptions); expect(compute.getAddress(REGION_ADDRESS_ID)).andReturn(regionForwardingAddress); replay(compute); initializeAddress(); Address updatedAddress = address.reload(); compareAddress(regionForwardingAddress, updatedAddress); verify(compute); }
@Test public void testExists_True() throws Exception { initializeExpectedAddress(3); Compute.AddressOption[] expectedOptions = {Compute.AddressOption.fields()}; expect(compute.getOptions()).andReturn(mockOptions); expect(compute.getAddress(REGION_ADDRESS_ID, expectedOptions)) .andReturn(regionForwardingAddress); replay(compute); initializeAddress(); assertTrue(address.exists()); verify(compute); }
@Test public void testGetGlobalAddress() { EasyMock.expect( computeRpcMock.getGlobalAddress(GLOBAL_ADDRESS_ID.getAddress(), EMPTY_RPC_OPTIONS)) .andReturn(GLOBAL_ADDRESS.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); Address address = compute.getAddress(GLOBAL_ADDRESS_ID); assertEquals(new Address(compute, new AddressInfo.BuilderImpl(GLOBAL_ADDRESS)), address); }
@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 testListRegionAddresses() { 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.listRegionAddresses(REGION_ADDRESS_ID.getRegion(), EMPTY_RPC_OPTIONS)) .andReturn(result); EasyMock.replay(computeRpcMock); Page<Address> page = compute.listRegionAddresses(REGION_ADDRESS_ID.getRegion()); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(addressList.toArray(), Iterables.toArray(page.getValues(), Address.class)); }
@Test public void testListGlobalAddressesWithOptions() { String cursor = "cursor"; compute = options.getService(); ImmutableList<Address> addressList = ImmutableList.of( new Address(compute, new AddressInfo.BuilderImpl(GLOBAL_ADDRESS)), new Address(compute, new AddressInfo.BuilderImpl(GLOBAL_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.listGlobalAddresses(ADDRESS_LIST_OPTIONS)).andReturn(result); EasyMock.replay(computeRpcMock); Page<Address> page = compute.listGlobalAddresses( ADDRESS_LIST_PAGE_SIZE, ADDRESS_LIST_PAGE_TOKEN, ADDRESS_LIST_FILTER); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(addressList.toArray(), Iterables.toArray(page.getValues(), Address.class)); }
@Test public void testGetGlobalAddressWithSelectedFields() { Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture.newInstance(); EasyMock.expect( computeRpcMock.getGlobalAddress( eq(GLOBAL_ADDRESS_ID.getAddress()), capture(capturedOptions))) .andReturn(GLOBAL_ADDRESS.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); Address address = compute.getAddress(GLOBAL_ADDRESS_ID, ADDRESS_OPTION_FIELDS); String selector = (String) capturedOptions.getValue().get(ADDRESS_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains("selfLink")); assertTrue(selector.contains("id")); assertTrue(selector.contains("description")); assertEquals(23, selector.length()); assertEquals(new Address(compute, new AddressInfo.BuilderImpl(GLOBAL_ADDRESS)), address); }
@Test public void testDeleteNull() { initializeExpectedAddress(3); expect(compute.getOptions()).andReturn(mockOptions); expect(compute.deleteAddress(REGION_ADDRESS_ID)).andReturn(null); replay(compute); initializeAddress(); assertNull(address.delete()); }
@Test public void testCreateGetAndDeleteGlobalAddress() throws InterruptedException, TimeoutException { String name = BASE_RESOURCE_NAME + "create-and-get-global-address"; AddressId addressId = GlobalAddressId.of(name); AddressInfo addressInfo = AddressInfo.of(addressId); Operation operation = compute.create(addressInfo); operation.waitFor(); // test get Address remoteAddress = compute.getAddress(addressId); resourceCleaner.add(addressId); assertNotNull(remoteAddress); assertTrue(remoteAddress.getAddressId() instanceof GlobalAddressId); assertEquals(addressId.getAddress(), remoteAddress.getAddressId().getAddress()); assertNotNull(remoteAddress.getAddress()); assertNotNull(remoteAddress.getCreationTimestamp()); assertNotNull(remoteAddress.getGeneratedId()); assertNotNull(remoteAddress.getStatus()); // test get with selected fields remoteAddress = compute.getAddress(addressId, Compute.AddressOption.fields()); assertNotNull(remoteAddress); assertTrue(remoteAddress.getAddressId() instanceof GlobalAddressId); assertEquals(addressId.getAddress(), remoteAddress.getAddressId().getAddress()); assertNull(remoteAddress.getAddress()); assertNull(remoteAddress.getCreationTimestamp()); assertNull(remoteAddress.getGeneratedId()); operation = remoteAddress.delete(); operation.waitFor(); resourceCleaner.remove(addressId); assertNull(compute.getAddress(addressId)); }
.setNetworkInterfaces(networkInterface) .build(); Operation instanceOperation = compute.create(instanceInfo); AddressId addressId = RegionAddressId.of(REGION, addressName); AddressInfo addressInfo = AddressInfo.of(addressId); Operation addressOperation = compute.create(addressInfo); addressOperation.waitFor(); instanceOperation.waitFor(); Address remoteAddress = compute.getAddress(addressId); resourceCleaner.add(addressId); Instance remoteInstance = compute.getInstance(instanceId); .setNatIp(remoteAddress.getAddress()) .setName("NAT") .build();
Compute.AddressFilter.equals(Compute.AddressField.NAME, prefix + "\\d"); Page<Address> addressPage = compute.listRegionAddresses(REGION, Compute.AddressListOption.filter(filter)); Iterator<Address> addressIterator = addressPage.iterateAll().iterator(); int count = 0; while (addressIterator.hasNext()) { Address address = addressIterator.next(); assertNotNull(address.getAddressId()); assertTrue(address.getAddressId() instanceof RegionAddressId); assertEquals(REGION, address.<RegionAddressId>getAddressId().getRegion()); assertTrue(addressSet.contains(address.getAddressId().getAddress())); assertNotNull(address.getAddress()); assertNotNull(address.getCreationTimestamp()); assertNotNull(address.getGeneratedId()); count++; while (addressIterator.hasNext()) { Address address = addressIterator.next(); assertTrue(address.getAddressId() instanceof RegionAddressId); assertEquals(REGION, address.<RegionAddressId>getAddressId().getRegion()); assertTrue(addressSet.contains(address.getAddressId().getAddress())); assertNotNull(address.getAddress()); assertNull(address.getCreationTimestamp()); assertNull(address.getGeneratedId()); assertNull(address.getStatus()); assertNull(address.getUsage()); count++;
Compute.AddressFilter.equals(Compute.AddressField.NAME, prefix + "\\d"); Page<Address> addressPage = compute.listGlobalAddresses(Compute.AddressListOption.filter(filter)); Iterator<Address> addressIterator = addressPage.iterateAll().iterator(); int count = 0; while (addressIterator.hasNext()) { Address address = addressIterator.next(); assertNotNull(address.getAddressId()); assertTrue(address.getAddressId() instanceof GlobalAddressId); assertTrue(addressSet.contains(address.getAddressId().getAddress())); assertNotNull(address.getAddress()); assertNotNull(address.getCreationTimestamp()); assertNotNull(address.getGeneratedId()); count++; while (addressIterator.hasNext()) { Address address = addressIterator.next(); assertTrue(address.getAddressId() instanceof GlobalAddressId); assertTrue(addressSet.contains(address.getAddressId().getAddress())); assertNotNull(address.getAddress()); assertNull(address.getCreationTimestamp()); assertNull(address.getGeneratedId()); assertNull(address.getStatus()); assertNull(address.getUsage()); count++;
@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); }
Operation operation = compute.create(AddressInfo.of(addressId)); ImageDiskConfiguration diskConfiguration = ImageDiskConfiguration.of(imageId); DiskInfo disk = DiskInfo.of(diskId, diskConfiguration); operation = compute.create(disk); Address externalIp = compute.getAddress(addressId); InstanceId instanceId = InstanceId.of("us-central1-a", "test-instance"); NetworkId networkId = NetworkId.of("default"); NetworkInterface networkInterface = NetworkInterface.newBuilder(networkId) .setAccessConfigurations(AccessConfig.of(externalIp.getAddress())) .build(); MachineTypeId machineTypeId = MachineTypeId.of("us-central1-a", "n1-standard-1");
/** * Fetches the current address' latest information. Returns {@code null} if the address does not * exist. * * @param options address options * @return an {@code Address} object with latest information or {@code null} if not found * @throws ComputeException upon failure */ public Address reload(AddressOption... options) { return compute.getAddress(getAddressId(), options); }
/** * Deletes this address. * * @return an {@code Operation} object if delete request was successfully sent, {@code null} if * the address was not found * @throws ComputeException upon failure */ public Operation delete(OperationOption... options) { return compute.deleteAddress(getAddressId(), options); }
@Test public void testToAndFromPb() { initializeExpectedAddress(20); compareAddress( globalForwardingAddress, Address.fromPb(serviceMockReturnsOptions, globalForwardingAddress.toPb())); compareAddress( regionForwardingAddress, Address.fromPb(serviceMockReturnsOptions, regionForwardingAddress.toPb())); compareAddress( instanceAddress, Address.fromPb(serviceMockReturnsOptions, instanceAddress.toPb())); Address address = new Address.Builder(serviceMockReturnsOptions, GLOBAL_ADDRESS_ID).build(); compareAddress(address, Address.fromPb(serviceMockReturnsOptions, address.toPb())); address = new Address.Builder(serviceMockReturnsOptions, REGION_ADDRESS_ID).build(); compareAddress(address, Address.fromPb(serviceMockReturnsOptions, address.toPb())); }