use of org.skife.jdbi.v2.tweak.HandleCallback in project druid by druid-io.
the class HadoopConverterJobTest method setUp.
@Before
public void setUp() throws Exception {
final MetadataStorageUpdaterJobSpec metadataStorageUpdaterJobSpec = new MetadataStorageUpdaterJobSpec() {
@Override
public String getSegmentTable() {
return derbyConnectorRule.metadataTablesConfigSupplier().get().getSegmentsTable();
}
@Override
public MetadataStorageConnectorConfig get() {
return derbyConnectorRule.getMetadataConnectorConfig();
}
};
final File scratchFileDir = temporaryFolder.newFolder();
storageLocProperty = System.getProperty(STORAGE_PROPERTY_KEY);
tmpSegmentDir = temporaryFolder.newFolder();
System.setProperty(STORAGE_PROPERTY_KEY, tmpSegmentDir.getAbsolutePath());
final URL url = Preconditions.checkNotNull(Query.class.getClassLoader().getResource("druid.sample.tsv"));
final File tmpInputFile = temporaryFolder.newFile();
FileUtils.retryCopy(new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return url.openStream();
}
}, tmpInputFile, FileUtils.IS_EXCEPTION, 3);
final HadoopDruidIndexerConfig hadoopDruidIndexerConfig = new HadoopDruidIndexerConfig(new HadoopIngestionSpec(new DataSchema(DATASOURCE, HadoopDruidIndexerConfig.JSON_MAPPER.convertValue(new StringInputRowParser(new DelimitedParseSpec(new TimestampSpec("ts", "iso", null), new DimensionsSpec(DimensionsSpec.getDefaultSchemas(Arrays.asList(TestIndex.DIMENSIONS)), null, null), "\t", "", Arrays.asList(TestIndex.COLUMNS)), null), Map.class), new AggregatorFactory[] { new DoubleSumAggregatorFactory(TestIndex.METRICS[0], TestIndex.METRICS[0]), new HyperUniquesAggregatorFactory("quality_uniques", "quality") }, new UniformGranularitySpec(Granularities.MONTH, Granularities.DAY, ImmutableList.<Interval>of(interval)), HadoopDruidIndexerConfig.JSON_MAPPER), new HadoopIOConfig(ImmutableMap.<String, Object>of("type", "static", "paths", tmpInputFile.getAbsolutePath()), metadataStorageUpdaterJobSpec, tmpSegmentDir.getAbsolutePath()), new HadoopTuningConfig(scratchFileDir.getAbsolutePath(), null, null, null, null, null, false, false, false, false, null, false, false, null, null, null, false, false)));
metadataStorageTablesConfigSupplier = derbyConnectorRule.metadataTablesConfigSupplier();
connector = derbyConnectorRule.getConnector();
try {
connector.getDBI().withHandle(new HandleCallback<Void>() {
@Override
public Void withHandle(Handle handle) throws Exception {
handle.execute("DROP TABLE druid_segments");
return null;
}
});
} catch (CallbackFailedException e) {
// Who cares
}
List<Jobby> jobs = ImmutableList.of(new Jobby() {
@Override
public boolean run() {
connector.createSegmentTable(metadataStorageUpdaterJobSpec.getSegmentTable());
return true;
}
}, new HadoopDruidDetermineConfigurationJob(hadoopDruidIndexerConfig), new HadoopDruidIndexerJob(hadoopDruidIndexerConfig, new SQLMetadataStorageUpdaterJobHandler(connector)));
JobHelper.runJobs(jobs, hadoopDruidIndexerConfig);
}
use of org.skife.jdbi.v2.tweak.HandleCallback 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);
}
use of org.skife.jdbi.v2.tweak.HandleCallback 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);
}
use of org.skife.jdbi.v2.tweak.HandleCallback 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);
}
use of org.skife.jdbi.v2.tweak.HandleCallback in project druid by druid-io.
the class SQLAuditManager method doAudit.
@Override
public <T> void doAudit(String key, String type, AuditInfo auditInfo, T payload, ConfigSerde<T> configSerde) {
AuditEntry auditEntry = AuditEntry.builder().key(key).type(type).auditInfo(auditInfo).payload(configSerde.serializeToString(payload, config.isSkipNullField())).build();
dbi.withHandle(new HandleCallback<Void>() {
@Override
public Void withHandle(Handle handle) throws Exception {
doAudit(auditEntry, handle);
return null;
}
});
}
Aggregations