@Test(groups = "slow", description = "Test Account DAO: tags") public void testTags() throws TagApiException, TagDefinitionApiException { final AccountModelDao account = createTestAccount(); final TagDefinitionModelDao tagDefinition = tagDefinitionDao.create(UUID.randomUUID().toString().substring(0, 4), UUID.randomUUID().toString(), ObjectType.ACCOUNT.name(), internalCallContext); final Tag tag = new DescriptiveTag(tagDefinition.getId(), ObjectType.ACCOUNT, account.getId(), internalCallContext.getCreatedDate()); tagDao.create(new TagModelDao(tag), internalCallContext); final List<TagModelDao> tags = tagDao.getTagsForObject(account.getId(), ObjectType.ACCOUNT, false, internalCallContext); Assert.assertEquals(tags.size(), 1); Assert.assertEquals(tags.get(0).getTagDefinitionId(), tagDefinition.getId()); Assert.assertEquals(tags.get(0).getObjectId(), account.getId()); Assert.assertEquals(tags.get(0).getObjectType(), ObjectType.ACCOUNT); }
@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()); } };
@Override public void create(final TagModelDao entity, final InternalCallContext context) throws TagApiException { validateApplicableObjectTypes(entity.getTagDefinitionId(), entity.getObjectType()); transactionalSqlDao.execute(false, TagApiException.class, getCreateEntitySqlDaoTransactionWrapper(entity, context)); }
@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()); }
private void addTag() throws TagDefinitionApiException, TagApiException { // Create a tag definition eventsListener.pushExpectedEvent(NextEvent.TAG_DEFINITION); final TagDefinitionModelDao tagDefinition = tagDefinitionDao.create(UUID.randomUUID().toString().substring(0, 5), UUID.randomUUID().toString().substring(0, 5), ObjectType.ACCOUNT.name(), internalCallContext); assertListenerStatus(); Assert.assertEquals(tagDefinitionDao.getById(tagDefinition.getId(), internalCallContext), tagDefinition); // Create a tag final UUID objectId = UUID.randomUUID(); final Tag theTag = new DescriptiveTag(tagDefinition.getId(), ObjectType.ACCOUNT, objectId, clock.getUTCNow()); eventsListener.pushExpectedEvent(NextEvent.TAG); tagDao.create(new TagModelDao(theTag), internalCallContext); assertListenerStatus(); final List<TagModelDao> tags = tagDao.getTagsForObject(objectId, ObjectType.ACCOUNT, false, internalCallContext); Assert.assertEquals(tags.size(), 1); tag = tags.get(0); Assert.assertEquals(tag.getTagDefinitionId(), tagDefinition.getId()); }
@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()); }
@Override public void addTag(final UUID objectId, final ObjectType objectType, final UUID tagDefinitionId, final InternalCallContext context) throws TagApiException { final TagModelDao tag = new TagModelDao(context.getCreatedDate(), tagDefinitionId, objectId, objectType); try { tagDao.create(tag, context); } catch (TagApiException e) { // Be lenient here and make the addTag method idempotent if (ErrorCode.TAG_ALREADY_EXISTS.getCode() != e.getCode()) { throw e; } } }
@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 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); }
@Override public boolean apply(final TagModelDao input) { return input.getObjectType() == objectType; } }));
@Override public void create(final TagModelDao tag, final InternalCallContext context) throws TagApiException { if (tagStore.get(tag.getObjectId()) == null) { tagStore.put(tag.getObjectId(), new ArrayList<TagModelDao>()); } // add it to the account tags if (tagStore.get(getAccountId(context.getAccountRecordId())) == null) { tagStore.put(getAccountId(context.getAccountRecordId()), new ArrayList<TagModelDao>()); } tagStore.get(tag.getObjectId()).add(tag); tagStore.get(getAccountId(context.getAccountRecordId())).add(tag); }
@Override public void deleteTag(final UUID objectId, final ObjectType objectType, final UUID tagDefinitionId, final InternalCallContext context) { final List<TagModelDao> tags = tagStore.get(objectId); if (tags != null) { final Iterator<TagModelDao> tagIterator = tags.iterator(); while (tagIterator.hasNext()) { final TagModelDao tag = tagIterator.next(); if (tag.getTagDefinitionId().equals(tagDefinitionId)) { tagIterator.remove(); } } } }
@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); } }
@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()); }
tagDao.create(new TagModelDao(tag), internalCallContext); assertListenerStatus(); Assert.assertEquals(foundTags.get(0).getTagDefinitionId(), createdTagDefinition.getId()); final List<TagModelDao> foundTagsForAccount = tagDao.getTagsForAccount(false, internalCallContext); Assert.assertEquals(foundTagsForAccount.size(), 1); Assert.assertEquals(foundTagsForAccount.get(0).getTagDefinitionId(), createdTagDefinition.getId());
@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()); }
@Override public void addTag(final UUID objectId, final ObjectType objectType, final UUID tagDefinitionId, final CallContext context) throws TagApiException { if (SystemTags.isSystemTag(tagDefinitionId)) { // TODO Create a proper ErrorCode instaed throw new IllegalStateException(String.format("Failed to add tag for tagDefinitionId='%s': System tags are reserved for the system.", tagDefinitionId)); } final InternalCallContext internalContext = internalCallContextFactory.createInternalCallContext(objectId, objectType, context); final TagModelDao tag = new TagModelDao(context.getCreatedDate(), tagDefinitionId, objectId, objectType); try { tagDao.create(tag, internalContext); } catch (TagApiException e) { // Be lenient here and make the addTag method idempotent if (ErrorCode.TAG_ALREADY_EXISTS.getCode() != e.getCode()) { throw e; } } }
@Override public void create(final TagModelDao entity, final InternalCallContext context) throws TagApiException { validateApplicableObjectTypes(entity.getTagDefinitionId(), entity.getObjectType()); transactionalSqlDao.execute(false, TagApiException.class, getCreateEntitySqlDaoTransactionWrapper(entity, context)); }
@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; } });