Add draft comment creation to test API of changes

The API for draft comments mimics the one for published comments. Which
type of comment should be created is decided at the start of the fluent
API by either using newDraftComment() or newComment().

The tests for draft comments are mostly copied from the ones for
published comments albeit with some additions. Even though a lot of
the code is shared between draft and published comments, having so many
tests proved to be necessary as otherwise we wouldn't have noticed the
different code required to correctly set a tag for draft comments.

Change-Id: I413143a2fc5502efbf37406342ad42b7f6d95490
This commit is contained in:
Alice Kober-Sotzek
2020-08-13 19:00:38 +02:00
parent a7a4f51879
commit 106961964e
4 changed files with 403 additions and 10 deletions

View File

@@ -27,8 +27,8 @@ public interface PerPatchsetOperations {
TestPatchset get();
/**
* Starts the fluent chain to create a new comment. The returned builder can be used to specify
* the attributes of the new comment. To create the comment for real, {@link
* Starts the fluent chain to create a new, published comment. The returned builder can be used to
* specify the attributes of the comment. To create the comment for real, {@link
* TestCommentCreation.Builder#create()} must be called.
*
* <p>Example:
@@ -37,6 +37,7 @@ public interface PerPatchsetOperations {
* String createdCommentUuid = changeOperations
* .change(changeId)
* .currentPatchset()
* .newComment()
* .onLine(2)
* .ofFile("file1")
* .create();
@@ -45,4 +46,25 @@ public interface PerPatchsetOperations {
* @return builder to create a new comment
*/
TestCommentCreation.Builder newComment();
/**
* Starts the fluent chain to create a new draft comment. The returned builder can be used to
* specify the attributes of the draft comment. To create the draft comment for real, {@link
* TestCommentCreation.Builder#create()} must be called.
*
* <p>Example:
*
* <pre>
* String createdDraftCommentUuid = changeOperations
* .change(changeId)
* .currentPatchset()
* .newDraftComment()
* .onLine(2)
* .ofFile("file1")
* .create();
* </pre>
*
* @return builder to create a new comment
*/
TestCommentCreation.Builder newDraftComment();
}

View File

@@ -18,6 +18,7 @@ import static com.google.gerrit.server.CommentsUtil.setCommentCommitId;
import com.google.gerrit.acceptance.testsuite.change.TestCommentCreation.CommentSide;
import com.google.gerrit.entities.Account;
import com.google.gerrit.entities.Comment.Status;
import com.google.gerrit.entities.HumanComment;
import com.google.gerrit.entities.Patch;
import com.google.gerrit.entities.PatchSet;
@@ -25,7 +26,6 @@ import com.google.gerrit.entities.Project;
import com.google.gerrit.extensions.client.Comment;
import com.google.gerrit.extensions.client.Comment.Range;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.UnprocessableEntityException;
import com.google.gerrit.server.CommentsUtil;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.IdentifiedUser.GenericFactory;
@@ -93,7 +93,12 @@ public class PerPatchsetOperationsImpl implements PerPatchsetOperations {
@Override
public TestCommentCreation.Builder newComment() {
return TestCommentCreation.builder(this::createComment);
return TestCommentCreation.builder(this::createComment, Status.PUBLISHED);
}
@Override
public TestCommentCreation.Builder newDraftComment() {
return TestCommentCreation.builder(this::createComment, Status.DRAFT);
}
private String createComment(TestCommentCreation commentCreation)
@@ -133,15 +138,16 @@ public class PerPatchsetOperationsImpl implements PerPatchsetOperations {
public boolean updateChange(ChangeContext context) throws Exception {
HumanComment comment = toNewComment(context, commentCreation);
ChangeUpdate changeUpdate = context.getUpdate(patchsetId);
changeUpdate.putComment(HumanComment.Status.PUBLISHED, comment);
// Only the tag set on the ChangeUpdate matters. The tag field of HumanComment is ignored.
changeUpdate.putComment(commentCreation.status(), comment);
// For published comments, only the tag set on the ChangeUpdate (and not on the HumanComment)
// matters.
commentCreation.tag().ifPresent(changeUpdate::setTag);
createdCommentUuid = comment.key.uuid;
return true;
}
private HumanComment toNewComment(ChangeContext context, TestCommentCreation commentCreation)
throws UnprocessableEntityException, PatchListNotAvailableException {
throws PatchListNotAvailableException {
String message = commentCreation.message().orElse("The text of a test comment.");
String filePath = commentCreation.file().orElse(Patch.PATCHSET_LEVEL);
@@ -159,6 +165,9 @@ public class PerPatchsetOperationsImpl implements PerPatchsetOperations {
message,
unresolved,
parentUuid);
// For draft comments, only the tag set on the HumanComment (and not on the ChangeUpdate)
// matters.
commentCreation.tag().ifPresent(tag -> newComment.tag = tag);
commentCreation.line().ifPresent(line -> newComment.setLineNbrAndRange(line, null));
// Specification of range trumps explicit line specification.

View File

@@ -19,6 +19,7 @@ import com.google.gerrit.acceptance.testsuite.ThrowingFunction;
import com.google.gerrit.acceptance.testsuite.change.TestRange.Position;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.entities.Account;
import com.google.gerrit.entities.Comment;
import com.google.gerrit.entities.Patch;
import java.util.Optional;
import java.util.function.Function;
@@ -46,11 +47,15 @@ public abstract class TestCommentCreation {
public abstract Optional<Account.Id> author();
abstract Comment.Status status();
abstract ThrowingFunction<TestCommentCreation, String> commentCreator();
public static TestCommentCreation.Builder builder(
ThrowingFunction<TestCommentCreation, String> commentCreator) {
return new AutoValue_TestCommentCreation.Builder().commentCreator(commentCreator);
ThrowingFunction<TestCommentCreation, String> commentCreator, Comment.Status commentStatus) {
return new AutoValue_TestCommentCreation.Builder()
.commentCreator(commentCreator)
.status(commentStatus);
}
@AutoValue.Builder
@@ -156,7 +161,8 @@ public abstract class TestCommentCreation {
/**
* UUID of another comment to which this comment is a reply. This comment must have similar
* attributes (e.g. file, line, side) as the parent comment.
* attributes (e.g. file, line, side) as the parent comment. The parent comment must be a
* published comment.
*/
public abstract Builder parentUuid(String parentUuid);
@@ -166,6 +172,13 @@ public abstract class TestCommentCreation {
/** Author of the comment. Must be an existing user account. */
public abstract Builder author(Account.Id accountId);
/**
* Status of the comment. Hidden in the API surface. Use {@link
* PerPatchsetOperations#newComment()} or {@link PerPatchsetOperations#newDraftComment()}
* depending on which type of comment you want to create.
*/
abstract Builder status(Comment.Status value);
abstract TestCommentCreation.Builder commentCreator(
ThrowingFunction<TestCommentCreation, String> commentCreator);