Skip to content

Commit

Permalink
Merge pull request #1526 from killbill/work-for-1503
Browse files Browse the repository at this point in the history
Work for 1503
  • Loading branch information
sbrossie committed Nov 1, 2021
2 parents 570340c + 0434134 commit 081be96
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.killbill.billing.ErrorCode;
import org.killbill.billing.ObjectType;
import org.killbill.billing.account.api.Account;
import org.killbill.billing.api.TestApiListener.NextEvent;
Expand Down Expand Up @@ -51,6 +53,7 @@

import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;

import static com.tc.util.Assert.fail;
Expand All @@ -63,6 +66,45 @@ public class TestIntegrationDryRunInvoice extends TestIntegrationBase {
private static final DryRunArguments DRY_RUN_UPCOMING_INVOICE_ARG = new TestDryRunArguments(DryRunType.UPCOMING_INVOICE);
private static final DryRunArguments DRY_RUN_TARGET_DATE_ARG = new TestDryRunArguments(DryRunType.TARGET_DATE);



@Test(groups = "slow", description = "https://github.com/killbill/killbill/issues/1503")
public void testForIssue_1503() throws Exception {
clock.setTime(new DateTime("2021-04-01T3:56:02"));

final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(1));
assertNotNull(account);

busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Blowdart", BillingPeriod.MONTHLY, "notrial", null);
final UUID entitlementId = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), "Something", null, null, false, true, ImmutableList.<PluginProperty>of(), callContext);
final Entitlement bp = entitlementApi.getEntitlementForId(entitlementId, callContext);
assertListenerStatus();

invoiceChecker.checkInvoice(account.getId(), 1, callContext,
new ExpectedInvoiceItemCheck(new LocalDate(2021, 4, 1), new LocalDate(2021, 5, 1), InvoiceItemType.RECURRING, new BigDecimal("29.95")));

bp.cancelEntitlementWithDate(new LocalDate(2021, 6, 1), true, ImmutableList.<PluginProperty>of(), callContext);

// We see one recurring from 2021-5-1 -> 2021-6-1
final Invoice dryRunInvoice1 = invoiceUserApi.triggerDryRunInvoiceGeneration(bp.getAccountId(), new LocalDate(2021, 5, 1), DRY_RUN_TARGET_DATE_ARG, callContext);
invoiceChecker.checkInvoiceNoAudits(dryRunInvoice1, ImmutableList.of(new ExpectedInvoiceItemCheck(new LocalDate(2021, 5, 1), new LocalDate(2021, 6, 1), InvoiceItemType.RECURRING, new BigDecimal("29.95"))));

// From any date > 2021-5-1, we should see nothing
Set<LocalDate> nextDates = ImmutableSet.of(new LocalDate(2021, 6, 1), /* cancelation date */
new LocalDate(2021, 6, 3),
new LocalDate(2021, 7, 1));

for (LocalDate targetDate : nextDates) {
try {
invoiceUserApi.triggerDryRunInvoiceGeneration(bp.getAccountId(), targetDate, DRY_RUN_TARGET_DATE_ARG, callContext);
Assert.fail(String.format("Should not have received an invoice for date %s", targetDate));
} catch(final InvoiceApiException e) {
assertEquals(e.getCode(), ErrorCode.INVOICE_NOTHING_TO_DO.getCode());
}
}

}
//
// Basic test with one subscription that verifies the behavior of using invoice dryRun api with no date
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ private Invoice processDryRun_TARGET_DATE_Invoice(final UUID accountId, final Lo
if (prev != null && prev.compareTo(cur) == 0) {
continue;
}
if (cur.compareTo(targetDate) >= 0) {
if (cur.compareTo(targetDate) > 0) {
break;
}

Expand Down Expand Up @@ -526,12 +526,17 @@ public boolean apply(final Invoice input) {
prev = cur;
}

pluginProperties = new LinkedList<PluginProperty>();
pluginProperties.add(new PluginProperty(DRY_RUN_CUR_DATE_PROP, targetDate, false));
pluginProperties.add(new PluginProperty(DRY_RUN_TARGET_DATE_PROP, targetDate, false));
final InvoiceWithFutureNotifications invoiceWithFutureNotifications = processAccountWithLockAndInputTargetDate(accountId, targetDate, billingEvents, accountInvoices, dryRunInfo, false, pluginProperties, context);
final Invoice targetInvoice = invoiceWithFutureNotifications != null ? invoiceWithFutureNotifications.getInvoice() : null;
return targetInvoice != null ? targetInvoice : additionalInvoice;
final boolean isTargetDateAlignedOnLastTransition = prev != null && prev.compareTo(targetDate) == 0;
if (isTargetDateAlignedOnLastTransition) {
return additionalInvoice;
} else { /* The provided targetDate does not coincide with any transition, so we try it and return what we find if there is anything or default to previous transition */
pluginProperties = new LinkedList<PluginProperty>();
pluginProperties.add(new PluginProperty(DRY_RUN_CUR_DATE_PROP, targetDate, false));
pluginProperties.add(new PluginProperty(DRY_RUN_TARGET_DATE_PROP, targetDate, false));
final InvoiceWithFutureNotifications invoiceWithFutureNotifications = processAccountWithLockAndInputTargetDate(accountId, targetDate, billingEvents, accountInvoices, dryRunInfo, false, pluginProperties, context);
final Invoice targetInvoice = invoiceWithFutureNotifications != null ? invoiceWithFutureNotifications.getInvoice() : null;
return targetInvoice != null ? targetInvoice : additionalInvoice;
}
}

private void parkAccount(final UUID accountId, final InternalCallContext context) {
Expand Down

0 comments on commit 081be96

Please sign in to comment.