diff --git a/src/test/java/io/jenkins/plugins/jfrog/AddBuildInfoActionTest.java b/src/test/java/io/jenkins/plugins/jfrog/AddBuildInfoActionTest.java index b480d59f..6f733a96 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/AddBuildInfoActionTest.java +++ b/src/test/java/io/jenkins/plugins/jfrog/AddBuildInfoActionTest.java @@ -3,7 +3,6 @@ import hudson.model.Action; import org.jenkinsci.plugins.workflow.job.WorkflowRun; import org.jfrog.build.api.util.NullLog; -import org.junit.Rule; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -12,9 +11,9 @@ import org.mockito.Captor; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnit; -import org.mockito.junit.MockitoRule; import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -22,14 +21,15 @@ import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.ArgumentMatchers.isA; import static org.mockito.Mockito.*; /** * @author yahavi **/ @ExtendWith(MockitoExtension.class) -public class AddBuildInfoActionTest { +@MockitoSettings(strictness = Strictness.LENIENT) +class AddBuildInfoActionTest { + private static final String RT_BP_OUTPUT = ("13:14:38 [\uD83D\uDD35Info] Deploying build info...\n" + "13:14:40 [\uD83D\uDD35Info] Build info successfully deployed.\n" + "{\n" + @@ -37,16 +37,13 @@ public class AddBuildInfoActionTest { "}"); private static final String EXPECTED_BUILD_INFO_URL = "http://127.0.0.1:8081/ui/builds/test/1/1682417678409/published?buildRepo=artifactory-build-info"; - @Rule - public MockitoRule rule = MockitoJUnit.rule().silent(); - @Captor - ArgumentCaptor valueCapture; + private ArgumentCaptor valueCapture; @Mock - WorkflowRun run; + private WorkflowRun run; - private static Stream positiveDataProvider() { + static Stream positiveDataProvider() { return Stream.of( Arguments.of("rt bp", RT_BP_OUTPUT), Arguments.of("rt build-publish", RT_BP_OUTPUT) @@ -55,16 +52,16 @@ private static Stream positiveDataProvider() { @ParameterizedTest @MethodSource("positiveDataProvider") - public void addBuildInfoActionPositiveTest(String command, String output) throws IOException { + void addBuildInfoActionPositiveTest(String command, String output) throws IOException { doNothing().when(run).addAction(valueCapture.capture()); runCliCommand(command, output); Mockito.verify(run, times(1)).addAction(isA(Action.class)); assertEquals(1, valueCapture.getAllValues().size()); - assertEquals(valueCapture.getValue().getUrlName(), EXPECTED_BUILD_INFO_URL); + assertEquals(EXPECTED_BUILD_INFO_URL, valueCapture.getValue().getUrlName()); } - private static Stream negativeDataProvider() { + static Stream negativeDataProvider() { return Stream.of( Arguments.of("rt u a b", RT_BP_OUTPUT), Arguments.of("rt bp", "{ \"a\": \"b\" }"), @@ -75,7 +72,7 @@ private static Stream negativeDataProvider() { @ParameterizedTest @MethodSource("negativeDataProvider") - public void addBuildInfoActionNegativeTest(String command, String output) throws IOException { + void addBuildInfoActionNegativeTest(String command, String output) throws IOException { runCliCommand(command, output); Mockito.verify(run, never()).addAction(isA(Action.class)); } diff --git a/src/test/java/io/jenkins/plugins/jfrog/ArtifactoryInstallerCheckCliVersionTest.java b/src/test/java/io/jenkins/plugins/jfrog/ArtifactoryInstallerCheckCliVersionTest.java index 05503eca..00def8d0 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/ArtifactoryInstallerCheckCliVersionTest.java +++ b/src/test/java/io/jenkins/plugins/jfrog/ArtifactoryInstallerCheckCliVersionTest.java @@ -1,49 +1,41 @@ package io.jenkins.plugins.jfrog; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; -import java.util.Arrays; -import java.util.Collection; +import java.util.stream.Stream; import static io.jenkins.plugins.jfrog.ArtifactoryInstaller.*; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * @author yahavi **/ -@RunWith(Parameterized.class) -public class ArtifactoryInstallerCheckCliVersionTest { - private final String inputVersion; - private final String expectedResult; - - public ArtifactoryInstallerCheckCliVersionTest(String inputVersion, String expectedResult) { - this.inputVersion = inputVersion; - this.expectedResult = expectedResult; - } - @Parameterized.Parameters - public static Collection dataProvider() { - return Arrays.asList( +class ArtifactoryInstallerCheckCliVersionTest { + + static Stream dataProvider() { + return Stream.of( // Positive tests - new Object[]{"", null}, - new Object[]{"2.6.1", null}, - new Object[]{"2.7.0", null}, + Arguments.of("", null), + Arguments.of("2.6.1", null), + Arguments.of("2.7.0", null), // Bad syntax - new Object[]{"bad version", BAD_VERSION_PATTERN_ERROR}, - new Object[]{"1.2", BAD_VERSION_PATTERN_ERROR}, - new Object[]{"1.2.a", BAD_VERSION_PATTERN_ERROR}, + Arguments.of("bad version", BAD_VERSION_PATTERN_ERROR), + Arguments.of("1.2", BAD_VERSION_PATTERN_ERROR), + Arguments.of("1.2.a", BAD_VERSION_PATTERN_ERROR), // Versions below minimum - new Object[]{"2.5.9", LOW_VERSION_PATTERN_ERROR}, - new Object[]{"2.6.0", LOW_VERSION_PATTERN_ERROR} + Arguments.of("2.5.9", LOW_VERSION_PATTERN_ERROR), + Arguments.of("2.6.0", LOW_VERSION_PATTERN_ERROR) ); } - @Test - public void testValidateCliVersion() { + @ParameterizedTest + @MethodSource("dataProvider") + void testValidateCliVersion(String inputVersion, String expectedResult) { assertEquals(expectedResult, validateCliVersion(inputVersion).getMessage()); } } diff --git a/src/test/java/io/jenkins/plugins/jfrog/CliEnvConfiguratorProxyTest.java b/src/test/java/io/jenkins/plugins/jfrog/CliEnvConfiguratorProxyTest.java index 982b3e60..f27f6c33 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/CliEnvConfiguratorProxyTest.java +++ b/src/test/java/io/jenkins/plugins/jfrog/CliEnvConfiguratorProxyTest.java @@ -1,30 +1,27 @@ package io.jenkins.plugins.jfrog; import io.jenkins.plugins.jfrog.configuration.JenkinsProxyConfiguration; -import org.junit.Before; -import org.junit.Test; - -import java.io.IOException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static io.jenkins.plugins.jfrog.CliEnvConfigurator.*; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertNull; /** * @author yahavi **/ @SuppressWarnings("HttpUrlsUsage") -public class CliEnvConfiguratorProxyTest extends CliEnvConfiguratorTest { +class CliEnvConfiguratorProxyTest extends CliEnvConfiguratorTest { - @Before - public void setUp() { - super.setUp(); + @BeforeEach + void beforeEach() { proxyConfiguration = new JenkinsProxyConfiguration(); proxyConfiguration.host = "acme.proxy.io"; } @Test - public void configureCliEnvHttpProxyTest() throws IOException, InterruptedException { + void configureCliEnvHttpProxyTest() throws Exception { proxyConfiguration.port = 80; invokeConfigureCliEnv(); assertEnv(envVars, HTTP_PROXY_ENV, "http://acme.proxy.io:80"); @@ -33,7 +30,7 @@ public void configureCliEnvHttpProxyTest() throws IOException, InterruptedExcept } @Test - public void configureCliEnvHttpsProxyTest() throws IOException, InterruptedException { + void configureCliEnvHttpsProxyTest() throws Exception { proxyConfiguration.port = 443; invokeConfigureCliEnv(); assertEnv(envVars, HTTP_PROXY_ENV, "https://acme.proxy.io:443"); @@ -42,7 +39,7 @@ public void configureCliEnvHttpsProxyTest() throws IOException, InterruptedExcep } @Test - public void configureCliEnvHttpProxyAuthTest() throws IOException, InterruptedException { + void configureCliEnvHttpProxyAuthTest() throws Exception { proxyConfiguration.port = 80; proxyConfiguration.username = "andor"; proxyConfiguration.password = "RogueOne"; @@ -53,7 +50,7 @@ public void configureCliEnvHttpProxyAuthTest() throws IOException, InterruptedEx } @Test - public void configureCliEnvHttpsProxyAuthTest() throws IOException, InterruptedException { + void configureCliEnvHttpsProxyAuthTest() throws Exception { proxyConfiguration.port = 443; proxyConfiguration.username = "andor"; proxyConfiguration.password = "RogueOne"; @@ -64,14 +61,14 @@ public void configureCliEnvHttpsProxyAuthTest() throws IOException, InterruptedE } @Test - public void configureCliEnvNoOverrideHttpTest() throws IOException, InterruptedException { + void configureCliEnvNoOverrideHttpTest() throws Exception { envVars.put(HTTP_PROXY_ENV, "http://acme2.proxy.io:777"); invokeConfigureCliEnv(); assertEnv(envVars, HTTP_PROXY_ENV, "http://acme2.proxy.io:777"); } @Test - public void configureCliEnvNoOverrideTest() throws IOException, InterruptedException { + void configureCliEnvNoOverrideTest() throws Exception { envVars.put(HTTP_PROXY_ENV, "http://acme2.proxy.io:80"); envVars.put(HTTPS_PROXY_ENV, "http://acme2.proxy.io:443"); invokeConfigureCliEnv(); diff --git a/src/test/java/io/jenkins/plugins/jfrog/CliEnvConfiguratorTest.java b/src/test/java/io/jenkins/plugins/jfrog/CliEnvConfiguratorTest.java index a0c49179..4dfd0b5a 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/CliEnvConfiguratorTest.java +++ b/src/test/java/io/jenkins/plugins/jfrog/CliEnvConfiguratorTest.java @@ -5,32 +5,36 @@ import io.jenkins.plugins.jfrog.actions.JFrogCliConfigEncryption; import io.jenkins.plugins.jfrog.configuration.JenkinsProxyConfiguration; import jenkins.model.Jenkins; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import java.io.File; -import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import static io.jenkins.plugins.jfrog.CliEnvConfigurator.*; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; /** * @author yahavi **/ -public class CliEnvConfiguratorTest { - @Rule - public JenkinsRule jenkinsRule = new JenkinsRule(); - @Rule - public TemporaryFolder tempFolder = new TemporaryFolder(); - JenkinsProxyConfiguration proxyConfiguration; - EnvVars envVars; - - @Before - public void setUp() { +@WithJenkins +class CliEnvConfiguratorTest { + + protected JenkinsRule jenkinsRule; + protected JenkinsProxyConfiguration proxyConfiguration; + protected EnvVars envVars; + + @TempDir + protected Path tempFolder; + + @BeforeEach + void beforeEach(JenkinsRule rule) { + jenkinsRule = rule; envVars = new EnvVars(); envVars.put("JOB_NAME", "buildName"); envVars.put("BUILD_NUMBER", "1"); @@ -38,8 +42,8 @@ public void setUp() { } @Test - public void configureCliEnvBasicTest() throws IOException, InterruptedException { - File jfrogHomeDir = tempFolder.newFolder("jfrog-home"); + void configureCliEnvBasicTest() throws Exception { + File jfrogHomeDir = Files.createTempDirectory(tempFolder, "jfrog-home").toFile(); FilePath jfrogHomeTempDir = new FilePath(jfrogHomeDir); invokeConfigureCliEnv(jfrogHomeTempDir, new JFrogCliConfigEncryption(envVars)); assertEnv(envVars, JFROG_CLI_BUILD_NAME, "buildName"); @@ -49,36 +53,36 @@ public void configureCliEnvBasicTest() throws IOException, InterruptedException } @Test - public void configEncryptionTest() throws IOException, InterruptedException { + void configEncryptionTest() throws Exception { JFrogCliConfigEncryption configEncryption = new JFrogCliConfigEncryption(envVars); assertTrue(configEncryption.shouldEncrypt()); assertEquals(32, configEncryption.getKey().length()); - invokeConfigureCliEnv(new FilePath(tempFolder.newFolder("encryption-test")), configEncryption); + invokeConfigureCliEnv(new FilePath(Files.createTempDirectory(tempFolder, "encryption-test").toFile()), configEncryption); assertEnv(envVars, JFROG_CLI_ENCRYPTION_KEY, configEncryption.getKeyFilePath()); } @Test - public void configEncryptionWithHomeDirTest() throws IOException, InterruptedException { + void configEncryptionWithHomeDirTest() throws Exception { // Config JFROG_CLI_HOME_DIR to disable key encryption envVars.put(JFROG_CLI_HOME_DIR, "/a/b/c"); JFrogCliConfigEncryption configEncryption = new JFrogCliConfigEncryption(envVars); - File emptyDir = tempFolder.newFolder("empty"); + File emptyDir = Files.createTempDirectory(tempFolder, "empty").toFile(); invokeConfigureCliEnv(new FilePath(emptyDir), configEncryption); assertFalse(configEncryption.shouldEncrypt()); assertFalse(envVars.containsKey(JFROG_CLI_ENCRYPTION_KEY)); } - void assertEnv(EnvVars envVars, String key, String expectedValue) { + static void assertEnv(EnvVars envVars, String key, String expectedValue) { assertEquals(expectedValue, envVars.get(key)); } - void invokeConfigureCliEnv() throws IOException, InterruptedException { - File emptyDir = tempFolder.newFolder("default"); + void invokeConfigureCliEnv() throws Exception { + File emptyDir = Files.createTempDirectory(tempFolder, "default").toFile(); this.invokeConfigureCliEnv(new FilePath(emptyDir), new JFrogCliConfigEncryption(envVars)); } - void invokeConfigureCliEnv(FilePath jfrogHomeTempDir, JFrogCliConfigEncryption configEncryption) throws IOException, InterruptedException { + void invokeConfigureCliEnv(FilePath jfrogHomeTempDir, JFrogCliConfigEncryption configEncryption) throws Exception { setProxyConfiguration(); configureCliEnv(envVars, jfrogHomeTempDir, configEncryption); } diff --git a/src/test/java/io/jenkins/plugins/jfrog/CreateNoProxyValueTest.java b/src/test/java/io/jenkins/plugins/jfrog/CreateNoProxyValueTest.java index 4bd6ec41..d76d2778 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/CreateNoProxyValueTest.java +++ b/src/test/java/io/jenkins/plugins/jfrog/CreateNoProxyValueTest.java @@ -1,50 +1,41 @@ package io.jenkins.plugins.jfrog; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; -import java.util.Arrays; -import java.util.Collection; +import java.util.stream.Stream; import static io.jenkins.plugins.jfrog.CliEnvConfigurator.createNoProxyValue; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * @author nathana **/ -@RunWith(Parameterized.class) -public class CreateNoProxyValueTest { - private final String noProxy; - private final String expectedResult; +class CreateNoProxyValueTest { - public CreateNoProxyValueTest(String noProxy, String expectedResult) { - this.noProxy = noProxy; - this.expectedResult = expectedResult; - } - - @Parameterized.Parameters - public static Collection dataProvider() { - return Arrays.asList( - new Object[]{"artifactory.jfrog.io", "artifactory.jfrog.io"}, - new Object[]{"artifactory.jfrog.io \n artifactory1.jfrog.io ", "artifactory.jfrog.io,artifactory1.jfrog.io"}, - new Object[]{" artifactory.jfrog.io \n \r artifactory1.jfrog.io;artifactory2.jfrog.io \n artifactory3.jfrog.io | artifactory4.jfrog.io \n artifactory5.jfrog.io ", "artifactory.jfrog.io,artifactory1.jfrog.io,artifactory2.jfrog.io,artifactory3.jfrog.io,artifactory4.jfrog.io,artifactory5.jfrog.io"}, - new Object[]{"\r\n", ""}, - new Object[]{";;;", ""}, - new Object[]{",,,", ""}, - new Object[]{"artifactory.jfrog.io;", "artifactory.jfrog.io"}, - new Object[]{"artifactory.jfrog.io,artifactory1.jfrog.io", "artifactory.jfrog.io,artifactory1.jfrog.io"}, - new Object[]{"artifactory.jfrog.io;artifactory1.jfrog.io;artifactory2.jfrog.io;artifactory3.jfrog.io", "artifactory.jfrog.io,artifactory1.jfrog.io,artifactory2.jfrog.io,artifactory3.jfrog.io"}, - new Object[]{"artifactory.jfrog.io|artifactory1.jfrog.io|artifactory2.jfrog.io|artifactory3.jfrog.io", "artifactory.jfrog.io,artifactory1.jfrog.io,artifactory2.jfrog.io,artifactory3.jfrog.io"}, - new Object[]{"artifactory.jfrog.io\nartifactory1.jfrog.io", "artifactory.jfrog.io,artifactory1.jfrog.io"}, - new Object[]{"artifactory.jfrog.io \nartifactory1.jfrog.io\nartifactory2.jfrog.io \n artifactory3.jfrog.io", "artifactory.jfrog.io,artifactory1.jfrog.io,artifactory2.jfrog.io,artifactory3.jfrog.io"}, - new Object[]{";artifactory.jfrog.io;", "artifactory.jfrog.io"}, - new Object[]{",artifactory.jfrog.io,", "artifactory.jfrog.io"} + static Stream dataProvider() { + return Stream.of( + Arguments.of("artifactory.jfrog.io", "artifactory.jfrog.io"), + Arguments.of("artifactory.jfrog.io \n artifactory1.jfrog.io ", "artifactory.jfrog.io,artifactory1.jfrog.io"), + Arguments.of(" artifactory.jfrog.io \n \r artifactory1.jfrog.io;artifactory2.jfrog.io \n artifactory3.jfrog.io | artifactory4.jfrog.io \n artifactory5.jfrog.io ", "artifactory.jfrog.io,artifactory1.jfrog.io,artifactory2.jfrog.io,artifactory3.jfrog.io,artifactory4.jfrog.io,artifactory5.jfrog.io"), + Arguments.of("\r\n", ""), + Arguments.of(";;;", ""), + Arguments.of(",,,", ""), + Arguments.of("artifactory.jfrog.io;", "artifactory.jfrog.io"), + Arguments.of("artifactory.jfrog.io,artifactory1.jfrog.io", "artifactory.jfrog.io,artifactory1.jfrog.io"), + Arguments.of("artifactory.jfrog.io;artifactory1.jfrog.io;artifactory2.jfrog.io;artifactory3.jfrog.io", "artifactory.jfrog.io,artifactory1.jfrog.io,artifactory2.jfrog.io,artifactory3.jfrog.io"), + Arguments.of("artifactory.jfrog.io|artifactory1.jfrog.io|artifactory2.jfrog.io|artifactory3.jfrog.io", "artifactory.jfrog.io,artifactory1.jfrog.io,artifactory2.jfrog.io,artifactory3.jfrog.io"), + Arguments.of("artifactory.jfrog.io\nartifactory1.jfrog.io", "artifactory.jfrog.io,artifactory1.jfrog.io"), + Arguments.of("artifactory.jfrog.io \nartifactory1.jfrog.io\nartifactory2.jfrog.io \n artifactory3.jfrog.io", "artifactory.jfrog.io,artifactory1.jfrog.io,artifactory2.jfrog.io,artifactory3.jfrog.io"), + Arguments.of(";artifactory.jfrog.io;", "artifactory.jfrog.io"), + Arguments.of(",artifactory.jfrog.io,", "artifactory.jfrog.io") ); } - @Test - public void createNoProxyValueTest() { + @ParameterizedTest + @MethodSource("dataProvider") + void createNoProxyValueTest(String noProxy, String expectedResult) { assertEquals(expectedResult, createNoProxyValue(noProxy)); } } diff --git a/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java b/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java index f8675110..1eb945ad 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java +++ b/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java @@ -8,7 +8,6 @@ import io.jenkins.plugins.jfrog.configuration.CredentialsConfig; import io.jenkins.plugins.jfrog.configuration.JFrogPlatformInstance; import org.jfrog.build.client.Version; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -21,21 +20,22 @@ import static io.jenkins.plugins.jfrog.JfStep.Execution.getJFrogCLIPath; import static io.jenkins.plugins.jfrog.JfStep.MIN_CLI_VERSION_PASSWORD_STDIN; import static io.jenkins.plugins.jfrog.JfrogInstallation.JFROG_BINARY_PATH; -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.ArgumentMatchers.any; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; + /** * @author yahavi **/ -public class JfStepTest { +class JfStepTest { @ParameterizedTest @MethodSource("jfrogCLIPathProvider") void getJFrogCLIPathTest(EnvVars inputEnvVars, boolean isWindows, String expectedOutput) { - Assertions.assertEquals(expectedOutput, getJFrogCLIPath(inputEnvVars, isWindows)); + assertEquals(expectedOutput, getJFrogCLIPath(inputEnvVars, isWindows)); } - private static Stream jfrogCLIPathProvider() { + static Stream jfrogCLIPathProvider() { return Stream.of( // Unix agent Arguments.of(new EnvVars(JFROG_BINARY_PATH, "a/b/c"), false, "a/b/c/jf"), @@ -86,7 +86,7 @@ void getJfrogCliVersionTest() throws IOException, InterruptedException { * Plugin launchers do not support password-stdin, as they do not have access to the standard input by default. * * @param cliVersion The CLI version - * @param isPluginLauncher Whether the launcher is a plugin launcher + * @param envVars The env vars * @param expectedOutput The expected output */ @ParameterizedTest @@ -115,7 +115,7 @@ void testAddCredentialsArguments(String cliVersion, EnvVars envVars, String expe assertTrue(builder.toList().contains(expectedOutput)); } - private static Stream provideTestArguments() { + static Stream provideTestArguments() { String passwordFlag = "--password="; String passwordStdinFlag = "--password-stdin"; EnvVars envVarsTrue = mock(EnvVars.class); diff --git a/src/test/java/io/jenkins/plugins/jfrog/JfrogBuilderTest.java b/src/test/java/io/jenkins/plugins/jfrog/JfrogBuilderTest.java index e5435ded..c4310b99 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/JfrogBuilderTest.java +++ b/src/test/java/io/jenkins/plugins/jfrog/JfrogBuilderTest.java @@ -147,14 +147,14 @@ void testPerformWithInvalidCommand() throws Exception { } @Test - void testPerformWithValidJfCommand() throws Exception { + void testPerformWithValidJfCommand() { JfrogBuilder validBuilder = new JfrogBuilder("jf rt ping"); assertNotNull(validBuilder); assertEquals("jf rt ping", validBuilder.getCommand()); } @Test - void testPerformWithValidJfrogCommand() throws Exception { + void testPerformWithValidJfrogCommand() { JfrogBuilder validBuilder = new JfrogBuilder("jfrog rt ping"); assertNotNull(validBuilder); assertEquals("jfrog rt ping", validBuilder.getCommand()); diff --git a/src/test/java/io/jenkins/plugins/jfrog/configuration/JFrogPlatformBuilderTest.java b/src/test/java/io/jenkins/plugins/jfrog/configuration/JFrogPlatformBuilderTest.java index 9c339e03..f6a6a99e 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/configuration/JFrogPlatformBuilderTest.java +++ b/src/test/java/io/jenkins/plugins/jfrog/configuration/JFrogPlatformBuilderTest.java @@ -1,18 +1,18 @@ package io.jenkins.plugins.jfrog.configuration; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author yahavi **/ @SuppressWarnings("HttpUrlsUsage") -public class JFrogPlatformBuilderTest { +class JFrogPlatformBuilderTest { @Test - public void testIsUnsafe() { + void testIsUnsafe() { assertFalse(JFrogPlatformBuilder.isUnsafe(false, "https://acme.jfrog.io")); assertFalse(JFrogPlatformBuilder.isUnsafe(false, "https://acme.jfrog.io")); assertFalse(JFrogPlatformBuilder.isUnsafe(true, "http://acme.jfrog.io")); @@ -23,7 +23,7 @@ public void testIsUnsafe() { } @Test - public void testIsInvalidProtocolOrEmptyUrl() { + void testIsInvalidProtocolOrEmptyUrl() { assertFalse(JFrogPlatformBuilder.isInvalidProtocolOrEmptyUrl("")); assertFalse(JFrogPlatformBuilder.isInvalidProtocolOrEmptyUrl("http://acme.jfrog.io")); assertFalse(JFrogPlatformBuilder.isInvalidProtocolOrEmptyUrl("https://acme.jfrog.io")); diff --git a/src/test/java/io/jenkins/plugins/jfrog/configuration/JFrogPlatformInstanceTest.java b/src/test/java/io/jenkins/plugins/jfrog/configuration/JFrogPlatformInstanceTest.java index 1f44e62b..acfbb162 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/configuration/JFrogPlatformInstanceTest.java +++ b/src/test/java/io/jenkins/plugins/jfrog/configuration/JFrogPlatformInstanceTest.java @@ -1,13 +1,13 @@ package io.jenkins.plugins.jfrog.configuration; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * @author yahavi **/ -public class JFrogPlatformInstanceTest { +class JFrogPlatformInstanceTest { private static final String DISTRIBUTION_URL = "https://acme.jfrog.io/distribution"; private static final String ARTIFACTORY_URL = "https://acme.jfrog.io/artifactory"; @@ -16,7 +16,7 @@ public class JFrogPlatformInstanceTest { private static final String SERVER_ID = "acme"; @Test - public void testInferArtifactoryUrl() { + void testInferArtifactoryUrl() { // Check that Artifactory URL inferred from the platform URL JFrogPlatformInstance jfrogPlatformInstance = new JFrogPlatformInstance(SERVER_ID, URL, null, "", "", ""); assertEquals("", jfrogPlatformInstance.getArtifactoryUrl()); @@ -29,7 +29,7 @@ public void testInferArtifactoryUrl() { } @Test - public void testInferDistributionUrl() { + void testInferDistributionUrl() { // Check that Distribution URL inferred from the platform URL JFrogPlatformInstance jfrogPlatformInstance = new JFrogPlatformInstance(SERVER_ID, URL, null, "", "", ""); assertEquals("", jfrogPlatformInstance.getDistributionUrl()); @@ -42,7 +42,7 @@ public void testInferDistributionUrl() { } @Test - public void testInferXrayUrl() { + void testInferXrayUrl() { // Check that Xray URL inferred from the platform URL JFrogPlatformInstance jfrogPlatformInstance = new JFrogPlatformInstance(SERVER_ID, URL, null, "", "", ""); assertEquals("", jfrogPlatformInstance.getXrayUrl()); diff --git a/src/test/java/io/jenkins/plugins/jfrog/configuration/JenkinsProxyConfigurationTest.java b/src/test/java/io/jenkins/plugins/jfrog/configuration/JenkinsProxyConfigurationTest.java index f3f5d744..647c235c 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/configuration/JenkinsProxyConfigurationTest.java +++ b/src/test/java/io/jenkins/plugins/jfrog/configuration/JenkinsProxyConfigurationTest.java @@ -2,57 +2,57 @@ import jenkins.model.Jenkins; import org.apache.commons.lang3.StringUtils; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.jvnet.hudson.test.JenkinsRule; -import java.util.Arrays; -import java.util.Collection; +import java.util.stream.Stream; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -@RunWith(Parameterized.class) -public class JenkinsProxyConfigurationTest { +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; - @Rule - public JenkinsRule jenkinsRule = new JenkinsRule(); +@WithJenkins +class JenkinsProxyConfigurationTest { private final hudson.ProxyConfiguration jenkinsProxyConfiguration = new hudson.ProxyConfiguration("proxy.jfrog.io", 1234); - private final String url; - public JenkinsProxyConfigurationTest(String url) { - this.url = url; + private JenkinsRule jenkinsRule; + + @BeforeEach + void beforeEach(JenkinsRule rule) { + jenkinsRule = rule; } @SuppressWarnings("HttpUrlsUsage") - @Parameterized.Parameters - public static Collection dataProvider() { - return Arrays.asList( + static Stream dataProvider() { + return Stream.of( // HTTP - new Object[]{"http://acme.jfrog.io"}, - new Object[]{"http://acme.jfrog.io/"}, - new Object[]{"http://acme.jfrog.io/artifactory"}, - new Object[]{"http://acme.jfrog.io:8081/artifactory"}, + Arguments.of("http://acme.jfrog.io"), + Arguments.of("http://acme.jfrog.io/"), + Arguments.of("http://acme.jfrog.io/artifactory"), + Arguments.of("http://acme.jfrog.io:8081/artifactory"), // HTTPS - new Object[]{"https://acme.jfrog.io"}, - new Object[]{"https://acme.jfrog.io/"}, - new Object[]{"https://acme.jfrog.io/artifactory"}, - new Object[]{"https://acme.jfrog.io:8081/artifactory"}, + Arguments.of("https://acme.jfrog.io"), + Arguments.of("https://acme.jfrog.io/"), + Arguments.of("https://acme.jfrog.io/artifactory"), + Arguments.of("https://acme.jfrog.io:8081/artifactory"), // SSH - new Object[]{"ssh://acme.jfrog.io"}, - new Object[]{"ssh://acme.jfrog.io/"}, - new Object[]{"ssh://acme.jfrog.io/artifactory"}, - new Object[]{"ssh://acme.jfrog.io:8081/artifactory"} + Arguments.of("ssh://acme.jfrog.io"), + Arguments.of("ssh://acme.jfrog.io/"), + Arguments.of("ssh://acme.jfrog.io/artifactory"), + Arguments.of("ssh://acme.jfrog.io:8081/artifactory") ); } - @Test - public void testShouldBypassProxy() { + @ParameterizedTest + @MethodSource("dataProvider") + void testShouldBypassProxy(String url) { setupProxy("*"); assertTrue(new JenkinsProxyConfiguration().shouldBypassProxy(url)); diff --git a/src/test/java/io/jenkins/plugins/jfrog/integration/FreestyleJobITest.java b/src/test/java/io/jenkins/plugins/jfrog/integration/FreestyleJobITest.java index ffba9793..e720bbcb 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/integration/FreestyleJobITest.java +++ b/src/test/java/io/jenkins/plugins/jfrog/integration/FreestyleJobITest.java @@ -8,7 +8,7 @@ import io.jenkins.plugins.jfrog.JfrogCliWrapper; import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.Test; -import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -18,16 +18,15 @@ * - JfrogCliWrapper (Build Environment wrapper) * - JfrogBuildInfoPublisher (Post-build action) */ +@WithJenkins class FreestyleJobITest extends PipelineTestBase { /** * JfrogCliWrapper should add JFROG_BINARY_PATH to the build environment. - * - * @param jenkins Jenkins instance injected automatically. */ @Test - public void testWrapperSetsBinaryPath(JenkinsRule jenkins) throws Exception { - setupJenkins(jenkins); + void testWrapperSetsBinaryPath() throws Exception { + setupJenkins(); configureJfrogCliFromReleases(StringUtils.EMPTY, true); FreeStyleProject project = jenkins.createFreeStyleProject("test-wrapper-path"); @@ -47,12 +46,10 @@ public void testWrapperSetsBinaryPath(JenkinsRule jenkins) throws Exception { /** * Without any installation selected, JfrogCliWrapper should still allow the build * to proceed (uses system PATH). - * - * @param jenkins Jenkins instance injected automatically. */ @Test - public void testWrapperNoInstallationUsesSysPath(JenkinsRule jenkins) throws Exception { - setupJenkins(jenkins); + void testWrapperNoInstallationUsesSysPath() throws Exception { + setupJenkins(); FreeStyleProject project = jenkins.createFreeStyleProject("test-wrapper-no-install"); JfrogCliWrapper wrapper = new JfrogCliWrapper(); @@ -67,12 +64,10 @@ public void testWrapperNoInstallationUsesSysPath(JenkinsRule jenkins) throws Exc /** * JfrogBuildInfoPublisher with publishOnlyOnSuccess=true must skip publish * when the build has failed. - * - * @param jenkins Jenkins instance injected automatically. */ @Test - public void testPublisherSkipsOnFailedBuild(JenkinsRule jenkins) throws Exception { - setupJenkins(jenkins); + void testPublisherSkipsOnFailedBuild() throws Exception { + setupJenkins(); FreeStyleProject project = jenkins.createFreeStyleProject("test-publisher-skip"); // Deliberately fail the build @@ -90,12 +85,10 @@ public void testPublisherSkipsOnFailedBuild(JenkinsRule jenkins) throws Exceptio /** * Freestyle flow: wrapper sets environment and build step successfully invokes the JFrog CLI. * Verifies that JFROG_BINARY_PATH points to the installation directory and 'jf -v' succeeds. - * - * @param jenkins Jenkins instance injected automatically. */ @Test - public void testFullFreestyleFlow(JenkinsRule jenkins) throws Exception { - setupJenkins(jenkins); + void testFullFreestyleFlow() throws Exception { + setupJenkins(); configureJfrogCliFromReleases(StringUtils.EMPTY, true); FreeStyleProject project = jenkins.createFreeStyleProject("test-freestyle-full"); @@ -115,12 +108,10 @@ public void testFullFreestyleFlow(JenkinsRule jenkins) throws Exception { /** * JfrogCliWrapper.DescriptorImpl.isApplicable must return true for a regular * FreeStyle project and false for a Matrix project (validated via reflection guard). - * - * @param jenkins Jenkins instance injected automatically. */ @Test - public void testWrapperNotApplicableForMatrixProject(JenkinsRule jenkins) throws Exception { - setupJenkins(jenkins); + void testWrapperNotApplicableForMatrixProject() throws Exception { + setupJenkins(); JfrogCliWrapper.DescriptorImpl descriptor = jenkins.jenkins.getDescriptorByType(JfrogCliWrapper.DescriptorImpl.class); diff --git a/src/test/java/io/jenkins/plugins/jfrog/integration/JFrogInstallationITest.java b/src/test/java/io/jenkins/plugins/jfrog/integration/JFrogInstallationITest.java index 0dbdfa1d..c437c47b 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/integration/JFrogInstallationITest.java +++ b/src/test/java/io/jenkins/plugins/jfrog/integration/JFrogInstallationITest.java @@ -6,7 +6,6 @@ import org.apache.commons.lang3.StringUtils; import org.jenkinsci.plugins.workflow.job.WorkflowRun; import org.junit.jupiter.api.Test; -import org.jvnet.hudson.test.JenkinsRule; import java.io.ByteArrayOutputStream; import java.io.File; @@ -21,72 +20,63 @@ class JFrogInstallationITest extends PipelineTestBase { // JFrog CLI version which is accessible for all operating systems. - private static final String jfrogCliTestVersion = "2.29.2"; + private static final String JFROG_CLI_TEST_VERSION = "2.29.2"; /** * Download Jfrog CLI from 'releases.jfrog.io' and adds it as a global tool. - * - * @param jenkins Jenkins instance injected automatically. */ @Test - public void testJFrogCliInstallationFromReleases(JenkinsRule jenkins) throws Exception { - setupJenkins(jenkins); + void testJFrogCliInstallationFromReleases() throws Exception { + setupJenkins(); // Download specific CLI version. - configureJfrogCliFromReleases(jfrogCliTestVersion, true); - WorkflowRun job = runPipeline(jenkins, "basic_version_command"); + configureJfrogCliFromReleases(JFROG_CLI_TEST_VERSION, true); + WorkflowRun job = runPipeline("basic_version_command"); // Verify specific version was installed. - assertTrue(job.getLog().contains("jf version " + jfrogCliTestVersion)); + assertTrue(job.getLog().contains("jf version " + JFROG_CLI_TEST_VERSION)); // Download the latest CLI version. configureJfrogCliFromReleases(StringUtils.EMPTY, true); - job = runPipeline(jenkins, "basic_version_command"); + job = runPipeline("basic_version_command"); // Verify newer version was installed. - assertFalse(job.getLog().contains("jf version " + jfrogCliTestVersion)); + assertFalse(job.getLog().contains("jf version " + JFROG_CLI_TEST_VERSION)); } /** * Download JFrog CLI from client Artifactory and adds it as a global tool. - * - * @param jenkins Jenkins instance injected automatically. */ @Test - public void testJFrogCliInstallationFromArtifactory(JenkinsRule jenkins) throws Exception { - setupJenkins(jenkins); + void testJFrogCliInstallationFromArtifactory() throws Exception { // Download the latest CLI version. // Using remote repository to 'releases.jfrog.io' in the client's Artifactory. configureJfrogCliFromArtifactory(JFROG_CLI_TOOL_NAME_1, TEST_CONFIGURED_SERVER_ID, getRepoKey(TestRepository.CLI_REMOTE_REPO), true); - WorkflowRun job = runPipeline(jenkins, "basic_version_command"); + WorkflowRun job = runPipeline("basic_version_command"); assertTrue(job.getLog().contains("jf version ")); } // Gets JfrogInstallation directory in Jenkins work root. - private Path getJfrogInstallationDir(JenkinsRule jenkins) { + private Path getJfrogInstallationDir() { return jenkins.jenkins.getRootDir().toPath().resolve("tools").resolve("io.jenkins.plugins.jfrog.JfrogInstallation"); } /** * Check that only one copy of JFrog CLI is being downloaded to the expected location when running multiple jobs. - * - * @param jenkins Jenkins instance injected automatically. */ @Test - public void testDownloadingJFrogCliOnce(JenkinsRule jenkins) throws Exception { - initPipelineTest(jenkins); + void testDownloadingJFrogCliOnce() throws Exception { + initPipelineTest(); // After running job for the first time, CLI's binary should be downloaded. - runPipeline(jenkins, "basic_version_command"); - long lastModified = getCliLastModified(jenkins); + runPipeline("basic_version_command"); + long lastModified = getCliLastModified(); // Rerunning the job and verifying that the binary was not downloaded again by comparing the modification time. - runPipeline(jenkins, "basic_version_command"); - assertEquals(lastModified, getCliLastModified(jenkins)); + runPipeline("basic_version_command"); + assertEquals(lastModified, getCliLastModified()); } /** * Finds the binary file in the CLI tool directory and returns its modification time. - * - * @param jenkins Jenkins instance injected automatically. * @return binary's modification time. */ - private long getCliLastModified(JenkinsRule jenkins) { - Path binaryPath = getJfrogInstallationDir(jenkins); + private long getCliLastModified() { + Path binaryPath = getJfrogInstallationDir(); String name = BINARY_NAME; if (!jenkins.createLocalLauncher().isUnix()) { name += ".exe"; @@ -98,26 +88,24 @@ private long getCliLastModified(JenkinsRule jenkins) { /** * Check that only one copy of the Maven extractor is downloaded to the expected 'dependencies' directory. - * - * @param jenkins Jenkins instance injected automatically. */ @Test - public void testDownloadingMavenExtractor(JenkinsRule jenkins) throws Exception { - initPipelineTest(jenkins); + void testDownloadingMavenExtractor() throws Exception { + initPipelineTest(); // After running job for the first time, Mvn extractor should be downloaded. - runPipeline(jenkins, "mvn_command"); - Path mvnDependenciesDirPath = getJfrogInstallationDir(jenkins).resolve(JfrogDependenciesDirName).resolve("maven"); + runPipeline("mvn_command"); + Path mvnDependenciesDirPath = getJfrogInstallationDir().resolve(JfrogDependenciesDirName).resolve("maven"); long lastModified = getExtractorLastModified(mvnDependenciesDirPath); // Rerunning the job and verifying that the extractor was not downloaded again by comparing the modification time. - runPipeline(jenkins, "mvn_command"); + runPipeline("mvn_command"); assertEquals(lastModified, getExtractorLastModified(mvnDependenciesDirPath)); } /** - * Finds the extractor' jar in the mvn extractor directory and returns its modification time. + * Finds the extractor jar in the mvn extractor directory and returns its modification time. * * @param mvnDependenciesDirPath maven's extractors directory inside the dependencies' directory. - * @return extractor' jar modification time. + * @return extractor jar modification time. */ private long getExtractorLastModified(Path mvnDependenciesDirPath) throws IOException { File[] fileList = mvnDependenciesDirPath.toFile().listFiles(); @@ -134,52 +122,46 @@ private long getExtractorLastModified(Path mvnDependenciesDirPath) throws IOExce /** * Configure two JFrog CLI tools, one with Releases installer and one with Artifactory installer, and test functionality for both. - * - * @param jenkins Jenkins instance injected automatically. */ @Test - public void testCombineReleasesAndArtifactoryTools(JenkinsRule jenkins) throws Exception { - setupJenkins(jenkins); + void testCombineReleasesAndArtifactoryTools() throws Exception { + setupJenkins(); // Download the latest CLI version from releases.jfrog.io. configureJfrogCliFromReleases(StringUtils.EMPTY, false); // Download the latest CLI version from Artifactory. configureJfrogCliFromArtifactory(JFROG_CLI_TOOL_NAME_2, TEST_CONFIGURED_SERVER_ID, getRepoKey(TestRepository.CLI_REMOTE_REPO), false); - runPipeline(jenkins, "basic_commands"); - runPipeline(jenkins, "basic_commands_2"); + runPipeline("basic_commands"); + runPipeline("basic_commands_2"); } /** * Configure two JFrog CLI tools, one with Releases installer and one with Artifactory installer, and test functionality for both. - * - * @param jenkins Jenkins instance injected automatically. */ @Test - public void testCombineReleasesAndArtifactoryToolsDifferentOrder(JenkinsRule jenkins) throws Exception { - setupJenkins(jenkins); + void testCombineReleasesAndArtifactoryToolsDifferentOrder() throws Exception { + setupJenkins(); // Download the latest CLI version from Artifactory. configureJfrogCliFromArtifactory(JFROG_CLI_TOOL_NAME_2, TEST_CONFIGURED_SERVER_ID, getRepoKey(TestRepository.CLI_REMOTE_REPO), false); // Download a specific CLI version from releases.jfrog.io. - configureJfrogCliFromReleases(jfrogCliTestVersion, false); + configureJfrogCliFromReleases(JFROG_CLI_TEST_VERSION, false); // Running CLI installed from releases.jfrog.io and verify specific version was installed. - WorkflowRun job = runPipeline(jenkins, "basic_commands"); - assertTrue(job.getLog().contains("jf version " + jfrogCliTestVersion)); + WorkflowRun job = runPipeline("basic_commands"); + assertTrue(job.getLog().contains("jf version " + JFROG_CLI_TEST_VERSION)); // Running CLI installed from Artifactory and verify the latest version was installed. - job = runPipeline(jenkins, "basic_commands_2"); - assertFalse(job.getLog().contains("jf version " + jfrogCliTestVersion)); + job = runPipeline("basic_commands_2"); + assertFalse(job.getLog().contains("jf version " + JFROG_CLI_TEST_VERSION)); } /** * Configure JFrog CLI tool and test functionality for the build-info action. - * - * @param jenkins Jenkins instance injected automatically. */ @Test - public void testBuildInfoAction(JenkinsRule jenkins) throws Exception { - setupJenkins(jenkins); + void testBuildInfoAction() throws Exception { + setupJenkins(); // Download a specific CLI version from releases.jfrog.io. configureJfrogCliFromReleases(Strings.EMPTY, false); // Running CLI installed from releases.jfrog.io and verify specific version was installed. - WorkflowRun job = runPipeline(jenkins, "build_info"); + WorkflowRun job = runPipeline("build_info"); // Assert build info published assertTrue(job.getLog().contains("Build info successfully deployed")); // Check build-info Action @@ -192,26 +174,24 @@ public void testBuildInfoAction(JenkinsRule jenkins) throws Exception { /** * Test the functionality of providing arguments as list: jf (['rt', 'ping']) - * - * @param jenkins Jenkins instance injected automatically. */ @Test - public void testArgList(JenkinsRule jenkins) throws Exception { - setupJenkins(jenkins); + void testArgList() throws Exception { + setupJenkins(); // Download the latest CLI version from releases.jfrog.io. configureJfrogCliFromReleases(StringUtils.EMPTY, false); // Download the latest CLI version from Artifactory. configureJfrogCliFromArtifactory(JFROG_CLI_TOOL_NAME_1, TEST_CONFIGURED_SERVER_ID, getRepoKey(TestRepository.CLI_REMOTE_REPO), false); - runPipeline(jenkins, "basic_commands_array"); + runPipeline("basic_commands_array"); } @Test - public void testConfigurationAsCode(JenkinsRule jenkins) throws Exception { - setupJenkins(jenkins); + void testConfigurationAsCode() throws Exception { + setupJenkins(); // Download the latest CLI version from Artifactory. configureJfrogCliFromArtifactory(JFROG_CLI_TOOL_NAME_2, TEST_CONFIGURED_SERVER_ID, getRepoKey(TestRepository.CLI_REMOTE_REPO), false); // Download a specific CLI version from releases.jfrog.io. - configureJfrogCliFromReleases(jfrogCliTestVersion, false); + configureJfrogCliFromReleases(JFROG_CLI_TEST_VERSION, false); try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { // Export configuration to a local variable @@ -228,13 +208,13 @@ public void testConfigurationAsCode(JenkinsRule jenkins) throws Exception { } @Test - public void testOutput(JenkinsRule jenkins) throws Exception { - setupJenkins(jenkins); + void testOutput() throws Exception { + setupJenkins(); // Download the latest CLI version from releases.jfrog.io. configureJfrogCliFromReleases(StringUtils.EMPTY, false); // Run pipeline that asserts the output is correct - runPipeline(jenkins, "version_output"); + runPipeline("version_output"); } } diff --git a/src/test/java/io/jenkins/plugins/jfrog/integration/ParallelInstallITest.java b/src/test/java/io/jenkins/plugins/jfrog/integration/ParallelInstallITest.java index 3e5dbbe7..f6c76433 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/integration/ParallelInstallITest.java +++ b/src/test/java/io/jenkins/plugins/jfrog/integration/ParallelInstallITest.java @@ -3,70 +3,63 @@ import io.jenkins.plugins.jfrog.ReleasesInstaller; import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.Test; -import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; /** * Integration tests verifying that parallel JFrog CLI installations do not corrupt * the binary or produce race conditions. */ +@WithJenkins class ParallelInstallITest extends PipelineTestBase { /** * Two parallel pipeline stages that share the same JFrog CLI tool must both * complete successfully without binary corruption. - * - * @param jenkins Jenkins instance injected automatically. */ @Test - public void testParallelStagesSameToolVersion(JenkinsRule jenkins) throws Exception { - setupJenkins(jenkins); + void testParallelStagesSameToolVersion() throws Exception { + setupJenkins(); configureJfrogCliFromReleases(StringUtils.EMPTY, true); - runPipeline(jenkins, "parallel_install"); + runPipeline("parallel_install"); } /** * Two parallel pipeline stages each using a different CLI version must both * complete and produce independent, valid binaries. - * - * @param jenkins Jenkins instance injected automatically. */ @Test - public void testParallelStagesTwoToolVersions(JenkinsRule jenkins) throws Exception { - setupJenkins(jenkins); + void testParallelStagesTwoToolVersions() throws Exception { + setupJenkins(); // Tool 1: latest CLI version configureJfrogCliFromReleases(StringUtils.EMPTY, true); // Tool 2: a known older specific version to verify independent parallel installs ReleasesInstaller installer = new ReleasesInstaller(); installer.setVersion("2.29.2"); configureJfrogCliTool(JFROG_CLI_TOOL_NAME_2, installer, false); - runPipeline(jenkins, "parallel_install_two_tools"); + runPipeline("parallel_install_two_tools"); } /** * Repeated installs of the same version should detect the valid cached binary * and skip re-download (verified by both runs succeeding without error). - * - * @param jenkins Jenkins instance injected automatically. */ @Test - public void testRepeatedInstallSkipsDownload(JenkinsRule jenkins) throws Exception { - setupJenkins(jenkins); + void testRepeatedInstallSkipsDownload() throws Exception { + setupJenkins(); configureJfrogCliFromReleases(StringUtils.EMPTY, true); // First run installs - runPipeline(jenkins, "basic_version_command"); + runPipeline("basic_version_command"); // Second run should hit the cache - runPipeline(jenkins, "basic_version_command"); + runPipeline("basic_version_command"); } /** * Install from Artifactory in parallel: both stages must succeed. - * - * @param jenkins Jenkins instance injected automatically. */ @Test - public void testParallelInstallFromArtifactory(JenkinsRule jenkins) throws Exception { - setupJenkins(jenkins); + void testParallelInstallFromArtifactory() throws Exception { + setupJenkins(); configureJfrogCliFromArtifactory(JFROG_CLI_TOOL_NAME_1, TEST_CONFIGURED_SERVER_ID, getRepoKey(TestRepository.CLI_REMOTE_REPO), true); - runPipeline(jenkins, "parallel_install"); + runPipeline("parallel_install"); } } diff --git a/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java b/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java index 220056dc..b0bdfbd9 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java +++ b/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java @@ -22,7 +22,6 @@ import io.jenkins.plugins.jfrog.configuration.CredentialsConfig; import io.jenkins.plugins.jfrog.configuration.JFrogPlatformBuilder; import io.jenkins.plugins.jfrog.configuration.JFrogPlatformInstance; -import io.jenkins.plugins.jfrog.jenkins.EnableJenkins; import jenkins.model.Jenkins; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; @@ -36,9 +35,9 @@ import org.jfrog.artifactory.client.ArtifactoryRequest; import org.jfrog.artifactory.client.ArtifactoryResponse; import org.jfrog.artifactory.client.impl.ArtifactoryRequestImpl; -import org.junit.Assert; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.jvnet.hudson.test.JenkinsRule; import java.io.IOException; @@ -46,39 +45,40 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.*; + +@WithJenkins +class PipelineTestBase { -@EnableJenkins -public class PipelineTestBase { private static long currentTime; private static Artifactory artifactoryClient; protected JenkinsRule jenkins; protected Slave slave; private static StringSubstitutor pipelineSubstitution; private static final String SLAVE_LABEL = "TestSlave"; - static final String PLATFORM_URL = System.getenv("JFROG_URL"); + protected static final String PLATFORM_URL = System.getenv("JFROG_URL"); private static final String ARTIFACTORY_URL = StringUtils.removeEnd(PLATFORM_URL, "/") + "/artifactory"; private static final String ARTIFACTORY_USERNAME = System.getenv("JFROG_USERNAME"); private static final String ARTIFACTORY_PASSWORD = System.getenv("JFROG_PASSWORD"); private static final String ACCESS_TOKEN = System.getenv("JFROG_ADMIN_TOKEN"); protected static final Path INTEGRATION_BASE_PATH = Paths.get(".").toAbsolutePath().normalize() .resolve(Paths.get("src", "test", "resources", "integration")); - static final String JFROG_CLI_TOOL_NAME_1 = "jfrog-cli"; - static final String JFROG_CLI_TOOL_NAME_2 = "jfrog-cli-2"; - static final String TEST_CONFIGURED_SERVER_ID = "serverId"; + protected static final String JFROG_CLI_TOOL_NAME_1 = "jfrog-cli"; + protected static final String JFROG_CLI_TOOL_NAME_2 = "jfrog-cli-2"; + protected static final String TEST_CONFIGURED_SERVER_ID = "serverId"; // Set up jenkins and configure latest JFrog CLI. - public void initPipelineTest(JenkinsRule jenkins) throws Exception { - setupJenkins(jenkins); + protected void initPipelineTest() throws Exception { + setupJenkins(); // Download the latest CLI version. configureJfrogCliFromReleases(StringUtils.EMPTY, true); } - // Set up test' environment + // Set up test environment @BeforeAll - public static void setupEnvironment() { + static void beforeAll() { currentTime = System.currentTimeMillis(); verifyEnvironment(); createClients(); @@ -87,15 +87,19 @@ public static void setupEnvironment() { Arrays.stream(TestRepository.values()).forEach(PipelineTestBase::createRepo); } + @BeforeEach + void beforeEach(JenkinsRule rule) { + jenkins = rule; + } + @AfterAll - public static void tearDown() { + static void afterAll() { // Remove repositories Arrays.stream(TestRepository.values()).forEach(PipelineTestBase::removeRepo); artifactoryClient.close(); } - public void setupJenkins(JenkinsRule jenkins) throws IOException { - this.jenkins = jenkins; + protected void setupJenkins() throws IOException { createSlave(); setGlobalConfiguration(); } @@ -126,7 +130,7 @@ private void createSlave() { * @param name - Pipeline name from 'jenkins-jfrog-plugin/src/test/resources/integration/pipelines' * @return the Jenkins job */ - WorkflowRun runPipeline(JenkinsRule jenkins, String name) throws Exception { + protected WorkflowRun runPipeline(String name) throws Exception { WorkflowJob project = jenkins.createProject(WorkflowJob.class); FilePath slaveWs = slave.getWorkspaceFor(project); if (slaveWs == null) { @@ -159,7 +163,7 @@ private static void verifyEnvironment() { */ private void setGlobalConfiguration() throws IOException { JFrogPlatformBuilder.DescriptorImpl jfrogBuilder = (JFrogPlatformBuilder.DescriptorImpl) jenkins.getInstance().getDescriptor(JFrogPlatformBuilder.class); - Assert.assertNotNull(jfrogBuilder); + assertNotNull(jfrogBuilder); CredentialsConfig platformCred = new CredentialsConfig(Secret.fromString(ARTIFACTORY_USERNAME), Secret.fromString(ARTIFACTORY_PASSWORD), Secret.fromString(ACCESS_TOKEN), "credentials"); List artifactoryServers = new ArrayList<>() {{ // Dummy server to test multiple configured servers. @@ -265,7 +269,7 @@ private static void createPipelineSubstitution() { * @param path - Filesystem path to fix * @return path compatible with Windows */ - static String fixWindowsPath(String path) { + protected static String fixWindowsPath(String path) { return StringUtils.replace(path, "\\", "\\\\"); } @@ -282,13 +286,13 @@ private String readPipeline(String name) throws IOException { return pipelineSubstitution.replace(pipeline); } - public static void configureJfrogCliFromReleases(String cliVersion, Boolean override) throws Exception { + protected static void configureJfrogCliFromReleases(String cliVersion, Boolean override) throws Exception { ReleasesInstaller releasesInstaller = new ReleasesInstaller(); releasesInstaller.setVersion(cliVersion); configureJfrogCliTool(JFROG_CLI_TOOL_NAME_1, releasesInstaller, override); } - public static void configureJfrogCliFromArtifactory(String toolName, String serverId, String repo, Boolean override) throws Exception { + protected static void configureJfrogCliFromArtifactory(String toolName, String serverId, String repo, Boolean override) throws Exception { configureJfrogCliTool(toolName, new ArtifactoryInstaller(serverId, repo, ""), override); } @@ -300,7 +304,7 @@ public static void configureJfrogCliFromArtifactory(String toolName, String serv * @param override The tool will override pre-configured ones and be set if true, otherwise it will be added to the installation array. * @throws IOException failed to configure the new tool. */ - public static void configureJfrogCliTool(String toolName, BinaryInstaller installer, Boolean override) throws Exception { + protected static void configureJfrogCliTool(String toolName, BinaryInstaller installer, Boolean override) throws Exception { Saveable NOOP = () -> { }; DescribableList, ToolPropertyDescriptor> toolProperties = new DescribableList<>(NOOP); diff --git a/src/test/java/io/jenkins/plugins/jfrog/integration/TestRepository.java b/src/test/java/io/jenkins/plugins/jfrog/integration/TestRepository.java index cd097a67..eb78d3fb 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/integration/TestRepository.java +++ b/src/test/java/io/jenkins/plugins/jfrog/integration/TestRepository.java @@ -1,5 +1,8 @@ package io.jenkins.plugins.jfrog.integration; +import lombok.Getter; + +@Getter enum TestRepository { LOCAL_REPO("jenkins-jfrog-tests-local", RepoType.LOCAL), CLI_REMOTE_REPO("jenkins-jfrog-tests-cli-remote", RepoType.REMOTE); @@ -17,10 +20,6 @@ enum RepoType { this.repoType = repoType; } - public String getRepoName() { - return repoName; - } - @Override public String toString() { return getRepoName(); diff --git a/src/test/java/io/jenkins/plugins/jfrog/jenkins/EnableJenkins.java b/src/test/java/io/jenkins/plugins/jfrog/jenkins/EnableJenkins.java deleted file mode 100644 index f59a7d51..00000000 --- a/src/test/java/io/jenkins/plugins/jfrog/jenkins/EnableJenkins.java +++ /dev/null @@ -1,54 +0,0 @@ -package io.jenkins.plugins.jfrog.jenkins; - - -import org.junit.jupiter.api.extension.ExtendWith; - -import java.lang.annotation.*; - -/** - * JUnit 5 meta annotation providing {@link org.jvnet.hudson.test.JenkinsRule JenkinsRule} integration. - * - *

- * Test methods using the rule extension need to accept it by {@link org.jvnet.hudson.test.JenkinsRule JenkinsRule} parameter; each test case - * gets a new rule object. - *

- * Annotating a class provides access for all of its tests. Unrelated test cases can omit the parameter. - * - *

- * @EnableJenkins
- * class ExampleJUnit5Test {
- *
- *     @Test
- *     public void example(JenkinsRule r) {
- *         // use 'r' ...
- *     }
- *
- *     @Test
- *     public void exampleNotUsingRule() {
- *         // ...
- *     }
- * }
- * 
- *

- * Annotating a method limits access to the method. - * - *

- * class ExampleJUnit5Test {
- *
- *     @EnableJenkins
- *     @Test
- *     public void example(JenkinsRule r) {
- *         // use 'r' ...
- *     }
- * }
- * 
- * - * @see JenkinsExtension - * @see org.junit.jupiter.api.extension.ExtendWith - */ -@Target({ElementType.TYPE, ElementType.METHOD}) -@Retention(RetentionPolicy.RUNTIME) -@Documented -@ExtendWith(JenkinsExtension.class) -public @interface EnableJenkins { -} diff --git a/src/test/java/io/jenkins/plugins/jfrog/jenkins/JUnit5JenkinsRule.java b/src/test/java/io/jenkins/plugins/jfrog/jenkins/JUnit5JenkinsRule.java deleted file mode 100644 index 15c9d821..00000000 --- a/src/test/java/io/jenkins/plugins/jfrog/jenkins/JUnit5JenkinsRule.java +++ /dev/null @@ -1,36 +0,0 @@ -package io.jenkins.plugins.jfrog.jenkins; - -import edu.umd.cs.findbugs.annotations.NonNull; -import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.jupiter.api.extension.ParameterContext; -import org.junit.runner.Description; -import org.jvnet.hudson.test.JenkinsRecipe; -import org.jvnet.hudson.test.JenkinsRule; - -import java.lang.reflect.Method; - -/** - * Provides JUnit 5 compatibility for {@link JenkinsRule}. - */ -class JUnit5JenkinsRule extends JenkinsRule { - private final ParameterContext context; - - JUnit5JenkinsRule(@NonNull ParameterContext context, @NonNull ExtensionContext extensionContext) { - this.context = context; - this.testDescription = Description.createTestDescription( - extensionContext.getTestClass().map(Class::getName).orElse(null), - extensionContext.getTestMethod().map(Method::getName).orElse(null)); - } - - @Override - public void recipe() throws Exception { - JenkinsRecipe jenkinsRecipe = context.findAnnotation(JenkinsRecipe.class).orElse(null); - if (jenkinsRecipe == null) { - return; - } - @SuppressWarnings("unchecked") final JenkinsRecipe.Runner runner = - (JenkinsRecipe.Runner) jenkinsRecipe.value().getDeclaredConstructor().newInstance(); - recipes.add(runner); - tearDowns.add(() -> runner.tearDown(this, jenkinsRecipe)); - } -} diff --git a/src/test/java/io/jenkins/plugins/jfrog/jenkins/JenkinsExtension.java b/src/test/java/io/jenkins/plugins/jfrog/jenkins/JenkinsExtension.java deleted file mode 100644 index 15e070d7..00000000 --- a/src/test/java/io/jenkins/plugins/jfrog/jenkins/JenkinsExtension.java +++ /dev/null @@ -1,43 +0,0 @@ -package io.jenkins.plugins.jfrog.jenkins; - - -import org.junit.jupiter.api.extension.*; -import org.jvnet.hudson.test.JenkinsRule; - -/** - * JUnit 5 extension providing {@link JenkinsRule} integration. - * - * @see EnableJenkins - */ -class JenkinsExtension implements ParameterResolver, AfterEachCallback { - - private static final String KEY = "jenkins-instance"; - private static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace.create(JenkinsExtension.class); - - @Override - public void afterEach(ExtensionContext context) throws Exception { - final JenkinsRule rule = context.getStore(NAMESPACE).remove(KEY, JenkinsRule.class); - if (rule == null) { - return; - } - rule.after(); - } - - @Override - public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { - return parameterContext.getParameter().getType().equals(JenkinsRule.class); - } - - @Override - public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { - final JenkinsRule rule = extensionContext.getStore(NAMESPACE).getOrComputeIfAbsent(KEY, key - -> new JUnit5JenkinsRule(parameterContext, extensionContext), JenkinsRule.class); - - try { - rule.before(); - return rule; - } catch (Throwable t) { - throw new ParameterResolutionException(t.getMessage(), t); - } - } -} \ No newline at end of file