Skip to content

Commit

Permalink
ADD:implement cherryPick,submitType,testSubmitType
Browse files Browse the repository at this point in the history
  • Loading branch information
suncl committed Aug 11, 2016
1 parent b83db35 commit c6274b8
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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<SubmitType>() {}.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<SubmitType>() {}.getType());
}

protected String getRequestPath() {
return changeApiRestClient.getRequestPath() + "/revisions/" + revision;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand All @@ -53,22 +57,28 @@ public Iterator<RevisionApiTestCase[]> 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();
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
)]}'
"MERGE_IF_NECESSARY"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
)]}'
"CHERRY_PICK"

0 comments on commit c6274b8

Please sign in to comment.