-
Notifications
You must be signed in to change notification settings - Fork 616
Expand file tree
/
Copy pathNoteBuildActionTest.java
More file actions
155 lines (136 loc) · 6.29 KB
/
Copy pathNoteBuildActionTest.java
File metadata and controls
155 lines (136 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package com.dabsquared.gitlabjenkins.webhook.build;
import static com.dabsquared.gitlabjenkins.cause.CauseDataBuilder.causeData;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import com.dabsquared.gitlabjenkins.GitLabPushTrigger;
import com.dabsquared.gitlabjenkins.cause.CauseData;
import com.dabsquared.gitlabjenkins.cause.GitLabWebHookCause;
import com.dabsquared.gitlabjenkins.gitlab.hook.model.NoteHook;
import hudson.model.FreeStyleProject;
import hudson.model.ParametersAction;
import hudson.model.StringParameterValue;
import hudson.model.queue.QueueTaskFuture;
import hudson.plugins.git.GitSCM;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import org.apache.commons.io.IOUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.jvnet.hudson.test.JenkinsRule;
import org.kohsuke.stapler.HttpResponses;
import org.kohsuke.stapler.StaplerResponse;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
/**
* @author Nikolay Ustinov
*/
@RunWith(MockitoJUnitRunner.class)
public class NoteBuildActionTest {
@ClassRule
public static JenkinsRule jenkins = new JenkinsRule();
@Rule
public TemporaryFolder tmp = new TemporaryFolder();
@Rule
public ExpectedException exception = ExpectedException.none();
@Mock
private StaplerResponse response;
@Mock
private GitLabPushTrigger trigger;
private String gitRepoUrl;
private String commitSha1;
@Before
public void setup() throws Exception {
Git.init().setDirectory(tmp.getRoot()).call();
tmp.newFile("test");
Git git = Git.open(tmp.getRoot());
git.add().addFilepattern("test");
RevCommit commit = git.commit().setSign(false).setMessage("test").call();
commitSha1 = commit.getId().getName();
gitRepoUrl = tmp.getRoot().toURI().toString();
}
@Test
public void build() throws IOException {
try {
FreeStyleProject testProject = jenkins.createFreeStyleProject();
testProject.addTrigger(trigger);
exception.expect(HttpResponses.HttpResponseException.class);
new NoteBuildAction(testProject, getJson("NoteEvent.json"), null).execute(response);
} finally {
ArgumentCaptor<NoteHook> noteHookArgumentCaptor = ArgumentCaptor.forClass(NoteHook.class);
verify(trigger).onPost(noteHookArgumentCaptor.capture());
assertThat(noteHookArgumentCaptor.getValue().getUser(), is(notNullValue()));
assertThat(noteHookArgumentCaptor.getValue().getUser().getName(), containsString("Administrator"));
assertThat(noteHookArgumentCaptor.getValue().getUser().getUsername(), containsString("root"));
}
}
@Test
public void build_alreadyBuiltMR_alreadyBuiltMR() throws IOException, ExecutionException, InterruptedException {
FreeStyleProject testProject = jenkins.createFreeStyleProject();
testProject.addTrigger(trigger);
testProject.setScm(new GitSCM(gitRepoUrl));
QueueTaskFuture<?> future = testProject.scheduleBuild2(
0, new ParametersAction(new StringParameterValue("gitlabTargetBranch", "master")));
if (future != null) {
future.get();
}
exception.expect(HttpResponses.HttpResponseException.class);
new NoteBuildAction(testProject, getJson("NoteEvent_alreadyBuiltMR.json"), null).execute(response);
verify(trigger).onPost(any(NoteHook.class));
}
@Test
public void build_alreadyBuiltMR_differentTargetBranch()
throws IOException, ExecutionException, InterruptedException {
FreeStyleProject testProject = jenkins.createFreeStyleProject();
testProject.addTrigger(trigger);
testProject.setScm(new GitSCM(gitRepoUrl));
QueueTaskFuture<?> future = testProject.scheduleBuild2(
0,
new GitLabWebHookCause(causeData()
.withActionType(CauseData.ActionType.NOTE)
.withSourceProjectId(1)
.withTargetProjectId(1)
.withBranch("feature")
.withSourceBranch("feature")
.withUserName("")
.withSourceRepoHomepage("https://gitlab.org/test")
.withSourceRepoName("test")
.withSourceNamespace("test-namespace")
.withSourceRepoUrl("git@gitlab.org:test.git")
.withSourceRepoSshUrl("git@gitlab.org:test.git")
.withSourceRepoHttpUrl("https://gitlab.org/test.git")
.withMergeRequestTitle("Test")
.withMergeRequestId(1)
.withMergeRequestIid(1)
.withTargetBranch("master")
.withTargetRepoName("test")
.withTargetNamespace("test-namespace")
.withTargetRepoSshUrl("git@gitlab.org:test.git")
.withTargetRepoHttpUrl("https://gitlab.org/test.git")
.withTriggeredByUser("test")
.withLastCommit("123")
.withTargetProjectUrl("https://gitlab.org/test")
.build()));
future.get();
exception.expect(HttpResponses.HttpResponseException.class);
new NoteBuildAction(testProject, getJson("NoteEvent_alreadyBuiltMR.json"), null).execute(response);
verify(trigger).onPost(any(NoteHook.class));
}
private String getJson(String name) throws IOException {
return IOUtils.toString(Objects.requireNonNull(getClass().getResourceAsStream(name)), StandardCharsets.UTF_8)
.replace("${commitSha1}", commitSha1);
}
}