Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ public void assertPasswordGuidance(@LoggedParam String password, @LoggedParam St

private void assertGuidanceMessage(String expectedGuidance)
{
Awaitility.await().atMost(Duration.ofSeconds(2)).untilAsserted(() ->
// Though it's usually quite fast, sometimes the password scoring API takes ~4 sec on TeamCity, so give it
// enough time to refresh
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() ->
assertEquals("Strength guidance for password", expectedGuidance, elementCache().strengthGuidance.getText()));
}

Expand Down
12 changes: 12 additions & 0 deletions src/org/labkey/test/params/FieldDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public class FieldDefinition extends PropertyDescriptor
// Collection of JSON properties not explicitly known by 'PropertyDescriptor'
private final Map<String, Object> _extraFieldProperties = new HashMap<>();

private String _namePart;

/**
* Define a non-lookup field of the specified type
* @param name field name
Expand Down Expand Up @@ -467,6 +469,16 @@ public void setAliquotOption(ExpSchema.DerivationDataScopeType aliquotOption)
_aliquotOption = aliquotOption;
}

public void setNamePart(String namePart)
{
_namePart = namePart;
}

public boolean isNamePartMatch(String namePart)
{
return _namePart != null && _namePart.equals(namePart);
}

public enum RangeType
{
Equals("Equals", Filter.Operator.EQUAL),
Expand Down
14 changes: 13 additions & 1 deletion src/org/labkey/test/params/FieldInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class FieldInfo implements CharSequence, WrapsFieldKey
private final String _label;
private final ColumnType _columnType;
private final Consumer<FieldDefinition> _fieldDefinitionMutator;
private String _namePart; // used for random field generation to track the name part used

private FieldInfo(FieldKey fieldKey, String label, ColumnType columnType, Consumer<FieldDefinition> fieldDefinitionMutator)
{
Expand Down Expand Up @@ -54,7 +55,9 @@ public FieldInfo(String name)
*/
public static FieldInfo random(String namePart, ColumnType columnType)
{
return new FieldInfo(TestDataGenerator.randomFieldName(namePart), columnType);
FieldInfo field = new FieldInfo(TestDataGenerator.randomFieldName(namePart), columnType);
field.setNamePart(namePart);
return field;
}

/**
Expand Down Expand Up @@ -165,9 +168,18 @@ private FieldDefinition getFieldDefinition(ColumnType columnType)
{
_fieldDefinitionMutator.accept(fieldDefinition);
}
if (_namePart != null)
{
fieldDefinition.setNamePart(_namePart);
}
return fieldDefinition;
}

private void setNamePart(String namePart)
{
_namePart = namePart;
}

@Override
public int length()
{
Expand Down
1 change: 0 additions & 1 deletion src/org/labkey/test/tests/AbstractKnitrReportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ protected void moduleReportDependencies()
public void testEmbeddedReportNonce()
{
CspConfigHelper.debugCspWarnings();
new CspConfigHelper(this).setEnforceCsp(false);

String name = "rhtml nonce check";
Locator[] reportContains = {nonceCheckSuccessLoc};
Expand Down
5 changes: 3 additions & 2 deletions src/org/labkey/test/tests/AttachmentFieldTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.labkey.test.util.DataRegionTable;
import org.labkey.test.util.PortalHelper;
import org.labkey.test.util.SampleTypeHelper;
import org.labkey.test.util.TestDataGenerator;

import java.io.File;
import java.util.List;
Expand Down Expand Up @@ -96,8 +97,8 @@ public void testFileFieldInSampleType()
@Test
public void testAttachmentFieldInLists()
{
String listName = "List with attachment field";
String fieldName = "testFile";
String listName = TestDataGenerator.randomDomainName("List with attachment field");
String fieldName = TestDataGenerator.randomFieldName("Test File");
goToProjectHome();
log("Creating the list");
_listHelper.createList(getProjectName(), listName, "id");
Expand Down
27 changes: 15 additions & 12 deletions src/org/labkey/test/tests/FileAttachmentColumnTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@
import org.labkey.test.pages.study.CreateStudyPage;
import org.labkey.test.params.FieldDefinition;
import org.labkey.test.params.FieldDefinition.ColumnType;
import org.labkey.test.params.FieldInfo;
import org.labkey.test.params.assay.GeneralAssayDesign;
import org.labkey.test.params.experiment.SampleTypeDefinition;
import org.labkey.test.util.DataRegionTable;
import org.labkey.test.util.ListHelper;
import org.labkey.test.util.PortalHelper;
import org.labkey.test.util.SampleTypeHelper;
import org.labkey.test.util.StudyHelper;
import org.labkey.test.util.TestDataGenerator;
import org.labkey.test.util.core.webdav.WebDavUploadHelper;
import org.labkey.test.util.exp.SampleTypeAPIHelper;
import org.openqa.selenium.By;
Expand Down Expand Up @@ -91,6 +93,7 @@ public class FileAttachmentColumnTest extends BaseWebDriverTest
private final String RESULT_FILE_COL = "resultFile";
private final String OTHER_RESULT_FILE_COL = "otherResultFile";
private final String STUDY_DATASET_NAME = "ogreSpiteLevels";
private static final FieldInfo LIST_ATTACHMENT_FIELD = new FieldInfo(TestDataGenerator.randomFieldName("File / Attachment"), ColumnType.Attachment);

@Override
protected void doCleanup(boolean afterTest) throws TestTimeoutException
Expand Down Expand Up @@ -181,8 +184,8 @@ public void verifyFileDownloadOnClick()
for (File testFile : downloadTestFiles)
{
int rowIndex = testListRegion.getRowIndex("Name", testFile.getName());
var downloadLink = testListRegion.link(rowIndex, "File");
doAndWaitForDownload(()-> downloadLink.click());
var downloadLink = testListRegion.link(rowIndex, LIST_ATTACHMENT_FIELD.getName());
doAndWaitForDownload(downloadLink::click);
}

// verify popup/sprite for jpeg
Expand Down Expand Up @@ -429,13 +432,13 @@ private void createListWithData(String containerPath)
String LIST_KEY = "TestListId";
listHelper.createList(getProjectName() + "/" + EXPORT_FOLDER_NAME, LIST_NAME, LIST_KEY,
new FieldDefinition("Name", ColumnType.String),
new FieldDefinition("File", ColumnType.Attachment));
LIST_ATTACHMENT_FIELD.getFieldDefinition());
goToManageLists();
listHelper.click(Locator.linkContainingText(LIST_NAME));

for (File file : SAMPLE_FILES)
{
Map<String, String> fileRow = Map.of("Name", file.getName(), "File", file.getAbsolutePath());
Map<String, String> fileRow = Map.of("Name", file.getName(), LIST_ATTACHMENT_FIELD.getName(), file.getAbsolutePath());
listHelper.insertNewRow(fileRow, false);
}
}
Expand All @@ -449,7 +452,7 @@ private void importSampleDataUI(String sampleTypeName, String containerPath, Lis
for (File file : files)
{
sampleFileData.add(Map.of("Name", file.getName(), "Color", "green",
"File", file.getName()));
"file", file.getName()));
}
helper.bulkImport(sampleFileData);
}
Expand Down Expand Up @@ -514,7 +517,7 @@ private void validateListData(String listName, String folderPath, List<File> exp
else
{
int rowIndex = testListRegion.getRowIndex("Name", testFile.getName());
var downloadLink = testListRegion.link(rowIndex, "File");
var downloadLink = testListRegion.link(rowIndex, LIST_ATTACHMENT_FIELD.getName());
doAndWaitForDownload(() -> downloadLink.click());
}
}
Expand Down Expand Up @@ -551,9 +554,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 @@ -577,9 +580,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 @@ -645,9 +648,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
15 changes: 8 additions & 7 deletions src/org/labkey/test/tests/InlineImagesListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.labkey.test.util.DataRegionExportHelper;
import org.labkey.test.util.DataRegionTable;
import org.labkey.test.util.ExcelHelper;
import org.labkey.test.util.TestDataGenerator;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

Expand All @@ -51,16 +52,16 @@
@BaseWebDriverTest.ClassTimeout(minutes = 5)
public class InlineImagesListTest extends BaseWebDriverTest
{
protected final static String LIST_NAME = "InlineImagesList";
protected final static String LIST_KEY_NAME = "Key";
protected final static String LIST_NAME = TestDataGenerator.randomDomainName("InlineImagesList");
protected final static String LIST_KEY_NAME = TestDataGenerator.randomFieldName("Key");
protected final static ColumnType LIST_KEY_TYPE = ColumnType.Integer;

protected final static String LIST_ATTACHMENT01_NAME = "Attachment01";
protected final static String LIST_ATTACHMENT01_LABEL = "Attachment Column 01";
protected final static String LIST_ATTACHMENT01_NAME = TestDataGenerator.randomFieldName("Attachment01");
protected final static String LIST_ATTACHMENT01_LABEL = TestDataGenerator.randomFieldName("Attachment Column 01");
protected final static String LIST_ATTACHMENT01_DESC = "An 1st attachment column.";

protected final static String LIST_ATTACHMENT02_NAME = "Attachment02";
protected final static String LIST_ATTACHMENT02_LABEL = "Attachment Column 02";
protected final static String LIST_ATTACHMENT02_NAME = TestDataGenerator.randomFieldName("Attachment02");
protected final static String LIST_ATTACHMENT02_LABEL = TestDataGenerator.randomFieldName("Attachment Column 02");
protected final static String LIST_ATTACHMENT02_DESC = "An 2nd attachment column.";

protected final static ColumnType LIST_ATTACHMENT_TYPE = ColumnType.Attachment;
Expand Down Expand Up @@ -132,7 +133,7 @@ private void doInit()
}

@Test
public final void ListTest() throws Exception
public final void testList() throws Exception
{
DataRegionTable list;
DataRegionExportHelper exportHelper;
Expand Down
Loading