Пример #1
0
  @Test(groups = "slow")
  public void testXXX() throws Exception {

    final AccountData accountData = getAccountData(1);
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);

    // We take april as it has 30 days (easier to play with BCD)
    // Set clock to the initial start date - we implicitly assume here that the account timezone is
    // UTC
    clock.setDay(new LocalDate(2012, 4, 1));

    //
    // Create original subscription (Trial PHASE) -> $0 invoice.
    final DefaultEntitlement bpSubscription =
        createBaseEntitlementAndCheckForCompletion(
            account.getId(),
            "bundleKey",
            "Pistol",
            ProductCategory.BASE,
            BillingPeriod.MONTHLY,
            NextEvent.CREATE,
            NextEvent.INVOICE);
    invoiceChecker.checkInvoice(
        account.getId(),
        1,
        callContext,
        new ExpectedInvoiceItemCheck(
            new LocalDate(2012, 4, 1), null, InvoiceItemType.FIXED, new BigDecimal("0")));
    subscriptionChecker.checkSubscriptionCreated(bpSubscription.getId(), internalCallContext);

    // Move to Evergreen PHASE, but add AUTO_INVOICING_OFF => No invoice
    add_AUTO_INVOICING_OFF_Tag(account.getId());
    busHandler.pushExpectedEvent(NextEvent.PHASE);
    clock.addDays(30);
    assertListenerStatus();

    // Add Cleaning ADD_ON => No Invoice
    busHandler.pushExpectedEvent(NextEvent.CREATE);
    addAOEntitlementAndCheckForCompletion(
        bpSubscription.getBundleId(), "Cleaning", ProductCategory.ADD_ON, BillingPeriod.MONTHLY);
    assertListenerStatus();

    // Make sure TestInvoicePluginApi will return an additional TAX item
    testInvoicePluginApi.addTaxItem();

    // Remove AUTO_INVOICING_OFF => Invoice + Payment
    remove_AUTO_INVOICING_OFF_Tag(account.getId(), NextEvent.INVOICE, NextEvent.PAYMENT);
    invoiceChecker.checkInvoice(
        account.getId(),
        2,
        callContext,
        new ExpectedInvoiceItemCheck(
            new LocalDate(2012, 5, 1),
            new LocalDate(2012, 6, 1),
            InvoiceItemType.RECURRING,
            new BigDecimal("29.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")));

    // 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());
    busHandler.pushExpectedEvents(NextEvent.CHANGE, NextEvent.CANCEL, NextEvent.BLOCK);
    changeEntitlementAndCheckForCompletion(
        bpSubscription, "Shotgun", BillingPeriod.MONTHLY, BillingActionPolicy.IMMEDIATE);
    assertListenerStatus();

    // Make sure TestInvoicePluginApi will return an additional TAX item
    testInvoicePluginApi.addTaxItem();

    // Remove AUTO_INVOICING_OFF => Invoice + Payment
    remove_AUTO_INVOICING_OFF_Tag(account.getId(), NextEvent.INVOICE, NextEvent.PAYMENT);
    invoiceChecker.checkInvoice(
        account.getId(),
        2,
        callContext,
        new ExpectedInvoiceItemCheck(
            new LocalDate(2012, 5, 1),
            new LocalDate(2012, 6, 1),
            InvoiceItemType.RECURRING,
            new BigDecimal("29.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")));

    invoiceChecker.checkInvoice(
        account.getId(),
        3,
        callContext,
        new ExpectedInvoiceItemCheck(
            new LocalDate(2012, 5, 1),
            new LocalDate(2012, 6, 1),
            InvoiceItemType.RECURRING,
            new BigDecimal("249.95")),
        new ExpectedInvoiceItemCheck(
            new LocalDate(2012, 5, 1),
            new LocalDate(2012, 6, 1),
            InvoiceItemType.REPAIR_ADJ,
            new BigDecimal("-29.95")),
        new ExpectedInvoiceItemCheck(
            new LocalDate(2012, 5, 1),
            new LocalDate(2012, 6, 1),
            InvoiceItemType.REPAIR_ADJ,
            new BigDecimal("-2.95")),
        new ExpectedInvoiceItemCheck(
            new LocalDate(2012, 5, 1), null, InvoiceItemType.TAX, new BigDecimal("1.0")));

    // Add AUTO_INVOICING_OFF and change to a higher plan on the same day
    add_AUTO_INVOICING_OFF_Tag(account.getId());
    busHandler.pushExpectedEvent(NextEvent.CHANGE);
    changeEntitlementAndCheckForCompletion(
        bpSubscription, "Assault-Rifle", BillingPeriod.MONTHLY, BillingActionPolicy.IMMEDIATE);
    assertListenerStatus();

    // Make sure TestInvoicePluginApi will return an additional TAX item
    testInvoicePluginApi.addTaxItem();

    // Remove AUTO_INVOICING_OFF => Invoice + Payment
    remove_AUTO_INVOICING_OFF_Tag(account.getId(), NextEvent.INVOICE, NextEvent.PAYMENT);

    invoiceChecker.checkInvoice(
        account.getId(),
        3,
        callContext,
        new ExpectedInvoiceItemCheck(
            new LocalDate(2012, 5, 1),
            new LocalDate(2012, 6, 1),
            InvoiceItemType.RECURRING,
            new BigDecimal("249.95")),
        new ExpectedInvoiceItemCheck(
            new LocalDate(2012, 5, 1),
            new LocalDate(2012, 6, 1),
            InvoiceItemType.REPAIR_ADJ,
            new BigDecimal("-29.95")),
        new ExpectedInvoiceItemCheck(
            new LocalDate(2012, 5, 1),
            new LocalDate(2012, 6, 1),
            InvoiceItemType.REPAIR_ADJ,
            new BigDecimal("-2.95")),
        new ExpectedInvoiceItemCheck(
            new LocalDate(2012, 5, 1), null, InvoiceItemType.TAX, new BigDecimal("1.0")));

    invoiceChecker.checkInvoice(
        account.getId(),
        4,
        callContext,
        new ExpectedInvoiceItemCheck(
            new LocalDate(2012, 5, 1),
            new LocalDate(2012, 6, 1),
            InvoiceItemType.RECURRING,
            new BigDecimal("599.95")),
        new ExpectedInvoiceItemCheck(
            new LocalDate(2012, 5, 1),
            new LocalDate(2012, 6, 1),
            InvoiceItemType.REPAIR_ADJ,
            new BigDecimal("-249.95")),
        new ExpectedInvoiceItemCheck(
            new LocalDate(2012, 5, 1), null, InvoiceItemType.TAX, new BigDecimal("1.0")));
  }
  private void setupIntegration(
      final PaymentPluginApiException expectedException,
      final RuntimeException expectedRuntimeException)
      throws Exception {

    final PaymentPluginApiWithTestControl paymentPluginApi = getTestPluginPaymentApi();

    final AccountData accountData = getAccountData(1);
    final Account account = createAccountWithOsgiPaymentMethod(accountData);

    // We take april as it has 30 days (easier to play with BCD)
    // Set clock to the initial start date - we implicitly assume here that the account timezone is
    // UTC
    clock.setDay(new LocalDate(2012, 4, 1));
    //
    // CREATE SUBSCRIPTION AND EXPECT BOTH EVENTS: NextEvent.CREATE NextEvent.INVOICE
    //
    final DefaultEntitlement baseEntitlement =
        createBaseEntitlementAndCheckForCompletion(
            account.getId(),
            "externalKey",
            "Shotgun",
            ProductCategory.BASE,
            BillingPeriod.MONTHLY,
            NextEvent.CREATE,
            NextEvent.INVOICE);
    //
    // ADD ADD_ON ON THE SAME DAY TO TRIGGER PAYMENT
    //

    final List<NextEvent> expectedEvents = new LinkedList<NextEvent>();
    expectedEvents.add(NextEvent.CREATE);
    expectedEvents.add(NextEvent.INVOICE);
    if (expectedException == null && expectedRuntimeException == null) {
      expectedEvents.add(NextEvent.PAYMENT);
    } else if (expectedException != null) {
      expectedEvents.add(NextEvent.PAYMENT_PLUGIN_ERROR);
      paymentPluginApi.setPaymentPluginApiExceptionOnNextCalls(expectedException);
    } else if (expectedRuntimeException != null) {
      expectedEvents.add(NextEvent.PAYMENT_PLUGIN_ERROR);
      paymentPluginApi.setPaymentRuntimeExceptionOnNextCalls(expectedRuntimeException);
    }

    final DefaultEntitlement aoEntitlement =
        addAOEntitlementAndCheckForCompletion(
            baseEntitlement.getBundleId(),
            "Telescopic-Scope",
            ProductCategory.ADD_ON,
            BillingPeriod.MONTHLY,
            expectedEvents.toArray(new NextEvent[expectedEvents.size()]));

    final Invoice invoice =
        invoiceChecker.checkInvoice(
            account.getId(),
            2,
            callContext,
            new ExpectedInvoiceItemCheck(
                new LocalDate(2012, 4, 1),
                new LocalDate(2012, 5, 1),
                InvoiceItemType.RECURRING,
                new BigDecimal("399.95")));

    if (expectedException == null && expectedRuntimeException == null) {
      paymentChecker.checkPayment(
          account.getId(),
          1,
          callContext,
          new ExpectedPaymentCheck(
              new LocalDate(2012, 4, 1),
              new BigDecimal("399.95"),
              PaymentStatus.SUCCESS,
              invoice.getId(),
              Currency.USD));
    } else if (expectedException != null) {
      paymentChecker.checkPayment(
          account.getId(),
          1,
          callContext,
          new ExpectedPaymentCheck(
              new LocalDate(2012, 4, 1),
              new BigDecimal("399.95"),
              PaymentStatus.PLUGIN_FAILURE,
              invoice.getId(),
              Currency.USD));
    } else if (expectedRuntimeException != null) {
      paymentChecker.checkPayment(
          account.getId(),
          1,
          callContext,
          new ExpectedPaymentCheck(
              new LocalDate(2012, 4, 1),
              new BigDecimal("399.95"),
              PaymentStatus.PLUGIN_FAILURE,
              invoice.getId(),
              Currency.USD));
    }
  }