From 0f46360ab84378f62b48d1d6612e94be126a2f62 Mon Sep 17 00:00:00 2001 From: Jonathan Cobb Date: Thu, 1 Oct 2020 10:48:30 -0400 Subject: [PATCH] add support for fast-forwarding by month, fix recurring billing test by using month steps instead of days --- .../bubble/test/BubbleApiRunnerListener.java | 20 +++++++++++++++++-- .../tests/payment/recurring_billing.json | 18 ++++++++--------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/bubble-server/src/test/java/bubble/test/BubbleApiRunnerListener.java b/bubble-server/src/test/java/bubble/test/BubbleApiRunnerListener.java index 96b321ef..8f98f287 100644 --- a/bubble-server/src/test/java/bubble/test/BubbleApiRunnerListener.java +++ b/bubble-server/src/test/java/bubble/test/BubbleApiRunnerListener.java @@ -7,10 +7,10 @@ package bubble.test; import bubble.BubbleHandlebars; import bubble.cloud.CloudServiceType; import bubble.cloud.payment.stripe.StripePaymentDriver; +import bubble.cloud.payment.stripe.mock.MockStripePaymentDriver; import bubble.dao.account.AccountDAO; import bubble.dao.cloud.BubbleDomainDAO; import bubble.dao.cloud.CloudServiceDAO; -import bubble.cloud.payment.stripe.mock.MockStripePaymentDriver; import bubble.model.account.Account; import bubble.model.cloud.CloudService; import bubble.server.BubbleConfiguration; @@ -20,6 +20,7 @@ import com.stripe.model.Token; import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.cobbzilla.wizard.client.script.SimpleApiRunnerListener; +import org.joda.time.DateTime; import java.util.HashMap; import java.util.List; @@ -30,6 +31,7 @@ import static org.cobbzilla.util.daemon.ZillaRuntime.*; import static org.cobbzilla.util.string.StringUtil.splitAndTrim; import static org.cobbzilla.util.system.Sleep.sleep; import static org.cobbzilla.util.time.TimeUtil.parseDuration; +import static org.joda.time.DurationFieldType.months; @Slf4j public class BubbleApiRunnerListener extends SimpleApiRunnerListener { @@ -57,7 +59,21 @@ public class BubbleApiRunnerListener extends SimpleApiRunnerListener { if (before == null) return; if (before.startsWith(FAST_FORWARD_AND_BILL)) { final List parts = splitAndTrim(before.substring(FAST_FORWARD_AND_BILL.length()), " "); - final long delta = parseDuration(parts.get(0)); + final long delta; + if (parts.get(0).endsWith("M")) { + // we're fast-forwarding in months + final String part = parts.get(0); + final int direction = part.startsWith("-") ? -1 : 1; + final int count = Integer.parseInt(part.startsWith("-") || part.startsWith("+") + ? part.substring(1, part.length()-1) + : part.substring(0, part.length()-1)); + final long now = now(); + final DateTime start = new DateTime(now); + final DateTime later = new DateTime(now).withFieldAdded(months(), direction * count); + delta = later.getMillis() - start.getMillis(); + } else { + delta = parseDuration(parts.get(0)); + } final long sleepTime = parts.size() > 1 ? parseDuration(parts.get(1)) : DEFAULT_BILLING_SLEEP; getStripePaymentDriver().flushCaches(); incrementSystemTimeOffset(delta); diff --git a/bubble-server/src/test/resources/models/tests/payment/recurring_billing.json b/bubble-server/src/test/resources/models/tests/payment/recurring_billing.json index 9285c2e6..5dbdddb1 100644 --- a/bubble-server/src/test/resources/models/tests/payment/recurring_billing.json +++ b/bubble-server/src/test/resources/models/tests/payment/recurring_billing.json @@ -196,8 +196,8 @@ }, { - "before": "fast_forward_and_bill 31d", - "comment": "1st fast-forward: +31 days, verify a new bill exists for accountPlan", + "before": "fast_forward_and_bill +1M", + "comment": "1st fast-forward: +1 month, verify a new bill exists for accountPlan", "request": { "uri": "me/plans/{{accountPlan.uuid}}/bills" }, "response": { "check": [ @@ -229,8 +229,8 @@ }, { - "before": "fast_forward_and_bill 31d", - "comment": "2nd fast-forward: fast-forward another +31 days, verify a new bill exists for accountPlan", + "before": "fast_forward_and_bill +1M", + "comment": "2nd fast-forward: fast-forward another month, verify a new bill exists for accountPlan", "request": { "uri": "me/plans/{{accountPlan.uuid}}/bills" }, "response": { "check": [ @@ -262,8 +262,8 @@ }, { - "before": "fast_forward_and_bill 66d 60s", - "comment": "3rd fast-forward: fast-forward even more, +66 days, we have missed a billing cycle, so two new bills should be created", + "before": "fast_forward_and_bill 2M 60s", + "comment": "3rd fast-forward: fast-forward even more, 2 months, we have missed a billing cycle, so two new bills should be created", "request": { "uri": "me/plans/{{accountPlan.uuid}}/bills" }, "response": { "check": [ @@ -308,8 +308,8 @@ }, { - "before": "fast_forward_and_bill 31d", - "comment": "4nd fast-forward: fast-forward another +33 days, verify new bill (unpaid) created", + "before": "fast_forward_and_bill 1M", + "comment": "4nd fast-forward: fast-forward another month, verify new bill (unpaid) created", "request": { "uri": "me/plans/{{accountPlan.uuid}}/bills" }, "response": { "store": "bills", @@ -359,7 +359,7 @@ { "before": "fast_forward_and_bill 7d", - "comment": "5th fast-forward: fast-forward another +6 days, bill is still unpaid, bubble is stopped", + "comment": "5th fast-forward: fast-forward another +7 days, bill is still unpaid, bubble is stopped", "request": { "session": "userSession", "uri": "me/plans/{{accountPlan.uuid}}/bills"