From 8fcd636f3ff85ab0cdd2cb62a4851642e8531976 Mon Sep 17 00:00:00 2001 From: Jason King Date: Fri, 23 Sep 2011 10:52:38 +0100 Subject: [PATCH] Issue 158: Added unit test for ProductOrderToJson --- .../softlayer/binders/ProductOrderToJson.java | 3 + .../binders/ProductOrderToJsonTest.java | 81 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/binders/ProductOrderToJsonTest.java diff --git a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/binders/ProductOrderToJson.java b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/binders/ProductOrderToJson.java index 06a626c94c..c43c2e326e 100644 --- a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/binders/ProductOrderToJson.java +++ b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/binders/ProductOrderToJson.java @@ -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 bindToRequest(R request, Object input) { + checkNotNull(input,"order"); ProductOrder order = ProductOrder.class.cast(input); request.setPayload(buildJson(order)); return request; diff --git a/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/binders/ProductOrderToJsonTest.java b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/binders/ProductOrderToJsonTest.java new file mode 100644 index 0000000000..c19c99bd39 --- /dev/null +++ b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/binders/ProductOrderToJsonTest.java @@ -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); + + } +}