killbill-aplcache

jaxrs: add test for CreditJson Signed-off-by: Pierre-Alexandre

6/8/2012 3:29:07 PM

Details

diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/ChargebackJson.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/ChargebackJson.java
index 65df8e7..22475d7 100644
--- a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/ChargebackJson.java
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/ChargebackJson.java
@@ -26,11 +26,11 @@ import com.ning.billing.invoice.api.InvoicePayment;
 
 // TODO: populate reason code, requested date from audit log
 public class ChargebackJson {
-    private final DateTime   requestedDate;
-    private final DateTime   effectiveDate;
+    private final DateTime requestedDate;
+    private final DateTime effectiveDate;
     private final BigDecimal chargebackAmount;
-    private final String     paymentId;
-    private final String     reason;
+    private final String paymentId;
+    private final String reason;
 
     @JsonCreator
     public ChargebackJson(@JsonProperty("requestedDate") final DateTime requestedDate,
diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/CreditJson.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/CreditJson.java
index fadff6b..4ec1ad7 100644
--- a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/CreditJson.java
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/CreditJson.java
@@ -28,11 +28,11 @@ import com.ning.billing.invoice.api.InvoiceItem;
 // TODO: add invoice number, reason and requested date to the json
 public class CreditJson {
     private final BigDecimal creditAmount;
-    private final UUID       invoiceId;
-    private final String     invoiceNumber;
-    private final DateTime   requestedDate;
-    private final DateTime   effectiveDate;
-    private final String     reason;
+    private final UUID invoiceId;
+    private final String invoiceNumber;
+    private final DateTime requestedDate;
+    private final DateTime effectiveDate;
+    private final String reason;
 
     @JsonCreator
     public CreditJson(@JsonProperty("creditAmount") final BigDecimal creditAmount,
@@ -81,4 +81,48 @@ public class CreditJson {
     public String getReason() {
         return reason;
     }
+
+    @Override
+    public boolean equals(final Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+
+        final CreditJson that = (CreditJson) o;
+
+        if (creditAmount != null ? !creditAmount.equals(that.creditAmount) : that.creditAmount != null) {
+            return false;
+        }
+        if (effectiveDate != null ? !effectiveDate.equals(that.effectiveDate) : that.effectiveDate != null) {
+            return false;
+        }
+        if (invoiceId != null ? !invoiceId.equals(that.invoiceId) : that.invoiceId != null) {
+            return false;
+        }
+        if (invoiceNumber != null ? !invoiceNumber.equals(that.invoiceNumber) : that.invoiceNumber != null) {
+            return false;
+        }
+        if (reason != null ? !reason.equals(that.reason) : that.reason != null) {
+            return false;
+        }
+        if (requestedDate != null ? !requestedDate.equals(that.requestedDate) : that.requestedDate != null) {
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = creditAmount != null ? creditAmount.hashCode() : 0;
+        result = 31 * result + (invoiceId != null ? invoiceId.hashCode() : 0);
+        result = 31 * result + (invoiceNumber != null ? invoiceNumber.hashCode() : 0);
+        result = 31 * result + (requestedDate != null ? requestedDate.hashCode() : 0);
+        result = 31 * result + (effectiveDate != null ? effectiveDate.hashCode() : 0);
+        result = 31 * result + (reason != null ? reason.hashCode() : 0);
+        return result;
+    }
 }
diff --git a/jaxrs/src/test/java/com/ning/billing/jaxrs/json/TestCreditJson.java b/jaxrs/src/test/java/com/ning/billing/jaxrs/json/TestCreditJson.java
new file mode 100644
index 0000000..a182d2d
--- /dev/null
+++ b/jaxrs/src/test/java/com/ning/billing/jaxrs/json/TestCreditJson.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2010-2012 Ning, Inc.
+ *
+ * Ning 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 com.ning.billing.jaxrs.json;
+
+import java.math.BigDecimal;
+import java.util.UUID;
+
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import com.fasterxml.jackson.datatype.joda.JodaModule;
+
+public class TestCreditJson {
+    private static final ObjectMapper mapper = new ObjectMapper();
+
+    static {
+        mapper.registerModule(new JodaModule());
+        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
+    }
+
+    @Test(groups = "fast")
+    public void testJson() throws Exception {
+        final BigDecimal creditAmount = BigDecimal.TEN;
+        final UUID invoiceId = UUID.randomUUID();
+        final String invoiceNumber = UUID.randomUUID().toString();
+        final DateTime requestedDate = new DateTime(DateTimeZone.UTC);
+        final DateTime effectiveDate = new DateTime(DateTimeZone.UTC);
+        final String reason = UUID.randomUUID().toString();
+        final CreditJson creditJson = new CreditJson(creditAmount, invoiceId, invoiceNumber, requestedDate, effectiveDate, reason);
+        Assert.assertEquals(creditJson.getRequestedDate(), requestedDate);
+        Assert.assertEquals(creditJson.getEffectiveDate(), effectiveDate);
+        Assert.assertEquals(creditJson.getCreditAmount(), creditAmount);
+        Assert.assertEquals(creditJson.getInvoiceId(), invoiceId);
+        Assert.assertEquals(creditJson.getInvoiceNumber(), invoiceNumber);
+        Assert.assertEquals(creditJson.getReason(), reason);
+
+        final String asJson = mapper.writeValueAsString(creditJson);
+        Assert.assertEquals(asJson, "{\"creditAmount\":" + creditJson.getCreditAmount() + "," +
+                "\"invoiceId\":\"" + creditJson.getInvoiceId().toString() + "\"," +
+                "\"invoiceNumber\":\"" + creditJson.getInvoiceNumber() + "\"," +
+                "\"requestedDate\":\"" + creditJson.getRequestedDate() + "\"," +
+                "\"effectiveDate\":\"" + creditJson.getEffectiveDate() + "\"," +
+                "\"reason\":\"" + creditJson.getReason() + "\"}");
+
+        final CreditJson fromJson = mapper.readValue(asJson, CreditJson.class);
+        Assert.assertEquals(fromJson, creditJson);
+    }
+}