@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 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; } } }
@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); }
@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); }
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 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 testInsertMultipleTags() throws TagApiException { final UUID objectId = UUID.randomUUID(); final ObjectType objectType = ObjectType.ACCOUNT; 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()); } }
tagDao.create(new TagModelDao(tag), internalCallContext); assertListenerStatus();
@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", 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 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()); }
@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(); }
@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 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; } } }
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 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 testInsertMultipleTags() throws TagApiException { final UUID objectId = UUID.randomUUID(); final ObjectType objectType = ObjectType.ACCOUNT; 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()); } }
@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 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()); }
@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(); }