Created AccountToLocation function class and added description to Address

This commit is contained in:
Jason King 2011-09-16 21:30:31 +01:00
parent 43141bcf78
commit af5cd63dfc
4 changed files with 140 additions and 53 deletions

View File

@ -33,6 +33,7 @@ public class Address implements Comparable<Address> {
private long id = -1;
private String country;
private String state;
private String description;
public Builder id(long id) {
this.id = id;
@ -49,28 +50,38 @@ public class Address implements Comparable<Address> {
return this;
}
public Builder description(String description) {
this.description = description;
return this;
}
public Address build() {
return new Address(id, country, state);
return new Address(id, country, state, description);
}
public static Builder fromAddress(Address in) {
return Address.builder().id(in.getId()).country(in.getCountry()).state(in.getState());
return Address.builder().id(in.getId())
.country(in.getCountry())
.state(in.getState())
.description(in.getDescription());
}
}
private long id = -1;
private String country;
private String state;
private String description;
// for deserializer
Address() {
}
public Address(long id, String country, String state) {
public Address(long id, String country, String state, String description) {
this.id = id;
this.country = country;
this.state = state;
this.description = description;
}
@Override
@ -100,12 +111,11 @@ public class Address implements Comparable<Address> {
}
/**
* @return TThe iso3166 code.
* This is a derived value of the form 'country-state'
* @return The description of the address.
*/
public String getIso3166Code() {
return ""+country+"-"+state;
}
public String getDescription() {
return description;
}
public Builder toBuilder() {
return Builder.fromAddress(this);
@ -135,7 +145,7 @@ public class Address implements Comparable<Address> {
@Override
public String toString() {
return "[id=" + id + ", country=" + country + ", state=" + state + "]";
return "[id=" + id + ", country=" + country + ", state=" + state + ", description=" + description + "]";
}

View File

@ -0,0 +1,50 @@
/**
* 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.functions;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope;
import org.jclouds.softlayer.domain.Address;
import javax.annotation.Nullable;
/**
* Converts an Address into a Location.
*/
public class AccountToLocation implements Function<Address,Location> {
@Override
public Location apply(Address address) {
return new LocationBuilder().scope(LocationScope.ZONE)
.metadata(ImmutableMap.<String, Object>of())
.description(address.getDescription())
.id(Long.toString(address.getId()))
.iso3166Codes(createIso3166Codes(address))
.build();
}
private Iterable<String> createIso3166Codes(Address address) {
return ImmutableSet.of(""+address.getCountry()+"-"+address.getState());
}
}

View File

@ -1,44 +0,0 @@
/**
* 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;
import static org.testng.AssertJUnit.assertEquals;
/**
* Tests {@code Address}
*
* @author Jason King
*/
@Test(sequential = true,groups = "unit")
public class AddressTest {
@Test
public void testGetIso3166Code() {
Address address = Address.builder().id(1).country("US").state("TX").build();
assertEquals(address.getIso3166Code(),"US-TX");
}
@Test
public void testGetIso3166CodeNullData() {
Address address = Address.builder().id(1).build();
assertEquals(address.getIso3166Code(),"null-null");
}
}

View File

@ -0,0 +1,71 @@
/**
* 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.functions;
import org.jclouds.domain.Location;
import org.jclouds.softlayer.domain.Address;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.Set;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;
/**
* Tests {@code AddressToLocation}
*
* @author Jason King
*/
@Test(sequential = true,groups = "unit")
public class AddressToLocationTest {
private AccountToLocation function;
@BeforeMethod
public void setup() {
function = new AccountToLocation();
}
@Test
public void testAddressToLocation() {
Address address = Address.builder().id(1)
.country("US")
.state("TX")
.description("This is Texas!").build();
Location location = function.apply(address);
assertEquals(location.getId(), Long.toString(address.getId()));
Set<String> iso3166Codes = location.getIso3166Codes();
assertEquals(iso3166Codes.size(),1);
assertTrue(iso3166Codes.contains("US-TX"));
}
@Test
public void testGetIso3166CodeNoCountryAndState() {
Address address = Address.builder().id(1)
.description("Nowhere").build();
Location location = function.apply(address);
assertEquals(location.getId(), Long.toString(address.getId()));
Set<String> iso3166Codes = location.getIso3166Codes();
assertEquals(iso3166Codes.size(),1);
assertTrue(iso3166Codes.contains("null-null"));
}
}