Search in sources :

Example 86 with Handle

use of jnc.platform.win32.Handle in project killbill by killbill.

the class TestIntegration method testWithDayLightSaving.

@Test(groups = "slow")
public void testWithDayLightSaving() throws Exception {
    clock.setTime(new DateTime("2015-09-01T08:01:01.000Z"));
    final DateTimeZone tz = DateTimeZone.forID("America/Juneau");
    final AccountData accountData = new MockAccountBuilder().name(UUID.randomUUID().toString().substring(1, 8)).firstNameLength(6).email(UUID.randomUUID().toString().substring(1, 8)).phone(UUID.randomUUID().toString().substring(1, 8)).migrated(false).externalKey(UUID.randomUUID().toString().substring(1, 8)).billingCycleDayLocal(1).currency(Currency.USD).paymentMethodId(UUID.randomUUID()).referenceTime(clock.getUTCNow()).timeZone(tz).build();
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    // 
    // CREATE SUBSCRIPTION AND EXPECT BOTH EVENTS: NextEvent.CREATE, NextEvent.BLOCK NextEvent.INVOICE
    // 
    final DefaultEntitlement bpSubscription = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", "Shotgun", ProductCategory.BASE, BillingPeriod.ANNUAL, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    // Check bundle after BP got created otherwise we get an error from auditApi.
    subscriptionChecker.checkSubscriptionCreated(bpSubscription.getId(), internalCallContext);
    invoiceChecker.checkInvoice(account.getId(), 1, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2015, 9, 1), null, InvoiceItemType.FIXED, new BigDecimal("0")));
    assertListenerStatus();
    // 
    // ADD ADD_ON ON THE SAME DAY
    // 
    final DefaultEntitlement aoSubscription = addAOEntitlementAndCheckForCompletion(bpSubscription.getBundleId(), "Bullets", ProductCategory.ADD_ON, BillingPeriod.NO_BILLING_PERIOD, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.NULL_INVOICE);
    assertListenerStatus();
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.NULL_INVOICE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addDays(30);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 2, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2015, 10, 1), new LocalDate(2016, 10, 1), InvoiceItemType.RECURRING, new BigDecimal("2399.95")), new ExpectedInvoiceItemCheck(new LocalDate(2015, 9, 1), new LocalDate(2015, 10, 1), InvoiceItemType.USAGE, BigDecimal.ZERO));
    // 2015-11-1
    busHandler.pushExpectedEvent(NextEvent.INVOICE);
    clock.addMonths(1);
    assertListenerStatus();
    // 2015-12-1
    busHandler.pushExpectedEvent(NextEvent.INVOICE);
    clock.addMonths(1);
    assertListenerStatus();
    // We sleep to let system creates lots of notification if an infinite loop was indeed happening
    Thread.sleep(3000);
    // And then we check that we only have the expected number of notifications in the history table.
    final Integer countNotifications = dbi.withHandle(new HandleCallback<Integer>() {

        @Override
        public Integer withHandle(final Handle handle) throws Exception {
            List<Map<String, Object>> res = handle.select("select count(*) as count from notifications_history;");
            final Integer count = Integer.valueOf(res.get(0).get("count").toString());
            return count;
        }
    });
    Assert.assertEquals(countNotifications.intValue(), 4);
}
Also used : Account(org.killbill.billing.account.api.Account) MockAccountBuilder(org.killbill.billing.mock.MockAccountBuilder) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) DateTimeZone(org.joda.time.DateTimeZone) BigDecimal(java.math.BigDecimal) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) Handle(org.skife.jdbi.v2.Handle) AccountData(org.killbill.billing.account.api.AccountData) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Test(org.testng.annotations.Test)

Example 87 with Handle

use of jnc.platform.win32.Handle in project killbill by killbill.

the class TestDefaultCustomFieldUserApi method testSaveCustomFieldWithAccountRecordId.

@Test(groups = "slow")
public void testSaveCustomFieldWithAccountRecordId() throws Exception {
    checkPagination(0);
    final String cfName = UUID.randomUUID().toString().substring(1, 4);
    final String cfValue = UUID.randomUUID().toString().substring(1, 4);
    final CustomField customField = new StringCustomField(cfName, cfValue, ObjectType.ACCOUNT, accountId, callContext.getCreatedDate());
    eventsListener.pushExpectedEvent(NextEvent.CUSTOM_FIELD);
    customFieldUserApi.addCustomFields(ImmutableList.<CustomField>of(customField), callContext);
    assertListenerStatus();
    checkPagination(1);
    // Verify the field was saved
    final List<CustomField> customFields = customFieldUserApi.getCustomFieldsForObject(accountId, ObjectType.ACCOUNT, callContext);
    Assert.assertEquals(customFields.size(), 1);
    Assert.assertEquals(customFields.get(0).getFieldName(), customField.getFieldName());
    Assert.assertEquals(customFields.get(0).getFieldValue(), customField.getFieldValue());
    Assert.assertEquals(customFields.get(0).getObjectId(), customField.getObjectId());
    Assert.assertEquals(customFields.get(0).getObjectType(), customField.getObjectType());
    // Verify the account_record_id was populated
    dbi.withHandle(new HandleCallback<Void>() {

        @Override
        public Void withHandle(final Handle handle) throws Exception {
            final List<Map<String, Object>> values = handle.select("select account_record_id from custom_fields where object_id = ?", accountId.toString());
            Assert.assertEquals(values.size(), 1);
            Assert.assertEquals(values.get(0).keySet().size(), 1);
            Assert.assertEquals(Long.valueOf(values.get(0).get("account_record_id").toString()), accountRecordId);
            return null;
        }
    });
    eventsListener.pushExpectedEvent(NextEvent.CUSTOM_FIELD);
    customFieldUserApi.removeCustomFields(customFields, callContext);
    assertListenerStatus();
    List<CustomField> remainingCustomFields = customFieldUserApi.getCustomFieldsForObject(accountId, ObjectType.ACCOUNT, callContext);
    Assert.assertEquals(remainingCustomFields.size(), 0);
    checkPagination(0);
    // Add again the custom field
    final CustomField newCustomField = new StringCustomField(cfName, cfValue, ObjectType.ACCOUNT, accountId, callContext.getCreatedDate());
    eventsListener.pushExpectedEvent(NextEvent.CUSTOM_FIELD);
    customFieldUserApi.addCustomFields(ImmutableList.<CustomField>of(newCustomField), callContext);
    assertListenerStatus();
    remainingCustomFields = customFieldUserApi.getCustomFieldsForObject(accountId, ObjectType.ACCOUNT, callContext);
    Assert.assertEquals(remainingCustomFields.size(), 1);
    checkPagination(1);
    // Delete again
    eventsListener.pushExpectedEvent(NextEvent.CUSTOM_FIELD);
    customFieldUserApi.removeCustomFields(remainingCustomFields, callContext);
    assertListenerStatus();
    remainingCustomFields = customFieldUserApi.getCustomFieldsForObject(accountId, ObjectType.ACCOUNT, callContext);
    Assert.assertEquals(remainingCustomFields.size(), 0);
    checkPagination(0);
}
Also used : StringCustomField(org.killbill.billing.util.customfield.StringCustomField) CustomField(org.killbill.billing.util.customfield.CustomField) StringCustomField(org.killbill.billing.util.customfield.StringCustomField) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) CustomFieldApiException(org.killbill.billing.util.api.CustomFieldApiException) Handle(org.skife.jdbi.v2.Handle) Test(org.testng.annotations.Test)

Example 88 with Handle

use of jnc.platform.win32.Handle in project killbill by killbill.

the class TestInternalCallContextFactory method testCreateInternalCallContextWithAccountRecordIdFromAccountObjectType.

@Test(groups = "slow")
public void testCreateInternalCallContextWithAccountRecordIdFromAccountObjectType() throws Exception {
    final UUID accountId = UUID.randomUUID();
    final Long accountRecordId = dbi.withHandle(new HandleCallback<Long>() {

        @Override
        public Long withHandle(final Handle handle) throws Exception {
            // Note: we always create an accounts table, see MysqlTestingHelper
            return update(handle, "insert into accounts (id, external_key, email, name, first_name_length, reference_time, time_zone, created_date, created_by, updated_date, updated_by, tenant_record_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", accountId.toString(), accountId.toString(), "yo@t.com", "toto", 4, new Date(), "UTC", new Date(), "i", new Date(), "j", internalCallContext.getTenantRecordId());
        }

        Long update(final Handle handle, final String sql, final Object... args) {
            final Update stmt = handle.createStatement(sql);
            int position = 0;
            for (final Object arg : args) {
                stmt.bind(position++, arg);
            }
            return stmt.executeAndReturnGeneratedKeys(new LongMapper(), "record_id").first();
        }
    });
    final ImmutableAccountData immutableAccountData = Mockito.mock(ImmutableAccountData.class);
    Mockito.when(immutableAccountInternalApi.getImmutableAccountDataByRecordId(Mockito.<Long>eq(accountRecordId), Mockito.<InternalTenantContext>any())).thenReturn(immutableAccountData);
    final InternalCallContext context = internalCallContextFactory.createInternalCallContext(accountId, ObjectType.ACCOUNT, callContext);
    // The account record id should have been looked up in the accounts table
    Assert.assertEquals(context.getAccountRecordId(), accountRecordId);
    verifyInternalCallContext(context);
}
Also used : ImmutableAccountData(org.killbill.billing.account.api.ImmutableAccountData) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) Update(org.skife.jdbi.v2.Update) Date(java.util.Date) Handle(org.skife.jdbi.v2.Handle) LongMapper(org.skife.jdbi.v2.util.LongMapper) UUID(java.util.UUID) Test(org.testng.annotations.Test)

Example 89 with Handle

use of jnc.platform.win32.Handle in project providence by morimekta.

the class MessageInserterTest method testDefaultMapping.

@Test
public void testDefaultMapping() {
    generator.setFillRate(1.0).setMaxCollectionItems(16);
    OptionalFields expected = generator.generate(OptionalFields.kDescriptor).mutate().setId(1234).setTimestampS((int) clock.instant().getEpochSecond()).setTimestampMs(clock.instant().getEpochSecond() * 1000).build();
    OptionalFields empty = OptionalFields.builder().setId(2345).build();
    try (Handle handle = db.getDBI().open()) {
        INSERTER.execute(handle, expected, empty);
        OptionalFields val = handle.createQuery("SELECT * FROM mappings.default_mappings WHERE id = :id").bind("id", expected.getId()).map(ProvidenceJdbi.toMessage(OptionalFields.kDescriptor, ProvidenceJdbi.columnsFromAllFields(), ProvidenceJdbi.withColumn("compact", MESSAGE), ProvidenceJdbi.withColumn("other_message", CLOB_MESSAGE))).first();
        OptionalFields val2 = handle.createQuery("SELECT * FROM mappings.default_mappings WHERE id = :id").bind("id", empty.getId()).map(ProvidenceJdbi.toMessage(OptionalFields.kDescriptor, ProvidenceJdbi.columnsFromAllFields(), ProvidenceJdbi.withColumn("compact", MESSAGE), ProvidenceJdbi.withColumn("other_message", CLOB_MESSAGE))).first();
        assertThat(val, is(equalToMessage(expected)));
        assertThat(val2, is(equalToMessage(empty)));
    }
}
Also used : OptionalFields(net.morimekta.test.providence.storage.jdbc.OptionalFields) Handle(org.skife.jdbi.v2.Handle) Test(org.junit.Test)

Example 90 with Handle

use of jnc.platform.win32.Handle in project providence by morimekta.

the class MessageRowMapperTest method testDefaultMapping.

@Test
public void testDefaultMapping() {
    generator.setFillRate(1.0).setMaxCollectionItems(16).withGenerator(CompactFields.kDescriptor, g -> {
        g.setValueGenerator(CompactFields._Field.NAME, ctx -> ctx.getFairy().textProducer().latinWord());
        g.setValueGenerator(CompactFields._Field.LABEL, ctx -> ctx.getFairy().textProducer().word());
    }).withGenerator(NormalFields.kDescriptor, g -> {
        g.setValueGenerator(NormalFields._Field.NAME, ctx -> ctx.getFairy().textProducer().latinWord());
        g.setValueGenerator(NormalFields._Field.LABEL, ctx -> ctx.getFairy().textProducer().word());
    });
    OptionalFields expected = generator.generate(OptionalFields.kDescriptor).mutate().setId(1234).setTimestampS((int) clock.instant().getEpochSecond()).setTimestampMs(clock.instant().getEpochSecond() * 1000).build();
    OptionalFields empty = OptionalFields.builder().setId(2345).build();
    try (Handle handle = db.getDBI().open()) {
        handle.createStatement("INSERT INTO mappings.default_mappings (" + "  id, present, tiny, small, medium, large, real," + "  fib, name, data, compact," + "  timestamp_s, timestamp_ms," + "  binary_message, blob_message, other_message," + "  blob_data, base64_data, int_bool" + ") VALUES (" + "  :e.id," + "  :e.present," + "  :e.tiny," + "  :e.small," + "  :e.medium," + "  :e.large," + "  :e.real," + "  :e.fib," + "  :e.name," + "  :e.data," + "  :e.message," + "  :timestamp_s," + "  :e.timestamp_ms," + "  :e.binary_message," + "  :e.blob_message," + "  :e.clob_message," + "  :e.blob_data," + "  :e.base64_data," + "  :e.int_bool" + ")").bind("timestamp_s", ProvidenceJdbi.toField(expected, TIMESTAMP_S, Types.TIMESTAMP)).bindNamedArgumentFinder(ProvidenceJdbi.forMessage("e", expected, ProvidenceJdbi.withType(TIMESTAMP_MS, Types.TIMESTAMP), ProvidenceJdbi.withType(BINARY_MESSAGE, Types.BINARY), ProvidenceJdbi.withType(BLOB_MESSAGE, Types.BLOB), ProvidenceJdbi.withType(CLOB_MESSAGE, Types.CLOB), ProvidenceJdbi.withType(BLOB_DATA, Types.BLOB), ProvidenceJdbi.withType(BASE64_DATA, Types.VARCHAR))).execute();
        handle.createStatement("INSERT INTO mappings.default_mappings (" + "  id, present, tiny, small, medium, large, real," + "  fib, name, data, compact," + "  timestamp_s, timestamp_ms," + "  binary_message, blob_message, other_message," + "  blob_data, base64_data, int_bool" + ") VALUES (" + "  :id," + "  :present," + "  :tiny," + "  :small," + "  :medium," + "  :large," + "  :real," + "  :fib," + "  :name," + "  :data," + "  :message," + "  :timestamp_s," + "  :timestamp_ms," + "  :binary_message," + "  :blob_message," + "  :clob_message," + "  :blob_data," + "  :base64_data," + "  :int_bool" + ")").bind("timestamp_s", ProvidenceJdbi.toField(empty, TIMESTAMP_S, Types.TIMESTAMP)).bindNamedArgumentFinder(ProvidenceJdbi.forMessage(empty, ProvidenceJdbi.withType(TIMESTAMP_MS, Types.TIMESTAMP), ProvidenceJdbi.withType(BINARY_MESSAGE, Types.BINARY), ProvidenceJdbi.withType(BLOB_MESSAGE, Types.BLOB), ProvidenceJdbi.withType(CLOB_MESSAGE, Types.CLOB), ProvidenceJdbi.withType(BLOB_DATA, Types.BLOB), ProvidenceJdbi.withType(BASE64_DATA, Types.VARCHAR), ProvidenceJdbi.withType(INT_BOOL, Types.INTEGER))).execute();
        OptionalFields val = handle.createQuery("SELECT m.* FROM mappings.default_mappings m WHERE id = :id").bind("id", ProvidenceJdbi.toField(expected, ID)).map(ProvidenceJdbi.toMessage("default_mappings", OptionalFields.kDescriptor, ProvidenceJdbi.columnsFromAllFields(), ProvidenceJdbi.withColumn("compact", MESSAGE), ProvidenceJdbi.withColumn("other_message", CLOB_MESSAGE))).first();
        OptionalFields val2 = handle.createQuery("SELECT * FROM mappings.default_mappings WHERE id = :id").bind("id", ProvidenceJdbi.toField(empty, ID)).map(ProvidenceJdbi.toMessage(OptionalFields.kDescriptor, ProvidenceJdbi.columnsFromAllFields(), ProvidenceJdbi.withColumn("compact", MESSAGE), ProvidenceJdbi.withColumn("other_message", CLOB_MESSAGE))).first();
        assertThat(val, is(equalToMessage(expected)));
        assertThat(val2, is(equalToMessage(empty)));
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) INT_BOOL(net.morimekta.test.providence.storage.jdbc.OptionalFields._Field.INT_BOOL) ID(net.morimekta.test.providence.storage.jdbc.OptionalFields._Field.ID) TIMESTAMP_MS(net.morimekta.test.providence.storage.jdbc.OptionalFields._Field.TIMESTAMP_MS) OptionalFields(net.morimekta.test.providence.storage.jdbc.OptionalFields) Test(org.junit.Test) BASE64_DATA(net.morimekta.test.providence.storage.jdbc.OptionalFields._Field.BASE64_DATA) NormalFields(net.morimekta.test.providence.storage.jdbc.NormalFields) CLOB_MESSAGE(net.morimekta.test.providence.storage.jdbc.OptionalFields._Field.CLOB_MESSAGE) BINARY_MESSAGE(net.morimekta.test.providence.storage.jdbc.OptionalFields._Field.BINARY_MESSAGE) BLOB_DATA(net.morimekta.test.providence.storage.jdbc.OptionalFields._Field.BLOB_DATA) BLOB_MESSAGE(net.morimekta.test.providence.storage.jdbc.OptionalFields._Field.BLOB_MESSAGE) Assert.assertThat(org.junit.Assert.assertThat) MESSAGE(net.morimekta.test.providence.storage.jdbc.OptionalFields._Field.MESSAGE) Rule(org.junit.Rule) Handle(org.skife.jdbi.v2.Handle) SimpleGeneratorWatcher(net.morimekta.providence.testing.generator.SimpleGeneratorWatcher) CompactFields(net.morimekta.test.providence.storage.jdbc.CompactFields) TIMESTAMP_S(net.morimekta.test.providence.storage.jdbc.OptionalFields._Field.TIMESTAMP_S) Clock(java.time.Clock) ProvidenceMatchers.equalToMessage(net.morimekta.providence.testing.ProvidenceMatchers.equalToMessage) Types(java.sql.Types) OptionalFields(net.morimekta.test.providence.storage.jdbc.OptionalFields) Handle(org.skife.jdbi.v2.Handle) Test(org.junit.Test)

Aggregations

Handle (org.skife.jdbi.v2.Handle)103 DBI (org.skife.jdbi.v2.DBI)28 Before (org.junit.Before)21 IOException (java.io.IOException)18 List (java.util.List)17 DataSourceFactory (io.dropwizard.db.DataSourceFactory)15 DBIFactory (io.dropwizard.jdbi.DBIFactory)15 SQLException (java.sql.SQLException)15 Map (java.util.Map)14 Test (org.junit.Test)14 Test (org.testng.annotations.Test)14 DateTime (org.joda.time.DateTime)13 ArrayList (java.util.ArrayList)11 TransactionStatus (org.skife.jdbi.v2.TransactionStatus)11 ResultSet (java.sql.ResultSet)10 ImmutableList (com.google.common.collect.ImmutableList)8 UUID (java.util.UUID)8 CallbackFailedException (org.skife.jdbi.v2.exceptions.CallbackFailedException)7 ImmutableSet (com.google.common.collect.ImmutableSet)6 Set (java.util.Set)6