Search in sources :

Example 26 with DataFile

use of edu.harvard.iq.dataverse.DataFile in project dataverse by IQSS.

the class WorldMapTokenTest method makeNewToken.

private WorldMapToken makeNewToken(TokenApplicationType tat) {
    WorldMapToken token;
    token = new WorldMapToken();
    token.setApplication(tat);
    token.setDatafile(new DataFile());
    token.setDataverseUser(new AuthenticatedUser());
    token.refreshToken();
    token.setToken();
    return token;
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)

Example 27 with DataFile

use of edu.harvard.iq.dataverse.DataFile in project dataverse by IQSS.

the class IngestUtilTest method testCheckForDuplicateFileNamesTabular.

@Test
public /**
 * Test tabular files (e.g., .dta) are changed when .tab files with the same
 * name exist.
 */
void testCheckForDuplicateFileNamesTabular() throws Exception {
    SimpleDateFormat dateFmt = new SimpleDateFormat("yyyyMMdd");
    // create dataset
    Dataset dataset = makeDataset();
    // create dataset version
    DatasetVersion datasetVersion = dataset.getEditVersion();
    datasetVersion.setCreateTime(dateFmt.parse("20001012"));
    datasetVersion.setLastUpdateTime(datasetVersion.getLastUpdateTime());
    datasetVersion.setId(MocksFactory.nextId());
    datasetVersion.setReleaseTime(dateFmt.parse("20010101"));
    datasetVersion.setVersionState(DatasetVersion.VersionState.RELEASED);
    datasetVersion.setMinorVersionNumber(0L);
    datasetVersion.setVersionNumber(1L);
    datasetVersion.setFileMetadatas(new ArrayList<>());
    // create datafiles
    List<DataFile> dataFileList = new ArrayList<>();
    DataFile datafile1 = new DataFile("application/x-strata");
    datafile1.setStorageIdentifier("foobar.dta");
    datafile1.setFilesize(200);
    datafile1.setModificationTime(new Timestamp(new Date().getTime()));
    datafile1.setCreateDate(new Timestamp(new Date().getTime()));
    datafile1.setPermissionModificationTime(new Timestamp(new Date().getTime()));
    datafile1.setOwner(dataset);
    datafile1.setIngestDone();
    datafile1.setChecksumType(DataFile.ChecksumType.SHA1);
    datafile1.setChecksumValue("Unknown");
    DataTable dt1 = new DataTable();
    dt1.setOriginalFileFormat("application/x-stata");
    datafile1.setDataTable(dt1);
    // set metadata and add version
    FileMetadata fmd1 = new FileMetadata();
    fmd1.setId(1L);
    fmd1.setLabel("foobar.tab");
    fmd1.setDataFile(datafile1);
    datafile1.getFileMetadatas().add(fmd1);
    datasetVersion.getFileMetadatas().add(fmd1);
    fmd1.setDatasetVersion(datasetVersion);
    DataFile datafile2 = new DataFile("application/x-strata");
    datafile2.setStorageIdentifier("foobar.dta");
    datafile2.setFilesize(200);
    datafile2.setModificationTime(new Timestamp(new Date().getTime()));
    datafile2.setCreateDate(new Timestamp(new Date().getTime()));
    datafile2.setPermissionModificationTime(new Timestamp(new Date().getTime()));
    datafile2.setOwner(dataset);
    datafile2.setIngestDone();
    datafile2.setChecksumType(DataFile.ChecksumType.SHA1);
    datafile2.setChecksumValue("Unknown");
    DataTable dt2 = new DataTable();
    dt2.setOriginalFileFormat("application/x-stata");
    datafile2.setDataTable(dt2);
    // set metadata and add version
    FileMetadata fmd2 = new FileMetadata();
    fmd2.setId(2L);
    fmd2.setLabel("foobar.dta");
    fmd2.setDataFile(datafile2);
    datafile2.getFileMetadatas().add(fmd2);
    dataFileList.add(datafile2);
    IngestUtil.checkForDuplicateFileNamesFinal(datasetVersion, dataFileList);
    boolean file2NameAltered = false;
    for (DataFile df : dataFileList) {
        if (df.getFileMetadata().getLabel().equals("foobar-1.dta")) {
            file2NameAltered = true;
        }
    }
    // check filename is altered since tabular and will change to .tab after ingest
    assertEquals(file2NameAltered, true);
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) DataTable(edu.harvard.iq.dataverse.DataTable) Dataset(edu.harvard.iq.dataverse.Dataset) MocksFactory.makeDataset(edu.harvard.iq.dataverse.mocks.MocksFactory.makeDataset) ArrayList(java.util.ArrayList) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) SimpleDateFormat(java.text.SimpleDateFormat) Timestamp(java.sql.Timestamp) Date(java.util.Date) Test(org.junit.Test)

Example 28 with DataFile

use of edu.harvard.iq.dataverse.DataFile in project dataverse by IQSS.

the class IngestUtilTest method testCheckForDuplicateFileNamesWithEmptyDirectoryLabels.

@Test
public /**
 * Test adding duplicate file name labels to a dataset version with empty
 * directory labels. This should simulate what happens when uploading a file
 * via the file upload UI.
 */
void testCheckForDuplicateFileNamesWithEmptyDirectoryLabels() throws Exception {
    SimpleDateFormat dateFmt = new SimpleDateFormat("yyyyMMdd");
    // create dataset
    Dataset dataset = makeDataset();
    // create dataset version
    DatasetVersion datasetVersion = dataset.getEditVersion();
    datasetVersion.setCreateTime(dateFmt.parse("20001012"));
    datasetVersion.setLastUpdateTime(datasetVersion.getLastUpdateTime());
    datasetVersion.setId(MocksFactory.nextId());
    datasetVersion.setReleaseTime(dateFmt.parse("20010101"));
    datasetVersion.setVersionState(DatasetVersion.VersionState.RELEASED);
    datasetVersion.setMinorVersionNumber(0L);
    datasetVersion.setVersionNumber(1L);
    datasetVersion.setFileMetadatas(new ArrayList<>());
    // create datafiles
    List<DataFile> dataFileList = new ArrayList<>();
    DataFile datafile1 = new DataFile("application/octet-stream");
    datafile1.setStorageIdentifier("datafile1.txt");
    datafile1.setFilesize(200);
    datafile1.setModificationTime(new Timestamp(new Date().getTime()));
    datafile1.setCreateDate(new Timestamp(new Date().getTime()));
    datafile1.setPermissionModificationTime(new Timestamp(new Date().getTime()));
    datafile1.setOwner(dataset);
    datafile1.setIngestDone();
    datafile1.setChecksumType(DataFile.ChecksumType.SHA1);
    datafile1.setChecksumValue("Unknown");
    // set metadata and add verson
    FileMetadata fmd1 = new FileMetadata();
    fmd1.setId(1L);
    fmd1.setLabel("datafile1.txt");
    fmd1.setDirectoryLabel("");
    fmd1.setDataFile(datafile1);
    datafile1.getFileMetadatas().add(fmd1);
    datasetVersion.getFileMetadatas().add(fmd1);
    fmd1.setDatasetVersion(datasetVersion);
    dataFileList.add(datafile1);
    DataFile datafile2 = new DataFile("application/octet-stream");
    datafile2.setStorageIdentifier("datafile2.txt");
    datafile2.setFilesize(200);
    datafile2.setModificationTime(new Timestamp(new Date().getTime()));
    datafile2.setCreateDate(new Timestamp(new Date().getTime()));
    datafile2.setPermissionModificationTime(new Timestamp(new Date().getTime()));
    datafile2.setOwner(dataset);
    datafile2.setIngestDone();
    datafile2.setChecksumType(DataFile.ChecksumType.SHA1);
    datafile2.setChecksumValue("Unknown");
    // set metadata and add version
    FileMetadata fmd2 = new FileMetadata();
    fmd2.setId(2L);
    fmd2.setLabel("datafile2.txt");
    fmd2.setDirectoryLabel("");
    fmd2.setDataFile(datafile2);
    datafile2.getFileMetadatas().add(fmd2);
    datasetVersion.getFileMetadatas().add(fmd2);
    fmd2.setDatasetVersion(datasetVersion);
    dataFileList.add(datafile2);
    IngestUtil.checkForDuplicateFileNamesFinal(datasetVersion, dataFileList);
    boolean file1NameAltered = false;
    boolean file2NameAltered = false;
    for (DataFile df : dataFileList) {
        if (df.getFileMetadata().getLabel().equals("datafile1-1.txt")) {
            file1NameAltered = true;
        }
        if (df.getFileMetadata().getLabel().equals("datafile2-1.txt")) {
            file2NameAltered = true;
        }
    }
    // check filenames are unique and unaltered
    assertEquals(file1NameAltered, true);
    assertEquals(file2NameAltered, true);
    // try to add data files with "-1" duplicates and see if it gets incremented to "-2"
    IngestUtil.checkForDuplicateFileNamesFinal(datasetVersion, dataFileList);
    for (DataFile df : dataFileList) {
        if (df.getFileMetadata().getLabel().equals("datafile1-2.txt")) {
            file1NameAltered = true;
        }
        if (df.getFileMetadata().getLabel().equals("datafile2-2.txt")) {
            file2NameAltered = true;
        }
    }
    // check filenames are unique and unaltered
    assertEquals(file1NameAltered, true);
    assertEquals(file2NameAltered, true);
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) Dataset(edu.harvard.iq.dataverse.Dataset) MocksFactory.makeDataset(edu.harvard.iq.dataverse.mocks.MocksFactory.makeDataset) ArrayList(java.util.ArrayList) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) SimpleDateFormat(java.text.SimpleDateFormat) Timestamp(java.sql.Timestamp) Date(java.util.Date) Test(org.junit.Test)

Example 29 with DataFile

use of edu.harvard.iq.dataverse.DataFile in project dataverse by IQSS.

the class ExternalToolServiceBeanTest method testfindAll.

@Test
public void testfindAll() {
    DataFile dataFile = new DataFile();
    dataFile.setId(42l);
    List<DataTable> dataTables = new ArrayList<DataTable>();
    dataTables.add(new DataTable());
    dataFile.setDataTables(dataTables);
    ApiToken apiToken = new ApiToken();
    apiToken.setTokenString("7196b5ce-f200-4286-8809-03ffdbc255d7");
    ExternalTool.Type type = ExternalTool.Type.EXPLORE;
    ExternalTool externalTool = new ExternalTool("displayName", "description", type, "http://foo.com", "{}");
    ExternalToolHandler externalToolHandler4 = new ExternalToolHandler(externalTool, dataFile, apiToken);
    List<ExternalTool> externalTools = new ArrayList<>();
    externalTools.add(externalTool);
    List<ExternalTool> availableExternalTools = ExternalToolServiceBean.findExternalToolsByFile(externalTools, dataFile);
    assertEquals(availableExternalTools.size(), 1);
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) DataTable(edu.harvard.iq.dataverse.DataTable) ArrayList(java.util.ArrayList) ApiToken(edu.harvard.iq.dataverse.authorization.users.ApiToken) Test(org.junit.Test)

Example 30 with DataFile

use of edu.harvard.iq.dataverse.DataFile in project dataverse by IQSS.

the class ExternalToolServiceBeanTest method testParseAddExternalToolInput.

@Test
public void testParseAddExternalToolInput() {
    JsonObjectBuilder job = Json.createObjectBuilder();
    job.add("displayName", "AwesomeTool");
    job.add("description", "This tool is awesome.");
    job.add("type", "explore");
    job.add("toolUrl", "http://awesometool.com");
    job.add("toolParameters", Json.createObjectBuilder().add("queryParameters", Json.createArrayBuilder().add(Json.createObjectBuilder().add("fileid", "{fileId}").build()).add(Json.createObjectBuilder().add("key", "{apiToken}").build()).build()).build());
    String tool = job.build().toString();
    System.out.println("tool: " + tool);
    ExternalTool externalTool = ExternalToolServiceBean.parseAddExternalToolManifest(tool);
    assertEquals("AwesomeTool", externalTool.getDisplayName());
    DataFile dataFile = new DataFile();
    dataFile.setId(42l);
    ApiToken apiToken = new ApiToken();
    apiToken.setTokenString("7196b5ce-f200-4286-8809-03ffdbc255d7");
    ExternalToolHandler externalToolHandler = new ExternalToolHandler(externalTool, dataFile, apiToken);
    String toolUrl = externalToolHandler.getToolUrlWithQueryParams();
    System.out.println("result: " + toolUrl);
    assertEquals("http://awesometool.com?fileid=42&key=7196b5ce-f200-4286-8809-03ffdbc255d7", toolUrl);
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) ApiToken(edu.harvard.iq.dataverse.authorization.users.ApiToken) JsonObjectBuilder(javax.json.JsonObjectBuilder) Test(org.junit.Test)

Aggregations

DataFile (edu.harvard.iq.dataverse.DataFile)111 Dataset (edu.harvard.iq.dataverse.Dataset)39 IOException (java.io.IOException)39 FileMetadata (edu.harvard.iq.dataverse.FileMetadata)30 ArrayList (java.util.ArrayList)25 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)20 File (java.io.File)20 FileNotFoundException (java.io.FileNotFoundException)18 Path (javax.ws.rs.Path)18 Dataverse (edu.harvard.iq.dataverse.Dataverse)17 FileInputStream (java.io.FileInputStream)16 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)14 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)13 Date (java.util.Date)13 GET (javax.ws.rs.GET)13 Test (org.junit.Test)13 Timestamp (java.sql.Timestamp)11 InputStream (java.io.InputStream)10 DataVariable (edu.harvard.iq.dataverse.datavariable.DataVariable)8 FileOutputStream (java.io.FileOutputStream)8