Search in sources :

Example 1 with DataFile

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

the class UpdateDatasetThumbnailCommandTest method setUp.

@Before
public void setUp() {
    dataset = new Dataset();
    testEngine = new TestDataverseEngine(new TestCommandContext() {

        @Override
        public DataFileServiceBean files() {
            return new DataFileServiceBean() {

                @Override
                public DataFile find(Object object) {
                    if (object == unfindableFile) {
                        return null;
                    } else if (object == thumbnailUnexpectedlyAbsent) {
                        return new DataFile();
                    } else {
                        return null;
                    }
                }
            };
        }

        @Override
        public DatasetServiceBean datasets() {
            return new DatasetServiceBean() {

                @Override
                public Dataset setDatasetFileAsThumbnail(Dataset dataset, DataFile datasetFileThumbnailToSwitchTo) {
                    return dataset;
                }
            };
        }

        @Override
        public SystemConfig systemConfig() {
            return new SystemConfig() {

                @Override
                public String getDataverseSiteUrl() {
                    return "https://dataverse.example.edu";
                }
            };
        }
    });
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) SystemConfig(edu.harvard.iq.dataverse.util.SystemConfig) TestCommandContext(edu.harvard.iq.dataverse.engine.TestCommandContext) DataFileServiceBean(edu.harvard.iq.dataverse.DataFileServiceBean) Dataset(edu.harvard.iq.dataverse.Dataset) DatasetServiceBean(edu.harvard.iq.dataverse.DatasetServiceBean) TestDataverseEngine(edu.harvard.iq.dataverse.engine.TestDataverseEngine) Before(org.junit.Before)

Example 2 with DataFile

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

the class ExternalToolHandlerTest method testGetToolUrlWithOptionalQueryParameters.

// TODO: It would probably be better to split these into individual tests.
@Test
public void testGetToolUrlWithOptionalQueryParameters() {
    ExternalTool.Type type = ExternalTool.Type.EXPLORE;
    String toolUrl = "http://example.com";
    ExternalTool externalTool = new ExternalTool("displayName", "description", type, toolUrl, "{}");
    // One query parameter, not a reserved word, no {fileId} (required) used.
    externalTool.setToolParameters(Json.createObjectBuilder().add("queryParameters", Json.createArrayBuilder().add(Json.createObjectBuilder().add("mode", "mode1"))).build().toString());
    DataFile nullDataFile = null;
    ApiToken nullApiToken = null;
    Exception expectedException1 = null;
    try {
        ExternalToolHandler externalToolHandler1 = new ExternalToolHandler(externalTool, nullDataFile, nullApiToken);
    } catch (Exception ex) {
        expectedException1 = ex;
    }
    assertNotNull(expectedException1);
    assertEquals("A DataFile is required.", expectedException1.getMessage());
    // Two query parameters.
    externalTool.setToolParameters(Json.createObjectBuilder().add("queryParameters", Json.createArrayBuilder().add(Json.createObjectBuilder().add("mode", "mode1")).add(Json.createObjectBuilder().add("key2", "value2"))).build().toString());
    Exception expectedException2 = null;
    try {
        ExternalToolHandler externalToolHandler2 = new ExternalToolHandler(externalTool, nullDataFile, nullApiToken);
    } catch (Exception ex) {
        expectedException2 = ex;
    }
    assertNotNull(expectedException2);
    assertEquals("A DataFile is required.", expectedException2.getMessage());
    // Two query parameters, both reserved words, one is {fileId} which is required.
    externalTool.setToolParameters(Json.createObjectBuilder().add("queryParameters", Json.createArrayBuilder().add(Json.createObjectBuilder().add("key1", "{fileId}")).add(Json.createObjectBuilder().add("key2", "{apiToken}"))).build().toString());
    DataFile dataFile = new DataFile();
    dataFile.setId(42l);
    ApiToken apiToken = new ApiToken();
    apiToken.setTokenString("7196b5ce-f200-4286-8809-03ffdbc255d7");
    ExternalToolHandler externalToolHandler3 = new ExternalToolHandler(externalTool, dataFile, apiToken);
    String result3 = externalToolHandler3.getQueryParametersForUrl();
    System.out.println("result3: " + result3);
    assertEquals("?key1=42&key2=7196b5ce-f200-4286-8809-03ffdbc255d7", result3);
    // Two query parameters, both reserved words, no apiToken
    externalTool.setToolParameters(Json.createObjectBuilder().add("queryParameters", Json.createArrayBuilder().add(Json.createObjectBuilder().add("key1", "{fileId}")).add(Json.createObjectBuilder().add("key2", "{apiToken}"))).build().toString());
    ExternalToolHandler externalToolHandler4 = new ExternalToolHandler(externalTool, dataFile, nullApiToken);
    String result4 = externalToolHandler4.getQueryParametersForUrl();
    System.out.println("result4: " + result4);
    assertEquals("?key1=42", result4);
    // Two query parameters, attempt to use a reserved word that doesn't exist.
    externalTool.setToolParameters(Json.createObjectBuilder().add("queryParameters", Json.createArrayBuilder().add(Json.createObjectBuilder().add("key1", "{junk}")).add(Json.createObjectBuilder().add("key2", "{apiToken}"))).build().toString());
    Exception expectedException = null;
    try {
        ExternalToolHandler externalToolHandler5 = new ExternalToolHandler(externalTool, dataFile, nullApiToken);
        String result5 = externalToolHandler5.getQueryParametersForUrl();
        System.out.println("result5: " + result5);
    } catch (Exception ex) {
        System.out.println("Exception caught: " + ex);
        expectedException = ex;
    }
    assertNotNull(expectedException);
    assertEquals("Unknown reserved word: {junk}", expectedException.getMessage());
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) ApiToken(edu.harvard.iq.dataverse.authorization.users.ApiToken) Test(org.junit.Test)

Example 3 with DataFile

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

the class MocksFactory method makeDatasetVersion.

public static DatasetVersion makeDatasetVersion(List<DataFileCategory> categories) {
    final DatasetVersion retVal = new DatasetVersion();
    final List<DataFile> files = makeFiles(10);
    final List<FileMetadata> metadatas = new ArrayList<>(10);
    Random rand = new Random();
    files.forEach(df -> {
        df.getFileMetadata().addCategory(categories.get(rand.nextInt(categories.size())));
        metadatas.add(df.getFileMetadata());
    });
    retVal.setFileMetadatas(metadatas);
    List<DatasetField> fields = new ArrayList<>();
    DatasetField field = new DatasetField();
    field.setId(nextId());
    field.setSingleValue("Sample Field Value");
    field.setDatasetFieldType(makeDatasetFieldType());
    fields.add(field);
    retVal.setDatasetFields(fields);
    return retVal;
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) Random(java.util.Random) DatasetField(edu.harvard.iq.dataverse.DatasetField) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) ArrayList(java.util.ArrayList) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion)

Example 4 with DataFile

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

the class MocksFactory method makeDataset.

public static Dataset makeDataset() {
    Dataset ds = new Dataset();
    ds.setId(nextId());
    ds.setIdentifier("sample-ds-" + ds.getId());
    ds.setCategoriesByName(Arrays.asList("CatOne", "CatTwo", "CatThree"));
    final List<DataFile> files = makeFiles(10);
    final List<FileMetadata> metadatas = new ArrayList<>(10);
    final List<DataFileCategory> categories = ds.getCategories();
    Random rand = new Random();
    files.forEach(df -> {
        df.getFileMetadata().addCategory(categories.get(rand.nextInt(categories.size())));
        metadatas.add(df.getFileMetadata());
    });
    ds.setFiles(files);
    final DatasetVersion initialVersion = ds.getVersions().get(0);
    initialVersion.setFileMetadatas(metadatas);
    List<DatasetField> fields = new ArrayList<>();
    DatasetField field = new DatasetField();
    field.setId(nextId());
    field.setSingleValue("Sample Field Value");
    field.setDatasetFieldType(makeDatasetFieldType());
    fields.add(field);
    initialVersion.setDatasetFields(fields);
    ds.setOwner(makeDataverse());
    return ds;
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) DataFileCategory(edu.harvard.iq.dataverse.DataFileCategory) Random(java.util.Random) DatasetField(edu.harvard.iq.dataverse.DatasetField) Dataset(edu.harvard.iq.dataverse.Dataset) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) ArrayList(java.util.ArrayList) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion)

Example 5 with DataFile

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

the class StorageIOTest method testGetDvObject.

@Test
public void testGetDvObject() {
    assertEquals(null, instance.getDvObject());
    instance.setDvObject(new Dataset());
    assertEquals(new Dataset(), instance.getDataset());
    try {
        instance.getDataFile();
        fail("This should have thrown");
    } catch (ClassCastException ex) {
        assertEquals(ex.getMessage(), "edu.harvard.iq.dataverse.Dataset cannot be cast to edu.harvard.iq.dataverse.DataFile");
    }
    try {
        instance.getDataverse();
        fail("This should have thrown");
    } catch (ClassCastException ex) {
        assertEquals(ex.getMessage(), "edu.harvard.iq.dataverse.Dataset cannot be cast to edu.harvard.iq.dataverse.Dataverse");
    }
    assertEquals(new DataFile(), new FileAccessIO<>(new DataFile()).getDataFile());
    assertEquals(new Dataverse(), new FileAccessIO<>(new Dataverse()).getDataverse());
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) Dataset(edu.harvard.iq.dataverse.Dataset) Dataverse(edu.harvard.iq.dataverse.Dataverse) 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