@Override protected TagDefinitionApiException generateAlreadyExistsException(final TagDefinitionModelDao entity, final InternalCallContext context) { return new TagDefinitionApiException(ErrorCode.TAG_DEFINITION_ALREADY_EXISTS, entity.getId()); } }
public DefaultTagDefinition(final TagDefinitionModelDao tagDefinitionModelDao, final boolean isControlTag) { this(tagDefinitionModelDao.getId(), tagDefinitionModelDao.getName(), tagDefinitionModelDao.getDescription(), isControlTag); }
@Override public TagDefinitionModelDao create(final String definitionName, final String description, final InternalCallContext context) throws TagDefinitionApiException { final TagDefinitionModelDao tag = new TagDefinitionModelDao(null, definitionName, description); tags.put(tag.getId().toString(), tag); return tag; }
protected void postBusEventFromTransaction(final TagDefinitionModelDao tagDefinition, final TagDefinitionModelDao savedTagDefinition, final ChangeType changeType, final EntitySqlDaoWrapperFactory<EntitySqlDao> 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.getSqlDao()); } catch (PersistentBus.EventBusException e) { log.warn("Failed to post tag definition event for tag " + tagDefinition.getId().toString(), e); } }
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 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", internalCallContext); assertListenerStatus(); uuids.add(defYo.getId()); eventsListener.pushExpectedEvent(NextEvent.TAG_DEFINITION); final TagDefinitionModelDao defBah = tagDefinitionDao.create(UUID.randomUUID().toString().substring(0, 5), "defintion bah", internalCallContext); assertListenerStatus(); uuids.add(defBah.getId()); eventsListener.pushExpectedEvent(NextEvent.TAG_DEFINITION); final TagDefinitionModelDao defZoo = tagDefinitionDao.create(UUID.randomUUID().toString().substring(0, 5), "defintion zoo", internalCallContext); assertListenerStatus(); uuids.add(defZoo.getId()); result = tagDefinitionDao.getByIds(uuids, internalCallContext); assertEquals(result.size(), 3); // Add control tag and retry uuids.add(ControlTagType.AUTO_PAY_OFF.getId()); result = tagDefinitionDao.getByIds(uuids, internalCallContext); assertEquals(result.size(), 4); result = tagDefinitionDao.getTagDefinitions(internalCallContext); assertEquals(result.size(), 3 + ControlTagType.values().length); }
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();
@Override public TagDefinitionModelDao inTransaction(final EntitySqlDaoWrapperFactory<EntitySqlDao> 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); tagDefinitionSqlDao.create(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.getSqlDao()); } catch (PersistentBus.EventBusException e) { log.warn("Failed to post tag definition creation event for tag " + 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", 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(), internalCallContext); Assert.fail("Should not be able to create a control 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()); }
@Test(groups = "slow") public void testTagDefinitionDeletionForUnusedDefinition() throws TagDefinitionApiException { final String definitionName = "TestTag1234"; eventsListener.pushExpectedEvent(NextEvent.TAG_DEFINITION); tagDefinitionDao.create(definitionName, "Some test tag", internalCallContext); assertListenerStatus(); TagDefinitionModelDao tagDefinition = tagDefinitionDao.getByName(definitionName, internalCallContext); assertNotNull(tagDefinition); eventsListener.pushExpectedEvent(NextEvent.TAG_DEFINITION); tagDefinitionDao.deleteById(tagDefinition.getId(), internalCallContext); assertListenerStatus(); tagDefinition = tagDefinitionDao.getByName(definitionName, internalCallContext); assertNull(tagDefinition); }
@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); }
tagDefinitionDao.deleteById(foundTagDefinition.getId(), internalCallContext); assertListenerStatus();
@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", 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(), internalCallContext); Assert.fail("Should not be able to create a control 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 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 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()); }
final boolean isControlTag = ControlTagType.getTypeFromId(tagDefinition.getId()) != null; switch (changeType) { case INSERT: