diff --git a/dhp-common/pom.xml b/dhp-common/pom.xml index 6df11f4ea..58982ed11 100644 --- a/dhp-common/pom.xml +++ b/dhp-common/pom.xml @@ -166,6 +166,21 @@ com.opencsv opencsv + + + org.hibernate.validator + hibernate-validator + + + + javax.el + javax.el-api + + + org.glassfish + javax.el + + diff --git a/dhp-common/src/main/java/eu/dnetlib/dhp/schema/oaf/utils/ConstraintViolation.java b/dhp-common/src/main/java/eu/dnetlib/dhp/schema/oaf/utils/ConstraintViolation.java new file mode 100644 index 000000000..943043598 --- /dev/null +++ b/dhp-common/src/main/java/eu/dnetlib/dhp/schema/oaf/utils/ConstraintViolation.java @@ -0,0 +1,35 @@ +package eu.dnetlib.dhp.schema.oaf.utils; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +public class ConstraintViolation implements Serializable { + + private Map properties = new HashMap<>(); + + public ConstraintViolation() {} + + /* + @JsonCreator + public ConstraintViolation( + @JsonProperty("path") final String path, + @JsonProperty("message") final String message) { + this.properties.put(path, message); + } + */ + + @JsonAnyGetter + public Map getProperties() { + return properties; + } + + @JsonAnySetter + public void add(String key, String value) { + properties.put(key, value); + } + +} diff --git a/dhp-common/src/main/java/eu/dnetlib/dhp/schema/oaf/utils/OafValidator.java b/dhp-common/src/main/java/eu/dnetlib/dhp/schema/oaf/utils/OafValidator.java new file mode 100644 index 000000000..d67166555 --- /dev/null +++ b/dhp-common/src/main/java/eu/dnetlib/dhp/schema/oaf/utils/OafValidator.java @@ -0,0 +1,32 @@ +package eu.dnetlib.dhp.schema.oaf.utils; + +import org.hibernate.validator.messageinterpolation.ParameterMessageInterpolator; + +import javax.validation.Validation; +import javax.validation.Validator; +import java.util.HashSet; +import java.util.Set; +import java.util.stream.Collectors; + +public class OafValidator { + + private static final Validator VALIDATOR = Validation + .byDefaultProvider() + .configure() + .messageInterpolator(new ParameterMessageInterpolator()) + .buildValidatorFactory() + .getValidator(); + + public static Set validate(final T oaf) { + return VALIDATOR.validate(oaf) + .stream() + .map(v -> { + final String path = v.getRootBeanClass().getSimpleName() + "." + v.getPropertyPath().toString(); + ConstraintViolation cv = new ConstraintViolation(); + cv.add(path, v.getMessage()); + return cv; + }) + .collect(Collectors.toCollection(HashSet::new)); + } + +} diff --git a/dhp-common/src/test/java/eu/dnetlib/dhp/schema/oaf/utils/OafValidatorTest.java b/dhp-common/src/test/java/eu/dnetlib/dhp/schema/oaf/utils/OafValidatorTest.java new file mode 100644 index 000000000..0fda10c43 --- /dev/null +++ b/dhp-common/src/test/java/eu/dnetlib/dhp/schema/oaf/utils/OafValidatorTest.java @@ -0,0 +1,46 @@ + +package eu.dnetlib.dhp.schema.oaf.utils; + +import java.io.IOException; +import java.util.Set; + +import com.fasterxml.jackson.core.type.TypeReference; +import org.apache.commons.io.IOUtils; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import eu.dnetlib.dhp.schema.oaf.Publication; + +import static org.junit.jupiter.api.Assertions.*; + +public class OafValidatorTest { + + @Test + void test_validate() throws IOException { + ObjectMapper mapper = new ObjectMapper(); + String json = IOUtils.toString(getClass().getResourceAsStream("publication_3.json")); + + Publication p = mapper.readValue(json, Publication.class); + + Set res = OafValidator.validate(p); + + assertNotNull(res); + assertFalse(res.isEmpty()); + + res.forEach(v -> { + System.out.println(v.getProperties()); + }); + + String reportJson = mapper.writeValueAsString(res); + assertNotNull(reportJson); + + Set report = mapper.readValue(reportJson, new TypeReference>() { }); + + assertNotNull(report); + + System.out.println(mapper.writeValueAsString(report)); + } + +} diff --git a/dhp-common/src/test/resources/eu/dnetlib/dhp/schema/oaf/utils/publication_3.json b/dhp-common/src/test/resources/eu/dnetlib/dhp/schema/oaf/utils/publication_3.json new file mode 100644 index 000000000..8f76f69c0 --- /dev/null +++ b/dhp-common/src/test/resources/eu/dnetlib/dhp/schema/oaf/utils/publication_3.json @@ -0,0 +1,41 @@ +{ + "id": "50|DansKnawCris::0829b5191605bdbea36d6502b8c1ce1f", + "resulttype": { + "classid": "publication" + }, + "pid": [ + { + "qualifier": { + "classid": "doi" + }, + "value": "10.1016/j.cmet.2011.03.013" + }, + { + "qualifier": { + "classid": "urn" + }, + "value": "urn:nbn:nl:ui:29-f3ed5f9e-edf6-457e-8848-61b58a4075e2" + }, + { + "qualifier": { + "classid": "scp-number" + }, + "value": "79953761260" + }, + { + "qualifier": { + "classid": "pmc" + }, + "value": "21459329" + } + ], + "collectedfrom": [ + { + "key": "", + "value": "" + } + ], + "instance": [ + {} + ] +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 136b9b867..fb22d27ed 100644 --- a/pom.xml +++ b/pom.xml @@ -531,6 +531,23 @@ 4.8.71 + + org.hibernate.validator + hibernate-validator + 6.0.13.Final + + + + javax.el + javax.el-api + 3.0.0 + + + org.glassfish + javax.el + 3.0.0 + + @@ -801,7 +818,7 @@ 3.3.3 3.4.2 [2.12,3.0) - [2.10.32] + [2.10.26-SNAPSHOT] [4.0.3] [6.0.5] [3.1.6]