Search in sources :

Example 1 with AuditLogWithHistory

use of org.killbill.billing.util.audit.AuditLogWithHistory in project killbill by killbill.

the class TestBlockingDao method testWithAuditAndHistory.

@Test(groups = "slow")
public void testWithAuditAndHistory() throws Exception {
    final UUID uuid = createAccount(getAccountData(1)).getId();
    final String overdueStateName = "StateBlock";
    final String service = "auditAndHistory";
    testListener.pushExpectedEvent(NextEvent.BLOCK);
    final BlockingState blockingState = new DefaultBlockingState(uuid, BlockingStateType.ACCOUNT, overdueStateName, service, false, true, false, clock.getUTCNow());
    blockingStateDao.setBlockingStatesAndPostBlockingTransitionEvent(ImmutableMap.<BlockingState, Optional<UUID>>of(blockingState, Optional.<UUID>absent()), internalCallContext);
    assertListenerStatus();
    final List<AuditLogWithHistory> h1 = blockingStateDao.getBlockingStateAuditLogsWithHistoryForId(blockingState.getId(), AuditLevel.FULL, internalCallContext);
    Assert.assertEquals(h1.size(), 1);
    final AuditLogWithHistory firstHistoryRow = h1.get(0);
    Assert.assertEquals(firstHistoryRow.getChangeType(), ChangeType.INSERT);
    final BlockingStateModelDao firstBlockingState = (BlockingStateModelDao) firstHistoryRow.getEntity();
    Assert.assertFalse(firstBlockingState.getBlockChange());
    Assert.assertTrue(firstBlockingState.getBlockEntitlement());
    Assert.assertFalse(firstBlockingState.getBlockBilling());
    Assert.assertTrue(firstBlockingState.isActive());
    blockingStateDao.unactiveBlockingState(blockingState.getId(), internalCallContext);
    final List<AuditLogWithHistory> h2 = blockingStateDao.getBlockingStateAuditLogsWithHistoryForId(blockingState.getId(), AuditLevel.FULL, internalCallContext);
    Assert.assertEquals(h2.size(), 2);
    final AuditLogWithHistory secondHistoryRow = h2.get(1);
    Assert.assertEquals(secondHistoryRow.getChangeType(), ChangeType.DELETE);
    final BlockingStateModelDao secondBlockingState = (BlockingStateModelDao) secondHistoryRow.getEntity();
    Assert.assertFalse(secondBlockingState.getBlockChange());
    Assert.assertTrue(secondBlockingState.getBlockEntitlement());
    Assert.assertFalse(secondBlockingState.getBlockBilling());
    Assert.assertFalse(secondBlockingState.isActive());
}
Also used : DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) BlockingState(org.killbill.billing.entitlement.api.BlockingState) UUID(java.util.UUID) AuditLogWithHistory(org.killbill.billing.util.audit.AuditLogWithHistory) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) Test(org.testng.annotations.Test)

Example 2 with AuditLogWithHistory

use of org.killbill.billing.util.audit.AuditLogWithHistory in project killbill by killbill.

the class TestJanitor method testUnknownEntriesWithExceptions.

@Test(groups = "slow")
public void testUnknownEntriesWithExceptions() throws PaymentApiException, EventBusException {
    final BigDecimal requestedAmount = BigDecimal.TEN;
    final String paymentExternalKey = "minus";
    final String transactionExternalKey = "plus";
    // Make sure the state as seen by the plugin will be in PaymentPluginStatus.ERROR, which will be returned later to Janitor
    mockPaymentProviderPlugin.makeNextPaymentFailWithException();
    try {
        testListener.pushExpectedEvent(NextEvent.PAYMENT_PLUGIN_ERROR);
        paymentApi.createAuthorization(account, account.getPaymentMethodId(), null, requestedAmount, account.getCurrency(), null, paymentExternalKey, transactionExternalKey, ImmutableList.<PluginProperty>of(), callContext);
    } catch (PaymentApiException ignore) {
        testListener.assertListenerStatus();
    }
    final Payment payment = paymentApi.getPaymentByExternalKey(paymentExternalKey, false, false, ImmutableList.<PluginProperty>of(), callContext);
    // Artificially move the transaction status to UNKNOWN
    final String paymentStateName = paymentSMHelper.getErroredStateForTransaction(TransactionType.AUTHORIZE).toString();
    testListener.pushExpectedEvent(NextEvent.PAYMENT_PLUGIN_ERROR);
    paymentDao.updatePaymentAndTransactionOnCompletion(account.getId(), null, payment.getId(), TransactionType.AUTHORIZE, paymentStateName, paymentStateName, payment.getTransactions().get(0).getId(), TransactionStatus.UNKNOWN, requestedAmount, account.getCurrency(), "foo", "bar", true, internalCallContext);
    testListener.assertListenerStatus();
    // Move clock for notification to be processed
    clock.addDeltaFromReality(5 * 60 * 1000);
    // NO because we will keep retrying as we can't fix it...
    // assertNotificationsCompleted(internalCallContext, 5);
    final List<AuditLogWithHistory> paymentTransactionHistoryBeforeJanitor = paymentDao.getPaymentTransactionAuditLogsWithHistoryForId(payment.getTransactions().get(0).getId(), AuditLevel.FULL, internalCallContext);
    Assert.assertEquals(paymentTransactionHistoryBeforeJanitor.size(), 3);
    // Nothing new happened
    final List<AuditLogWithHistory> paymentTransactionHistoryAfterJanitor = paymentDao.getPaymentTransactionAuditLogsWithHistoryForId(payment.getTransactions().get(0).getId(), AuditLevel.FULL, internalCallContext);
    Assert.assertEquals(paymentTransactionHistoryAfterJanitor.size(), 3);
}
Also used : Payment(org.killbill.billing.payment.api.Payment) PaymentApiException(org.killbill.billing.payment.api.PaymentApiException) AuditLogWithHistory(org.killbill.billing.util.audit.AuditLogWithHistory) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 3 with AuditLogWithHistory

use of org.killbill.billing.util.audit.AuditLogWithHistory in project killbill by killbill.

the class TestJanitor method testUnknownEntriesWithFailures.

// Flaky, see https://github.com/killbill/killbill/issues/860
@Test(groups = "slow", retryAnalyzer = FlakyRetryAnalyzer.class)
public void testUnknownEntriesWithFailures() throws PaymentApiException, EventBusException {
    final BigDecimal requestedAmount = BigDecimal.TEN;
    final String paymentExternalKey = "minus";
    final String transactionExternalKey = "plus";
    // Make sure the state as seen by the plugin will be in PaymentPluginStatus.ERROR, which will be returned later to Janitor
    mockPaymentProviderPlugin.makeNextPaymentFailWithError();
    testListener.pushExpectedEvent(NextEvent.PAYMENT_ERROR);
    final Payment payment = paymentApi.createAuthorization(account, account.getPaymentMethodId(), null, requestedAmount, account.getCurrency(), null, paymentExternalKey, transactionExternalKey, ImmutableList.<PluginProperty>of(), callContext);
    testListener.assertListenerStatus();
    // Artificially move the transaction status to UNKNOWN
    final String paymentStateName = paymentSMHelper.getErroredStateForTransaction(TransactionType.AUTHORIZE).toString();
    testListener.pushExpectedEvent(NextEvent.PAYMENT_PLUGIN_ERROR);
    paymentDao.updatePaymentAndTransactionOnCompletion(account.getId(), null, payment.getId(), TransactionType.AUTHORIZE, paymentStateName, paymentStateName, payment.getTransactions().get(0).getId(), TransactionStatus.UNKNOWN, requestedAmount, account.getCurrency(), "foo", "bar", true, internalCallContext);
    testListener.assertListenerStatus();
    final List<AuditLogWithHistory> paymentTransactionHistoryBeforeJanitor = paymentDao.getPaymentTransactionAuditLogsWithHistoryForId(payment.getTransactions().get(0).getId(), AuditLevel.FULL, internalCallContext);
    Assert.assertEquals(paymentTransactionHistoryBeforeJanitor.size(), 3);
    // Move clock for notification to be processed
    testListener.pushExpectedEvent(NextEvent.PAYMENT_ERROR);
    clock.addDeltaFromReality(5 * 60 * 1000);
    assertNotificationsCompleted(internalCallContext, 5);
    testListener.assertListenerStatus();
    // Proves the Janitor ran (and updated the transaction)
    final List<AuditLogWithHistory> paymentTransactionHistoryAfterJanitor = paymentDao.getPaymentTransactionAuditLogsWithHistoryForId(payment.getTransactions().get(0).getId(), AuditLevel.FULL, internalCallContext);
    Assert.assertEquals(paymentTransactionHistoryAfterJanitor.size(), 4);
    PaymentTransactionModelDao history3 = (PaymentTransactionModelDao) paymentTransactionHistoryAfterJanitor.get(3).getEntity();
    Assert.assertEquals(history3.getTransactionStatus(), TransactionStatus.PAYMENT_FAILURE);
    final Payment updatedPayment = paymentApi.getPayment(payment.getId(), false, false, ImmutableList.<PluginProperty>of(), callContext);
    // Janitor should have moved us to PAYMENT_FAILURE
    assertEquals(updatedPayment.getTransactions().get(0).getTransactionStatus(), TransactionStatus.PAYMENT_FAILURE);
}
Also used : Payment(org.killbill.billing.payment.api.Payment) PaymentTransactionModelDao(org.killbill.billing.payment.dao.PaymentTransactionModelDao) AuditLogWithHistory(org.killbill.billing.util.audit.AuditLogWithHistory) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 4 with AuditLogWithHistory

use of org.killbill.billing.util.audit.AuditLogWithHistory in project killbill by killbill.

the class TestAccountDao method testUpdate.

@Test(groups = "slow", description = "Test Account DAO: basic update (1)")
public void testUpdate() throws Exception {
    final AccountModelDao account = createTestAccount();
    accountDao.create(account, internalCallContext);
    refreshCallContext(account.getId());
    final AccountModelDao createdAccount = accountDao.getAccountByKey(account.getExternalKey(), internalCallContext);
    List<AuditLogWithHistory> auditLogsWithHistory = accountDao.getAuditLogsWithHistoryForId(account.getId(), AuditLevel.FULL, internalCallContext);
    Assert.assertEquals(auditLogsWithHistory.size(), 1);
    final AccountModelDao history1 = (AccountModelDao) auditLogsWithHistory.get(0).getEntity();
    Assert.assertEquals(auditLogsWithHistory.get(0).getChangeType(), ChangeType.INSERT);
    Assert.assertEquals(history1.getAccountRecordId(), createdAccount.getRecordId());
    Assert.assertEquals(history1.getTenantRecordId(), createdAccount.getTenantRecordId());
    Assert.assertEquals(history1.getExternalKey(), createdAccount.getExternalKey());
    Assert.assertEquals(history1.getMigrated(), createdAccount.getMigrated());
    Assert.assertEquals(history1.getTimeZone(), createdAccount.getTimeZone());
    Assert.assertEquals(history1.getLocale(), createdAccount.getLocale());
    final AccountData accountData = new MockAccountBuilder(new DefaultAccount(account)).migrated(false).locale("FR-CA").build();
    final AccountModelDao updatedAccount = new AccountModelDao(account.getId(), accountData);
    accountDao.update(updatedAccount, true, internalCallContext);
    final AccountModelDao retrievedAccount = accountDao.getAccountByKey(account.getExternalKey(), internalCallContext);
    checkAccountsEqual(retrievedAccount, updatedAccount);
    auditLogsWithHistory = accountDao.getAuditLogsWithHistoryForId(retrievedAccount.getId(), AuditLevel.FULL, internalCallContext);
    Assert.assertEquals(auditLogsWithHistory.size(), 2);
    final AccountModelDao history2 = (AccountModelDao) auditLogsWithHistory.get(1).getEntity();
    Assert.assertEquals(auditLogsWithHistory.get(0).getChangeType(), ChangeType.INSERT);
    Assert.assertEquals(auditLogsWithHistory.get(1).getChangeType(), ChangeType.UPDATE);
    Assert.assertEquals(history2.getAccountRecordId(), retrievedAccount.getRecordId());
    Assert.assertEquals(history2.getTenantRecordId(), retrievedAccount.getTenantRecordId());
    Assert.assertEquals(history2.getExternalKey(), retrievedAccount.getExternalKey());
    Assert.assertEquals(history2.getMigrated(), retrievedAccount.getMigrated());
    Assert.assertEquals(history2.getTimeZone(), retrievedAccount.getTimeZone());
    Assert.assertEquals(history2.getLocale(), retrievedAccount.getLocale());
    final AccountData accountData2 = new MockAccountBuilder(new DefaultAccount(updatedAccount)).locale("en_US").build();
    final AccountModelDao updatedAccount2 = new AccountModelDao(account.getId(), accountData2);
    accountDao.update(updatedAccount2, true, internalCallContext);
    final AccountModelDao retrievedAccount2 = accountDao.getAccountByKey(account.getExternalKey(), internalCallContext);
    checkAccountsEqual(retrievedAccount2, updatedAccount2);
    auditLogsWithHistory = accountDao.getAuditLogsWithHistoryForId(retrievedAccount2.getId(), AuditLevel.FULL, internalCallContext);
    Assert.assertEquals(auditLogsWithHistory.size(), 3);
    final AccountModelDao history3 = (AccountModelDao) auditLogsWithHistory.get(2).getEntity();
    Assert.assertEquals(auditLogsWithHistory.get(0).getChangeType(), ChangeType.INSERT);
    Assert.assertEquals(auditLogsWithHistory.get(1).getChangeType(), ChangeType.UPDATE);
    Assert.assertEquals(auditLogsWithHistory.get(2).getChangeType(), ChangeType.UPDATE);
    Assert.assertEquals(history3.getAccountRecordId(), retrievedAccount2.getRecordId());
    Assert.assertEquals(history3.getTenantRecordId(), retrievedAccount2.getTenantRecordId());
    Assert.assertEquals(history3.getExternalKey(), retrievedAccount2.getExternalKey());
    Assert.assertEquals(history3.getMigrated(), retrievedAccount2.getMigrated());
    Assert.assertEquals(history3.getTimeZone(), retrievedAccount2.getTimeZone());
    Assert.assertEquals(history3.getLocale(), retrievedAccount2.getLocale());
}
Also used : DefaultAccount(org.killbill.billing.account.api.DefaultAccount) MockAccountBuilder(org.killbill.billing.mock.MockAccountBuilder) DefaultMutableAccountData(org.killbill.billing.account.api.DefaultMutableAccountData) MutableAccountData(org.killbill.billing.account.api.MutableAccountData) AccountData(org.killbill.billing.account.api.AccountData) AuditLogWithHistory(org.killbill.billing.util.audit.AuditLogWithHistory) Test(org.testng.annotations.Test)

Example 5 with AuditLogWithHistory

use of org.killbill.billing.util.audit.AuditLogWithHistory in project killbill by killbill.

the class AccountResource method getAccountEmailAuditLogsWithHistory.

@TimedResource
@GET
@Path("/{accountId:" + UUID_PATTERN + "}/" + EMAILS + "/{accountEmailId:" + UUID_PATTERN + "}/" + AUDIT_LOG_WITH_HISTORY)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Retrieve account email audit logs with history by id", response = AuditLogJson.class, responseContainer = "List")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Account not found") })
public Response getAccountEmailAuditLogsWithHistory(@PathParam("accountId") final UUID accountId, @PathParam("accountEmailId") final UUID accountEmailId, @javax.ws.rs.core.Context final HttpServletRequest request) throws AccountApiException {
    final TenantContext tenantContext = context.createTenantContextWithAccountId(accountId, request);
    final List<AuditLogWithHistory> auditLogWithHistory = accountUserApi.getEmailAuditLogsWithHistoryForId(accountEmailId, AuditLevel.FULL, tenantContext);
    return Response.status(Status.OK).entity(getAuditLogsWithHistory(auditLogWithHistory)).build();
}
Also used : TenantContext(org.killbill.billing.util.callcontext.TenantContext) AuditLogWithHistory(org.killbill.billing.util.audit.AuditLogWithHistory) Path(javax.ws.rs.Path) TimedResource(org.killbill.commons.metrics.TimedResource) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

AuditLogWithHistory (org.killbill.billing.util.audit.AuditLogWithHistory)23 ApiOperation (io.swagger.annotations.ApiOperation)16 ApiResponses (io.swagger.annotations.ApiResponses)16 GET (javax.ws.rs.GET)16 Path (javax.ws.rs.Path)16 Produces (javax.ws.rs.Produces)16 TenantContext (org.killbill.billing.util.callcontext.TenantContext)16 TimedResource (org.killbill.commons.metrics.TimedResource)16 Test (org.testng.annotations.Test)7 UUID (java.util.UUID)3 BigDecimal (java.math.BigDecimal)2 DateTime (org.joda.time.DateTime)2 Payment (org.killbill.billing.payment.api.Payment)2 Account (org.killbill.billing.account.api.Account)1 AccountData (org.killbill.billing.account.api.AccountData)1 DefaultAccount (org.killbill.billing.account.api.DefaultAccount)1 DefaultMutableAccountData (org.killbill.billing.account.api.DefaultMutableAccountData)1 MutableAccountData (org.killbill.billing.account.api.MutableAccountData)1 BlockingState (org.killbill.billing.entitlement.api.BlockingState)1 DefaultBlockingState (org.killbill.billing.junction.DefaultBlockingState)1