From af5cd63dfc96a5693acb491cf15aab5121242a79 Mon Sep 17 00:00:00 2001 From: Jason King Date: Fri, 16 Sep 2011 21:30:31 +0100 Subject: [PATCH] Created AccountToLocation function class and added description to Address --- .../org/jclouds/softlayer/domain/Address.java | 28 +++++--- .../functions/AccountToLocation.java | 50 +++++++++++++ .../jclouds/softlayer/domain/AddressTest.java | 44 ------------ .../functions/AddressToLocationTest.java | 71 +++++++++++++++++++ 4 files changed, 140 insertions(+), 53 deletions(-) create mode 100644 sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/functions/AccountToLocation.java delete mode 100644 sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/domain/AddressTest.java create mode 100644 sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/functions/AddressToLocationTest.java diff --git a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/domain/Address.java b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/domain/Address.java index 6b2ca9f340..b3d632d62b 100644 --- a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/domain/Address.java +++ b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/domain/Address.java @@ -33,6 +33,7 @@ public class Address implements Comparable
{ 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
{ 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
{ } /** - * @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
{ @Override public String toString() { - return "[id=" + id + ", country=" + country + ", state=" + state + "]"; + return "[id=" + id + ", country=" + country + ", state=" + state + ", description=" + description + "]"; } diff --git a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/functions/AccountToLocation.java b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/functions/AccountToLocation.java new file mode 100644 index 0000000000..a900da9250 --- /dev/null +++ b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/functions/AccountToLocation.java @@ -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 { + + @Override + public Location apply(Address address) { + return new LocationBuilder().scope(LocationScope.ZONE) + .metadata(ImmutableMap.of()) + .description(address.getDescription()) + .id(Long.toString(address.getId())) + .iso3166Codes(createIso3166Codes(address)) + .build(); + } + + private Iterable createIso3166Codes(Address address) { + return ImmutableSet.of(""+address.getCountry()+"-"+address.getState()); + } +} diff --git a/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/domain/AddressTest.java b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/domain/AddressTest.java deleted file mode 100644 index 3b345e2414..0000000000 --- a/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/domain/AddressTest.java +++ /dev/null @@ -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"); - } -} diff --git a/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/functions/AddressToLocationTest.java b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/functions/AddressToLocationTest.java new file mode 100644 index 0000000000..93a48c8bc0 --- /dev/null +++ b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/functions/AddressToLocationTest.java @@ -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 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 iso3166Codes = location.getIso3166Codes(); + assertEquals(iso3166Codes.size(),1); + assertTrue(iso3166Codes.contains("null-null")); + } +}