killbill-memoizeit

jaxrs: update RefundJson with adjusted invoice items We

8/1/2012 7:55:11 PM

Details

diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/AccountTimelineJson.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/AccountTimelineJson.java
index 29b1d29..9d889d4 100644
--- a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/AccountTimelineJson.java
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/AccountTimelineJson.java
@@ -125,7 +125,8 @@ public class AccountTimelineJson {
             final List<RefundJson> refunds = new ArrayList<RefundJson>();
             for (final Refund refund : refundsByPayment.get(payment.getId())) {
                 final List<AuditLog> auditLogs = refundsAuditLogs.get(refund.getId());
-                refunds.add(new RefundJson(refund, auditLogs));
+                // TODO add adjusted invoice items?
+                refunds.add(new RefundJson(refund, null, auditLogs));
             }
 
             final List<ChargebackJson> chargebacks = new ArrayList<ChargebackJson>();
diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/RefundJson.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/RefundJson.java
index 390837b..b2e1e58 100644
--- a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/RefundJson.java
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/RefundJson.java
@@ -23,11 +23,15 @@ import javax.annotation.Nullable;
 
 import org.joda.time.DateTime;
 
+import com.ning.billing.invoice.api.InvoiceItem;
 import com.ning.billing.payment.api.Refund;
 import com.ning.billing.util.audit.AuditLog;
 
 import com.fasterxml.jackson.annotation.JsonCreator;
 import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.base.Function;
+import com.google.common.collect.Collections2;
+import com.google.common.collect.ImmutableList;
 
 public class RefundJson extends JsonBase {
 
@@ -37,6 +41,7 @@ public class RefundJson extends JsonBase {
     private final Boolean isAdjusted;
     private final DateTime requestedDate;
     private final DateTime effectiveDate;
+    private final List<InvoiceItemJsonSimple> adjustments;
 
     @JsonCreator
     public RefundJson(@JsonProperty("refund_id") final String refundId,
@@ -45,6 +50,7 @@ public class RefundJson extends JsonBase {
                       @JsonProperty("adjusted") final Boolean isAdjusted,
                       @JsonProperty("requestedDate") final DateTime requestedDate,
                       @JsonProperty("effectiveDate") final DateTime effectiveDate,
+                      @JsonProperty("adjustments") @Nullable final List<InvoiceItemJsonSimple> adjustments,
                       @JsonProperty("auditLogs") @Nullable final List<AuditLogJson> auditLogs) {
         super(auditLogs);
         this.refundId = refundId;
@@ -53,15 +59,19 @@ public class RefundJson extends JsonBase {
         this.isAdjusted = isAdjusted;
         this.requestedDate = requestedDate;
         this.effectiveDate = effectiveDate;
+        this.adjustments = adjustments;
     }
 
-    public RefundJson(final Refund refund) {
-        this(refund, null);
-    }
-
-    public RefundJson(final Refund refund, final List<AuditLog> auditLogs) {
+    public RefundJson(final Refund refund, @Nullable final List<InvoiceItem> adjustments, @Nullable final List<AuditLog> auditLogs) {
         this(refund.getId().toString(), refund.getPaymentId().toString(), refund.getRefundAmount(), refund.isAdjusted(),
-             refund.getEffectiveDate(), refund.getEffectiveDate(), toAuditLogJson(auditLogs));
+             refund.getEffectiveDate(), refund.getEffectiveDate(),
+             adjustments == null ? null : ImmutableList.<InvoiceItemJsonSimple>copyOf(Collections2.transform(adjustments, new Function<InvoiceItem, InvoiceItemJsonSimple>() {
+                 @Override
+                 public InvoiceItemJsonSimple apply(@Nullable final InvoiceItem input) {
+                     return new InvoiceItemJsonSimple(input);
+                 }
+             })),
+             toAuditLogJson(auditLogs));
     }
 
     public String getRefundId() {
@@ -88,6 +98,10 @@ public class RefundJson extends JsonBase {
         return effectiveDate;
     }
 
+    public List<InvoiceItemJsonSimple> getAdjustments() {
+        return adjustments;
+    }
+
     @Override
     public String toString() {
         final StringBuilder sb = new StringBuilder();
@@ -98,6 +112,7 @@ public class RefundJson extends JsonBase {
         sb.append(", isAdjusted=").append(isAdjusted);
         sb.append(", requestedDate=").append(requestedDate);
         sb.append(", effectiveDate=").append(effectiveDate);
+        sb.append(", adjustments=").append(adjustments);
         sb.append('}');
         return sb.toString();
     }
@@ -110,6 +125,7 @@ public class RefundJson extends JsonBase {
         result = 31 * result + (isAdjusted != null ? isAdjusted.hashCode() : 0);
         result = 31 * result + (requestedDate != null ? requestedDate.hashCode() : 0);
         result = 31 * result + (effectiveDate != null ? effectiveDate.hashCode() : 0);
+        result = 31 * result + (adjustments != null ? adjustments.hashCode() : 0);
         return result;
     }
 
@@ -185,6 +201,14 @@ public class RefundJson extends JsonBase {
             return false;
         }
 
+        if (adjustments == null) {
+            if (other.adjustments != null) {
+                return false;
+            }
+        } else if (!adjustments.equals(other.adjustments)) {
+            return false;
+        }
+
         return true;
     }
 }
diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/AccountResource.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/AccountResource.java
index ff6a5fe..47e8e64 100644
--- a/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/AccountResource.java
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/AccountResource.java
@@ -397,7 +397,8 @@ public class AccountResource extends JaxRsResourceBase {
         final List<RefundJson> result = new ArrayList<RefundJson>(Collections2.transform(refunds, new Function<Refund, RefundJson>() {
             @Override
             public RefundJson apply(Refund input) {
-                return new RefundJson(input);
+                // TODO Return adjusted items and audits
+                return new RefundJson(input, null, null);
             }
         }));
 
diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/PaymentResource.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/PaymentResource.java
index 8ee10fa..8e61d24 100644
--- a/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/PaymentResource.java
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/PaymentResource.java
@@ -88,7 +88,8 @@ public class PaymentResource extends JaxRsResourceBase {
         final List<RefundJson> result = new ArrayList<RefundJson>(Collections2.transform(refunds, new Function<Refund, RefundJson>() {
             @Override
             public RefundJson apply(final Refund input) {
-                return new RefundJson(input);
+                // TODO Return adjusted items and audits
+                return new RefundJson(input, null, null);
             }
         }));
 
diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/RefundResource.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/RefundResource.java
index fe1e3c7..c50c17a 100644
--- a/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/RefundResource.java
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/RefundResource.java
@@ -57,7 +57,8 @@ public class RefundResource extends JaxRsResourceBase {
     @Produces(APPLICATION_JSON)
     public Response getRefund(@PathParam("refundId") final String refundId) throws PaymentApiException {
         final Refund refund = paymentApi.getRefund(UUID.fromString(refundId));
-        return Response.status(Status.OK).entity(new RefundJson(refund)).build();
+        // TODO Return adjusted items and audits
+        return Response.status(Status.OK).entity(new RefundJson(refund, null, null)).build();
     }
 
     @Override
diff --git a/jaxrs/src/test/java/com/ning/billing/jaxrs/json/TestRefundJson.java b/jaxrs/src/test/java/com/ning/billing/jaxrs/json/TestRefundJson.java
new file mode 100644
index 0000000..d4dde15
--- /dev/null
+++ b/jaxrs/src/test/java/com/ning/billing/jaxrs/json/TestRefundJson.java
@@ -0,0 +1,83 @@
+/*
+ * 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.List;
+import java.util.UUID;
+
+import org.joda.time.DateTime;
+import org.joda.time.LocalDate;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import com.ning.billing.catalog.api.Currency;
+import com.ning.billing.jaxrs.JaxrsTestSuite;
+import com.ning.billing.util.clock.Clock;
+import com.ning.billing.util.clock.ClockMock;
+
+import com.google.common.collect.ImmutableList;
+
+public class TestRefundJson extends JaxrsTestSuite {
+
+    private final Clock clock = new ClockMock();
+
+    @Test(groups = "fast")
+    public void testJson() throws Exception {
+        final String refundId = UUID.randomUUID().toString();
+        final String paymentId = UUID.randomUUID().toString();
+        final BigDecimal refundAmount = BigDecimal.TEN;
+        final boolean isAdjusted = true;
+        final DateTime requestedDate = clock.getUTCNow();
+        final DateTime effectiveDate = clock.getUTCNow();
+        final List<InvoiceItemJsonSimple> adjustments = ImmutableList.<InvoiceItemJsonSimple>of(createInvoiceItemJson());
+        final List<AuditLogJson> auditLogs = createAuditLogsJson();
+        final RefundJson refundJson = new RefundJson(refundId, paymentId, refundAmount, isAdjusted, requestedDate,
+                                                     effectiveDate, adjustments, auditLogs);
+        Assert.assertEquals(refundJson.getRefundId(), refundId);
+        Assert.assertEquals(refundJson.getPaymentId(), paymentId);
+        Assert.assertEquals(refundJson.getRefundAmount(), refundAmount);
+        Assert.assertEquals(refundJson.isAdjusted(), isAdjusted);
+        Assert.assertEquals(refundJson.getRequestedDate(), requestedDate);
+        Assert.assertEquals(refundJson.getEffectiveDate(), effectiveDate);
+        Assert.assertEquals(refundJson.getAdjustments(), adjustments);
+        Assert.assertEquals(refundJson.getAuditLogs(), auditLogs);
+
+        final String asJson = mapper.writeValueAsString(refundJson);
+        final RefundJson fromJson = mapper.readValue(asJson, RefundJson.class);
+        Assert.assertEquals(fromJson, refundJson);
+    }
+
+    private InvoiceItemJsonSimple createInvoiceItemJson() {
+        final String invoiceItemId = UUID.randomUUID().toString();
+        final String invoiceId = UUID.randomUUID().toString();
+        final String accountId = UUID.randomUUID().toString();
+        final String bundleId = UUID.randomUUID().toString();
+        final String subscriptionId = UUID.randomUUID().toString();
+        final String planName = UUID.randomUUID().toString();
+        final String phaseName = UUID.randomUUID().toString();
+        final String description = UUID.randomUUID().toString();
+        final LocalDate startDate = clock.getUTCToday();
+        final LocalDate endDate = clock.getUTCToday();
+        final BigDecimal amount = BigDecimal.TEN;
+        final Currency currency = Currency.MXN;
+        final List<AuditLogJson> auditLogs = createAuditLogsJson();
+        return new InvoiceItemJsonSimple(invoiceItemId, invoiceId, accountId, bundleId, subscriptionId,
+                                         planName, phaseName, description, startDate, endDate,
+                                         amount, currency, auditLogs);
+    }
+}
diff --git a/server/src/test/java/com/ning/billing/jaxrs/TestPayment.java b/server/src/test/java/com/ning/billing/jaxrs/TestPayment.java
index 2b16d3f..a10062e 100644
--- a/server/src/test/java/com/ning/billing/jaxrs/TestPayment.java
+++ b/server/src/test/java/com/ning/billing/jaxrs/TestPayment.java
@@ -92,7 +92,7 @@ public class TestPayment extends TestJaxrsBase {
 
         // Issue the refund
 
-        final RefundJson refundJson = new RefundJson(null, paymentId, paymentAmount, false, null, null, null);
+        final RefundJson refundJson = new RefundJson(null, paymentId, paymentAmount, false, null, null, null, null);
         baseJson = mapper.writeValueAsString(refundJson);
         response = doPost(uri, baseJson, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
         assertEquals(response.getStatusCode(), Status.CREATED.getStatusCode());