Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010-2013 Ning, Inc.
   * Copyright 2014 Groupon, Inc
   * Copyright 2014 The Billing Project, LLC
   *
   * The Billing Project licenses this file to you under the Apache License, version 2.0
   * (the "License"); you may not use this file except in compliance with the
   * License.  You may obtain a copy of the License at:
   *
  *    http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
  * License for the specific language governing permissions and limitations
  * under the License.
  */
 
 package org.killbill.billing.beatrix.integration.osgi;
 
 import java.util.Map;
 import java.util.UUID;
 
 
 
 import static com.jayway.awaitility.Awaitility.await;

Basic OSGI test that relies on the 'test' bundle (org.killbill.billing.osgi.bundles.test.TestActivator)

The test checks that the bundle: - gets started - can make API call - can listen to KB events - can register a service (PaymentPluginApi) that this test calls - can write in the DB using the DataSource (this is how the assertion work)

 
 public class TestBasicOSGIWithTestBundle extends TestOSGIBase {
 
     // Magic name, see org.killbill.billing.osgi.bundles.test.TestActivator
     private static final String TEST_PLUGIN_NAME = "test";
     private static final String BUNDLE_TEST_RESOURCE = "killbill-osgi-bundles-test-beatrix";
 
     @BeforeClass(groups = "slow")
     public void beforeClass() throws Exception {
         super.beforeClass();
 
         final String killbillVersion = System.getProperty("killbill.version");
         final SetupBundleWithAssertion setupTest = new SetupBundleWithAssertion(killbillVersion);
         setupTest.setupJavaBundle();
     }
 
     @Test(groups = "slow")
     public void testBundleTest() throws Exception {
         // At this point test bundle should have been started already
         final TestActivatorWithAssertion assertTor = new TestActivatorWithAssertion();
         assertTor.assertPluginInitialized();
 
         // Send an event and expect the test bundle to listen to KB events and write the account id in its table
         final ExternalBusTestEvent event = new ExternalBusTestEvent();
         .post(event);
         assertTor.assertPluginReceivedEvent(event.getAccountId().toString());
 
         // Retrieve the PaymentPluginApi that the test bundle registered
 
         // Make a payment and expect the test bundle to correctly write in its table the input values
         final UUID paymentId = UUID.randomUUID();
         final UUID transactionId = UUID.randomUUID();
         final UUID paymentMethodId = UUID.randomUUID();
         final BigDecimal paymentAmount = new BigDecimal("14.32");
         final Currency currency = .;
         final PaymentTransactionInfoPlugin PaymentTransactionInfoPlugin = paymentPluginApi.purchasePayment(event.getAccountId(), paymentIdtransactionIdpaymentMethodIdpaymentAmountcurrency, ImmutableList.<PluginProperty>of(), );
         Assert.assertEquals(PaymentTransactionInfoPlugin.getKbPaymentId(), paymentId);
         Assert.assertEquals(PaymentTransactionInfoPlugin.getKbTransactionPaymentId(), transactionId);
         Assert.assertEquals(PaymentTransactionInfoPlugin.getAmount().compareTo(paymentAmount), 0);
         Assert.assertEquals(PaymentTransactionInfoPlugin.getCurrency(), currency);
        assertTor.assertPluginCreatedPayment(paymentIdpaymentMethodIdpaymentAmount);
    }
    private static final class TestActivatorWithAssertion {
        private final IDBI dbi;
        public TestActivatorWithAssertion(final IDBI dbi) {
            this. = dbi;
        }
        public void assertPluginInitialized() {
            assertWithCallback(new AwaitCallback() {
                @Override
                public boolean isSuccess() {
                    return isPluginInitialized();
                }
            }, "Plugin did not complete initialization");
        }
        public void assertPluginReceivedEvent(final String expectedExternalKey) {
            assertWithCallback(new AwaitCallback() {
                @Override
                public boolean isSuccess() {
                    return isValidAccountExternalKey(expectedExternalKey);
                }
            }, "Plugin did not receive event");
        }
        public void assertPluginCreatedPayment(final UUID expectedPaymentIdfinal UUID expectedPaymentMethodIdfinal BigDecimal expectedAmount) {
            assertWithCallback(new AwaitCallback() {
                @Override
                public boolean isSuccess() {
                    return isValidPayment(expectedPaymentIdexpectedPaymentMethodIdexpectedAmount);
                }
            }, "Plugin did not create the payment");
        }
        private void assertWithCallback(final AwaitCallback callbackfinal String error) {
            try {
                await().atMost(15, .).until(new Callable<Boolean>() {
                    @Override
                    public Boolean call() throws Exception {
                        return callback.isSuccess();
                    }
                });
            } catch (final Exception e) {
                Assert.fail(errore);
            }
        }
        private boolean isValidPayment(final UUID expectedPaymentIdfinal UUID expectedPaymentMethodIdfinal BigDecimal expectedAmount) {
            final TestModel test = getTestModelFirstRecord();
            return expectedPaymentId.equals(test.getPaymentId()) &&
                   expectedPaymentMethodId.equals(test.getPaymentMethodId()) &&
                   expectedAmount.compareTo(test.getAmount()) == 0;
        }
        private boolean isPluginInitialized() {
            final TestModel test = getTestModelFirstRecord();
            return test.isStarted();
        }
        private boolean isValidAccountExternalKey(final String expectedExternalKey) {
            final TestModel test = getTestModelFirstRecord();
            return expectedExternalKey.equals(test.getAccountExternalKey());
        }
        private TestModel getTestModelFirstRecord() {
            return .inTransaction(new TransactionCallback<TestModel>() {
                @Override
                public TestModel inTransaction(final Handle connfinal TransactionStatus statusthrows Exception {
                    final Query<Map<StringObject>> q = conn.createQuery("SELECT is_started, external_key, payment_id, payment_method_id, payment_amount FROM test_bundle WHERE record_id = 1;");
                    return q.map(new TestMapper()).first();
                }
            });
        }
    }
    private static final class TestModel {
        private final Boolean isStarted;
        private final String accountExternalKey;
        private final UUID paymentId;
        private final UUID paymentMethodId;
        private final BigDecimal amount;
        private TestModel(final Boolean startedfinal String accountExternalKeyfinal UUID paymentIdfinal UUID paymentMethodIdfinal BigDecimal amount) {
             = started;
            this. = accountExternalKey;
            this. = paymentId;
            this. = paymentMethodId;
            this. = amount;
        }
        public Boolean isStarted() {
            return ;
        }
        public String getAccountExternalKey() {
            return ;
        }
        public UUID getPaymentId() {
            return ;
        }
        public UUID getPaymentMethodId() {
            return ;
        }
        public BigDecimal getAmount() {
            return ;
        }
    }
    private static final class TestMapper implements ResultSetMapper<TestModel> {
        @Override
        public TestModel map(final int indexfinal ResultSet rfinal StatementContext ctxthrows SQLException {
            final Boolean isStarted = r.getBoolean("is_started");
            final String externalKey = r.getString("external_key");
            final UUID paymentId = r.getString("payment_id") != null ? UUID.fromString(r.getString("payment_id")) : null;
            final UUID paymentMethodId = r.getString("payment_method_id") != null ? UUID.fromString(r.getString("payment_method_id")) : null;
            final BigDecimal amount = r.getBigDecimal("payment_amount");
            return new TestModel(isStartedexternalKeypaymentIdpaymentMethodIdamount);
        }
    }
    private interface AwaitCallback {
        boolean isSuccess();
    }
New to GrepCode? Check out our FAQ X