@Override public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception { final TagDefinitionModelDao tagDefinition = getTagDefinitionFromTransaction(tagDefinitionId, entitySqlDaoWrapperFactory, context); final TagSqlDao transactional = entitySqlDaoWrapperFactory.become(TagSqlDao.class); final List<TagModelDao> tags = transactional.getTagsForObject(objectId, objectType, context); TagModelDao tag = null; for (final TagModelDao cur : tags) { if (cur.getTagDefinitionId().equals(tagDefinitionId)) { tag = cur; break; } } if (tag == null) { throw new TagApiException(ErrorCode.TAG_DOES_NOT_EXIST, tagDefinition.getName()); } // Delete the tag transactional.markTagAsDeleted(tag.getId().toString(), context); postBusEventFromTransaction(tag, tag, ChangeType.DELETE, entitySqlDaoWrapperFactory, context); return null; } });
@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); }
@Override public Tag apply(final TagModelDao input) { return TagModelDaoHelper.isControlTag(input.getTagDefinitionId()) ? new DefaultControlTag(input.getId(), ControlTagType.getTypeFromId(input.getTagDefinitionId()), input.getObjectType(), input.getObjectId(), input.getCreatedDate()) : new DescriptiveTag(input.getId(), input.getTagDefinitionId(), input.getObjectType(), input.getObjectId(), input.getCreatedDate()); } };
@Test(groups = "slow") public void testCacheRecordId() throws Exception { this.transactionalSqlDao = new EntitySqlDaoTransactionalJdbiWrapper(dbi, roDbi, clock, controlCacheDispatcher, nonEntityDao, internalCallContextFactory); final TagModelDao tag = new TagModelDao(clock.getUTCNow(), UUID.randomUUID(), UUID.randomUUID(), ObjectType.TAG); // Verify we start with nothing in the cache Assert.assertEquals(getCacheSize(CacheType.RECORD_ID), 0); insertTag(tag); // Verify we still have nothing after insert in the cache Assert.assertEquals(getCacheSize(CacheType.RECORD_ID), 0); final Long tagRecordId = getTagRecordId(tag.getId()); // Verify we now have something in the cache Assert.assertEquals(getCacheSize(CacheType.RECORD_ID), 1); final Long recordIdFromCache = retrieveRecordIdFromCache(tag.getId()); Assert.assertNotNull(recordIdFromCache); Assert.assertEquals(recordIdFromCache, tagRecordId); // We cannot assume the number to be 1 here as the auto_increment implementation // depends on the database. // See also http://h2database.com/html/grammar.html#create_sequence Assert.assertTrue(recordIdFromCache > 0); Assert.assertEquals(getCacheSize(CacheType.RECORD_ID), 1); }
@Test(groups = "slow") public void testRetrieveAuditsViaHistory() throws Exception { addTag(); for (final AuditLevel level : AuditLevel.values()) { final List<AuditLog> auditLogs = auditDao.getAuditLogsForId(TableName.TAG, tag.getId(), level, internalCallContext); verifyAuditLogsForTag(auditLogs, level); final AccountAuditLogs accountAuditLogs = auditDao.getAuditLogsForAccountRecordId(level, internalCallContext); verifyAuditLogsForTag(accountAuditLogs.getAuditLogs(ObjectType.TAG).getAuditLogs(tag.getId()), level); final AccountAuditLogsForObjectType accountAuditLogsForObjectType = auditDao.getAuditLogsForAccountRecordId(TableName.TAG, level, internalCallContext); verifyAuditLogsForTag(accountAuditLogsForObjectType.getAuditLogs(tag.getId()), level); } }
case INSERT: tagEvent = (isControlTag) ? tagEventBuilder.newControlTagCreationEvent(tag.getId(), tag.getObjectId(), tag.getObjectType(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()) : tagEventBuilder.newUserTagCreationEvent(tag.getId(), tag.getObjectId(), tag.getObjectType(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); break; case DELETE: tagEvent = (isControlTag) ? tagEventBuilder.newControlTagDeletionEvent(tag.getId(), tag.getObjectId(), tag.getObjectType(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()) : tagEventBuilder.newUserTagDeletionEvent(tag.getId(), tag.getObjectId(), tag.getObjectType(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); break; bus.postFromTransaction(tagEvent, entitySqlDaoWrapperFactory.getHandle().getConnection()); } catch (final PersistentBus.EventBusException e) { log.warn("Failed to post tag event for tagId='{}'", tag.getId().toString(), e);
@Test(groups = "slow") public void testAllCachesAfterGetById() throws Exception { this.transactionalSqlDao = new EntitySqlDaoTransactionalJdbiWrapper(dbi, roDbi, clock, controlCacheDispatcher, nonEntityDao, internalCallContextFactory); final TagModelDao tag = new TagModelDao(clock.getUTCNow(), UUID.randomUUID(), UUID.randomUUID(), ObjectType.TAG); insertTag(tag); // Verify we start with nothing in the cache Assert.assertEquals(getCacheSize(CacheType.RECORD_ID), 0); Assert.assertEquals(getCacheSize(CacheType.ACCOUNT_RECORD_ID), 0); Assert.assertEquals(getCacheSize(CacheType.TENANT_RECORD_ID), 0); Assert.assertEquals(getCacheSize(CacheType.OBJECT_ID), 0); final TagModelDao result = getById(tag.getId()); Assert.assertEquals(getCacheSize(CacheType.RECORD_ID), 1); Assert.assertEquals(getCacheSize(CacheType.ACCOUNT_RECORD_ID), 1); Assert.assertEquals(getCacheSize(CacheType.TENANT_RECORD_ID), 1); Assert.assertEquals(getCacheSize(CacheType.OBJECT_ID), 1); final Long recordId = (Long) controlCacheDispatcher.getCacheController(CacheType.RECORD_ID).get(tag.getId().toString(), new CacheLoaderArgument(ObjectType.TAG)); Assert.assertEquals(recordId, result.getRecordId()); final Long tenantRecordId = (Long) controlCacheDispatcher.getCacheController(CacheType.TENANT_RECORD_ID).get(tag.getId().toString(), new CacheLoaderArgument(ObjectType.TAG)); Assert.assertEquals(tenantRecordId, result.getTenantRecordId()); final UUID objectId = (UUID) controlCacheDispatcher.getCacheController(CacheType.OBJECT_ID).get(TableName.TAG + CacheControllerDispatcher.CACHE_KEY_SEPARATOR + recordId, new CacheLoaderArgument(ObjectType.TAG)); Assert.assertEquals(objectId, result.getId()); final Long accountRecordId = (Long) controlCacheDispatcher.getCacheController(CacheType.ACCOUNT_RECORD_ID).get(tag.getId().toString(), new CacheLoaderArgument(ObjectType.TAG)); Assert.assertEquals(accountRecordId, result.getAccountRecordId()); }
@Test(groups = "slow") public void testControlTagCreation() throws TagApiException { final UUID accountId = UUID.randomUUID(); final ControlTag tag = new DefaultControlTag(ControlTagType.AUTO_INVOICING_OFF, ObjectType.ACCOUNT, accountId, clock.getUTCNow()); eventsListener.pushExpectedEvent(NextEvent.TAG); tagDao.create(new TagModelDao(tag), internalCallContext); assertListenerStatus(); final TagModelDao savedTag = tagDao.getById(tag.getId(), internalCallContext); assertEquals(savedTag.getTagDefinitionId(), tag.getTagDefinitionId()); assertEquals(savedTag.getId(), tag.getId()); }
@Test(groups = "slow") public void testTagCreationAndRetrieval() throws TagApiException, TagDefinitionApiException { final UUID accountId = UUID.randomUUID(); eventsListener.pushExpectedEvent(NextEvent.TAG_DEFINITION); tagDefinitionDao.create("tag1", "First tag", ObjectType.ACCOUNT.name(), internalCallContext); assertListenerStatus(); eventsListener.pushExpectedEvent(NextEvent.TAG_DEFINITION); final TagDefinitionModelDao testTagDefinition = tagDefinitionDao.create("testTagDefinition", "Second tag", ObjectType.ACCOUNT.name(), internalCallContext); assertListenerStatus(); final Tag tag = new DescriptiveTag(testTagDefinition.getId(), ObjectType.ACCOUNT, accountId, clock.getUTCNow()); eventsListener.pushExpectedEvent(NextEvent.TAG); tagDao.create(new TagModelDao(tag), internalCallContext); assertListenerStatus(); final TagModelDao savedTag = tagDao.getById(tag.getId(), internalCallContext); assertEquals(savedTag.getTagDefinitionId(), tag.getTagDefinitionId()); assertEquals(savedTag.getId(), tag.getId()); }
@Override public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception { final TagDefinitionModelDao tagDefinition = getTagDefinitionFromTransaction(tagDefinitionId, entitySqlDaoWrapperFactory, context); final TagSqlDao transactional = entitySqlDaoWrapperFactory.become(TagSqlDao.class); final List<TagModelDao> tags = transactional.getTagsForObject(objectId, objectType, context); TagModelDao tag = null; for (final TagModelDao cur : tags) { if (cur.getTagDefinitionId().equals(tagDefinitionId)) { tag = cur; break; } } if (tag == null) { throw new TagApiException(ErrorCode.TAG_DOES_NOT_EXIST, tagDefinition.getName()); } // Delete the tag transactional.markTagAsDeleted(tag.getId().toString(), context); postBusEventFromTransaction(tag, tag, ChangeType.DELETE, entitySqlDaoWrapperFactory, context); return null; } });
@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); }
@Override public Tag apply(final TagModelDao input) { return TagModelDaoHelper.isControlTag(input.getTagDefinitionId()) ? new DefaultControlTag(input.getId(), ControlTagType.getTypeFromId(input.getTagDefinitionId()), input.getObjectType(), input.getObjectId(), input.getCreatedDate()) : new DescriptiveTag(input.getId(), input.getTagDefinitionId(), input.getObjectType(), input.getObjectId(), input.getCreatedDate()); } };
@Test(groups = "slow") public void testCacheRecordId() throws Exception { this.transactionalSqlDao = new EntitySqlDaoTransactionalJdbiWrapper(dbi, roDbi, clock, controlCacheDispatcher, nonEntityDao, internalCallContextFactory); final TagModelDao tag = new TagModelDao(clock.getUTCNow(), UUID.randomUUID(), UUID.randomUUID(), ObjectType.TAG); // Verify we start with nothing in the cache Assert.assertEquals(getCacheSize(CacheType.RECORD_ID), 0); insertTag(tag); // Verify we still have nothing after insert in the cache Assert.assertEquals(getCacheSize(CacheType.RECORD_ID), 0); final Long tagRecordId = getTagRecordId(tag.getId()); // Verify we now have something in the cache Assert.assertEquals(getCacheSize(CacheType.RECORD_ID), 1); final Long recordIdFromCache = retrieveRecordIdFromCache(tag.getId()); Assert.assertNotNull(recordIdFromCache); Assert.assertEquals(recordIdFromCache, tagRecordId); // We cannot assume the number to be 1 here as the auto_increment implementation // depends on the database. // See also http://h2database.com/html/grammar.html#create_sequence Assert.assertTrue(recordIdFromCache > 0); Assert.assertEquals(getCacheSize(CacheType.RECORD_ID), 1); }
@Test(groups = "slow") public void testRetrieveAuditsViaHistory() throws Exception { addTag(); for (final AuditLevel level : AuditLevel.values()) { final List<AuditLog> auditLogs = auditDao.getAuditLogsForId(TableName.TAG, tag.getId(), level, internalCallContext); verifyAuditLogsForTag(auditLogs, level); final AccountAuditLogs accountAuditLogs = auditDao.getAuditLogsForAccountRecordId(level, internalCallContext); verifyAuditLogsForTag(accountAuditLogs.getAuditLogs(ObjectType.TAG).getAuditLogs(tag.getId()), level); final AccountAuditLogsForObjectType accountAuditLogsForObjectType = auditDao.getAuditLogsForAccountRecordId(TableName.TAG, level, internalCallContext); verifyAuditLogsForTag(accountAuditLogsForObjectType.getAuditLogs(tag.getId()), level); } }
case INSERT: tagEvent = (isControlTag) ? tagEventBuilder.newControlTagCreationEvent(tag.getId(), tag.getObjectId(), tag.getObjectType(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()) : tagEventBuilder.newUserTagCreationEvent(tag.getId(), tag.getObjectId(), tag.getObjectType(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); break; case DELETE: tagEvent = (isControlTag) ? tagEventBuilder.newControlTagDeletionEvent(tag.getId(), tag.getObjectId(), tag.getObjectType(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()) : tagEventBuilder.newUserTagDeletionEvent(tag.getId(), tag.getObjectId(), tag.getObjectType(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); break; bus.postFromTransaction(tagEvent, entitySqlDaoWrapperFactory.getHandle().getConnection()); } catch (final PersistentBus.EventBusException e) { log.warn("Failed to post tag event for tagId='{}'", tag.getId().toString(), e);
@Test(groups = "slow") public void testAllCachesAfterGetById() throws Exception { this.transactionalSqlDao = new EntitySqlDaoTransactionalJdbiWrapper(dbi, roDbi, clock, controlCacheDispatcher, nonEntityDao, internalCallContextFactory); final TagModelDao tag = new TagModelDao(clock.getUTCNow(), UUID.randomUUID(), UUID.randomUUID(), ObjectType.TAG); insertTag(tag); // Verify we start with nothing in the cache Assert.assertEquals(getCacheSize(CacheType.RECORD_ID), 0); Assert.assertEquals(getCacheSize(CacheType.ACCOUNT_RECORD_ID), 0); Assert.assertEquals(getCacheSize(CacheType.TENANT_RECORD_ID), 0); Assert.assertEquals(getCacheSize(CacheType.OBJECT_ID), 0); final TagModelDao result = getById(tag.getId()); Assert.assertEquals(getCacheSize(CacheType.RECORD_ID), 1); Assert.assertEquals(getCacheSize(CacheType.ACCOUNT_RECORD_ID), 1); Assert.assertEquals(getCacheSize(CacheType.TENANT_RECORD_ID), 1); Assert.assertEquals(getCacheSize(CacheType.OBJECT_ID), 1); final Long recordId = (Long) controlCacheDispatcher.getCacheController(CacheType.RECORD_ID).get(tag.getId().toString(), new CacheLoaderArgument(ObjectType.TAG)); Assert.assertEquals(recordId, result.getRecordId()); final Long tenantRecordId = (Long) controlCacheDispatcher.getCacheController(CacheType.TENANT_RECORD_ID).get(tag.getId().toString(), new CacheLoaderArgument(ObjectType.TAG)); Assert.assertEquals(tenantRecordId, result.getTenantRecordId()); final UUID objectId = (UUID) controlCacheDispatcher.getCacheController(CacheType.OBJECT_ID).get(TableName.TAG + CacheControllerDispatcher.CACHE_KEY_SEPARATOR + recordId, new CacheLoaderArgument(ObjectType.TAG)); Assert.assertEquals(objectId, result.getId()); final Long accountRecordId = (Long) controlCacheDispatcher.getCacheController(CacheType.ACCOUNT_RECORD_ID).get(tag.getId().toString(), new CacheLoaderArgument(ObjectType.TAG)); Assert.assertEquals(accountRecordId, result.getAccountRecordId()); }
@Test(groups = "slow") public void testControlTagCreation() throws TagApiException { final UUID accountId = UUID.randomUUID(); final ControlTag tag = new DefaultControlTag(ControlTagType.AUTO_INVOICING_OFF, ObjectType.ACCOUNT, accountId, clock.getUTCNow()); eventsListener.pushExpectedEvent(NextEvent.TAG); tagDao.create(new TagModelDao(tag), internalCallContext); assertListenerStatus(); final TagModelDao savedTag = tagDao.getById(tag.getId(), internalCallContext); assertEquals(savedTag.getTagDefinitionId(), tag.getTagDefinitionId()); assertEquals(savedTag.getId(), tag.getId()); }
@Test(groups = "slow") public void testTagCreationAndRetrieval() throws TagApiException, TagDefinitionApiException { final UUID accountId = UUID.randomUUID(); eventsListener.pushExpectedEvent(NextEvent.TAG_DEFINITION); tagDefinitionDao.create("tag1", "First tag", ObjectType.ACCOUNT.name(), internalCallContext); assertListenerStatus(); eventsListener.pushExpectedEvent(NextEvent.TAG_DEFINITION); final TagDefinitionModelDao testTagDefinition = tagDefinitionDao.create("testTagDefinition", "Second tag", ObjectType.ACCOUNT.name(), internalCallContext); assertListenerStatus(); final Tag tag = new DescriptiveTag(testTagDefinition.getId(), ObjectType.ACCOUNT, accountId, clock.getUTCNow()); eventsListener.pushExpectedEvent(NextEvent.TAG); tagDao.create(new TagModelDao(tag), internalCallContext); assertListenerStatus(); final TagModelDao savedTag = tagDao.getById(tag.getId(), internalCallContext); assertEquals(savedTag.getTagDefinitionId(), tag.getTagDefinitionId()); assertEquals(savedTag.getId(), tag.getId()); }