Skip to content
Permalink
Browse files
Merge pull request #104 from lanwen/JENKINS-32132_tests
Test for JENKINS-32132 with rule to mock GH
  • Loading branch information
lanwen committed Dec 22, 2015
2 parents 4197fdf + 4024d62 commit 5e14ccae9de8c0ba5226ac4008f05c6f7bbd0a79
@@ -1,30 +1,82 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package com.cloudbees.jenkins;

import com.github.tomakehurst.wiremock.common.Slf4jNotifier;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.Build;
import hudson.model.BuildListener;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.Revision;
import hudson.plugins.git.util.BuildData;
import org.eclipse.jgit.lib.ObjectId;
import org.jenkinsci.plugins.github.config.GitHubPluginConfig;
import org.jenkinsci.plugins.github.test.GHMockRule;
import org.jenkinsci.plugins.github.test.GHMockRule.FixedGHRepoNameTestContributor;
import org.jenkinsci.plugins.github.test.InjectJenkinsMembersRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExternalResource;
import org.junit.rules.RuleChain;
import org.junit.runner.RunWith;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestBuilder;
import org.jvnet.hudson.test.TestExtension;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import javax.inject.Inject;

import static com.cloudbees.jenkins.GitHubSetCommitStatusBuilderTest.SOME_SHA;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.mockito.Mockito.when;

/**
* Tests for {@link GitHubCommitNotifier}.
*
* @author Oleg Nenashev <o.v.nenashev@gmail.com>
*/
@RunWith(MockitoJUnitRunner.class)
public class GitHubCommitNotifierTest {

@Rule
@Mock
public BuildData data;

@Mock
public Revision rev;

@Inject
public GitHubPluginConfig config;

public JenkinsRule jRule = new JenkinsRule();

@Rule
public RuleChain chain = RuleChain.outerRule(jRule).around(new InjectJenkinsMembersRule(jRule, this));

@Rule
public GHMockRule github = new GHMockRule(
new WireMockRule(
wireMockConfig().dynamicPort().notifier(new Slf4jNotifier(true))
))
.stubUser()
.stubRepo()
.stubStatuses();


@Rule
public ExternalResource prep = new ExternalResource() {
@Override
protected void before() throws Throwable {
when(data.getLastBuiltRevision()).thenReturn(rev);
when(rev.getSha1()).thenReturn(ObjectId.fromString(SOME_SHA));
}
};

@Test
@Issue("JENKINS-23641")
public void testNoBuildData() throws Exception {
@@ -63,4 +115,28 @@ public void testMarkSuccessOnCommitNotifierFailure() throws Exception {
Build b = prj.scheduleBuild2(0).get();
jRule.assertBuildStatus(Result.SUCCESS, b);
}

@Test
public void shouldWriteStatusOnGH() throws Exception {
config.getConfigs().add(github.serverConfig());
FreeStyleProject prj = jRule.createFreeStyleProject();

prj.getBuildersList().add(new TestBuilder() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) {
build.addAction(data);
return true;
}
});

prj.getPublishersList().add(new GitHubCommitNotifier(Result.SUCCESS.toString()));

prj.scheduleBuild2(0).get();

github.service().verify(1, postRequestedFor(urlPathMatching(".*/" + SOME_SHA)));
}

@TestExtension
public static final FixedGHRepoNameTestContributor CONTRIBUTOR = new FixedGHRepoNameTestContributor();

}
@@ -1,23 +1,86 @@
package com.cloudbees.jenkins;

import com.github.tomakehurst.wiremock.common.Slf4jNotifier;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.Build;
import hudson.model.BuildListener;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.plugins.git.Revision;
import hudson.plugins.git.util.BuildData;
import hudson.tasks.Builder;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jgit.lib.ObjectId;
import org.jenkinsci.plugins.github.config.GitHubPluginConfig;
import org.jenkinsci.plugins.github.test.GHMockRule;
import org.jenkinsci.plugins.github.test.GHMockRule.FixedGHRepoNameTestContributor;
import org.jenkinsci.plugins.github.test.InjectJenkinsMembersRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExternalResource;
import org.junit.rules.RuleChain;
import org.junit.runner.RunWith;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestBuilder;
import org.jvnet.hudson.test.TestExtension;
import org.jvnet.hudson.test.recipes.LocalData;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import javax.inject.Inject;
import java.util.List;

import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static com.google.common.collect.Lists.newArrayList;
import static org.mockito.Mockito.when;

/**
* Tests for {@link GitHubSetCommitStatusBuilder}.
*
* @author Oleg Nenashev <o.v.nenashev@gmail.com>
*/
@RunWith(MockitoJUnitRunner.class)
public class GitHubSetCommitStatusBuilderTest {

@Rule
public static final String SOME_SHA = StringUtils.repeat("f", 40);

@Mock
public BuildData data;

@Mock
public Revision rev;

@Inject
public GitHubPluginConfig config;

public JenkinsRule jRule = new JenkinsRule();

@Rule
public RuleChain chain = RuleChain.outerRule(jRule).around(new InjectJenkinsMembersRule(jRule, this));

@Rule
public GHMockRule github = new GHMockRule(
new WireMockRule(
wireMockConfig().dynamicPort().notifier(new Slf4jNotifier(true))
))
.stubUser()
.stubRepo()
.stubStatuses();

@Rule
public ExternalResource prep = new ExternalResource() {
@Override
protected void before() throws Throwable {
when(data.getLastBuiltRevision()).thenReturn(rev);
when(rev.getSha1()).thenReturn(ObjectId.fromString(SOME_SHA));
}
};

@Test
@Issue("JENKINS-23641")
public void testNoBuildData() throws Exception {
@@ -27,4 +90,29 @@ public void testNoBuildData() throws Exception {
jRule.assertBuildStatus(Result.FAILURE, b);
jRule.assertLogContains(org.jenkinsci.plugins.github.util.Messages.BuildDataHelper_NoBuildDataError(), b);
}

@Test
@LocalData
@Issue("JENKINS-32132")
public void shouldLoadNullStatusMessage() throws Exception {
config.getConfigs().add(github.serverConfig());
FreeStyleProject prj = jRule.getInstance().getItemByFullName("step", FreeStyleProject.class);

List<Builder> builders = newArrayList(prj.getBuildersList().toList());
builders.add(0, new TestBuilder() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) {
build.addAction(data);
return true;
}
});

prj.getBuildersList().replaceBy(builders);
prj.scheduleBuild2(0).get();

github.service().verify(1, postRequestedFor(urlPathMatching(".*/" + SOME_SHA)));
}

@TestExtension
public static final FixedGHRepoNameTestContributor CONTRIBUTOR = new FixedGHRepoNameTestContributor();
}
@@ -20,6 +20,7 @@
import static com.jayway.restassured.RestAssured.given;
import static com.jayway.restassured.config.EncoderConfig.encoderConfig;
import static com.jayway.restassured.config.RestAssuredConfig.newConfig;
import static java.lang.String.format;
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import static javax.servlet.http.HttpServletResponse.SC_METHOD_NOT_ALLOWED;
import static javax.servlet.http.HttpServletResponse.SC_OK;
@@ -139,13 +140,17 @@ public Header eventHeader(String event) {
return new Header(GHEventHeader.PayloadHandler.EVENT_HEADER, event);
}

public static String classpath(String path) throws IOException {
public static String classpath(String path) {
return classpath(GitHubWebHookFullTest.class, path);
}

public static String classpath(Class<?> clazz, String path) throws IOException {
return IOUtils.toString(clazz.getClassLoader().getResourceAsStream(
clazz.getName().replace(PACKAGE_SEPARATOR, File.separator) + File.separator + path
), Charsets.UTF_8);
public static String classpath(Class<?> clazz, String path) {
try {
return IOUtils.toString(clazz.getClassLoader().getResourceAsStream(
clazz.getName().replace(PACKAGE_SEPARATOR, File.separator) + File.separator + path
), Charsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException(format("Can't load %s for class %s", path, clazz), e);
}
}
}
@@ -2,7 +2,7 @@

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.jenkinsci.plugins.github.config.GitHubServerConfig;
import org.junit.Before;
import org.jenkinsci.plugins.github.test.GHMockRule;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
@@ -12,10 +12,6 @@
import java.nio.file.Path;
import java.util.Collections;

import static com.cloudbees.jenkins.GitHubWebHookFullTest.classpath;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static com.google.common.collect.Lists.newArrayList;
import static java.nio.file.Files.newDirectoryStream;
@@ -36,12 +32,8 @@
public JenkinsRule jRule = new JenkinsRule();

@Rule
public WireMockRule github = new WireMockRule(wireMockConfig().dynamicPort());
public GHMockRule github = new GHMockRule(new WireMockRule(wireMockConfig().dynamicPort())).stubUser();

@Before
public void setUp() throws Exception {
stubUserResponse();
}

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

GitHubServerConfig config = new GitHubServerConfig(CHANGED_CREDS_ID);
config.setCustomApiUrl(true);
config.setApiUrl(constructApiUrl());
config.setApiUrl(github.serverConfig().getApiUrl());
config.setClientCacheSize(1);

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

GitHubServerConfig config = new GitHubServerConfig(CHANGED_CREDS_ID);
config.setCustomApiUrl(true);
config.setApiUrl(constructApiUrl());
config.setApiUrl(github.serverConfig().getApiUrl());
config.setClientCacheSize(0);

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

private String constructApiUrl() {
return "http://localhost:" + github.port();
}

private void makeCachedRequestWithCredsId(String credsId) throws IOException {
jRule.getInstance().getDescriptorByType(GitHubServerConfig.DescriptorImpl.class)
.doVerifyCredentials(constructApiUrl(), credsId, 1);
}

private void stubUserResponse() throws IOException {
github.stubFor(get(urlPathEqualTo("/user"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json; charset=utf-8")
.withBody(classpath(getClass(), "user.json"))));
.doVerifyCredentials(github.serverConfig().getApiUrl(), credsId, 1);
}
}

0 comments on commit 5e14cca

Please sign in to comment.