Skip to content

Commit

Permalink
BFD-3118: Dynamically discover project version for testing (#2098)
Browse files Browse the repository at this point in the history
  • Loading branch information
brianburton authored Dec 14, 2023
1 parent 1f6758f commit 1639b66
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ public void normalUsage() {
getProjectDirectory()
.resolve(Paths.get("..", "dev", "ssl-stores", "server-truststore.pfx"))
.toString());
envValues.put(
AppConfiguration.ENV_VAR_KEY_WAR,
getProjectDirectory()
.resolve(Paths.get("target", "sample", "bfd-server-launcher-sample-1.0.0-SNAPSHOT.war"))
.toString());
envValues.put(AppConfiguration.ENV_VAR_KEY_WAR, ServerTestUtils.getSampleWar().toString());

ConfigLoader config = ConfigLoader.builder().addMap(envValues).build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.jupiter.api.Assumptions.assumeTrue;

import gov.cms.bfd.MavenUtils;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -140,12 +141,14 @@ static void skipOnUnsupportedOs() {
}

/**
* Gets the {@link Path} to the <code>bfd-server-launcher-sample</code> WAR.
* Gets the {@link Path} to the {@code bfd-server-launcher-sample} WAR.
*
* @return the {@link Path}
*/
static Path getSampleWar() {
final String projectVersion = MavenUtils.findProjectVersion();
final String warFileName = String.format("bfd-server-launcher-sample-%s.war", projectVersion);
return AppConfigurationIT.getProjectDirectory()
.resolve(Paths.get("target", "sample", "bfd-server-launcher-sample-1.0.0-SNAPSHOT.war"));
.resolve(Paths.get("target", "sample", warFileName));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package gov.cms.bfd;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.testcontainers.shaded.com.google.common.annotations.VisibleForTesting;
import org.testcontainers.shaded.com.google.common.base.Strings;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/** Extract maven project meta data for use in tests. */
public class MavenUtils {
/** Name of maven pom file to search for. */
public static final String POM_FILE_NAME = "pom.xml";

/**
* Searches for a {@code pom.xml} file starting from the current working directory and then
* proceeding through parent directories until root is reached. Looks for parent version string
* within the pom file and returns it if one is found that is not empty.
*
* @return project version string from pom file
* @throws RuntimeException if any error happens or no suitable pom file could be found
*/
public static String findProjectVersion() {
return findProjectVersion(POM_FILE_NAME);
}

/**
* Searches for a file with the given name starting from the current working directory and then
* proceeding through parent directories until root is reached. Looks for parent version string
* within the pom file and returns it if one is found that is not empty.
*
* @param pomFileName name of file to search for
* @return project version string from pom file
* @throws RuntimeException if any error happens or no suitable pom file could be found
*/
@VisibleForTesting
static String findProjectVersion(String pomFileName) {
File previousDirectory;
File directory = new File(".").getAbsoluteFile();
do {
File pomFile = new File(directory, pomFileName);
if (pomFile.exists()) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File(pomFileName));
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodes =
(NodeList)
xPath.evaluate("/project/parent/version", document, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); ++i) {
Element e = (Element) nodes.item(i);
String version = e.getTextContent();
if (!Strings.isNullOrEmpty(version)) {
return version;
}
}
} catch (Exception ex) {
throw new RuntimeException("encountered error while parsing pom.xml file", ex);
}
}
previousDirectory = directory;
directory = previousDirectory.getParentFile();
} while (directory != null && !directory.equals(previousDirectory));
throw new RuntimeException("failed to find a pom.xml file containing a version!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package gov.cms.bfd;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;
import org.testcontainers.shaded.com.google.common.base.Strings;

/** Unit tests for {@link MavenUtils}. */
public class MavenUtilsTest {
/** Verifies that pom file can be found and parsed successfully. */
@Test
void findsProjectVersionSuccessfully() {
String version = MavenUtils.findProjectVersion();
assertFalse(Strings.isNullOrEmpty(version), "version string should be non-empty");
}

/** Verifies that method throws an exception if it cannot find a pom file. */
@Test
void failsToFindPomFile() {
assertThrows(
RuntimeException.class, () -> MavenUtils.findProjectVersion("not-a-real-pom-file"));
}
}

0 comments on commit 1639b66

Please sign in to comment.