-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#428 Add basic mimetype validation to XDS Documententry.mimetype
attribute
- Loading branch information
Showing
5 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
...ds/src/main/java/org/openehealth/ipf/commons/ihe/xds/core/validate/MimeTypeValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.openehealth.ipf.commons.ihe.xds.core.validate; | ||
|
||
import static org.apache.commons.lang3.Validate.notNull; | ||
import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidatorAssertions.metaDataAssert; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Validate the basic format of a the document mime type. | ||
* | ||
* Mime type must consist of type and subtype, separated by a / | ||
*/ | ||
public class MimeTypeValidator implements ValueValidator { | ||
private static final Pattern MIME_PATTERN = Pattern.compile("\\w+\\/[-+.\\w]+"); | ||
|
||
@Override | ||
public void validate(String mimeType) throws XDSMetaDataException { | ||
notNull(mimeType, "mimeType cannot be null"); | ||
|
||
metaDataAssert(MIME_PATTERN.matcher(mimeType).matches(), ValidationMessage.MIME_TYPE_FORMAT, mimeType); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...rc/test/java/org/openehealth/ipf/commons/ihe/xds/core/validate/MimeTypeValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.openehealth.ipf.commons.ihe.xds.core.validate; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
class MimeTypeValidatorTest { | ||
private static final MimeTypeValidator validator = new MimeTypeValidator(); | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"text/xml", "application/hl7-v3", "application/fhir+xml"}) | ||
public void testValidateGoodCases(String validMimeType) throws XDSMetaDataException { | ||
validator.validate(validMimeType); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"something", "application/test blank", "application/is/wrong"}) | ||
public void testValidateBadCases(String invalidMimeType) throws XDSMetaDataException { | ||
assertThrows(XDSMetaDataException.class, () -> validator.validate(invalidMimeType)); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters