killbill-aplcache

jaxrs: add test for BundleTimelineJson Add various missing

6/8/2012 2:57:43 PM

Details

diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/BundleJsonWithSubscriptions.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/BundleJsonWithSubscriptions.java
index 3e2eb85..364e1f5 100644
--- a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/BundleJsonWithSubscriptions.java
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/BundleJsonWithSubscriptions.java
@@ -60,4 +60,32 @@ public class BundleJsonWithSubscriptions extends BundleJsonSimple {
     public BundleJsonWithSubscriptions() {
         this(null, null, null);
     }
+
+    @Override
+    public boolean equals(final Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+        if (!super.equals(o)) {
+            return false;
+        }
+
+        final BundleJsonWithSubscriptions that = (BundleJsonWithSubscriptions) o;
+
+        if (subscriptions != null ? !subscriptions.equals(that.subscriptions) : that.subscriptions != null) {
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = super.hashCode();
+        result = 31 * result + (subscriptions != null ? subscriptions.hashCode() : 0);
+        return result;
+    }
 }
diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/BundleTimelineJson.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/BundleTimelineJson.java
index 47906ee..c6f945a 100644
--- a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/BundleTimelineJson.java
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/BundleTimelineJson.java
@@ -70,4 +70,44 @@ public class BundleTimelineJson {
     public String getReasonForChange() {
         return reasonForChange;
     }
+
+    @Override
+    public boolean equals(final Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+
+        final BundleTimelineJson that = (BundleTimelineJson) o;
+
+        if (bundle != null ? !bundle.equals(that.bundle) : that.bundle != null) {
+            return false;
+        }
+        if (invoices != null ? !invoices.equals(that.invoices) : that.invoices != null) {
+            return false;
+        }
+        if (payments != null ? !payments.equals(that.payments) : that.payments != null) {
+            return false;
+        }
+        if (reasonForChange != null ? !reasonForChange.equals(that.reasonForChange) : that.reasonForChange != null) {
+            return false;
+        }
+        if (viewId != null ? !viewId.equals(that.viewId) : that.viewId != null) {
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = viewId != null ? viewId.hashCode() : 0;
+        result = 31 * result + (bundle != null ? bundle.hashCode() : 0);
+        result = 31 * result + (payments != null ? payments.hashCode() : 0);
+        result = 31 * result + (invoices != null ? invoices.hashCode() : 0);
+        result = 31 * result + (reasonForChange != null ? reasonForChange.hashCode() : 0);
+        return result;
+    }
 }
diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/PaymentJsonSimple.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/PaymentJsonSimple.java
index 922c15b..86b8efd 100644
--- a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/PaymentJsonSimple.java
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/PaymentJsonSimple.java
@@ -122,4 +122,64 @@ public class PaymentJsonSimple {
     public UUID getAccountId() {
         return accountId;
     }
+
+    @Override
+    public boolean equals(final Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+
+        final PaymentJsonSimple that = (PaymentJsonSimple) o;
+
+        if (accountId != null ? !accountId.equals(that.accountId) : that.accountId != null) {
+            return false;
+        }
+        if (amount != null ? !amount.equals(that.amount) : that.amount != null) {
+            return false;
+        }
+        if (currency != null ? !currency.equals(that.currency) : that.currency != 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 (paidAmount != null ? !paidAmount.equals(that.paidAmount) : that.paidAmount != null) {
+            return false;
+        }
+        if (paymentId != null ? !paymentId.equals(that.paymentId) : that.paymentId != null) {
+            return false;
+        }
+        if (requestedDate != null ? !requestedDate.equals(that.requestedDate) : that.requestedDate != null) {
+            return false;
+        }
+        if (retryCount != null ? !retryCount.equals(that.retryCount) : that.retryCount != null) {
+            return false;
+        }
+        if (status != null ? !status.equals(that.status) : that.status != null) {
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = paidAmount != null ? paidAmount.hashCode() : 0;
+        result = 31 * result + (amount != null ? amount.hashCode() : 0);
+        result = 31 * result + (invoiceId != null ? invoiceId.hashCode() : 0);
+        result = 31 * result + (paymentId != null ? paymentId.hashCode() : 0);
+        result = 31 * result + (requestedDate != null ? requestedDate.hashCode() : 0);
+        result = 31 * result + (effectiveDate != null ? effectiveDate.hashCode() : 0);
+        result = 31 * result + (retryCount != null ? retryCount.hashCode() : 0);
+        result = 31 * result + (currency != null ? currency.hashCode() : 0);
+        result = 31 * result + (status != null ? status.hashCode() : 0);
+        result = 31 * result + (accountId != null ? accountId.hashCode() : 0);
+        return result;
+    }
 }
diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/SubscriptionJsonWithEvents.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/SubscriptionJsonWithEvents.java
index 6200788..07ba180 100644
--- a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/SubscriptionJsonWithEvents.java
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/SubscriptionJsonWithEvents.java
@@ -40,9 +40,7 @@ public class SubscriptionJsonWithEvents extends SubscriptionJsonSimple {
     @JsonView(BundleTimelineViews.WriteTimeline.class)
     private final List<SubscriptionNewEventJson> newEvents;
 
-
     public static class SubscriptionReadEventJson extends SubscriptionBaseEventJson {
-
         @JsonView(BundleTimelineViews.Timeline.class)
         private final String eventId;
 
@@ -89,6 +87,34 @@ public class SubscriptionJsonWithEvents extends SubscriptionJsonSimple {
                     + getClass() + ", hashCode()=" + hashCode()
                     + ", toString()=" + super.toString() + "]";
         }
+
+        @Override
+        public boolean equals(final Object o) {
+            if (this == o) {
+                return true;
+            }
+            if (o == null || getClass() != o.getClass()) {
+                return false;
+            }
+
+            final SubscriptionReadEventJson that = (SubscriptionReadEventJson) o;
+
+            if (effectiveDate != null ? !effectiveDate.equals(that.effectiveDate) : that.effectiveDate != null) {
+                return false;
+            }
+            if (eventId != null ? !eventId.equals(that.eventId) : that.eventId != null) {
+                return false;
+            }
+
+            return true;
+        }
+
+        @Override
+        public int hashCode() {
+            int result = eventId != null ? eventId.hashCode() : 0;
+            result = 31 * result + (effectiveDate != null ? effectiveDate.hashCode() : 0);
+            return result;
+        }
     }
 
     public static class SubscriptionDeletedEventJson extends SubscriptionReadEventJson {
@@ -102,7 +128,6 @@ public class SubscriptionJsonWithEvents extends SubscriptionJsonSimple {
                                             @JsonProperty("event_type") final String eventType,
                                             @JsonProperty("phase") final String phase) {
             super(eventId, billingPeriod, requestedDate, effectiveDate, product, priceList, eventType, phase);
-
         }
     }
 
@@ -132,7 +157,6 @@ public class SubscriptionJsonWithEvents extends SubscriptionJsonSimple {
     }
 
     public static class SubscriptionBaseEventJson {
-
         @JsonView(BundleTimelineViews.Timeline.class)
         private final String billingPeriod;
 
@@ -200,8 +224,51 @@ public class SubscriptionJsonWithEvents extends SubscriptionJsonSimple {
         public String getPhase() {
             return phase;
         }
-    }
 
+        @Override
+        public boolean equals(final Object o) {
+            if (this == o) {
+                return true;
+            }
+            if (o == null || getClass() != o.getClass()) {
+                return false;
+            }
+
+            final SubscriptionBaseEventJson that = (SubscriptionBaseEventJson) o;
+
+            if (billingPeriod != null ? !billingPeriod.equals(that.billingPeriod) : that.billingPeriod != null) {
+                return false;
+            }
+            if (eventType != null ? !eventType.equals(that.eventType) : that.eventType != null) {
+                return false;
+            }
+            if (phase != null ? !phase.equals(that.phase) : that.phase != null) {
+                return false;
+            }
+            if (priceList != null ? !priceList.equals(that.priceList) : that.priceList != null) {
+                return false;
+            }
+            if (product != null ? !product.equals(that.product) : that.product != null) {
+                return false;
+            }
+            if (requestedDate != null ? !requestedDate.equals(that.requestedDate) : that.requestedDate != null) {
+                return false;
+            }
+
+            return true;
+        }
+
+        @Override
+        public int hashCode() {
+            int result = billingPeriod != null ? billingPeriod.hashCode() : 0;
+            result = 31 * result + (requestedDate != null ? requestedDate.hashCode() : 0);
+            result = 31 * result + (product != null ? product.hashCode() : 0);
+            result = 31 * result + (priceList != null ? priceList.hashCode() : 0);
+            result = 31 * result + (eventType != null ? eventType.hashCode() : 0);
+            result = 31 * result + (phase != null ? phase.hashCode() : 0);
+            return result;
+        }
+    }
 
     @JsonCreator
     public SubscriptionJsonWithEvents(@JsonProperty("subscription_id") final String subscriptionId,
@@ -256,4 +323,36 @@ public class SubscriptionJsonWithEvents extends SubscriptionJsonSimple {
     public List<SubscriptionDeletedEventJson> getDeletedEvents() {
         return deletedEvents;
     }
+
+    @Override
+    public boolean equals(final Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+
+        final SubscriptionJsonWithEvents that = (SubscriptionJsonWithEvents) o;
+
+        if (deletedEvents != null ? !deletedEvents.equals(that.deletedEvents) : that.deletedEvents != null) {
+            return false;
+        }
+        if (events != null ? !events.equals(that.events) : that.events != null) {
+            return false;
+        }
+        if (newEvents != null ? !newEvents.equals(that.newEvents) : that.newEvents != null) {
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = events != null ? events.hashCode() : 0;
+        result = 31 * result + (deletedEvents != null ? deletedEvents.hashCode() : 0);
+        result = 31 * result + (newEvents != null ? newEvents.hashCode() : 0);
+        return result;
+    }
 }
diff --git a/jaxrs/src/test/java/com/ning/billing/jaxrs/json/TestBundleTimelineJson.java b/jaxrs/src/test/java/com/ning/billing/jaxrs/json/TestBundleTimelineJson.java
new file mode 100644
index 0000000..180e816
--- /dev/null
+++ b/jaxrs/src/test/java/com/ning/billing/jaxrs/json/TestBundleTimelineJson.java
@@ -0,0 +1,161 @@
+/*
+ * 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.mockito.Mockito;
+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;
+import com.google.common.collect.ImmutableList;
+import com.ning.billing.catalog.api.BillingPeriod;
+import com.ning.billing.catalog.api.PhaseType;
+import com.ning.billing.catalog.api.PlanPhaseSpecifier;
+import com.ning.billing.catalog.api.ProductCategory;
+import com.ning.billing.entitlement.api.SubscriptionTransitionType;
+import com.ning.billing.entitlement.api.timeline.SubscriptionTimeline;
+import com.ning.billing.util.clock.DefaultClock;
+
+public class TestBundleTimelineJson {
+    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 String viewId = UUID.randomUUID().toString();
+        final String reason = UUID.randomUUID().toString();
+
+        final BundleJsonWithSubscriptions bundleJsonWithSubscriptions = createBundleWithSubscriptions();
+        final InvoiceJsonSimple invoiceJsonSimple = createInvoice();
+        final PaymentJsonSimple paymentJsonSimple = createPayment(UUID.fromString(invoiceJsonSimple.getAccountId()),
+                                                                  UUID.fromString(invoiceJsonSimple.getInvoiceId()));
+
+        final BundleTimelineJson bundleTimelineJson = new BundleTimelineJson(viewId,
+                                                                             bundleJsonWithSubscriptions,
+                                                                             ImmutableList.<PaymentJsonSimple>of(paymentJsonSimple),
+                                                                             ImmutableList.<InvoiceJsonSimple>of(invoiceJsonSimple),
+                                                                             reason);
+
+        final String asJson = mapper.writeValueAsString(bundleTimelineJson);
+
+        final SubscriptionJsonWithEvents subscription = bundleTimelineJson.getBundle().getSubscriptions().get(0);
+        final SubscriptionJsonWithEvents.SubscriptionReadEventJson event = subscription.getEvents().get(0);
+        final PaymentJsonSimple payment = bundleTimelineJson.getPayments().get(0);
+        final InvoiceJsonSimple invoice = bundleTimelineJson.getInvoices().get(0);
+
+        Assert.assertEquals(asJson, "{\"viewId\":\"" + bundleTimelineJson.getViewId() + "\"," +
+                "\"bundle\":{\"bundleId\":\"" + bundleTimelineJson.getBundle().getBundleId() + "\"," +
+                "\"externalKey\":\"" + bundleTimelineJson.getBundle().getExternalKey() + "\"," +
+                "\"subscriptions\":" +
+                "[{\"events\":[{\"eventId\":\"" + event.getEventId() + "\"," +
+                "\"billingPeriod\":\"" + event.getBillingPeriod() + "\"," +
+                "\"product\":\"" + event.getProduct() + "\"," +
+                "\"priceList\":\"" + event.getPriceList() + "\"," +
+                "\"eventType\":\"" + event.getEventType() + "\"," +
+                "\"phase\":\"" + event.getPhase() + "\"," +
+                "\"requestedDate\":null," +
+                "\"effectiveDate\":\"" + event.getEffectiveDate().toDateTimeISO().toString() + "\"}]," +
+                "\"subscriptionId\":\"" + subscription.getSubscriptionId() + "\"," +
+                "\"deletedEvents\":null," +
+                "\"newEvents\":null}]}," +
+                "\"payments\":[{\"amount\":" + payment.getAmount() + "," +
+                "\"paidAmount\":" + payment.getPaidAmount() + "," +
+                "\"invoiceId\":\"" + payment.getInvoiceId() + "\"," +
+                "\"paymentId\":\"" + payment.getPaymentId() + "\"," +
+                "\"requestedDate\":\"" + payment.getRequestedDate().toDateTimeISO().toString() + "\"," +
+                "\"effectiveDate\":\"" + payment.getEffectiveDate().toDateTimeISO().toString() + "\"," +
+                "\"retryCount\":" + payment.getRetryCount() + "," +
+                "\"currency\":\"" + payment.getCurrency() + "\"," +
+                "\"status\":\"" + payment.getStatus() + "\"," +
+                "\"accountId\":\"" + payment.getAccountId() + "\"}]," +
+                "\"invoices\":[{\"amount\":" + invoice.getAmount() + "," +
+                "\"credit\":" + invoice.getCredit() + "," +
+                "\"invoiceId\":\"" + invoice.getInvoiceId() + "\"," +
+                "\"invoiceDate\":\"" + invoice.getInvoiceDate().toDateTimeISO().toString() + "\"," +
+                "\"targetDate\":\"" + invoice.getTargetDate() + "\"," +
+                "\"invoiceNumber\":\"" + invoice.getInvoiceNumber() + "\"," +
+                "\"balance\":" + invoice.getBalance() + "," +
+                "\"accountId\":\"" + invoice.getAccountId() + "\"}]," +
+                "\"reasonForChange\":\"" + reason + "\"}");
+
+        final BundleTimelineJson fromJson = mapper.readValue(asJson, BundleTimelineJson.class);
+        Assert.assertEquals(fromJson, bundleTimelineJson);
+    }
+
+    private BundleJsonWithSubscriptions createBundleWithSubscriptions() {
+        final SubscriptionTimeline.ExistingEvent event = Mockito.mock(SubscriptionTimeline.ExistingEvent.class);
+        final DateTime effectiveDate = DefaultClock.toUTCDateTime(new DateTime(DateTimeZone.UTC));
+        final UUID eventId = UUID.randomUUID();
+        final PlanPhaseSpecifier planPhaseSpecifier = new PlanPhaseSpecifier(UUID.randomUUID().toString(), ProductCategory.BASE,
+                                                                             BillingPeriod.NO_BILLING_PERIOD, UUID.randomUUID().toString(),
+                                                                             PhaseType.EVERGREEN);
+        Mockito.when(event.getEffectiveDate()).thenReturn(effectiveDate);
+        Mockito.when(event.getEventId()).thenReturn(eventId);
+        Mockito.when(event.getSubscriptionTransitionType()).thenReturn(SubscriptionTransitionType.CREATE);
+        Mockito.when(event.getPlanPhaseSpecifier()).thenReturn(planPhaseSpecifier);
+
+        final SubscriptionTimeline subscriptionTimeline = Mockito.mock(SubscriptionTimeline.class);
+        Mockito.when(subscriptionTimeline.getId()).thenReturn(UUID.randomUUID());
+        Mockito.when(subscriptionTimeline.getExistingEvents()).thenReturn(ImmutableList.<SubscriptionTimeline.ExistingEvent>of(event));
+
+        final UUID bundleId = UUID.randomUUID();
+        final String externalKey = UUID.randomUUID().toString();
+        final SubscriptionJsonWithEvents subscription = new SubscriptionJsonWithEvents(bundleId, subscriptionTimeline);
+
+        return new BundleJsonWithSubscriptions(bundleId.toString(), externalKey, ImmutableList.<SubscriptionJsonWithEvents>of(subscription));
+    }
+
+    private InvoiceJsonSimple createInvoice() {
+        final UUID accountId = UUID.randomUUID();
+        final UUID invoiceId = UUID.randomUUID();
+        final BigDecimal invoiceAmount = BigDecimal.TEN;
+        final BigDecimal credit = BigDecimal.ONE;
+        final DateTime invoiceDate = DefaultClock.toUTCDateTime(new DateTime(DateTimeZone.UTC));
+        final DateTime targetDate = DefaultClock.toUTCDateTime(new DateTime(DateTimeZone.UTC));
+        final String invoiceNumber = UUID.randomUUID().toString();
+        final BigDecimal balance = BigDecimal.ZERO;
+
+        return new InvoiceJsonSimple(invoiceAmount, credit, invoiceId.toString(), invoiceDate,
+                                     targetDate, invoiceNumber, balance, accountId.toString());
+    }
+
+    private PaymentJsonSimple createPayment(final UUID accountId, final UUID invoiceId) {
+        final UUID paymentId = UUID.randomUUID();
+        final BigDecimal paidAmount = BigDecimal.TEN;
+        final BigDecimal amount = BigDecimal.ZERO;
+        final DateTime paymentRequestedDate = DefaultClock.toUTCDateTime(new DateTime(DateTimeZone.UTC));
+        final DateTime paymentEffectiveDate = DefaultClock.toUTCDateTime(new DateTime(DateTimeZone.UTC));
+        final Integer retryCount = Integer.MAX_VALUE;
+        final String currency = "USD";
+        final String status = UUID.randomUUID().toString();
+
+        return new PaymentJsonSimple(amount, paidAmount, invoiceId, paymentId,
+                                     paymentRequestedDate, paymentEffectiveDate, retryCount,
+                                     currency, status, accountId);
+    }
+}