|
| 1 | +package com.dabsquared.gitlabjenkins.publisher; |
| 2 | + |
| 3 | +import hudson.model.AbstractBuild; |
| 4 | +import hudson.model.BuildListener; |
| 5 | +import hudson.model.Result; |
| 6 | +import hudson.model.StreamBuildListener; |
| 7 | +import org.junit.*; |
| 8 | +import org.jvnet.hudson.test.JenkinsRule; |
| 9 | +import org.mockserver.client.server.MockServerClient; |
| 10 | +import org.mockserver.junit.MockServerRule; |
| 11 | +import org.mockserver.model.HttpRequest; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.UnsupportedEncodingException; |
| 15 | +import java.nio.charset.Charset; |
| 16 | + |
| 17 | +import static com.dabsquared.gitlabjenkins.publisher.TestUtility.*; |
| 18 | +import static org.mockserver.model.HttpRequest.request; |
| 19 | +import static org.mockserver.model.HttpResponse.response; |
| 20 | + |
| 21 | +public class GitLabApproveMergeRequestPublisherTest { |
| 22 | + |
| 23 | + @ClassRule |
| 24 | + public static MockServerRule mockServer = new MockServerRule(new Object()); |
| 25 | + |
| 26 | + @ClassRule |
| 27 | + public static JenkinsRule jenkins = new JenkinsRule(); |
| 28 | + |
| 29 | + private MockServerClient mockServerClient; |
| 30 | + private BuildListener listener; |
| 31 | + |
| 32 | + @BeforeClass |
| 33 | + public static void setupClass() throws IOException { |
| 34 | + setupGitLabConnections(jenkins, mockServer); |
| 35 | + } |
| 36 | + |
| 37 | + @Before |
| 38 | + public void setup() { |
| 39 | + listener = new StreamBuildListener(jenkins.createTaskListener().getLogger(), Charset.defaultCharset()); |
| 40 | + mockServerClient = new MockServerClient("localhost", mockServer.getPort()); |
| 41 | + } |
| 42 | + |
| 43 | + @After |
| 44 | + public void cleanup() { |
| 45 | + mockServerClient.reset(); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void matrixAggregatable() throws InterruptedException, IOException { |
| 50 | + verifyMatrixAggregatable(GitLabApproveMergeRequestPublisher.class, listener); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void success_approve_unstable_v4() throws IOException, InterruptedException { |
| 55 | + performApprovalAndVerify(mockSimpleBuild(GITLAB_CONNECTION_V4, Result.SUCCESS), "v4", MERGE_REQUEST_IID, true); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void success_v4() throws IOException, InterruptedException { |
| 60 | + performApprovalAndVerify(mockSimpleBuild(GITLAB_CONNECTION_V4, Result.SUCCESS), "v4", MERGE_REQUEST_IID, false); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void unstable_approve_unstable_v4() throws IOException, InterruptedException { |
| 65 | + performApprovalAndVerify(mockSimpleBuild(GITLAB_CONNECTION_V4, Result.UNSTABLE), "v4", MERGE_REQUEST_IID, true); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void unstable_dontapprove_v4() throws IOException, InterruptedException { |
| 70 | + performUnapprovalAndVerify(mockSimpleBuild(GITLAB_CONNECTION_V4, Result.UNSTABLE), "v4", MERGE_REQUEST_IID, false); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void failed_approve_unstable_v4() throws IOException, InterruptedException { |
| 75 | + performUnapprovalAndVerify(mockSimpleBuild(GITLAB_CONNECTION_V4, Result.FAILURE), "v4", MERGE_REQUEST_IID, true); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void failed_v4() throws IOException, InterruptedException { |
| 80 | + performUnapprovalAndVerify(mockSimpleBuild(GITLAB_CONNECTION_V4, Result.FAILURE), "v4", MERGE_REQUEST_IID, false); |
| 81 | + } |
| 82 | + |
| 83 | + private void performApprovalAndVerify(AbstractBuild build, String apiLevel, int mergeRequestId, boolean approveUnstable) throws InterruptedException, IOException { |
| 84 | + GitLabApproveMergeRequestPublisher publisher = preparePublisher(new GitLabApproveMergeRequestPublisher(approveUnstable), build); |
| 85 | + publisher.perform(build, null, listener); |
| 86 | + |
| 87 | + mockServerClient.verify(prepareSendApprovalWithSuccessResponse(build, apiLevel, mergeRequestId)); |
| 88 | + } |
| 89 | + |
| 90 | + private void performUnapprovalAndVerify(AbstractBuild build, String apiLevel, int mergeRequestId, boolean approveUnstable) throws InterruptedException, IOException { |
| 91 | + GitLabApproveMergeRequestPublisher publisher = preparePublisher(new GitLabApproveMergeRequestPublisher(approveUnstable), build); |
| 92 | + publisher.perform(build, null, listener); |
| 93 | + |
| 94 | + mockServerClient.verify(prepareSendUnapprovalWithSuccessResponse(build, apiLevel, mergeRequestId)); |
| 95 | + } |
| 96 | + |
| 97 | + private HttpRequest prepareSendApprovalWithSuccessResponse(AbstractBuild build, String apiLevel, int mergeRequestId) throws UnsupportedEncodingException { |
| 98 | + HttpRequest approvalRequest = prepareSendApproval(apiLevel, mergeRequestId); |
| 99 | + mockServerClient.when(approvalRequest).respond(response().withStatusCode(200)); |
| 100 | + return approvalRequest; |
| 101 | + } |
| 102 | + |
| 103 | + private HttpRequest prepareSendUnapprovalWithSuccessResponse(AbstractBuild build, String apiLevel, int mergeRequestId) throws UnsupportedEncodingException { |
| 104 | + HttpRequest unapprovalRequest = prepareSendUnapproval(apiLevel, mergeRequestId); |
| 105 | + mockServerClient.when(unapprovalRequest).respond(response().withStatusCode(200)); |
| 106 | + return unapprovalRequest; |
| 107 | + } |
| 108 | + |
| 109 | + private HttpRequest prepareSendApproval(final String apiLevel, int mergeRequestId) throws UnsupportedEncodingException { |
| 110 | + return request() |
| 111 | + .withPath("/gitlab/api/" + apiLevel + "/projects/" + PROJECT_ID + "/merge_requests/" + mergeRequestId + "/approve") |
| 112 | + .withMethod("POST") |
| 113 | + .withHeader("PRIVATE-TOKEN", "secret"); |
| 114 | + } |
| 115 | + |
| 116 | + private HttpRequest prepareSendUnapproval(final String apiLevel, int mergeRequestId) throws UnsupportedEncodingException { |
| 117 | + return request() |
| 118 | + .withPath("/gitlab/api/" + apiLevel + "/projects/" + PROJECT_ID + "/merge_requests/" + mergeRequestId + "/unapprove") |
| 119 | + .withMethod("POST") |
| 120 | + .withHeader("PRIVATE-TOKEN", "secret"); |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments