Issues 158: iso3166Code: use country only if state is missing. Whitespace trimming and null checking

This commit is contained in:
Jason King 2011-09-17 10:35:47 +01:00
parent e7dce95ace
commit a655d8ab25
4 changed files with 112 additions and 10 deletions

View File

@ -22,6 +22,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
import javax.inject.Inject;
import static com.google.common.base.Strings.nullToEmpty;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope;
@ -58,7 +59,16 @@ public class DatacenterToLocation implements Function<Datacenter,Location> {
}
private Iterable<String> createIso3166Codes(Address address) {
return address != null ? ImmutableSet.<String> of("" + address.getCountry() + "-" + address.getState())
: ImmutableSet.<String> of();
if (address== null) return ImmutableSet.<String> of();
final String country = nullToEmpty(address.getCountry()).trim();
if (country.isEmpty()) return ImmutableSet.<String> of();
final String state = nullToEmpty(address.getState()).trim();
if (state.isEmpty()) return ImmutableSet.<String> of(address.getCountry());
return ImmutableSet.<String> of("" + country + "-" + state);
}
}

View File

@ -18,6 +18,9 @@
*/
package org.jclouds.softlayer.domain;
import static com.google.common.base.Strings.emptyToNull;
import static com.google.common.base.Preconditions.checkNotNull;
/**
*
* @author Jason King
@ -79,7 +82,7 @@ public class Address implements Comparable<Address> {
public Address(long id, String country, String state, String description) {
this.id = id;
this.country = country;
this.country = checkNotNull(emptyToNull(country),"country cannot be null or empty:"+country);
this.state = state;
this.description = description;
}

View File

@ -50,12 +50,19 @@ public class DatacenterToLocationTest {
@Test
public void testDatacenterToLocation() {
Datacenter address = Datacenter.builder().id(1).longName("This is Texas!").locationAddress(
Address.builder().country("US").state("TX").description("This is Texas!").build()).build();
Address address = Address.builder().country("US")
.state("TX")
.description("This is Texas!")
.build();
Location location = function.apply(address);
Datacenter datacenter = Datacenter.builder().id(1)
.longName("Texas Datacenter")
.locationAddress(address)
.build();
assertEquals(location.getId(), Long.toString(address.getId()));
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"));
@ -63,11 +70,53 @@ public class DatacenterToLocationTest {
@Test
public void testGetIso3166CodeNoCountryAndState() {
Datacenter address = Datacenter.builder().id(1).longName("Nowhere").build();
Location location = function.apply(address);
Datacenter datacenter = Datacenter.builder().id(1)
.longName("Nowhere")
.build();
assertEquals(location.getId(), Long.toString(address.getId()));
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"));
}
}

View File

@ -0,0 +1,40 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you 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 org.jclouds.softlayer.domain;
import org.testng.annotations.Test;
/**
* Tests {@code Address}
*
* @author Jason King
*/
@Test(singleThreaded = true, groups = "unit")
public class AddressTest {
@Test(expectedExceptions = java.lang.NullPointerException.class )
public void testConstructionWithEmpty() {
Address.builder().country(null).build();
}
@Test(expectedExceptions = java.lang.NullPointerException.class )
public void testConstructionWithNoCountry() {
Address.builder().country("").build();
}
}