* 'master' of https://github.com/jsonking/jclouds:
  Refactored SoftLayerUtils to ProductPackagePredicates
  Created AccountToLocation function class and added description to Address
  Issue 158 added Address class and included in api calls. Provides iso3166 code
  Fixed @see DatacenterClient
  Issue 158: Extracted getProductPackageId to SoftLayerUtils and added tests
  ProductPackageClientLiveTest used Cloud Server pkg name instead of id
This commit is contained in:
Adrian Cole 2011-09-16 16:09:18 -07:00
commit 2d4bcb6e08
11 changed files with 474 additions and 61 deletions

View File

@ -0,0 +1,152 @@
/**
* 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;
/**
*
* @author Jason King
* @see <a href= "http://sldn.softlayer.com/reference/datatypes/SoftLayer_Account_Address"
* />
*/
public class Address implements Comparable<Address> {
public static Builder builder() {
return new Builder();
}
public static class Builder {
private long id = -1;
private String country;
private String state;
private String description;
public Builder id(long id) {
this.id = id;
return this;
}
public Builder country(String country) {
this.country = country;
return this;
}
public Builder state(String state) {
this.state = state;
return this;
}
public Builder description(String description) {
this.description = description;
return this;
}
public Address build() {
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())
.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, String description) {
this.id = id;
this.country = country;
this.state = state;
this.description = description;
}
@Override
public int compareTo(Address arg0) {
return new Long(id).compareTo(arg0.getId());
}
/**
* @return The unique id of the address.
*/
public long getId() {
return id;
}
/**
* @return The country of the address.
*/
public String getCountry() {
return country;
}
/**
* @return The state of the address.
*/
public String getState() {
return state;
}
/**
* @return The description of the address.
*/
public String getDescription() {
return description;
}
public Builder toBuilder() {
return Builder.fromAddress(this);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (id ^ (id >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Address other = (Address) obj;
if (id != other.id)
return false;
return true;
}
@Override
public String toString() {
return "[id=" + id + ", country=" + country + ", state=" + state + ", description=" + description + "]";
}
}

View File

@ -33,6 +33,7 @@ public class Datacenter implements Comparable<Datacenter> {
private long id = -1;
private String name;
private String longName;
private Address locationAddress;
public Builder id(long id) {
this.id = id;
@ -49,28 +50,35 @@ public class Datacenter implements Comparable<Datacenter> {
return this;
}
public Builder locationAddress(Address locationAddress) {
this.locationAddress = locationAddress;
return this;
}
public Datacenter build() {
return new Datacenter(id, name, longName);
return new Datacenter(id, name, longName, locationAddress);
}
public static Builder fromDatacenter(Datacenter in) {
return Datacenter.builder().id(in.getId()).name(in.getName()).longName(in.getLongName());
return Datacenter.builder().id(in.getId()).name(in.getName()).longName(in.getLongName()).locationAddress(in.getLocationAddress());
}
}
private long id = -1;
private String name;
private String longName;
private Address locationAddress;
// for deserializer
Datacenter() {
}
public Datacenter(long id, String name, String longName) {
public Datacenter(long id, String name, String longName, Address locationAddress) {
this.id = id;
this.name = name;
this.longName = longName;
this.locationAddress = locationAddress;
}
@Override
@ -99,6 +107,13 @@ public class Datacenter implements Comparable<Datacenter> {
return longName;
}
/**
* @return A location's physical address (optional).
*/
public Address getLocationAddress() {
return locationAddress;
}
public Builder toBuilder() {
return Builder.fromDatacenter(this);
}
@ -127,7 +142,7 @@ public class Datacenter implements Comparable<Datacenter> {
@Override
public String toString() {
return "[id=" + id + ", name=" + name + ", longName=" + longName + "]";
return "[id=" + id + ", country=" + name + ", state=" + longName + "], locationAddress=" + locationAddress + "]";
}

View File

@ -49,7 +49,7 @@ import com.google.common.util.concurrent.ListenableFuture;
public interface DatacenterAsyncClient {
/**
* @see LocationDatacenterClient#listDatacenters
* @see DatacenterClient#listDatacenters
*/
@GET
@Path("/SoftLayer_Location_Datacenter/Datacenters.json")
@ -59,7 +59,7 @@ public interface DatacenterAsyncClient {
ListenableFuture<Set<Datacenter>> listDatacenters();
/**
* @see LocationDatacenterClient#getLocationDatacenter
* @see DatacenterClient#getDatacenter
*/
@GET
@Path("/SoftLayer_Location_Datacenter/{id}.json")

View File

@ -43,7 +43,7 @@ import javax.ws.rs.core.MediaType;
@RequestFilters(BasicAuthentication.class)
@Path("/v{jclouds.api-version}")
public interface ProductPackageAsyncClient {
public static String PRODUCT_MASK = "items;locations";
public static String PRODUCT_MASK = "items;locations.locationAddress";
/**
* @see ProductPackageClient#getProductPackage

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

@ -0,0 +1,55 @@
/**
* 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.predicates;
import com.google.common.base.Predicate;
import org.jclouds.softlayer.domain.ProductPackage;
import org.jclouds.softlayer.features.AccountClient;
import java.util.NoSuchElementException;
public class ProductPackagePredicates {
/**
* Attempts to obtain the packageId for the supplied packageName.
* @param accountClient @see AccountClient
* @param packageName The name field of the @see ProductPackage to find
* @return The id of the package or null if no match found
*/
public static Long getProductPackageId(AccountClient accountClient,String packageName) {
for (ProductPackage productPackage : accountClient.getActivePackages()) {
if (named(packageName).apply(productPackage)) return productPackage.getId();
}
throw new NoSuchElementException("ProductPackage:"+packageName+" not found");
}
/**
* Tests if the product package name equals the packageName
* @param packageName
* @return true if the name is equal, otherwise false.
*/
public static Predicate named(final String packageName) {
return new Predicate<ProductPackage>() {
public boolean apply(ProductPackage productPackage) {
return productPackage.getName().equals(packageName);
}
};
}
}

View File

@ -23,6 +23,7 @@ import static org.testng.Assert.assertTrue;
import java.util.Set;
import org.jclouds.softlayer.domain.Address;
import org.jclouds.softlayer.domain.Datacenter;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
@ -57,30 +58,44 @@ public class DatacenterClientLiveTest extends BaseSoftLayerClientLiveTest {
}
}
private void checkDatacenter(Datacenter vg) {
assert vg.getId() > 0 : vg;
assert vg.getName() != null : vg;
assert vg.getLongName() != null : vg;
}
@Test
public void testListDatacentersContent() {
Builder<Datacenter> expected = ImmutableSet.<Datacenter> builder();
expected.add(Datacenter.builder().id(3).name("dal01").longName("Dallas").build());
expected.add(Datacenter.builder().id(18171).name("sea01").longName("Seattle").build());
expected.add(Datacenter.builder().id(168642).name("sjc01").longName("San Jose 1").build());
expected.add(Datacenter.builder().id(2).name("dal00").longName("Corporate HQ").build());
expected.add(Datacenter.builder().id(37473).name("wdc01").longName("Washington, DC").build());
expected.add(Datacenter.builder().id(154770).name("dal02").longName("Dallas 2").build());
expected.add(Datacenter.builder().id(138124).name("dal05").longName("Dallas 5").build());
expected.add(Datacenter.builder().id(167093).name("hou01").longName("Houston 1").build());
expected.add(Datacenter.builder().id(167094).name("lon01").longName("London 1").build());
expected.add(Datacenter.builder().id(167092).name("dal04").longName("Dallas 4").build());
expected.add(Datacenter.builder().id(224092).name("sng01").longName("Singapore 1").build());
expected.add(Datacenter.builder().id(142775).name("hou02").longName("Houston 2").build());
expected.add(Datacenter.builder().id(142776).name("dal07").longName("Dallas 7").build());
expected.add(Datacenter.builder().id(154820).name("dal06").longName("Dallas 6").build());
Builder<Datacenter> builder = ImmutableSet.<Datacenter> builder();
builder.add(Datacenter.builder().id(3).name("dal01").longName("Dallas").build());
builder.add(Datacenter.builder().id(18171).name("sea01").longName("Seattle").build());
builder.add(Datacenter.builder().id(168642).name("sjc01").longName("San Jose 1").build());
builder.add(Datacenter.builder().id(2).name("dal00").longName("Corporate HQ").build());
builder.add(Datacenter.builder().id(37473).name("wdc01").longName("Washington, DC").build());
builder.add(Datacenter.builder().id(154770).name("dal02").longName("Dallas 2").build());
builder.add(Datacenter.builder().id(138124).name("dal05").longName("Dallas 5").build());
builder.add(Datacenter.builder().id(167093).name("hou01").longName("Houston 1").build());
builder.add(Datacenter.builder().id(167094).name("lon01").longName("London 1").build());
builder.add(Datacenter.builder().id(167092).name("dal04").longName("Dallas 4").build());
builder.add(Datacenter.builder().id(224092).name("sng01").longName("Singapore 1").build());
builder.add(Datacenter.builder().id(142775).name("hou02").longName("Houston 2").build());
builder.add(Datacenter.builder().id(142776).name("dal07").longName("Dallas 7").build());
builder.add(Datacenter.builder().id(154820).name("dal06").longName("Dallas 6").build());
Set<Datacenter> response = client.listDatacenters();
assertEquals(response.toString(), expected.build().toString());
Set<Datacenter> expected = builder.build();
assertEquals(response.size(),expected.size());
assertTrue(response.containsAll(expected));
for( Datacenter datacenter: response) {
Address address = datacenter.getLocationAddress();
if(address!=null) checkAddress(address);
}
}
private void checkDatacenter(Datacenter dc) {
assert dc.getId() > 0 : dc;
assert dc.getName() != null : dc;
assert dc.getLongName() != null : dc;
}
private void checkAddress(Address address) {
assert address.getId() >0 : address;
assert address.getCountry() != null : address;
assert address.getState() != null : address;
}
}

View File

@ -43,7 +43,7 @@ public class ProductPackageAsyncClientTest extends BaseSoftLayerAsyncClientTest<
assertRequestLineEquals(
httpRequest,
"GET https://api.softlayer.com/rest/v3/SoftLayer_Product_Package/1234.json?objectMask=items%3Blocations HTTP/1.1");
"GET https://api.softlayer.com/rest/v3/SoftLayer_Product_Package/1234.json?objectMask=items%3Blocations.locationAddress HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false);

View File

@ -18,19 +18,16 @@
*/
package org.jclouds.softlayer.features;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import com.google.common.collect.ImmutableSet;
import org.jclouds.softlayer.domain.Datacenter;
import org.jclouds.softlayer.domain.ProductItem;
import org.jclouds.softlayer.domain.ProductItemPrice;
import org.jclouds.softlayer.domain.ProductPackage;
import org.jclouds.softlayer.domain.*;
import org.jclouds.softlayer.predicates.ProductPackagePredicates;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
import java.util.Set;
import static org.testng.Assert.*;
/**
* Tests behavior of {@code ProductPackageClient}
*
@ -42,13 +39,17 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
public void setupClient() {
super.setupClient();
client = context.getApi().getProductPackageClient();
accountClient = context.getApi().getAccountClient();
}
private static final String CLOUD_SERVER_PACKAGE_NAME = "Cloud Server";
private ProductPackageClient client;
private AccountClient accountClient;
@Test
public void testGetProductPackage() {
for (ProductPackage productPackage : context.getApi().getAccountClient().getActivePackages()) {
for (ProductPackage productPackage : accountClient.getActivePackages()) {
ProductPackage response = client.getProductPackage(productPackage.getId());
assert null != response;
@ -72,7 +73,6 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
@Test
public void testDatacentersForCloudLayer() {
ProductPackage productPackage = context.getApi().getProductPackageClient().getProductPackage(getCloudLayerPackageId());
ImmutableSet.Builder<Datacenter> builder = ImmutableSet.builder();
builder.add(Datacenter.builder().id(3).name("dal01").longName("Dallas").build());
@ -83,14 +83,19 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
Set<Datacenter> expected = builder.build();
Long productPackageId = ProductPackagePredicates.getProductPackageId(accountClient, CLOUD_SERVER_PACKAGE_NAME);
assertNotNull(productPackageId);
ProductPackage productPackage = client.getProductPackage(productPackageId);
Set<Datacenter> datacenters = productPackage.getDatacenters();
assertEquals(datacenters.size(), expected.size());
assertTrue(datacenters.containsAll(expected));
}
// TODO The packageId will be obtained via a search call later.
private int getCloudLayerPackageId() {
return 46;
for(Datacenter dataCenter: datacenters) {
Address address = dataCenter.getLocationAddress();
assertNotNull(address);
checkAddress(address);
}
}
private void checkProductItem(ProductItem item) {
@ -120,4 +125,9 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
assert datacenter.getLongName() != null : datacenter;
}
private void checkAddress(Address address) {
assert address.getId() >0 : address;
assert address.getCountry() != null : address;
assert address.getState() != null : address;
}
}

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"));
}
}

View File

@ -0,0 +1,45 @@
/**
* 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.predicates;
import org.jclouds.softlayer.domain.ProductPackage;
import org.testng.annotations.Test;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertFalse;
/**
* Tests {@code ProductPackagePredicates}
*
* @author Jason King
*/
@Test(sequential = true,groups = "unit")
public class ProductPackagePredicatesTest {
@Test
public void testMatches() {
ProductPackage productPackage = ProductPackage.builder().name("foo").build();
assertTrue(ProductPackagePredicates.named("foo").apply(productPackage));
}
@Test
public void testDoesNotMatch() {
ProductPackage productPackage = ProductPackage.builder().name("foo").build();
assertFalse(ProductPackagePredicates.named("bar").apply(productPackage));
}
}