Search in sources :

Example 1 with PaymentOperation

use of org.killbill.billing.payment.core.sm.payments.PaymentOperation in project killbill by killbill.

the class TestPluginOperation method testOperationThrowsRuntimeException.

@Test(groups = "fast")
public void testOperationThrowsRuntimeException() throws Exception {
    final CallbackTest callback = new CallbackTest(new NullPointerException("Expected for the test"));
    final PaymentOperation pluginOperation = getPluginOperation();
    try {
        pluginOperation.dispatchWithAccountLockAndTimeout(PLUGIN_NAME_PLACEHOLDER, callback);
        Assert.fail();
    } catch (final OperationException e) {
        Assert.assertEquals(e.getOperationResult(), OperationResult.EXCEPTION);
        Assert.assertTrue(e.getCause() instanceof PaymentApiException);
        Assert.assertTrue(e.getCause().getCause() instanceof NullPointerException);
    }
}
Also used : PaymentOperation(org.killbill.billing.payment.core.sm.payments.PaymentOperation) PaymentApiException(org.killbill.billing.payment.api.PaymentApiException) OperationException(org.killbill.automaton.OperationException) Test(org.testng.annotations.Test)

Example 2 with PaymentOperation

use of org.killbill.billing.payment.core.sm.payments.PaymentOperation in project killbill by killbill.

the class TestPluginOperation method testLocking.

private void testLocking() throws Exception {
    final Semaphore available = new Semaphore(1, true);
    final CallbackTest callback = new CallbackTest(available);
    final PaymentOperation pluginOperation = getPluginOperation(true);
    // Take the only permit
    available.acquire();
    // Start the plugin operation in the background (will block)
    runPluginOperationInBackground(pluginOperation, callback, false);
    Awaitility.await().until(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return callback.getStartCount() == 1;
        }
    });
    // The operation should be blocked here because we have the semaphore
    Assert.assertEquals(callback.getRunCount(), 0);
    // Trying to run the operation again will throw LockFailedException
    Awaitility.await().atMost(2 * TIMEOUT, TimeUnit.SECONDS).untilTrue(runPluginOperationInBackground(pluginOperation, callback, true));
    Assert.assertEquals(callback.getRunCount(), 0);
    // Release the semaphore
    available.release();
    // Give some time for the operation to run
    Awaitility.await().until(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return callback.getRunCount() == 1;
        }
    });
    // Verify the final state
    Assert.assertEquals(available.availablePermits(), 1);
}
Also used : PaymentOperation(org.killbill.billing.payment.core.sm.payments.PaymentOperation) Semaphore(java.util.concurrent.Semaphore) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PaymentPluginApiException(org.killbill.billing.payment.plugin.api.PaymentPluginApiException) OperationException(org.killbill.automaton.OperationException) PaymentApiException(org.killbill.billing.payment.api.PaymentApiException)

Example 3 with PaymentOperation

use of org.killbill.billing.payment.core.sm.payments.PaymentOperation in project killbill by killbill.

the class TestPluginOperation method testOperationThrowsPaymentApiException.

@Test(groups = "fast")
public void testOperationThrowsPaymentApiException() throws Exception {
    final CallbackTest callback = new CallbackTest(new PaymentApiException(ErrorCode.__UNKNOWN_ERROR_CODE));
    final PaymentOperation pluginOperation = getPluginOperation();
    try {
        pluginOperation.dispatchWithAccountLockAndTimeout(PLUGIN_NAME_PLACEHOLDER, callback);
        Assert.fail();
    } catch (final OperationException e) {
        Assert.assertEquals(e.getOperationResult(), OperationResult.EXCEPTION);
        Assert.assertTrue(e.getCause() instanceof PaymentApiException);
    }
}
Also used : PaymentOperation(org.killbill.billing.payment.core.sm.payments.PaymentOperation) PaymentApiException(org.killbill.billing.payment.api.PaymentApiException) OperationException(org.killbill.automaton.OperationException) Test(org.testng.annotations.Test)

Aggregations

OperationException (org.killbill.automaton.OperationException)3 PaymentApiException (org.killbill.billing.payment.api.PaymentApiException)3 PaymentOperation (org.killbill.billing.payment.core.sm.payments.PaymentOperation)3 Test (org.testng.annotations.Test)2 Semaphore (java.util.concurrent.Semaphore)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 PaymentPluginApiException (org.killbill.billing.payment.plugin.api.PaymentPluginApiException)1