Search in sources :

Example 1 with TenantKeyValue

use of org.killbill.billing.client.model.gen.TenantKeyValue in project killbill by killbill.

the class TestTenantKV method testPerTenantPluginPaymentStateMachineConfig.

@Test(groups = "slow", description = "Upload and retrieve a per plugin payment state machine config")
public void testPerTenantPluginPaymentStateMachineConfig() throws Exception {
    final RequestOptions requestOptionsForOriginalTenant = requestOptions;
    // Create another tenant - it will have a different state machine
    createTenant(UUID.randomUUID().toString(), UUID.randomUUID().toString(), true);
    // Verify initial state
    final TenantKeyValue emptyTenantKey = tenantApi.getPluginPaymentStateMachineConfig(PLUGIN_NAME, requestOptionsForOriginalTenant);
    assertEquals(emptyTenantKey.getValues().size(), 0);
    final TenantKeyValue emptyTenantKeyOtherTenant = tenantApi.getPluginPaymentStateMachineConfig(PLUGIN_NAME, requestOptions);
    assertEquals(emptyTenantKeyOtherTenant.getValues().size(), 0);
    callbackServlet.pushExpectedEvent(ExtBusEventType.TENANT_CONFIG_CHANGE);
    final String stateMachineConfig = getResourceBodyString("org/killbill/billing/server/SimplePaymentStates.xml");
    final TenantKeyValue tenantKey0 = tenantApi.uploadPluginPaymentStateMachineConfig(PLUGIN_NAME, stateMachineConfig, requestOptions);
    callbackServlet.assertListenerStatus();
    assertEquals(tenantKey0.getKey(), TenantKV.TenantKey.PLUGIN_PAYMENT_STATE_MACHINE_ + PLUGIN_NAME);
    // Verify only the other tenant has the new state machine
    final TenantKeyValue emptyTenantKey1 = tenantApi.getPluginPaymentStateMachineConfig(PLUGIN_NAME, requestOptionsForOriginalTenant);
    assertEquals(emptyTenantKey1.getValues().size(), 0);
    final TenantKeyValue tenantKey1OtherTenant = tenantApi.getPluginPaymentStateMachineConfig(PLUGIN_NAME, requestOptions);
    assertEquals(tenantKey1OtherTenant.getKey(), TenantKV.TenantKey.PLUGIN_PAYMENT_STATE_MACHINE_ + PLUGIN_NAME);
    assertEquals(tenantKey1OtherTenant.getValues().size(), 1);
    // Create an auth in both tenant
    final Payment payment = createComboPaymentTransaction(requestOptionsForOriginalTenant);
    final Payment paymentOtherTenant = createComboPaymentTransaction(requestOptions);
    // Void in the first tenant (allowed by the default state machine)
    callbackServlet.pushExpectedEvent(ExtBusEventType.PAYMENT_SUCCESS);
    paymentApi.voidPayment(payment.getPaymentId(), new PaymentTransaction(), NULL_PLUGIN_NAMES, NULL_PLUGIN_PROPERTIES, requestOptionsForOriginalTenant);
    callbackServlet.assertListenerStatus();
    final Payment voidPayment = paymentApi.getPayment(payment.getPaymentId(), NULL_PLUGIN_PROPERTIES, requestOptionsForOriginalTenant);
    assertEquals(voidPayment.getTransactions().get(0).getStatus(), TransactionStatus.SUCCESS);
    assertEquals(voidPayment.getTransactions().get(1).getStatus(), TransactionStatus.SUCCESS);
    // Void in the other tenant (disallowed)
    try {
        paymentApi.voidPayment(paymentOtherTenant.getPaymentId(), new PaymentTransaction(), NULL_PLUGIN_NAMES, NULL_PLUGIN_PROPERTIES, requestOptions);
        Assert.fail();
    } catch (final KillBillClientException e) {
        assertEquals((int) e.getBillingException().getCode(), ErrorCode.PAYMENT_INVALID_OPERATION.getCode());
    }
    callbackServlet.assertListenerStatus();
    // Remove the custom state machine
    callbackServlet.pushExpectedEvent(ExtBusEventType.TENANT_CONFIG_DELETION);
    tenantApi.deletePluginPaymentStateMachineConfig(PLUGIN_NAME, requestOptions);
    final TenantKeyValue tenantKey2 = tenantApi.getPluginPaymentStateMachineConfig(PLUGIN_NAME, requestOptions);
    callbackServlet.assertListenerStatus();
    assertEquals(tenantKey2.getKey(), TenantKV.TenantKey.PLUGIN_PAYMENT_STATE_MACHINE_ + PLUGIN_NAME);
    assertEquals(tenantKey2.getValues().size(), 0);
    final AtomicReference<Payment> voidPaymentOtherTenant2Ref = new AtomicReference<Payment>();
    Awaitility.await().atMost(8, TimeUnit.SECONDS).pollInterval(Durations.TWO_SECONDS).until(new Callable<Boolean>() {

        @Override
        public Boolean call() {
            // The void should now go through
            try {
                callbackServlet.pushExpectedEvent(ExtBusEventType.PAYMENT_SUCCESS);
                paymentApi.voidPayment(paymentOtherTenant.getPaymentId(), new PaymentTransaction(), NULL_PLUGIN_NAMES, NULL_PLUGIN_PROPERTIES, requestOptions);
                final Payment voidPaymentOtherTenant2 = paymentApi.getPayment(paymentOtherTenant.getPaymentId(), NULL_PLUGIN_PROPERTIES, requestOptions);
                callbackServlet.assertListenerStatus();
                voidPaymentOtherTenant2Ref.set(voidPaymentOtherTenant2);
                return voidPaymentOtherTenant2 != null;
            } catch (final KillBillClientException e) {
                // Invalidation hasn't happened yet
                return false;
            }
        }
    });
    assertEquals(voidPaymentOtherTenant2Ref.get().getTransactions().get(0).getStatus(), TransactionStatus.SUCCESS);
    assertEquals(voidPaymentOtherTenant2Ref.get().getTransactions().get(1).getStatus(), TransactionStatus.SUCCESS);
}
Also used : PaymentTransaction(org.killbill.billing.client.model.gen.PaymentTransaction) ComboPaymentTransaction(org.killbill.billing.client.model.gen.ComboPaymentTransaction) Payment(org.killbill.billing.client.model.gen.Payment) RequestOptions(org.killbill.billing.client.RequestOptions) TenantKeyValue(org.killbill.billing.client.model.gen.TenantKeyValue) KillBillClientException(org.killbill.billing.client.KillBillClientException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.testng.annotations.Test)

Example 2 with TenantKeyValue

use of org.killbill.billing.client.model.gen.TenantKeyValue in project killbill by killbill.

the class TestPerTenantConfig method testFailedPaymentWithPerTenantRetryConfig.

@Test(groups = "slow")
public void testFailedPaymentWithPerTenantRetryConfig() throws Exception {
    // Create the tenant
    createTenant("tenantSuperTuned", "2367$$ffr79", true);
    // Configure our plugin to fail
    mockPaymentProviderPlugin.makeAllInvoicesFailWithError(true);
    // Upload the config
    final ObjectMapper mapper = new ObjectMapper();
    final HashMap<String, String> perTenantProperties = new HashMap<String, String>();
    perTenantProperties.put("org.killbill.payment.retry.days", "1,1,1");
    final String perTenantConfig = mapper.writeValueAsString(perTenantProperties);
    callbackServlet.pushExpectedEvent(ExtBusEventType.TENANT_CONFIG_CHANGE);
    final TenantKeyValue tenantKey = tenantApi.uploadPerTenantConfiguration(perTenantConfig, requestOptions);
    callbackServlet.assertListenerStatus();
    final Account accountJson = createAccountWithPMBundleAndSubscriptionAndWaitForFirstInvoice(false);
    final Payments payments = accountApi.getPaymentsForAccount(accountJson.getAccountId(), NULL_PLUGIN_PROPERTIES, requestOptions);
    Assert.assertEquals(payments.size(), 1);
    Assert.assertEquals(payments.get(0).getTransactions().size(), 1);
    // 
    // Because we have specified a retry interval of one day we should see the new attempt after moving clock 1 day (and not 8 days which is default)
    // 
    // 
    // Now unregister special per tenant config and when the first retry occurs one day after (and still fails), it now sets a retry date of 8 days
    // 
    callbackServlet.pushExpectedEvents(ExtBusEventType.TENANT_CONFIG_DELETION);
    tenantApi.deletePerTenantConfiguration(requestOptions);
    callbackServlet.assertListenerStatus();
    callbackServlet.pushExpectedEvents(ExtBusEventType.INVOICE_PAYMENT_FAILED, ExtBusEventType.PAYMENT_FAILED);
    clock.addDays(1);
    callbackServlet.assertListenerStatus();
    final Payments payments2 = accountApi.getPaymentsForAccount(accountJson.getAccountId(), NULL_PLUGIN_PROPERTIES, requestOptions);
    Assert.assertEquals(payments2.size(), 1);
    Assert.assertEquals(payments2.get(0).getTransactions().size(), 2);
    Assert.assertEquals(payments2.get(0).getTransactions().get(0).getStatus(), TransactionStatus.PAYMENT_FAILURE);
    Assert.assertEquals(payments2.get(0).getTransactions().get(1).getStatus(), TransactionStatus.PAYMENT_FAILURE);
    clock.addDays(1);
    callbackServlet.assertListenerStatus();
    // No retry with default config
    final Payments payments3 = accountApi.getPaymentsForAccount(accountJson.getAccountId(), NULL_PLUGIN_PROPERTIES, requestOptions);
    Assert.assertEquals(payments3.size(), 1);
    Assert.assertEquals(payments3.get(0).getTransactions().size(), 2);
    mockPaymentProviderPlugin.makeAllInvoicesFailWithError(false);
    callbackServlet.pushExpectedEvents(ExtBusEventType.INVOICE_PAYMENT_SUCCESS, ExtBusEventType.PAYMENT_SUCCESS);
    clock.addDays(7);
    callbackServlet.assertListenerStatus();
    final Payments payments4 = accountApi.getPaymentsForAccount(accountJson.getAccountId(), NULL_PLUGIN_PROPERTIES, requestOptions);
    Assert.assertEquals(payments4.size(), 1);
    Assert.assertEquals(payments4.get(0).getTransactions().size(), 3);
    Assert.assertEquals(payments4.get(0).getTransactions().get(0).getStatus(), TransactionStatus.PAYMENT_FAILURE);
    Assert.assertEquals(payments4.get(0).getTransactions().get(1).getStatus(), TransactionStatus.PAYMENT_FAILURE);
    Assert.assertEquals(payments4.get(0).getTransactions().get(2).getStatus(), TransactionStatus.SUCCESS);
}
Also used : Account(org.killbill.billing.client.model.gen.Account) HashMap(java.util.HashMap) Payments(org.killbill.billing.client.model.Payments) TenantKeyValue(org.killbill.billing.client.model.gen.TenantKeyValue) ObjectMapper(org.killbill.billing.util.jackson.ObjectMapper) Test(org.testng.annotations.Test)

Example 3 with TenantKeyValue

use of org.killbill.billing.client.model.gen.TenantKeyValue in project killbill by killbill.

the class TestTenantKV method testPerTenantPluginConfig.

@Test(groups = "slow", description = "Upload and retrieve a per plugin config")
public void testPerTenantPluginConfig() throws Exception {
    final String pluginName = "PLUGIN_FOO";
    callbackServlet.pushExpectedEvent(ExtBusEventType.TENANT_CONFIG_CHANGE);
    final String pluginConfig = getResourceBodyString("org/killbill/billing/server/plugin.yml");
    final TenantKeyValue tenantKey0 = tenantApi.uploadPluginConfiguration(pluginName, pluginConfig, requestOptions);
    callbackServlet.assertListenerStatus();
    assertEquals(tenantKey0.getKey(), TenantKV.TenantKey.PLUGIN_CONFIG_ + pluginName);
    final TenantKeyValue tenantKey1 = tenantApi.getPluginConfiguration(pluginName, requestOptions);
    assertEquals(tenantKey1.getKey(), TenantKV.TenantKey.PLUGIN_CONFIG_ + pluginName);
    assertEquals(tenantKey1.getValues().size(), 1);
    tenantApi.deletePluginConfiguration(pluginName, requestOptions);
    final TenantKeyValue tenantKey2 = tenantApi.getPluginConfiguration(pluginName, requestOptions);
    assertEquals(tenantKey2.getKey(), TenantKV.TenantKey.PLUGIN_CONFIG_ + pluginName);
    assertEquals(tenantKey2.getValues().size(), 0);
}
Also used : TenantKeyValue(org.killbill.billing.client.model.gen.TenantKeyValue) Test(org.testng.annotations.Test)

Example 4 with TenantKeyValue

use of org.killbill.billing.client.model.gen.TenantKeyValue in project killbill by killbill.

the class TestTenantKV method testAllPluginConfig.

@Test(groups = "slow", description = "Retrieve per tenant keys and values based on a key prefix")
public void testAllPluginConfig() throws Exception {
    final String keyPrefix = "dummy_tenant";
    final String tenant1KeyName = "dummy_tenant_1_key";
    final String tenant1Value = "dummy_tenant_1_value";
    final TenantKeyValue tenant1Config = tenantApi.insertUserKeyValue(tenant1KeyName, tenant1Value, requestOptions);
    final String tenant2KeyName = "dummy_tenant_2_key";
    final String tenant2Value = "dummy_tenant_2_value";
    final TenantKeyValue tenant2Config = tenantApi.insertUserKeyValue(tenant2KeyName, tenant2Value, requestOptions);
    final TenantKeyValues tenantKeyValues = tenantApi.getAllPluginConfiguration(keyPrefix, requestOptions);
    Assert.assertFalse(tenantKeyValues.isEmpty());
    Assert.assertSame(tenantKeyValues.size(), 2);
    assertEquals(tenantKeyValues.get(0).getKey(), tenant1Config.getKey());
    assertEquals(tenantKeyValues.get(0).getValues(), tenant1Config.getValues());
    assertEquals(tenantKeyValues.get(1).getKey(), tenant2Config.getKey());
    assertEquals(tenantKeyValues.get(1).getValues(), tenant2Config.getValues());
}
Also used : TenantKeyValues(org.killbill.billing.client.model.TenantKeyValues) TenantKeyValue(org.killbill.billing.client.model.gen.TenantKeyValue) Test(org.testng.annotations.Test)

Aggregations

TenantKeyValue (org.killbill.billing.client.model.gen.TenantKeyValue)4 Test (org.testng.annotations.Test)4 HashMap (java.util.HashMap)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 KillBillClientException (org.killbill.billing.client.KillBillClientException)1 RequestOptions (org.killbill.billing.client.RequestOptions)1 Payments (org.killbill.billing.client.model.Payments)1 TenantKeyValues (org.killbill.billing.client.model.TenantKeyValues)1 Account (org.killbill.billing.client.model.gen.Account)1 ComboPaymentTransaction (org.killbill.billing.client.model.gen.ComboPaymentTransaction)1 Payment (org.killbill.billing.client.model.gen.Payment)1 PaymentTransaction (org.killbill.billing.client.model.gen.PaymentTransaction)1 ObjectMapper (org.killbill.billing.util.jackson.ObjectMapper)1