|
| 1 | +package com.gitee.jenkins.webhook; |
| 2 | + |
| 3 | +import static org.hamcrest.CoreMatchers.instanceOf; |
| 4 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 5 | +import static org.mockito.Mockito.when; |
| 6 | + |
| 7 | +import com.gitee.jenkins.webhook.ActionResolver.NoopAction; |
| 8 | +import com.gitee.jenkins.webhook.build.PullRequestBuildAction; |
| 9 | +import com.gitee.jenkins.webhook.build.NoteBuildAction; |
| 10 | +import com.gitee.jenkins.webhook.build.PushBuildAction; |
| 11 | +import com.gitee.jenkins.webhook.status.BranchBuildPageRedirectAction; |
| 12 | +import com.gitee.jenkins.webhook.status.BranchStatusPngAction; |
| 13 | +import com.gitee.jenkins.webhook.status.CommitBuildPageRedirectAction; |
| 14 | +import com.gitee.jenkins.webhook.status.CommitStatusPngAction; |
| 15 | +import com.gitee.jenkins.webhook.status.StatusJsonAction; |
| 16 | +import jakarta.servlet.ReadListener; |
| 17 | +import jakarta.servlet.ServletInputStream; |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.InputStream; |
| 20 | +import org.junit.jupiter.api.BeforeAll; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 23 | +import org.jvnet.hudson.test.JenkinsRule; |
| 24 | +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; |
| 25 | +import org.kohsuke.stapler.StaplerRequest2; |
| 26 | +import org.mockito.Mock; |
| 27 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 28 | +import org.mockito.junit.jupiter.MockitoSettings; |
| 29 | +import org.mockito.quality.Strictness; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Robin Müller |
| 33 | + */ |
| 34 | +@WithJenkins |
| 35 | +@ExtendWith(MockitoExtension.class) |
| 36 | +@MockitoSettings(strictness = Strictness.LENIENT) |
| 37 | +class ActionResolverTest { |
| 38 | + |
| 39 | + private static JenkinsRule jenkins; |
| 40 | + |
| 41 | + @Mock |
| 42 | + private StaplerRequest2 request; |
| 43 | + |
| 44 | + @BeforeAll |
| 45 | + static void setUp(JenkinsRule rule) { |
| 46 | + jenkins = rule; |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void getBranchBuildPageRedirect() throws Exception { |
| 51 | + String projectName = "getBranchBuildPageRedirect"; |
| 52 | + jenkins.createFreeStyleProject(projectName); |
| 53 | + when(request.getRestOfPath()).thenReturn(""); |
| 54 | + when(request.hasParameter("ref")).thenReturn(true); |
| 55 | + when(request.getMethod()).thenReturn("GET"); |
| 56 | + |
| 57 | + WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request); |
| 58 | + |
| 59 | + assertThat(resolvedAction, instanceOf(BranchBuildPageRedirectAction.class)); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void getCommitStatus() throws Exception { |
| 64 | + String projectName = "getCommitStatus"; |
| 65 | + jenkins.createFreeStyleProject(projectName); |
| 66 | + when(request.getRestOfPath()).thenReturn("builds/1234abcd/status.json"); |
| 67 | + when(request.getMethod()).thenReturn("GET"); |
| 68 | + |
| 69 | + WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request); |
| 70 | + |
| 71 | + assertThat(resolvedAction, instanceOf(StatusJsonAction.class)); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void getCommitBuildPageRedirect_builds() throws Exception { |
| 76 | + String projectName = "getCommitBuildPageRedirect_builds"; |
| 77 | + jenkins.createFreeStyleProject(projectName); |
| 78 | + when(request.getRestOfPath()).thenReturn("builds/1234abcd"); |
| 79 | + when(request.getMethod()).thenReturn("GET"); |
| 80 | + |
| 81 | + WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request); |
| 82 | + |
| 83 | + assertThat(resolvedAction, instanceOf(CommitBuildPageRedirectAction.class)); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void getCommitBuildPageRedirect_commits() throws Exception { |
| 88 | + String projectName = "getCommitBuildPageRedirect_commits"; |
| 89 | + jenkins.createFreeStyleProject(projectName); |
| 90 | + when(request.getRestOfPath()).thenReturn("commits/7890efab"); |
| 91 | + when(request.getMethod()).thenReturn("GET"); |
| 92 | + |
| 93 | + WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request); |
| 94 | + |
| 95 | + assertThat(resolvedAction, instanceOf(CommitBuildPageRedirectAction.class)); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void getBranchStatusPng() throws Exception { |
| 100 | + String projectName = "getBranchStatusPng"; |
| 101 | + jenkins.createFreeStyleProject(projectName); |
| 102 | + when(request.getRestOfPath()).thenReturn("builds/status.png"); |
| 103 | + when(request.hasParameter("ref")).thenReturn(true); |
| 104 | + when(request.getMethod()).thenReturn("GET"); |
| 105 | + |
| 106 | + WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request); |
| 107 | + |
| 108 | + assertThat(resolvedAction, instanceOf(BranchStatusPngAction.class)); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void getCommitStatusPng() throws Exception { |
| 113 | + String projectName = "getCommitStatusPng"; |
| 114 | + jenkins.createFreeStyleProject(projectName); |
| 115 | + when(request.getRestOfPath()).thenReturn("builds/status.png"); |
| 116 | + when(request.hasParameter("ref")).thenReturn(false); |
| 117 | + when(request.getMethod()).thenReturn("GET"); |
| 118 | + |
| 119 | + WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request); |
| 120 | + |
| 121 | + assertThat(resolvedAction, instanceOf(CommitStatusPngAction.class)); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void postMergeRequest() throws Exception { |
| 126 | + String projectName = "postMergeRequest"; |
| 127 | + jenkins.createFreeStyleProject(projectName); |
| 128 | + when(request.getRestOfPath()).thenReturn(""); |
| 129 | + when(request.getMethod()).thenReturn("POST"); |
| 130 | + when(request.getHeader("X-Gitee-Event")).thenReturn("Merge Request Hook"); |
| 131 | + when(request.getInputStream()) |
| 132 | + .thenReturn(new ResourceServletInputStream("ActionResolverTest_pullRequest.json")); |
| 133 | + |
| 134 | + WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request); |
| 135 | + |
| 136 | + assertThat(resolvedAction, instanceOf(PullRequestBuildAction.class)); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + void postNote() throws Exception { |
| 141 | + String projectName = "postNote"; |
| 142 | + jenkins.createFreeStyleProject(projectName); |
| 143 | + when(request.getRestOfPath()).thenReturn(""); |
| 144 | + when(request.getMethod()).thenReturn("POST"); |
| 145 | + when(request.getHeader("X-Gitee-Event")).thenReturn("Note Hook"); |
| 146 | + when(request.getInputStream()).thenReturn(new ResourceServletInputStream("ActionResolverTest_postNote.json")); |
| 147 | + |
| 148 | + WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request); |
| 149 | + |
| 150 | + assertThat(resolvedAction, instanceOf(NoteBuildAction.class)); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + void postPush() throws Exception { |
| 155 | + String projectName = "postPush"; |
| 156 | + jenkins.createFreeStyleProject(projectName); |
| 157 | + when(request.getRestOfPath()).thenReturn(""); |
| 158 | + when(request.getMethod()).thenReturn("POST"); |
| 159 | + when(request.getHeader("X-Gitee-Event")).thenReturn("Push Hook"); |
| 160 | + when(request.getInputStream()).thenReturn(new ResourceServletInputStream("ActionResolverTest_postPush.json")); |
| 161 | + |
| 162 | + WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request); |
| 163 | + |
| 164 | + assertThat(resolvedAction, instanceOf(PushBuildAction.class)); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + void postPushTag() throws Exception { |
| 169 | + String projectName = "postPushTag"; |
| 170 | + jenkins.createFreeStyleProject(projectName); |
| 171 | + when(request.getRestOfPath()).thenReturn(""); |
| 172 | + when(request.getMethod()).thenReturn("POST"); |
| 173 | + when(request.getHeader("X-Gitee-Event")).thenReturn("Tag Push Hook"); |
| 174 | + when(request.getInputStream()) |
| 175 | + .thenReturn(new ResourceServletInputStream("ActionResolverTest_postPushTag.json")); |
| 176 | + |
| 177 | + WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request); |
| 178 | + |
| 179 | + assertThat(resolvedAction, instanceOf(PushBuildAction.class)); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + void postPushMissingEventHeader() throws Exception { |
| 184 | + String projectName = "postPushMissingEventHeader"; |
| 185 | + jenkins.createFreeStyleProject(projectName); |
| 186 | + when(request.getRestOfPath()).thenReturn(""); |
| 187 | + when(request.getMethod()).thenReturn("POST"); |
| 188 | + when(request.getHeader("X-Gitee-Event")).thenReturn(null); |
| 189 | + when(request.getInputStream()).thenReturn(new ResourceServletInputStream("ActionResolverTest_postPush.json")); |
| 190 | + |
| 191 | + WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request); |
| 192 | + |
| 193 | + assertThat(resolvedAction, instanceOf(NoopAction.class)); |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + void postPushUnsupportedEventHeader() throws Exception { |
| 198 | + String projectName = "postPushUnsupportedEventHeader"; |
| 199 | + jenkins.createFreeStyleProject(projectName); |
| 200 | + when(request.getRestOfPath()).thenReturn(""); |
| 201 | + when(request.getMethod()).thenReturn("POST"); |
| 202 | + when(request.getHeader("X-Gitee-Event")).thenReturn("__Not Supported Header__"); |
| 203 | + when(request.getInputStream()).thenReturn(new ResourceServletInputStream("ActionResolverTest_postPush.json")); |
| 204 | + |
| 205 | + WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request); |
| 206 | + |
| 207 | + assertThat(resolvedAction, instanceOf(NoopAction.class)); |
| 208 | + } |
| 209 | + |
| 210 | + private static class ResourceServletInputStream extends ServletInputStream { |
| 211 | + |
| 212 | + private final InputStream input; |
| 213 | + |
| 214 | + private ResourceServletInputStream(String classResourceName) { |
| 215 | + this.input = getClass().getResourceAsStream(classResourceName); |
| 216 | + } |
| 217 | + |
| 218 | + @Override |
| 219 | + public int read() throws IOException { |
| 220 | + return input.read(); |
| 221 | + } |
| 222 | + |
| 223 | + @Override |
| 224 | + public boolean isReady() { |
| 225 | + return true; |
| 226 | + } |
| 227 | + |
| 228 | + @Override |
| 229 | + public boolean isFinished() { |
| 230 | + return true; |
| 231 | + } |
| 232 | + |
| 233 | + @Override |
| 234 | + public void setReadListener(ReadListener var1) {} |
| 235 | + } |
| 236 | +} |
0 commit comments