Search in sources :

Example 1 with FileTypeValueOptions

use of org.hisp.dhis.common.FileTypeValueOptions in project dhis2-core by dhis2.

the class ValidationUtilsTest method testFileValueTypeOptionValidation.

@Test
void testFileValueTypeOptionValidation() throws IOException {
    long oneHundredMegaBytes = 1024 * (1024 * 100L);
    ValueType valueType = ValueType.FILE_RESOURCE;
    FileTypeValueOptions options = new FileTypeValueOptions();
    options.setMaxFileSize(oneHundredMegaBytes);
    options.setAllowedContentTypes(ImmutableSet.of("jpg", "pdf"));
    FileResource fileResource = new FileResource("name", "jpg", oneHundredMegaBytes, "md5sum", FileResourceDomain.DOCUMENT);
    assertNull(dataValueIsValid(fileResource, valueType, options));
    fileResource = new FileResource("name", "jpg", 1024 * (1024 * 101L), "md5sum", FileResourceDomain.DOCUMENT);
    assertEquals("not_valid_file_size_too_big", dataValueIsValid(fileResource, valueType, options));
    fileResource = new FileResource("name", "exe", oneHundredMegaBytes, "md5sum", FileResourceDomain.DOCUMENT);
    assertEquals("not_valid_file_content_type", dataValueIsValid(fileResource, valueType, options));
}
Also used : ValueType(org.hisp.dhis.common.ValueType) FileResource(org.hisp.dhis.fileresource.FileResource) FileTypeValueOptions(org.hisp.dhis.common.FileTypeValueOptions) Test(org.junit.jupiter.api.Test)

Example 2 with FileTypeValueOptions

use of org.hisp.dhis.common.FileTypeValueOptions in project dhis2-core by dhis2.

the class ValidationUtils method validateFileResource.

private static String validateFileResource(FileResource value, FileTypeValueOptions options) {
    FileResource fileResource = value;
    FileTypeValueOptions fileTypeValueOptions = options;
    if (fileResource.getContentLength() > fileTypeValueOptions.getMaxFileSize()) {
        return "not_valid_file_size_too_big";
    }
    if (!fileTypeValueOptions.getAllowedContentTypes().contains(fileResource.getContentType().toLowerCase())) {
        return "not_valid_file_content_type";
    }
    return null;
}
Also used : FileResource(org.hisp.dhis.fileresource.FileResource) FileTypeValueOptions(org.hisp.dhis.common.FileTypeValueOptions)

Example 3 with FileTypeValueOptions

use of org.hisp.dhis.common.FileTypeValueOptions in project dhis2-core by dhis2.

the class DataElementWithValueTypeOptionsTest method createDataElementWithFileValueTypeOptions.

private DataElement createDataElementWithFileValueTypeOptions(char uniqueCharacter, long maxFileSize) {
    FileTypeValueOptions fileTypeValueOptions = new FileTypeValueOptions();
    fileTypeValueOptions.setMaxFileSize(maxFileSize);
    DataElement dataElement = new DataElement();
    dataElement.setAutoFields();
    dataElement.setUid(BASE_DE_UID + uniqueCharacter);
    dataElement.setName("DataElement" + uniqueCharacter);
    dataElement.setShortName("DataElementShort" + uniqueCharacter);
    dataElement.setCode("DataElementCode" + uniqueCharacter);
    dataElement.setDescription("DataElementDescription" + uniqueCharacter);
    dataElement.setValueType(ValueType.FILE_RESOURCE);
    dataElement.setDomainType(DataElementDomain.AGGREGATE);
    dataElement.setAggregationType(AggregationType.SUM);
    dataElement.setZeroIsSignificant(false);
    dataElement.setValueTypeOptions(fileTypeValueOptions);
    if (categoryService != null) {
        dataElement.setCategoryCombo(categoryService.getDefaultCategoryCombo());
    }
    return dataElement;
}
Also used : FileTypeValueOptions(org.hisp.dhis.common.FileTypeValueOptions)

Aggregations

FileTypeValueOptions (org.hisp.dhis.common.FileTypeValueOptions)3 FileResource (org.hisp.dhis.fileresource.FileResource)2 ValueType (org.hisp.dhis.common.ValueType)1 Test (org.junit.jupiter.api.Test)1