killbill-aplcache

jaxrs: add test for AccountJson Change also tabs to spaces.

6/7/2012 6:35:44 PM

Details

jaxrs/pom.xml 6(+6 -0)

diff --git a/jaxrs/pom.xml b/jaxrs/pom.xml
index c8fc0cd..da838a5 100644
--- a/jaxrs/pom.xml
+++ b/jaxrs/pom.xml
@@ -45,6 +45,12 @@
             <artifactId>killbill-api</artifactId>
         </dependency>
         <dependency>
+            <groupId>com.ning.billing</groupId>
+            <artifactId>killbill-util</artifactId>
+            <type>test-jar</type>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>joda-time</groupId>
             <artifactId>joda-time</artifactId>
         </dependency>
diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/AccountJson.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/AccountJson.java
index 22ae311..58847fe 100644
--- a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/AccountJson.java
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/AccountJson.java
@@ -16,55 +16,54 @@
 
 package com.ning.billing.jaxrs.json;
 
+import org.joda.time.DateTimeZone;
+
 import com.fasterxml.jackson.annotation.JsonCreator;
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.fasterxml.jackson.annotation.JsonView;
-import org.joda.time.DateTimeZone;
-
 import com.ning.billing.account.api.Account;
 import com.ning.billing.account.api.AccountData;
 import com.ning.billing.catalog.api.Currency;
 
 public class AccountJson extends AccountJsonSimple {
-
     // STEPH Missing city, locale, postalCode from https://home.ninginc.com:8443/display/REVINFRA/Killbill+1.0+APIs
 
     @JsonView(BundleTimelineViews.Base.class)
     private final String name;
-    
+
     @JsonView(BundleTimelineViews.Base.class)
     private final Integer length;
-        
+
     @JsonView(BundleTimelineViews.Base.class)
     private final String email;
-    
+
     @JsonView(BundleTimelineViews.Base.class)
     private final Integer billCycleDay;
-    
+
     @JsonView(BundleTimelineViews.Base.class)
     private final String currency;
-    
+
     @JsonView(BundleTimelineViews.Base.class)
     private final String paymentProvider;
-    
+
     @JsonView(BundleTimelineViews.Base.class)
     private final String timeZone;
-    
+
     @JsonView(BundleTimelineViews.Base.class)
     private final String address1;
-    
+
     @JsonView(BundleTimelineViews.Base.class)
     private final String address2;
-    
+
     @JsonView(BundleTimelineViews.Base.class)
     private final String company;
-    
+
     @JsonView(BundleTimelineViews.Base.class)
     private final String state;
-    
+
     @JsonView(BundleTimelineViews.Base.class)
     private final String country;
-    
+
     @JsonView(BundleTimelineViews.Base.class)
     private final String phone;
 
@@ -85,21 +84,24 @@ public class AccountJson extends AccountJsonSimple {
         this.country = account.getCountry();
         this.phone = account.getPhone();
     }
-    
+
     public AccountData toAccountData() {
         return new AccountData() {
             @Override
             public DateTimeZone getTimeZone() {
                 return (timeZone != null) ? DateTimeZone.forID(timeZone) : null;
             }
+
             @Override
             public String getStateOrProvince() {
                 return state;
             }
+
             @Override
             public String getPostalCode() {
                 return null;
             }
+
             @Override
             public String getPhone() {
                 return phone;
@@ -119,51 +121,62 @@ public class AccountJson extends AccountJsonSimple {
             public String getPaymentProviderName() {
                 return paymentProvider;
             }
+
             @Override
             public String getName() {
                 return name;
             }
+
             @Override
             public String getLocale() {
                 return null;
             }
+
             @Override
             public int getFirstNameLength() {
                 return length;
             }
+
             @Override
             public String getExternalKey() {
                 return externalKey;
             }
+
             @Override
             public String getEmail() {
                 return email;
             }
+
             @Override
             public Currency getCurrency() {
-                Currency result =  (currency != null) ? Currency.valueOf(currency) : Currency.USD;
-                return result;
+                return (currency != null) ? Currency.valueOf(currency) : Currency.USD;
             }
+
             @Override
             public String getCountry() {
                 return country;
             }
+
             @Override
             public String getCompanyName() {
                 return company;
             }
+
             @Override
             public String getCity() {
                 return null;
             }
+
             @Override
             public int getBillCycleDay() {
                 return billCycleDay;
             }
+
             @Override
             public String getAddress2() {
                 return address2;
             }
+
             @Override
             public String getAddress1() {
                 return address1;
@@ -192,20 +205,20 @@ public class AccountJson extends AccountJsonSimple {
 
     @JsonCreator
     public AccountJson(@JsonProperty("accountId") String accountId,
-            @JsonProperty("name") String name,
-            @JsonProperty("firstNameLength") Integer length,
-            @JsonProperty("externalKey") String externalKey,
-            @JsonProperty("email") String email,
-            @JsonProperty("billingDay") Integer billCycleDay,
-            @JsonProperty("currency") String currency,
-            @JsonProperty("paymentProvider") String paymentProvider,
-            @JsonProperty("timezone") String timeZone,
-            @JsonProperty("address1") String address1,
-            @JsonProperty("address2") String address2,
-            @JsonProperty("company") String company,
-            @JsonProperty("state") String state,
-            @JsonProperty("country") String country,
-            @JsonProperty("phone") String phone) {
+                       @JsonProperty("name") String name,
+                       @JsonProperty("firstNameLength") Integer length,
+                       @JsonProperty("externalKey") String externalKey,
+                       @JsonProperty("email") String email,
+                       @JsonProperty("billingDay") Integer billCycleDay,
+                       @JsonProperty("currency") String currency,
+                       @JsonProperty("paymentProvider") String paymentProvider,
+                       @JsonProperty("timezone") String timeZone,
+                       @JsonProperty("address1") String address1,
+                       @JsonProperty("address2") String address2,
+                       @JsonProperty("company") String company,
+                       @JsonProperty("state") String state,
+                       @JsonProperty("country") String country,
+                       @JsonProperty("phone") String phone) {
         super(accountId, externalKey);
         this.name = name;
         this.length = length;
@@ -274,130 +287,130 @@ public class AccountJson extends AccountJsonSimple {
         return phone;
     }
 
-	@Override
-	public int hashCode() {
-		final int prime = 31;
-		int result = 1;
-		result = prime * result
-				+ ((accountId == null) ? 0 : accountId.hashCode());
-		result = prime * result
-				+ ((address1 == null) ? 0 : address1.hashCode());
-		result = prime * result
-				+ ((address2 == null) ? 0 : address2.hashCode());
-		result = prime * result
-				+ ((billCycleDay == null) ? 0 : billCycleDay.hashCode());
-		result = prime * result + ((company == null) ? 0 : company.hashCode());
-		result = prime * result + ((country == null) ? 0 : country.hashCode());
-		result = prime * result
-				+ ((currency == null) ? 0 : currency.hashCode());
-		result = prime * result + ((email == null) ? 0 : email.hashCode());
-		result = prime * result
-				+ ((externalKey == null) ? 0 : externalKey.hashCode());
-		result = prime * result + ((length == null) ? 0 : length.hashCode());
-		result = prime * result + ((name == null) ? 0 : name.hashCode());
-		result = prime * result
-				+ ((paymentProvider == null) ? 0 : paymentProvider.hashCode());
-		result = prime * result + ((phone == null) ? 0 : phone.hashCode());
-		result = prime * result + ((state == null) ? 0 : state.hashCode());
-		result = prime * result
-				+ ((timeZone == null) ? 0 : timeZone.hashCode());
-		return result;
-	}
-
-	// Used to check POST versus GET
-	public boolean equalsNoId(Object obj) {
-		if (this == obj)
-			return true;
-		if (obj == null)
-			return false;
-		if (getClass() != obj.getClass())
-			return false;
-		AccountJson other = (AccountJson) obj;
-		if (address1 == null) {
-			if (other.address1 != null)
-				return false;
-		} else if (!address1.equals(other.address1))
-			return false;
-		if (address2 == null) {
-			if (other.address2 != null)
-				return false;
-		} else if (!address2.equals(other.address2))
-			return false;
-		if (billCycleDay == null) {
-			if (other.billCycleDay != null)
-				return false;
-		} else if (!billCycleDay.equals(other.billCycleDay))
-			return false;
-		if (company == null) {
-			if (other.company != null)
-				return false;
-		} else if (!company.equals(other.company))
-			return false;
-		if (country == null) {
-			if (other.country != null)
-				return false;
-		} else if (!country.equals(other.country))
-			return false;
-		if (currency == null) {
-			if (other.currency != null)
-				return false;
-		} else if (!currency.equals(other.currency))
-			return false;
-		if (email == null) {
-			if (other.email != null)
-				return false;
-		} else if (!email.equals(other.email))
-			return false;
-		if (externalKey == null) {
-			if (other.externalKey != null)
-				return false;
-		} else if (!externalKey.equals(other.externalKey))
-			return false;
-		if (length == null) {
-			if (other.length != null)
-				return false;
-		} else if (!length.equals(other.length))
-			return false;
-		if (name == null) {
-			if (other.name != null)
-				return false;
-		} else if (!name.equals(other.name))
-			return false;
-		if (paymentProvider == null) {
-			if (other.paymentProvider != null)
-				return false;
-		} else if (!paymentProvider.equals(other.paymentProvider))
-			return false;
-		if (phone == null) {
-			if (other.phone != null)
-				return false;
-		} else if (!phone.equals(other.phone))
-			return false;
-		if (state == null) {
-			if (other.state != null)
-				return false;
-		} else if (!state.equals(other.state))
-			return false;
-		if (timeZone == null) {
-			if (other.timeZone != null)
-				return false;
-		} else if (!timeZone.equals(other.timeZone))
-			return false;
-		return true;
-	}
-
-	@Override
-	public boolean equals(Object obj) {
-		if (equalsNoId(obj) == false) {
-			return false;
-		} else {
-			AccountJson other = (AccountJson) obj;
-			if (accountId == null) {
-				if (other.accountId != null)
-					return false;
-			} else if (!accountId.equals(other.accountId))
-				return false;
-		}
-		return true;
-	}
- }
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result
+                + ((accountId == null) ? 0 : accountId.hashCode());
+        result = prime * result
+                + ((address1 == null) ? 0 : address1.hashCode());
+        result = prime * result
+                + ((address2 == null) ? 0 : address2.hashCode());
+        result = prime * result
+                + ((billCycleDay == null) ? 0 : billCycleDay.hashCode());
+        result = prime * result + ((company == null) ? 0 : company.hashCode());
+        result = prime * result + ((country == null) ? 0 : country.hashCode());
+        result = prime * result
+                + ((currency == null) ? 0 : currency.hashCode());
+        result = prime * result + ((email == null) ? 0 : email.hashCode());
+        result = prime * result
+                + ((externalKey == null) ? 0 : externalKey.hashCode());
+        result = prime * result + ((length == null) ? 0 : length.hashCode());
+        result = prime * result + ((name == null) ? 0 : name.hashCode());
+        result = prime * result
+                + ((paymentProvider == null) ? 0 : paymentProvider.hashCode());
+        result = prime * result + ((phone == null) ? 0 : phone.hashCode());
+        result = prime * result + ((state == null) ? 0 : state.hashCode());
+        result = prime * result
+                + ((timeZone == null) ? 0 : timeZone.hashCode());
+        return result;
+    }
+
+    // Used to check POST versus GET
+    public boolean equalsNoId(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        AccountJson other = (AccountJson) obj;
+        if (address1 == null) {
+            if (other.address1 != null)
+                return false;
+        } else if (!address1.equals(other.address1))
+            return false;
+        if (address2 == null) {
+            if (other.address2 != null)
+                return false;
+        } else if (!address2.equals(other.address2))
+            return false;
+        if (billCycleDay == null) {
+            if (other.billCycleDay != null)
+                return false;
+        } else if (!billCycleDay.equals(other.billCycleDay))
+            return false;
+        if (company == null) {
+            if (other.company != null)
+                return false;
+        } else if (!company.equals(other.company))
+            return false;
+        if (country == null) {
+            if (other.country != null)
+                return false;
+        } else if (!country.equals(other.country))
+            return false;
+        if (currency == null) {
+            if (other.currency != null)
+                return false;
+        } else if (!currency.equals(other.currency))
+            return false;
+        if (email == null) {
+            if (other.email != null)
+                return false;
+        } else if (!email.equals(other.email))
+            return false;
+        if (externalKey == null) {
+            if (other.externalKey != null)
+                return false;
+        } else if (!externalKey.equals(other.externalKey))
+            return false;
+        if (length == null) {
+            if (other.length != null)
+                return false;
+        } else if (!length.equals(other.length))
+            return false;
+        if (name == null) {
+            if (other.name != null)
+                return false;
+        } else if (!name.equals(other.name))
+            return false;
+        if (paymentProvider == null) {
+            if (other.paymentProvider != null)
+                return false;
+        } else if (!paymentProvider.equals(other.paymentProvider))
+            return false;
+        if (phone == null) {
+            if (other.phone != null)
+                return false;
+        } else if (!phone.equals(other.phone))
+            return false;
+        if (state == null) {
+            if (other.state != null)
+                return false;
+        } else if (!state.equals(other.state))
+            return false;
+        if (timeZone == null) {
+            if (other.timeZone != null)
+                return false;
+        } else if (!timeZone.equals(other.timeZone))
+            return false;
+        return true;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (!equalsNoId(obj)) {
+            return false;
+        } else {
+            AccountJson other = (AccountJson) obj;
+            if (accountId == null) {
+                if (other.accountId != null)
+                    return false;
+            } else if (!accountId.equals(other.accountId))
+                return false;
+        }
+        return true;
+    }
+}
diff --git a/jaxrs/src/test/java/com/ning/billing/jaxrs/json/TestAccountJson.java b/jaxrs/src/test/java/com/ning/billing/jaxrs/json/TestAccountJson.java
new file mode 100644
index 0000000..8b9a7ee
--- /dev/null
+++ b/jaxrs/src/test/java/com/ning/billing/jaxrs/json/TestAccountJson.java
@@ -0,0 +1,108 @@
+package com.ning.billing.jaxrs.json;
+
+import java.util.UUID;
+
+import org.joda.time.DateTimeZone;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.ning.billing.account.api.Account;
+import com.ning.billing.catalog.api.Currency;
+import com.ning.billing.mock.MockAccountBuilder;
+
+public class TestAccountJson {
+    private static final ObjectMapper mapper = new ObjectMapper();
+
+    @Test(groups = "fast")
+    public void testJson() throws Exception {
+        final String accountId = UUID.randomUUID().toString();
+        final String name = UUID.randomUUID().toString();
+        final Integer length = 12;
+        final String externalKey = UUID.randomUUID().toString();
+        final String email = UUID.randomUUID().toString();
+        final Integer billCycleDay = 6;
+        final String currency = UUID.randomUUID().toString();
+        final String paymentProvider = UUID.randomUUID().toString();
+        final String timeZone = UUID.randomUUID().toString();
+        final String address1 = UUID.randomUUID().toString();
+        final String address2 = UUID.randomUUID().toString();
+        final String company = UUID.randomUUID().toString();
+        final String state = UUID.randomUUID().toString();
+        final String country = UUID.randomUUID().toString();
+        final String phone = UUID.randomUUID().toString();
+
+        final AccountJson accountJson = new AccountJson(accountId, name, length, externalKey,
+                                                        email, billCycleDay, currency, paymentProvider,
+                                                        timeZone, address1, address2, company, state,
+                                                        country, phone);
+        Assert.assertEquals(accountJson.getAccountId(), accountId);
+        Assert.assertEquals(accountJson.getName(), name);
+        Assert.assertEquals(accountJson.getLength(), length);
+        Assert.assertEquals(accountJson.getExternalKey(), externalKey);
+        Assert.assertEquals(accountJson.getEmail(), email);
+        Assert.assertEquals(accountJson.getBillCycleDay(), billCycleDay);
+        Assert.assertEquals(accountJson.getCurrency(), currency);
+        Assert.assertEquals(accountJson.getPaymentProvider(), paymentProvider);
+        Assert.assertEquals(accountJson.getTimeZone(), timeZone);
+        Assert.assertEquals(accountJson.getAddress1(), address1);
+        Assert.assertEquals(accountJson.getAddress2(), address2);
+        Assert.assertEquals(accountJson.getCompany(), company);
+        Assert.assertEquals(accountJson.getState(), state);
+        Assert.assertEquals(accountJson.getCountry(), country);
+        Assert.assertEquals(accountJson.getPhone(), phone);
+
+        final String asJson = mapper.writeValueAsString(accountJson);
+        Assert.assertEquals(asJson, "{\"accountId\":\"" + accountJson.getAccountId() + "\",\"name\":\"" + accountJson.getName() + "\"," +
+                "\"externalKey\":\"" + accountJson.getExternalKey() + "\",\"email\":\"" + accountJson.getEmail() + "\"," +
+                "\"currency\":\"" + accountJson.getCurrency() + "\",\"paymentProvider\":\"" + accountJson.getPaymentProvider() + "\"," +
+                "\"address1\":\"" + accountJson.getAddress1() + "\",\"address2\":\"" + accountJson.getAddress2() + "\"," +
+                "\"company\":\"" + accountJson.getCompany() + "\",\"state\":\"" + accountJson.getState() + "\"," +
+                "\"country\":\"" + accountJson.getCountry() + "\",\"phone\":\"" + accountJson.getPhone() + "\"," +
+                "\"length\":" + accountJson.getLength() + ",\"billCycleDay\":" + accountJson.getBillCycleDay() + "," +
+                "\"timeZone\":\"" + accountJson.getTimeZone() + "\"}");
+
+        final AccountJson fromJson = mapper.readValue(asJson, AccountJson.class);
+        Assert.assertEquals(fromJson, accountJson);
+    }
+
+    @Test(groups = "fast")
+    public void testFromAccount() throws Exception {
+        final MockAccountBuilder accountBuilder = new MockAccountBuilder();
+        accountBuilder.address1(UUID.randomUUID().toString());
+        accountBuilder.address2(UUID.randomUUID().toString());
+        accountBuilder.billingCycleDay(4);
+        accountBuilder.city(UUID.randomUUID().toString());
+        accountBuilder.companyName(UUID.randomUUID().toString());
+        accountBuilder.country(UUID.randomUUID().toString());
+        accountBuilder.currency(Currency.GBP);
+        accountBuilder.email(UUID.randomUUID().toString());
+        accountBuilder.externalKey(UUID.randomUUID().toString());
+        accountBuilder.firstNameLength(12);
+        accountBuilder.isNotifiedForInvoices(true);
+        accountBuilder.locale(UUID.randomUUID().toString());
+        accountBuilder.migrated(true);
+        accountBuilder.name(UUID.randomUUID().toString());
+        accountBuilder.paymentProviderName(UUID.randomUUID().toString());
+        accountBuilder.phone(UUID.randomUUID().toString());
+        accountBuilder.postalCode(UUID.randomUUID().toString());
+        accountBuilder.stateOrProvince(UUID.randomUUID().toString());
+        accountBuilder.timeZone(DateTimeZone.UTC);
+        final Account account = accountBuilder.build();
+
+        final AccountJson accountJson = new AccountJson(account);
+        Assert.assertEquals(accountJson.getAddress1(), account.getAddress1());
+        Assert.assertEquals(accountJson.getAddress2(), account.getAddress2());
+        Assert.assertEquals((int) accountJson.getBillCycleDay(), (int) account.getBillCycleDay());
+        Assert.assertEquals(accountJson.getCountry(), account.getCountry());
+        Assert.assertEquals(accountJson.getCompany(), account.getCompanyName());
+        Assert.assertEquals(accountJson.getCurrency(), account.getCurrency().toString());
+        Assert.assertEquals(accountJson.getEmail(), account.getEmail());
+        Assert.assertEquals(accountJson.getExternalKey(), account.getExternalKey());
+        Assert.assertEquals(accountJson.getName(), account.getName());
+        Assert.assertEquals(accountJson.getPaymentProvider(), account.getPaymentProviderName());
+        Assert.assertEquals(accountJson.getPhone(), account.getPhone());
+        Assert.assertEquals(accountJson.getState(), account.getStateOrProvince());
+        Assert.assertEquals(accountJson.getTimeZone(), account.getTimeZone().toString());
+    }
+}