killbill-aplcache

jaxrs: add test for SubscriptionJsonNoEvents Signed-off-by:

6/8/2012 5:38:42 PM

Details

diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/SubscriptionJsonNoEvents.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/SubscriptionJsonNoEvents.java
index aa3972a..27c6dc9 100644
--- a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/SubscriptionJsonNoEvents.java
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/SubscriptionJsonNoEvents.java
@@ -16,6 +16,8 @@
 
 package com.ning.billing.jaxrs.json;
 
+import javax.annotation.Nullable;
+
 import org.joda.time.DateTime;
 
 import com.fasterxml.jackson.annotation.JsonCreator;
@@ -45,16 +47,15 @@ public class SubscriptionJsonNoEvents extends SubscriptionJsonSimple {
     @JsonView(BundleTimelineViews.Base.class)
     private final DateTime chargedThroughDate;
 
-
     @JsonCreator
-    public SubscriptionJsonNoEvents(@JsonProperty("subscriptionId") final String subscriptionId,
-                                    @JsonProperty("bundleId") final String bundleId,
-                                    @JsonProperty("startDate") final DateTime startDate,
-                                    @JsonProperty("productName") final String productName,
-                                    @JsonProperty("productCategory") final String productCategory,
-                                    @JsonProperty("billingPeriod") final String billingPeriod,
-                                    @JsonProperty("priceList") final String priceList,
-                                    @JsonProperty("chargedThroughDate") final DateTime chargedThroughDate) {
+    public SubscriptionJsonNoEvents(@JsonProperty("subscriptionId") @Nullable final String subscriptionId,
+                                    @JsonProperty("bundleId") @Nullable final String bundleId,
+                                    @JsonProperty("startDate") @Nullable final DateTime startDate,
+                                    @JsonProperty("productName") @Nullable final String productName,
+                                    @JsonProperty("productCategory") @Nullable final String productCategory,
+                                    @JsonProperty("billingPeriod") @Nullable final String billingPeriod,
+                                    @JsonProperty("priceList") @Nullable final String priceList,
+                                    @JsonProperty("chargedThroughDate") @Nullable final DateTime chargedThroughDate) {
         super(subscriptionId);
         this.bundleId = bundleId;
         this.startDate = startDate;
@@ -66,28 +67,15 @@ public class SubscriptionJsonNoEvents extends SubscriptionJsonSimple {
     }
 
     public SubscriptionJsonNoEvents() {
-        super(null);
-        this.bundleId = null;
-        this.startDate = null;
-        this.productName = null;
-        this.productCategory = null;
-        this.billingPeriod = null;
-        this.priceList = null;
-        this.chargedThroughDate = null;
+        this(null, null, null, null, null, null, null, null);
     }
 
     public SubscriptionJsonNoEvents(final Subscription data) {
-        super(data.getId().toString());
-        this.bundleId = data.getBundleId().toString();
-        this.startDate = data.getStartDate();
-        this.productName = data.getCurrentPlan().getProduct().getName();
-        this.productCategory = data.getCurrentPlan().getProduct().getCategory().toString();
-        this.billingPeriod = data.getCurrentPlan().getBillingPeriod().toString();
-        this.priceList = data.getCurrentPriceList().getName();
-        this.chargedThroughDate = data.getChargedThroughDate();
+        this(data.getId().toString(), data.getBundleId().toString(), data.getStartDate(), data.getCurrentPlan().getProduct().getName(),
+             data.getCurrentPlan().getProduct().getCategory().toString(), data.getCurrentPlan().getBillingPeriod().toString(),
+             data.getCurrentPriceList().getName(), data.getChargedThroughDate());
     }
 
-
     public String getSubscriptionId() {
         return subscriptionId;
     }
@@ -120,7 +108,6 @@ public class SubscriptionJsonNoEvents extends SubscriptionJsonSimple {
         return chargedThroughDate;
     }
 
-
     @Override
     public String toString() {
         return "SubscriptionJson [subscriptionId=" + subscriptionId
@@ -130,86 +117,55 @@ public class SubscriptionJsonNoEvents extends SubscriptionJsonSimple {
     }
 
     @Override
-    public int hashCode() {
-        final int prime = 31;
-        int result = 1;
-        result = prime * result
-                + ((billingPeriod == null) ? 0 : billingPeriod.hashCode());
-        result = prime * result
-                + ((bundleId == null) ? 0 : bundleId.hashCode());
-        result = prime * result
-                + ((priceList == null) ? 0 : priceList.hashCode());
-        result = prime * result
-                + ((productCategory == null) ? 0 : productCategory.hashCode());
-        result = prime * result
-                + ((productName == null) ? 0 : productName.hashCode());
-        result = prime * result
-                + ((subscriptionId == null) ? 0 : subscriptionId.hashCode());
-        return result;
+    public boolean equals(final Object o) {
+        return equalsNoId(o) && super.equals(o);
     }
 
-    @Override
-    public boolean equals(final Object obj) {
-        if (!equalsNoId(obj)) {
-            return false;
+    public boolean equalsNoId(final Object o) {
+        if (this == o) {
+            return true;
         }
-        final SubscriptionJsonNoEvents other = (SubscriptionJsonNoEvents) obj;
-        if (subscriptionId == null) {
-            if (other.subscriptionId != null) {
-                return false;
-            }
-        } else if (!subscriptionId.equals(other.subscriptionId)) {
+        if (o == null || getClass() != o.getClass()) {
             return false;
         }
-        return true;
-    }
 
-    public boolean equalsNoId(final Object obj) {
-        if (this == obj) {
-            return true;
-        }
-        if (obj == null) {
+        final SubscriptionJsonNoEvents that = (SubscriptionJsonNoEvents) o;
+
+        if (billingPeriod != null ? !billingPeriod.equals(that.billingPeriod) : that.billingPeriod != null) {
             return false;
         }
-        if (getClass() != obj.getClass()) {
+        if (bundleId != null ? !bundleId.equals(that.bundleId) : that.bundleId != null) {
             return false;
         }
-        final SubscriptionJsonNoEvents other = (SubscriptionJsonNoEvents) obj;
-        if (billingPeriod == null) {
-            if (other.billingPeriod != null) {
-                return false;
-            }
-        } else if (!billingPeriod.equals(other.billingPeriod)) {
+        if (chargedThroughDate != null ? !chargedThroughDate.equals(that.chargedThroughDate) : that.chargedThroughDate != null) {
             return false;
         }
-        if (bundleId == null) {
-            if (other.bundleId != null) {
-                return false;
-            }
-        } else if (!bundleId.equals(other.bundleId)) {
+        if (priceList != null ? !priceList.equals(that.priceList) : that.priceList != null) {
             return false;
         }
-        if (priceList == null) {
-            if (other.priceList != null) {
-                return false;
-            }
-        } else if (!priceList.equals(other.priceList)) {
+        if (productCategory != null ? !productCategory.equals(that.productCategory) : that.productCategory != null) {
             return false;
         }
-        if (productCategory == null) {
-            if (other.productCategory != null) {
-                return false;
-            }
-        } else if (!productCategory.equals(other.productCategory)) {
+        if (productName != null ? !productName.equals(that.productName) : that.productName != null) {
             return false;
         }
-        if (productName == null) {
-            if (other.productName != null) {
-                return false;
-            }
-        } else if (!productName.equals(other.productName)) {
+        if (startDate != null ? !startDate.equals(that.startDate) : that.startDate != null) {
             return false;
         }
+
         return true;
     }
+
+    @Override
+    public int hashCode() {
+        int result = super.hashCode();
+        result = 31 * result + (startDate != null ? startDate.hashCode() : 0);
+        result = 31 * result + (bundleId != null ? bundleId.hashCode() : 0);
+        result = 31 * result + (productName != null ? productName.hashCode() : 0);
+        result = 31 * result + (productCategory != null ? productCategory.hashCode() : 0);
+        result = 31 * result + (billingPeriod != null ? billingPeriod.hashCode() : 0);
+        result = 31 * result + (priceList != null ? priceList.hashCode() : 0);
+        result = 31 * result + (chargedThroughDate != null ? chargedThroughDate.hashCode() : 0);
+        return result;
+    }
 }
diff --git a/jaxrs/src/test/java/com/ning/billing/jaxrs/json/TestSubscriptionJsonNoEvents.java b/jaxrs/src/test/java/com/ning/billing/jaxrs/json/TestSubscriptionJsonNoEvents.java
new file mode 100644
index 0000000..42d0794
--- /dev/null
+++ b/jaxrs/src/test/java/com/ning/billing/jaxrs/json/TestSubscriptionJsonNoEvents.java
@@ -0,0 +1,118 @@
+/*
+ * 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.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.ning.billing.catalog.api.BillingPeriod;
+import com.ning.billing.catalog.api.InternationalPrice;
+import com.ning.billing.catalog.api.Plan;
+import com.ning.billing.catalog.api.PlanPhase;
+import com.ning.billing.catalog.api.PriceList;
+import com.ning.billing.catalog.api.Product;
+import com.ning.billing.catalog.api.ProductCategory;
+import com.ning.billing.entitlement.api.user.Subscription;
+
+public class TestSubscriptionJsonNoEvents {
+    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 subscriptionId = UUID.randomUUID().toString();
+        final String bundleId = UUID.randomUUID().toString();
+        final DateTime startDate = new DateTime(DateTimeZone.UTC);
+        final String productName = UUID.randomUUID().toString();
+        final String productCategory = UUID.randomUUID().toString();
+        final String billingPeriod = UUID.randomUUID().toString();
+        final String priceList = UUID.randomUUID().toString();
+        final DateTime chargedThroughDate = new DateTime(DateTimeZone.UTC);
+        final SubscriptionJsonNoEvents subscriptionJsonNoEvents = new SubscriptionJsonNoEvents(subscriptionId, bundleId, startDate,
+                                                                                               productName, productCategory, billingPeriod,
+                                                                                               priceList, chargedThroughDate);
+        Assert.assertEquals(subscriptionJsonNoEvents.getSubscriptionId(), subscriptionId);
+        Assert.assertEquals(subscriptionJsonNoEvents.getBundleId(), bundleId);
+        Assert.assertEquals(subscriptionJsonNoEvents.getStartDate(), startDate);
+        Assert.assertEquals(subscriptionJsonNoEvents.getProductName(), productName);
+        Assert.assertEquals(subscriptionJsonNoEvents.getProductCategory(), productCategory);
+        Assert.assertEquals(subscriptionJsonNoEvents.getBillingPeriod(), billingPeriod);
+        Assert.assertEquals(subscriptionJsonNoEvents.getPriceList(), priceList);
+        Assert.assertEquals(subscriptionJsonNoEvents.getChargedThroughDate(), chargedThroughDate);
+
+        final String asJson = mapper.writeValueAsString(subscriptionJsonNoEvents);
+        Assert.assertEquals(asJson, "{\"subscriptionId\":\"" + subscriptionJsonNoEvents.getSubscriptionId() + "\"," +
+                "\"bundleId\":\"" + subscriptionJsonNoEvents.getBundleId() + "\"," +
+                "\"startDate\":\"" + subscriptionJsonNoEvents.getStartDate().toDateTimeISO().toString() + "\"," +
+                "\"productName\":\"" + subscriptionJsonNoEvents.getProductName() + "\"," +
+                "\"productCategory\":\"" + subscriptionJsonNoEvents.getProductCategory() + "\"," +
+                "\"billingPeriod\":\"" + subscriptionJsonNoEvents.getBillingPeriod() + "\"," +
+                "\"priceList\":\"" + subscriptionJsonNoEvents.getPriceList() + "\"," +
+                "\"chargedThroughDate\":\"" + subscriptionJsonNoEvents.getChargedThroughDate().toDateTimeISO().toString() + "\"}");
+
+        final SubscriptionJsonNoEvents fromJson = mapper.readValue(asJson, SubscriptionJsonNoEvents.class);
+        Assert.assertEquals(fromJson, subscriptionJsonNoEvents);
+    }
+
+    @Test(groups = "fast")
+    public void testFromSubscriptionSubscription() throws Exception {
+        final Product product = Mockito.mock(Product.class);
+        Mockito.when(product.getName()).thenReturn(UUID.randomUUID().toString());
+        Mockito.when(product.getCategory()).thenReturn(ProductCategory.STANDALONE);
+
+        final InternationalPrice price = Mockito.mock(InternationalPrice.class);
+        final PlanPhase planPhase = Mockito.mock(PlanPhase.class);
+        Mockito.when(planPhase.getRecurringPrice()).thenReturn(price);
+
+        final Plan plan = Mockito.mock(Plan.class);
+        Mockito.when(plan.getProduct()).thenReturn(product);
+        Mockito.when(plan.getName()).thenReturn(UUID.randomUUID().toString());
+        Mockito.when(plan.getBillingPeriod()).thenReturn(BillingPeriod.QUARTERLY);
+        Mockito.when(plan.getFinalPhase()).thenReturn(planPhase);
+
+        final PriceList priceList = Mockito.mock(PriceList.class);
+
+        final Subscription subscription = Mockito.mock(Subscription.class);
+        Mockito.when(subscription.getId()).thenReturn(UUID.randomUUID());
+        Mockito.when(subscription.getBundleId()).thenReturn(UUID.randomUUID());
+        Mockito.when(subscription.getStartDate()).thenReturn(new DateTime(DateTimeZone.UTC));
+        Mockito.when(subscription.getCurrentPlan()).thenReturn(plan);
+        Mockito.when(subscription.getCurrentPriceList()).thenReturn(priceList);
+        Mockito.when(subscription.getChargedThroughDate()).thenReturn(new DateTime(DateTimeZone.UTC));
+
+        final SubscriptionJsonNoEvents subscriptionJsonNoEvents = new SubscriptionJsonNoEvents(subscription);
+        Assert.assertEquals(subscriptionJsonNoEvents.getSubscriptionId(), subscription.getId().toString());
+        Assert.assertEquals(subscriptionJsonNoEvents.getStartDate(), subscription.getStartDate());
+        Assert.assertEquals(subscriptionJsonNoEvents.getBundleId(), subscription.getBundleId().toString());
+        Assert.assertEquals(subscriptionJsonNoEvents.getProductName(), subscription.getCurrentPlan().getProduct().getName());
+        Assert.assertEquals(subscriptionJsonNoEvents.getProductCategory(), subscription.getCurrentPlan().getProduct().getCategory().toString());
+        Assert.assertEquals(subscriptionJsonNoEvents.getBillingPeriod(), subscription.getCurrentPlan().getBillingPeriod().toString());
+        Assert.assertEquals(subscriptionJsonNoEvents.getChargedThroughDate(), subscription.getChargedThroughDate());
+    }
+}