Search in sources :

Example 1 with AccountModelDao

use of org.killbill.billing.account.dao.AccountModelDao in project killbill by killbill.

the class TestIntegrationParentInvoice method testParentingWithFuturePhaseEvent.

@Test(groups = "slow")
public void testParentingWithFuturePhaseEvent() throws Exception {
    final int billingDay = 14;
    final DateTime initialCreationDate = new DateTime(2015, 5, 15, 0, 0, 0, 0, testTimeZone);
    // set clock to the initial start date
    clock.setTime(initialCreationDate);
    final Account parentAccount = createAccountWithNonOsgiPaymentMethod(getAccountData(billingDay));
    Account childAccount = createAccountWithNonOsgiPaymentMethod(getAccountData(billingDay));
    // Verify mapping
    childAccount = accountUserApi.getAccountById(childAccount.getId(), callContext);
    assertNull(childAccount.getParentAccountId());
    assertFalse(childAccount.isPaymentDelegatedToParent());
    List<Account> childrenAccounts = accountUserApi.getChildrenAccounts(parentAccount.getId(), callContext);
    assertEquals(childrenAccounts.size(), 0);
    // Create subscription
    createBaseEntitlementAndCheckForCompletion(childAccount.getId(), "bundleKey1", "Shotgun", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    // First child invoice over TRIAL period
    List<Invoice> childInvoices = invoiceUserApi.getInvoicesByAccount(childAccount.getId(), false, callContext);
    assertEquals(childInvoices.size(), 1);
    assertEquals(childInvoices.get(0).getBalance().compareTo(BigDecimal.ZERO), 0);
    // Add parent to the child -- the child still pays its invoices though
    AccountModelDao childAccountModelDao = new AccountModelDao(childAccount.getId(), childAccount);
    childAccountModelDao.setParentAccountId(parentAccount.getId());
    childAccountModelDao.setIsPaymentDelegatedToParent(false);
    accountUserApi.updateAccount(new DefaultAccount(childAccountModelDao), callContext);
    // Verify mapping
    childAccount = accountUserApi.getAccountById(childAccount.getId(), callContext);
    assertEquals(childAccount.getParentAccountId(), parentAccount.getId());
    assertFalse(childAccount.isPaymentDelegatedToParent());
    childrenAccounts = accountUserApi.getChildrenAccounts(parentAccount.getId(), callContext);
    assertEquals(childrenAccounts.size(), 1);
    assertEquals(childrenAccounts.get(0).getId(), childAccount.getId());
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
    clock.addDays(30);
    assertListenerStatus();
    // The parent still has no invoice
    List<Invoice> parentInvoices = invoiceUserApi.getInvoicesByAccount(parentAccount.getId(), false, callContext);
    assertEquals(parentInvoices.size(), 0);
    // Second child invoice over Recurring period
    childInvoices = invoiceUserApi.getInvoicesByAccount(childAccount.getId(), false, callContext);
    assertEquals(childInvoices.size(), 2);
    assertEquals(childInvoices.get(1).getBalance().compareTo(BigDecimal.ZERO), 0);
    assertEquals(childInvoices.get(1).getPayments().size(), 1);
    assertEquals(paymentApi.getPayment(childInvoices.get(1).getPayments().get(0).getPaymentId(), false, false, ImmutableList.<PluginProperty>of(), callContext).getPaymentMethodId(), childAccount.getPaymentMethodId());
    // The child now delegates its payments
    childAccountModelDao = new AccountModelDao(childAccount.getId(), childAccount);
    childAccountModelDao.setIsPaymentDelegatedToParent(true);
    accountUserApi.updateAccount(new DefaultAccount(childAccountModelDao), callContext);
    // Verify mapping
    childAccount = accountUserApi.getAccountById(childAccount.getId(), callContext);
    assertEquals(childAccount.getParentAccountId(), parentAccount.getId());
    assertTrue(childAccount.isPaymentDelegatedToParent());
    childrenAccounts = accountUserApi.getChildrenAccounts(parentAccount.getId(), callContext);
    assertEquals(childrenAccounts.size(), 1);
    assertEquals(childrenAccounts.get(0).getId(), childAccount.getId());
    busHandler.pushExpectedEvents(NextEvent.INVOICE);
    clock.addDays(30);
    assertListenerStatus();
    // Moving a day the NotificationQ calls the commitInvoice. No payment is expected
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
    clock.addDays(1);
    assertListenerStatus();
    // The parent now owns the invoice
    parentInvoices = invoiceUserApi.getInvoicesByAccount(parentAccount.getId(), false, callContext);
    assertEquals(parentInvoices.size(), 1);
    final Invoice parentInvoice = parentInvoices.get(0);
    assertEquals(parentInvoice.getNumberOfItems(), 1);
    assertEquals(parentInvoice.getStatus(), InvoiceStatus.COMMITTED);
    assertTrue(parentInvoice.isParentInvoice());
    assertEquals(parentInvoice.getBalance().compareTo(BigDecimal.ZERO), 0);
    assertEquals(parentInvoice.getPayments().size(), 1);
    assertEquals(paymentApi.getPayment(parentInvoice.getPayments().get(0).getPaymentId(), false, false, ImmutableList.<PluginProperty>of(), callContext).getPaymentMethodId(), parentAccount.getPaymentMethodId());
    // Third child invoice over Recurring period
    childInvoices = invoiceUserApi.getInvoicesByAccount(childAccount.getId(), false, callContext);
    assertEquals(childInvoices.size(), 3);
    assertEquals(childInvoices.get(2).getBalance().compareTo(BigDecimal.ZERO), 0);
}
Also used : DefaultAccount(org.killbill.billing.account.api.DefaultAccount) Account(org.killbill.billing.account.api.Account) AccountModelDao(org.killbill.billing.account.dao.AccountModelDao) DefaultAccount(org.killbill.billing.account.api.DefaultAccount) Invoice(org.killbill.billing.invoice.api.Invoice) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 2 with AccountModelDao

use of org.killbill.billing.account.dao.AccountModelDao in project killbill by killbill.

the class DefaultAccountApiBase method getAccountByKey.

protected Account getAccountByKey(final String key, final InternalTenantContext context) throws AccountApiException {
    final AccountModelDao accountModelDao = accountDao.getAccountByKey(key, context);
    if (accountModelDao == null) {
        throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_KEY, key);
    }
    final Account account = new DefaultAccount(accountModelDao);
    accountCacheController.putIfAbsent(account.getId(), new DefaultImmutableAccountData(account));
    return account;
}
Also used : AccountModelDao(org.killbill.billing.account.dao.AccountModelDao) DefaultAccount(org.killbill.billing.account.api.DefaultAccount) Account(org.killbill.billing.account.api.Account) DefaultAccount(org.killbill.billing.account.api.DefaultAccount) DefaultImmutableAccountData(org.killbill.billing.account.api.DefaultImmutableAccountData) AccountApiException(org.killbill.billing.account.api.AccountApiException)

Example 3 with AccountModelDao

use of org.killbill.billing.account.dao.AccountModelDao in project killbill by killbill.

the class TestDefaultAccountUserApi method testCreateChildAccountWithInvalidParent.

@Test(groups = "slow", description = "Test Account create Child with a non existing Parent", expectedExceptions = AccountApiException.class, expectedExceptionsMessageRegExp = "Account does not exist for id .*")
public void testCreateChildAccountWithInvalidParent() throws Exception {
    final AccountModelDao childAccountModel = createTestAccount();
    childAccountModel.setParentAccountId(UUID.randomUUID());
    final AccountData childAccountData = new DefaultAccount(childAccountModel);
    final Account childAccount = accountUserApi.createAccount(childAccountData, callContext);
}
Also used : AccountModelDao(org.killbill.billing.account.dao.AccountModelDao) DefaultAccount(org.killbill.billing.account.api.DefaultAccount) AccountTestUtils.createTestAccount(org.killbill.billing.account.AccountTestUtils.createTestAccount) Account(org.killbill.billing.account.api.Account) DefaultAccount(org.killbill.billing.account.api.DefaultAccount) DefaultMutableAccountData(org.killbill.billing.account.api.DefaultMutableAccountData) AccountTestUtils.createAccountData(org.killbill.billing.account.AccountTestUtils.createAccountData) MutableAccountData(org.killbill.billing.account.api.MutableAccountData) AccountData(org.killbill.billing.account.api.AccountData) Test(org.testng.annotations.Test)

Example 4 with AccountModelDao

use of org.killbill.billing.account.dao.AccountModelDao in project killbill by killbill.

the class TestDefaultAccountUserApi method testCreateAccountWithExternalKeyOverLimit.

@Test(groups = "slow", description = "Test Account creation with External Key over limit")
public void testCreateAccountWithExternalKeyOverLimit() throws Exception {
    AccountModelDao accountModelDao = createTestAccount();
    // Set an externalKey of 256 characters (over limit)
    accountModelDao.setExternalKey("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis,.");
    final AccountData accountData = new DefaultAccount(accountModelDao);
    try {
        accountUserApi.createAccount(accountData, callContext);
        Assert.fail();
    } catch (final AccountApiException e) {
        assertEquals(e.getCode(), ErrorCode.EXTERNAL_KEY_LIMIT_EXCEEDED.getCode());
    }
}
Also used : AccountModelDao(org.killbill.billing.account.dao.AccountModelDao) DefaultAccount(org.killbill.billing.account.api.DefaultAccount) DefaultMutableAccountData(org.killbill.billing.account.api.DefaultMutableAccountData) AccountTestUtils.createAccountData(org.killbill.billing.account.AccountTestUtils.createAccountData) MutableAccountData(org.killbill.billing.account.api.MutableAccountData) AccountData(org.killbill.billing.account.api.AccountData) AccountApiException(org.killbill.billing.account.api.AccountApiException) Test(org.testng.annotations.Test)

Example 5 with AccountModelDao

use of org.killbill.billing.account.dao.AccountModelDao in project killbill by killbill.

the class AccountTestUtils method checkAccountsEqual.

public static void checkAccountsEqual(final AccountData retrievedAccount, final AccountData account) {
    final UUID fakeId = UUID.randomUUID();
    checkAccountsEqual(new AccountModelDao(fakeId, retrievedAccount), new AccountModelDao(fakeId, account));
}
Also used : AccountModelDao(org.killbill.billing.account.dao.AccountModelDao) UUID(java.util.UUID)

Aggregations

AccountModelDao (org.killbill.billing.account.dao.AccountModelDao)15 DefaultAccount (org.killbill.billing.account.api.DefaultAccount)14 Account (org.killbill.billing.account.api.Account)11 Test (org.testng.annotations.Test)9 AccountApiException (org.killbill.billing.account.api.AccountApiException)6 AccountData (org.killbill.billing.account.api.AccountData)6 DefaultMutableAccountData (org.killbill.billing.account.api.DefaultMutableAccountData)6 MutableAccountData (org.killbill.billing.account.api.MutableAccountData)6 AccountTestUtils.createAccountData (org.killbill.billing.account.AccountTestUtils.createAccountData)5 AccountTestUtils.createTestAccount (org.killbill.billing.account.AccountTestUtils.createTestAccount)5 UUID (java.util.UUID)2 DateTime (org.joda.time.DateTime)2 Invoice (org.killbill.billing.invoice.api.Invoice)2 BigDecimal (java.math.BigDecimal)1 DateTimeZone (org.joda.time.DateTimeZone)1 DefaultImmutableAccountData (org.killbill.billing.account.api.DefaultImmutableAccountData)1 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)1 Currency (org.killbill.billing.catalog.api.Currency)1 AccountCreationInternalEvent (org.killbill.billing.events.AccountCreationInternalEvent)1