@Test public void testAddLocalCommentToLocalPost() throws Exception { Transaction txn = new Transaction(null, false); BdfDictionary postMeta = BdfDictionary.of( new BdfEntry(KEY_TYPE, POST.getInt()), new BdfEntry(KEY_RSS_FEED, false), new BdfEntry(KEY_ORIGINAL_MSG_ID, messageId), new BdfEntry(KEY_AUTHOR, authorList1), new BdfEntry(KEY_TIMESTAMP, timestamp), new BdfEntry(KEY_TIME_RECEIVED, timeReceived) ); MessageId commentId = new MessageId(getRandomId()); Message commentMsg = new Message(commentId, blog1.getId(), timestamp, getRandomBytes(MAX_MESSAGE_LENGTH)); BdfDictionary commentMeta = BdfDictionary.of( new BdfEntry(KEY_TYPE, COMMENT.getInt()), new BdfEntry(KEY_COMMENT, comment), new BdfEntry(KEY_TIMESTAMP, timestamp), new BdfEntry(KEY_ORIGINAL_MSG_ID, commentId), new BdfEntry(KEY_ORIGINAL_PARENT_MSG_ID, messageId), new BdfEntry(KEY_PARENT_MSG_ID, messageId), new BdfEntry(KEY_AUTHOR, authorList1) ); context.checking(new Expectations() {{ oneOf(db).startTransaction(false); will(returnValue(txn)); oneOf(blogPostFactory).createBlogComment(blog1.getId(), localAuthor1, comment, messageId, messageId); will(returnValue(commentMsg)); oneOf(clientHelper).toList(localAuthor1); will(returnValue(authorList1)); oneOf(clientHelper).addLocalMessage(txn, commentMsg, commentMeta, true); oneOf(clientHelper).parseAndValidateAuthor(authorList1); will(returnValue(localAuthor1)); oneOf(identityManager).getAuthorStatus(txn, localAuthor1.getId()); will(returnValue(OURSELVES)); oneOf(clientHelper).getMessageMetadataAsDictionary(txn, messageId); will(returnValue(postMeta)); oneOf(clientHelper).parseAndValidateAuthor(authorList1); will(returnValue(localAuthor1)); oneOf(identityManager).getAuthorStatus(txn, localAuthor1.getId()); will(returnValue(OURSELVES)); oneOf(db).commitTransaction(txn); oneOf(db).endTransaction(txn); }}); BlogPostHeader postHeader = new BlogPostHeader(POST, blog1.getId(), messageId, null, timestamp, timeReceived, localAuthor1, OURSELVES, false, true); blogManager.addLocalComment(localAuthor1, blog1.getId(), comment, postHeader); context.assertIsSatisfied(); assertEquals(1, txn.getEvents().size()); assertTrue(txn.getEvents().get(0) instanceof BlogPostAddedEvent); BlogPostAddedEvent e = (BlogPostAddedEvent) txn.getEvents().get(0); assertEquals(blog1.getId(), e.getGroupId()); BlogPostHeader h = e.getHeader(); assertEquals(COMMENT, h.getType()); assertFalse(h.isRssFeed()); assertEquals(timestamp, h.getTimestamp()); assertEquals(timestamp, h.getTimeReceived()); assertEquals(commentId, h.getId()); assertEquals(blog1.getId(), h.getGroupId()); assertEquals(messageId, h.getParentId()); assertEquals(OURSELVES, h.getAuthorStatus()); assertEquals(localAuthor1, h.getAuthor()); assertTrue(h instanceof BlogCommentHeader); BlogPostHeader h1 = ((BlogCommentHeader) h).getParent(); assertEquals(POST, h1.getType()); assertFalse(h1.isRssFeed()); assertEquals(timestamp, h1.getTimestamp()); assertEquals(timeReceived, h1.getTimeReceived()); assertEquals(messageId, h1.getId()); assertEquals(blog1.getId(), h.getGroupId()); assertNull(h1.getParentId()); assertEquals(OURSELVES, h1.getAuthorStatus()); assertEquals(localAuthor1, h1.getAuthor()); assertEquals(h1.getId(), ((BlogCommentHeader) h).getRootPost().getId()); }
@Override public void addLocalComment(LocalAuthor author, GroupId groupId, @Nullable String comment, BlogPostHeader parentHeader) throws DbException { MessageType type = parentHeader.getType(); if (type != POST && type != COMMENT) throw new IllegalArgumentException("Comment on unknown type!"); Transaction txn = db.startTransaction(false); try { MessageId parentOriginalId = getOriginalMessageId(txn, parentHeader); MessageId parentCurrentId = wrapMessage(txn, groupId, parentHeader, parentOriginalId); Message message = blogPostFactory.createBlogComment(groupId, author, comment, parentOriginalId, parentCurrentId); BdfDictionary meta = new BdfDictionary(); meta.put(KEY_TYPE, COMMENT.getInt()); if (comment != null) meta.put(KEY_COMMENT, comment); meta.put(KEY_TIMESTAMP, message.getTimestamp()); meta.put(KEY_ORIGINAL_MSG_ID, message.getId()); meta.put(KEY_ORIGINAL_PARENT_MSG_ID, parentOriginalId); meta.put(KEY_PARENT_MSG_ID, parentCurrentId); meta.put(KEY_AUTHOR, clientHelper.toList(author)); clientHelper.addLocalMessage(txn, message, meta, true); BlogPostHeader h = getPostHeaderFromMetadata(txn, groupId, message.getId(), meta); BlogPostAddedEvent event = new BlogPostAddedEvent(groupId, h, true); txn.attach(event); db.commitTransaction(txn); } catch (FormatException e) { throw new DbException(e); } catch (GeneralSecurityException e) { throw new IllegalArgumentException("Invalid key of author", e); } finally { db.endTransaction(txn); } }
BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
		ContactHook, Client { @Override public void addLocalComment(LocalAuthor author, GroupId groupId, @Nullable String comment, BlogPostHeader parentHeader) throws DbException { MessageType type = parentHeader.getType(); if (type != POST && type != COMMENT) throw new IllegalArgumentException("Comment on unknown type!"); Transaction txn = db.startTransaction(false); try { MessageId parentOriginalId = getOriginalMessageId(txn, parentHeader); MessageId parentCurrentId = wrapMessage(txn, groupId, parentHeader, parentOriginalId); Message message = blogPostFactory.createBlogComment(groupId, author, comment, parentOriginalId, parentCurrentId); BdfDictionary meta = new BdfDictionary(); meta.put(KEY_TYPE, COMMENT.getInt()); if (comment != null) meta.put(KEY_COMMENT, comment); meta.put(KEY_TIMESTAMP, message.getTimestamp()); meta.put(KEY_ORIGINAL_MSG_ID, message.getId()); meta.put(KEY_ORIGINAL_PARENT_MSG_ID, parentOriginalId); meta.put(KEY_PARENT_MSG_ID, parentCurrentId); meta.put(KEY_AUTHOR, clientHelper.toList(author)); clientHelper.addLocalMessage(txn, message, meta, true); BlogPostHeader h = getPostHeaderFromMetadata(txn, groupId, message.getId(), meta); BlogPostAddedEvent event = new BlogPostAddedEvent(groupId, h, true); txn.attach(event); db.commitTransaction(txn); } catch (FormatException e) { throw new DbException(e); } catch (GeneralSecurityException e) { throw new IllegalArgumentException("Invalid key of author", e); } finally { db.endTransaction(txn); } } }
BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
		ContactHook, Client { @Override public void addLocalComment(LocalAuthor author, GroupId groupId, @Nullable String comment, BlogPostHeader parentHeader) throws DbException { MessageType type = parentHeader.getType(); if (type != POST && type != COMMENT) throw new IllegalArgumentException("Comment on unknown type!"); Transaction txn = db.startTransaction(false); try { MessageId parentOriginalId = getOriginalMessageId(txn, parentHeader); MessageId parentCurrentId = wrapMessage(txn, groupId, parentHeader, parentOriginalId); Message message = blogPostFactory.createBlogComment(groupId, author, comment, parentOriginalId, parentCurrentId); BdfDictionary meta = new BdfDictionary(); meta.put(KEY_TYPE, COMMENT.getInt()); if (comment != null) meta.put(KEY_COMMENT, comment); meta.put(KEY_TIMESTAMP, message.getTimestamp()); meta.put(KEY_ORIGINAL_MSG_ID, message.getId()); meta.put(KEY_ORIGINAL_PARENT_MSG_ID, parentOriginalId); meta.put(KEY_PARENT_MSG_ID, parentCurrentId); meta.put(KEY_AUTHOR, clientHelper.toList(author)); clientHelper.addLocalMessage(txn, message, meta, true); BlogPostHeader h = getPostHeaderFromMetadata(txn, groupId, message.getId(), meta); BlogPostAddedEvent event = new BlogPostAddedEvent(groupId, h, true); txn.attach(event); db.commitTransaction(txn); } catch (FormatException e) { throw new DbException(e); } catch (GeneralSecurityException e) { throw new IllegalArgumentException("Invalid key of author", e); } finally { db.endTransaction(txn); } } @Inject  BlogManagerImpl(DatabaseComponent db, IdentityManager identityManager,
			ClientHelper clientHelper, MetadataParser metadataParser,
			BlogFactory blogFactory, BlogPostFactory blogPostFactory); }
BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
		ContactHook, Client { @Override public void addLocalComment(LocalAuthor author, GroupId groupId, @Nullable String comment, BlogPostHeader parentHeader) throws DbException { MessageType type = parentHeader.getType(); if (type != POST && type != COMMENT) throw new IllegalArgumentException("Comment on unknown type!"); Transaction txn = db.startTransaction(false); try { MessageId parentOriginalId = getOriginalMessageId(txn, parentHeader); MessageId parentCurrentId = wrapMessage(txn, groupId, parentHeader, parentOriginalId); Message message = blogPostFactory.createBlogComment(groupId, author, comment, parentOriginalId, parentCurrentId); BdfDictionary meta = new BdfDictionary(); meta.put(KEY_TYPE, COMMENT.getInt()); if (comment != null) meta.put(KEY_COMMENT, comment); meta.put(KEY_TIMESTAMP, message.getTimestamp()); meta.put(KEY_ORIGINAL_MSG_ID, message.getId()); meta.put(KEY_ORIGINAL_PARENT_MSG_ID, parentOriginalId); meta.put(KEY_PARENT_MSG_ID, parentCurrentId); meta.put(KEY_AUTHOR, clientHelper.toList(author)); clientHelper.addLocalMessage(txn, message, meta, true); BlogPostHeader h = getPostHeaderFromMetadata(txn, groupId, message.getId(), meta); BlogPostAddedEvent event = new BlogPostAddedEvent(groupId, h, true); txn.attach(event); db.commitTransaction(txn); } catch (FormatException e) { throw new DbException(e); } catch (GeneralSecurityException e) { throw new IllegalArgumentException("Invalid key of author", e); } finally { db.endTransaction(txn); } } @Inject  BlogManagerImpl(DatabaseComponent db, IdentityManager identityManager,
			ClientHelper clientHelper, MetadataParser metadataParser,
			BlogFactory blogFactory, BlogPostFactory blogPostFactory); @Override void createLocalState(Transaction txn); @Override void addingContact(Transaction txn, Contact c); @Override void removingContact(Transaction txn, Contact c); @Override void addBlog(Blog b); @Override void addBlog(Transaction txn, Blog b); @Override boolean canBeRemoved(Blog b); @Override void removeBlog(Blog b); @Override void removeBlog(Transaction txn, Blog b); @Override void addLocalPost(BlogPost p); @Override void addLocalPost(Transaction txn, BlogPost p); @Override void addLocalComment(LocalAuthor author, GroupId groupId,
			@Nullable String comment, BlogPostHeader parentHeader); @Override Blog getBlog(GroupId g); @Override Blog getBlog(Transaction txn, GroupId g); @Override Collection<Blog> getBlogs(LocalAuthor localAuthor); @Override Blog getPersonalBlog(Author author); @Override Collection<Blog> getBlogs(); @Override BlogPostHeader getPostHeader(GroupId g, MessageId m); @Override String getPostBody(MessageId m); @Override Collection<BlogPostHeader> getPostHeaders(GroupId g); @Override void setReadFlag(MessageId m, boolean read); @Override void registerRemoveBlogHook(RemoveBlogHook hook); }
BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
		ContactHook, Client { @Override public void addLocalComment(LocalAuthor author, GroupId groupId, @Nullable String comment, BlogPostHeader parentHeader) throws DbException { MessageType type = parentHeader.getType(); if (type != POST && type != COMMENT) throw new IllegalArgumentException("Comment on unknown type!"); Transaction txn = db.startTransaction(false); try { MessageId parentOriginalId = getOriginalMessageId(txn, parentHeader); MessageId parentCurrentId = wrapMessage(txn, groupId, parentHeader, parentOriginalId); Message message = blogPostFactory.createBlogComment(groupId, author, comment, parentOriginalId, parentCurrentId); BdfDictionary meta = new BdfDictionary(); meta.put(KEY_TYPE, COMMENT.getInt()); if (comment != null) meta.put(KEY_COMMENT, comment); meta.put(KEY_TIMESTAMP, message.getTimestamp()); meta.put(KEY_ORIGINAL_MSG_ID, message.getId()); meta.put(KEY_ORIGINAL_PARENT_MSG_ID, parentOriginalId); meta.put(KEY_PARENT_MSG_ID, parentCurrentId); meta.put(KEY_AUTHOR, clientHelper.toList(author)); clientHelper.addLocalMessage(txn, message, meta, true); BlogPostHeader h = getPostHeaderFromMetadata(txn, groupId, message.getId(), meta); BlogPostAddedEvent event = new BlogPostAddedEvent(groupId, h, true); txn.attach(event); db.commitTransaction(txn); } catch (FormatException e) { throw new DbException(e); } catch (GeneralSecurityException e) { throw new IllegalArgumentException("Invalid key of author", e); } finally { db.endTransaction(txn); } } @Inject  BlogManagerImpl(DatabaseComponent db, IdentityManager identityManager,
			ClientHelper clientHelper, MetadataParser metadataParser,
			BlogFactory blogFactory, BlogPostFactory blogPostFactory); @Override void createLocalState(Transaction txn); @Override void addingContact(Transaction txn, Contact c); @Override void removingContact(Transaction txn, Contact c); @Override void addBlog(Blog b); @Override void addBlog(Transaction txn, Blog b); @Override boolean canBeRemoved(Blog b); @Override void removeBlog(Blog b); @Override void removeBlog(Transaction txn, Blog b); @Override void addLocalPost(BlogPost p); @Override void addLocalPost(Transaction txn, BlogPost p); @Override void addLocalComment(LocalAuthor author, GroupId groupId,
			@Nullable String comment, BlogPostHeader parentHeader); @Override Blog getBlog(GroupId g); @Override Blog getBlog(Transaction txn, GroupId g); @Override Collection<Blog> getBlogs(LocalAuthor localAuthor); @Override Blog getPersonalBlog(Author author); @Override Collection<Blog> getBlogs(); @Override BlogPostHeader getPostHeader(GroupId g, MessageId m); @Override String getPostBody(MessageId m); @Override Collection<BlogPostHeader> getPostHeaders(GroupId g); @Override void setReadFlag(MessageId m, boolean read); @Override void registerRemoveBlogHook(RemoveBlogHook hook);  }