@Test public void testAddLocalCommentToRebloggedRemoteRssPost() throws Exception { Transaction txn = new Transaction(null, false); MessageId wrappedPostId = new MessageId(getRandomId()); BdfList wrappedPostBody = BdfList.of("wrappedPostBody"); MessageId originalCommentId = new MessageId(getRandomId()); BdfList originalCommentBody = BdfList.of("originalCommentBody"); MessageId rewrappedPostId = new MessageId(getRandomId()); Message rewrappedPostMsg = new Message(rewrappedPostId, blog2.getId(), timestamp, getRandomBytes(MAX_MESSAGE_LENGTH)); BdfDictionary rewrappedPostMeta = BdfDictionary.of( new BdfEntry(KEY_TYPE, WRAPPED_POST.getInt()), new BdfEntry(KEY_RSS_FEED, true), new BdfEntry(KEY_ORIGINAL_MSG_ID, messageId), new BdfEntry(KEY_AUTHOR, rssAuthorList), new BdfEntry(KEY_TIMESTAMP, timestamp), new BdfEntry(KEY_TIME_RECEIVED, timeReceived) ); MessageId wrappedCommentId = new MessageId(getRandomId()); Message wrappedCommentMsg = new Message(wrappedCommentId, blog2.getId(), timestamp, getRandomBytes(MAX_MESSAGE_LENGTH)); BdfDictionary wrappedCommentMeta = BdfDictionary.of( new BdfEntry(KEY_TYPE, WRAPPED_COMMENT.getInt()), new BdfEntry(KEY_COMMENT, comment), new BdfEntry(KEY_PARENT_MSG_ID, rewrappedPostId), new BdfEntry(KEY_ORIGINAL_MSG_ID, originalCommentId), new BdfEntry(KEY_AUTHOR, authorList1), new BdfEntry(KEY_TIMESTAMP, timestamp), new BdfEntry(KEY_TIME_RECEIVED, timeReceived) ); String localComment = getRandomString(MAX_BLOG_COMMENT_LENGTH); MessageId localCommentId = new MessageId(getRandomId()); Message localCommentMsg = new Message(localCommentId, blog2.getId(), timestamp, getRandomBytes(MAX_MESSAGE_LENGTH)); BdfDictionary localCommentMeta = BdfDictionary.of( new BdfEntry(KEY_TYPE, COMMENT.getInt()), new BdfEntry(KEY_COMMENT, localComment), new BdfEntry(KEY_TIMESTAMP, timestamp), new BdfEntry(KEY_ORIGINAL_MSG_ID, localCommentId), new BdfEntry(KEY_ORIGINAL_PARENT_MSG_ID, originalCommentId), new BdfEntry(KEY_PARENT_MSG_ID, wrappedCommentId), new BdfEntry(KEY_AUTHOR, authorList2) ); context.checking(new Expectations() {{ oneOf(db).startTransaction(false); will(returnValue(txn)); oneOf(clientHelper).getMessageAsList(txn, wrappedPostId); will(returnValue(wrappedPostBody)); oneOf(blogPostFactory).rewrapWrappedPost(blog2.getId(), wrappedPostBody); will(returnValue(rewrappedPostMsg)); oneOf(clientHelper).toList(rssLocalAuthor); will(returnValue(rssAuthorList)); oneOf(clientHelper).addLocalMessage(txn, rewrappedPostMsg, rewrappedPostMeta, true); oneOf(clientHelper).getMessageAsList(txn, originalCommentId); will(returnValue(originalCommentBody)); oneOf(clientHelper).getMessageMetadataAsDictionary(txn, wrappedPostId); will(returnValue(rewrappedPostMeta)); oneOf(db).getGroup(txn, blog1.getId()); will(returnValue(blog1.getGroup())); oneOf(blogPostFactory).wrapComment(blog2.getId(), blog1.getGroup().getDescriptor(), timestamp, originalCommentBody, rewrappedPostId); will(returnValue(wrappedCommentMsg)); oneOf(clientHelper).toList(localAuthor1); will(returnValue(authorList1)); oneOf(clientHelper).addLocalMessage(txn, wrappedCommentMsg, wrappedCommentMeta, true); oneOf(blogPostFactory).createBlogComment(blog2.getId(), localAuthor2, localComment, originalCommentId, wrappedCommentId); will(returnValue(localCommentMsg)); oneOf(clientHelper).toList(localAuthor2); will(returnValue(authorList2)); oneOf(clientHelper).addLocalMessage(txn, localCommentMsg, localCommentMeta, true); oneOf(clientHelper).parseAndValidateAuthor(authorList2); will(returnValue(localAuthor2)); oneOf(identityManager).getAuthorStatus(txn, localAuthor2.getId()); will(returnValue(OURSELVES)); oneOf(clientHelper).getMessageMetadataAsDictionary(txn, wrappedCommentId); will(returnValue(wrappedCommentMeta)); oneOf(clientHelper).parseAndValidateAuthor(authorList1); will(returnValue(localAuthor1)); oneOf(identityManager).getAuthorStatus(txn, localAuthor1.getId()); will(returnValue(VERIFIED)); oneOf(clientHelper).getMessageMetadataAsDictionary(txn, rewrappedPostId); will(returnValue(rewrappedPostMeta)); oneOf(clientHelper).parseAndValidateAuthor(rssAuthorList); will(returnValue(rssLocalAuthor)); oneOf(db).commitTransaction(txn); oneOf(db).endTransaction(txn); }}); BlogPostHeader wrappedPostHeader = new BlogPostHeader(WRAPPED_POST, blog1.getId(), wrappedPostId, null, timestamp, timeReceived, rssLocalAuthor, NONE, true, true); BlogCommentHeader originalCommentHeader = new BlogCommentHeader(COMMENT, blog1.getId(), comment, wrappedPostHeader, originalCommentId, timestamp, timeReceived, localAuthor1, VERIFIED, true); blogManager.addLocalComment(localAuthor2, blog2.getId(), localComment, originalCommentHeader); context.assertIsSatisfied(); assertEquals(1, txn.getEvents().size()); assertTrue(txn.getEvents().get(0) instanceof BlogPostAddedEvent); BlogPostAddedEvent e = (BlogPostAddedEvent) txn.getEvents().get(0); assertEquals(blog2.getId(), e.getGroupId()); BlogPostHeader h = e.getHeader(); assertEquals(COMMENT, h.getType()); assertFalse(h.isRssFeed()); assertEquals(timestamp, h.getTimestamp()); assertEquals(timestamp, h.getTimeReceived()); assertEquals(localCommentId, h.getId()); assertEquals(blog2.getId(), h.getGroupId()); assertEquals(wrappedCommentId, h.getParentId()); assertEquals(OURSELVES, h.getAuthorStatus()); assertEquals(localAuthor2, h.getAuthor()); assertTrue(h instanceof BlogCommentHeader); BlogPostHeader h1 = ((BlogCommentHeader) h).getParent(); assertEquals(WRAPPED_COMMENT, h1.getType()); assertFalse(h1.isRssFeed()); assertEquals(timestamp, h1.getTimestamp()); assertEquals(timeReceived, h1.getTimeReceived()); assertEquals(wrappedCommentId, h1.getId()); assertEquals(blog2.getId(), h1.getGroupId()); assertEquals(rewrappedPostId, h1.getParentId()); assertEquals(VERIFIED, h1.getAuthorStatus()); assertEquals(localAuthor1, h1.getAuthor()); assertTrue(h1 instanceof BlogCommentHeader); BlogPostHeader h2 = ((BlogCommentHeader) h1).getParent(); assertEquals(WRAPPED_POST, h2.getType()); assertTrue(h2.isRssFeed()); assertEquals(timestamp, h2.getTimestamp()); assertEquals(timeReceived, h2.getTimeReceived()); assertEquals(rewrappedPostId, h2.getId()); assertEquals(blog2.getId(), h2.getGroupId()); assertNull(h2.getParentId()); assertEquals(NONE, h2.getAuthorStatus()); assertEquals(rssLocalAuthor, h2.getAuthor()); assertEquals(h2.getId(), ((BlogCommentHeader) h).getRootPost().getId()); assertEquals(h2.getId(), ((BlogCommentHeader) h1).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);  }