Search in sources :

Example 6 with ActionVisitor

use of com.google.gerrit.extensions.api.changes.ActionVisitor in project gerrit by GerritCodeReview.

the class ActionsIT method currentRevisionActionVisitor.

@Test
public void currentRevisionActionVisitor() throws Exception {
    String id = createChange().getChangeId();
    amendChange(id);
    ChangeInfo origChange = gApi.changes().id(id).get(CHANGE_ACTIONS);
    Change.Id changeId = Change.id(origChange._number);
    class Visitor implements ActionVisitor {

        @Override
        public boolean visit(String name, ActionInfo actionInfo, ChangeInfo changeInfo) {
            // Do nothing; implicitly called for CURRENT_ACTIONS.
            return true;
        }

        @Override
        public boolean visit(String name, ActionInfo actionInfo, ChangeInfo changeInfo, RevisionInfo revisionInfo) {
            assertThat(changeInfo).isNotNull();
            assertThat(changeInfo._number).isEqualTo(origChange._number);
            assertThat(revisionInfo).isNotNull();
            assertThat(revisionInfo._number).isEqualTo(2);
            if (name.equals("cherrypick")) {
                return false;
            }
            if (name.equals("rebase")) {
                actionInfo.label = "All Your Base";
            }
            return true;
        }
    }
    Map<String, ActionInfo> origActions = gApi.changes().id(id).current().actions();
    assertThat(origActions.keySet()).containsAtLeast("cherrypick", "rebase");
    assertThat(origActions.get("rebase").label).isEqualTo("Rebase");
    try (Registration registration = extensionRegistry.newRegistration().add(new Visitor())) {
        // Test different codepaths within ActionJson...
        // ...via revision API.
        visitedCurrentRevisionActionsAssertions(origActions, gApi.changes().id(id).current().actions());
        // ...via change API with option.
        EnumSet<ListChangesOption> opts = EnumSet.of(CURRENT_ACTIONS, CURRENT_REVISION);
        ChangeInfo changeInfo = gApi.changes().id(id).get(opts);
        RevisionInfo revisionInfo = Iterables.getOnlyElement(changeInfo.revisions.values());
        visitedCurrentRevisionActionsAssertions(origActions, revisionInfo.actions);
        // ...via ChangeJson directly.
        ChangeData cd = changeDataFactory.create(project, changeId);
        revisionJsonFactory.create(opts).getRevisionInfo(cd, cd.patchSet(PatchSet.id(changeId, 1)));
    }
}
Also used : ListChangesOption(com.google.gerrit.extensions.client.ListChangesOption) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) ActionVisitor(com.google.gerrit.extensions.api.changes.ActionVisitor) ActionVisitor(com.google.gerrit.extensions.api.changes.ActionVisitor) ActionInfo(com.google.gerrit.extensions.common.ActionInfo) Change(com.google.gerrit.entities.Change) ChangeData(com.google.gerrit.server.query.change.ChangeData) RevisionInfo(com.google.gerrit.extensions.common.RevisionInfo) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 7 with ActionVisitor

use of com.google.gerrit.extensions.api.changes.ActionVisitor in project gerrit by GerritCodeReview.

the class ActionsIT method changeActionVisitor.

@Test
public void changeActionVisitor() throws Exception {
    String id = createChange().getChangeId();
    ChangeInfo origChange = gApi.changes().id(id).get(CHANGE_ACTIONS);
    class Visitor implements ActionVisitor {

        @Override
        public boolean visit(String name, ActionInfo actionInfo, ChangeInfo changeInfo) {
            assertThat(changeInfo).isNotNull();
            assertThat(changeInfo._number).isEqualTo(origChange._number);
            if (name.equals("followup")) {
                return false;
            }
            if (name.equals("abandon")) {
                actionInfo.label = "Abandon All Hope";
            }
            return true;
        }

        @Override
        public boolean visit(String name, ActionInfo actionInfo, ChangeInfo changeInfo, RevisionInfo revisionInfo) {
            throw new UnsupportedOperationException();
        }
    }
    Map<String, ActionInfo> origActions = origChange.actions;
    assertThat(origActions.keySet()).containsAtLeast("followup", "abandon");
    assertThat(origActions.get("abandon").label).isEqualTo("Abandon");
    try (Registration registration = extensionRegistry.newRegistration().add(new Visitor())) {
        Map<String, ActionInfo> newActions = gApi.changes().id(id).get(EnumSet.of(ListChangesOption.CHANGE_ACTIONS)).actions;
        Set<String> expectedNames = new TreeSet<>(origActions.keySet());
        expectedNames.remove("followup");
        assertThat(newActions.keySet()).isEqualTo(expectedNames);
        ActionInfo abandon = newActions.get("abandon");
        assertThat(abandon).isNotNull();
        assertThat(abandon.label).isEqualTo("Abandon All Hope");
    }
}
Also used : ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) RevisionInfo(com.google.gerrit.extensions.common.RevisionInfo) ActionVisitor(com.google.gerrit.extensions.api.changes.ActionVisitor) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) TreeSet(java.util.TreeSet) ActionVisitor(com.google.gerrit.extensions.api.changes.ActionVisitor) ActionInfo(com.google.gerrit.extensions.common.ActionInfo) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Aggregations

ActionVisitor (com.google.gerrit.extensions.api.changes.ActionVisitor)7 ActionInfo (com.google.gerrit.extensions.common.ActionInfo)6 ChangeInfo (com.google.gerrit.extensions.common.ChangeInfo)5 RevisionInfo (com.google.gerrit.extensions.common.RevisionInfo)5 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)4 Test (org.junit.Test)4 Registration (com.google.gerrit.acceptance.ExtensionRegistry.Registration)3 ListChangesOption (com.google.gerrit.extensions.client.ListChangesOption)2 PrivateInternals_UiActionDescription (com.google.gerrit.extensions.webui.PrivateInternals_UiActionDescription)2 UiAction (com.google.gerrit.extensions.webui.UiAction)2 ChangeData (com.google.gerrit.server.query.change.ChangeData)2 LinkedHashMap (java.util.LinkedHashMap)2 Change (com.google.gerrit.entities.Change)1 Change (com.google.gerrit.reviewdb.client.Change)1 CurrentUser (com.google.gerrit.server.CurrentUser)1 TreeSet (java.util.TreeSet)1