Skip to content

Commit

Permalink
beatrix: Add test case to verify that invoice ietm IDs passed from pl…
Browse files Browse the repository at this point in the history
…ugin are honored by invoicing system. See #818
  • Loading branch information
sbrossie committed Nov 16, 2017
1 parent f55038b commit 4818a4f
Showing 1 changed file with 30 additions and 17 deletions.
Expand Up @@ -19,11 +19,10 @@


import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.UUID; import java.util.UUID;


import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;


import org.joda.time.LocalDate; import org.joda.time.LocalDate;
Expand Down Expand Up @@ -55,9 +54,12 @@
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; import org.testng.annotations.Test;


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


import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;


public class TestWithTaxItems extends TestIntegrationBase { public class TestWithTaxItems extends TestIntegrationBase {


Expand Down Expand Up @@ -143,7 +145,8 @@ public void testBasicTaxItems() throws Exception {
assertListenerStatus(); assertListenerStatus();


// Make sure TestInvoicePluginApi will return an additional TAX item // Make sure TestInvoicePluginApi will return an additional TAX item
testInvoicePluginApi.addTaxItem(UUID.randomUUID(), new TaxInvoiceItem(UUID.randomUUID(), null, account.getId(), null, "Tax Item", new LocalDate(2012, 5, 1), BigDecimal.ONE, account.getCurrency())); final UUID pluginInvoiceItemId = UUID.randomUUID();
testInvoicePluginApi.addTaxItem(new TaxInvoiceItem(pluginInvoiceItemId, null, account.getId(), null, "Tax Item", new LocalDate(2012, 5, 1), BigDecimal.ONE, account.getCurrency()));


// Remove AUTO_INVOICING_OFF => Invoice + Payment // Remove AUTO_INVOICING_OFF => Invoice + Payment
remove_AUTO_INVOICING_OFF_Tag(account.getId(), NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT); remove_AUTO_INVOICING_OFF_Tag(account.getId(), NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
Expand All @@ -152,14 +155,27 @@ public void testBasicTaxItems() throws Exception {
new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), InvoiceItemType.RECURRING, new BigDecimal("2.95")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), InvoiceItemType.RECURRING, new BigDecimal("2.95")),
new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), null, InvoiceItemType.TAX, new BigDecimal("1.0"))); new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), null, InvoiceItemType.TAX, new BigDecimal("1.0")));


final List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, callContext);
assertEquals(invoices.size(), 2);
final List<InvoiceItem> invoiceItems = invoices.get(1).getInvoiceItems();
final InvoiceItem taxItem = Iterables.tryFind(invoiceItems, new Predicate<InvoiceItem>() {
@Override
public boolean apply(final InvoiceItem input) {
return input.getInvoiceItemType() == InvoiceItemType.TAX;
}
}).orNull();
assertNotNull(taxItem);
// verify the ID is the one passed by the plugin #818
assertEquals(taxItem.getId(), pluginInvoiceItemId);

// Add AUTO_INVOICING_OFF and change to a higher plan on the same day that already include the 'Cleaning' ADD_ON, so it gets cancelled // Add AUTO_INVOICING_OFF and change to a higher plan on the same day that already include the 'Cleaning' ADD_ON, so it gets cancelled
add_AUTO_INVOICING_OFF_Tag(account.getId()); add_AUTO_INVOICING_OFF_Tag(account.getId());
busHandler.pushExpectedEvents(NextEvent.CHANGE, NextEvent.CANCEL, NextEvent.BLOCK); busHandler.pushExpectedEvents(NextEvent.CHANGE, NextEvent.CANCEL, NextEvent.BLOCK);
changeEntitlementAndCheckForCompletion(bpSubscription, "Shotgun", BillingPeriod.MONTHLY, BillingActionPolicy.IMMEDIATE); changeEntitlementAndCheckForCompletion(bpSubscription, "Shotgun", BillingPeriod.MONTHLY, BillingActionPolicy.IMMEDIATE);
assertListenerStatus(); assertListenerStatus();


// Make sure TestInvoicePluginApi will return an additional TAX item // Make sure TestInvoicePluginApi will return an additional TAX item
testInvoicePluginApi.addTaxItem(UUID.randomUUID(), new TaxInvoiceItem(UUID.randomUUID(), null, account.getId(), null, "Tax Item", new LocalDate(2012, 5, 1), BigDecimal.ONE, account.getCurrency())); testInvoicePluginApi.addTaxItem(new TaxInvoiceItem(UUID.randomUUID(), null, account.getId(), null, "Tax Item", new LocalDate(2012, 5, 1), BigDecimal.ONE, account.getCurrency()));


// Remove AUTO_INVOICING_OFF => Invoice + Payment // Remove AUTO_INVOICING_OFF => Invoice + Payment
remove_AUTO_INVOICING_OFF_Tag(account.getId(), NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT); remove_AUTO_INVOICING_OFF_Tag(account.getId(), NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
Expand All @@ -181,7 +197,7 @@ public void testBasicTaxItems() throws Exception {
assertListenerStatus(); assertListenerStatus();


// Make sure TestInvoicePluginApi will return an additional TAX item // Make sure TestInvoicePluginApi will return an additional TAX item
testInvoicePluginApi.addTaxItem(UUID.randomUUID(), new TaxInvoiceItem(UUID.randomUUID(), null, account.getId(), null, "Tax Item", new LocalDate(2012, 5, 1), BigDecimal.ONE, account.getCurrency())); testInvoicePluginApi.addTaxItem(new TaxInvoiceItem(UUID.randomUUID(), null, account.getId(), null, "Tax Item", new LocalDate(2012, 5, 1), BigDecimal.ONE, account.getCurrency()));


// Remove AUTO_INVOICING_OFF => Invoice + Payment // Remove AUTO_INVOICING_OFF => Invoice + Payment
remove_AUTO_INVOICING_OFF_Tag(account.getId(), NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT); remove_AUTO_INVOICING_OFF_Tag(account.getId(), NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
Expand Down Expand Up @@ -214,7 +230,6 @@ public void testDryRunTaxItemsWithCredits() throws Exception {
invoiceChecker.checkInvoice(account.getId(), 1, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), null, InvoiceItemType.FIXED, new BigDecimal("0"))); invoiceChecker.checkInvoice(account.getId(), 1, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), null, InvoiceItemType.FIXED, new BigDecimal("0")));
subscriptionChecker.checkSubscriptionCreated(bpSubscription.getId(), internalCallContext); subscriptionChecker.checkSubscriptionCreated(bpSubscription.getId(), internalCallContext);



busHandler.pushExpectedEvents(NextEvent.INVOICE); busHandler.pushExpectedEvents(NextEvent.INVOICE);
invoiceUserApi.insertCredit(account.getId(), new BigDecimal("100"), clock.getUTCToday(), account.getCurrency(), true, "VIP", callContext); invoiceUserApi.insertCredit(account.getId(), new BigDecimal("100"), clock.getUTCToday(), account.getCurrency(), true, "VIP", callContext);
assertListenerStatus(); assertListenerStatus();
Expand All @@ -224,7 +239,7 @@ public void testDryRunTaxItemsWithCredits() throws Exception {
new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), new LocalDate(2012, 4, 1), InvoiceItemType.CREDIT_ADJ, new BigDecimal("-100"))); new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), new LocalDate(2012, 4, 1), InvoiceItemType.CREDIT_ADJ, new BigDecimal("-100")));


// Make sure TestInvoicePluginApi will return an additional TAX item // Make sure TestInvoicePluginApi will return an additional TAX item
testInvoicePluginApi.addTaxItem(UUID.randomUUID(), new TaxInvoiceItem(UUID.randomUUID(), null, account.getId(), null, "Tax Item", new LocalDate(2012, 4, 1), BigDecimal.ONE, account.getCurrency())); testInvoicePluginApi.addTaxItem(new TaxInvoiceItem(UUID.randomUUID(), null, account.getId(), null, "Tax Item", new LocalDate(2012, 4, 1), BigDecimal.ONE, account.getCurrency()));


// Verify dry-run scenario // Verify dry-run scenario
final Invoice dryRunInvoice = invoiceUserApi.triggerInvoiceGeneration(account.getId(), new LocalDate(2012, 5, 1), new TestDryRunArguments(DryRunType.TARGET_DATE), callContext); final Invoice dryRunInvoice = invoiceUserApi.triggerInvoiceGeneration(account.getId(), new LocalDate(2012, 5, 1), new TestDryRunArguments(DryRunType.TARGET_DATE), callContext);
Expand All @@ -234,7 +249,7 @@ public void testDryRunTaxItemsWithCredits() throws Exception {
new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), new LocalDate(2012, 4, 1), InvoiceItemType.CBA_ADJ, new BigDecimal("-30.95")))); new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), new LocalDate(2012, 4, 1), InvoiceItemType.CBA_ADJ, new BigDecimal("-30.95"))));


// Make sure TestInvoicePluginApi will return an additional TAX item // Make sure TestInvoicePluginApi will return an additional TAX item
testInvoicePluginApi.addTaxItem(UUID.randomUUID(), new TaxInvoiceItem(UUID.randomUUID(), null, account.getId(), null, "Tax Item", new LocalDate(2012, 5, 1), BigDecimal.ONE, account.getCurrency())); testInvoicePluginApi.addTaxItem(new TaxInvoiceItem(UUID.randomUUID(), null, account.getId(), null, "Tax Item", new LocalDate(2012, 5, 1), BigDecimal.ONE, account.getCurrency()));


// Move to Evergreen PHASE to verify non-dry-run scenario // Move to Evergreen PHASE to verify non-dry-run scenario
busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE); busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE);
Expand Down Expand Up @@ -266,13 +281,13 @@ public void testUpdateTaxItems() throws Exception {


// Make sure TestInvoicePluginApi will return an additional TAX item // Make sure TestInvoicePluginApi will return an additional TAX item
final UUID invoiceTaxItemId = UUID.randomUUID(); final UUID invoiceTaxItemId = UUID.randomUUID();
testInvoicePluginApi.addTaxItem(invoiceTaxItemId, new TaxInvoiceItem(invoiceTaxItemId, null, account.getId(), null, "Tax Item", new LocalDate(2012, 4, 1), BigDecimal.ONE, account.getCurrency())); testInvoicePluginApi.addTaxItem(new TaxInvoiceItem(invoiceTaxItemId, null, account.getId(), null, "Tax Item", new LocalDate(2012, 4, 1), BigDecimal.ONE, account.getCurrency()));


// Insert external charge autoCommit = false => Invoice will be in DRAFT // Insert external charge autoCommit = false => Invoice will be in DRAFT
invoiceUserApi.insertExternalCharges(account.getId(), clock.getUTCNow().toLocalDate(), ImmutableList.<InvoiceItem>of(new ExternalChargeInvoiceItem(null, account.getId(), null, "foo", new LocalDate(2012, 4, 1), null, new BigDecimal("33.80"), account.getCurrency())), false, callContext); invoiceUserApi.insertExternalCharges(account.getId(), clock.getUTCNow().toLocalDate(), ImmutableList.<InvoiceItem>of(new ExternalChargeInvoiceItem(null, account.getId(), null, "foo", new LocalDate(2012, 4, 1), null, new BigDecimal("33.80"), account.getCurrency())), false, callContext);


// Make sure TestInvoicePluginApi **update** the original TAX item // Make sure TestInvoicePluginApi **update** the original TAX item
testInvoicePluginApi.addTaxItem(invoiceTaxItemId, new TaxInvoiceItem(invoiceTaxItemId, null, account.getId(), null, "Tax Item", new LocalDate(2012, 4, 1), new BigDecimal("12.45"), account.getCurrency())); testInvoicePluginApi.addTaxItem(new TaxInvoiceItem(invoiceTaxItemId, null, account.getId(), null, "Tax Item", new LocalDate(2012, 4, 1), new BigDecimal("12.45"), account.getCurrency()));


// Move to Evergreen PHASE, but invoice remains in DRAFT mode // Move to Evergreen PHASE, but invoice remains in DRAFT mode
busHandler.pushExpectedEvents(NextEvent.PHASE /*, NextEvent.INVOICE */); busHandler.pushExpectedEvents(NextEvent.PHASE /*, NextEvent.INVOICE */);
Expand All @@ -296,17 +311,16 @@ public void testUpdateTaxItems() throws Exception {


public class TestInvoicePluginApi implements InvoicePluginApi { public class TestInvoicePluginApi implements InvoicePluginApi {


private final Map<UUID, TaxInvoiceItem> taxItems; private final List<TaxInvoiceItem> taxItems;


public TestInvoicePluginApi() { public TestInvoicePluginApi() {
taxItems = new HashMap<UUID, TaxInvoiceItem>(); taxItems = new ArrayList<TaxInvoiceItem>();
} }


@Override @Override
public List<InvoiceItem> getAdditionalInvoiceItems(final Invoice invoice, final boolean isDryRun, final Iterable<PluginProperty> pluginProperties, final CallContext callContext) { public List<InvoiceItem> getAdditionalInvoiceItems(final Invoice invoice, final boolean isDryRun, final Iterable<PluginProperty> pluginProperties, final CallContext callContext) {
final List<InvoiceItem> result = new ArrayList<InvoiceItem>(); final List<InvoiceItem> result = new ArrayList<InvoiceItem>();
for (UUID itemId : taxItems.keySet()) { for (TaxInvoiceItem item : taxItems) {
final TaxInvoiceItem item = taxItems.get(itemId);
result.add(new TaxInvoiceItem(item.getId(), invoice.getId(), invoice.getAccountId(), item.getBundleId(), "Tax Item", item.getStartDate(), item.getAmount(), invoice.getCurrency())); result.add(new TaxInvoiceItem(item.getId(), invoice.getId(), invoice.getAccountId(), item.getBundleId(), "Tax Item", item.getStartDate(), item.getAmount(), invoice.getCurrency()));
} }
taxItems.clear(); taxItems.clear();
Expand All @@ -317,10 +331,9 @@ public void reset() {
taxItems.clear(); taxItems.clear();
} }


public void addTaxItem(final UUID invoiceItemId, final TaxInvoiceItem item) { public void addTaxItem(final TaxInvoiceItem item) {
taxItems.put(invoiceItemId, item); taxItems.add(item);
} }



} }
} }

1 comment on commit 4818a4f

@pierre
Copy link
Member

@pierre pierre commented on 4818a4f Nov 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.