Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions src/test/java/io/jenkins/plugins/jfrog/AddBuildInfoActionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,41 +11,39 @@
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;
import java.nio.charset.StandardCharsets;
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" +
" \"buildInfoUiUrl\": \"http://127.0.0.1:8081/ui/builds/test/1/1682417678409/published?buildRepo=artifactory-build-info\"\n" +
"}");
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<Action> valueCapture;
private ArgumentCaptor<Action> valueCapture;

@Mock
WorkflowRun run;
private WorkflowRun run;

private static Stream<Arguments> positiveDataProvider() {
static Stream<Arguments> positiveDataProvider() {
return Stream.of(
Arguments.of("rt bp", RT_BP_OUTPUT),
Arguments.of("rt build-publish", RT_BP_OUTPUT)
Expand All @@ -55,16 +52,16 @@ private static Stream<Arguments> 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<Arguments> negativeDataProvider() {
static Stream<Arguments> negativeDataProvider() {
return Stream.of(
Arguments.of("rt u a b", RT_BP_OUTPUT),
Arguments.of("rt bp", "{ \"a\": \"b\" }"),
Expand All @@ -75,7 +72,7 @@ private static Stream<Arguments> 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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Object[]> dataProvider() {
return Arrays.asList(
class ArtifactoryInstallerCheckCliVersionTest {

static Stream<Arguments> 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());
}
}
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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");
Expand All @@ -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";
Expand All @@ -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";
Expand All @@ -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();
Expand Down
56 changes: 30 additions & 26 deletions src/test/java/io/jenkins/plugins/jfrog/CliEnvConfiguratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,45 @@
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");
envVars.put("BUILD_URL", "https://acme.jenkins.io");
}

@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");
Expand All @@ -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);
}
Expand Down
Loading
Loading