Issue 158: Datacenter is more closely related to Location than Address

This commit is contained in:
Adrian Cole 2011-09-16 16:56:13 -07:00
parent e80786a1b2
commit 626b45ee46
2 changed files with 26 additions and 22 deletions

View File

@ -22,27 +22,29 @@ import org.jclouds.domain.Location;
import org.jclouds.domain.LocationBuilder; import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope; import org.jclouds.domain.LocationScope;
import org.jclouds.softlayer.domain.Address; import org.jclouds.softlayer.domain.Address;
import org.jclouds.softlayer.domain.Datacenter;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
/** /**
* Converts an Address into a Location. * Converts an Datacenter into a Location.
*/ */
public class AccountToLocation implements Function<Address,Location> { public class DatacenterToLocation implements Function<Datacenter,Location> {
@Override @Override
public Location apply(Address address) { public Location apply(Datacenter datacenter) {
return new LocationBuilder().scope(LocationScope.ZONE) return new LocationBuilder().scope(LocationScope.ZONE)
.metadata(ImmutableMap.<String, Object>of()) .metadata(ImmutableMap.<String, Object>of())
.description(address.getDescription()) .description(datacenter.getLongName())
.id(Long.toString(address.getId())) .id(Long.toString(datacenter.getId()))
.iso3166Codes(createIso3166Codes(address)) .iso3166Codes(createIso3166Codes(datacenter.getLocationAddress()))
.build(); .build();
} }
private Iterable<String> createIso3166Codes(Address address) { private Iterable<String> createIso3166Codes(Address address) {
return ImmutableSet.of(""+address.getCountry()+"-"+address.getState()); return address != null ? ImmutableSet.<String> of("" + address.getCountry() + "-" + address.getState())
} : ImmutableSet.<String> of();
}
} }

View File

@ -20,6 +20,7 @@ package org.jclouds.softlayer.compute.functions;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
import org.jclouds.softlayer.domain.Address; import org.jclouds.softlayer.domain.Address;
import org.jclouds.softlayer.domain.Datacenter;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -29,43 +30,44 @@ import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue; import static org.testng.AssertJUnit.assertTrue;
/** /**
* Tests {@code AddressToLocation} * Tests {@code DatacenterToLocation}
* *
* @author Jason King * @author Jason King
*/ */
@Test(singleThreaded = true,groups = "unit") @Test(singleThreaded = true,groups = "unit")
public class AddressToLocationTest { public class DatacenterToLocationTest {
private AccountToLocation function; private DatacenterToLocation function;
@BeforeMethod @BeforeMethod
public void setup() { public void setup() {
function = new AccountToLocation(); function = new DatacenterToLocation();
} }
@Test @Test
public void testAddressToLocation() { public void testDatacenterToLocation() {
Address address = Address.builder().id(1) Datacenter address = Datacenter.builder().id(1)
.longName("This is Texas!")
.locationAddress(Address.builder()
.country("US") .country("US")
.state("TX") .state("TX")
.description("This is Texas!").build(); .description("This is Texas!").build()).build();
Location location = function.apply(address); Location location = function.apply(address);
assertEquals(location.getId(), Long.toString(address.getId())); assertEquals(location.getId(), Long.toString(address.getId()));
Set<String> iso3166Codes = location.getIso3166Codes(); Set<String> iso3166Codes = location.getIso3166Codes();
assertEquals(iso3166Codes.size(),1); assertEquals(iso3166Codes.size(), 1);
assertTrue(iso3166Codes.contains("US-TX")); assertTrue(iso3166Codes.contains("US-TX"));
} }
@Test @Test
public void testGetIso3166CodeNoCountryAndState() { public void testGetIso3166CodeNoCountryAndState() {
Address address = Address.builder().id(1) Datacenter address = Datacenter.builder().id(1)
.description("Nowhere").build(); .longName("Nowhere").build();
Location location = function.apply(address); Location location = function.apply(address);
assertEquals(location.getId(), Long.toString(address.getId())); assertEquals(location.getId(), Long.toString(address.getId()));
Set<String> iso3166Codes = location.getIso3166Codes(); Set<String> iso3166Codes = location.getIso3166Codes();
assertEquals(iso3166Codes.size(),1); assertEquals(iso3166Codes.size(), 0);
assertTrue(iso3166Codes.contains("null-null"));
} }
} }