diff --git a/src/org/labkey/test/TestFileUtils.java b/src/org/labkey/test/TestFileUtils.java index b9a1228ceb..ca119a4c68 100644 --- a/src/org/labkey/test/TestFileUtils.java +++ b/src/org/labkey/test/TestFileUtils.java @@ -64,6 +64,8 @@ import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.security.Security; import java.util.ArrayList; import java.util.Arrays; @@ -105,6 +107,9 @@ public static String getFileContents(final File file) return getFileContents(path); } + /** + * Get text content of a file. Will throw an error for non-text files (e.g. PDF). + */ public static String getFileContents(Path path) { try @@ -117,6 +122,21 @@ public static String getFileContents(Path path) } } + /** + * Compute MD5 hash for the given file. Useful checking file equivalence. + */ + public static String getMD5Hash(Path path) + { + try + { + return new String(MessageDigest.getInstance("MD5").digest(Files.readAllBytes(path)), StandardCharsets.UTF_8); + } + catch (IOException | NoSuchAlgorithmException fail) + { + throw new RuntimeException(fail); + } + } + public static String getStreamContentsAsString(InputStream is) throws IOException { return StringUtils.join(IOUtils.readLines(is, Charset.defaultCharset()).toArray(), System.lineSeparator()); diff --git a/src/org/labkey/test/tests/FileAttachmentColumnTest.java b/src/org/labkey/test/tests/FileAttachmentColumnTest.java index 9c05dc255c..2d86c8c135 100644 --- a/src/org/labkey/test/tests/FileAttachmentColumnTest.java +++ b/src/org/labkey/test/tests/FileAttachmentColumnTest.java @@ -550,9 +550,9 @@ private void validateSampleData(String sampleType, String folderPath, List { // verify fie download behavior File downloadedFile = doAndWaitForDownload(() -> optionalFileLink.get().click()); - checker().wrapAssertion(() -> Assertions.assertThat(TestFileUtils.getFileContents(downloadedFile)) + checker().wrapAssertion(() -> Assertions.assertThat(TestFileUtils.getMD5Hash(downloadedFile.toPath())) .as("expect the downloaded file to be the expected file") - .isEqualTo(TestFileUtils.getFileContents(file))); // guard against renames like file2.xyz + .isEqualTo(TestFileUtils.getMD5Hash(file.toPath()))); // guard against renames like file2.xyz } } } @@ -576,9 +576,9 @@ private void validateAssayRun(String assayName, String folderPath, String runNam if (optionalFileLink.isPresent()) { var file = doAndWaitForDownload(()-> optionalFileLink.get().click()); - checker().wrapAssertion(()-> Assertions.assertThat(TestFileUtils.getFileContents(file)) + checker().wrapAssertion(()-> Assertions.assertThat(TestFileUtils.getMD5Hash(file.toPath())) .as("expect the downloaded file to have equivalent content") - .isEqualTo(TestFileUtils.getFileContents(runFile))); + .isEqualTo(TestFileUtils.getMD5Hash(runFile.toPath()))); } var resultsPage = runsPage.clickAssayIdLink(runName); @@ -644,9 +644,9 @@ private void validateDatasetData(String datasetName, String folderPath, List optionalFileLink.get().click()); - checker().wrapAssertion(() -> Assertions.assertThat(TestFileUtils.getFileContents(downloadedFile)) + checker().wrapAssertion(() -> Assertions.assertThat(TestFileUtils.getMD5Hash(downloadedFile.toPath())) .as("expect the downloaded file to be the expected file") - .isEqualTo(TestFileUtils.getFileContents(file))); // guard against renames like file2.xyz + .isEqualTo(TestFileUtils.getMD5Hash(file.toPath()))); // guard against renames like file2.xyz } } }