CommentsIT: Add coverage of range values and file comments

Change If6c4cbce introduced a null pointer exception when a draft
change comment includes a range, and we didn't notice because ranges
are not used in the tests. This null pointer exception was already
fixed by change I0e3c734d. The test cases now fail when that change
is not applied.

Change I77772b6f fixed another null pointer exception when posting a
file level comment (i.e. the optional line member was null).

Extend CommentsIT to include test coverage of comment ranges and file
comments.

Fix another null pointer exception when posting file comments, that
was exposed by the newly added tests.

Change-Id: I517cc24ff3bbabdcbc14d743b8788de33b9c0241
This commit is contained in:
David Pursehouse
2015-01-10 21:30:09 +09:00
parent b28b27b834
commit d13b7f9b89
2 changed files with 96 additions and 61 deletions

View File

@@ -22,6 +22,7 @@ import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.PushOneCommit; import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.acceptance.RestResponse; import com.google.gerrit.acceptance.RestResponse;
import com.google.gerrit.extensions.api.changes.ReviewInput; import com.google.gerrit.extensions.api.changes.ReviewInput;
import com.google.gerrit.extensions.client.Comment;
import com.google.gerrit.extensions.client.Side; import com.google.gerrit.extensions.client.Side;
import com.google.gerrit.extensions.common.CommentInfo; import com.google.gerrit.extensions.common.CommentInfo;
import com.google.gerrit.server.notedb.NotesMigration; import com.google.gerrit.server.notedb.NotesMigration;
@@ -44,24 +45,29 @@ public class CommentsIT extends AbstractDaemonTest {
return NotesMigration.allEnabledConfig(); return NotesMigration.allEnabledConfig();
} }
private final Integer lines[] = {0, 1};
@Test @Test
public void createDraft() throws Exception { public void createDraft() throws Exception {
for (Integer line : lines) {
PushOneCommit.Result r = createChange(); PushOneCommit.Result r = createChange();
String changeId = r.getChangeId(); String changeId = r.getChangeId();
String revId = r.getCommit().getName(); String revId = r.getCommit().getName();
ReviewInput.CommentInput comment = newCommentInfo( ReviewInput.CommentInput comment = newCommentInfo(
"file1", Side.REVISION, 1, "comment 1"); "file1", Side.REVISION, line, "comment 1");
addDraft(changeId, revId, comment); addDraft(changeId, revId, comment);
Map<String, List<CommentInfo>> result = getDraftComments(changeId, revId); Map<String, List<CommentInfo>> result = getDraftComments(changeId, revId);
assertThat(result).hasSize(1); assertThat(result).hasSize(1);
CommentInfo actual = Iterables.getOnlyElement(result.get(comment.path)); CommentInfo actual = Iterables.getOnlyElement(result.get(comment.path));
assertCommentInfo(comment, actual); assertCommentInfo(comment, actual);
} }
}
@Test @Test
public void postComment() throws Exception { public void postComment() throws Exception {
for (Integer line : lines) {
String file = "file"; String file = "file";
String contents = "contents"; String contents = "contents " + line;
PushOneCommit push = pushFactory.create(db, admin.getIdent(), PushOneCommit push = pushFactory.create(db, admin.getIdent(),
"first subject", file, contents); "first subject", file, contents);
PushOneCommit.Result r = push.to(git, "refs/for/master"); PushOneCommit.Result r = push.to(git, "refs/for/master");
@@ -69,7 +75,7 @@ public class CommentsIT extends AbstractDaemonTest {
String revId = r.getCommit().getName(); String revId = r.getCommit().getName();
ReviewInput input = new ReviewInput(); ReviewInput input = new ReviewInput();
ReviewInput.CommentInput comment = newCommentInfo( ReviewInput.CommentInput comment = newCommentInfo(
file, Side.REVISION, 1, "comment 1"); file, Side.REVISION, line, "comment 1");
input.comments = new HashMap<>(); input.comments = new HashMap<>();
input.comments.put(comment.path, Lists.newArrayList(comment)); input.comments.put(comment.path, Lists.newArrayList(comment));
revision(r).review(input); revision(r).review(input);
@@ -78,14 +84,16 @@ public class CommentsIT extends AbstractDaemonTest {
CommentInfo actual = Iterables.getOnlyElement(result.get(comment.path)); CommentInfo actual = Iterables.getOnlyElement(result.get(comment.path));
assertCommentInfo(comment, actual); assertCommentInfo(comment, actual);
} }
}
@Test @Test
public void putDraft() throws Exception { public void putDraft() throws Exception {
for (Integer line : lines) {
PushOneCommit.Result r = createChange(); PushOneCommit.Result r = createChange();
String changeId = r.getChangeId(); String changeId = r.getChangeId();
String revId = r.getCommit().getName(); String revId = r.getCommit().getName();
ReviewInput.CommentInput comment = newCommentInfo( ReviewInput.CommentInput comment = newCommentInfo(
"file1", Side.REVISION, 1, "comment 1"); "file1", Side.REVISION, line, "comment 1");
addDraft(changeId, revId, comment); addDraft(changeId, revId, comment);
Map<String, List<CommentInfo>> result = getDraftComments(changeId, revId); Map<String, List<CommentInfo>> result = getDraftComments(changeId, revId);
CommentInfo actual = Iterables.getOnlyElement(result.get(comment.path)); CommentInfo actual = Iterables.getOnlyElement(result.get(comment.path));
@@ -97,31 +105,36 @@ public class CommentsIT extends AbstractDaemonTest {
actual = Iterables.getOnlyElement(result.get(comment.path)); actual = Iterables.getOnlyElement(result.get(comment.path));
assertCommentInfo(comment, actual); assertCommentInfo(comment, actual);
} }
}
@Test @Test
public void getDraft() throws Exception { public void getDraft() throws Exception {
for (Integer line : lines) {
PushOneCommit.Result r = createChange(); PushOneCommit.Result r = createChange();
String changeId = r.getChangeId(); String changeId = r.getChangeId();
String revId = r.getCommit().getName(); String revId = r.getCommit().getName();
ReviewInput.CommentInput comment = newCommentInfo( ReviewInput.CommentInput comment = newCommentInfo(
"file1", Side.REVISION, 1, "comment 1"); "file1", Side.REVISION, line, "comment 1");
CommentInfo returned = addDraft(changeId, revId, comment); CommentInfo returned = addDraft(changeId, revId, comment);
CommentInfo actual = getDraftComment(changeId, revId, returned.id); CommentInfo actual = getDraftComment(changeId, revId, returned.id);
assertCommentInfo(comment, actual); assertCommentInfo(comment, actual);
} }
}
@Test @Test
public void deleteDraft() throws Exception { public void deleteDraft() throws Exception {
for (Integer line : lines) {
PushOneCommit.Result r = createChange(); PushOneCommit.Result r = createChange();
String changeId = r.getChangeId(); String changeId = r.getChangeId();
String revId = r.getCommit().getName(); String revId = r.getCommit().getName();
ReviewInput.CommentInput comment = newCommentInfo( ReviewInput.CommentInput comment = newCommentInfo(
"file1", Side.REVISION, 1, "comment 1"); "file1", Side.REVISION, line, "comment 1");
CommentInfo returned = addDraft(changeId, revId, comment); CommentInfo returned = addDraft(changeId, revId, comment);
deleteDraft(changeId, revId, returned.id); deleteDraft(changeId, revId, returned.id);
Map<String, List<CommentInfo>> drafts = getDraftComments(changeId, revId); Map<String, List<CommentInfo>> drafts = getDraftComments(changeId, revId);
assertThat(drafts).isEmpty(); assertThat(drafts).isEmpty();
} }
}
private CommentInfo addDraft(String changeId, String revId, private CommentInfo addDraft(String changeId, String revId,
ReviewInput.CommentInput c) throws IOException { ReviewInput.CommentInput c) throws IOException {
@@ -176,18 +189,40 @@ public class CommentsIT extends AbstractDaemonTest {
assertThat(actual.line).isEqualTo(expected.line); assertThat(actual.line).isEqualTo(expected.line);
assertThat(actual.message).isEqualTo(expected.message); assertThat(actual.message).isEqualTo(expected.message);
assertThat(actual.inReplyTo).isEqualTo(expected.inReplyTo); assertThat(actual.inReplyTo).isEqualTo(expected.inReplyTo);
assertCommentRange(expected.range, actual.range);
if (actual.side == null) { if (actual.side == null) {
assertThat(Side.REVISION).isEqualTo(expected.side); assertThat(Side.REVISION).isEqualTo(expected.side);
} }
} }
private static void assertCommentRange(Comment.Range expected,
Comment.Range actual) {
if (expected == null) {
assertThat(actual).isNull();
} else {
assertThat(actual).isNotNull();
assertThat(actual.startLine).isEqualTo(expected.startLine);
assertThat(actual.startCharacter).isEqualTo(expected.startCharacter);
assertThat(actual.endLine).isEqualTo(expected.endLine);
assertThat(actual.endCharacter).isEqualTo(expected.endCharacter);
}
}
private ReviewInput.CommentInput newCommentInfo(String path, private ReviewInput.CommentInput newCommentInfo(String path,
Side side, int line, String message) { Side side, int line, String message) {
ReviewInput.CommentInput input = new ReviewInput.CommentInput(); ReviewInput.CommentInput input = new ReviewInput.CommentInput();
input.path = path; input.path = path;
input.side = side; input.side = side;
input.line = line; input.line = line != 0 ? line : null;
input.message = message; input.message = message;
if (line != 0) {
Comment.Range range = new Comment.Range();
range.startLine = 1;
range.startCharacter = 1;
range.endLine = 1;
range.endCharacter = 5;
input.range = range;
}
return input; return input;
} }
} }

View File

@@ -358,7 +358,7 @@ public class PostReview implements RestModifyView<RevisionResource, ReviewInput>
new PatchLineComment.Key( new PatchLineComment.Key(
new Patch.Key(rsrc.getPatchSet().getId(), path), new Patch.Key(rsrc.getPatchSet().getId(), path),
ChangeUtil.messageUUID(db.get())), ChangeUtil.messageUUID(db.get())),
c.line, c.line != null ? c.line : 0,
rsrc.getAccountId(), rsrc.getAccountId(),
parent, timestamp); parent, timestamp);
} else if (parent != null) { } else if (parent != null) {