From c6274b82f0a1af9dba0a000b6f579961aa29d693 Mon Sep 17 00:00:00 2001 From: suncl Date: Thu, 11 Aug 2016 17:27:06 +0800 Subject: [PATCH] ADD:implement cherryPick,submitType,testSubmitType --- .../http/changes/RevisionApiRestClient.java | 26 ++++++ .../changes/RevisionApiRestClientTest.java | 79 ++++++++++++++++++- .../client/rest/http/changes/submittype.json | 2 + .../rest/http/changes/testsubmittype.json | 2 + 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/com/urswolfer/gerrit/client/rest/http/changes/submittype.json create mode 100644 src/test/resources/com/urswolfer/gerrit/client/rest/http/changes/testsubmittype.json diff --git a/src/main/java/com/urswolfer/gerrit/client/rest/http/changes/RevisionApiRestClient.java b/src/main/java/com/urswolfer/gerrit/client/rest/http/changes/RevisionApiRestClient.java index cbe6f1ab..e2fcbf4e 100644 --- a/src/main/java/com/urswolfer/gerrit/client/rest/http/changes/RevisionApiRestClient.java +++ b/src/main/java/com/urswolfer/gerrit/client/rest/http/changes/RevisionApiRestClient.java @@ -17,12 +17,15 @@ package com.urswolfer.gerrit.client.rest.http.changes; import com.google.gerrit.extensions.api.changes.*; +import com.google.gerrit.extensions.client.SubmitType; import com.google.gerrit.extensions.common.CommentInfo; import com.google.gerrit.extensions.common.FileInfo; +import com.google.gerrit.extensions.common.TestSubmitRuleInput; import com.google.gerrit.extensions.restapi.BinaryResult; import com.google.gerrit.extensions.restapi.RestApiException; import com.google.gerrit.extensions.restapi.Url; import com.google.gson.JsonElement; +import com.google.gson.reflect.TypeToken; import com.urswolfer.gerrit.client.rest.http.GerritRestClient; import com.urswolfer.gerrit.client.rest.http.util.BinaryResultUtils; import org.apache.http.HttpResponse; @@ -93,6 +96,14 @@ public void publish() throws RestApiException { gerritRestClient.postRequest(request); } + @Override + public ChangeApi cherryPick(CherryPickInput in) throws RestApiException { + String request = getRequestPath() + "/cherrypick"; + String json = gerritRestClient.getGson().toJson(in); + gerritRestClient.postRequest(request, json); + return changeApiRestClient; + } + @Override public ChangeApi rebase() throws RestApiException { return rebase(new RebaseInput()); @@ -174,6 +185,21 @@ public BinaryResult patch() throws RestApiException { } } + @Override + public SubmitType submitType() throws RestApiException { + String request = getRequestPath() + "/submit_type"; + JsonElement jsonElement = gerritRestClient.getRequest(request); + return gerritRestClient.getGson().fromJson(jsonElement, new TypeToken() {}.getType()); + } + + @Override + public SubmitType testSubmitType(TestSubmitRuleInput in) throws RestApiException { + String request = getRequestPath() + "/test.submit_type"; + String json = gerritRestClient.getGson().toJson(in); + JsonElement jsonElement = gerritRestClient.postRequest(request,json); + return gerritRestClient.getGson().fromJson(jsonElement, new TypeToken() {}.getType()); + } + protected String getRequestPath() { return changeApiRestClient.getRequestPath() + "/revisions/" + revision; } diff --git a/src/test/java/com/urswolfer/gerrit/client/rest/http/changes/RevisionApiRestClientTest.java b/src/test/java/com/urswolfer/gerrit/client/rest/http/changes/RevisionApiRestClientTest.java index a995f9bd..582ea186 100644 --- a/src/test/java/com/urswolfer/gerrit/client/rest/http/changes/RevisionApiRestClientTest.java +++ b/src/test/java/com/urswolfer/gerrit/client/rest/http/changes/RevisionApiRestClientTest.java @@ -18,11 +18,15 @@ import com.google.common.collect.Lists; import com.google.common.truth.Truth; +import com.google.gerrit.extensions.api.changes.CherryPickInput; import com.google.gerrit.extensions.api.changes.ReviewInput; import com.google.gerrit.extensions.api.changes.SubmitInput; +import com.google.gerrit.extensions.client.SubmitType; +import com.google.gerrit.extensions.common.TestSubmitRuleInput; import com.google.gerrit.extensions.restapi.BinaryResult; import com.google.gson.JsonElement; import com.urswolfer.gerrit.client.rest.http.GerritRestClient; +import com.urswolfer.gerrit.client.rest.http.common.AbstractParserTest; import com.urswolfer.gerrit.client.rest.http.common.GerritRestClientBuilder; import org.apache.commons.codec.binary.Base64; import org.apache.http.HttpEntity; @@ -39,7 +43,7 @@ /** * @author Thomas Forrer */ -public class RevisionApiRestClientTest { +public class RevisionApiRestClientTest extends AbstractParserTest { private static final String CHANGE_ID = "packages%2Ftest~master~Ieabd72e73f3da0df90fd6e8cba8f6c5dd7d120df"; private static final String FILE_PATH = "src/main/README.md"; @@ -53,22 +57,28 @@ public Iterator testCases() throws Exception { .expectReviewUrl("/changes/" + CHANGE_ID + "/revisions/current/review") .expectSubmitUrl("/changes/" + CHANGE_ID + "/submit") .expectPublishUrl("/changes/" + CHANGE_ID + "/revisions/current/publish") + .expectCherryPickUrl("/changes/" + CHANGE_ID + "/revisions/current/cherrypick") .expectRebaseUrl("/changes/" + CHANGE_ID + "/revisions/current/rebase") .expectGetFileUrl("/changes/" + CHANGE_ID + "/revisions/current/files") .expectFileReviewedUrl("/changes/" + CHANGE_ID + "/revisions/current/files/" + FILE_PATH_ENCODED + "/reviewed") .expectGetCommentsUrl("/changes/" + CHANGE_ID + "/revisions/current/comments/") .expectGetDraftsUrl("/changes/" + CHANGE_ID + "/revisions/current/drafts/") + .expectSubmitTypeUrl("/changes/" + CHANGE_ID + "/revisions/current/submit_type") + .expectTestSubmitTypeUrl("/changes/" + CHANGE_ID + "/revisions/current/test.submit_type") .get(), withRevision("3") .expectRevisionUrl("/changes/" + CHANGE_ID + "/revisions/3") .expectReviewUrl("/changes/" + CHANGE_ID + "/revisions/3/review") .expectSubmitUrl("/changes/" + CHANGE_ID + "/submit") .expectPublishUrl("/changes/" + CHANGE_ID + "/revisions/3/publish") + .expectCherryPickUrl("/changes/" + CHANGE_ID + "/revisions/3/cherrypick") .expectRebaseUrl("/changes/" + CHANGE_ID + "/revisions/3/rebase") .expectGetFileUrl("/changes/" + CHANGE_ID + "/revisions/3/files") .expectFileReviewedUrl("/changes/" + CHANGE_ID + "/revisions/3/files/" + FILE_PATH_ENCODED + "/reviewed") .expectGetCommentsUrl("/changes/" + CHANGE_ID + "/revisions/3/comments/") .expectGetDraftsUrl("/changes/" + CHANGE_ID + "/revisions/3/drafts/") + .expectSubmitTypeUrl("/changes/" + CHANGE_ID + "/revisions/3/submit_type") + .expectTestSubmitTypeUrl("/changes/" + CHANGE_ID + "/revisions/3/test.submit_type") .get() ).iterator(); } @@ -141,6 +151,23 @@ public void testPublish(RevisionApiTestCase testCase) throws Exception { EasyMock.verify(gerritRestClient); } + @Test(dataProvider = "TestCases") + public void testCherryPick(RevisionApiTestCase testCase) throws Exception { + GerritRestClient gerritRestClient = new GerritRestClientBuilder() + .expectPost(testCase.cherryPickUrl, + "{\"message\":\"Implementing Feature X\",\"destination\":\"release-branch\"}") + .expectGetGson() + .get(); + + ChangesRestClient changesRestClient = getChangesRestClient(gerritRestClient); + CherryPickInput cherryPickInput = new CherryPickInput(); + cherryPickInput.message = "Implementing Feature X"; + cherryPickInput.destination = "release-branch"; + changesRestClient.id(CHANGE_ID).revision(testCase.revision).cherryPick(cherryPickInput); + + EasyMock.verify(gerritRestClient); + } + @Test(dataProvider = "TestCases") public void testRebase(RevisionApiTestCase testCase) throws Exception { GerritRestClient gerritRestClient = new GerritRestClientBuilder() @@ -270,6 +297,38 @@ public void testPatch() throws Exception { } } + @Test(dataProvider = "TestCases") + public void testSubmitType(RevisionApiTestCase testCase) throws Exception { + JsonElement jsonElement = getJsonElement("submittype.json"); + GerritRestClient gerritRestClient = new GerritRestClientBuilder() + .expectGet(testCase.submitTypeUrl, jsonElement) + .expectGetGson() + .get(); + ChangesRestClient changesRestClient = getChangesRestClient(gerritRestClient); + SubmitType expectSubmitType = changesRestClient.id(CHANGE_ID).revision(testCase.revision).submitType(); + + Truth.assertThat(expectSubmitType.equals(SubmitType.MERGE_IF_NECESSARY)); + EasyMock.verify(gerritRestClient); + } + + @Test(dataProvider = "TestCases") + public void testTestSubmitType(RevisionApiTestCase testCase) throws Exception { + JsonElement jsonElement = getJsonElement("testsubmittype.json"); + GerritRestClient gerritRestClient = new GerritRestClientBuilder() + .expectPost(testCase.testSubmitTypeUrl, "{\"rule\":\"submit_type(cherry_pick)\",\"filters\":\"SKIP\"}", jsonElement) + .expectGetGson() + .expectGetGson() + .get(); + ChangesRestClient changesRestClient = getChangesRestClient(gerritRestClient); + TestSubmitRuleInput testSubmitRuleInput = new TestSubmitRuleInput(); + testSubmitRuleInput.filters = TestSubmitRuleInput.Filters.SKIP; + testSubmitRuleInput.rule = "submit_type(cherry_pick)"; + SubmitType expectSubmitType = changesRestClient.id(CHANGE_ID).revision(testCase.revision).testSubmitType(testSubmitRuleInput); + + Truth.assertThat(expectSubmitType.equals(SubmitType.CHERRY_PICK)); + EasyMock.verify(gerritRestClient); + } + private ChangesRestClient getChangesRestClient(GerritRestClient gerritRestClient) { ChangesParser changesParser = EasyMock.createMock(ChangesParser.class); CommentsParser commentsParser = EasyMock.createMock(CommentsParser.class); @@ -302,11 +361,14 @@ private static final class RevisionApiTestCase { private String reviewUrl; private String submitUrl; private String publishUrl; + private String cherryPickUrl; private String rebaseUrl; private String fileUrl; private String fileReviewedUrl; private String getCommentsUrl; private String getDraftsUrl; + private String submitTypeUrl; + private String testSubmitTypeUrl; private RevisionApiTestCase(String revision) { this.revision = revision; @@ -332,6 +394,11 @@ private RevisionApiTestCase expectPublishUrl(String publishUrl) { return this; } + private RevisionApiTestCase expectCherryPickUrl(String cherryPickUrl) { + this.cherryPickUrl = cherryPickUrl; + return this; + } + private RevisionApiTestCase expectRebaseUrl(String rebaseUrl) { this.rebaseUrl = rebaseUrl; return this; @@ -357,6 +424,16 @@ private RevisionApiTestCase expectGetDraftsUrl(String getDraftsUrl) { return this; } + private RevisionApiTestCase expectSubmitTypeUrl(String submitTypeUrl) { + this.submitTypeUrl = submitTypeUrl; + return this; + } + + private RevisionApiTestCase expectTestSubmitTypeUrl(String testSubmitTypeUrl) { + this.testSubmitTypeUrl = testSubmitTypeUrl; + return this; + } + private RevisionApiTestCase[] get() { return new RevisionApiTestCase[]{this}; } diff --git a/src/test/resources/com/urswolfer/gerrit/client/rest/http/changes/submittype.json b/src/test/resources/com/urswolfer/gerrit/client/rest/http/changes/submittype.json new file mode 100644 index 00000000..6ee881ee --- /dev/null +++ b/src/test/resources/com/urswolfer/gerrit/client/rest/http/changes/submittype.json @@ -0,0 +1,2 @@ +)]}' +"MERGE_IF_NECESSARY" diff --git a/src/test/resources/com/urswolfer/gerrit/client/rest/http/changes/testsubmittype.json b/src/test/resources/com/urswolfer/gerrit/client/rest/http/changes/testsubmittype.json new file mode 100644 index 00000000..91e12e39 --- /dev/null +++ b/src/test/resources/com/urswolfer/gerrit/client/rest/http/changes/testsubmittype.json @@ -0,0 +1,2 @@ +)]}' +"CHERRY_PICK"