@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();
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; } } }
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") 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 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 = "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()); }
@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); }
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);
@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); }
@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); }
@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();
protected AuditLog getPaymentCreationAuditLog(final UUID paymentId, final AccountAuditLogs accountAuditLogs) throws OSGIServiceNotAvailable { final List<AuditLog> auditLogsForPayment = accountAuditLogs.getAuditLogsForPayment(paymentId); for (final AuditLog auditLog : auditLogsForPayment) { if (auditLog.getChangeType().equals(ChangeType.INSERT)) { return auditLog; } } logService.log(LogService.LOG_WARNING, "Unable to find payment creation audit log for id " + paymentId); return null; }
protected AuditLog getInvoiceCreationAuditLog(final UUID invoiceId, final AccountAuditLogs accountAuditLogs) throws OSGIServiceNotAvailable { final List<AuditLog> auditLogsForInvoice = accountAuditLogs.getAuditLogsForInvoice(invoiceId); for (final AuditLog auditLog : auditLogsForInvoice) { if (auditLog.getChangeType().equals(ChangeType.INSERT)) { return auditLog; } } logService.log(LogService.LOG_WARNING, "Unable to find Invoice creation audit log for id " + invoiceId); return null; }
protected AuditLog getInvoicePaymentCreationAuditLog(final UUID invoicePaymentId, final AccountAuditLogs accountAuditLogs) throws OSGIServiceNotAvailable { final List<AuditLog> auditLogsForInvoicePayment = accountAuditLogs.getAuditLogsForInvoicePayment(invoicePaymentId); for (final AuditLog auditLog : auditLogsForInvoicePayment) { if (auditLog.getChangeType().equals(ChangeType.INSERT)) { return auditLog; } } logService.log(LogService.LOG_WARNING, "Unable to find Invoice payment creation audit log for id " + invoicePaymentId); return null; }
protected AuditLog getAccountCreationAuditLog(final UUID accountId, final AccountAuditLogs accountAuditLogs) throws OSGIServiceNotAvailable { final List<AuditLog> auditLogsForAccount = accountAuditLogs.getAuditLogsForAccount(); for (final AuditLog auditLog : auditLogsForAccount) { if (auditLog.getChangeType().equals(ChangeType.INSERT)) { return auditLog; } } logService.log(LogService.LOG_WARNING, "Unable to find Account creation audit log for id " + accountId); return null; }
protected AuditLog getBundleCreationAuditLog(final UUID bundleId, final AccountAuditLogs accountAuditLogs) throws OSGIServiceNotAvailable { final List<AuditLog> auditLogsForBundle = accountAuditLogs.getAuditLogsForBundle(bundleId); for (final AuditLog auditLog : auditLogsForBundle) { if (auditLog.getChangeType().equals(ChangeType.INSERT)) { return auditLog; } } logService.log(LogService.LOG_WARNING, "Unable to find Bundle creation audit log for id " + bundleId); return null; }
protected AuditLog getInvoiceItemCreationAuditLog(final UUID invoiceItemId, final AccountAuditLogs accountAuditLogs) throws OSGIServiceNotAvailable { final List<AuditLog> auditLogsForInvoiceItem = accountAuditLogs.getAuditLogsForInvoiceItem(invoiceItemId); for (final AuditLog auditLog : auditLogsForInvoiceItem) { if (auditLog.getChangeType().equals(ChangeType.INSERT)) { return auditLog; } } logService.log(LogService.LOG_WARNING, "Unable to find Invoice item creation audit log for id " + invoiceItemId); return null; }
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; } }
protected AuditLog getSubscriptionEventCreationAuditLog(final UUID subscriptionEventId, final ObjectType objectType, final AccountAuditLogs accountAuditLogs) throws OSGIServiceNotAvailable { final List<AuditLog> auditLogsForSubscriptionEvent = accountAuditLogs.getAuditLogs(objectType).getAuditLogs(subscriptionEventId); for (final AuditLog auditLog : auditLogsForSubscriptionEvent) { if (auditLog.getChangeType().equals(ChangeType.INSERT)) { return auditLog; } } logService.log(LogService.LOG_WARNING, "Unable to find Subscription event creation audit log for id " + subscriptionEventId); return null; }
private void checkSubscriptionEventAuditLog(final List<SubscriptionEvent> transitions, final int idx, final SubscriptionEventType expectedType) { assertEquals(transitions.get(idx).getSubscriptionEventType(), expectedType); final List<AuditLog> auditLogs = auditUserApi.getAuditLogs(transitions.get(idx).getId(), transitions.get(idx).getSubscriptionEventType().getObjectType(), AuditLevel.FULL, callContext); assertEquals(auditLogs.size(), 1); assertEquals(auditLogs.get(0).getChangeType(), ChangeType.INSERT); } }