@Override public boolean apply(final TagDefinitionModelDao input) { return input.getId().equals(tagDefinitionId); } });
@Override protected TagDefinitionApiException generateAlreadyExistsException(final TagDefinitionModelDao entity, final InternalCallContext context) { return new TagDefinitionApiException(ErrorCode.TAG_DEFINITION_ALREADY_EXISTS, entity.getId()); } }
public static TagDefinitionModelDao lookup(final UUID tagDefinitionId) { for (final ControlTagType t : ControlTagType.values()) { if (t.getId().equals(tagDefinitionId)) { return new TagDefinitionModelDao(t); } } for (final TagDefinitionModelDao t : SYSTEM_DEFINED_TAG_DEFINITIONS) { if (t.getId().equals(tagDefinitionId)) { return t; } } return null; } }
@Override public TagDefinitionModelDao create(final String definitionName, final String description, final String tagDefinitionObjectTypes, final InternalCallContext context) throws TagDefinitionApiException { final TagDefinitionModelDao tag = new TagDefinitionModelDao(null, definitionName, description, tagDefinitionObjectTypes); tags.put(tag.getId().toString(), tag); return tag; }
public DefaultTagDefinition(final TagDefinitionModelDao tagDefinitionModelDao, final boolean isControlTag) { this(tagDefinitionModelDao.getId(), tagDefinitionModelDao.getName(), tagDefinitionModelDao.getDescription(), isControlTag, toObjectTypes(tagDefinitionModelDao.getApplicableObjectTypes())); }
protected void postBusEventFromTransaction(final TagDefinitionModelDao tagDefinition, final TagDefinitionModelDao savedTagDefinition, final ChangeType changeType, final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory, final InternalCallContext context) throws BillingExceptionBase { final TagDefinitionInternalEvent tagDefinitionEvent; final boolean isControlTag = TagModelDaoHelper.isControlTag(tagDefinition.getName()); switch (changeType) { case INSERT: tagDefinitionEvent = (isControlTag) ? tagEventBuilder.newControlTagDefinitionCreationEvent(tagDefinition.getId(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()) : tagEventBuilder.newUserTagDefinitionCreationEvent(tagDefinition.getId(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); break; case DELETE: tagDefinitionEvent = (isControlTag) ? tagEventBuilder.newControlTagDefinitionDeletionEvent(tagDefinition.getId(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()) : tagEventBuilder.newUserTagDefinitionDeletionEvent(tagDefinition.getId(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); break; default: return; } try { bus.postFromTransaction(tagDefinitionEvent, entitySqlDaoWrapperFactory.getHandle().getConnection()); } catch (final PersistentBus.EventBusException e) { log.warn("Failed to post tag definition event for tagDefinitionId='{}'", tagDefinition.getId().toString(), e); } }
@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); }
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 testGetByIds() throws TagDefinitionApiException { final List<UUID> uuids = new ArrayList<UUID>(); // Check with a empty Collection first List<TagDefinitionModelDao> result = tagDefinitionDao.getByIds(uuids, internalCallContext); assertEquals(result.size(), 0); eventsListener.pushExpectedEvent(NextEvent.TAG_DEFINITION); final TagDefinitionModelDao defYo = tagDefinitionDao.create(UUID.randomUUID().toString().substring(0, 5), "defintion yo", ObjectType.ACCOUNT.name(), internalCallContext); assertListenerStatus(); uuids.add(defYo.getId()); eventsListener.pushExpectedEvent(NextEvent.TAG_DEFINITION); final TagDefinitionModelDao defBah = tagDefinitionDao.create(UUID.randomUUID().toString().substring(0, 5), "defintion bah", ObjectType.ACCOUNT.name(), internalCallContext); assertListenerStatus(); uuids.add(defBah.getId()); eventsListener.pushExpectedEvent(NextEvent.TAG_DEFINITION); final TagDefinitionModelDao defZoo = tagDefinitionDao.create(UUID.randomUUID().toString().substring(0, 5), "defintion zoo", ObjectType.ACCOUNT.name(), internalCallContext); assertListenerStatus(); uuids.add(defZoo.getId()); result = tagDefinitionDao.getByIds(uuids, internalCallContext); assertEquals(result.size(), 3); // Add invoice tag and retry uuids.add(ControlTagType.AUTO_PAY_OFF.getId()); result = tagDefinitionDao.getByIds(uuids, internalCallContext); assertEquals(result.size(), 4); result = tagDefinitionDao.getTagDefinitions(true, internalCallContext); assertEquals(result.size(), 3 + SystemTags.get(true).size()); }
@Override public TagDefinitionModelDao inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception { final TagDefinitionSqlDao tagDefinitionSqlDao = entitySqlDaoWrapperFactory.become(TagDefinitionSqlDao.class); // Make sure the tag definition doesn't exist already final TagDefinitionModelDao existingDefinition = tagDefinitionSqlDao.getByName(definitionName, context); if (existingDefinition != null) { throw new TagDefinitionApiException(ErrorCode.TAG_DEFINITION_ALREADY_EXISTS, definitionName); } // Create it final TagDefinitionModelDao tagDefinition = new TagDefinitionModelDao(context.getCreatedDate(), definitionName, description, tagDefinitionObjectTypes); createAndRefresh(tagDefinitionSqlDao, tagDefinition, context); // Post an event to the bus final boolean isControlTag = TagModelDaoHelper.isControlTag(tagDefinition.getName()); final TagDefinitionInternalEvent tagDefinitionEvent; if (isControlTag) { tagDefinitionEvent = tagEventBuilder.newControlTagDefinitionCreationEvent(tagDefinition.getId(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); } else { tagDefinitionEvent = tagEventBuilder.newUserTagDefinitionCreationEvent(tagDefinition.getId(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); } try { bus.postFromTransaction(tagDefinitionEvent, entitySqlDaoWrapperFactory.getHandle().getConnection()); } catch (final PersistentBus.EventBusException e) { log.warn("Failed to post tag definition creation event for tagDefinitionId='{}'", tagDefinition.getId(), e); } return tagDefinition; } });
@Test(groups = "slow") public void testGetById() throws TagDefinitionApiException { // User Tag eventsListener.pushExpectedEvent(NextEvent.TAG_DEFINITION); final TagDefinitionModelDao defYo = tagDefinitionDao.create(UUID.randomUUID().toString().substring(0, 5), "defintion yo", ObjectType.ACCOUNT.name(),internalCallContext); assertListenerStatus(); final TagDefinitionModelDao resDefYo = tagDefinitionDao.getById(defYo.getId(), internalCallContext); assertEquals(defYo, resDefYo); // Control Tag try { tagDefinitionDao.create(ControlTagType.AUTO_INVOICING_OFF.name(), ControlTagType.AUTO_INVOICING_OFF.name(), ObjectType.ACCOUNT.name(),internalCallContext); Assert.fail("Should not be able to create a invoice tag"); } catch (TagDefinitionApiException ignore) { } final TagDefinitionModelDao resdef_AUTO_INVOICING_OFF = tagDefinitionDao.getById(ControlTagType.AUTO_INVOICING_OFF.getId(), internalCallContext); assertEquals(resdef_AUTO_INVOICING_OFF.getId(), ControlTagType.AUTO_INVOICING_OFF.getId()); assertEquals(resdef_AUTO_INVOICING_OFF.getName(), ControlTagType.AUTO_INVOICING_OFF.name()); assertEquals(resdef_AUTO_INVOICING_OFF.getDescription(), ControlTagType.AUTO_INVOICING_OFF.getDescription()); }
final Tag tag = new DescriptiveTag(createdTagDefinition.getId(), objectType, objectId, internalCallContext.getCreatedDate()); 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()); tagDao.deleteTag(objectId, objectType, createdTagDefinition.getId(), internalCallContext); assertListenerStatus(); tagDao.deleteTag(objectId, objectType, createdTagDefinition.getId(), internalCallContext); Assert.fail("Deleting same tag again should fail"); } catch (final TagApiException e) {
@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", ObjectType.ACCOUNT.name(), 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); }
@Test(groups = "slow") public void testGetByName() throws TagDefinitionApiException { // User Tag eventsListener.pushExpectedEvent(NextEvent.TAG_DEFINITION); final TagDefinitionModelDao defYo = tagDefinitionDao.create(UUID.randomUUID().toString().substring(0, 5), "defintion yo", ObjectType.ACCOUNT.name(),internalCallContext); assertListenerStatus(); final TagDefinitionModelDao resDefYo = tagDefinitionDao.getByName(defYo.getName(), internalCallContext); assertEquals(defYo, resDefYo); // Control Tag try { tagDefinitionDao.create(ControlTagType.AUTO_PAY_OFF.name(), ControlTagType.AUTO_INVOICING_OFF.name(), ObjectType.ACCOUNT.name(), internalCallContext); Assert.fail("Should not be able to create a invoice tag"); } catch (TagDefinitionApiException ignore) { } final TagDefinitionModelDao resdef_AUTO_PAY_OFF = tagDefinitionDao.getByName(ControlTagType.AUTO_PAY_OFF.name(), internalCallContext); assertEquals(resdef_AUTO_PAY_OFF.getId(), ControlTagType.AUTO_PAY_OFF.getId()); assertEquals(resdef_AUTO_PAY_OFF.getName(), ControlTagType.AUTO_PAY_OFF.name()); assertEquals(resdef_AUTO_PAY_OFF.getDescription(), ControlTagType.AUTO_PAY_OFF.getDescription()); }
@Test(groups = "slow") public void testTagDefinitionDeletionForUnusedDefinition() throws TagDefinitionApiException { final String definitionName = "TestTag1234"; eventsListener.pushExpectedEvent(NextEvent.TAG_DEFINITION); tagDefinitionDao.create(definitionName, "Some test tag", ObjectType.ACCOUNT.name(), internalCallContext); assertListenerStatus(); TagDefinitionModelDao tagDefinition = tagDefinitionDao.getByName(definitionName, internalCallContext); assertNotNull(tagDefinition); eventsListener.pushExpectedEvent(NextEvent.TAG_DEFINITION); tagDefinitionDao.deleteById(tagDefinition.getId(), internalCallContext); assertListenerStatus(); try { tagDefinitionDao.getByName(definitionName, internalCallContext); Assert.fail("Call should fail"); } catch (TagDefinitionApiException expected) { } }
@Test(groups = "slow") public void testDeleteTagBeforeDeleteTagDefinition() throws TagDefinitionApiException, TagApiException { final String definitionName = "TestTag1234567"; eventsListener.pushExpectedEvent(NextEvent.TAG_DEFINITION); tagDefinitionDao.create(definitionName, "Some test tag", ObjectType.ACCOUNT.name(), 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(); }
tagDefinitionDao.deleteById(foundTagDefinition.getId(), internalCallContext); assertListenerStatus();
@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()); }
final boolean isControlTag = ControlTagType.getTypeFromId(tagDefinition.getId()) != null; switch (changeType) { case INSERT:
@Override public boolean apply(final TagDefinitionModelDao input) { return input.getId().equals(tagDefinitionId); } });