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));
}
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;
}
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;
}
Aggregations