Search in sources :

Example 6 with Registration

use of com.google.gerrit.acceptance.ExtensionRegistry.Registration in project gerrit by GerritCodeReview.

the class ProjectIT method createAndDeleteBranchByPush.

@Test
public void createAndDeleteBranchByPush() throws Exception {
    ProjectIndexedCounter projectIndexedCounter = new ProjectIndexedCounter();
    try (Registration registration = extensionRegistry.newRegistration().add(projectIndexedCounter)) {
        projectOperations.project(project).forUpdate().add(allow(Permission.PUSH).ref("refs/*").group(adminGroupUuid()).force(true)).update();
        projectIndexedCounter.clear();
        assertThat(hasHead(project, "foo")).isFalse();
        PushOneCommit.Result r = pushTo("refs/heads/foo");
        r.assertOkStatus();
        assertThat(getRemoteHead(project.get(), "foo")).isEqualTo(r.getCommit());
        projectIndexedCounter.assertNoReindex();
        PushResult r2 = GitUtil.pushOne(testRepo, null, "refs/heads/foo", false, true, null);
        assertThat(r2.getRemoteUpdate("refs/heads/foo").getStatus()).isEqualTo(Status.OK);
        assertThat(hasHead(project, "foo")).isFalse();
        projectIndexedCounter.assertNoReindex();
    }
}
Also used : Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) PushResult(org.eclipse.jgit.transport.PushResult) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 7 with Registration

use of com.google.gerrit.acceptance.ExtensionRegistry.Registration in project gerrit by GerritCodeReview.

the class PostReviewIT method onPostReviewExtensionThatDoesntExtendTheChangeMessage.

@Test
public void onPostReviewExtensionThatDoesntExtendTheChangeMessage() throws Exception {
    PushOneCommit.Result r = createChange();
    TestOnPostReview testOnPostReview = new TestOnPostReview(/* message= */
    null);
    try (Registration registration = extensionRegistry.newRegistration().add(testOnPostReview)) {
        ReviewInput input = new ReviewInput().label(LabelId.CODE_REVIEW, 1);
        gApi.changes().id(r.getChangeId()).current().review(input);
        Collection<ChangeMessageInfo> messages = gApi.changes().id(r.getChangeId()).get().messages;
        assertThat(Iterables.getLast(messages).message).isEqualTo("Patch Set 1: Code-Review+1");
    }
}
Also used : Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) ChangeMessageInfo(com.google.gerrit.extensions.common.ChangeMessageInfo) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 8 with Registration

use of com.google.gerrit.acceptance.ExtensionRegistry.Registration in project gerrit by GerritCodeReview.

the class PostReviewIT method votingTheSameVoteSecondTimeDoesNotFireOnCommentAdded.

@Test
public void votingTheSameVoteSecondTimeDoesNotFireOnCommentAdded() throws Exception {
    PushOneCommit.Result r = createChange();
    // Add a new vote.
    ReviewInput input = new ReviewInput().label(LabelId.CODE_REVIEW, 2);
    gApi.changes().id(r.getChangeId()).current().review(input);
    assertThat(r.getChange().approvals().values()).hasSize(1);
    TestListener testListener = new TestListener();
    try (Registration registration = extensionRegistry.newRegistration().add(testListener)) {
        // Post without changing the vote.
        input = new ReviewInput().label(LabelId.CODE_REVIEW, 2);
        gApi.changes().id(r.getChangeId()).current().review(input);
        // Event not fired.
        assertThat(testListener.lastCommentAddedEvent).isNull();
    }
}
Also used : Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 9 with Registration

use of com.google.gerrit.acceptance.ExtensionRegistry.Registration in project gerrit by GerritCodeReview.

the class PostReviewIT method addingAndDeletingReviewers.

@Test
public void addingAndDeletingReviewers() throws Exception {
    PushOneCommit.Result r = createChange();
    TestAccount user2 = accountCreator.user2();
    TestAccount user3 = accountCreator.create("user3", "user3@email.com", "user3", "user3");
    TestAccount user4 = accountCreator.create("user4", "user4@email.com", "user4", "user4");
    // add user and user2
    gApi.changes().id(r.getChangeId()).current().review(ReviewInput.create().reviewer(user.email()).reviewer(user2.email()));
    sender.clear();
    TestReviewerAddedListener testReviewerAddedListener = new TestReviewerAddedListener();
    TestReviewerDeletedListener testReviewerDeletedListener = new TestReviewerDeletedListener();
    try (Registration registration = extensionRegistry.newRegistration().add(testReviewerAddedListener).add(testReviewerDeletedListener)) {
        // remove user and user2 while adding user3 and user4
        ReviewResult reviewResult = gApi.changes().id(r.getChangeId()).current().review(ReviewInput.create().reviewer(user.email(), ReviewerState.REMOVED, /* confirmed= */
        true).reviewer(user2.email(), ReviewerState.REMOVED, /* confirmed= */
        true).reviewer(user3.email()).reviewer(user4.email()));
        assertThat(reviewResult.reviewers.values().stream().filter(a -> a.removed != null).map(a -> a.removed.name).collect(toImmutableSet())).containsExactly(user.fullName(), user2.fullName());
        assertThat(reviewResult.reviewers.values().stream().filter(a -> a.reviewers != null).map(a -> Iterables.getOnlyElement(a.reviewers).name).collect(toImmutableSet())).containsExactly(user3.fullName(), user4.fullName());
    }
    assertThat(gApi.changes().id(r.getChangeId()).reviewers().stream().map(a -> a.name).collect(toImmutableSet())).containsExactly(user3.fullName(), user4.fullName());
    // Ensure only one batch email was sent for this operation
    FakeEmailSender.Message message = Iterables.getOnlyElement(sender.getMessages());
    assertThat(message.body()).containsMatch(Pattern.quote("Hello ") + "(" + Pattern.quote(String.format("%s, %s", user3.fullName(), user4.fullName())) + "|" + Pattern.quote(String.format("%s, %s", user4.fullName(), user3.fullName())) + ")");
    assertThat(message.htmlBody()).containsMatch("(" + Pattern.quote(String.format("%s and %s", user3.fullName(), user4.fullName())) + "|" + Pattern.quote(String.format("%s and %s", user4.fullName(), user3.fullName())) + ")" + Pattern.quote(" to <strong>review</strong> this change"));
    assertThat(message.body()).containsMatch(Pattern.quote("removed ") + "(" + Pattern.quote(String.format("%s, %s", user.fullName(), user2.fullName())) + "|" + Pattern.quote(String.format("%s, %s", user2.fullName(), user.fullName())) + ")");
    assertThat(message.htmlBody()).containsMatch(Pattern.quote("removed ") + "(" + Pattern.quote(String.format("%s and %s", user.fullName(), user2.fullName())) + "|" + Pattern.quote(String.format("%s and %s", user2.fullName(), user.fullName())) + ")");
    // Ensure that events have been sent:
    // * 1 batch event for adding user3 and user4 as reviewers
    // * 2 events for removing user and user2 as reviewers (one event per removed reviewer, batch
    // event not available for reviewer removal)
    assertThat(testReviewerAddedListener.receivedEvents).hasSize(1);
    assertThat(testReviewerAddedListener.getReviewerIds()).containsExactly(user3.id(), user4.id());
    assertThat(testReviewerDeletedListener.receivedEvents).hasSize(2);
    assertThat(testReviewerDeletedListener.getReviewerIds()).containsExactly(user.id(), user2.id());
}
Also used : ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) Module(com.google.inject.Module) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) TestAccount(com.google.gerrit.acceptance.TestAccount) Inject(com.google.inject.Inject) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) ReviewResult(com.google.gerrit.extensions.api.changes.ReviewResult) ReviewerDeletedListener(com.google.gerrit.extensions.events.ReviewerDeletedListener) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) AccountInfo(com.google.gerrit.extensions.common.AccountInfo) Map(java.util.Map) DraftHandling(com.google.gerrit.extensions.api.changes.ReviewInput.DraftHandling) Side(com.google.gerrit.extensions.client.Side) PostReview(com.google.gerrit.server.restapi.change.PostReview) ImmutableSet(com.google.common.collect.ImmutableSet) Mockito.clearInvocations(org.mockito.Mockito.clearInvocations) ImmutableMap(com.google.common.collect.ImmutableMap) Timestamp(java.sql.Timestamp) Collection(java.util.Collection) Account(com.google.gerrit.entities.Account) SubmitRecord(com.google.gerrit.entities.SubmitRecord) RequestScopeOperations(com.google.gerrit.acceptance.testsuite.request.RequestScopeOperations) ChangeData(com.google.gerrit.server.query.change.ChangeData) List(java.util.List) Nullable(com.google.gerrit.common.Nullable) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Optional(java.util.Optional) GerritJUnit.assertThrows(com.google.gerrit.testing.GerritJUnit.assertThrows) Exports(com.google.gerrit.extensions.annotations.Exports) Pattern(java.util.regex.Pattern) Mockito.mock(org.mockito.Mockito.mock) FactoryModule(com.google.gerrit.extensions.config.FactoryModule) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) Iterables(com.google.common.collect.Iterables) CommentValidationContext(com.google.gerrit.extensions.validators.CommentValidationContext) LabelId(com.google.gerrit.entities.LabelId) DraftInput(com.google.gerrit.extensions.api.changes.DraftInput) ReviewerState(com.google.gerrit.extensions.client.ReviewerState) CommentAddedListener(com.google.gerrit.extensions.events.CommentAddedListener) CommentForValidation(com.google.gerrit.extensions.validators.CommentForValidation) Captor(org.mockito.Captor) ReviewerAddedListener(com.google.gerrit.extensions.events.ReviewerAddedListener) ArrayList(java.util.ArrayList) CommentsRejectedException(com.google.gerrit.server.update.CommentsRejectedException) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) CommentInput(com.google.gerrit.extensions.api.changes.ReviewInput.CommentInput) TestCommentHelper(com.google.gerrit.testing.TestCommentHelper) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Change(com.google.gerrit.entities.Change) PatchSet(com.google.gerrit.entities.PatchSet) OnPostReview(com.google.gerrit.server.restapi.change.OnPostReview) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) FakeEmailSender(com.google.gerrit.testing.FakeEmailSender) ExtensionRegistry(com.google.gerrit.acceptance.ExtensionRegistry) Before(org.junit.Before) SubmitRule(com.google.gerrit.server.rules.SubmitRule) ChangeMessageInfo(com.google.gerrit.extensions.common.ChangeMessageInfo) RobotCommentInfo(com.google.gerrit.extensions.common.RobotCommentInfo) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Correspondence(com.google.common.truth.Correspondence) Collectors.toList(java.util.stream.Collectors.toList) RobotCommentInput(com.google.gerrit.extensions.api.changes.ReviewInput.RobotCommentInput) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) CommentValidator(com.google.gerrit.extensions.validators.CommentValidator) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) FakeEmailSender(com.google.gerrit.testing.FakeEmailSender) ReviewResult(com.google.gerrit.extensions.api.changes.ReviewResult) TestAccount(com.google.gerrit.acceptance.TestAccount) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 10 with Registration

use of com.google.gerrit.acceptance.ExtensionRegistry.Registration in project gerrit by GerritCodeReview.

the class PostReviewIT method submitRulesAreInvokedOnlyOnce.

@Test
public void submitRulesAreInvokedOnlyOnce() throws Exception {
    PushOneCommit.Result r = createChange();
    TestSubmitRule testSubmitRule = new TestSubmitRule();
    try (Registration registration = extensionRegistry.newRegistration().add(testSubmitRule)) {
        ReviewInput input = new ReviewInput().label(LabelId.CODE_REVIEW, 1);
        gApi.changes().id(r.getChangeId()).current().review(input);
    }
    assertThat(testSubmitRule.count).isEqualTo(1);
}
Also used : Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Aggregations

Registration (com.google.gerrit.acceptance.ExtensionRegistry.Registration)205 Test (org.junit.Test)200 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)194 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)90 AccountIndexedCounter (com.google.gerrit.acceptance.AccountIndexedCounter)47 RestResponse (com.google.gerrit.acceptance.RestResponse)39 GerritConfig (com.google.gerrit.acceptance.config.GerritConfig)38 ChangeInfo (com.google.gerrit.extensions.common.ChangeInfo)31 ReviewInput (com.google.gerrit.extensions.api.changes.ReviewInput)23 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)22 TestAccount (com.google.gerrit.acceptance.TestAccount)19 PublicKeyStore.keyToString (com.google.gerrit.gpg.PublicKeyStore.keyToString)19 AccountInfo (com.google.gerrit.extensions.common.AccountInfo)16 RequestCancelledException (com.google.gerrit.server.cancellation.RequestCancelledException)15 Config (org.eclipse.jgit.lib.Config)14 BranchInput (com.google.gerrit.extensions.api.projects.BranchInput)12 CreateProjectArgs (com.google.gerrit.server.project.CreateProjectArgs)11 ProjectCreationValidationListener (com.google.gerrit.server.validators.ProjectCreationValidationListener)11 RevCommit (org.eclipse.jgit.revwalk.RevCommit)11 ImmutableList (com.google.common.collect.ImmutableList)10