@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); tagDao.create(tag, context); }
@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 Tag apply(final TagModelDao input) { return TagModelDaoHelper.isControlTag(input.getTagDefinitionId()) ? new DefaultControlTag(ControlTagType.getTypeFromId(input.getTagDefinitionId()), objectType, objectId, input.getCreatedDate()) : new DescriptiveTag(input.getTagDefinitionId(), objectType, objectId, input.getCreatedDate()); } }));
@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()); }
@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 testCacheRecordId() throws Exception { this.transactionalSqlDao = new EntitySqlDaoTransactionalJdbiWrapper(dbi, clock, controlCacheDispatcher, nonEntityDao); final TagModelDao tag = new TagModelDao(clock.getUTCNow(), UUID.randomUUID(), UUID.randomUUID(), ObjectType.TAG); // Verify we start with nothing in the cache Assert.assertEquals(getCacheSize(), 0); insertTag(tag); // Verify we still have nothing after insert in the cache Assert.assertEquals(getCacheSize(), 0); final Long tagRecordId = getTagRecordId(tag.getId()); // Verify we now have something in the cache Assert.assertEquals(getCacheSize(), 1); final Long recordIdFromCache = retrieveRecordIdFromCache(tag.getId()); Assert.assertNotNull(recordIdFromCache); Assert.assertEquals(recordIdFromCache, new Long(1)); Assert.assertEquals(tagRecordId, new Long(1)); Assert.assertEquals(getCacheSize(), 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>()); } tagStore.get(tag.getObjectId()).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); } }
@Override protected TagApiException generateAlreadyExistsException(final TagModelDao entity, final InternalCallContext context) { // Print the tag details, not the id here, as we throw this exception when checking if a tag already exists // by using the isSame(TagModelDao) method (see above) return new TagApiException(ErrorCode.TAG_ALREADY_EXISTS, entity.toString()); }
@Override public boolean equals(final Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } if (!super.equals(o)) { return false; } final TagModelDao that = (TagModelDao) o; return isSame(that); }
@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()); }
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());
@Override public boolean apply(final TagModelDao input) { return objectType.equals(input.getObjectType()); } }));
final TagDefinitionModelDao tagDefinition = getTagDefinitionFromTransaction(tag.getTagDefinitionId(), entitySqlDaoWrapperFactory, context); final boolean isControlTag = ControlTagType.getTypeFromId(tagDefinition.getId()) != null; switch (changeType) { case INSERT: tagEvent = (isControlTag) ? tagEventBuilder.newControlTagCreationEvent(tag.getId(), tag.getObjectId(), tag.getObjectType(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()) : tagEventBuilder.newUserTagCreationEvent(tag.getId(), tag.getObjectId(), tag.getObjectType(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); break; case DELETE: tagEvent = (isControlTag) ? tagEventBuilder.newControlTagDeletionEvent(tag.getId(), tag.getObjectId(), tag.getObjectType(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()) : tagEventBuilder.newUserTagDeletionEvent(tag.getId(), tag.getObjectId(), tag.getObjectType(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); break; bus.postFromTransaction(tagEvent, entitySqlDaoWrapperFactory.getSqlDao()); } catch (PersistentBus.EventBusException e) { log.warn("Failed to post tag event for tag " + tag.getId().toString(), e);
@Override public void addTag(final UUID objectId, final ObjectType objectType, final UUID tagDefinitionId, final CallContext context) throws TagApiException { 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") 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); }