From e613acf3ee1eea6cd2f9c40dde5ccea75182aa7a Mon Sep 17 00:00:00 2001 From: strangelookingnerd <49242855+strangelookingnerd@users.noreply.github.com> Date: Thu, 27 Mar 2025 10:23:37 +0100 Subject: [PATCH] Migrate tests to JUnit5 * Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup --- .../plugins/plaincredentials/BaseTest.java | 76 +++++++++--------- .../ConfigurationAsCodeTest.java | 23 +++--- .../plaincredentials/FileCredentialsTest.java | 30 ++++---- .../plaincredentials/SecretBytesTest.java | 77 +++++++++---------- 4 files changed, 100 insertions(+), 106 deletions(-) diff --git a/src/test/java/org/jenkinsci/plugins/plaincredentials/BaseTest.java b/src/test/java/org/jenkinsci/plugins/plaincredentials/BaseTest.java index e6dca10..670f3b5 100644 --- a/src/test/java/org/jenkinsci/plugins/plaincredentials/BaseTest.java +++ b/src/test/java/org/jenkinsci/plugins/plaincredentials/BaseTest.java @@ -1,68 +1,66 @@ package org.jenkinsci.plugins.plaincredentials; -import java.io.IOException; -import java.net.URISyntaxException; -import java.nio.file.Files; -import java.util.Arrays; -import java.util.Base64; -import java.util.Collections; -import java.util.List; - -import com.cloudbees.plugins.credentials.SecretBytes; -import org.apache.commons.fileupload.FileItem; -import org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl; -import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.jvnet.hudson.test.JenkinsRule; - import com.cloudbees.plugins.credentials.CredentialsProvider; import com.cloudbees.plugins.credentials.CredentialsScope; import com.cloudbees.plugins.credentials.CredentialsStore; +import com.cloudbees.plugins.credentials.SecretBytes; import com.cloudbees.plugins.credentials.domains.Domain; import com.cloudbees.plugins.credentials.impl.BaseStandardCredentials; - -import hudson.model.FileParameterValue.FileItemImpl; +import hudson.model.FileParameterValue; import hudson.security.ACL; import hudson.util.Secret; +import org.apache.commons.fileupload.FileItem; +import org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl; +import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; + +import java.nio.file.Files; +import java.util.Arrays; +import java.util.Base64; +import java.util.Collections; +import java.util.List; -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; -public class BaseTest { +@WithJenkins +class BaseTest { private static final String CRED_ID = "Custom-ID"; - @Rule - public JenkinsRule r = new JenkinsRule(); + private JenkinsRule r; private CredentialsStore store; - @Before - public void setup(){ + @BeforeEach + void setup(JenkinsRule rule) { + r = rule; store = CredentialsProvider.lookupStores(r.jenkins).iterator().next(); } @Test - public void secretTextBaseTest() throws IOException { + void secretTextBaseTest() throws Exception { StringCredentialsImpl credential = new StringCredentialsImpl(CredentialsScope.GLOBAL, CRED_ID, "Test Secret Text", Secret.fromString("password")); StringCredentialsImpl updatedCredential = new StringCredentialsImpl(credential.getScope(), CRED_ID, "Updated Secret Text", credential.getSecret()); testCreateUpdateDelete(credential, updatedCredential); } @Test - public void secretFileBaseTest() throws IOException, URISyntaxException { + void secretFileBaseTest() throws Exception { secretFileTest(false); } @Test - public void secretFileBaseTestWithDeprecatedCtor() throws IOException, URISyntaxException { + void secretFileBaseTestWithDeprecatedCtor() throws Exception { secretFileTest(true); } - private void secretFileTest(boolean useDeprecatedConstructor) throws IOException, URISyntaxException { - FileItem fileItem = createEmptyFileItem(); + private void secretFileTest(boolean useDeprecatedConstructor) throws Exception { + FileItem fileItem = FileItem.fromFileUpload2FileItem(createEmptyFileItem()); FileCredentialsImpl credential; @@ -81,14 +79,14 @@ private void secretFileTest(boolean useDeprecatedConstructor) throws IOException * * @param credential the credential to create * @param updatedCredential the credential that will replace the first one during update - * @throws IOException + * @throws Exception */ - private void testCreateUpdateDelete(T credential, T updatedCredential) throws IOException { + private void testCreateUpdateDelete(T credential, T updatedCredential) throws Exception { // Add a credential store.addCredentials(Domain.global(), credential); // Look up all credentials - List credentials = CredentialsProvider.lookupCredentials(BaseStandardCredentials.class, r.jenkins, ACL.SYSTEM, Collections.emptyList()); + List credentials = CredentialsProvider.lookupCredentialsInItemGroup(BaseStandardCredentials.class, r.jenkins, ACL.SYSTEM2, Collections.emptyList()); // There is one credential assertThat(credentials.size(), is(1)); @@ -99,7 +97,7 @@ private void testCreateUpdateDelete(T creden store.updateCredentials(Domain.global(), cred, updatedCredential); // Look up all credentials again - credentials = CredentialsProvider.lookupCredentials(BaseStandardCredentials.class, r.jenkins, ACL.SYSTEM, Collections.emptyList()); + credentials = CredentialsProvider.lookupCredentialsInItemGroup(BaseStandardCredentials.class, r.jenkins, ACL.SYSTEM2, Collections.emptyList()); // There is still 1 credential but the description has been updated assertThat(credentials.size(), is(1)); @@ -112,7 +110,7 @@ private void testCreateUpdateDelete(T creden store.removeCredentials(Domain.global(), cred); // Look up all credentials again - credentials = CredentialsProvider.lookupCredentials(BaseStandardCredentials.class, r.jenkins, ACL.SYSTEM, Collections.emptyList()); + credentials = CredentialsProvider.lookupCredentialsInItemGroup(BaseStandardCredentials.class, r.jenkins, ACL.SYSTEM2, Collections.emptyList()); // There are no credentials anymore assertThat(credentials.size(), is(0)); @@ -122,9 +120,9 @@ private void testCreateUpdateDelete(T creden * Creates an empty FileItem for testing purposes * * @return - * @throws IOException + * @throws Exception */ - private FileItem createEmptyFileItem() throws IOException { - return new FileItemImpl(Files.createTempFile("credential-test", null).toFile()); + private static FileParameterValue.FileItemImpl2 createEmptyFileItem() throws Exception { + return new FileParameterValue.FileItemImpl2(Files.createTempFile("credential-test", null).toFile()); } } diff --git a/src/test/java/org/jenkinsci/plugins/plaincredentials/ConfigurationAsCodeTest.java b/src/test/java/org/jenkinsci/plugins/plaincredentials/ConfigurationAsCodeTest.java index b3d3787..fcb825f 100644 --- a/src/test/java/org/jenkinsci/plugins/plaincredentials/ConfigurationAsCodeTest.java +++ b/src/test/java/org/jenkinsci/plugins/plaincredentials/ConfigurationAsCodeTest.java @@ -2,34 +2,33 @@ import com.cloudbees.plugins.credentials.CredentialsMatchers; import com.cloudbees.plugins.credentials.CredentialsProvider; -import com.cloudbees.plugins.credentials.domains.DomainRequirement; import hudson.security.ACL; import io.jenkins.plugins.casc.misc.ConfiguredWithCode; import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule; +import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode; import org.apache.commons.io.IOUtils; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import java.nio.charset.StandardCharsets; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; /** * @author Nicolas De Loof */ -public class ConfigurationAsCodeTest { - - @Rule - public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule(); +@WithJenkinsConfiguredWithCode +class ConfigurationAsCodeTest { @Test @ConfiguredWithCode("ConfigurationAsCode.yaml") - public void should_configure_file_credentials() throws Exception { + void should_configure_file_credentials(JenkinsConfiguredWithCodeRule j) throws Exception { FileCredentials credentials = CredentialsMatchers.firstOrNull( - CredentialsProvider.lookupCredentials(FileCredentials.class, j.jenkins, ACL.SYSTEM, (DomainRequirement) null), + CredentialsProvider.lookupCredentialsInItemGroup(FileCredentials.class, j.jenkins, ACL.SYSTEM2, null), CredentialsMatchers.withId("secret-file")); assertNotNull(credentials); assertEquals("Some secret file", credentials.getDescription()); assertEquals("my-secret-file", credentials.getFileName()); - assertEquals("FOO_BAR", IOUtils.toString(credentials.getContent())); + assertEquals("FOO_BAR", IOUtils.toString(credentials.getContent(), StandardCharsets.UTF_8)); } } diff --git a/src/test/java/org/jenkinsci/plugins/plaincredentials/FileCredentialsTest.java b/src/test/java/org/jenkinsci/plugins/plaincredentials/FileCredentialsTest.java index 2bcb0d6..29d1431 100644 --- a/src/test/java/org/jenkinsci/plugins/plaincredentials/FileCredentialsTest.java +++ b/src/test/java/org/jenkinsci/plugins/plaincredentials/FileCredentialsTest.java @@ -28,32 +28,30 @@ import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemHeaders; import org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.Issue; +import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import java.io.File; -import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.io.UnsupportedEncodingException; -import org.jvnet.hudson.test.JenkinsRule; -public class FileCredentialsTest { +import static org.junit.jupiter.api.Assertions.assertThrows; - @Rule - public JenkinsRule r = new JenkinsRule(); +@WithJenkins +class FileCredentialsTest { - @Test(expected = IllegalArgumentException.class) + @Test @Issue("JENKINS-30926") - public void shouldThrowAnExceptionIfFileNameIsBlank() throws IOException { - new FileCredentialsImpl(CredentialsScope.GLOBAL, "1", "", new StubFileItem(), "", SecretBytes.fromString("")); + void shouldThrowAnExceptionIfFileNameIsBlank(JenkinsRule r) { + assertThrows(IllegalArgumentException.class, () -> new FileCredentialsImpl(CredentialsScope.GLOBAL, "1", "", new StubFileItem(), "", SecretBytes.fromString(""))); } - private class StubFileItem implements FileItem { + private static class StubFileItem implements FileItem { @Override - public InputStream getInputStream() throws IOException { + public InputStream getInputStream() { return null; } @@ -83,7 +81,7 @@ public byte[] get() { } @Override - public String getString(String encoding) throws UnsupportedEncodingException { + public String getString(String encoding) { return null; } @@ -93,7 +91,7 @@ public String getString() { } @Override - public void write(File file) throws Exception { + public void write(File file) { } @@ -123,7 +121,7 @@ public void setFormField(boolean state) { } @Override - public OutputStream getOutputStream() throws IOException { + public OutputStream getOutputStream() { return null; } diff --git a/src/test/java/org/jenkinsci/plugins/plaincredentials/SecretBytesTest.java b/src/test/java/org/jenkinsci/plugins/plaincredentials/SecretBytesTest.java index 3058057..d6f3965 100644 --- a/src/test/java/org/jenkinsci/plugins/plaincredentials/SecretBytesTest.java +++ b/src/test/java/org/jenkinsci/plugins/plaincredentials/SecretBytesTest.java @@ -29,37 +29,36 @@ import com.cloudbees.plugins.credentials.SecretBytes; import com.cloudbees.plugins.credentials.SystemCredentialsProvider; import com.cloudbees.plugins.credentials.cli.CreateCredentialsByXmlCommand; -import com.cloudbees.plugins.credentials.domains.DomainRequirement; import hudson.cli.CLICommandInvoker; import hudson.security.ACL; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.nio.charset.StandardCharsets; -import java.util.Base64; -import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.jvnet.hudson.test.recipes.LocalData; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.util.Base64; + import static hudson.cli.CLICommandInvoker.Matcher.succeededSilently; -import hudson.util.RobustReflectionConverter; -import java.util.logging.Level; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; -import static org.junit.Assume.*; -import org.jvnet.hudson.test.LoggerRule; +import static org.junit.jupiter.api.Assumptions.assumeTrue; -public class SecretBytesTest { +@WithJenkins +class SecretBytesTest { - @Rule - public JenkinsRule r = new JenkinsRule(); + private JenkinsRule r; - @Rule - public LoggerRule logging = new LoggerRule().record(RobustReflectionConverter.class, Level.FINE); + @BeforeEach + void setup(JenkinsRule rule) { + r = rule; + } /** * Verifies that {@link SecretBytes} will treat a Base64 encoded plain text content as the content to be encrypted @@ -68,22 +67,22 @@ public class SecretBytesTest { */ @Test @LocalData - public void loadUnencrypted() throws Exception { + void loadUnencrypted() throws Exception { // these are the magic strings - assumeThat(Base64.getEncoder().encodeToString("This is Base64 encoded plain text\n".getBytes(StandardCharsets.UTF_8)), is( + assumeTrue(Base64.getEncoder().encodeToString("This is Base64 encoded plain text\n".getBytes(StandardCharsets.UTF_8)).equals( "VGhpcyBpcyBCYXNlNjQgZW5jb2RlZCBwbGFpbiB0ZXh0Cg==")); // first check that the file on disk contains the unencrypted text - assertThat(FileUtils.readFileToString(new File(r.jenkins.getRootDir(), "credentials.xml")), + assertThat(FileUtils.readFileToString(new File(r.jenkins.getRootDir(), "credentials.xml"), StandardCharsets.UTF_8), containsString("VGhpcyBpcyBCYXNlNjQgZW5jb2RlZCBwbGFpbiB0ZXh0Cg==")); // get the credential instance under test FileCredentials c = CredentialsMatchers.firstOrNull( - CredentialsProvider.lookupCredentials( + CredentialsProvider.lookupCredentialsInItemGroup( FileCredentials.class, r.jenkins, - ACL.SYSTEM, - (List) null + ACL.SYSTEM2, + null ), CredentialsMatchers.withId("secret-file") ); @@ -98,7 +97,7 @@ public void loadUnencrypted() throws Exception { // now when we re-save the credentials this should encrypt with the instance's secret key SystemCredentialsProvider.getInstance().save(); - assertThat(FileUtils.readFileToString(new File(r.jenkins.getRootDir(), "credentials.xml")), + assertThat(FileUtils.readFileToString(new File(r.jenkins.getRootDir(), "credentials.xml"), StandardCharsets.UTF_8), not(containsString("VGhpcyBpcyBCYXNlNjQgZW5jb2RlZCBwbGFpbiB0ZXh0Cg=="))); } @@ -109,18 +108,18 @@ public void loadUnencrypted() throws Exception { */ @Test @LocalData - public void migrateLegacyData() throws Exception { + void migrateLegacyData() throws Exception { // first check that the file on disk contains the legacy format - assertThat(FileUtils.readFileToString(new File(r.jenkins.getRootDir(), "credentials.xml")), + assertThat(FileUtils.readFileToString(new File(r.jenkins.getRootDir(), "credentials.xml"), StandardCharsets.UTF_8), allOf(containsString(""), not(containsString("")))); // get the credential instance under test FileCredentials c = CredentialsMatchers.firstOrNull( - CredentialsProvider.lookupCredentials( + CredentialsProvider.lookupCredentialsInItemGroup( FileCredentials.class, r.jenkins, - ACL.SYSTEM, - (List) null + ACL.SYSTEM2, + null ), CredentialsMatchers.withId("legacyData") ); @@ -135,19 +134,19 @@ public void migrateLegacyData() throws Exception { // now when we re-save the credentials this should persist in the new format SystemCredentialsProvider.getInstance().save(); - assertThat(FileUtils.readFileToString(new File(r.jenkins.getRootDir(), "credentials.xml")), + assertThat(FileUtils.readFileToString(new File(r.jenkins.getRootDir(), "credentials.xml"), StandardCharsets.UTF_8), allOf(not(containsString("")), containsString(""))); } @Test - public void createFileCredentialsByXml() throws Exception { + void createFileCredentialsByXml() throws Exception { // get the credential instance doesn't exist yet FileCredentials c = CredentialsMatchers.firstOrNull( - CredentialsProvider.lookupCredentials( + CredentialsProvider.lookupCredentialsInItemGroup( FileCredentials.class, r.jenkins, - ACL.SYSTEM, - (List) null + ACL.SYSTEM2, + null ), CredentialsMatchers.withId("secret-file") ); @@ -172,11 +171,11 @@ public void createFileCredentialsByXml() throws Exception { // get the credential instance under test c = CredentialsMatchers.firstOrNull( - CredentialsProvider.lookupCredentials( + CredentialsProvider.lookupCredentialsInItemGroup( FileCredentials.class, r.jenkins, - ACL.SYSTEM, - (List) null + ACL.SYSTEM2, + null ), CredentialsMatchers.withId("secret-file") ); @@ -191,7 +190,7 @@ public void createFileCredentialsByXml() throws Exception { // now when we re-save the credentials this should encrypt with the instance's secret key SystemCredentialsProvider.getInstance().save(); - assertThat(FileUtils.readFileToString(new File(r.jenkins.getRootDir(), "credentials.xml")), + assertThat(FileUtils.readFileToString(new File(r.jenkins.getRootDir(), "credentials.xml"), StandardCharsets.UTF_8), not(containsString("VGhpcyBpcyBCYXNlNjQgZW5jb2RlZCBwbGFpbiB0ZXh0Cg=="))); }