8000 Test for JENKINS-32132 with rule to mock GH · github-cloud/github-plugin@4024d62 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4024d62

Browse files
committed
Test for JENKINS-32132 with rule to mock GH
1 parent 57d7870 commit 4024d62

File tree

11 files changed

+542
-38
lines changed

11 files changed

+542
-38
lines changed

src/test/java/com/cloudbees/jenkins/GitHubCommitNotifierTest.java

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,82 @@
1-
/*
2-
* To change this license header, choose License Headers in Project Properties.
3-
* To change this template file, choose Tools | Templates
4-
* and open the template in the editor.
5-
*/
6-
71
package com.cloudbees.jenkins;
82

3+
import com.github.tomakehurst.wiremock.common.Slf4jNotifier;
4+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
5+
import hudson.Launcher;
6+
import hudson.model.AbstractBuild;
97
import hudson.model.Build;
8+
import hudson.model.BuildListener;
109
import hudson.model.FreeStyleProject;
1110
import hudson.model.Result;
1211
import hudson.plugins.git.GitSCM;
12+
import hudson.plugins.git.Revision;
13+
import hudson.plugins.git.util.BuildData;
14+
import org.eclipse.jgit.lib.ObjectId;
15+
import org.jenkinsci.plugins.github.config.GitHubPluginConfig;
16+
import org.jenkinsci.plugins.github.test.GHMockRule;
17+
import org.jenkinsci.plugins.github.test.GHMockRule.FixedGHRepoNameTestContributor;
18+
import org.jenkinsci.plugins.github.test.InjectJenkinsMembersRule;
1319
import org.junit.Rule;
1420
import org.junit.Test;
21+
import org.junit.rules.ExternalResource;
22+
import org.junit.rules.RuleChain;
23+
import org.junit.runner.RunWith;
1524
import org.jvnet.hudson.test.Issue;
1625
import org.jvnet.hudson.test.JenkinsRule;
26+
import org.jvnet.hudson.test.TestBuilder;
27+
import org.jvnet.hudson.test.TestExtension;
28+
import org.mockito.Mock;
29+
import org.mockito.runners.MockitoJUnitRunner;
30+
31+
import javax.inject.Inject;
32+
33+
import static com.cloudbees.jenkins.GitHubSetCommitStatusBuilderTest.SOME_SHA;
34+
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
35+
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
36+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
37+
import static org.mockito.Mockito.when;
1738

1839
/**
1940
* Tests for {@link GitHubCommitNotifier}.
2041
*
2142
* @author Oleg Nenashev <o.v.nenashev@gmail.com>
2243
*/
44+
@RunWith(MockitoJUnitRunner.class)
2345
public class GitHubCommitNotifierTest {
2446

25-
@Rule
47+
@Mock
48+
public BuildData data;
49+
50+
@Mock
51+
public Revision rev;
52+
53+
@Inject
54+
public GitHubPluginConfig config;
55+
2656
public JenkinsRule jRule = new JenkinsRule();
2757

58+
@Rule
59+
public RuleChain chain = RuleChain.outerRule(jRule).around(new InjectJenkinsMembersRule(jRule, this));
60+
61+
@Rule
62+
public GHMockRule github = new GHMockRule(
63+
new WireMockRule(
64+
wireMockConfig().dynamicPort().notifier(new Slf4jNotifier(true))
65+
))
66+
.stubUser()
67+
.stubRepo()
68+
.stubStatuses();
69+
70+
71+
@Rule
72+
public ExternalResource prep = new ExternalResource() {
73+
@Override
74+
protected void before() throws Throwable {
75+
when(data.getLastBuiltRevision()).thenReturn(rev);
76+
when(rev.getSha1()).thenReturn(ObjectId.fromString(SOME_SHA));
77+
}
78+
};
79+
2880
@Test
2981
@Issue("JENKINS-23641")
3082
public void testNoBuildData() throws Exception {
@@ -63,4 +115,28 @@ public void testMarkSuccessOnCommitNotifierFailure() throws Exception {
63115
Build b = prj.scheduleBuild2(0).get();
64116
jRule.assertBuildStatus(Result.SUCCESS, b);
65117
}
118+
119+
@Test
120+
public void shouldWriteStatusOnGH() throws Exception {
121+
config.getConfigs().add(github.serverConfig());
122+
FreeStyleProject prj = jRule.createFreeStyleProject();
123+
124+
prj.getBuildersList().add(new TestBuilder() {
125+
@Override
126+
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) {
127+
build.addAction(data);
128+
return true;
129+
}
130+
});
131+
132+
prj.getPublishersList().add(new GitHubCommitNotifier(Result.SUCCESS.toString()));
133+
134+
prj.scheduleBuild2(0).get();
135+
136+
github.service().verify(1, postRequestedFor(urlPathMatching(".*/" + SOME_SHA)));
137+
}
138+
139+
@TestExtension
140+
public static final FixedGHRepoNameTestContributor CONTRIBUTOR = new FixedGHRepoNameTestContributor();
141+
66142
}

src/test/java/com/cloudbees/jenkins/GitHubSetCommitStatusBuilderTest.java

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,86 @@
11
package com.cloudbees.jenkins;
22

3+
import com.github.tomakehurst.wiremock.common.Slf4jNotifier;
4+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
5+
import hudson.Launcher;
6+
import hudson.model.AbstractBuild;
37
import hudson.model.Build;
8+
import hudson.model.BuildListener;
49
import hudson.model.FreeStyleProject;
510
import hudson.model.Result;
11+
import hudson.plugins.git.Revision;
12+
import hudson.plugins.git.util.BuildData;
13+
import hudson.tasks.Builder;
14+
import org.apache.commons.lang3.StringUtils;
15+
import org.eclipse.jgit.lib.ObjectId;
16+
import org.jenkinsci.plugins.github.config.GitHubPluginConfig;
17+
import org.jenkinsci.plugins.github.test.GHMockRule;
18+
import org.jenkinsci.plugins.github.test.GHMockRule.FixedGHRepoNameTestContributor;
19+
import org.jenkinsci.plugins.github.test.InjectJenkinsMembersRule;
620
import org.junit.Rule;
721
import org.junit.Test;
22+
import org.junit.rules.ExternalResource;
23+
import org.junit.rules.RuleChain;
24+
import org.junit.runner.RunWith;
825
import org.jvnet.hudson.test.Issue;
926
import org.jvnet.hudson.test.JenkinsRule;
27+
import org.jvnet.hudson.test.TestBuilder;
28+
import org.jvnet.hudson.test.TestExtension;
29+
import org.jvnet.hudson.test.recipes.LocalData;
30+
import org.mockito.Mock;
31+
import org.mockito.runners.MockitoJUnitRunner;
32+
33+
import javax.inject.Inject;
34+
import java.util.List;
35+
36+
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
37+
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
38+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
39+
import static com.google.common.collect.Lists.newArrayList;
40+
import static org.mockito.Mockito.when;
1041

1142
/**
1243
* Tests for {@link GitHubSetCommitStatusBuilder}.
1344
*
1445
* @author Oleg Nenashev <o.v.nenashev@gmail.com>
1546
*/
47+
@RunWith(MockitoJUnitRunner.class)
1648
public class GitHubSetCommitStatusBuilderTest {
1749

18-
@Rule
50+
public static final String SOME_SHA = StringUtils.repeat("f", 40);
51+
52+
@Mock
53+
public BuildData data;
54+
55+
@Mock
56+
public Revision rev;
57+
58+
@Inject
59+
public GitHubPluginConfig config;
60+
1961
public JenkinsRule jRule = new JenkinsRule();
2062

63+
@Rule
64+
public RuleChain chain = RuleChain.outerRule(jRule).around(new InjectJenkinsMembersRule(jRule, this));
65+
66+
@Rule
67+
public GHMockRule github = new GHMockRule(
68+
new WireMockRule(
69+
wireMockConfig().dynamicPort().notifier(new Slf4jNotifier(true))
70+
))
71+
.stubUser()
72+
.stubRepo()
73+
.stubStatuses();
74+
75+
@Rule
76+
public ExternalResource prep = new ExternalResource() {
77+
@Override
78+
protected void before() throws Throwable {
79+
when(data.getLastBuiltRevision()).thenReturn(rev);
80+
when(rev.getSha1()).thenReturn(ObjectId.fromString(SOME_SHA));
81+
}
82+
};
83+
2184
@Test
2285
@Issue("JENKINS-23641")
2386
public void testNoBuildData() throws Exception {
@@ -27,4 +90,29 @@ public void testNoBuildData() throws Exception {
2790
jRule.assertBuildStatus(Result.FAILURE, b);
2891
jRule.assertLogContains(org.jenkinsci.plugins.github.util.Messages.BuildDataHelper_NoBuildDataError(), b);
2992
}
93+
94+
@Test
95+
@LocalData
96+
@Issue("JENKINS-32132")
97+
public void shouldLoadNullStatusMessage() throws Exception {
98+
config.getConfigs().add(github.serverConfig());
99+
FreeStyleProject prj = jRule.getInstance().getItemByFullName("step", FreeStyleProject.class);
100+
101+
List<Builder> builders = newArrayList(prj.getBuildersList().toList());
102+
builders.add(0, new TestBuilder() {
103+
@Override
104+
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) {
105+
build.addAction(data);
106+
return true;
107+
}
108+
});
109+
110+
prj.getBuildersList().replaceBy(builders);
111+
prj.scheduleBuild2(0).get();
112+
113+
github.service().verify(1, postRequestedFor(urlPathMatching(".*/" + SOME_SHA)));
114+
}
115+
116+
@TestExtension
117+
public static final FixedGHRepoNameTestContributor CONTRIBUTOR = new FixedGHRepoNameTestContributor();
30118
}

src/test/java/com/cloudbees/jenkins/GitHubWebHookFullTest.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static com.jayway.restassured.RestAssured.given;
2121
import static com.jayway.restassured.config.EncoderConfig.encoderConfig;
2222
import static com.jayway.restassured.config.RestAssuredConfig.newConfig;
23+
import static java.lang.String.format;
2324
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
2425
import static javax.servlet.http.HttpServletResponse.SC_METHOD_NOT_ALLOWED;
2526
import static javax.servlet.http.HttpServletResponse.SC_OK;
@@ -139,13 +140,17 @@ public Header eventHeader(String event) {
139140
return new Header(GHEventHeader.PayloadHandler.EVENT_HEADER, event);
140141
}
141142

142-
public static String classpath(String path) throws IOException {
143+
public static String classpath(String path) {
143144
return classpath(GitHubWebHookFullTest.class, path);
144145
}
145146

146-
public static String classpath(Class<?> clazz, String path) throws IOException {
147-
return IOUtils.toString(clazz.getClassLoader().getResourceAsStream(
148-
clazz.getName().replace(PACKAGE_SEPARATOR, File.separator) + File.separator + path
149-
), Charsets.UTF_8);
147+
public static String classpath(Class<?> clazz, String path) {
148+
try {
149+
return IOUtils.toString(clazz.getClassLoader().getResourceAsStream(
150+
clazz.getName().replace(PACKAGE_SEPARATOR, File.separator) + File.separator + path
151+
), Charsets.UTF_8);
152+
} catch (IOException e) {
153+
throw new RuntimeException(format("Can't load %s for class %s", path, clazz), e);
154+
}
150155
}
151156
}

src/test/java/org/jenkinsci/plugins/github/internal/GitHubClientCacheCleanupTest.java

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.github.tomakehurst.wiremock.junit.WireMockRule;
44
import org.jenkinsci.plugins.github.config.GitHubServerConfig;
5-
import org.junit.Before;
5+
import org.jenkinsci.plugins.github.test.GHMockRule;
66
import org.junit.Rule;
77
import org.junit.Test;
88
import org.jvnet.hudson.test.JenkinsRule;
@@ -12,10 +12,6 @@
1212
import java.nio.file.Path;
1313
import java.util.Collections;
1414

15-
import static com.cloudbees.jenkins.GitHubWebHookFullTest.classpath;
16-
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
17-
import static com.github.tomakehurst.wiremock.client.WireMock.get;
18-
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
1915
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
2016
import static com.google.common.collect.Lists.newArrayList;
2117
import static java.nio.file.Files.newDirectoryStream;
@@ -36,12 +32,8 @@ public class GitHubClientCacheCleanupTest {
3632
public JenkinsRule jRule = new JenkinsRule();
3733

3834
@Rule
39-
public WireMockRule github = new WireMockRule(wireMockConfig().dynamicPort());
35+
public GHMockRule github = new GHMockRule(new WireMockRule(wireMockConfig().dynamicPort())).stubUser();
4036

41-
@Before
42-
public void setUp() throws Exception {
43-
stubUserResponse();
44-
}
4537

4638
@Test
4739
public void shouldCreateCachedFolder() throws Exception {
@@ -82,7 +74,7 @@ public void shouldRemoveOnlyNotActiveCachedDirAfterClean() throws Exception {
8274

8375
GitHubServerConfig config = new GitHubServerConfig(CHANGED_CREDS_ID);
8476
config.setCustomApiUrl(true);
85-
config.setApiUrl(constructApiUrl());
77+
config.setApiUrl(github.serverConfig().getApiUrl());
8678
config.setClientCacheSize(1);
8779

8880
clearRedundantCaches(newArrayList(config));
@@ -96,7 +88,7 @@ public void shouldRemoveCacheWhichNotEnabled() throws Exception {
9688

9789
GitHubServerConfig config = new GitHubServerConfig(CHANGED_CREDS_ID);
9890
config.setCustomApiUrl(true);
99-
config.setApiUrl(constructApiUrl());
91+
config.setApiUrl(github.serverConfig().getApiUrl());
10092
config.setClientCacheSize(0);
10193

10294
clearRedundantCaches(newArrayList(config));
@@ -110,20 +102,8 @@ private void it(String comment, int count) throws IOException {
110102
}
111103
}
112104

113-
private String constructApiUrl() {
114-
return "http://localhost:" + github.port();
115-
}
116-
117105
private void makeCachedRequestWithCredsId(String credsId) throws IOException {
118106
jRule.getInstance().getDescriptorByType(GitHubServerConfig.DescriptorImpl.class)
119-
.doVerifyCredentials(constructApiUrl(), credsId, 1);
120-
}
121-
122-
private void stubUserResponse() throws IOException {
123-
github.stubFor(get(urlPathEqualTo("/user"))
124-
.willReturn(aResponse()
125-
.withStatus(200)
126-
.withHeader("Content-Type", "application/json; charset=utf-8")
127-
.withBody(classpath(getClass(), "user.json"))));
107+
.doVerifyCredentials(github.serverConfig().getApiUrl(), credsId, 1);
128108
}
129109
}

0 commit comments

Comments
 (0)
0