Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yahavi committed Aug 24, 2023
1 parent 4e5f01d commit d79abaa
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;

import static org.jfrog.gradle.plugin.artifactory.Constant.*;
import static org.jfrog.gradle.plugin.artifactory.utils.Utils.createDeployableArtifactsFile;

public class GradleFunctionalTestBase {
// ArtifactoryManager
Expand Down Expand Up @@ -90,13 +92,14 @@ public void runPublishTest(String gradleVersion, Path sourceDir, ValidationUtils
}

public interface TestEnvCreator {
void create() throws IOException;
void create(String deployableArtifacts) throws IOException;
}

public void runPublishCITest(String gradleVersion, Path sourceDir, boolean cleanUp, TestEnvCreator testEnvCreator, ValidationUtils.BuildResultValidation validation) throws IOException {
public void runPublishCITest(String gradleVersion, Path sourceDir, boolean cleanUp, TestEnvCreator testEnvCreator, ValidationUtils.CiBuildResultValidation validation) throws IOException {
// Create test environment
Utils.createTestDir(sourceDir);
testEnvCreator.create();
Path deployableArtifacts = createDeployableArtifactsFile();
testEnvCreator.create(deployableArtifacts.toString());
Map<String, String> extendedEnv = new HashMap<>(envVars) {{
put(BuildInfoConfigProperties.PROP_PROPS_FILE, TestConstant.BUILD_INFO_PROPERTIES_TARGET.toString());
put(RESOLUTION_URL_ENV, getArtifactoryUrl() + virtualRepo);
Expand All @@ -105,12 +108,13 @@ public void runPublishCITest(String gradleVersion, Path sourceDir, boolean clean
}};
// Run Gradle
BuildResult buildResult = Utils.runGradleArtifactoryPublish(gradleVersion, extendedEnv, true);
validation.validate(buildResult);
validation.validate(buildResult, deployableArtifacts);
// Cleanup
if (cleanUp) {
Pair<String, String> buildDetails = Utils.getBuildDetails(buildResult);
Utils.cleanTestBuilds(artifactoryManager, buildDetails.getLeft(), buildDetails.getRight(), null);
}
Files.deleteIfExists(deployableArtifacts);
}

private void initArtifactoryManager() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public void androidTest() throws IOException {

@Test
public void androidCiTest() throws IOException {
runPublishCITest(GRADLE_ANDROID_VERSION, TestConstant.ANDROID_GRADLE_CI_EXAMPLE, true, () -> Utils.generateBuildInfoProperties(this, "", true, true),
this::checkBuildResults);
runPublishCITest(GRADLE_ANDROID_VERSION, TestConstant.ANDROID_GRADLE_CI_EXAMPLE, true, (deployableArtifacts) -> Utils.generateBuildInfoProperties(this, "", true, true, ""),
(buildResult, deployableArtifacts) -> checkBuildResults(buildResult));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ public class PluginBomPublishTest extends GradleFunctionalTestBase {
@Test(dataProvider = "gradleVersions")
public void publishDefaultBomTest(String gradleVersion) throws IOException {
runPublishCITest(gradleVersion, TestConstant.GRADLE_EXAMPLE_DEFAULT_BOM, true,
() -> Utils.generateBuildInfoProperties(this, "", true, true),
buildResult -> ValidationUtils.checkBomBuild(buildResult, TestConstant.BUILD_INFO_JSON.toFile(), 2)
(deployableArtifacts) -> Utils.generateBuildInfoProperties(this, "", true, true, ""),
(buildResult, deployableArtifacts) -> ValidationUtils.checkBomBuild(buildResult, TestConstant.BUILD_INFO_JSON.toFile(), 2)
);
}

@Test(dataProvider = "gradleVersions")
public void publishCustomBomTest(String gradleVersion) throws IOException {
runPublishCITest(gradleVersion, TestConstant.GRADLE_EXAMPLE_CUSTOM_BOM, true,
() -> Utils.generateBuildInfoProperties(this, "customMavenJavaPlatform", true, true),
buildResult -> ValidationUtils.checkBomBuild(buildResult, TestConstant.BUILD_INFO_JSON.toFile(), 2)
(deployableArtifacts) -> Utils.generateBuildInfoProperties(this, "customMavenJavaPlatform", true, true, ""),
(buildResult, deployableArtifacts) -> ValidationUtils.checkBomBuild(buildResult, TestConstant.BUILD_INFO_JSON.toFile(), 2)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,40 @@ public class PluginCiPublishTest extends GradleFunctionalTestBase {
@Test(dataProvider = "gradleVersions")
public void ciServerTest(String gradleVersion) throws IOException {
runPublishCITest(gradleVersion, TestConstant.GRADLE_EXAMPLE_CI_SERVER, true,
() -> Utils.generateBuildInfoProperties(this, "", true, true),
buildResult -> ValidationUtils.checkBuildResults(artifactoryManager, buildResult, localRepo)
(deployableArtifacts) -> Utils.generateBuildInfoProperties(this, "", true, true, deployableArtifacts),
(buildResult, deployableArtifacts) -> ValidationUtils.checkBuildResults(artifactoryManager, buildResult, localRepo, deployableArtifacts)
);
}

@Test(dataProvider = "gradleVersions")
public void ciServerResolverOnlyTest(String gradleVersion) throws IOException {
runPublishCITest(gradleVersion, TestConstant.GRADLE_EXAMPLE_CI_SERVER, false,
() -> Utils.generateBuildInfoProperties(this, "", false, false),
buildResult -> ValidationUtils.checkLocalBuild(buildResult, TestConstant.BUILD_INFO_JSON.toFile(), 2, 0)
(deployableArtifacts) -> Utils.generateBuildInfoProperties(this, "", false, false, ""),
(buildResult, deployableArtifacts) -> ValidationUtils.checkLocalBuild(buildResult, TestConstant.BUILD_INFO_JSON.toFile(), 2, 0)
);
}

@Test(dataProvider = "gradleVersions")
public void ciServerPublicationsTest(String gradleVersion) throws IOException {
runPublishCITest(gradleVersion, TestConstant.GRADLE_EXAMPLE_CI_SERVER, true,
() -> Utils.generateBuildInfoProperties(this, "mavenJava,customIvyPublication", true, true),
buildResult -> ValidationUtils.checkBuildResults(artifactoryManager, buildResult, localRepo)
(deployableArtifacts) -> Utils.generateBuildInfoProperties(this, "mavenJava,customIvyPublication", true, true, deployableArtifacts),
(buildResult, deployableArtifacts) -> ValidationUtils.checkBuildResults(artifactoryManager, buildResult, localRepo, deployableArtifacts)
);
}

@Test(dataProvider = "gradleVersions")
public void ciRequestedByTest(String gradleVersion) throws IOException {
runPublishCITest(gradleVersion, TestConstant.GRADLE_EXAMPLE_CI_SERVER, false,
() -> Utils.generateBuildInfoProperties(this, "mavenJava,customIvyPublication", false, true),
buildResult -> ValidationUtils.checkLocalBuild(buildResult, TestConstant.BUILD_INFO_JSON.toFile(), 3, 5)
(deployableArtifacts) -> Utils.generateBuildInfoProperties(this, "mavenJava,customIvyPublication", false, true, ""),
(buildResult, deployableArtifacts) -> ValidationUtils.checkLocalBuild(buildResult, TestConstant.BUILD_INFO_JSON.toFile(), 3, 5)
);
}

@Test(dataProvider = "gradleVersions")
public void ciServerArchivesTest(String gradleVersion) throws IOException {
runPublishCITest(gradleVersion, TestConstant.GRADLE_EXAMPLE_CI_SERVER_ARCHIVES, true,
() -> Utils.generateBuildInfoProperties(this, "", true, true),
buildResult -> ValidationUtils.checkArchivesBuildResults(artifactoryManager, buildResult, localRepo)
(deployableArtifacts) -> Utils.generateBuildInfoProperties(this, "", true, true, ""),
(buildResult, deployableArtifacts) -> ValidationUtils.checkArchivesBuildResults(artifactoryManager, buildResult, localRepo)
);
}

Expand All @@ -60,13 +60,13 @@ public void versionCatalogTest(String gradleVersion) throws IOException {
throw new SkipException("Version catalog test requires at least Gradle version " + MIN_GRADLE_VERSION_CATALOG_VERSION);
}
runPublishCITest(gradleVersion, TestConstant.GRADLE_EXAMPLE_VERSION_CATALOG_PRODUCER, false,
() -> Utils.generateBuildInfoProperties(this, "versionCatalogProducer", false, true),
buildResult -> ValidationUtils.verifyArtifacts(artifactoryManager, localRepo + "/", EXPECTED_VERSION_CATALOG_PRODUCER_ARTIFACTS)
(deployableArtifacts) -> Utils.generateBuildInfoProperties(this, "versionCatalogProducer", false, true, ""),
(buildResult, deployableArtifacts) -> ValidationUtils.verifyArtifacts(artifactoryManager, localRepo + "/", EXPECTED_VERSION_CATALOG_PRODUCER_ARTIFACTS)
);

runPublishCITest(gradleVersion, TestConstant.GRADLE_EXAMPLE_VERSION_CATALOG_CONSUMER, true,
() -> Utils.generateBuildInfoProperties(this, "versionCatalogConsumer", true, true),
buildResult -> ValidationUtils.checkVersionCatalogResults(artifactoryManager, buildResult, virtualRepo)
(deployableArtifacts) -> Utils.generateBuildInfoProperties(this, "versionCatalogConsumer", true, true, ""),
(buildResult, deployableArtifacts) -> ValidationUtils.checkVersionCatalogResults(artifactoryManager, buildResult, virtualRepo)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import java.util.*;
import java.util.regex.Matcher;

import static org.jfrog.build.api.BuildInfoFields.DEPLOYABLE_ARTIFACTS;
import static org.jfrog.build.extractor.BuildInfoExtractorUtils.BUILD_BROWSE_URL;
import static org.jfrog.build.extractor.ci.BuildInfoProperties.BUILD_INFO_PREFIX;
import static org.testng.Assert.assertTrue;

public class Utils {
Expand All @@ -49,6 +51,16 @@ public static void createTestDir(Path sourceDir) throws IOException {
FileUtils.copyDirectory(sourceDir.toFile(), TestConstant.TEST_DIR);
}

/**
* Create the deployable artifacts file.
*
* @return the path to the generated deployable artifacts file.
* @throws IOException - In case of any IO error
*/
public static Path createDeployableArtifactsFile() throws IOException {
return Files.createFile(TestConstant.TEST_DIR.toPath().resolve("deployable.artifacts")).toAbsolutePath();
}

/**
* Run 'ArtifactoryPublish' task with specific context.
*
Expand Down Expand Up @@ -105,18 +117,22 @@ private static void generateInitScript() throws IOException {
/**
* Generate buildinfo.properties file with publisher and other properties base on the given inputs.
*
* @param testBase - the test that hold the Artifactory properties that the user/CI server needs to provide
* @param publications - the publications to add into the properties
* @param publishBuildInfo - property that decide if to publish the build info
* @param setDeployer - if true it will set the deployer properties for the build info
* @param testBase - the test that hold the Artifactory properties that the user/CI server needs to provide
* @param publications - the publications to add into the properties
* @param publishBuildInfo - property that decide if to publish the build info
* @param setDeployer - if true it will set the deployer properties for the build info
* @param deployableArtifacts - path to deployable artifacts file, or empty if not necessary
* @throws IOException - In case of any IO error
*/
public static void generateBuildInfoProperties(GradleFunctionalTestBase testBase, String publications, boolean publishBuildInfo, boolean setDeployer) throws IOException {
public static void generateBuildInfoProperties(GradleFunctionalTestBase testBase, String publications, boolean publishBuildInfo, boolean setDeployer, String deployableArtifacts) throws IOException {
String content = generateBuildInfoPropertiesForServer(testBase, publications, publishBuildInfo, TestConstant.BUILD_INFO_PROPERTIES_SOURCE_RESOLVER);
if (setDeployer) {
content += "\n";
content += generateBuildInfoPropertiesForServer(testBase, publications, publishBuildInfo, TestConstant.BUILD_INFO_PROPERTIES_SOURCE_DEPLOYER);
}
if (StringUtils.isNotBlank(deployableArtifacts)) {
content += String.format("\n%s%s=%s", BUILD_INFO_PREFIX, DEPLOYABLE_ARTIFACTS, deployableArtifacts);
}
Files.write(TestConstant.BUILD_INFO_PROPERTIES_TARGET, content.getBytes(StandardCharsets.UTF_8));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,42 @@
import org.gradle.testkit.runner.BuildTask;
import org.jfrog.build.api.dependency.PropertySearchResult;
import org.jfrog.build.api.util.CommonUtils;
import org.jfrog.build.client.DeployableArtifactDetail;
import org.jfrog.build.extractor.ci.Artifact;
import org.jfrog.build.extractor.ci.BuildInfo;
import org.jfrog.build.extractor.ci.Dependency;
import org.jfrog.build.extractor.ci.Module;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.ArtifactoryManager;
import org.jfrog.gradle.plugin.artifactory.TestConstant;
import org.testng.collections.Sets;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.apache.commons.lang3.StringUtils.equalsAny;
import static org.apache.commons.lang3.StringUtils.*;
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS;
import static org.jfrog.build.extractor.BuildInfoExtractorUtils.jsonStringToBuildInfo;
import static org.jfrog.build.extractor.clientConfiguration.deploy.DeployableArtifactsUtils.loadDeployableArtifactsFromFile;
import static org.jfrog.gradle.plugin.artifactory.Constant.*;
import static org.jfrog.gradle.plugin.artifactory.TestConstant.ARTIFACTS_GROUP_ID;
import static org.jfrog.gradle.plugin.artifactory.TestConstant.EXPECTED_VERSION_CATALOG_CONSUMER_ARTIFACTS;
import static org.testng.Assert.*;
import static org.testng.Assert.assertTrue;

public class ValidationUtils {
public interface BuildResultValidation {
void validate(BuildResult buildResult) throws IOException;
}

public interface CiBuildResultValidation {
void validate(BuildResult buildResult, Path deployableArtifacts) throws IOException;
}

/**
* Check build results of a Gradle project with publications.
*
Expand All @@ -42,6 +54,46 @@ public static void checkBuildResults(ArtifactoryManager artifactoryManager, Buil
checkBuildResults(artifactoryManager, buildResult, localRepo, TestConstant.EXPECTED_MODULE_ARTIFACTS, 5);
}

/**
* Check build results of a Gradle project with publications.
*
* @param artifactoryManager - The ArtifactoryManager client
* @param buildResult - The build results
* @param localRepo - Local Maven repository in Artifactory
* @param deployableArtifacts - The local deployable artifacts file expected to be populated with the deployed artifacts
* @throws IOException - In case of any IO error
*/
public static void checkBuildResults(ArtifactoryManager artifactoryManager, BuildResult buildResult, String localRepo, Path deployableArtifacts) throws IOException {
checkBuildResults(artifactoryManager, buildResult, localRepo);
checkDeployableArtifacts(deployableArtifacts, Sets.newHashSet(TestConstant.EXPECTED_MODULE_ARTIFACTS), localRepo);
}

/**
* Check deployable artifacts file content.
*
* @param deployableArtifacts - Path to the file containing the deployable artifacts
* @param expectedArtifacts - Expected deployed artifacts
* @param localRepo - Local repository in Artifactory
* @throws IOException - In case of any IO error
*/
private static void checkDeployableArtifacts(Path deployableArtifacts, Set<String> expectedArtifacts, String localRepo) throws IOException {
Map<String, List<DeployableArtifactDetail>> deployableArtifactsDetails = loadDeployableArtifactsFromFile(deployableArtifacts.toFile(), deployableArtifacts.toFile());
assertTrue(deployableArtifactsDetails.containsKey("shared"));
assertTrue(deployableArtifactsDetails.containsKey("webservice"));
assertTrue(deployableArtifactsDetails.containsKey("api"));
for (Map.Entry<String, List<DeployableArtifactDetail>> entry : deployableArtifactsDetails.entrySet()) {
for (DeployableArtifactDetail deployableArtifact : entry.getValue()) {
assertFalse(isBlank(deployableArtifact.getSourcePath()));
assertTrue(expectedArtifacts.contains(removeStart("/" + deployableArtifact.getArtifactDest(), ARTIFACTS_GROUP_ID)));
assertFalse(isBlank(deployableArtifact.getSha1()));
assertFalse(isBlank(deployableArtifact.getSha256()));
assertTrue(deployableArtifact.isDeploySucceeded());
assertEquals(deployableArtifact.getTargetRepository(), localRepo);
assertEquals(deployableArtifact.getProperties().size(), 3);
}
}
}

/**
* Check build results of a Gradle project without publications.
*
Expand Down Expand Up @@ -84,7 +136,7 @@ private static void checkBuildResults(ArtifactoryManager artifactoryManager, Bui
assertProjectsSuccess(buildResult);

// Check that all expected artifacts uploaded to Artifactory
verifyArtifacts(artifactoryManager, localRepo + TestConstant.ARTIFACTS_GROUP_ID, expectedArtifacts);
verifyArtifacts(artifactoryManager, localRepo + ARTIFACTS_GROUP_ID, expectedArtifacts);

// Check build info
BuildInfo buildInfo = getBuildInfo(artifactoryManager, buildResult);
Expand Down

0 comments on commit d79abaa

Please sign in to comment.