DatacenterToLocation implements Function<Datacenter,Location> { @Override public Location apply(Datacenter datacenter) { return new LocationBuilder().scope(LocationScope.ZONE) .metadata(ImmutableMap.<String, Object>of()) .description(datacenter.getLongName()) .id(Long.toString(datacenter.getId())) .iso3166Codes(createIso3166Codes(datacenter.getLocationAddress())) .parent(Iterables.getOnlyElement(provider.get())) .build(); } @Inject  DatacenterToLocation(JustProvider provider); @Override Location apply(Datacenter datacenter);  }
@Test public void testGetIso3166CodeNoCountryAndState() { Datacenter datacenter = Datacenter.builder().id(1).longName("Nowhere").build(); Location location = function.apply(datacenter); assertEquals(location.getId(), Long.toString(datacenter.getId())); Set<String> iso3166Codes = location.getIso3166Codes(); assertEquals(iso3166Codes.size(), 0); }
@Test public void testGetIso3166CodeCountryOnly() { Address address = Address.builder().country("US").description("This is North America!").build(); Datacenter datacenter = Datacenter.builder().id(1).longName("Nowhere").locationAddress(address).build(); Location location = function.apply(datacenter); assertEquals(location.getId(), Long.toString(datacenter.getId())); Set<String> iso3166Codes = location.getIso3166Codes(); assertEquals(iso3166Codes.size(), 1); assertTrue(iso3166Codes.contains("US")); }
@Test public void testGetIso3166CodeWhitespaceTrimmer() { Address address = Address.builder().country(" US ").state(" TX ").description("This is spaced out Texas") .build(); Datacenter datacenter = Datacenter.builder().id(1).longName("Nowhere").locationAddress(address).build(); Location location = function.apply(datacenter); assertEquals(location.getId(), Long.toString(datacenter.getId())); Set<String> iso3166Codes = location.getIso3166Codes(); assertEquals(iso3166Codes.size(), 1); assertTrue(iso3166Codes.contains("US-TX")); }