Issue 158: Added unit test for ProductOrderToJson

This commit is contained in:
Jason King 2011-09-23 10:52:38 +01:00
parent d9b4f2746f
commit 8fcd636f3f
2 changed files with 84 additions and 0 deletions

View File

@ -33,6 +33,8 @@ import org.jclouds.softlayer.domain.VirtualGuest;
import javax.inject.Inject;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Converts a ProductOrder into a json string
* valid for placing an order via the softlayer api
@ -51,6 +53,7 @@ public class ProductOrderToJson implements Binder {
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object input) {
checkNotNull(input,"order");
ProductOrder order = ProductOrder.class.cast(input);
request.setPayload(buildJson(order));
return request;

View File

@ -0,0 +1,81 @@
package org.jclouds.softlayer.binders;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.gson.Gson;
import org.jclouds.http.HttpRequest;
import org.jclouds.json.Json;
import org.jclouds.json.internal.GsonWrapper;
import org.jclouds.rest.Binder;
import org.jclouds.softlayer.domain.ProductItemPrice;
import org.jclouds.softlayer.domain.ProductOrder;
import org.jclouds.softlayer.domain.VirtualGuest;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
import org.testng.collections.Sets;
import java.net.URI;
import static org.testng.Assert.assertEquals;
/**
* Tests behavior of {@code ProductOrderToJsonTest}
*
* @author Jason King
*/
@Test(groups = "unit")
public class ProductOrderToJsonTest {
private static final String FORMAT =
"{'parameters':[{'complexType':'SoftLayer_Container_Product_Order_Virtual_Guest'," +
"'packageId':%d," +
"'location':'%s'," +
"'prices':[{'id':%d},{'id':%d}]," +
"'virtualGuests':[{'hostname':'%s','domain':'%s'}]," +
"'quantity':%d," +
"'useHourlyPricing':%b}" +
"]}";
private HttpRequest request;
private Binder binder;
@BeforeGroups(groups = { "unit" })
public void setup() {
request = HttpRequest.builder().method("GET").endpoint(URI.create("http://momma")).build();
Json json = new GsonWrapper(new Gson());
binder = new ProductOrderToJson(json);
}
@Test(expectedExceptions = NullPointerException.class)
public void testNullOrder() {
binder.bindToRequest(request, null);
}
@Test
public void testCorrect() {
ProductItemPrice price1 = ProductItemPrice.builder().id(100).build();
ProductItemPrice price2 = ProductItemPrice.builder().id(101).build();
VirtualGuest guest = VirtualGuest.builder().hostname("myhost")
.domain("mydomain")
.build();
ProductOrder order = ProductOrder.builder()
.packageId(123)
.location("loc456")
.quantity(99)
.useHourlyPricing(true)
.prices(ImmutableSet.of(price1,price2))
.virtualGuest(guest)
.build();
String expected = String.format(FORMAT.replaceAll("'","\""),
123,"loc456",100,101,"myhost","mydomain",99,true);
HttpRequest req = binder.bindToRequest(request, order);
assertEquals(req.getPayload().getRawContent(), expected);
}
}