From 7d1ae429d0e35fa356b92c6bc7019a0a076b4c29 Mon Sep 17 00:00:00 2001 From: labkey-tchad Date: Wed, 9 Jul 2025 11:48:02 -0700 Subject: [PATCH] Create `TestFileUtils.getMD5Hash` to compare binary files Don't use `Files.readString` to read non-text files --- src/org/labkey/test/TestFileUtils.java | 20 +++++++++++++++++++ .../test/tests/FileAttachmentColumnTest.java | 12 +++++------ 2 files changed, 26 insertions(+), 6 deletions(-) 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 } } }