use of com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.CommentNotificationContainer in project binnavi by google.
the class PostgreSQLNotificationParserTest method testCommentTableNotificationParser1.
@Test
public void testCommentTableNotificationParser1() {
CommentManager.get(provider).initializeGlobalInstructionComment(instruction, Lists.newArrayList(comment1, comment2, comment3));
final String COMMENTS_TABLE_NOTIFICATION_1 = "bn_comments UPDATE 3333 2222 " + CommonTestObjects.TEST_USER_3.getUserId() + " AAA";
final CommentNotification result = PostgreSQLCommentNotificationParser.processCommentNotification(new MockPGNotification("comment_changes", COMMENTS_TABLE_NOTIFICATION_1), provider);
assertNotNull(result);
final CommentNotificationContainer container = (CommentNotificationContainer) result;
final IComment comment = container.getCurrentComment();
assertEquals(comment3, comment);
final IComment testComment = new CComment(3333, CommonTestObjects.TEST_USER_3, comment2, "AAA");
assertEquals(testComment, container.getNewComment());
}
use of com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.CommentNotificationContainer in project binnavi by google.
the class PostgreSQLCommentNotificationParser method processCommentNotification.
/**
* Parses a {@link PGNotification} notification from the database back end for comment table
* changes. These changes can not directly be mapped to any of the commentable objects as the
* relation to which commentable object they belong is not in the notification message. This
* notification is generated for the following situations for any commentable object:
*
* <pre>
*
*Delete a comment:
*
*[1] Comment 1 [1] Comment 1
*[2] Comment 2 ->
*[3] Comment 3 [3] Comment 3
*
*Edit a comment:
*
*[1] Comment 1 [1] Comment 1
*[2] Comment 2 -> [2] Edited Comment 2
*[3] Comment 3 [3] Comment 3
*
*</pre>
*
* @param notification The {@link PGNotification} from the PostgreSQL database server.
* @param provider The {@link SQLProvider} which is used to communicate with the database.
*/
static CommentNotification processCommentNotification(final PGNotification notification, final SQLProvider provider) {
final Matcher matcher = COMMENTS_PATTERN.matcher(notification.getParameter());
if (!matcher.find()) {
return null;
}
Integer commentId = null;
try {
commentId = Integer.parseInt(matcher.group(3));
} catch (final NumberFormatException exception) {
throw new IllegalStateException(exception);
}
final IComment comment = CommentManager.get(provider).getCommentById(commentId);
if (comment == null) {
return null;
}
final String databaseOperation = matcher.group(2);
Integer parentId = null;
try {
parentId = matcher.group(4).equalsIgnoreCase("null") ? null : Integer.parseInt(matcher.group(4));
} catch (final NumberFormatException exception) {
throw new IllegalStateException(exception);
}
// parent.
if (databaseOperation.equals("DELETE")) {
if (((parentId == null) && (comment.getParent() != null)) || ((parentId != null) && (comment.getParent() != null) && (!parentId.equals(comment.getParent().getId())))) {
final Integer localCommentParentId = parentId;
final Integer notificationCommentParentId = comment.getParent() != null ? comment.getParent().getId() : null;
throw new IllegalStateException("IE02521: The parent comment of the localy stored comment: " + localCommentParentId + " is not equal to the " + "notification comments parent comment: " + notificationCommentParentId);
}
}
final String commentContent = matcher.group(9);
if (!commentContent.equals(comment.getComment()) && databaseOperation.equals("DELETE")) {
throw new IllegalStateException("IE02522: The local comments comment: " + comment.getComment() + "is not equal to the notification comments content: " + commentContent);
}
Integer commentUserId = null;
try {
commentUserId = Integer.parseInt(matcher.group(7));
} catch (final NumberFormatException exception) {
throw new IllegalStateException(exception);
}
if (!commentUserId.equals(comment.getUser().getUserId())) {
throw new IllegalStateException("IE02523: The user of the localy stored comment: " + commentUserId + " is not equal to the " + "notifications comments user: " + comment.getUser().getUserId());
}
final IComment parentComment = CommentManager.get(provider).getCommentById(parentId);
final IComment newComment = new CComment(comment.getId(), comment.getUser(), parentComment, commentContent);
final CommentOperation operation = databaseOperation.equalsIgnoreCase("UPDATE") ? CommentOperation.EDIT : CommentOperation.DELETE;
return new CommentNotificationContainer(comment, newComment, operation);
}
Aggregations