Unverified Commit facd6a53 authored by Greg Messner's avatar Greg Messner Committed by GitHub
Browse files

Simplify create and update merge request (#464) (#473).

parent cfaa09ca
......@@ -14,6 +14,7 @@ import org.gitlab4j.api.models.Commit;
import org.gitlab4j.api.models.Issue;
import org.gitlab4j.api.models.MergeRequest;
import org.gitlab4j.api.models.MergeRequestFilter;
import org.gitlab4j.api.models.MergeRequestParams;
import org.gitlab4j.api.models.Participant;
/**
......@@ -337,7 +338,24 @@ public class MergeRequestApi extends AbstractApi {
}
/**
* Creates a merge request and optionally assigns a reviewer to it.
* Creates a merge request.
*
* <pre><code>GitLab Endpoint: POST /projects/:id/merge_requests</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param params a MergeRequestParams instance holding the info to create the merge request
* @return the created MergeRequest instance
* @throws GitLabApiException if any exception occurs
* @since GitLab Starter 8.17, GitLab CE 11.0.
*/
public MergeRequest createMergeRequest(Object projectIdOrPath, MergeRequestParams params) throws GitLabApiException {
GitLabApiForm form = params.getForm(true);
Response response = post(Response.Status.CREATED, form, "projects", getProjectIdOrPath(projectIdOrPath), "merge_requests");
return (response.readEntity(MergeRequest.class));
}
/**
* Creates a merge request.
*
* <pre><code>GitLab Endpoint: POST /projects/:id/merge_requests</code></pre>
*
......@@ -359,24 +377,23 @@ public class MergeRequestApi extends AbstractApi {
public MergeRequest createMergeRequest(Object projectIdOrPath, String sourceBranch, String targetBranch, String title, String description, Integer assigneeId,
Integer targetProjectId, String[] labels, Integer milestoneId, Boolean removeSourceBranch, Boolean squash) throws GitLabApiException {
Form formData = new Form();
addFormParam(formData, "source_branch", sourceBranch, true);
addFormParam(formData, "target_branch", targetBranch, true);
addFormParam(formData, "title", title, true);
addFormParam(formData, "description", description, false);
addFormParam(formData, "assignee_id", assigneeId, false);
addFormParam(formData, "target_project_id", targetProjectId, false);
addFormParam(formData, "labels", labels == null ? null : String.join(",", labels), false);
addFormParam(formData, "milestone_id", milestoneId, false);
addFormParam(formData, "remove_source_branch", removeSourceBranch, false);
addFormParam(formData, "squash", squash, false);
Response response = post(Response.Status.CREATED, formData, "projects", getProjectIdOrPath(projectIdOrPath), "merge_requests");
return (response.readEntity(MergeRequest.class));
MergeRequestParams params = new MergeRequestParams()
.withSourceBranch(sourceBranch)
.withTargetBranch(targetBranch)
.withTitle(title)
.withDescription(description)
.withAssigneeId(assigneeId)
.withTargetProjectId(targetProjectId)
.withLabels(labels)
.withMilestoneId(milestoneId)
.withRemoveSourceBranch(removeSourceBranch)
.withSquash(squash);
return(createMergeRequest(projectIdOrPath, params));
}
/**
* Creates a merge request and optionally assigns a reviewer to it.
* Creates a merge request.
*
* <pre><code>GitLab Endpoint: POST /projects/:id/merge_requests</code></pre>
*
......@@ -395,7 +412,19 @@ public class MergeRequestApi extends AbstractApi {
*/
public MergeRequest createMergeRequest(Object projectIdOrPath, String sourceBranch, String targetBranch, String title, String description, Integer assigneeId,
Integer targetProjectId, String[] labels, Integer milestoneId, Boolean removeSourceBranch) throws GitLabApiException {
return createMergeRequest(projectIdOrPath, sourceBranch, targetBranch, title, description, assigneeId, targetProjectId, labels, milestoneId, removeSourceBranch, null);
MergeRequestParams params = new MergeRequestParams()
.withSourceBranch(sourceBranch)
.withTargetBranch(targetBranch)
.withTitle(title)
.withDescription(description)
.withAssigneeId(assigneeId)
.withTargetProjectId(targetProjectId)
.withLabels(labels)
.withMilestoneId(milestoneId)
.withRemoveSourceBranch(removeSourceBranch);
return(createMergeRequest(projectIdOrPath, params));
}
/**
......@@ -414,54 +443,33 @@ public class MergeRequestApi extends AbstractApi {
*/
public MergeRequest createMergeRequest(Object projectIdOrPath, String sourceBranch, String targetBranch, String title, String description, Integer assigneeId)
throws GitLabApiException {
return createMergeRequest(projectIdOrPath, sourceBranch, targetBranch, title, description, assigneeId, null, null, null, null);
MergeRequestParams params = new MergeRequestParams()
.withSourceBranch(sourceBranch)
.withTargetBranch(targetBranch)
.withTitle(title)
.withDescription(description)
.withAssigneeId(assigneeId);
return(createMergeRequest(projectIdOrPath, params));
}
/**
* Updates an existing merge request. You can change branches, title, or even close the MR.
*
* <p>NOTE: GitLab API V4 uses IID (internal ID), V3 uses ID to identify the merge request.</p>
* Updates an existing merge request. You can change branches, title, or even close the merge request.
*
* <pre><code>GitLab Endpoint: PUT /projects/:id/merge_requests/:merge_request_iid</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param mergeRequestIid the internal ID of the merge request to update
* @param targetBranch the target branch, optional
* @param title the title for the merge request
* @param assigneeId the Assignee user ID, optional
* @param description the description of the merge request, optional
* @param stateEvent new state for the merge request, optional
* @param labels comma separated list of labels, optional
* @param milestoneId the ID of a milestone, optional
* @param removeSourceBranch Flag indicating if a merge request should remove the source
* branch when merging, optional
* @param squash Squash commits into a single commit when merging, optional
* @param discussionLocked Flag indicating if the merge request's discussion is locked, optional
* @param allowCollaboration Allow commits from members who can merge to the target branch,
* optional
* @param params a MergeRequestParams instance holding the info to update the merge request
* @return the updated merge request
* @throws GitLabApiException if any exception occurs
*/
public MergeRequest updateMergeRequest(Object projectIdOrPath, Integer mergeRequestIid,
String targetBranch, String title, Integer assigneeId, String description,
StateEvent stateEvent, String labels, Integer milestoneId, Boolean removeSourceBranch,
Boolean squash, Boolean discussionLocked, Boolean allowCollaboration)
throws GitLabApiException {
Form formData = new GitLabApiForm()
.withParam("target_branch", targetBranch)
.withParam("title", title)
.withParam("assignee_id", assigneeId)
.withParam("description", description)
.withParam("state_event", stateEvent)
.withParam("labels", labels)
.withParam("milestone_id", milestoneId)
.withParam("remove_source_branch", removeSourceBranch)
.withParam("squash", squash)
.withParam("discussion_locked", discussionLocked)
.withParam("allow_collaboration", allowCollaboration);
return updateMergeRequest(projectIdOrPath, mergeRequestIid, formData);
public MergeRequest updateMergeRequest(Object projectIdOrPath, Integer mergeRequestIid, MergeRequestParams params) throws GitLabApiException {
GitLabApiForm form = params.getForm(false);
Response response = put(Response.Status.OK, form.asMap(),
"projects", getProjectIdOrPath(projectIdOrPath), "merge_requests", mergeRequestIid);
return (response.readEntity(MergeRequest.class));
}
/**
......@@ -480,68 +488,40 @@ public class MergeRequestApi extends AbstractApi {
* @param stateEvent new state for the merge request, optional
* @param labels comma separated list of labels, optional
* @param milestoneId the ID of a milestone, optional
* @param removeSourceBranch Flag indicating if a merge request should remove the source
* branch when merging, optional
* @param squash Squash commits into a single commit when merging, optional
* @param discussionLocked Flag indicating if the merge request's discussion is locked, optional
* @param allowCollaboration Allow commits from members who can merge to the target branch,
* optional
* @return the updated merge request
* @throws GitLabApiException if any exception occurs
*/
@Deprecated
public MergeRequest updateMergeRequest(Object projectIdOrPath, Integer mergeRequestIid, String targetBranch,
String title, Integer assigneeId, String description, StateEvent stateEvent, String labels,
Integer milestoneId) throws GitLabApiException {
Form formData = new GitLabApiForm()
.withParam("target_branch", targetBranch)
.withParam("title", title)
.withParam("assignee_id", assigneeId)
.withParam("description", description)
.withParam("state_event", stateEvent)
.withParam("labels", labels)
.withParam("milestone_id", milestoneId);
return updateMergeRequest(projectIdOrPath, mergeRequestIid, formData);
}
/**
* Updates an existing merge request. You can change branches, title, or even close the MR.
*
* <p>NOTE: GitLab API V4 uses IID (internal ID), V3 uses ID to identify the merge request.</p>
*
* <pre><code>GitLab Endpoint: PUT /projects/:id/merge_requests/:merge_request_iid</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param mergeRequestIid the internal ID of the merge request to update
* @param sourceBranch the source branch
* @param targetBranch the target branch
* @param title the title for the merge request
* @param description the description of the merge request
* @param assigneeId the Assignee user ID, optional
* @return the updated merge request
* @throws GitLabApiException if any exception occurs
* @deprecated as of release 4.4.3
*/
@Deprecated
public MergeRequest updateMergeRequest(Object projectIdOrPath, Integer mergeRequestIid, String sourceBranch, String targetBranch, String title, String description,
Integer assigneeId) throws GitLabApiException {
Form formData = new Form();
addFormParam(formData, "source_branch", sourceBranch, false);
addFormParam(formData, "target_branch", targetBranch, false);
addFormParam(formData, "title", title, false);
addFormParam(formData, "description", description, false);
addFormParam(formData, "assignee_id", assigneeId, false);
return updateMergeRequest(projectIdOrPath, mergeRequestIid, formData);
}
protected MergeRequest updateMergeRequest(Object projectIdOrPath, Integer mergeRequestIid,
Form formData) throws GitLabApiException {
if (mergeRequestIid == null) {
throw new RuntimeException("mergeRequestId cannot be null");
}
public MergeRequest updateMergeRequest(Object projectIdOrPath, Integer mergeRequestIid,
String targetBranch, String title, Integer assigneeId, String description,
StateEvent stateEvent, String labels, Integer milestoneId, Boolean removeSourceBranch,
Boolean squash, Boolean discussionLocked, Boolean allowCollaboration)
throws GitLabApiException {
Response response = put(Response.Status.OK, formData.asMap(), "projects", getProjectIdOrPath(projectIdOrPath),
"merge_requests", mergeRequestIid);
return (response.readEntity(MergeRequest.class));
String[] labelsArray = null;
if (labels != null) {
labelsArray = labels.split(",", -1);
}
MergeRequestParams params = new MergeRequestParams()
.withTargetBranch(targetBranch)
.withTitle(title)
.withAssigneeId(assigneeId)
.withDescription(description)
.withStateEvent(stateEvent)
.withLabels(labelsArray)
.withMilestoneId(milestoneId)
.withRemoveSourceBranch(removeSourceBranch)
.withDiscussionLocked(discussionLocked)
.withAllowCollaboration(allowCollaboration)
.withSquash(squash);
return (updateMergeRequest(projectIdOrPath, mergeRequestIid, params));
}
/**
......
package org.gitlab4j.api.models;
import java.util.Arrays;
import java.util.List;
import org.gitlab4j.api.Constants.StateEvent;
import org.gitlab4j.api.GitLabApiForm;
/**
* This class provides the form parameters for creating and updating merge requests.
*/
public class MergeRequestParams {
private String sourceBranch;
private String targetBranch;
private String title;
private Integer assigneeId;
private List<Integer> assigneeIds;
private Integer milestoneId;
private List<String> labels;
private String description;
private Integer targetProjectId;
private StateEvent stateEvent;
private Boolean removeSourceBranch;
private Boolean squash;
private Boolean discussionLocked;
private Boolean allowCollaboration;
private Integer approvalsBeforeMerge;
/**
* Set the source branch. This is for merge request creation only.
*
* @param sourceBranch the sourceBranch to set
* @return the reference to this MergeRequestParams instance
*/
public MergeRequestParams withSourceBranch(String sourceBranch) {
this.sourceBranch = sourceBranch;
return (this);
}
/**
* Set the target branch.
*
* @param targetBranch the targetBranch to set
* @return the reference to this MergeRequestParams instance
*/
public MergeRequestParams withTargetBranch(String targetBranch) {
this.targetBranch = targetBranch;
return (this);
}
/**
* Set the title of the merge request.
*
* @param title the title to set
* @return the reference to this MergeRequestParams instance
*/
public MergeRequestParams withTitle(String title) {
this.title = title;
return (this);
}
/**
* Set the assignee user ID.
*
* @param assigneeId the assigneeId to set
* @return the reference to this MergeRequestParams instance
*/
public MergeRequestParams withAssigneeId(Integer assigneeId) {
this.assigneeId = assigneeId;
return (this);
}
/**
* The ID of the user(s) to assign the merge request to. Set to 0 or provide
* an empty value to unassign all assignees.
*
* @param assigneeIds the assigneeIds to set
* @return the reference to this MergeRequestParams instance
*/
public MergeRequestParams withAssigneeIds(List<Integer> assigneeIds) {
this.assigneeIds = assigneeIds;
return (this);
}
/**
* Set the milestone ID field value.
*
* @param milestoneId the milestoneId to set
* @return the reference to this MergeRequestParams instance
*/
public MergeRequestParams withMilestoneId(Integer milestoneId) {
this.milestoneId = milestoneId;
return (this);
}
/**
* Set the labels for the merge request.
*
* @param labels the List of labels to set
* @return the reference to this MergeRequestParams instance
*/
public MergeRequestParams withLabels(List<String> labels) {
this.labels = labels;
return (this);
}
/**
* Set the labels for the merge request.
*
* @param labels the array of labels to set
* @return the reference to this MergeRequestParams instance
*/
public MergeRequestParams withLabels(String[] labels) {
this.labels = (labels != null ? Arrays.asList(labels) : null);
return (this);
}
/**
* Set the description of the merge request. Limited to 1,048,576 characters.
*
* @param description the description to set
* @return the reference to this MergeRequestParams instance
*/
public MergeRequestParams withDescription(String description) {
this.description = description;
return (this);
}
/**
* Set the target project ID. This is for merge request creation only.
*
* @param targetProjectId the targetProjectId to set
* @return the reference to this MergeRequestParams instance
*/
public MergeRequestParams withTargetProjectId(Integer targetProjectId) {
this.targetProjectId = targetProjectId;
return (this);
}
/**
* New state (close/reopen). This is for merge request updates only.
*
* @param stateEvent the stateEvent to set
* @return the reference to this MergeRequestParams instance
*/
public MergeRequestParams withStateEvent(StateEvent stateEvent) {
this.stateEvent = stateEvent;
return (this);
}
/**
* Flag indicating if a merge request should remove the source branch when merging.
*
* @param removeSourceBranch the removeSourceBranch to set
* @return the reference to this MergeRequestParams instance
*/
public MergeRequestParams withRemoveSourceBranch(Boolean removeSourceBranch) {
this.removeSourceBranch = removeSourceBranch;
return (this);
}
/**
* Squash commits into a single commit when merging.
*
* @param squash the squash to set
* @return the reference to this MergeRequestParams instance
*/
public MergeRequestParams withSquash(Boolean squash) {
this.squash = squash;
return (this);
}
/**
* Flag indicating if the merge request’s discussion is locked. If the discussion is locked only
* project members can add, edit or resolve comments. This is for merge request updates only.
*
* @param discussionLocked the discussionLocked to set
* @return the reference to this MergeRequestParams instance
*/
public MergeRequestParams withDiscussionLocked(Boolean discussionLocked) {
this.discussionLocked = discussionLocked;
return (this);
}
/**
* Allow commits from members who can merge to the target branch.
*
* @param allowCollaboration the allowCollaboration to set
* @return the reference to this MergeRequestParams instance
*/
public MergeRequestParams withAllowCollaboration(Boolean allowCollaboration) {
this.allowCollaboration = allowCollaboration;
return (this);
}
/**
* Set the approvals_before_merge field value. This is for merge request creation only.
*
* @param approvalsBeforeMerge the approvalsBeforeMerge to set
* @return the reference to this MergeRequestParams instance
*/
public MergeRequestParams withApprovalsBeforeMerge(Integer approvalsBeforeMerge) {
this.approvalsBeforeMerge = approvalsBeforeMerge;
return (this);
}
/**
* Get the form params specified by this instance.
*
* @param isCreate set to true if this is for a create merge request API call,
* set to false if this is for an update merge request
* @return a GitLabApiForm instance holding the form parameters for this MergeRequestParams instance
*/
public GitLabApiForm getForm(boolean isCreate) {
GitLabApiForm form = new GitLabApiForm()
.withParam("target_branch", targetBranch, isCreate)
.withParam("title", title, isCreate)
.withParam("assignee_id", assigneeId)
.withParam("assignee_ids", assigneeIds)
.withParam("milestone_id", milestoneId)
.withParam("labels", (labels != null ? String.join(",", labels) : null))
.withParam("description", description)
.withParam("remove_source_branch", removeSourceBranch)
.withParam("squash", squash)
.withParam("allow_collaboration", allowCollaboration);
if (isCreate) {
form.withParam("source_branch", sourceBranch, true)
.withParam("target_project_id", targetProjectId)
.withParam("approvals_before_merge", approvalsBeforeMerge);
} else {
form.withParam("state_event", stateEvent)
.withParam("discussion_locked", discussionLocked);
}
return (form);
}
}
package org.gitlab4j.api;
import static org.hamcrest.Matchers.hasEntry;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assume.assumeNotNull;
import java.util.Collections;
import javax.ws.rs.core.MultivaluedMap;
import java.util.Optional;
import java.util.stream.Stream;
import org.gitlab4j.api.models.Branch;
import org.gitlab4j.api.models.MergeRequest;
import org.gitlab4j.api.models.MergeRequestParams;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.RepositoryFile;
import org.gitlab4j.api.models.User;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.junit.experimental.categories.Category;
import org.junit.runners.MethodSorters;
public class TestMergeRequestApi {
/**
* In order for these tests to run you must set the following properties in test-gitlab4j.properties
*
* TEST_NAMESPACE
* TEST_PROJECT_NAME
* TEST_HOST_URL
* TEST_PRIVATE_TOKEN
*
* If any of the above are NULL, all tests in this class will be skipped.
*
* NOTE: &amp;FixMethodOrder(MethodSorters.NAME_ASCENDING) is very important to insure that testCreate() is executed first.
*/
@Category(IntegrationTest.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestMergeRequestApi extends AbstractIntegrationTest {
@Mock private GitLabApi gitLabApi;
@Mock private GitLabApiClient gitLabApiClient;
@Captor private ArgumentCaptor<MultivaluedMap<String, String>> attributeCaptor;
private MockResponse response;
private static final String TEST_BRANCH_NAME = "feature/gitlab4j-merge-request-test";
private static final String TEST_MR_TITLE = "Merge Request test: gitlab4j-merge-request-test";
private static final String TEST_DESCRIPTION = "Description for Merge Request test";
@SuppressWarnings("deprecation")
@Before
public void setUp() throws Exception {
initMocks(this);
response = new MockResponse(MergeRequest.class, "merge-request.json", null);
when(gitLabApi.getApiClient()).thenReturn(gitLabApiClient);
when(gitLabApiClient.validateSecretToken(any())).thenReturn(true);
when(gitLabApiClient.put(attributeCaptor.capture(), Mockito.<Object>anyVararg()))
.thenReturn(response);
private static GitLabApi gitLabApi;
private static Project testProject;
private static User currentUser;
public TestMergeRequestApi() {
super();
}
@Test
public void whenAllArgumentsNull_thenNoAttributesSent() throws Exception {
new MergeRequestApi(gitLabApi).updateMergeRequest(1, 2, null, null, null, null, null, null,
null, null, null, null, null);
assertEquals(0, attributeCaptor.getValue().size());
@BeforeClass
public static void setup() {
// Must setup the connection to the GitLab test server
gitLabApi = baseTestSetup();
testProject = getTestProject();
currentUser = getCurrentUser();
teardown();
}
@Test
public void falseBooleansAreSerializedCorrectly() throws Exception {
new MergeRequestApi(gitLabApi).updateMergeRequest(1, 2, null, null, null, null, null, null,
null, null, null, null, false);
assertThat(attributeCaptor.getValue(),
hasEntry("allow_collaboration", Collections.singletonList("false")));
@AfterClass
public static void teardown() {
if (testProject == null) {
return;
}
try {
Stream<MergeRequest> mergeRequests = gitLabApi.getMergeRequestApi().getMergeRequestsStream(testProject);
MergeRequest mergeRequest = mergeRequests.filter(
m -> TEST_MR_TITLE.equals(m.getTitle())).findFirst().orElse(null);
if (mergeRequest != null) {
gitLabApi.getMergeRequestApi().deleteMergeRequest(testProject, mergeRequest.getIid());
}
} catch (GitLabApiException ignore) {
}
try {
gitLabApi.getRepositoryApi().deleteBranch(testProject, TEST_BRANCH_NAME);
} catch (GitLabApiException ignore) {
}
}
@Before
public void beforeMethod() {
assumeNotNull(testProject);
}
@Test
public void trueBooleansAreSerializedCorrectly() throws Exception {
new MergeRequestApi(gitLabApi).updateMergeRequest(1, 2, null, null, null, null, null, null,
null, null, null, null, true);
assertThat(attributeCaptor.getValue(),
hasEntry("allow_collaboration", Collections.singletonList("true")));
public void testCreateAndUpdateMergeRequest() throws GitLabApiException {
// Create a test branch
Branch branch = gitLabApi.getRepositoryApi().createBranch(testProject, TEST_BRANCH_NAME, "master");
assertNotNull(branch);
// Create a new file in the test branch
RepositoryFile repoFile = new RepositoryFile();
repoFile.setFilePath("README-FOR-TESTING-MERGE-REQUEST.md");
repoFile.setContent("This is content");
gitLabApi.getRepositoryFileApi().createFile(testProject, repoFile, TEST_BRANCH_NAME, "Initial commit.");
MergeRequest mr = null;
try {
MergeRequestParams params = new MergeRequestParams()
.withSourceBranch(TEST_BRANCH_NAME)
.withTargetBranch("master")
.withTitle(TEST_MR_TITLE);
mr = gitLabApi.getMergeRequestApi().createMergeRequest(testProject, params);
assertEquals(TEST_MR_TITLE, mr.getTitle());
params = new MergeRequestParams()
.withAssigneeId(currentUser.getId())
.withDescription(TEST_DESCRIPTION)
.withDiscussionLocked(true);
MergeRequest updatedMr = gitLabApi.getMergeRequestApi().updateMergeRequest(testProject, mr.getIid(), params);
assertEquals(currentUser.getId(), updatedMr.getAssignee().getId());
assertEquals(TEST_DESCRIPTION, updatedMr.getDescription());
assertEquals(true, updatedMr.getDiscussionLocked());
gitLabApi.getMergeRequestApi().deleteMergeRequest(testProject, mr.getIid());
Optional<MergeRequest> deletedMr =
gitLabApi.getMergeRequestApi().getOptionalMergeRequest(testProject, mr.getIid());
mr = null;
assertFalse(deletedMr.isPresent());
} finally {
if (mr != null) {
try {
gitLabApi.getMergeRequestApi().deleteMergeRequest(testProject, mr.getIid());
} catch (Exception ignore) {
}
}
try {
gitLabApi.getRepositoryApi().deleteBranch(testProject, TEST_BRANCH_NAME);
} catch (GitLabApiException ignore) {
}
}
}
}
package org.gitlab4j.api;
import static org.hamcrest.Matchers.hasEntry;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import java.util.Collections;
import javax.ws.rs.core.MultivaluedMap;
import org.gitlab4j.api.models.MergeRequest;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
public class TestUnitMergeRequestApi {
@Mock private GitLabApi mockGitLabApi;
@Mock private GitLabApiClient mockedGitLabApiClient;
@Captor private ArgumentCaptor<MultivaluedMap<String, String>> attributeCaptor;
private MockResponse mockedResponse;
@Before
public void setUp() throws Exception {
initMocks(this);
mockedResponse = new MockResponse(MergeRequest.class, "merge-request.json", null);
when(mockGitLabApi.getApiClient()).thenReturn(mockedGitLabApiClient);
when(mockedGitLabApiClient.validateSecretToken(any())).thenReturn(true);
when(mockedGitLabApiClient.put(attributeCaptor.capture(), Mockito.<Object>any()))
.thenReturn(mockedResponse);
}
@Test
public void whenAllArgumentsNull_thenNoAttributesSent() throws Exception {
new MergeRequestApi(mockGitLabApi).updateMergeRequest(1, 2, null, null, null, null, null, null,
null, null, null, null, null);
assertEquals(0, attributeCaptor.getValue().size());
}
@Test
public void falseBooleansAreSerializedCorrectly() throws Exception {
new MergeRequestApi(mockGitLabApi).updateMergeRequest(1, 2, null, null, null, null, null, null,
null, null, null, null, false);
assertThat(attributeCaptor.getValue(),
hasEntry("allow_collaboration", Collections.singletonList("false")));
}
@Test
public void trueBooleansAreSerializedCorrectly() throws Exception {
new MergeRequestApi(mockGitLabApi).updateMergeRequest(1, 2, null, null, null, null, null, null,
null, null, null, null, true);
assertThat(attributeCaptor.getValue(),
hasEntry("allow_collaboration", Collections.singletonList("true")));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment