killbill-aplcache

context: use latest ClockUtil functions Signed-off-by:

2/16/2016 5:00:45 PM

Details

diff --git a/api/src/main/java/org/killbill/billing/callcontext/InternalCallContext.java b/api/src/main/java/org/killbill/billing/callcontext/InternalCallContext.java
index ca0cd0c..bc78b08 100644
--- a/api/src/main/java/org/killbill/billing/callcontext/InternalCallContext.java
+++ b/api/src/main/java/org/killbill/billing/callcontext/InternalCallContext.java
@@ -63,8 +63,8 @@ public class InternalCallContext extends InternalTenantContext {
         this.contextUserType = userType;
         this.reasonCode = reasonCode;
         this.comments = comment;
-        this.createdDate = toUTCDateTime(createdDate);
-        this.updatedDate = toUTCDateTime(updatedDate);
+        this.createdDate = createdDate;
+        this.updatedDate = updatedDate;
     }
 
     public InternalCallContext(final Long tenantRecordId, final CallContext callContext, final DateTime utcNow) {
diff --git a/api/src/main/java/org/killbill/billing/callcontext/TimeAwareContext.java b/api/src/main/java/org/killbill/billing/callcontext/TimeAwareContext.java
index 1f913d0..89f710c 100644
--- a/api/src/main/java/org/killbill/billing/callcontext/TimeAwareContext.java
+++ b/api/src/main/java/org/killbill/billing/callcontext/TimeAwareContext.java
@@ -21,9 +21,9 @@ import javax.annotation.Nullable;
 
 import org.joda.time.DateTime;
 import org.joda.time.DateTimeZone;
-import org.joda.time.IllegalInstantException;
 import org.joda.time.LocalDate;
 import org.joda.time.LocalTime;
+import org.killbill.clock.ClockUtil;
 
 public class TimeAwareContext {
 
@@ -35,47 +35,16 @@ public class TimeAwareContext {
         this.referenceTime = computeReferenceTime(referenceDateTime);
     }
 
-    /// Generic functions
-    /// TODO Move to ClockUtil
-
-    // Create a DateTime object forcing the time zone to be UTC
-    protected DateTime toUTCDateTime(final DateTime dateTime) {
-        return toDateTime(dateTime, DateTimeZone.UTC);
-    }
-
-    // Create a DateTime object using the specified timezone (usually, the one on the account)
-    public DateTime toDateTime(final DateTime dateTime, final DateTimeZone accountTimeZone) {
-        return dateTime.toDateTime(accountTimeZone);
-    }
-
-    /// DateTime <-> LocalDate transformations
-
-    // Create a DateTime object using the specified reference time and timezone (usually, the one on the account)
     public DateTime toUTCDateTime(final LocalDate localDate) {
         validateContext();
 
-         DateTime targetDateTime;
-        try {
-            targetDateTime = new DateTime(localDate.getYear(),
-                                          localDate.getMonthOfYear(),
-                                          localDate.getDayOfMonth(),
-                                          getReferenceTime().getHourOfDay(),
-                                          getReferenceTime().getMinuteOfHour(),
-                                          getReferenceTime().getSecondOfMinute(),
-                                          getFixedOffsetTimeZone());
-        } catch (final IllegalInstantException e) {
-            // DST gap (shouldn't happen when using fixed offset timezones)
-            targetDateTime = localDate.toDateTimeAtStartOfDay(getFixedOffsetTimeZone());
-        }
-
-        return toUTCDateTime(targetDateTime);
+        return ClockUtil.toUTCDateTime(localDate, getReferenceTime(), getFixedOffsetTimeZone());
     }
 
-    // Create a LocalDate object using the specified timezone (usually, the one on the account), respecting the offset at the time of the referenceDateTime
     public LocalDate toLocalDate(final DateTime dateTime) {
         validateContext();
 
-        return new LocalDate(dateTime, getFixedOffsetTimeZone());
+        return ClockUtil.toLocalDate(dateTime, getFixedOffsetTimeZone());
     }
 
     private void validateContext() {
@@ -84,16 +53,19 @@ public class TimeAwareContext {
         }
     }
 
+    // For convenience (used in tests)
+
+    //@VisibleForTesting
     protected LocalTime computeReferenceTime(@Nullable final DateTime referenceTime) {
-        return referenceTime == null ? null : toDateTime(referenceTime, getFixedOffsetTimeZone()).toLocalTime();
+        return referenceTime == null ? null : ClockUtil.toDateTime(referenceTime, getFixedOffsetTimeZone()).toLocalTime();
     }
 
-    // For convenience, to be overridden in tests
-
+    //@VisibleForTesting
     public DateTimeZone getFixedOffsetTimeZone() {
         return fixedOffsetTimeZone;
     }
 
+    //@VisibleForTesting
     public LocalTime getReferenceTime() {
         return referenceTime;
     }
diff --git a/jaxrs/src/main/java/org/killbill/billing/jaxrs/resources/InvoiceResource.java b/jaxrs/src/main/java/org/killbill/billing/jaxrs/resources/InvoiceResource.java
index fed8945..848a7fd 100644
--- a/jaxrs/src/main/java/org/killbill/billing/jaxrs/resources/InvoiceResource.java
+++ b/jaxrs/src/main/java/org/killbill/billing/jaxrs/resources/InvoiceResource.java
@@ -52,7 +52,6 @@ import javax.ws.rs.core.Response.Status;
 import javax.ws.rs.core.UriInfo;
 
 import org.joda.time.DateTime;
-import org.joda.time.DateTimeZone;
 import org.joda.time.LocalDate;
 import org.killbill.billing.ErrorCode;
 import org.killbill.billing.ObjectType;
@@ -366,7 +365,7 @@ public class InvoiceResource extends JaxRsResourceBase {
 
         final Account account = accountUserApi.getAccountById(UUID.fromString(accountId), callContext);
 
-        final DryRunArguments dryRunArguments = new DefaultDryRunArguments(dryRunSubscriptionSpec, account.getTimeZone(), account.getCurrency(), clock);
+        final DryRunArguments dryRunArguments = new DefaultDryRunArguments(dryRunSubscriptionSpec, account, clock);
         try {
             final Invoice generatedInvoice = invoiceApi.triggerInvoiceGeneration(UUID.fromString(accountId), inputDate, dryRunArguments,
                                                                                  callContext);
@@ -958,7 +957,7 @@ public class InvoiceResource extends JaxRsResourceBase {
         private final BillingActionPolicy billingPolicy;
         private final List<PlanPhasePriceOverride> overrides;
 
-        public DefaultDryRunArguments(final InvoiceDryRunJson input, final DateTimeZone accountTimeZone, final Currency currency, final Clock clock) {
+        public DefaultDryRunArguments(final InvoiceDryRunJson input, final Account account, final Clock clock) {
             if (input == null) {
                 this.dryRunType = DryRunType.TARGET_DATE;
                 this.action = null;
@@ -973,7 +972,7 @@ public class InvoiceResource extends JaxRsResourceBase {
                 this.action = input.getDryRunAction() != null ? SubscriptionEventType.valueOf(input.getDryRunAction()) : null;
                 this.subscriptionId = input.getSubscriptionId() != null ? UUID.fromString(input.getSubscriptionId()) : null;
                 this.bundleId = input.getBundleId() != null ? UUID.fromString(input.getBundleId()) : null;
-                this.effectiveDate = input.getEffectiveDate() != null ? ClockUtil.computeDateTimeWithUTCReferenceTime(input.getEffectiveDate(), clock.getUTCNow().toLocalTime(), accountTimeZone, clock) : null;
+                this.effectiveDate = input.getEffectiveDate() != null ? ClockUtil.toUTCDateTime(input.getEffectiveDate(), ClockUtil.toDateTime(account.getReferenceTime(), account.getFixedOffsetTimeZone()).toLocalTime(), account.getFixedOffsetTimeZone()) : null;
                 this.billingPolicy = input.getBillingPolicy() != null ? BillingActionPolicy.valueOf(input.getBillingPolicy()) : null;
                 final PlanPhaseSpecifier planPhaseSpecifier = (input.getProductName() != null &&
                                                                input.getProductCategory() != null &&
@@ -991,9 +990,9 @@ public class InvoiceResource extends JaxRsResourceBase {
                                      @Override
                                      public PlanPhasePriceOverride apply(@Nullable final PhasePriceOverrideJson input) {
                                          if (input.getPhaseName() != null) {
-                                             return new DefaultPlanPhasePriceOverride(input.getPhaseName(), currency, input.getFixedPrice(), input.getRecurringPrice());
+                                             return new DefaultPlanPhasePriceOverride(input.getPhaseName(), account.getCurrency(), input.getFixedPrice(), input.getRecurringPrice());
                                          } else {
-                                             return new DefaultPlanPhasePriceOverride(planPhaseSpecifier, currency, input.getFixedPrice(), input.getRecurringPrice());
+                                             return new DefaultPlanPhasePriceOverride(planPhaseSpecifier, account.getCurrency(), input.getFixedPrice(), input.getRecurringPrice());
                                          }
                                      }
                                  })) : ImmutableList.<PlanPhasePriceOverride>of();
diff --git a/junction/src/main/java/org/killbill/billing/junction/plumbing/billing/BillCycleDayCalculator.java b/junction/src/main/java/org/killbill/billing/junction/plumbing/billing/BillCycleDayCalculator.java
index 84b3bc1..4cee36a 100644
--- a/junction/src/main/java/org/killbill/billing/junction/plumbing/billing/BillCycleDayCalculator.java
+++ b/junction/src/main/java/org/killbill/billing/junction/plumbing/billing/BillCycleDayCalculator.java
@@ -42,6 +42,7 @@ import org.killbill.billing.subscription.api.SubscriptionBase;
 import org.killbill.billing.subscription.api.SubscriptionBaseInternalApi;
 import org.killbill.billing.subscription.api.SubscriptionBaseTransitionType;
 import org.killbill.billing.subscription.api.user.SubscriptionBaseApiException;
+import org.killbill.clock.ClockUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -143,7 +144,7 @@ public class BillCycleDayCalculator {
         }
 
         final DateTime date = plan.dateOfFirstRecurringNonZeroCharge(subscription.getStartDate(), initialPhaseType);
-        final int bcdLocal = context.toDateTime(date, account.getTimeZone()).getDayOfMonth();
+        final int bcdLocal = ClockUtil.toDateTime(date, account.getTimeZone()).getDayOfMonth();
         log.info("Calculated BCD: subscription id {}, subscription start {}, timezone {}, bcd {}",
                  subscription.getId(), date.toDateTimeISO(), account.getTimeZone(), bcdLocal);