Skip to content

Commit d2939f9

Browse files
authored
Compare thumbnail files instead of stringifying them (#2555)
1 parent df5ce63 commit d2939f9

File tree

4 files changed

+65
-44
lines changed

4 files changed

+65
-44
lines changed

src/org/labkey/test/TestFileUtils.java

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@
6464
import java.nio.file.Paths;
6565
import java.nio.file.SimpleFileVisitor;
6666
import java.nio.file.attribute.BasicFileAttributes;
67-
import java.security.MessageDigest;
68-
import java.security.NoSuchAlgorithmException;
6967
import java.security.Security;
7068
import java.util.ArrayList;
7169
import java.util.Arrays;
@@ -122,21 +120,6 @@ public static String getFileContents(Path path)
122120
}
123121
}
124122

125-
/**
126-
* Compute MD5 hash for the given file. Useful checking file equivalence.
127-
*/
128-
public static String getMD5Hash(Path path)
129-
{
130-
try
131-
{
132-
return new String(MessageDigest.getInstance("MD5").digest(Files.readAllBytes(path)), StandardCharsets.UTF_8);
133-
}
134-
catch (IOException | NoSuchAlgorithmException fail)
135-
{
136-
throw new RuntimeException(fail);
137-
}
138-
}
139-
140123
public static String getStreamContentsAsString(InputStream is) throws IOException
141124
{
142125
return StringUtils.join(IOUtils.readLines(is, Charset.defaultCharset()).toArray(), System.lineSeparator());
@@ -372,10 +355,46 @@ public static File getTestTempDir()
372355
return new File(buildDir, "testTemp");
373356
}
374357

375-
public static File ensureTestTempDir() throws IOException
358+
/**
359+
* Creates a directory under the 'testTemp' directory: 'build/testTemp/[children]'
360+
* @param children will be appended to the testTemp path
361+
* @return A file pointer to the specified directory. The directory will exist
362+
* @throws IOException if the directories were not created
363+
*/
364+
public static File ensureTestTempDir(String... children) throws IOException
376365
{
377366
File file = getTestTempDir();
367+
for (String child : children)
368+
{
369+
file = new File(file, child);
370+
}
371+
378372
FileUtils.forceMkdir(file);
373+
374+
return file;
375+
}
376+
377+
/**
378+
* Creates a directory under the 'testTemp' directory to contain the specified file. 'build/testTemp[/children]/lastChild'
379+
* @param children will be appended to the testTemp path to construct the desired file's path
380+
* @return A file pointer to the specified file. The file's parents will exist but the file might not
381+
* @throws IOException if the parent directories were not created
382+
*/
383+
public static File ensureTestTempFile(String... children) throws IOException
384+
{
385+
File file = getTestTempDir();
386+
387+
for (String child : children)
388+
{
389+
file = new File(file, child);
390+
}
391+
392+
if (file.toString().length() == getTestTempDir().toString().length())
393+
{
394+
throw new IllegalArgumentException("No valid children were provided: " + Arrays.toString(children));
395+
}
396+
FileUtils.forceMkdirParent(file);
397+
379398
return file;
380399
}
381400

src/org/labkey/test/tests/FileAttachmentColumnTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -553,9 +553,9 @@ private void validateSampleData(String sampleType, String folderPath, List<File>
553553
{
554554
// verify fie download behavior
555555
File downloadedFile = doAndWaitForDownload(() -> optionalFileLink.get().click());
556-
checker().wrapAssertion(() -> Assertions.assertThat(TestFileUtils.getMD5Hash(downloadedFile.toPath()))
556+
checker().wrapAssertion(() -> Assertions.assertThat(downloadedFile)
557557
.as("expect the downloaded file to be the expected file")
558-
.isEqualTo(TestFileUtils.getMD5Hash(file.toPath()))); // guard against renames like file2.xyz
558+
.hasSameBinaryContentAs(file)); // guard against renames like file2.xyz
559559
}
560560
}
561561
}
@@ -579,9 +579,9 @@ private void validateAssayRun(String assayName, String folderPath, String runNam
579579
if (optionalFileLink.isPresent())
580580
{
581581
var file = doAndWaitForDownload(()-> optionalFileLink.get().click());
582-
checker().wrapAssertion(()-> Assertions.assertThat(TestFileUtils.getMD5Hash(file.toPath()))
582+
checker().wrapAssertion(()-> Assertions.assertThat(file)
583583
.as("expect the downloaded file to have equivalent content")
584-
.isEqualTo(TestFileUtils.getMD5Hash(runFile.toPath())));
584+
.hasSameBinaryContentAs(runFile));
585585
}
586586

587587
var resultsPage = runsPage.clickAssayIdLink(runName);
@@ -647,9 +647,9 @@ private void validateDatasetData(String datasetName, String folderPath, List<Fil
647647
{
648648
// verify fie download behavior
649649
File downloadedFile = doAndWaitForDownload(() -> optionalFileLink.get().click());
650-
checker().wrapAssertion(() -> Assertions.assertThat(TestFileUtils.getMD5Hash(downloadedFile.toPath()))
650+
checker().wrapAssertion(() -> Assertions.assertThat(downloadedFile)
651651
.as("expect the downloaded file to be the expected file")
652-
.isEqualTo(TestFileUtils.getMD5Hash(file.toPath()))); // guard against renames like file2.xyz
652+
.hasSameBinaryContentAs(file)); // guard against renames like file2.xyz
653653
}
654654
}
655655
}

src/org/labkey/test/tests/SimpleModuleTest.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
package org.labkey.test.tests;
1717

1818
import org.apache.commons.lang3.StringUtils;
19-
import org.apache.commons.text.similarity.LevenshteinDistance;
2019
import org.apache.hc.core5.http.HttpStatus;
20+
import org.assertj.core.api.Assertions;
2121
import org.intellij.lang.annotations.Language;
2222
import org.jetbrains.annotations.Nullable;
2323
import org.junit.Assert;
@@ -62,6 +62,7 @@
6262
import org.labkey.test.util.Maps;
6363
import org.labkey.test.util.PortalHelper;
6464
import org.labkey.test.util.RReportHelper;
65+
import org.labkey.test.util.SimpleHttpRequest;
6566
import org.labkey.test.util.TestLogger;
6667
import org.labkey.test.util.WikiHelper;
6768
import org.labkey.test.util.ext4cmp.Ext4FieldRef;
@@ -1125,7 +1126,7 @@ private void doTestQueryViews()
11251126
}
11261127

11271128
@LogMethod
1128-
private void doTestReports()
1129+
private void doTestReports() throws IOException
11291130
{
11301131
RReportHelper _rReportHelper = new RReportHelper(this);
11311132
WikiHelper wikiHelper = new WikiHelper(this);
@@ -1177,7 +1178,7 @@ private void doTestReports()
11771178
}
11781179

11791180
@LogMethod
1180-
private void doTestReportThumbnails()
1181+
private void doTestReportThumbnails() throws IOException
11811182
{
11821183
goToProjectHome();
11831184
log("Verify custom module report thumbnail images");
@@ -1187,14 +1188,13 @@ private void doTestReportThumbnails()
11871188
}
11881189

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

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

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

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

12151213
@LogMethod
@@ -1222,20 +1220,18 @@ private void doTestReportCreatedDate()
12221220
}
12231221

12241222
@LogMethod
1225-
private void verifyReportThumbnail(@LoggedParam String reportTitle)
1223+
private void verifyReportThumbnail(@LoggedParam String reportTitle) throws IOException
12261224
{
12271225
File expectedThumbnailFile = TestFileUtils.getSampleData(THUMBNAIL_FOLDER + reportTitle + THUMBNAIL_FILENAME);
1228-
String expectedThumbnail = TestFileUtils.getFileContents(expectedThumbnailFile);
12291226

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

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

12411237
@LogMethod

src/org/labkey/test/util/SimpleHttpRequest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.apache.commons.io.IOUtils;
2020
import org.apache.commons.lang3.StringUtils;
2121
import org.labkey.remoteapi.Connection;
22+
import org.labkey.test.TestFileUtils;
2223
import org.openqa.selenium.Cookie;
2324
import org.openqa.selenium.WebDriver;
2425

@@ -219,16 +220,21 @@ else if (responseFilename != null && fileName.contains("."))
219220
}
220221
}
221222

223+
public File getResponseAsFile() throws IOException
224+
{
225+
return getResponseAsFile(TestFileUtils.ensureTestTempDir());
226+
}
227+
222228
private void useCopiedSession(HttpURLConnection con)
223229
{
224230
StringBuilder cookieString = new StringBuilder();
225-
for (Map.Entry cookie : _cookies.entrySet())
231+
for (Map.Entry<String, String> cookie : _cookies.entrySet())
226232
{
227233
if (cookie.getKey().equals(Connection.X_LABKEY_CSRF))
228-
con.setRequestProperty((String)cookie.getKey(), (String)cookie.getValue());
234+
con.setRequestProperty(cookie.getKey(), cookie.getValue());
229235

230236
if (cookie.getKey().equals(Connection.JSESSIONID))
231-
con.setRequestProperty((String)cookie.getKey(), (String)cookie.getValue());
237+
con.setRequestProperty(cookie.getKey(), cookie.getValue());
232238

233239
if (!cookieString.isEmpty())
234240
cookieString.append("; ");

0 commit comments

Comments
 (0)