protected AuditLog createAuditLog() { final AuditLog auditLog = Mockito.mock(AuditLog.class); final DateTime utcNow = clock.getUTCNow(); Mockito.when(auditLog.getCreatedDate()).thenReturn(utcNow); Mockito.when(auditLog.getReasonCode()).thenReturn(UUID.randomUUID().toString()); Mockito.when(auditLog.getUserName()).thenReturn(UUID.randomUUID().toString()); Mockito.when(auditLog.getUserToken()).thenReturn(UUID.randomUUID().toString()); Mockito.when(auditLog.getComment()).thenReturn(UUID.randomUUID().toString()); Mockito.when(auditLog.getChangeType()).thenReturn(ChangeType.DELETE); return auditLog; } }
@Override public boolean apply(final AuditLog auditLog) { // As we consume the data source, cache the entries cacheAuditLog(auditLog); return objectId.equals(auditLog.getAuditedEntityId()) && // Given our ordering, this should always be true for the first entry ChangeType.INSERT.equals(auditLog.getChangeType()); } }).orNull();
@Override public boolean apply(final AuditLog auditLog) { return objectType.equals(auditLog.getAuditedObjectType()); } };
public AuditLogJson(final AuditLog auditLog) { this(auditLog.getChangeType().toString(), auditLog.getCreatedDate(), auditLog.getAuditedObjectType(), auditLog.getAuditedEntityId(), auditLog.getUserName(), auditLog.getReasonCode(), auditLog.getComment(), auditLog.getUserToken(), null); }
private List<AuditLog> filterAuditLogs(final AuditLevel auditLevel, final List<AuditLog> auditLogs) { // TODO Do the filtering in the query if (AuditLevel.FULL.equals(auditLevel)) { return auditLogs; } else if (AuditLevel.MINIMAL.equals(auditLevel) && !auditLogs.isEmpty()) { if (ChangeType.INSERT.equals(auditLogs.get(0).getChangeType())) { return ImmutableList.<AuditLog>of(auditLogs.get(0)); } else { // We may be coming here via the history code path - only a single mapped history record id // will be for the initial INSERT return ImmutableList.<AuditLog>of(); } } else if (AuditLevel.NONE.equals(auditLevel)) { return ImmutableList.<AuditLog>of(); } else { return auditLogs; } } }
private <T extends EntitySqlDao<M, E>, M extends EntityModelDao<E>, E extends Entity> void checkAuditLog(final ChangeType changeType, @Nullable final CallContext context, final AuditLog auditLog, final UUID entityId, Class<T> sqlDao, boolean useHistory, boolean checkContext) { Assert.assertEquals(auditLog.getChangeType(), changeType); if (checkContext) { Assert.assertEquals(auditLog.getUserName(), context.getUserName()); Assert.assertEquals(auditLog.getComment(), context.getComments()); //Assert.assertEquals(auditLog.getCreatedDate().comparesTo(callcontext.getCreatedDate())); // We can't take userToken oustide of the 'if' because for instance NextBillingDate invoice will not have it. Assert.assertEquals(auditLog.getUserToken(), context.getUserToken().toString()); } final M entityModel = extractEntityModelFromEntityWithTargetRecordId(entityId, auditLog.getId(), sqlDao, context, useHistory); Assert.assertEquals(entityModel.getId(), entityId); }
private void cacheAuditLog(final AuditLog auditLog) { initializeIfNeeded(auditLog.getAuditedEntityId()); auditLogsCache.get(auditLog.getAuditedEntityId()).add(auditLog); }
@Test(groups = "slow") public void testVerifyAuditCachesAreCleared() throws Exception { addTag(); final List<AuditLog> firstAuditLogs = auditDao.getAuditLogsForId(TableName.TAG, tag.getId(), AuditLevel.FULL, internalCallContext); Assert.assertEquals(firstAuditLogs.size(), 1); Assert.assertEquals(firstAuditLogs.get(0).getChangeType(), ChangeType.INSERT); eventsListener.pushExpectedEvent(NextEvent.TAG); tagDao.deleteTag(tag.getObjectId(), tag.getObjectType(), tag.getTagDefinitionId(), internalCallContext); assertListenerStatus(); final List<AuditLog> secondAuditLogs = auditDao.getAuditLogsForId(TableName.TAG, tag.getId(), AuditLevel.FULL, internalCallContext); Assert.assertEquals(secondAuditLogs.size(), 2); Assert.assertEquals(secondAuditLogs.get(0).getChangeType(), ChangeType.INSERT); Assert.assertEquals(secondAuditLogs.get(1).getChangeType(), ChangeType.DELETE); }
private void cacheAuditLog(final AuditLog auditLog) { initializeIfNeeded(auditLog.getAuditedEntityId()); auditLogsCache.get(auditLog.getAuditedEntityId()).add(auditLog); }
private void verifyAuditLogsForTag(final List<AuditLog> auditLogs, final AuditLevel level) { if (AuditLevel.NONE.equals(level)) { Assert.assertEquals(auditLogs.size(), 0); return; } Assert.assertEquals(auditLogs.size(), 1); Assert.assertEquals(auditLogs.get(0).getUserToken(), internalCallContext.getUserToken().toString()); Assert.assertEquals(auditLogs.get(0).getChangeType(), ChangeType.INSERT); Assert.assertEquals(auditLogs.get(0).getComment(), internalCallContext.getComments()); Assert.assertEquals(auditLogs.get(0).getReasonCode(), internalCallContext.getReasonCode()); Assert.assertEquals(auditLogs.get(0).getUserName(), internalCallContext.getCreatedBy()); Assert.assertNotNull(auditLogs.get(0).getCreatedDate()); } }
@Test(groups = "slow", description = "Test Account DAO: add and remove email") public void testAddRemoveAccountEmail() throws AccountApiException { final UUID accountId = UUID.randomUUID(); // Add a new e-mail final AccountEmail email = new DefaultAccountEmail(accountId, "test@gmail.com"); accountDao.addEmail(new AccountEmailModelDao(email), internalCallContext); final List<AccountEmailModelDao> accountEmails = accountDao.getEmailsByAccountId(accountId, internalCallContext); Assert.assertEquals(accountEmails.size(), 1); Assert.assertEquals(accountEmails.get(0).getAccountId(), accountId); Assert.assertEquals(accountEmails.get(0).getEmail(), email.getEmail()); // Verify audits final List<AuditLog> auditLogsForAccountEmail = auditDao.getAuditLogsForId(TableName.ACCOUNT_EMAIL, email.getId(), AuditLevel.FULL, internalCallContext); Assert.assertEquals(auditLogsForAccountEmail.size(), 1); Assert.assertEquals(auditLogsForAccountEmail.get(0).getChangeType(), ChangeType.INSERT); // Delete the e-mail accountDao.removeEmail(new AccountEmailModelDao(email), internalCallContext); Assert.assertEquals(accountDao.getEmailsByAccountId(accountId, internalCallContext).size(), 0); }
@Override public boolean apply(final AuditLog auditLog) { // As we consume the data source, cache the entries cacheAuditLog(auditLog); return objectId.equals(auditLog.getAuditedEntityId()) && // Given our ordering, this should always be true for the first entry ChangeType.INSERT.equals(auditLog.getChangeType()); } }).orNull();
@Override public boolean apply(final AuditLog auditLog) { return objectType.equals(auditLog.getAuditedObjectType()); } };
@Test(groups = "fast") public void testGetters() throws Exception { final TableName tableName = TableName.ACCOUNT_EMAIL_HISTORY; final long recordId = Long.MAX_VALUE; final ChangeType changeType = ChangeType.DELETE; final EntityAudit entityAudit = new EntityAudit(tableName, recordId, changeType, null); final UUID accountId = UUID.randomUUID(); final UUID tenantId = UUID.randomUUID(); final String userName = UUID.randomUUID().toString(); final CallOrigin callOrigin = CallOrigin.EXTERNAL; final UserType userType = UserType.CUSTOMER; final UUID userToken = UUID.randomUUID(); final DefaultCallContext callContext = new DefaultCallContext(accountId, tenantId, userName, callOrigin, userType, userToken, clock); final AuditLog auditLog = new DefaultAuditLog(new AuditLogModelDao(entityAudit, callContext), ObjectType.ACCOUNT_EMAIL, UUID.randomUUID()); Assert.assertEquals(auditLog.getChangeType(), changeType); Assert.assertNull(auditLog.getComment()); Assert.assertNotNull(auditLog.getCreatedDate()); Assert.assertNull(auditLog.getReasonCode()); Assert.assertEquals(auditLog.getUserName(), userName); Assert.assertEquals(auditLog.getUserToken(), userToken.toString()); }
Assert.assertEquals(auditLogsForAccountEmail1.get(0).getChangeType(), ChangeType.INSERT); Assert.assertEquals(auditLogsForAccountEmail1.get(1).getChangeType(), ChangeType.DELETE); final List<AuditLog> auditLogsForAccountEmail2 = auditDao.getAuditLogsForId(TableName.ACCOUNT_EMAIL, accountEmail2.getId(), AuditLevel.FULL, internalCallContext); Assert.assertEquals(auditLogsForAccountEmail2.size(), 1); Assert.assertEquals(auditLogsForAccountEmail2.get(0).getChangeType(), ChangeType.INSERT);
protected AuditLog createAuditLog() { final AuditLog auditLog = Mockito.mock(AuditLog.class); final DateTime utcNow = clock.getUTCNow(); Mockito.when(auditLog.getCreatedDate()).thenReturn(utcNow); Mockito.when(auditLog.getReasonCode()).thenReturn(UUID.randomUUID().toString()); Mockito.when(auditLog.getUserName()).thenReturn(UUID.randomUUID().toString()); Mockito.when(auditLog.getUserToken()).thenReturn(UUID.randomUUID().toString()); Mockito.when(auditLog.getComment()).thenReturn(UUID.randomUUID().toString()); Mockito.when(auditLog.getChangeType()).thenReturn(ChangeType.DELETE); return auditLog; } }
@Test(groups = "slow", description = "Test Account: verify audits") public void testAudits() throws AccountApiException { // Special test to verify audits - they are handled a bit differently due to the account record id (see EntitySqlDaoWrapperInvocationHandler#insertAudits) final AccountModelDao account1 = createTestAccount(); accountDao.create(account1, internalCallContext); refreshCallContext(account1.getId()); // Verify audits via account record id final DefaultAccountAuditLogs auditLogsForAccount1ViaAccountRecordId1 = auditDao.getAuditLogsForAccountRecordId(AuditLevel.FULL, internalCallContext); Assert.assertEquals(auditLogsForAccount1ViaAccountRecordId1.getAuditLogsForAccount().size(), 1); Assert.assertEquals(auditLogsForAccount1ViaAccountRecordId1.getAuditLogsForAccount().get(0).getChangeType(), ChangeType.INSERT); // Add an entry in the account_history table to make sure we pick up the right // record id / target record id / account record id in the audit_log table accountDao.updatePaymentMethod(account1.getId(), UUID.randomUUID(), internalCallContext); final AccountModelDao account2 = createTestAccount(); accountDao.create(account2, internalCallContext); refreshCallContext(account2.getId()); // Verify audits via account record id final DefaultAccountAuditLogs auditLogsForAccount2ViaAccountRecordId = auditDao.getAuditLogsForAccountRecordId(AuditLevel.FULL, internalCallContext); Assert.assertEquals(auditLogsForAccount2ViaAccountRecordId.getAuditLogsForAccount().size(), 1); Assert.assertEquals(auditLogsForAccount2ViaAccountRecordId.getAuditLogsForAccount().get(0).getChangeType(), ChangeType.INSERT); refreshCallContext(account1.getId()); final DefaultAccountAuditLogs auditLogsForAccount1ViaAccountRecordId2 = auditDao.getAuditLogsForAccountRecordId(AuditLevel.FULL, internalCallContext); Assert.assertEquals(auditLogsForAccount1ViaAccountRecordId2.getAuditLogsForAccount().size(), 2); Assert.assertEquals(auditLogsForAccount1ViaAccountRecordId2.getAuditLogsForAccount().get(0).getChangeType(), ChangeType.INSERT); Assert.assertEquals(auditLogsForAccount1ViaAccountRecordId2.getAuditLogsForAccount().get(1).getChangeType(), ChangeType.UPDATE); }
private void verifyAuditLogsForTag(final List<AuditLog> auditLogs, final AuditLevel level) { if (AuditLevel.NONE.equals(level)) { Assert.assertEquals(auditLogs.size(), 0); return; } Assert.assertEquals(auditLogs.size(), 1); Assert.assertEquals(auditLogs.get(0).getUserToken(), internalCallContext.getUserToken().toString()); Assert.assertEquals(auditLogs.get(0).getChangeType(), ChangeType.INSERT); Assert.assertEquals(auditLogs.get(0).getComment(), internalCallContext.getComments()); Assert.assertEquals(auditLogs.get(0).getReasonCode(), internalCallContext.getReasonCode()); Assert.assertEquals(auditLogs.get(0).getUserName(), internalCallContext.getCreatedBy()); Assert.assertNotNull(auditLogs.get(0).getCreatedDate()); } }
@Test(groups = "slow", description = "Test Account: basic DAO calls") public void testBasic() throws AccountApiException { final AccountModelDao account = createTestAccount(); accountDao.create(account, internalCallContext); refreshCallContext(account.getId()); // Retrieve by key AccountModelDao retrievedAccount = accountDao.getAccountByKey(account.getExternalKey(), internalCallContext); checkAccountsEqual(retrievedAccount, account); // Retrieve by id retrievedAccount = accountDao.getById(retrievedAccount.getId(), internalCallContext); checkAccountsEqual(retrievedAccount, account); // Retrieve all final Pagination<AccountModelDao> allAccounts = accountDao.getAll(internalCallContext); final List<AccountModelDao> all = ImmutableList.<AccountModelDao>copyOf(allAccounts); Assert.assertNotNull(all); Assert.assertEquals(all.size(), 1); checkAccountsEqual(all.get(0), account); // Verify audits final List<AuditLog> auditLogsForAccount = auditDao.getAuditLogsForId(TableName.ACCOUNT, account.getId(), AuditLevel.FULL, internalCallContext); Assert.assertEquals(auditLogsForAccount.size(), 1); Assert.assertEquals(auditLogsForAccount.get(0).getChangeType(), ChangeType.INSERT); }
@Test(groups = "fast") public void testGetters() throws Exception { final TableName tableName = TableName.ACCOUNT_EMAIL_HISTORY; final long recordId = Long.MAX_VALUE; final ChangeType changeType = ChangeType.DELETE; final EntityAudit entityAudit = new EntityAudit(tableName, recordId, changeType, null); final UUID accountId = UUID.randomUUID(); final UUID tenantId = UUID.randomUUID(); final String userName = UUID.randomUUID().toString(); final CallOrigin callOrigin = CallOrigin.EXTERNAL; final UserType userType = UserType.CUSTOMER; final UUID userToken = UUID.randomUUID(); final DefaultCallContext callContext = new DefaultCallContext(accountId, tenantId, userName, callOrigin, userType, userToken, clock); final AuditLog auditLog = new DefaultAuditLog(new AuditLogModelDao(entityAudit, callContext), ObjectType.ACCOUNT_EMAIL, UUID.randomUUID()); Assert.assertEquals(auditLog.getChangeType(), changeType); Assert.assertNull(auditLog.getComment()); Assert.assertNotNull(auditLog.getCreatedDate()); Assert.assertNull(auditLog.getReasonCode()); Assert.assertEquals(auditLog.getUserName(), userName); Assert.assertEquals(auditLog.getUserToken(), userToken.toString()); }