Skip to content

Commit

Permalink
Merge pull request #279 from jetersen/fix/plugin-version-compatibility
Browse files Browse the repository at this point in the history
update parent pom, use plugin bom, remove powermock
  • Loading branch information
scaytrase committed Jan 17, 2022
2 parents 8d31cfd + 1b0db32 commit 640dbc5
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 276 deletions.
38 changes: 21 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.57</version>
<version>4.31</version>
<relativePath />
</parent>
<packaging>hpi</packaging>
<properties>
<revision>1.25</revision>
<changelist>-SNAPSHOT</changelist>
<!-- Baseline Jenkins version you use to build the plugin. Users must have this version or newer to run. -->
<jenkins.version>2.60.3</jenkins.version>
<jenkins.version>2.303.1</jenkins.version>
<java.level>8</java.level>
<hpi.compatibleSinceVersion>1.9</hpi.compatibleSinceVersion>
</properties>
Expand Down Expand Up @@ -77,33 +77,40 @@
</plugins>
</reporting>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.303.x</artifactId>
<version>1055.v9637a690f734</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>display-url-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>apache-httpcomponents-client-4-api</artifactId>
<version>4.5.5-3.0</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<version>3.0.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials</artifactId>
<version>2.1.4</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plain-credentials</artifactId>
<version>1.1</version>
<type>jar</type>
</dependency>
<dependency>
Expand All @@ -112,31 +119,28 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>token-macro</artifactId>
<version>2.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
<version>1.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jenkins.configuration-as-code</groupId>
<artifactId>test-harness</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ SSLContext buildSslContext(boolean ignoreUnverifiedSSL, Credentials credentials)
}

void configureProxy(HttpClientBuilder builder, URL url) {
Jenkins jenkins = Jenkins.getInstance();
Jenkins jenkins = Jenkins.get();
ProxyConfiguration proxyConfig = jenkins.proxy;
if (proxyConfig == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,11 @@ void setHttpNotifierSelector(HttpNotifierSelector httpNotifierSelector) {

HttpNotifierSelector getHttpNotifierSelector() {
if (httpNotifierSelector == null) {
Jenkins jenkins = Jenkins.getInstance();
Jenkins jenkins = Jenkins.get();
Injector injector = jenkins.getInjector();
injector.injectMembers(this);
if (injector != null) {
injector.injectMembers(this);
}
}
return httpNotifierSelector;
}
Expand Down Expand Up @@ -362,7 +364,7 @@ private boolean perform(Run<?, ?> run,
* @return Root URL contained in the global config
*/
private String getRootUrl() {
Jenkins instance = Jenkins.getInstance();
Jenkins instance = Jenkins.get();

return (instance.getRootUrl() != null) ? instance.getRootUrl() : globalConfig.getUrl();
}
Expand Down Expand Up @@ -554,7 +556,7 @@ private SSLContext buildSslContext(boolean ignoreUnverifiedSSL, Credentials cred
*/
@Deprecated
private void configureProxy(HttpClientBuilder builder, URL url) {
Jenkins jenkins = Jenkins.getInstance();
Jenkins jenkins = Jenkins.get();
ProxyConfiguration proxyConfig = jenkins.proxy;
if (proxyConfig == null) {
return;
Expand Down Expand Up @@ -621,7 +623,7 @@ protected DescriptorImpl(boolean load) {
}

public ListBoxModel doFillCredentialsIdItems(@AncestorInPath Item project) {
Jenkins jenkins = Jenkins.getInstance();
Jenkins jenkins = Jenkins.get();

if (project != null && project.hasPermission(Item.CONFIGURE)) {
return new StandardListBoxModel()
Expand Down Expand Up @@ -854,7 +856,7 @@ private <T extends Credentials> T getCredentials(final Class<T> clazz, final Ite
}
if (StringUtils.isNotBlank(credentialsId) && clazz != null && project != null) {
credentials = CredentialsMatchers.firstOrNull(
CredentialsProvider.lookupCredentials(clazz, Jenkins.getInstance(), ACL.SYSTEM, new ArrayList<>()),
CredentialsProvider.lookupCredentials(clazz, Jenkins.get(), ACL.SYSTEM, new ArrayList<>()),
CredentialsMatchers.withId(credentialsId));
}
}
Expand Down Expand Up @@ -1082,10 +1084,12 @@ protected String getBuildDescription(
final Run<?, ?> run,
final StashBuildState state) {

if (run.getDescription() != null
&& run.getDescription().trim().length() > 0) {
String runDescription = run.getDescription();

if (runDescription != null
&& runDescription.trim().length() > 0) {

return run.getDescription();
return runDescription;
} else {
switch (state) {
case INPROGRESS:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package org.jenkinsci.plugins.stashNotifier;

import io.jenkins.plugins.casc.ConfigurationAsCode;
import org.apache.commons.io.IOUtils;
import io.jenkins.plugins.casc.ConfigurationContext;
import io.jenkins.plugins.casc.ConfiguratorRegistry;
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import io.jenkins.plugins.casc.model.CNode;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;

import static io.jenkins.plugins.casc.misc.Util.getUnclassifiedRoot;
import static io.jenkins.plugins.casc.misc.Util.toStringFromYamlFile;
import static io.jenkins.plugins.casc.misc.Util.toYamlString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.core.Is.is;

public class ConfigAsCodeTest {
@Rule
public JenkinsRule rule = new JenkinsRule();
@Rule public JenkinsConfiguredWithCodeRule rule = new JenkinsConfiguredWithCodeRule();

@Test
@ConfiguredWithCode("configuration-as-code.yml")
public void should_support_jcasc_from_yaml() throws Exception {
URL configFileUrl = ConfigAsCodeTest.class.getResource(getClass().getSimpleName() + "/configuration-as-code.yml");
ConfigurationAsCode.get().configure(configFileUrl.toString());
StashNotifier.DescriptorImpl stashNotifierConfig = rule.jenkins.getDescriptorByType(StashNotifier.DescriptorImpl.class);

assertThat(stashNotifierConfig.isConsiderUnstableAsSuccess(), equalTo(true));
Expand All @@ -34,26 +33,16 @@ public void should_support_jcasc_from_yaml() throws Exception {
}

@Test
@ConfiguredWithCode("configuration-as-code.yml")
public void should_support_jcasc_to_yaml() throws Exception {
StashNotifier.DescriptorImpl stashNotifierConfig = rule.jenkins.getDescriptorByType(StashNotifier.DescriptorImpl.class);

stashNotifierConfig.setConsiderUnstableAsSuccess(true);
stashNotifierConfig.setCredentialsId("bitbucket-credentials");
stashNotifierConfig.setDisableInprogressNotification(true);
stashNotifierConfig.setIgnoreUnverifiedSsl(true);
stashNotifierConfig.setIncludeBuildNumberInKey(true);
stashNotifierConfig.setPrependParentProjectKey(true);
stashNotifierConfig.setStashRootUrl("https://my.company.intranet/bitbucket");
ConfiguratorRegistry registry = ConfiguratorRegistry.get();
ConfigurationContext context = new ConfigurationContext(registry);
CNode yourAttribute = getUnclassifiedRoot(context).get("notifyBitbucket");

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ConfigurationAsCode.get().export(outputStream);
String exportedYaml = outputStream.toString("UTF-8");
String exported = toYamlString(yourAttribute);

InputStream yamlStream = getClass().getResourceAsStream(getClass().getSimpleName() + "/configuration-as-code.yml");
String expectedYaml = IOUtils.toString(yamlStream, "UTF-8")
.replaceAll("\r\n?", "\n")
.replace("unclassified:\n", "");
String expected = toStringFromYamlFile(this, "configuration-as-code-expected.yml");

assertThat(exportedYaml, containsString(expectedYaml));
assertThat(exported, is(expected));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.jenkinsci.plugins.stashNotifier;

import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.model.AbstractBuild;
Expand All @@ -23,65 +23,64 @@
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;
import org.junit.Before;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.File;
import java.io.PrintStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.mockito.MockedStatic;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest({Secret.class, Jenkins.class, HttpClientBuilder.class, TokenMacro.class, CredentialsMatchers.class, com.cloudbees.plugins.credentials.CredentialsProvider.class, AbstractProject.class})
@PowerMockIgnore("javax.net.ssl.*")
public class DefaultApacheHttpNotifierTest {

final static String sha1 = "1234567890123456789012345678901234567890";
private CloseableHttpClient client;
private static CloseableHttpClient client;
private static MockedStatic<Jenkins> mockedJenkins;
private static MockedStatic<CredentialsProvider> mockedCredentialsProvider;
private static MockedStatic<Secret> mockedSecret;
private static MockedStatic<HttpClientBuilder> mockedHttpClientBuilder;
private static MockedStatic<TokenMacro> mockedTokenMacro;
private final HttpNotifier httpNotifier = new DefaultApacheHttpNotifier();

BuildListener buildListener;
AbstractBuild<?, ?> build;
Run<?, ?> run;
FilePath workspace;
private static BuildListener buildListener;

@Before
public void setUp() throws Exception {
PowerMockito.mockStatic(Secret.class);
PowerMockito.mockStatic(Jenkins.class);
PowerMockito.mockStatic(HttpClientBuilder.class);
PowerMockito.mockStatic(TokenMacro.class);
PowerMockito.mockStatic(com.cloudbees.plugins.credentials.CredentialsProvider.class);
@BeforeClass
public static void setUp() throws Exception {
mockedSecret = mockStatic(Secret.class);
mockedJenkins = mockStatic(Jenkins.class);
mockedHttpClientBuilder = mockStatic(HttpClientBuilder.class);
mockedTokenMacro = mockStatic(TokenMacro.class);
mockedCredentialsProvider = mockStatic(com.cloudbees.plugins.credentials.CredentialsProvider.class);

buildListener = mock(BuildListener.class);
Jenkins jenkins = mock(Jenkins.class);
build = mock(AbstractBuild.class);
run = mock(Run.class);
AbstractProject project = PowerMockito.mock(AbstractProject.class);
AbstractBuild<?, ?> build = mock(AbstractBuild.class);
Run<?, ?> run = mock(Run.class);

AbstractProject<?, ?> project = mock(AbstractProject.class);
File file = mock(File.class);
when(file.getPath()).thenReturn("/tmp/fake/path");
FilePath filePath = new FilePath(file);
PowerMockito.when(project.getSomeWorkspace()).thenReturn(filePath);
workspace = project.getSomeWorkspace();
when(project.getSomeWorkspace()).thenReturn(filePath);
FilePath workspace = project.getSomeWorkspace();
EnvVars environment = mock(EnvVars.class);
PrintStream logger = System.out;
Secret secret = mock(Secret.class);
HttpClientBuilder httpClientBuilder = PowerMockito.mock(HttpClientBuilder.class);
HttpClientBuilder httpClientBuilder = mock(HttpClientBuilder.class);
client = mock(CloseableHttpClient.class);
CloseableHttpResponse resp = mock(CloseableHttpResponse.class);
StatusLine statusLine = mock(StatusLine.class);
Expand All @@ -90,13 +89,13 @@ public void setUp() throws Exception {
Build lastBuild = mock(Build.class);
List<BuildData> actions = Collections.singletonList(action);

when(Jenkins.getInstance()).thenReturn(jenkins);
when(Jenkins.get()).thenReturn(jenkins);
when(jenkins.getRootUrl()).thenReturn("http://localhost/");
when(build.getEnvironment(buildListener)).thenReturn(environment);
when(action.getLastBuiltRevision()).thenReturn(revision);
when(revision.getSha1String()).thenReturn(sha1);
when(build.getProject()).thenReturn(project);
when(run.getParent()).thenReturn(project);
doReturn(project).when(build).getProject();
doReturn(project).when(run).getParent();
when(build.getFullDisplayName()).thenReturn("foo");
when(build.getUrl()).thenReturn("foo");
when(build.getActions(BuildData.class)).thenReturn(actions);
Expand All @@ -114,14 +113,23 @@ public void setUp() throws Exception {
when(lastBuild.getMarked()).thenReturn(revision);

when(TokenMacro.expandAll(build, buildListener, "test-project")).thenReturn("prepend-key");
when(com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
when(CredentialsProvider.lookupCredentials(
any(),
any(ItemGroup.class),
any(Authentication.class),
anyList()
)).thenReturn(new ArrayList<>());
}

@AfterClass
public static void close() {
mockedJenkins.close();
mockedCredentialsProvider.close();
mockedSecret.close();
mockedHttpClientBuilder.close();
mockedTokenMacro.close();
}

private NotificationResult notifyStash(int statusCode) throws Exception {
PrintStream logger = mock(PrintStream.class);
URI uri = BuildStatusUriFactory.create("http://localhost", "df02f57eea1cda72fa2412102f061dd7f6188e98");
Expand Down
Loading

0 comments on commit 640dbc5

Please sign in to comment.