@Override public List<TagModelDao> getTagsForAccountType(final UUID accountId, final ObjectType objectType, final boolean includedDeleted, final InternalTenantContext internalTenantContext) { final List<TagModelDao> allTags = getTagsForAccount(includedDeleted, internalTenantContext); return ImmutableList.<TagModelDao>copyOf(Collections2.filter(allTags, new Predicate<TagModelDao>() { @Override public boolean apply(final TagModelDao input) { return input.getObjectType() == objectType; } })); }
@Override public void create(final TagModelDao entity, final InternalCallContext context) throws TagApiException { transactionalSqlDao.execute(TagApiException.class, getCreateEntitySqlDaoTransactionWrapper(entity, context)); }
tagDao.create(new TagModelDao(tag), internalCallContext); assertListenerStatus(); final List<TagModelDao> foundTags = tagDao.getTagsForObject(objectId, objectType, false, internalCallContext); Assert.assertEquals(foundTags.size(), 1); 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()); tagDao.deleteTag(objectId, objectType, createdTagDefinition.getId(), internalCallContext); assertListenerStatus(); Assert.assertEquals(tagDao.getTagsForObject(objectId, objectType, false, internalCallContext).size(), 0); Assert.assertEquals(tagDao.getTagsForAccount(false, internalCallContext).size(), 0); Assert.assertEquals(tagDao.getTagsForObject(objectId, objectType, true, internalCallContext).size(), 1); Assert.assertEquals(tagDao.getTagsForAccount(true, internalCallContext).size(), 1);
@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), 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 testDeleteTagBeforeDeleteTagDefinition() throws TagDefinitionApiException, TagApiException { final String definitionName = "TestTag1234567"; eventsListener.pushExpectedEvent(NextEvent.TAG_DEFINITION); tagDefinitionDao.create(definitionName, "Some test tag", internalCallContext); assertListenerStatus(); final TagDefinitionModelDao tagDefinition = tagDefinitionDao.getByName(definitionName, internalCallContext); assertNotNull(tagDefinition); final UUID objectId = UUID.randomUUID(); final Tag tag = new DescriptiveTag(tagDefinition.getId(), ObjectType.ACCOUNT, objectId, internalCallContext.getCreatedDate()); eventsListener.pushExpectedEvent(NextEvent.TAG); tagDao.create(new TagModelDao(tag), internalCallContext); assertListenerStatus(); eventsListener.pushExpectedEvent(NextEvent.TAG); tagDao.deleteTag(objectId, ObjectType.ACCOUNT, tagDefinition.getId(), internalCallContext); assertListenerStatus(); eventsListener.pushExpectedEvent(NextEvent.TAG_DEFINITION); tagDefinitionDao.deleteById(tagDefinition.getId(), internalCallContext); assertListenerStatus(); }
@Test(groups = "slow") public void testInsertMultipleTags() throws TagApiException { final UUID objectId = UUID.randomUUID(); final ObjectType objectType = ObjectType.INVOICE_ITEM; eventsListener.pushExpectedEvent(NextEvent.TAG); final Tag tag = new DescriptiveTag(ControlTagType.AUTO_INVOICING_OFF.getId(), objectType, objectId, internalCallContext.getCreatedDate()); tagDao.create(new TagModelDao(tag), internalCallContext); assertListenerStatus(); try { final Tag tag2 = new DescriptiveTag(ControlTagType.AUTO_INVOICING_OFF.getId(), objectType, objectId, internalCallContext.getCreatedDate()); tagDao.create(new TagModelDao(tag2), internalCallContext); Assert.fail("Should not be able to create twice the same tag"); assertListenerStatus(); } catch (final TagApiException e) { Assert.assertEquals(ErrorCode.TAG_ALREADY_EXISTS.getCode(), e.getCode()); } }
@Override public Void inTransaction(final EntitySqlDaoWrapperFactory<EntitySqlDao> 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); }
final TagDefinitionModelDao tagDefinition = getTagDefinitionFromTransaction(tag.getTagDefinitionId(), entitySqlDaoWrapperFactory, context); final boolean isControlTag = ControlTagType.getTypeFromId(tagDefinition.getId()) != null; switch (changeType) {
@Test(groups = "slow") public void testTagCreationAndRetrieval() throws TagApiException, TagDefinitionApiException { final UUID accountId = UUID.randomUUID(); eventsListener.pushExpectedEvent(NextEvent.TAG_DEFINITION); tagDefinitionDao.create("tag1", "First tag", internalCallContext); assertListenerStatus(); eventsListener.pushExpectedEvent(NextEvent.TAG_DEFINITION); final TagDefinitionModelDao testTagDefinition = tagDefinitionDao.create("testTagDefinition", "Second tag", 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()); }
@Test(groups = "slow", expectedExceptions = TagDefinitionApiException.class) public void testTagDefinitionDeletionForDefinitionInUse() throws TagDefinitionApiException, TagApiException { final String definitionName = "TestTag12345"; eventsListener.pushExpectedEvent(NextEvent.TAG_DEFINITION); tagDefinitionDao.create(definitionName, "Some test tag", internalCallContext); assertListenerStatus(); final TagDefinitionModelDao tagDefinition = tagDefinitionDao.getByName(definitionName, internalCallContext); assertNotNull(tagDefinition); final UUID objectId = UUID.randomUUID(); final Tag tag = new DescriptiveTag(tagDefinition.getId(), ObjectType.ACCOUNT, objectId, internalCallContext.getCreatedDate()); eventsListener.pushExpectedEvent(NextEvent.TAG); tagDao.create(new TagModelDao(tag), internalCallContext); assertListenerStatus(); tagDefinitionDao.deleteById(tagDefinition.getId(), internalCallContext); }