@Override public void doAudit(final AuditEntry auditEntry) { dbi.withHandle( new HandleCallback<Void>() { @Override public Void withHandle(Handle handle) throws Exception { doAudit(auditEntry, handle); return null; } } ); }
@Override public List<RolesPermissionsModelDao> getRoleDefinition(final String role) { return dbi.inTransaction(new TransactionCallback<List<RolesPermissionsModelDao>>() { @Override public List<RolesPermissionsModelDao> inTransaction(final Handle handle, final TransactionStatus status) throws Exception { final RolesPermissionsSqlDao rolesPermissionsSqlDao = handle.attach(RolesPermissionsSqlDao.class); return rolesPermissionsSqlDao.getByRoleName(role); } }); }
@Test(groups = "slow") public void testInheritQueries() throws Exception { final KombuchaSqlDao dao = dbi.onDemand(KombuchaSqlDao.class); // Verify non inherited template Assert.assertTrue(dao.isItTimeForKombucha()); // Verify inherited templates Assert.assertFalse(dao.getAll(internalCallContext).hasNext()); } }
@Test(groups = "slow") public void testCreateInternalCallContextWithAccountRecordIdFromAccountObjectType() throws Exception { final UUID accountId = UUID.randomUUID(); final Long accountRecordId = 19384012L; final ImmutableAccountData immutableAccountData = Mockito.mock(ImmutableAccountData.class); Mockito.when(immutableAccountInternalApi.getImmutableAccountDataByRecordId(Mockito.<Long>eq(accountRecordId), Mockito.<InternalTenantContext>any())).thenReturn(immutableAccountData); dbi.withHandle(new HandleCallback<Void>() { @Override public Void withHandle(final Handle handle) throws Exception { // Note: we always create an accounts table, see MysqlTestingHelper handle.execute("insert into accounts (record_id, id, external_key, email, name, first_name_length, reference_time, time_zone, created_date, created_by, updated_date, updated_by) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", accountRecordId, accountId.toString(), accountId.toString(), "yo@t.com", "toto", 4, new Date(), "UTC", new Date(), "i", new Date(), "j"); return null; } }); 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); }
@Test(groups = "slow") public void testSimpleLocking() throws IOException, LockFailedException { final String lockName = UUID.randomUUID().toString(); final GlobalLock lock = locker.lockWithNumberOfTries(LockerType.ACCNT_INV_PAY.toString(), lockName, 3); dbi.inTransaction(new TransactionCallback<Void>() { @Override public Void inTransaction(final Handle conn, final TransactionStatus status) throws Exception { conn.execute("insert into dummy2 (dummy_id) values ('" + UUID.randomUUID().toString() + "')"); return null; } }); Assert.assertEquals(locker.isFree(LockerType.ACCNT_INV_PAY.toString(), lockName), false); boolean gotException = false; try { locker.lockWithNumberOfTries(LockerType.ACCNT_INV_PAY.toString(), lockName, 1); } catch (LockFailedException e) { gotException = true; } Assert.assertTrue(gotException); lock.release(); Assert.assertEquals(locker.isFree(LockerType.ACCNT_INV_PAY.toString(), lockName), true); } }
@Test(groups = "slow", description = "Test Account creation with same External Key in different tenants") public void testCreateAccountWithSameExternalKeyInDifferentTenants() throws Exception { final AccountData accountData = createAccountData(); final Account account1 = accountUserApi.createAccount(accountData, callContext); try { // Same tenant accountUserApi.createAccount(accountData, callContext); Assert.fail(); } catch (final AccountApiException e) { assertEquals(e.getCode(), ErrorCode.ACCOUNT_ALREADY_EXISTS.getCode()); } final TenantSqlDao tenantSqlDao = dbi.onDemand(TenantSqlDao.class); final TenantModelDao tenant2 = new TenantModelDao(); tenantSqlDao.create(tenant2, internalCallContext); final CallContext callContext2 = new DefaultCallContext(account1.getId(), tenant2.getId(), callContext.getUserName(), callContext.getCallOrigin(), callContext.getUserType(), callContext.getUserToken(), clock); final Account account2 = accountUserApi.createAccount(accountData, callContext2); Assert.assertEquals(account1.getExternalKey(), account2.getExternalKey()); Assert.assertNotEquals(account1.getId(), account2.getId()); } }
@Test(groups = "slow") public void testWithMultipleCancellationEvent() throws SubscriptionBillingApiException, SubscriptionBaseApiException { final String prod = "Shotgun"; assertEquals(trialPhase.getPhaseType(), PhaseType.TRIAL); assertEquals(trialPhase.getPhaseType(), PhaseType.EVERGREEN); subscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext); assertEquals(subscription.getLastActiveProduct().getName(), prod); assertEquals(subscription.getLastActivePriceList().getName(), planSet); assertEquals(subscription.getLastActiveBillingPeriod(), term); newCancelEvent.setId(UUID.randomUUID()); final Handle handle = dbi.open(); final SubscriptionEventSqlDao sqlDao = handle.attach(SubscriptionEventSqlDao.class); try {
@Test(groups = "slow", description = "Test Pagination: basic SqlDAO and DAO calls") public void testTagDefinitionsPagination() throws Exception { final TagDefinitionSqlDao tagDefinitionSqlDao = dbi.onDemand(TagDefinitionSqlDao.class); Assert.assertEquals(ImmutableList.<TagDefinitionModelDao>copyOf(tagDefinitionSqlDao.getAll(internalCallContext)).size(), 10); Assert.assertEquals(ImmutableList.<TagDefinitionModelDao>copyOf(tagDefinitionSqlDao.get(0L, 100L, "record_id", "asc", internalCallContext)).size(), 10); Assert.assertEquals(ImmutableList.<TagDefinitionModelDao>copyOf(tagDefinitionSqlDao.get(5L, 100L, "record_id", "asc", internalCallContext)).size(), 5); Assert.assertEquals(ImmutableList.<TagDefinitionModelDao>copyOf(tagDefinitionSqlDao.get(5L, 10L, "record_id", "asc", internalCallContext)).size(), 5); Assert.assertEquals(ImmutableList.<TagDefinitionModelDao>copyOf(tagDefinitionSqlDao.get(0L, 5L, "record_id", "asc", internalCallContext)).size(), 5);
@Test(groups = "slow") public void testRetrieveAuditsDirectly() throws Exception { addTag(); // Verify we get an audit entry for the tag_history table final Handle handle = dbi.open(); final String tagHistoryString = (String) handle.select("select id from tag_history limit 1").get(0).get("id"); handle.close(); for (final AuditLevel level : AuditLevel.values()) { final List<AuditLog> auditLogs = auditDao.getAuditLogsForId(TableName.TAG_HISTORY, UUID.fromString(tagHistoryString), level, internalCallContext); verifyAuditLogsForTag(auditLogs, level); } }
public void checkTrackingIds(final Invoice invoice, final Set<String> expectedTrackingIds, final InternalCallContext internalCallContext) { final InvoiceTrackingSqlDao dao = dbi.onDemand(InvoiceTrackingSqlDao.class); final List<InvoiceTrackingModelDao> result = dao.getTrackingsForInvoice(invoice.getId().toString(), internalCallContext); final Set<String> existingTrackingIds = ImmutableSet.copyOf(Iterables.transform(result, new Function<InvoiceTrackingModelDao, String>() { @Override public String apply(final InvoiceTrackingModelDao input) { return input.getTrackingId(); } })); assertEquals(existingTrackingIds.size(), expectedTrackingIds.size()); for (final String cur : existingTrackingIds) { assertTrue(expectedTrackingIds.contains(cur)); } }
@Override public Object execute() { Assert.assertEquals(subscriptionDao.getSubscriptionBundleForAccount(accountId, internalCallContext).size(), 1); Mockito.verify(dbiSpy, Mockito.times(3)).open(); Mockito.verify(roDbiSpy, Mockito.times(3)).open(); return null; } });
@Override public void define(String key, Object value) { delegate.define(key, value); }
@Test(groups = "slow") public void testInheritQueries() throws Exception { final KombuchaSqlDao dao = dbi.onDemand(KombuchaSqlDao.class); // Verify non inherited template Assert.assertTrue(dao.isItTimeForKombucha()); // Verify inherited templates Assert.assertFalse(dao.getAll(internalCallContext).hasNext()); } }
@Test(groups = "slow") public void testCreateInternalCallContextWithAccountRecordIdFromSimpleObjectType() throws Exception { final UUID invoiceId = UUID.randomUUID(); Mockito.when(immutableAccountInternalApi.getImmutableAccountDataByRecordId(Mockito.<Long>eq(accountRecordId), Mockito.<InternalTenantContext>any())).thenReturn(immutableAccountData); dbi.withHandle(new HandleCallback<Void>() { @Override public Void withHandle(final Handle handle) throws Exception { Assert.assertEquals(context.getAccountRecordId(), accountRecordId); verifyInternalCallContext(context);
@Test(groups = "slow") public void testSimpleLocking() throws IOException, LockFailedException { final String lockName = UUID.randomUUID().toString(); final GlobalLock lock = locker.lockWithNumberOfTries(LockerType.ACCNT_INV_PAY.toString(), lockName, 3); dbi.inTransaction(new TransactionCallback<Void>() { @Override public Void inTransaction(final Handle conn, final TransactionStatus status) throws Exception { conn.execute("insert into dummy2 (dummy_id) values ('" + UUID.randomUUID().toString() + "')"); return null; } }); Assert.assertEquals(locker.isFree(LockerType.ACCNT_INV_PAY.toString(), lockName), false); boolean gotException = false; try { locker.lockWithNumberOfTries(LockerType.ACCNT_INV_PAY.toString(), lockName, 1); } catch (LockFailedException e) { gotException = true; } Assert.assertTrue(gotException); lock.release(); Assert.assertEquals(locker.isFree(LockerType.ACCNT_INV_PAY.toString(), lockName), true); } }
@Test public void testMaintenanceBlocked() { long tableId = createTable("test"); List<ColumnInfo> columns = ImmutableList.of(new ColumnInfo(1, BIGINT)); Set<UUID> oldShards = ImmutableSet.of(UUID.randomUUID()); dbi.onDemand(MetadataDao.class).blockMaintenance(tableId); long transactionId = shardManager.beginTransaction(); try { shardManager.replaceShardUuids(transactionId, tableId, columns, oldShards, ImmutableSet.of(), OptionalLong.empty()); fail("expected exception"); } catch (PrestoException e) { assertEquals(e.getErrorCode(), TRANSACTION_CONFLICT.toErrorCode()); assertEquals(e.getMessage(), "Maintenance is blocked for table"); } }
@Test(groups = "slow") public void testBundleExternalKeyTransferred() throws Exception { assertEquals(result.size(), 1); assertEquals(result.get(0).getExternalKey(), bundle.getExternalKey()); assertEquals(result2.size(), 1); dbi.onDemand(BundleSqlDao.class).renameBundleExternalKey(externalKey, "foo", internalCallContext); final List<SubscriptionBaseBundle> result4 = dao.getSubscriptionBundlesForKey(externalKey, internalCallContext); assertEquals(result4.size(), 2);