Skip to content

Compare thumbnail files instead of stringifying them #2555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 11, 2025
Merged
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
55 changes: 37 additions & 18 deletions src/org/labkey/test/TestFileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@
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;
Expand Down Expand Up @@ -122,21 +120,6 @@ 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());
Expand Down Expand Up @@ -372,10 +355,46 @@ public static File getTestTempDir()
return new File(buildDir, "testTemp");
}

public static File ensureTestTempDir() throws IOException
/**
* Creates a directory under the 'testTemp' directory: 'build/testTemp/[children]'
* @param children will be appended to the testTemp path
* @return A file pointer to the specified directory. The directory will exist
* @throws IOException if the directories were not created
*/
public static File ensureTestTempDir(String... children) throws IOException
{
File file = getTestTempDir();
for (String child : children)
{
file = new File(file, child);
}

FileUtils.forceMkdir(file);

return file;
}

/**
* Creates a directory under the 'testTemp' directory to contain the specified file. 'build/testTemp[/children]/lastChild'
* @param children will be appended to the testTemp path to construct the desired file's path
* @return A file pointer to the specified file. The file's parents will exist but the file might not
* @throws IOException if the parent directories were not created
*/
public static File ensureTestTempFile(String... children) throws IOException
{
File file = getTestTempDir();

for (String child : children)
{
file = new File(file, child);
}

if (file.toString().length() == getTestTempDir().toString().length())
{
throw new IllegalArgumentException("No valid children were provided: " + Arrays.toString(children));
}
FileUtils.forceMkdirParent(file);

return file;
}

Expand Down
12 changes: 6 additions & 6 deletions src/org/labkey/test/tests/FileAttachmentColumnTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -553,9 +553,9 @@ private void validateSampleData(String sampleType, String folderPath, List<File>
{
// verify fie download behavior
File downloadedFile = doAndWaitForDownload(() -> optionalFileLink.get().click());
checker().wrapAssertion(() -> Assertions.assertThat(TestFileUtils.getMD5Hash(downloadedFile.toPath()))
checker().wrapAssertion(() -> Assertions.assertThat(downloadedFile)
.as("expect the downloaded file to be the expected file")
.isEqualTo(TestFileUtils.getMD5Hash(file.toPath()))); // guard against renames like file2.xyz
.hasSameBinaryContentAs(file)); // guard against renames like file2.xyz
}
}
}
Expand All @@ -579,9 +579,9 @@ private void validateAssayRun(String assayName, String folderPath, String runNam
if (optionalFileLink.isPresent())
{
var file = doAndWaitForDownload(()-> optionalFileLink.get().click());
checker().wrapAssertion(()-> Assertions.assertThat(TestFileUtils.getMD5Hash(file.toPath()))
checker().wrapAssertion(()-> Assertions.assertThat(file)
.as("expect the downloaded file to have equivalent content")
.isEqualTo(TestFileUtils.getMD5Hash(runFile.toPath())));
.hasSameBinaryContentAs(runFile));
}

var resultsPage = runsPage.clickAssayIdLink(runName);
Expand Down Expand Up @@ -647,9 +647,9 @@ private void validateDatasetData(String datasetName, String folderPath, List<Fil
{
// verify fie download behavior
File downloadedFile = doAndWaitForDownload(() -> optionalFileLink.get().click());
checker().wrapAssertion(() -> Assertions.assertThat(TestFileUtils.getMD5Hash(downloadedFile.toPath()))
checker().wrapAssertion(() -> Assertions.assertThat(downloadedFile)
.as("expect the downloaded file to be the expected file")
.isEqualTo(TestFileUtils.getMD5Hash(file.toPath()))); // guard against renames like file2.xyz
.hasSameBinaryContentAs(file)); // guard against renames like file2.xyz
}
}
}
Expand Down
30 changes: 13 additions & 17 deletions src/org/labkey/test/tests/SimpleModuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package org.labkey.test.tests;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.similarity.LevenshteinDistance;
import org.apache.hc.core5.http.HttpStatus;
import org.assertj.core.api.Assertions;
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.Nullable;
import org.junit.Assert;
Expand Down Expand Up @@ -62,6 +62,7 @@
import org.labkey.test.util.Maps;
import org.labkey.test.util.PortalHelper;
import org.labkey.test.util.RReportHelper;
import org.labkey.test.util.SimpleHttpRequest;
import org.labkey.test.util.TestLogger;
import org.labkey.test.util.WikiHelper;
import org.labkey.test.util.ext4cmp.Ext4FieldRef;
Expand Down Expand Up @@ -1125,7 +1126,7 @@ private void doTestQueryViews()
}

@LogMethod
private void doTestReports()
private void doTestReports() throws IOException
{
RReportHelper _rReportHelper = new RReportHelper(this);
WikiHelper wikiHelper = new WikiHelper(this);
Expand Down Expand Up @@ -1177,7 +1178,7 @@ private void doTestReports()
}

@LogMethod
private void doTestReportThumbnails()
private void doTestReportThumbnails() throws IOException
{
goToProjectHome();
log("Verify custom module report thumbnail images");
Expand All @@ -1187,14 +1188,13 @@ private void doTestReportThumbnails()
}

@LogMethod
private void doTestReportIcon()
private void doTestReportIcon() throws IOException
{
log("Verify custom module report icon image");
setFormElement(Locator.xpath("//table[contains(@class, 'dataset-search')]//input"), KNITR_PEOPLE);
waitForElementToDisappear(Locator.tag("tr").withClass("x4-grid-row").containing(WANT_TO_BE_COOL).notHidden());

File expectedIconFile = TestFileUtils.getSampleData(THUMBNAIL_FOLDER + KNITR_PEOPLE + ICON_FILENAME);
String expectedIcon = TestFileUtils.getFileContents(expectedIconFile);

WebElement img = waitForElement(Locator.tag("img").withClass("dataview-icon").withoutClass("x4-tree-icon-parent").notHidden());
String backgroundImage = StringUtils.trimToEmpty(img.getCssValue("background-image"));
Expand All @@ -1204,12 +1204,10 @@ private void doTestReportIcon()
Assert.fail("Module report icon style is not as expected: " + img.getDomAttribute("style"));
}
String iconUrl = matcher.group(1);
String iconData = WebTestHelper.getHttpResponse(iconUrl).getResponseBody();
File downloadedIcon = new SimpleHttpRequest(iconUrl).getResponseAsFile(TestFileUtils.ensureTestTempFile(KNITR_PEOPLE + ICON_FILENAME));

int lengthToCompare = 3000;
int diff = LevenshteinDistance.getDefaultInstance().apply(expectedIcon.substring(0, lengthToCompare), iconData.substring(0, lengthToCompare));
assertTrue("Module report icon is not as expected, diff is " + diff, expectedIcon.equals(iconData) ||
diff <= lengthToCompare * 0.03); // Might be slightly different due to indentations, etc
Assertions.assertThat(downloadedIcon).as("Module report icon is not as expected")
.hasSameBinaryContentAs(expectedIconFile);
}

@LogMethod
Expand All @@ -1222,20 +1220,18 @@ private void doTestReportCreatedDate()
}

@LogMethod
private void verifyReportThumbnail(@LoggedParam String reportTitle)
private void verifyReportThumbnail(@LoggedParam String reportTitle) throws IOException
{
File expectedThumbnailFile = TestFileUtils.getSampleData(THUMBNAIL_FOLDER + reportTitle + THUMBNAIL_FILENAME);
String expectedThumbnail = TestFileUtils.getFileContents(expectedThumbnailFile);

WebElement reportLink = waitForElement(Locator.xpath("//a[text()='" + reportTitle + "']"));
mouseOver(reportLink);
WebElement thumbnail = waitForElement(Locator.xpath("//div[@class='thumbnail']/img").notHidden());
String thumbnailData = WebTestHelper.getHttpResponse(thumbnail.getDomProperty("src")).getResponseBody();
File downloadedThumbnail = new SimpleHttpRequest(thumbnail.getDomProperty("src"))
.getResponseAsFile(TestFileUtils.ensureTestTempFile(reportTitle + THUMBNAIL_FILENAME));

int lengthToCompare = 5000;
int diff = LevenshteinDistance.getDefaultInstance().apply(expectedThumbnail.substring(0, lengthToCompare), thumbnailData.substring(0, lengthToCompare));
assertTrue("Module report thumbnail is not as expected, diff is " + diff, expectedThumbnail.equals(thumbnailData) ||
diff <= lengthToCompare * 0.03); // Might be slightly different due to indentations, etc
Assertions.assertThat(downloadedThumbnail).as("Module report thumbnail is not as expected")
.hasSameBinaryContentAs(expectedThumbnailFile);
}

@LogMethod
Expand Down
12 changes: 9 additions & 3 deletions src/org/labkey/test/util/SimpleHttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.labkey.remoteapi.Connection;
import org.labkey.test.TestFileUtils;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;

Expand Down Expand Up @@ -219,16 +220,21 @@ else if (responseFilename != null && fileName.contains("."))
}
}

public File getResponseAsFile() throws IOException
{
return getResponseAsFile(TestFileUtils.ensureTestTempDir());
}

private void useCopiedSession(HttpURLConnection con)
{
StringBuilder cookieString = new StringBuilder();
for (Map.Entry cookie : _cookies.entrySet())
for (Map.Entry<String, String> cookie : _cookies.entrySet())
{
if (cookie.getKey().equals(Connection.X_LABKEY_CSRF))
con.setRequestProperty((String)cookie.getKey(), (String)cookie.getValue());
con.setRequestProperty(cookie.getKey(), cookie.getValue());

if (cookie.getKey().equals(Connection.JSESSIONID))
con.setRequestProperty((String)cookie.getKey(), (String)cookie.getValue());
con.setRequestProperty(cookie.getKey(), cookie.getValue());

if (!cookieString.isEmpty())
cookieString.append("; ");
Expand Down