Skip to content

Commit

Permalink
Issue#335 - tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wwjbatista committed May 3, 2018
1 parent 16a5486 commit 0b3b0fa
Show file tree
Hide file tree
Showing 15 changed files with 363 additions and 107 deletions.
Expand Up @@ -193,4 +193,9 @@ public boolean apply(final AccountModelDao input) {
public List<AuditLogWithHistory> getAuditLogsWithHistoryForId(final UUID accountId, final AuditLevel auditLevel, final InternalTenantContext context) throws AccountApiException { public List<AuditLogWithHistory> getAuditLogsWithHistoryForId(final UUID accountId, final AuditLevel auditLevel, final InternalTenantContext context) throws AccountApiException {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }

@Override
public List<AuditLogWithHistory> getEmailAuditLogsWithHistoryForId(final UUID accountEmailId, final AuditLevel auditLevel, final InternalTenantContext context) throws AccountApiException {
throw new UnsupportedOperationException();
}
} }
44 changes: 10 additions & 34 deletions payment/src/test/java/org/killbill/billing/payment/TestJanitor.java
Expand Up @@ -47,6 +47,7 @@
import org.killbill.billing.payment.bus.PaymentBusEventHandler; import org.killbill.billing.payment.bus.PaymentBusEventHandler;
import org.killbill.billing.payment.core.janitor.Janitor; import org.killbill.billing.payment.core.janitor.Janitor;
import org.killbill.billing.payment.dao.PaymentAttemptModelDao; import org.killbill.billing.payment.dao.PaymentAttemptModelDao;
import org.killbill.billing.payment.dao.PaymentModelDao;
import org.killbill.billing.payment.dao.PaymentTransactionModelDao; import org.killbill.billing.payment.dao.PaymentTransactionModelDao;
import org.killbill.billing.payment.glue.DefaultPaymentService; import org.killbill.billing.payment.glue.DefaultPaymentService;
import org.killbill.billing.payment.invoice.InvoicePaymentControlPluginApi; import org.killbill.billing.payment.invoice.InvoicePaymentControlPluginApi;
Expand All @@ -55,6 +56,8 @@
import org.killbill.billing.payment.provider.DefaultNoOpPaymentInfoPlugin; import org.killbill.billing.payment.provider.DefaultNoOpPaymentInfoPlugin;
import org.killbill.billing.payment.provider.MockPaymentProviderPlugin; import org.killbill.billing.payment.provider.MockPaymentProviderPlugin;
import org.killbill.billing.platform.api.KillbillConfigSource; import org.killbill.billing.platform.api.KillbillConfigSource;
import org.killbill.billing.util.api.AuditLevel;
import org.killbill.billing.util.audit.AuditLogWithHistory;
import org.killbill.billing.util.callcontext.InternalCallContextFactory; import org.killbill.billing.util.callcontext.InternalCallContextFactory;
import org.killbill.billing.util.entity.dao.DBRouterUntyped; import org.killbill.billing.util.entity.dao.DBRouterUntyped;
import org.killbill.billing.util.entity.dao.DBRouterUntyped.THREAD_STATE; import org.killbill.billing.util.entity.dao.DBRouterUntyped.THREAD_STATE;
Expand Down Expand Up @@ -331,7 +334,7 @@ public void testUnknownEntriesWithFailures() throws PaymentApiException, EventBu
"foo", "bar", internalCallContext); "foo", "bar", internalCallContext);
testListener.assertListenerStatus(); testListener.assertListenerStatus();


final List<PaymentTransactionModelDao> paymentTransactionHistoryBeforeJanitor = getPaymentTransactionHistory(transactionExternalKey); final List<AuditLogWithHistory> paymentTransactionHistoryBeforeJanitor = paymentDao.getPaymentTransactionAuditLogsWithHistoryForId(payment.getTransactions().get(0).getId(), AuditLevel.FULL, internalCallContext);
Assert.assertEquals(paymentTransactionHistoryBeforeJanitor.size(), 3); Assert.assertEquals(paymentTransactionHistoryBeforeJanitor.size(), 3);


// Move clock for notification to be processed // Move clock for notification to be processed
Expand All @@ -341,9 +344,11 @@ public void testUnknownEntriesWithFailures() throws PaymentApiException, EventBu
testListener.assertListenerStatus(); testListener.assertListenerStatus();


// Proves the Janitor ran (and updated the transaction) // Proves the Janitor ran (and updated the transaction)
final List<PaymentTransactionModelDao> paymentTransactionHistoryAfterJanitor = getPaymentTransactionHistory(transactionExternalKey); final List<AuditLogWithHistory> paymentTransactionHistoryAfterJanitor = paymentDao.getPaymentTransactionAuditLogsWithHistoryForId(payment.getTransactions().get(0).getId(), AuditLevel.FULL, internalCallContext);
Assert.assertEquals(paymentTransactionHistoryAfterJanitor.size(), 4); Assert.assertEquals(paymentTransactionHistoryAfterJanitor.size(), 4);
Assert.assertEquals(paymentTransactionHistoryAfterJanitor.get(3).getTransactionStatus(), TransactionStatus.PAYMENT_FAILURE);
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); final Payment updatedPayment = paymentApi.getPayment(payment.getId(), false, false, ImmutableList.<PluginProperty>of(), callContext);
// Janitor should have moved us to PAYMENT_FAILURE // Janitor should have moved us to PAYMENT_FAILURE
Expand Down Expand Up @@ -381,11 +386,11 @@ public void testUnknownEntriesWithExceptions() throws PaymentApiException, Event
// NO because we will keep retrying as we can't fix it... // NO because we will keep retrying as we can't fix it...
//assertNotificationsCompleted(internalCallContext, 5); //assertNotificationsCompleted(internalCallContext, 5);


final List<PaymentTransactionModelDao> paymentTransactionHistoryBeforeJanitor = getPaymentTransactionHistory(transactionExternalKey); final List<AuditLogWithHistory> paymentTransactionHistoryBeforeJanitor = paymentDao.getPaymentTransactionAuditLogsWithHistoryForId(payment.getTransactions().get(0).getId(), AuditLevel.FULL, internalCallContext);
Assert.assertEquals(paymentTransactionHistoryBeforeJanitor.size(), 3); Assert.assertEquals(paymentTransactionHistoryBeforeJanitor.size(), 3);


// Nothing new happened // Nothing new happened
final List<PaymentTransactionModelDao> paymentTransactionHistoryAfterJanitor = getPaymentTransactionHistory(transactionExternalKey); final List<AuditLogWithHistory> paymentTransactionHistoryAfterJanitor = paymentDao.getPaymentTransactionAuditLogsWithHistoryForId(payment.getTransactions().get(0).getId(), AuditLevel.FULL, internalCallContext);
Assert.assertEquals(paymentTransactionHistoryAfterJanitor.size(), 3); Assert.assertEquals(paymentTransactionHistoryAfterJanitor.size(), 3);
} }


Expand Down Expand Up @@ -521,35 +526,6 @@ private List<PluginProperty> createPropertiesForInvoice(final Invoice invoice) {
return result; return result;
} }


// I wish we had a simplest way to query our history rows..
private List<PaymentTransactionModelDao> getPaymentTransactionHistory(final String transactionExternalKey) {
return dbi.withHandle(new HandleCallback<List<PaymentTransactionModelDao>>() {
@Override
public List<PaymentTransactionModelDao> withHandle(final Handle handle) throws Exception {
final List<Map<String, Object>> queryResult = handle.select("select * from payment_transaction_history where transaction_external_key = ? order by record_id asc",
transactionExternalKey);
final List<PaymentTransactionModelDao> result = new ArrayList<PaymentTransactionModelDao>(queryResult.size());
for (final Map<String, Object> row : queryResult) {
final PaymentTransactionModelDao transactionModelDao = new PaymentTransactionModelDao(UUID.fromString((String) row.get("id")),
null,
(String) row.get("transaction_external_key"),
null,
null,
UUID.fromString((String) row.get("payment_id")),
TransactionType.valueOf((String) row.get("transaction_type")),
null,
TransactionStatus.valueOf((String) row.get("transaction_status")),
(BigDecimal) row.get("amount"),
Currency.valueOf((String) row.get("currency")),
(String) row.get("gateway_error_code"),
String.valueOf(row.get("gateway_error_msg")));
result.add(transactionModelDao);
}
return result;
}
});
}

private void assertNotificationsCompleted(final InternalCallContext internalCallContext, final long timeoutSec) { private void assertNotificationsCompleted(final InternalCallContext internalCallContext, final long timeoutSec) {
try { try {
await().atMost(timeoutSec, SECONDS).until(new Callable<Boolean>() { await().atMost(timeoutSec, SECONDS).until(new Callable<Boolean>() {
Expand Down
Expand Up @@ -27,8 +27,12 @@
import org.killbill.billing.payment.api.PluginProperty; import org.killbill.billing.payment.api.PluginProperty;
import org.killbill.billing.payment.dao.PaymentMethodModelDao; import org.killbill.billing.payment.dao.PaymentMethodModelDao;
import org.killbill.billing.payment.dao.PaymentMethodSqlDao; import org.killbill.billing.payment.dao.PaymentMethodSqlDao;
import org.killbill.billing.payment.dao.PaymentModelDao;
import org.killbill.billing.util.api.AuditLevel;
import org.killbill.billing.util.audit.AuditLogWithHistory;
import org.killbill.billing.util.audit.ChangeType; import org.killbill.billing.util.audit.ChangeType;
import org.killbill.billing.util.dao.EntityHistoryModelDao; import org.killbill.billing.util.dao.EntityHistoryModelDao;
import org.killbill.billing.util.dao.TableName;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.Test; import org.testng.annotations.Test;


Expand Down Expand Up @@ -69,35 +73,34 @@ public void testDeletePaymentMethod() throws Exception {
final UUID paymentMethodId = paymentMethodProcessor.createOrGetExternalPaymentMethod("pmExternalKey", account, PLUGIN_PROPERTIES, callContext, internalCallContext); final UUID paymentMethodId = paymentMethodProcessor.createOrGetExternalPaymentMethod("pmExternalKey", account, PLUGIN_PROPERTIES, callContext, internalCallContext);
final PaymentMethodModelDao paymentMethodModelDao = paymentDao.getPaymentMethod(paymentMethodId, internalCallContext); final PaymentMethodModelDao paymentMethodModelDao = paymentDao.getPaymentMethod(paymentMethodId, internalCallContext);


final List<EntityHistoryModelDao<PaymentMethodModelDao, PaymentMethod>> history1 = getPaymentMethodHistory(paymentMethodModelDao.getRecordId()); List<AuditLogWithHistory> auditLogsWithHistory = paymentDao.getPaymentMethodAuditLogsWithHistoryForId(paymentMethodModelDao.getId(), AuditLevel.FULL, internalCallContext);
Assert.assertEquals(history1.size(), 1); Assert.assertEquals(auditLogsWithHistory.size(), 1);
Assert.assertEquals(history1.get(0).getChangeType(), ChangeType.INSERT);
Assert.assertEquals(history1.get(0).getEntity().getAccountRecordId(), paymentMethodModelDao.getAccountRecordId()); PaymentMethodModelDao history1 = (PaymentMethodModelDao) auditLogsWithHistory.get(0).getEntity();
Assert.assertEquals(history1.get(0).getEntity().getTenantRecordId(), paymentMethodModelDao.getTenantRecordId()); Assert.assertEquals(auditLogsWithHistory.get(0).getChangeType(), ChangeType.INSERT);
Assert.assertEquals(history1.get(0).getEntity().getExternalKey(), paymentMethodModelDao.getExternalKey()); Assert.assertEquals(history1.getAccountRecordId(), paymentMethodModelDao.getAccountRecordId());
Assert.assertTrue(history1.get(0).getEntity().isActive()); Assert.assertEquals(history1.getTenantRecordId(), paymentMethodModelDao.getTenantRecordId());
Assert.assertEquals(history1.getExternalKey(), paymentMethodModelDao.getExternalKey());
Assert.assertTrue(history1.isActive());


paymentMethodProcessor.deletedPaymentMethod(account, paymentMethodId, true, true, ImmutableList.<PluginProperty>of(), callContext, internalCallContext); paymentMethodProcessor.deletedPaymentMethod(account, paymentMethodId, true, true, ImmutableList.<PluginProperty>of(), callContext, internalCallContext);


final List<EntityHistoryModelDao<PaymentMethodModelDao, PaymentMethod>> history2 = getPaymentMethodHistory(paymentMethodModelDao.getRecordId()); auditLogsWithHistory = paymentDao.getPaymentMethodAuditLogsWithHistoryForId(paymentMethodModelDao.getId(), AuditLevel.FULL, internalCallContext);
Assert.assertEquals(history2.size(), 2); Assert.assertEquals(auditLogsWithHistory.size(), 2);
Assert.assertEquals(history2.get(0).getChangeType(), ChangeType.INSERT);
Assert.assertEquals(history2.get(0).getEntity().getAccountRecordId(), paymentMethodModelDao.getAccountRecordId()); history1 = (PaymentMethodModelDao) auditLogsWithHistory.get(0).getEntity();
Assert.assertEquals(history2.get(0).getEntity().getTenantRecordId(), paymentMethodModelDao.getTenantRecordId()); PaymentMethodModelDao history2 = (PaymentMethodModelDao) auditLogsWithHistory.get(1).getEntity();
Assert.assertEquals(history2.get(0).getEntity().getExternalKey(), paymentMethodModelDao.getExternalKey()); Assert.assertEquals(auditLogsWithHistory.get(0).getChangeType(), ChangeType.INSERT);
Assert.assertTrue(history2.get(0).getEntity().isActive()); Assert.assertEquals(history1.getAccountRecordId(), paymentMethodModelDao.getAccountRecordId());
Assert.assertEquals(history1.getTenantRecordId(), paymentMethodModelDao.getTenantRecordId());
Assert.assertEquals(history1.getExternalKey(), paymentMethodModelDao.getExternalKey());
Assert.assertTrue(history1.isActive());
// Note: it looks like we don't consider this as a DELETE, probably because we can un-delete such payment methods? // Note: it looks like we don't consider this as a DELETE, probably because we can un-delete such payment methods?
Assert.assertEquals(history2.get(1).getChangeType(), ChangeType.UPDATE); Assert.assertEquals(auditLogsWithHistory.get(1).getChangeType(), ChangeType.UPDATE);
Assert.assertEquals(history2.get(1).getEntity().getAccountRecordId(), paymentMethodModelDao.getAccountRecordId()); Assert.assertEquals(history2.getAccountRecordId(), paymentMethodModelDao.getAccountRecordId());
Assert.assertEquals(history2.get(1).getEntity().getTenantRecordId(), paymentMethodModelDao.getTenantRecordId()); Assert.assertEquals(history2.getTenantRecordId(), paymentMethodModelDao.getTenantRecordId());
Assert.assertEquals(history2.get(1).getEntity().getExternalKey(), paymentMethodModelDao.getExternalKey()); Assert.assertEquals(history2.getExternalKey(), paymentMethodModelDao.getExternalKey());
// Note: upon deletion, the recorded state is the same as before the delete // Note: upon deletion, the recorded state is the same as before the delete
Assert.assertTrue(history2.get(1).getEntity().isActive()); Assert.assertTrue(history2.isActive());
}

private List<EntityHistoryModelDao<PaymentMethodModelDao, PaymentMethod>> getPaymentMethodHistory(final Long paymentMethodRecordId) {
// See https://github.com/killbill/killbill/issues/335
final PaymentMethodSqlDao paymentMethodSqlDao = dbi.onDemand(PaymentMethodSqlDao.class);
return paymentMethodSqlDao.getHistoryForTargetRecordId(paymentMethodRecordId, internalCallContext);
} }
} }
Expand Up @@ -39,6 +39,8 @@
import org.killbill.billing.payment.api.PaymentApiException; import org.killbill.billing.payment.api.PaymentApiException;
import org.killbill.billing.payment.api.TransactionStatus; import org.killbill.billing.payment.api.TransactionStatus;
import org.killbill.billing.payment.api.TransactionType; import org.killbill.billing.payment.api.TransactionType;
import org.killbill.billing.util.api.AuditLevel;
import org.killbill.billing.util.audit.AuditLogWithHistory;
import org.killbill.billing.util.entity.DefaultPagination; import org.killbill.billing.util.entity.DefaultPagination;
import org.killbill.billing.util.entity.Pagination; import org.killbill.billing.util.entity.Pagination;
import org.killbill.billing.util.entity.dao.MockEntityDaoBase; import org.killbill.billing.util.entity.dao.MockEntityDaoBase;
Expand Down Expand Up @@ -425,6 +427,26 @@ public List<PaymentMethodModelDao> refreshPaymentMethods(final String pluginName
return ImmutableList.<PaymentMethodModelDao>of(); return ImmutableList.<PaymentMethodModelDao>of();
} }


@Override
public List<AuditLogWithHistory> getPaymentAuditLogsWithHistoryForId(final UUID paymentId, final AuditLevel auditLevel, final InternalTenantContext context) {
throw new UnsupportedOperationException();
}

@Override
public List<AuditLogWithHistory> getPaymentMethodAuditLogsWithHistoryForId(final UUID paymentMethodId, final AuditLevel auditLevel, final InternalTenantContext context) {
throw new UnsupportedOperationException();
}

@Override
public List<AuditLogWithHistory> getPaymentAttemptAuditLogsWithHistoryForId(final UUID paymentAttemptId, final AuditLevel auditLevel, final InternalTenantContext context) {
throw new UnsupportedOperationException();
}

@Override
public List<AuditLogWithHistory> getPaymentTransactionAuditLogsWithHistoryForId(final UUID paymentTransactionId, final AuditLevel auditLevel, final InternalTenantContext context) {
throw new UnsupportedOperationException();
}

@Override @Override
public PaymentMethodModelDao getPaymentMethodIncludedDeleted(final UUID paymentMethodId, final InternalTenantContext context) { public PaymentMethodModelDao getPaymentMethodIncludedDeleted(final UUID paymentMethodId, final InternalTenantContext context) {
return getPaymentMethod(paymentMethodId, context); return getPaymentMethod(paymentMethodId, context);
Expand Down

0 comments on commit 0b3b0fa

Please sign in to comment.